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