]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/tunelp.c
lib: fix strutils.h, remove STRTOXX_EXIT_CODE
[thirdparty/util-linux.git] / sys-utils / tunelp.c
1 /*
2 * Copyright (C) 1992-1997 Michael K. Johnson, johnsonm@redhat.com
3 *
4 * This file is licensed under the terms of the GNU General Public
5 * License, version 2, or any later version. See file COPYING for
6 * information on distribution conditions.
7 */
8
9 /*
10 * This command is deprecated. The utility is in maintenance mode,
11 * meaning we keep them in source tree for backward compatibility
12 * only. Do not waste time making this command better, unless the
13 * fix is about security or other very critical issue.
14 *
15 * See Documentation/deprecated.txt for more information.
16 */
17
18 /*
19 * $Log: tunelp.c,v $
20 * Revision 1.9 1998/06/08 19:37:11 janl
21 * Thus compiles tunelp with 2.1.103 kernels
22 *
23 * Revision 1.8 1997/07/06 00:14:06 aebr
24 * Fixes to silence -Wall.
25 *
26 * Revision 1.7 1997/06/20 16:10:38 janl
27 * tunelp refreshed from authors archive.
28 *
29 * Revision 1.9 1997/06/20 12:56:43 johnsonm
30 * Finished fixing license terms.
31 *
32 * Revision 1.8 1997/06/20 12:34:59 johnsonm
33 * Fixed copyright and license.
34 *
35 * Revision 1.7 1995/03/29 11:16:23 johnsonm
36 * TYPO fixed...
37 *
38 * Revision 1.6 1995/03/29 11:12:15 johnsonm
39 * Added third argument to ioctl needed with new kernels
40 *
41 * Revision 1.5 1995/01/13 10:33:43 johnsonm
42 * Chris's changes for new ioctl numbers and backwards compatibility
43 * and the reset ioctl.
44 *
45 * Revision 1.4 1995/01/03 17:42:14 johnsonm
46 * -s isn't supposed to take an argument; removed : after s in getopt...
47 *
48 * Revision 1.3 1995/01/03 07:36:49 johnsonm
49 * Fixed typo
50 *
51 * Revision 1.2 1995/01/03 07:33:44 johnsonm
52 * revisions for lp driver updates in Linux 1.1.76
53 *
54 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
55 * - added Native Language Support
56 *
57 * 1999-05-07 Merged LPTRUSTIRQ patch by Andrea Arcangeli (1998/11/29), aeb
58 *
59 */
60
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <getopt.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <sys/ioctl.h>
68 #include <sys/stat.h>
69 #include <sys/types.h>
70 #include <unistd.h>
71
72 #include "lp.h"
73 #include "nls.h"
74 #include "closestream.h"
75 #include "strutils.h"
76
77 #define EXIT_LP_MALLOC 2
78 #define EXIT_LP_BADVAL 3
79 #define EXIT_LP_IO_ERR 4
80
81 #define XALLOC_EXIT_CODE EXIT_LP_MALLOC
82 #include "xalloc.h"
83
84 struct command {
85 long op;
86 long val;
87 struct command *next;
88 };
89
90 static void __attribute__((__noreturn__)) print_usage(FILE *out)
91 {
92 fputs(USAGE_HEADER, out);
93 fprintf(out, _(" %s [options] <device>\n"), program_invocation_short_name);
94
95 fputs(USAGE_SEPARATOR, out);
96 fputs(_("Set various parameters for the line printer.\n"), out);
97
98 fputs(USAGE_OPTIONS, out);
99 fputs(_(" -i, --irq <num> specify parallel port irq\n"), out);
100 fputs(_(" -t, --time <ms> driver wait time in milliseconds\n"), out);
101 fputs(_(" -c, --chars <num> number of output characters before sleep\n"), out);
102 fputs(_(" -w, --wait <us> strobe wait in micro seconds\n"), out);
103 /* TRANSLATORS: do not translate <on|off> arguments. The
104 argument reader does not recognize locale, unless `on' is
105 exactly that very same string. */
106 fputs(_(" -a, --abort <on|off> abort on error\n"), out);
107 fputs(_(" -o, --check-status <on|off> check printer status before printing\n"), out);
108 fputs(_(" -C, --careful <on|off> extra checking to status check\n"), out);
109 fputs(_(" -s, --status query printer status\n"), out);
110 fputs(_(" -T, --trust-irq <on|off> make driver to trust irq\n"), out);
111 fputs(_(" -r, --reset reset the port\n"), out);
112 fputs(_(" -q, --print-irq <on|off> display current irq setting\n"), out);
113 fputs(USAGE_SEPARATOR, out);
114 fputs(USAGE_HELP, out);
115 fputs(USAGE_VERSION, out);
116 fprintf(out, USAGE_MAN_TAIL("tunelp(8)"));
117
118 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
119 }
120
121 int main(int argc, char **argv)
122 {
123 int c, fd, irq, status, show_irq, offset = 0, retval;
124 char *filename;
125 struct stat statbuf;
126 struct command *cmds, *cmdst;
127 static const struct option longopts[] = {
128 {"irq", required_argument, NULL, 'i'},
129 {"time", required_argument, NULL, 't'},
130 {"chars", required_argument, NULL, 'c'},
131 {"wait", required_argument, NULL, 'w'},
132 {"abort", required_argument, NULL, 'a'},
133 {"check-status", required_argument, NULL, 'o'},
134 {"careful", required_argument, NULL, 'C'},
135 {"status", no_argument, NULL, 's'},
136 {"trust-irq", required_argument, NULL, 'T'},
137 {"reset", no_argument, NULL, 'r'},
138 {"print-irq", required_argument, NULL, 'q'},
139 {"version", no_argument, NULL, 'V'},
140 {"help", no_argument, NULL, 'h'},
141 {NULL, 0, NULL, 0}
142 };
143
144 setlocale(LC_ALL, "");
145 bindtextdomain(PACKAGE, LOCALEDIR);
146 textdomain(PACKAGE);
147 atexit(close_stdout);
148
149 strutils_set_exitcode(EXIT_LP_BADVAL);
150
151 if (argc < 2)
152 print_usage(stderr);
153
154 cmdst = cmds = xmalloc(sizeof(struct command));
155 cmds->next = NULL;
156
157 show_irq = 1;
158 while ((c = getopt_long(argc, argv, "t:c:w:a:i:ho:C:sq:rT:vV", longopts, NULL)) != -1) {
159 switch (c) {
160 case 'h':
161 print_usage(stdout);
162 break;
163 case 'i':
164 cmds->op = LPSETIRQ;
165 cmds->val = strtol_or_err(optarg, _("argument error"));
166 cmds->next = xmalloc(sizeof(struct command));
167 cmds = cmds->next;
168 cmds->next = NULL;
169 break;
170 case 't':
171 cmds->op = LPTIME;
172 cmds->val = strtol_or_err(optarg, _("argument error"));
173 cmds->next = xmalloc(sizeof(struct command));
174 cmds = cmds->next;
175 cmds->next = NULL;
176 break;
177 case 'c':
178 cmds->op = LPCHAR;
179 cmds->val = strtol_or_err(optarg, _("argument error"));
180 cmds->next = xmalloc(sizeof(struct command));
181 cmds = cmds->next;
182 cmds->next = NULL;
183 break;
184 case 'w':
185 cmds->op = LPWAIT;
186 cmds->val = strtol_or_err(optarg, _("argument error"));
187 cmds->next = xmalloc(sizeof(struct command));
188 cmds = cmds->next;
189 cmds->next = NULL;
190 break;
191 case 'a':
192 cmds->op = LPABORT;
193 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
194 cmds->next = xmalloc(sizeof(struct command));
195 cmds = cmds->next;
196 cmds->next = NULL;
197 break;
198 case 'q':
199 show_irq = parse_switch(optarg, _("argument error"), "on", "off", NULL);
200 break;
201 case 'o':
202 cmds->op = LPABORTOPEN;
203 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
204 cmds->next = xmalloc(sizeof(struct command));
205 cmds = cmds->next;
206 cmds->next = NULL;
207 break;
208 case 'C':
209 cmds->op = LPCAREFUL;
210 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
211 cmds->next = xmalloc(sizeof(struct command));
212 cmds = cmds->next;
213 cmds->next = NULL;
214 break;
215 case 's':
216 show_irq = 0;
217 cmds->op = LPGETSTATUS;
218 cmds->val = 0;
219 cmds->next = xmalloc(sizeof(struct command));
220 cmds = cmds->next;
221 cmds->next = NULL;
222 break;
223 case 'r':
224 cmds->op = LPRESET;
225 cmds->val = 0;
226 cmds->next = xmalloc(sizeof(struct command));
227 cmds = cmds->next;
228 cmds->next = NULL;
229 break;
230 case 'T':
231 /* Note: this will do the wrong thing on
232 * 2.0.36 when compiled under 2.2.x
233 */
234 cmds->op = LPTRUSTIRQ;
235 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
236 cmds->next = xmalloc(sizeof(struct command));
237 cmds = cmds->next;
238 cmds->next = NULL;
239 break;
240 case 'v':
241 case 'V':
242 printf(UTIL_LINUX_VERSION);
243 return EXIT_SUCCESS;
244 default:
245 errtryhelp(EXIT_FAILURE);
246 }
247 }
248
249 if (optind != argc - 1)
250 print_usage(stderr);
251
252 filename = xstrdup(argv[optind]);
253 fd = open(filename, O_WRONLY | O_NONBLOCK, 0);
254 /* Need to open O_NONBLOCK in case ABORTOPEN is already set
255 * and printer is off or off-line or in an error condition.
256 * Otherwise we would abort...
257 */
258 if (fd < 0)
259 err(EXIT_FAILURE, "%s", filename);
260
261 if (fstat(fd, &statbuf))
262 err(EXIT_FAILURE, "%s: stat() failed", filename);
263
264 if (!S_ISCHR(statbuf.st_mode)) {
265 warnx(_("%s not an lp device"), filename);
266 print_usage(stderr);
267 }
268 /* Allow for binaries compiled under a new kernel to work on
269 * the old ones The irq argument to ioctl isn't touched by
270 * the old kernels, but we don't want to cause the kernel to
271 * complain if we are using a new kernel
272 */
273 if (LPGETIRQ >= 0x0600 && ioctl(fd, LPGETIRQ, &irq) < 0
274 && errno == EINVAL)
275 /* We don't understand the new ioctls */
276 offset = 0x0600;
277
278 cmds = cmdst;
279 while (cmds->next) {
280 if (cmds->op == LPGETSTATUS) {
281 status = 0xdeadbeef;
282 retval = ioctl(fd, LPGETSTATUS - offset, &status);
283 if (retval < 0)
284 warnx(_("LPGETSTATUS error"));
285 else {
286 if (status == (int)0xdeadbeef)
287 /* a few 1.1.7x kernels will do this */
288 status = retval;
289 printf(_("%s status is %d"), filename, status);
290 if (!(status & LP_PBUSY))
291 printf(_(", busy"));
292 if (!(status & LP_PACK))
293 printf(_(", ready"));
294 if ((status & LP_POUTPA))
295 printf(_(", out of paper"));
296 if ((status & LP_PSELECD))
297 printf(_(", on-line"));
298 if (!(status & LP_PERRORP))
299 printf(_(", error"));
300 printf("\n");
301 }
302 } else
303 if (ioctl(fd, cmds->op - offset, cmds->val) < 0)
304 warn(_("ioctl failed"));
305 cmdst = cmds;
306 cmds = cmds->next;
307 free(cmdst);
308 }
309
310 if (show_irq) {
311 irq = 0xdeadbeef;
312 retval = ioctl(fd, LPGETIRQ - offset, &irq);
313 if (retval == -1)
314 err(EXIT_LP_IO_ERR, _("LPGETIRQ error"));
315 if (irq == (int)0xdeadbeef)
316 /* up to 1.1.77 will do this */
317 irq = retval;
318 if (irq)
319 printf(_("%s using IRQ %d\n"), filename, irq);
320 else
321 printf(_("%s using polling\n"), filename);
322 }
323 free(filename);
324 close(fd);
325
326 return EXIT_SUCCESS;
327 }