]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/tunelp.c
blockdev: correct man page name in --help
[thirdparty/util-linux.git] / sys-utils / tunelp.c
CommitLineData
61319c6e
SK
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 */
6dbe3af9 8
7d2600e2
SK
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
d3e8f659 18/*
726f69e2 19 * $Log: tunelp.c,v $
2b6fc908
KZ
20 * Revision 1.9 1998/06/08 19:37:11 janl
21 * Thus compiles tunelp with 2.1.103 kernels
22 *
fd6b7a7f
KZ
23 * Revision 1.8 1997/07/06 00:14:06 aebr
24 * Fixes to silence -Wall.
726f69e2 25 *
fd6b7a7f
KZ
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
6dbe3af9
KZ
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 *
b50945d4 54 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
7eda085c
KZ
55 * - added Native Language Support
56 *
57 * 1999-05-07 Merged LPTRUSTIRQ patch by Andrea Arcangeli (1998/11/29), aeb
6dbe3af9
KZ
58 *
59 */
60
61319c6e 61#include <errno.h>
7eda085c 62#include <fcntl.h>
a21108ba 63#include <getopt.h>
61319c6e 64#include <stdio.h>
66ee8158 65#include <stdlib.h>
7eda085c 66#include <string.h>
61319c6e
SK
67#include <sys/ioctl.h>
68#include <sys/stat.h>
69#include <sys/types.h>
70#include <unistd.h>
2ab428f6 71
7eda085c
KZ
72#include "lp.h"
73#include "nls.h"
efb8854f 74#include "closestream.h"
9c8b9fba 75#include "strutils.h"
6dbe3af9 76
05691d9e 77#define EXIT_LP_MALLOC 2
9c8b9fba 78#define EXIT_LP_BADVAL 3
7e3c80a7
SK
79#define EXIT_LP_IO_ERR 4
80
05691d9e
RM
81#define XALLOC_EXIT_CODE EXIT_LP_MALLOC
82#include "xalloc.h"
83
6dbe3af9 84struct command {
61319c6e
SK
85 long op;
86 long val;
87 struct command *next;
6dbe3af9
KZ
88};
89
6e1eda6f 90static void __attribute__((__noreturn__)) usage(void)
61319c6e 91{
6e1eda6f 92 FILE *out = stdout;
a21108ba
SK
93 fputs(USAGE_HEADER, out);
94 fprintf(out, _(" %s [options] <device>\n"), program_invocation_short_name);
6dbe3af9 95
451dbcfa
BS
96 fputs(USAGE_SEPARATOR, out);
97 fputs(_("Set various parameters for the line printer.\n"), out);
98
a21108ba
SK
99 fputs(USAGE_OPTIONS, out);
100 fputs(_(" -i, --irq <num> specify parallel port irq\n"), out);
101 fputs(_(" -t, --time <ms> driver wait time in milliseconds\n"), out);
102 fputs(_(" -c, --chars <num> number of output characters before sleep\n"), out);
103 fputs(_(" -w, --wait <us> strobe wait in micro seconds\n"), out);
104 /* TRANSLATORS: do not translate <on|off> arguments. The
105 argument reader does not recognize locale, unless `on' is
106 exactly that very same string. */
107 fputs(_(" -a, --abort <on|off> abort on error\n"), out);
108 fputs(_(" -o, --check-status <on|off> check printer status before printing\n"), out);
109 fputs(_(" -C, --careful <on|off> extra checking to status check\n"), out);
110 fputs(_(" -s, --status query printer status\n"), out);
111 fputs(_(" -T, --trust-irq <on|off> make driver to trust irq\n"), out);
112 fputs(_(" -r, --reset reset the port\n"), out);
113 fputs(_(" -q, --print-irq <on|off> display current irq setting\n"), out);
114 fputs(USAGE_SEPARATOR, out);
b1a294c4 115 print_usage_help_options(30);
a21108ba
SK
116 fprintf(out, USAGE_MAN_TAIL("tunelp(8)"));
117
6e1eda6f 118 exit(EXIT_SUCCESS);
7eda085c 119}
6dbe3af9 120
61319c6e
SK
121int main(int argc, char **argv)
122{
123 int c, fd, irq, status, show_irq, offset = 0, retval;
915b4bac 124 char *filename;
61319c6e
SK
125 struct stat statbuf;
126 struct command *cmds, *cmdst;
a21108ba
SK
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 };
6dbe3af9 143
61319c6e
SK
144 setlocale(LC_ALL, "");
145 bindtextdomain(PACKAGE, LOCALEDIR);
146 textdomain(PACKAGE);
efb8854f 147 atexit(close_stdout);
eb63b9b8 148
9c8b9fba
RM
149 strutils_set_exitcode(EXIT_LP_BADVAL);
150
6e1eda6f
RM
151 if (argc < 2) {
152 warnx(_("not enough arguments"));
153 errtryhelp(EXIT_FAILURE);
154 }
6dbe3af9 155
61319c6e 156 cmdst = cmds = xmalloc(sizeof(struct command));
87918040 157 cmds->next = NULL;
6dbe3af9 158
61319c6e 159 show_irq = 1;
a21108ba 160 while ((c = getopt_long(argc, argv, "t:c:w:a:i:ho:C:sq:rT:vV", longopts, NULL)) != -1) {
61319c6e
SK
161 switch (c) {
162 case 'h':
6e1eda6f 163 usage();
61319c6e
SK
164 break;
165 case 'i':
166 cmds->op = LPSETIRQ;
7e3c80a7 167 cmds->val = strtol_or_err(optarg, _("argument error"));
61319c6e
SK
168 cmds->next = xmalloc(sizeof(struct command));
169 cmds = cmds->next;
87918040 170 cmds->next = NULL;
61319c6e
SK
171 break;
172 case 't':
173 cmds->op = LPTIME;
7e3c80a7 174 cmds->val = strtol_or_err(optarg, _("argument error"));
61319c6e
SK
175 cmds->next = xmalloc(sizeof(struct command));
176 cmds = cmds->next;
87918040 177 cmds->next = NULL;
61319c6e
SK
178 break;
179 case 'c':
180 cmds->op = LPCHAR;
7e3c80a7 181 cmds->val = strtol_or_err(optarg, _("argument error"));
61319c6e
SK
182 cmds->next = xmalloc(sizeof(struct command));
183 cmds = cmds->next;
87918040 184 cmds->next = NULL;
61319c6e
SK
185 break;
186 case 'w':
187 cmds->op = LPWAIT;
7e3c80a7 188 cmds->val = strtol_or_err(optarg, _("argument error"));
61319c6e
SK
189 cmds->next = xmalloc(sizeof(struct command));
190 cmds = cmds->next;
87918040 191 cmds->next = NULL;
61319c6e
SK
192 break;
193 case 'a':
194 cmds->op = LPABORT;
7e5dd0e5 195 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
61319c6e
SK
196 cmds->next = xmalloc(sizeof(struct command));
197 cmds = cmds->next;
87918040 198 cmds->next = NULL;
61319c6e
SK
199 break;
200 case 'q':
7e5dd0e5 201 show_irq = parse_switch(optarg, _("argument error"), "on", "off", NULL);
a291c7ff 202 break;
61319c6e
SK
203 case 'o':
204 cmds->op = LPABORTOPEN;
7e5dd0e5 205 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
61319c6e
SK
206 cmds->next = xmalloc(sizeof(struct command));
207 cmds = cmds->next;
87918040 208 cmds->next = NULL;
61319c6e
SK
209 break;
210 case 'C':
211 cmds->op = LPCAREFUL;
7e5dd0e5 212 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
61319c6e
SK
213 cmds->next = xmalloc(sizeof(struct command));
214 cmds = cmds->next;
87918040 215 cmds->next = NULL;
61319c6e
SK
216 break;
217 case 's':
218 show_irq = 0;
219 cmds->op = LPGETSTATUS;
220 cmds->val = 0;
221 cmds->next = xmalloc(sizeof(struct command));
222 cmds = cmds->next;
87918040 223 cmds->next = NULL;
61319c6e 224 break;
61319c6e
SK
225 case 'r':
226 cmds->op = LPRESET;
227 cmds->val = 0;
228 cmds->next = xmalloc(sizeof(struct command));
229 cmds = cmds->next;
87918040 230 cmds->next = NULL;
61319c6e 231 break;
61319c6e
SK
232 case 'T':
233 /* Note: this will do the wrong thing on
234 * 2.0.36 when compiled under 2.2.x
235 */
236 cmds->op = LPTRUSTIRQ;
7e5dd0e5 237 cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
61319c6e
SK
238 cmds->next = xmalloc(sizeof(struct command));
239 cmds = cmds->next;
87918040 240 cmds->next = NULL;
61319c6e 241 break;
61319c6e
SK
242 case 'v':
243 case 'V':
f6277500 244 printf(UTIL_LINUX_VERSION);
130314aa 245 return EXIT_SUCCESS;
61319c6e 246 default:
677ec86c 247 errtryhelp(EXIT_FAILURE);
61319c6e
SK
248 }
249 }
7eda085c 250
6e1eda6f
RM
251 if (optind != argc - 1) {
252 warnx(_("no device specified"));
253 errtryhelp(EXIT_FAILURE);
254 }
7eda085c 255
0b077eab 256 filename = xstrdup(argv[optind]);
61319c6e
SK
257 fd = open(filename, O_WRONLY | O_NONBLOCK, 0);
258 /* Need to open O_NONBLOCK in case ABORTOPEN is already set
259 * and printer is off or off-line or in an error condition.
260 * Otherwise we would abort...
261 */
915b4bac
SK
262 if (fd < 0)
263 err(EXIT_FAILURE, "%s", filename);
7eda085c 264
97209c2b
KZ
265 if (fstat(fd, &statbuf))
266 err(EXIT_FAILURE, "%s: stat() failed", filename);
7eda085c 267
61319c6e 268 if (!S_ISCHR(statbuf.st_mode)) {
915b4bac 269 warnx(_("%s not an lp device"), filename);
6e1eda6f 270 errtryhelp(EXIT_FAILURE);
61319c6e
SK
271 }
272 /* Allow for binaries compiled under a new kernel to work on
273 * the old ones The irq argument to ioctl isn't touched by
274 * the old kernels, but we don't want to cause the kernel to
275 * complain if we are using a new kernel
276 */
277 if (LPGETIRQ >= 0x0600 && ioctl(fd, LPGETIRQ, &irq) < 0
278 && errno == EINVAL)
279 /* We don't understand the new ioctls */
280 offset = 0x0600;
6dbe3af9 281
61319c6e
SK
282 cmds = cmdst;
283 while (cmds->next) {
61319c6e
SK
284 if (cmds->op == LPGETSTATUS) {
285 status = 0xdeadbeef;
286 retval = ioctl(fd, LPGETSTATUS - offset, &status);
287 if (retval < 0)
915b4bac 288 warnx(_("LPGETSTATUS error"));
61319c6e
SK
289 else {
290 if (status == (int)0xdeadbeef)
291 /* a few 1.1.7x kernels will do this */
292 status = retval;
293 printf(_("%s status is %d"), filename, status);
294 if (!(status & LP_PBUSY))
295 printf(_(", busy"));
296 if (!(status & LP_PACK))
297 printf(_(", ready"));
298 if ((status & LP_POUTPA))
299 printf(_(", out of paper"));
300 if ((status & LP_PSELECD))
301 printf(_(", on-line"));
302 if (!(status & LP_PERRORP))
303 printf(_(", error"));
304 printf("\n");
305 }
306 } else
915b4bac
SK
307 if (ioctl(fd, cmds->op - offset, cmds->val) < 0)
308 warn(_("ioctl failed"));
61319c6e
SK
309 cmdst = cmds;
310 cmds = cmds->next;
311 free(cmdst);
312 }
6dbe3af9 313
61319c6e
SK
314 if (show_irq) {
315 irq = 0xdeadbeef;
316 retval = ioctl(fd, LPGETIRQ - offset, &irq);
915b4bac
SK
317 if (retval == -1)
318 err(EXIT_LP_IO_ERR, _("LPGETIRQ error"));
61319c6e
SK
319 if (irq == (int)0xdeadbeef)
320 /* up to 1.1.77 will do this */
321 irq = retval;
322 if (irq)
323 printf(_("%s using IRQ %d\n"), filename, irq);
324 else
325 printf(_("%s using polling\n"), filename);
326 }
0b077eab 327 free(filename);
61319c6e 328 close(fd);
6dbe3af9 329
130314aa 330 return EXIT_SUCCESS;
6dbe3af9 331}