]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7594b224 | 2 | * "$Id: lp.c 6356 2007-03-19 13:54:48Z mike $" |
ef416fc2 | 3 | * |
4 | * "lp" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
7594b224 | 6 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Parse options and send files for printing. | |
27 | * restart_job() - Restart a job. | |
28 | * set_job_attrs() - Set job attributes. | |
29 | * sighandler() - Signal catcher for when we print from stdin... | |
30 | */ | |
31 | ||
32 | /* | |
33 | * Include necessary headers... | |
34 | */ | |
35 | ||
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <errno.h> | |
39 | #include <cups/string.h> | |
40 | #include <cups/cups.h> | |
41 | #include <cups/i18n.h> | |
42 | ||
43 | ||
44 | #ifndef WIN32 | |
45 | # include <unistd.h> | |
46 | # include <signal.h> | |
47 | ||
48 | ||
49 | /* | |
50 | * Local functions. | |
51 | */ | |
52 | ||
53 | void sighandler(int); | |
54 | #endif /* !WIN32 */ | |
fa73b229 | 55 | int restart_job(const char *command, int job_id); |
56 | int set_job_attrs(const char *command, int job_id, int num_options, | |
57 | cups_option_t *options); | |
ef416fc2 | 58 | |
59 | ||
60 | /* | |
61 | * Globals... | |
62 | */ | |
63 | ||
64 | char tempfile[1024]; /* Temporary file for printing from stdin */ | |
65 | ||
66 | ||
67 | /* | |
68 | * 'main()' - Parse options and send files for printing. | |
69 | */ | |
70 | ||
71 | int | |
72 | main(int argc, /* I - Number of command-line arguments */ | |
73 | char *argv[]) /* I - Command-line arguments */ | |
74 | { | |
75 | int i, j; /* Looping vars */ | |
76 | int job_id; /* Job ID */ | |
77 | char *printer, /* Printer name */ | |
78 | *instance, /* Instance name */ | |
79 | *val, /* Option value */ | |
80 | *title; /* Job title */ | |
81 | int priority; /* Job priority (1-100) */ | |
82 | int num_copies; /* Number of copies per file */ | |
83 | int num_files; /* Number of files to print */ | |
84 | const char *files[1000]; /* Files to print */ | |
85 | int num_dests; /* Number of destinations */ | |
86 | cups_dest_t *dests, /* Destinations */ | |
87 | *dest; /* Selected destination */ | |
88 | int num_options; /* Number of options */ | |
89 | cups_option_t *options; /* Options */ | |
fa73b229 | 90 | int end_options; /* No more options? */ |
ef416fc2 | 91 | int silent; /* Silent or verbose output? */ |
92 | char buffer[8192]; /* Copy buffer */ | |
fa73b229 | 93 | ssize_t bytes; /* Bytes copied */ |
94 | off_t filesize; /* Size of temp file */ | |
ef416fc2 | 95 | int temp; /* Temporary file descriptor */ |
96 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
97 | struct sigaction action; /* Signal action */ | |
98 | struct sigaction oldaction; /* Old signal action */ | |
99 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET*/ | |
100 | ||
101 | ||
102 | #ifdef __sun | |
103 | /* | |
104 | * Solaris does some rather strange things to re-queue remote print | |
105 | * jobs. On bootup, the "lp" command is run as "printd" to re-spool | |
106 | * any remote jobs in /var/spool/print. Since CUPS doesn't need this | |
107 | * nonsense, we just need to add the necessary check here to prevent | |
108 | * lp from causing boot problems... | |
109 | */ | |
110 | ||
111 | if ((val = strrchr(argv[0], '/')) != NULL) | |
112 | val ++; | |
113 | else | |
114 | val = argv[0]; | |
115 | ||
116 | if (!strcmp(val, "printd")) | |
117 | return (0); | |
118 | #endif /* __sun */ | |
119 | ||
07725fee | 120 | _cupsSetLocale(argv); |
d09495fa | 121 | |
ef416fc2 | 122 | silent = 0; |
123 | printer = NULL; | |
124 | num_dests = 0; | |
125 | dests = NULL; | |
126 | num_options = 0; | |
127 | options = NULL; | |
128 | num_files = 0; | |
129 | title = NULL; | |
130 | job_id = 0; | |
fa73b229 | 131 | end_options = 0; |
ef416fc2 | 132 | |
133 | for (i = 1; i < argc; i ++) | |
fa73b229 | 134 | if (argv[i][0] == '-' && argv[i][1] && !end_options) |
ef416fc2 | 135 | switch (argv[i][1]) |
136 | { | |
137 | case 'E' : /* Encrypt */ | |
138 | #ifdef HAVE_SSL | |
139 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
140 | #else | |
fa73b229 | 141 | _cupsLangPrintf(stderr, |
ef416fc2 | 142 | _("%s: Sorry, no encryption support compiled in!\n"), |
143 | argv[0]); | |
144 | #endif /* HAVE_SSL */ | |
145 | break; | |
146 | ||
fa73b229 | 147 | case 'U' : /* Username */ |
148 | if (argv[i][2] != '\0') | |
149 | cupsSetUser(argv[i] + 2); | |
150 | else | |
151 | { | |
152 | i ++; | |
153 | if (i >= argc) | |
154 | { | |
155 | _cupsLangPrintf(stderr, | |
156 | _("%s: Error - expected username after " | |
157 | "\'-U\' option!\n"), | |
158 | argv[0]); | |
159 | return (1); | |
160 | } | |
161 | ||
162 | cupsSetUser(argv[i]); | |
163 | } | |
164 | break; | |
165 | ||
ef416fc2 | 166 | case 'c' : /* Copy to spool dir (always enabled) */ |
167 | break; | |
168 | ||
169 | case 'd' : /* Destination printer or class */ | |
170 | if (argv[i][2] != '\0') | |
171 | printer = argv[i] + 2; | |
172 | else | |
173 | { | |
174 | i ++; | |
175 | ||
176 | if (i >= argc) | |
177 | { | |
fa73b229 | 178 | _cupsLangPrintf(stderr, |
179 | _("%s: Error - expected destination after " | |
180 | "\'-d\' option!\n"), | |
181 | argv[0]); | |
ef416fc2 | 182 | return (1); |
183 | } | |
184 | ||
185 | printer = argv[i]; | |
186 | } | |
187 | ||
188 | if ((instance = strrchr(printer, '/')) != NULL) | |
189 | *instance++ = '\0'; | |
190 | ||
191 | if (num_dests == 0) | |
192 | num_dests = cupsGetDests(&dests); | |
193 | ||
194 | if ((dest = cupsGetDest(printer, instance, num_dests, dests)) != NULL) | |
195 | { | |
196 | for (j = 0; j < dest->num_options; j ++) | |
197 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
198 | num_options = cupsAddOption(dest->options[j].name, | |
199 | dest->options[j].value, | |
200 | num_options, &options); | |
201 | } | |
202 | break; | |
203 | ||
204 | case 'f' : /* Form */ | |
205 | if (!argv[i][2]) | |
206 | { | |
207 | i ++; | |
208 | ||
209 | if (i >= argc) | |
210 | { | |
fa73b229 | 211 | _cupsLangPrintf(stderr, |
212 | _("%s: Error - expected form after \'-f\' " | |
213 | "option!\n"), | |
214 | argv[0]); | |
ef416fc2 | 215 | return (1); |
216 | } | |
217 | } | |
218 | ||
fa73b229 | 219 | _cupsLangPrintf(stderr, _("%s: Warning - form option ignored!\n"), |
220 | argv[0]); | |
ef416fc2 | 221 | break; |
222 | ||
223 | case 'h' : /* Destination host */ | |
224 | if (argv[i][2] != '\0') | |
225 | cupsSetServer(argv[i] + 2); | |
226 | else | |
227 | { | |
228 | i ++; | |
229 | ||
230 | if (i >= argc) | |
231 | { | |
fa73b229 | 232 | _cupsLangPrintf(stderr, |
233 | _("%s: Error - expected hostname after " | |
234 | "\'-h\' option!\n"), | |
235 | argv[0]); | |
ef416fc2 | 236 | return (1); |
237 | } | |
238 | ||
239 | cupsSetServer(argv[i]); | |
240 | } | |
241 | break; | |
242 | ||
243 | case 'i' : /* Change job */ | |
244 | if (argv[i][2]) | |
245 | val = argv[i] + 2; | |
246 | else | |
247 | { | |
248 | i ++; | |
249 | ||
250 | if (i >= argc) | |
251 | { | |
fa73b229 | 252 | _cupsLangPrintf(stderr, |
253 | _("%s: Expected job ID after \'-i\' option!\n"), | |
254 | argv[0]); | |
ef416fc2 | 255 | return (1); |
256 | } | |
257 | ||
258 | val = argv[i]; | |
259 | } | |
260 | ||
261 | if (num_files > 0) | |
262 | { | |
fa73b229 | 263 | _cupsLangPrintf(stderr, |
264 | _("%s: Error - cannot print files and alter " | |
265 | "jobs simultaneously!\n"), | |
266 | argv[0]); | |
ef416fc2 | 267 | return (1); |
268 | } | |
269 | ||
270 | if (strrchr(val, '-') != NULL) | |
271 | job_id = atoi(strrchr(val, '-') + 1); | |
272 | else | |
273 | job_id = atoi(val); | |
274 | ||
275 | if (job_id < 0) | |
276 | { | |
fa73b229 | 277 | _cupsLangPrintf(stderr, _("%s: Error - bad job ID!\n"), argv[0]); |
ef416fc2 | 278 | break; |
279 | } | |
280 | break; | |
281 | ||
282 | case 'm' : /* Send email when job is done */ | |
283 | #ifdef __sun | |
284 | case 'p' : /* Notify on completion */ | |
285 | #endif /* __sun */ | |
286 | case 'w' : /* Write to console or email */ | |
fa73b229 | 287 | { |
288 | char email[1024]; /* EMail address */ | |
289 | ||
290 | ||
291 | snprintf(email, sizeof(email), "mailto:%s@%s", cupsUser(), | |
757d2cad | 292 | httpGetHostname(NULL, buffer, sizeof(buffer))); |
7594b224 | 293 | num_options = cupsAddOption("notify-recipient-uri", email, |
fa73b229 | 294 | num_options, &options); |
295 | } | |
296 | ||
297 | silent = 1; | |
ef416fc2 | 298 | break; |
299 | ||
300 | case 'n' : /* Number of copies */ | |
301 | if (argv[i][2] != '\0') | |
302 | num_copies = atoi(argv[i] + 2); | |
303 | else | |
304 | { | |
305 | i ++; | |
306 | ||
307 | if (i >= argc) | |
308 | { | |
fa73b229 | 309 | _cupsLangPrintf(stderr, |
310 | _("%s: Error - expected copies after " | |
311 | "\'-n\' option!\n"), | |
312 | argv[0]); | |
ef416fc2 | 313 | return (1); |
314 | } | |
315 | ||
316 | num_copies = atoi(argv[i]); | |
317 | } | |
318 | ||
319 | sprintf(buffer, "%d", num_copies); | |
320 | num_options = cupsAddOption("copies", buffer, num_options, &options); | |
321 | break; | |
322 | ||
323 | case 'o' : /* Option */ | |
324 | if (argv[i][2] != '\0') | |
325 | num_options = cupsParseOptions(argv[i] + 2, num_options, &options); | |
326 | else | |
327 | { | |
328 | i ++; | |
329 | ||
330 | if (i >= argc) | |
331 | { | |
fa73b229 | 332 | _cupsLangPrintf(stderr, |
333 | _("%s: Error - expected option string after " | |
334 | "\'-o\' option!\n"), | |
335 | argv[0]); | |
ef416fc2 | 336 | return (1); |
337 | } | |
338 | ||
339 | num_options = cupsParseOptions(argv[i], num_options, &options); | |
340 | } | |
341 | break; | |
342 | ||
343 | #ifndef __sun | |
344 | case 'p' : /* Queue priority */ | |
345 | #endif /* !__sun */ | |
346 | case 'q' : /* Queue priority */ | |
347 | if (argv[i][2] != '\0') | |
348 | priority = atoi(argv[i] + 2); | |
349 | else | |
350 | { | |
351 | if ((i + 1) >= argc) | |
352 | { | |
fa73b229 | 353 | _cupsLangPrintf(stderr, |
354 | _("%s: Error - expected priority after " | |
355 | "\'-%c\' option!\n"), | |
356 | argv[0], argv[i][1]); | |
ef416fc2 | 357 | return (1); |
358 | } | |
359 | ||
360 | i ++; | |
361 | ||
362 | priority = atoi(argv[i]); | |
363 | } | |
364 | ||
365 | /* | |
366 | * For 100% Solaris compatibility, need to add: | |
367 | * | |
368 | * priority = 99 * (39 - priority) / 39 + 1; | |
369 | * | |
370 | * However, to keep CUPS lp the same across all platforms | |
371 | * we will break compatibility this far... | |
372 | */ | |
373 | ||
374 | if (priority < 1 || priority > 100) | |
375 | { | |
fa73b229 | 376 | _cupsLangPrintf(stderr, |
377 | _("%s: Error - priority must be between 1 and " | |
378 | "100.\n"), | |
379 | argv[0]); | |
ef416fc2 | 380 | return (1); |
381 | } | |
382 | ||
383 | sprintf(buffer, "%d", priority); | |
384 | num_options = cupsAddOption("job-priority", buffer, num_options, &options); | |
385 | break; | |
386 | ||
387 | case 's' : /* Silent */ | |
388 | silent = 1; | |
389 | break; | |
390 | ||
391 | case 't' : /* Title */ | |
392 | if (argv[i][2] != '\0') | |
393 | title = argv[i] + 2; | |
394 | else | |
395 | { | |
396 | i ++; | |
397 | ||
398 | if (i >= argc) | |
399 | { | |
fa73b229 | 400 | _cupsLangPrintf(stderr, |
401 | _("%s: Error - expected title after " | |
402 | "\'-t\' option!\n"), | |
403 | argv[0]); | |
ef416fc2 | 404 | return (1); |
405 | } | |
406 | ||
407 | title = argv[i]; | |
408 | } | |
409 | break; | |
410 | ||
411 | case 'y' : /* mode-list */ | |
412 | if (!argv[i][2]) | |
413 | { | |
414 | i ++; | |
415 | ||
416 | if (i >= argc) | |
417 | { | |
fa73b229 | 418 | _cupsLangPrintf(stderr, |
419 | _("%s: Error - expected mode list after " | |
420 | "\'-y\' option!\n"), | |
421 | argv[0]); | |
ef416fc2 | 422 | return (1); |
423 | } | |
424 | } | |
425 | ||
fa73b229 | 426 | _cupsLangPrintf(stderr, |
427 | _("%s: Warning - mode option ignored!\n"), | |
428 | argv[0]); | |
ef416fc2 | 429 | break; |
430 | ||
431 | case 'H' : /* Hold job */ | |
432 | if (argv[i][2]) | |
433 | val = argv[i] + 2; | |
434 | else | |
435 | { | |
436 | i ++; | |
437 | ||
438 | if (i >= argc) | |
439 | { | |
fa73b229 | 440 | _cupsLangPrintf(stderr, |
441 | _("%s: Error - expected hold name after " | |
442 | "\'-H\' option!\n"), | |
443 | argv[0]); | |
ef416fc2 | 444 | return (1); |
445 | } | |
446 | ||
447 | val = argv[i]; | |
448 | } | |
449 | ||
450 | if (!strcmp(val, "hold")) | |
451 | num_options = cupsAddOption("job-hold-until", "indefinite", | |
452 | num_options, &options); | |
453 | else if (!strcmp(val, "resume") || | |
454 | !strcmp(val, "release")) | |
455 | num_options = cupsAddOption("job-hold-until", "no-hold", | |
456 | num_options, &options); | |
457 | else if (!strcmp(val, "immediate")) | |
458 | num_options = cupsAddOption("job-priority", "100", | |
459 | num_options, &options); | |
460 | else if (!strcmp(val, "restart")) | |
461 | { | |
462 | if (job_id < 1) | |
463 | { | |
fa73b229 | 464 | _cupsLangPrintf(stderr, |
465 | _("%s: Need job ID (\'-i jobid\') before " | |
466 | "\'-H restart\'!\n"), | |
467 | argv[0]); | |
ef416fc2 | 468 | return (1); |
469 | } | |
470 | ||
fa73b229 | 471 | if (restart_job(argv[0], job_id)) |
ef416fc2 | 472 | return (1); |
473 | } | |
474 | else | |
475 | num_options = cupsAddOption("job-hold-until", val, | |
476 | num_options, &options); | |
477 | break; | |
478 | ||
479 | case 'P' : /* Page list */ | |
480 | if (argv[i][2]) | |
481 | val = argv[i] + 2; | |
482 | else | |
483 | { | |
484 | i ++; | |
485 | ||
486 | if (i >= argc) | |
487 | { | |
fa73b229 | 488 | _cupsLangPrintf(stderr, |
489 | _("%s: Error - expected page list after " | |
490 | "\'-P\' option!\n"), | |
491 | argv[0]); | |
ef416fc2 | 492 | return (1); |
493 | } | |
494 | ||
495 | val = argv[i]; | |
496 | } | |
497 | ||
498 | num_options = cupsAddOption("page-ranges", val, num_options, | |
499 | &options); | |
500 | break; | |
501 | ||
502 | case 'S' : /* character set */ | |
503 | if (!argv[i][2]) | |
504 | { | |
505 | i ++; | |
506 | ||
507 | if (i >= argc) | |
508 | { | |
fa73b229 | 509 | _cupsLangPrintf(stderr, |
510 | _("%s: Error - expected character set after " | |
511 | "\'-S\' option!\n"), | |
512 | argv[0]); | |
ef416fc2 | 513 | return (1); |
514 | } | |
515 | } | |
516 | ||
fa73b229 | 517 | _cupsLangPrintf(stderr, |
518 | _("%s: Warning - character set option ignored!\n"), | |
519 | argv[0]); | |
ef416fc2 | 520 | break; |
521 | ||
522 | case 'T' : /* Content-Type */ | |
523 | if (!argv[i][2]) | |
524 | { | |
525 | i ++; | |
526 | ||
527 | if (i >= argc) | |
528 | { | |
fa73b229 | 529 | _cupsLangPrintf(stderr, |
530 | _("%s: Error - expected content type after " | |
531 | "\'-T\' option!\n"), | |
532 | argv[0]); | |
ef416fc2 | 533 | return (1); |
534 | } | |
535 | } | |
536 | ||
fa73b229 | 537 | _cupsLangPrintf(stderr, |
538 | _("%s: Warning - content type option ignored!\n"), | |
539 | argv[0]); | |
540 | break; | |
541 | ||
542 | case '-' : /* Stop processing options */ | |
543 | end_options = 1; | |
ef416fc2 | 544 | break; |
545 | ||
546 | default : | |
fa73b229 | 547 | _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'!\n"), |
548 | argv[0], argv[i][1]); | |
ef416fc2 | 549 | return (1); |
550 | } | |
551 | else if (!strcmp(argv[i], "-")) | |
552 | { | |
553 | if (num_files || job_id) | |
554 | { | |
fa73b229 | 555 | _cupsLangPrintf(stderr, |
556 | _("%s: Error - cannot print from stdin if files or a " | |
557 | "job ID are provided!\n"), | |
558 | argv[0]); | |
ef416fc2 | 559 | return (1); |
560 | } | |
561 | ||
562 | break; | |
563 | } | |
564 | else if (num_files < 1000 && job_id == 0) | |
565 | { | |
566 | /* | |
567 | * Print a file... | |
568 | */ | |
569 | ||
570 | if (access(argv[i], R_OK) != 0) | |
571 | { | |
fa73b229 | 572 | _cupsLangPrintf(stderr, _("%s: Error - unable to access \"%s\" - %s\n"), |
573 | argv[0], argv[i], strerror(errno)); | |
ef416fc2 | 574 | return (1); |
575 | } | |
576 | ||
577 | files[num_files] = argv[i]; | |
578 | num_files ++; | |
579 | ||
580 | if (title == NULL) | |
581 | { | |
582 | if ((title = strrchr(argv[i], '/')) != NULL) | |
583 | title ++; | |
584 | else | |
585 | title = argv[i]; | |
586 | } | |
587 | } | |
588 | else | |
fa73b229 | 589 | _cupsLangPrintf(stderr, _("%s: Error - too many files - \"%s\"\n"), |
590 | argv[0], argv[i]); | |
ef416fc2 | 591 | |
592 | /* | |
593 | * See if we are altering an existing job... | |
594 | */ | |
595 | ||
596 | if (job_id) | |
fa73b229 | 597 | return (set_job_attrs(argv[0], job_id, num_options, options)); |
ef416fc2 | 598 | |
599 | /* | |
600 | * See if we have any files to print; if not, print from stdin... | |
601 | */ | |
602 | ||
603 | if (printer == NULL) | |
604 | { | |
605 | if (num_dests == 0) | |
606 | num_dests = cupsGetDests(&dests); | |
607 | ||
608 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL) | |
609 | { | |
610 | printer = dest->name; | |
611 | ||
612 | for (j = 0; j < dest->num_options; j ++) | |
613 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
614 | num_options = cupsAddOption(dest->options[j].name, | |
615 | dest->options[j].value, | |
616 | num_options, &options); | |
617 | } | |
618 | } | |
619 | ||
620 | if (printer == NULL) | |
621 | { | |
622 | val = NULL; | |
623 | ||
624 | if ((printer = getenv("LPDEST")) == NULL) | |
625 | { | |
626 | if ((printer = getenv("PRINTER")) != NULL) | |
627 | { | |
628 | if (!strcmp(printer, "lp")) | |
629 | printer = NULL; | |
630 | else | |
631 | val = "PRINTER"; | |
632 | } | |
633 | } | |
634 | else | |
635 | val = "LPDEST"; | |
636 | ||
637 | if (printer && !cupsGetDest(printer, NULL, num_dests, dests)) | |
fa73b229 | 638 | _cupsLangPrintf(stderr, |
639 | _("%s: Error - %s environment variable names " | |
ef416fc2 | 640 | "non-existent destination \"%s\"!\n"), |
fa73b229 | 641 | argv[0], val, printer); |
ef416fc2 | 642 | else if (cupsLastError() == IPP_NOT_FOUND) |
fa73b229 | 643 | _cupsLangPrintf(stderr, |
644 | _("%s: Error - no default destination available.\n"), | |
645 | argv[0]); | |
ef416fc2 | 646 | else |
fa73b229 | 647 | _cupsLangPrintf(stderr, |
648 | _("%s: Error - scheduler not responding!\n"), | |
649 | argv[0]); | |
ef416fc2 | 650 | |
651 | return (1); | |
652 | } | |
653 | ||
654 | if (num_files > 0) | |
655 | job_id = cupsPrintFiles(printer, num_files, files, title, num_options, options); | |
656 | else | |
657 | { | |
658 | num_files = 1; | |
659 | ||
660 | #ifndef WIN32 | |
661 | # if defined(HAVE_SIGSET) | |
662 | sigset(SIGHUP, sighandler); | |
663 | if (sigset(SIGINT, sighandler) == SIG_IGN) | |
664 | sigset(SIGINT, SIG_IGN); | |
665 | sigset(SIGTERM, sighandler); | |
666 | # elif defined(HAVE_SIGACTION) | |
667 | memset(&action, 0, sizeof(action)); | |
668 | action.sa_handler = sighandler; | |
669 | ||
670 | sigaction(SIGHUP, &action, NULL); | |
671 | sigaction(SIGINT, NULL, &oldaction); | |
672 | if (oldaction.sa_handler != SIG_IGN) | |
673 | sigaction(SIGINT, &action, NULL); | |
674 | sigaction(SIGTERM, &action, NULL); | |
675 | # else | |
676 | signal(SIGHUP, sighandler); | |
677 | if (signal(SIGINT, sighandler) == SIG_IGN) | |
678 | signal(SIGINT, SIG_IGN); | |
679 | signal(SIGTERM, sighandler); | |
680 | # endif | |
681 | #endif /* !WIN32 */ | |
682 | ||
683 | temp = cupsTempFd(tempfile, sizeof(tempfile)); | |
684 | ||
685 | if (temp < 0) | |
686 | { | |
fa73b229 | 687 | _cupsLangPrintf(stderr, |
688 | _("%s: Error - unable to create temporary file \"%s\" - %s\n"), | |
689 | argv[0], tempfile, strerror(errno)); | |
ef416fc2 | 690 | return (1); |
691 | } | |
692 | ||
fa73b229 | 693 | while ((bytes = read(0, buffer, sizeof(buffer))) > 0) |
694 | if (write(temp, buffer, bytes) < 0) | |
ef416fc2 | 695 | { |
fa73b229 | 696 | _cupsLangPrintf(stderr, |
697 | _("%s: Error - unable to write to temporary file " | |
ef416fc2 | 698 | "\"%s\" - %s\n"), |
fa73b229 | 699 | argv[0], tempfile, strerror(errno)); |
ef416fc2 | 700 | close(temp); |
701 | unlink(tempfile); | |
702 | return (1); | |
703 | } | |
704 | ||
fa73b229 | 705 | filesize = lseek(temp, 0, SEEK_CUR); |
ef416fc2 | 706 | close(temp); |
707 | ||
fa73b229 | 708 | if (filesize <= 0) |
ef416fc2 | 709 | { |
fa73b229 | 710 | _cupsLangPrintf(stderr, |
711 | _("%s: Error - stdin is empty, so no job has been sent.\n"), | |
712 | argv[0]); | |
ef416fc2 | 713 | unlink(tempfile); |
714 | return (1); | |
715 | } | |
716 | ||
717 | if (title) | |
718 | job_id = cupsPrintFile(printer, tempfile, title, num_options, options); | |
719 | else | |
720 | job_id = cupsPrintFile(printer, tempfile, "(stdin)", num_options, options); | |
721 | ||
722 | unlink(tempfile); | |
723 | } | |
724 | ||
725 | if (job_id < 1) | |
726 | { | |
fa73b229 | 727 | _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString()); |
ef416fc2 | 728 | return (1); |
729 | } | |
730 | else if (!silent) | |
fa73b229 | 731 | _cupsLangPrintf(stdout, _("request id is %s-%d (%d file(s))\n"), |
ef416fc2 | 732 | printer, job_id, num_files); |
733 | ||
734 | return (0); | |
735 | } | |
736 | ||
737 | ||
738 | /* | |
739 | * 'restart_job()' - Restart a job. | |
740 | */ | |
741 | ||
742 | int /* O - Exit status */ | |
fa73b229 | 743 | restart_job(const char *command, /* I - Command name */ |
744 | int job_id) /* I - Job ID */ | |
ef416fc2 | 745 | { |
746 | http_t *http; /* HTTP connection to server */ | |
fa73b229 | 747 | ipp_t *request; /* IPP request */ |
ef416fc2 | 748 | char uri[HTTP_MAX_URI]; /* URI for job */ |
749 | ||
750 | ||
751 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); | |
752 | ||
fa73b229 | 753 | request = ippNewRequest(IPP_RESTART_JOB); |
ef416fc2 | 754 | |
755 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); | |
756 | ||
757 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
758 | "job-uri", NULL, uri); | |
759 | ||
760 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
761 | "requesting-user-name", NULL, cupsUser()); | |
762 | ||
fa73b229 | 763 | ippDelete(cupsDoRequest(http, request, "/jobs")); |
ef416fc2 | 764 | |
fa73b229 | 765 | if (cupsLastError() > IPP_OK_CONFLICT) |
ef416fc2 | 766 | { |
fa73b229 | 767 | _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString()); |
ef416fc2 | 768 | return (1); |
769 | } | |
770 | ||
771 | return (0); | |
772 | } | |
773 | ||
774 | ||
775 | /* | |
776 | * 'set_job_attrs()' - Set job attributes. | |
777 | */ | |
778 | ||
779 | int /* O - Exit status */ | |
fa73b229 | 780 | set_job_attrs(const char *command, /* I - Command name */ |
781 | int job_id, /* I - Job ID */ | |
ef416fc2 | 782 | int num_options,/* I - Number of options */ |
783 | cups_option_t *options) /* I - Options */ | |
784 | { | |
785 | http_t *http; /* HTTP connection to server */ | |
fa73b229 | 786 | ipp_t *request; /* IPP request */ |
ef416fc2 | 787 | char uri[HTTP_MAX_URI]; /* URI for job */ |
788 | ||
789 | ||
790 | if (num_options == 0) | |
791 | return (0); | |
792 | ||
793 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); | |
794 | ||
fa73b229 | 795 | request = ippNewRequest(IPP_SET_JOB_ATTRIBUTES); |
ef416fc2 | 796 | |
797 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); | |
798 | ||
799 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
800 | "job-uri", NULL, uri); | |
801 | ||
802 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
803 | "requesting-user-name", NULL, cupsUser()); | |
804 | ||
805 | cupsEncodeOptions(request, num_options, options); | |
806 | ||
fa73b229 | 807 | ippDelete(cupsDoRequest(http, request, "/jobs")); |
ef416fc2 | 808 | |
fa73b229 | 809 | if (cupsLastError() > IPP_OK_CONFLICT) |
ef416fc2 | 810 | { |
fa73b229 | 811 | _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString()); |
ef416fc2 | 812 | return (1); |
813 | } | |
814 | ||
815 | return (0); | |
816 | } | |
817 | ||
818 | ||
819 | #ifndef WIN32 | |
820 | /* | |
821 | * 'sighandler()' - Signal catcher for when we print from stdin... | |
822 | */ | |
823 | ||
824 | void | |
fa73b229 | 825 | sighandler(int s) /* I - Signal number */ |
ef416fc2 | 826 | { |
827 | /* | |
828 | * Remove the temporary file we're using to print from stdin... | |
829 | */ | |
830 | ||
831 | unlink(tempfile); | |
832 | ||
833 | /* | |
834 | * Exit... | |
835 | */ | |
836 | ||
837 | exit(s); | |
838 | } | |
839 | #endif /* !WIN32 */ | |
840 | ||
841 | ||
842 | /* | |
7594b224 | 843 | * End of "$Id: lp.c 6356 2007-03-19 13:54:48Z mike $". |
ef416fc2 | 844 | */ |