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