2 * Copyright (C) 1992-1997 Michael K. Johnson, johnsonm@redhat.com
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.
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.
15 * See Documentation/deprecated.txt for more information.
20 * Revision 1.9 1998/06/08 19:37:11 janl
21 * Thus compiles tunelp with 2.1.103 kernels
23 * Revision 1.8 1997/07/06 00:14:06 aebr
24 * Fixes to silence -Wall.
26 * Revision 1.7 1997/06/20 16:10:38 janl
27 * tunelp refreshed from authors archive.
29 * Revision 1.9 1997/06/20 12:56:43 johnsonm
30 * Finished fixing license terms.
32 * Revision 1.8 1997/06/20 12:34:59 johnsonm
33 * Fixed copyright and license.
35 * Revision 1.7 1995/03/29 11:16:23 johnsonm
38 * Revision 1.6 1995/03/29 11:12:15 johnsonm
39 * Added third argument to ioctl needed with new kernels
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.
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...
48 * Revision 1.3 1995/01/03 07:36:49 johnsonm
51 * Revision 1.2 1995/01/03 07:33:44 johnsonm
52 * revisions for lp driver updates in Linux 1.1.76
54 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
55 * - added Native Language Support
57 * 1999-05-07 Merged LPTRUSTIRQ patch by Andrea Arcangeli (1998/11/29), aeb
67 #include <sys/ioctl.h>
69 #include <sys/types.h>
74 #include "closestream.h"
77 #define EXIT_LP_MALLOC 2
78 #define EXIT_LP_BADVAL 3
79 #define EXIT_LP_IO_ERR 4
81 #define XALLOC_EXIT_CODE EXIT_LP_MALLOC
90 static void __attribute__((__noreturn__
)) print_usage(FILE *out
)
92 fputs(USAGE_HEADER
, out
);
93 fprintf(out
, _(" %s [options] <device>\n"), program_invocation_short_name
);
95 fputs(USAGE_SEPARATOR
, out
);
96 fputs(_("Set various parameters for the line printer.\n"), out
);
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)"));
118 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
121 int main(int argc
, char **argv
)
123 int c
, fd
, irq
, status
, show_irq
, offset
= 0, retval
;
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'},
144 setlocale(LC_ALL
, "");
145 bindtextdomain(PACKAGE
, LOCALEDIR
);
147 atexit(close_stdout
);
149 strutils_set_exitcode(EXIT_LP_BADVAL
);
154 cmdst
= cmds
= xmalloc(sizeof(struct command
));
158 while ((c
= getopt_long(argc
, argv
, "t:c:w:a:i:ho:C:sq:rT:vV", longopts
, NULL
)) != -1) {
165 cmds
->val
= strtol_or_err(optarg
, _("argument error"));
166 cmds
->next
= xmalloc(sizeof(struct command
));
172 cmds
->val
= strtol_or_err(optarg
, _("argument error"));
173 cmds
->next
= xmalloc(sizeof(struct command
));
179 cmds
->val
= strtol_or_err(optarg
, _("argument error"));
180 cmds
->next
= xmalloc(sizeof(struct command
));
186 cmds
->val
= strtol_or_err(optarg
, _("argument error"));
187 cmds
->next
= xmalloc(sizeof(struct command
));
193 cmds
->val
= parse_switch(optarg
, _("argument error"), "on", "off", NULL
);
194 cmds
->next
= xmalloc(sizeof(struct command
));
199 show_irq
= parse_switch(optarg
, _("argument error"), "on", "off", NULL
);
202 cmds
->op
= LPABORTOPEN
;
203 cmds
->val
= parse_switch(optarg
, _("argument error"), "on", "off", NULL
);
204 cmds
->next
= xmalloc(sizeof(struct command
));
209 cmds
->op
= LPCAREFUL
;
210 cmds
->val
= parse_switch(optarg
, _("argument error"), "on", "off", NULL
);
211 cmds
->next
= xmalloc(sizeof(struct command
));
217 cmds
->op
= LPGETSTATUS
;
219 cmds
->next
= xmalloc(sizeof(struct command
));
226 cmds
->next
= xmalloc(sizeof(struct command
));
231 /* Note: this will do the wrong thing on
232 * 2.0.36 when compiled under 2.2.x
234 cmds
->op
= LPTRUSTIRQ
;
235 cmds
->val
= parse_switch(optarg
, _("argument error"), "on", "off", NULL
);
236 cmds
->next
= xmalloc(sizeof(struct command
));
242 printf(UTIL_LINUX_VERSION
);
245 errtryhelp(EXIT_FAILURE
);
249 if (optind
!= argc
- 1)
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...
259 err(EXIT_FAILURE
, "%s", filename
);
261 if (fstat(fd
, &statbuf
))
262 err(EXIT_FAILURE
, "%s: stat() failed", filename
);
264 if (!S_ISCHR(statbuf
.st_mode
)) {
265 warnx(_("%s not an lp device"), filename
);
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
273 if (LPGETIRQ
>= 0x0600 && ioctl(fd
, LPGETIRQ
, &irq
) < 0
275 /* We don't understand the new ioctls */
280 if (cmds
->op
== LPGETSTATUS
) {
282 retval
= ioctl(fd
, LPGETSTATUS
- offset
, &status
);
284 warnx(_("LPGETSTATUS error"));
286 if (status
== (int)0xdeadbeef)
287 /* a few 1.1.7x kernels will do this */
289 printf(_("%s status is %d"), filename
, status
);
290 if (!(status
& LP_PBUSY
))
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"));
303 if (ioctl(fd
, cmds
->op
- offset
, cmds
->val
) < 0)
304 warn(_("ioctl failed"));
312 retval
= ioctl(fd
, LPGETIRQ
- offset
, &irq
);
314 err(EXIT_LP_IO_ERR
, _("LPGETIRQ error"));
315 if (irq
== (int)0xdeadbeef)
316 /* up to 1.1.77 will do this */
319 printf(_("%s using IRQ %d\n"), filename
, irq
);
321 printf(_("%s using polling\n"), filename
);