]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/ipp.c
Added support for "-h" option...
[thirdparty/cups.git] / backend / ipp.c
CommitLineData
c8f9565c 1/*
97fcaf92 2 * "$Id: ipp.c,v 1.15 1999/12/08 14:59:49 mike Exp $"
c8f9565c 3 *
4 * IPP backend for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-1999 by Easy Software Products, all rights reserved.
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" 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
8784b6a6 17 * 44141 Airport View Drive, Suite 204
c8f9565c 18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
6d0582d2 26 * main() - Send a file to the printer or server.
c8f9565c 27 */
28
29/*
30 * Include necessary headers.
31 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <errno.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <cups/cups.h>
39#include <cups/language.h>
40#include <cups/string.h>
41
42
43/*
44 * 'main()' - Send a file to the printer or server.
45 *
46 * Usage:
47 *
48 * printer-uri job-id user title copies options [file]
49 */
50
51int /* O - Exit status */
52main(int argc, /* I - Number of command-line arguments (6 or 7) */
53 char *argv[]) /* I - Command-line arguments */
54{
55 int i; /* Looping var */
56 int n, n2; /* Attribute values */
57 char *option, /* Name of option */
58 *val, /* Pointer to option value */
59 *s; /* Pointer into option value */
60 int num_options; /* Number of printer options */
61 cups_option_t *options; /* Printer options */
62 char method[255], /* Method in URI */
63 hostname[1024], /* Hostname */
64 username[255], /* Username info */
65 resource[1024], /* Resource info (printer name) */
66 filename[1024]; /* File to print */
67 int port; /* Port number (not used) */
68 char password[255], /* Password info */
69 uri[HTTP_MAX_URI];/* Updated URI without user/pass */
70 http_status_t status; /* Status of HTTP job */
97fcaf92 71 ipp_status_t ipp_status; /* Status of IPP request */
c8f9565c 72 FILE *fp; /* File to print */
73 http_t *http; /* HTTP connection */
74 ipp_t *request, /* IPP request */
75 *response; /* IPP response */
76 ipp_attribute_t *job_id; /* job-id attribute */
97fcaf92 77 ipp_attribute_t *copies_sup; /* copies-supported attribute */
c8f9565c 78 cups_lang_t *language; /* Default language */
79 struct stat fileinfo; /* File statistics */
80 size_t nbytes, /* Number of bytes written */
81 tbytes; /* Total bytes written */
82 char buffer[8192]; /* Output buffer */
97fcaf92 83 int copies; /* Number of copies remaining */
c8f9565c 84
85
86 if (argc < 6 || argc > 7)
87 {
88 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
89 argv[0]);
90 return (1);
91 }
92
93 /*
94 * If we have 7 arguments, print the file named on the command-line.
97fcaf92 95 * Otherwise, copy stdin to a temporary file and print the temporary
96 * file.
c8f9565c 97 */
98
99 if (argc == 6)
97fcaf92 100 {
101 /*
102 * Copy stdin to a temporary file...
103 */
104
105 FILE *fp; /* Temporary file */
106 char buffer[8192]; /* Buffer for copying */
107 int bytes; /* Number of bytes read */
108
109
110 if ((fp = fopen(cupsTempFile(filename, sizeof(filename)), "w")) == NULL)
111 {
112 perror("ERROR: unable to create temporary file");
113 return (1);
114 }
115
116 while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
117 if (fwrite(buffer, 1, bytes, fp) < bytes)
118 {
119 perror("ERROR: unable to write to temporary file");
120 fclose(fp);
121 unlink(filename);
122 return (1);
123 }
124
125 fclose(fp);
126 }
127 else
128 {
129 strncpy(filename, argv[6], sizeof(filename) - 1);
130 filename[sizeof(filename) - 1] = '\0';
131 }
132
133 /*
134 * Open the print file...
135 */
136
137 if ((fp = fopen(filename, "rb")) == NULL)
c8f9565c 138 {
139 perror("ERROR: Unable to open print file");
140 return (1);
141 }
142 else
97fcaf92 143 stat(filename, &fileinfo);
c8f9565c 144
145 /*
146 * Extract the hostname and printer name from the URI...
147 */
148
149 httpSeparate(argv[0], method, username, hostname, &port, resource);
150
151 /*
152 * Try connecting to the remote server...
153 */
154
97fcaf92 155 do
c8f9565c 156 {
97fcaf92 157 fprintf(stderr, "INFO: Connecting to %s...\n", hostname);
c8f9565c 158
97fcaf92 159 if ((http = httpConnect(hostname, port)) == NULL)
160 if (errno == ECONNREFUSED)
161 {
162 fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...",
163 hostname);
164 sleep(30);
165 }
166 else
167 {
168 perror("ERROR: Unable to connect to IPP host");
169
170 if (fp != stdin)
171 fclose(fp);
172 return (1);
173 }
c8f9565c 174 }
97fcaf92 175 while (http == NULL);
c8f9565c 176
177 /*
178 * Build a URI for the printer and fill the standard IPP attributes for
8ce7c000 179 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
180 * might contain username:password information...
c8f9565c 181 */
182
3f9cb6c6 183 snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, hostname, port, resource);
c8f9565c 184
185 /*
97fcaf92 186 * First validate the destination and see if the device supports multiple
187 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
188 * don't support the copies attribute...
c8f9565c 189 */
190
97fcaf92 191 language = cupsLangDefault();
192 copies_sup = NULL;
c8f9565c 193
97fcaf92 194 do
c8f9565c 195 {
196 /*
97fcaf92 197 * Build the IPP request...
c8f9565c 198 */
199
97fcaf92 200 request = ippNew();
201 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
202 request->request.op.request_id = 1;
c8f9565c 203
97fcaf92 204 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
205 "attributes-charset", NULL, cupsLangEncoding(language));
206
207 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
208 "attributes-natural-language", NULL,
209 language != NULL ? language->language : "C");
c8f9565c 210
97fcaf92 211 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
212 NULL, uri);
c8f9565c 213
97fcaf92 214 /*
215 * Now fill in the HTTP request stuff...
216 */
c8f9565c 217
97fcaf92 218 httpClearFields(http);
219 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
220 if (username[0])
c8f9565c 221 {
97fcaf92 222 httpEncode64(password, username);
223 httpSetField(http, HTTP_FIELD_AUTHORIZATION, password);
224 }
c8f9565c 225
97fcaf92 226 sprintf(buffer, "%u", ippLength(request));
227 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, buffer);
c8f9565c 228
97fcaf92 229 /*
230 * Do the request...
231 */
c8f9565c 232
97fcaf92 233 for (;;)
c8f9565c 234 {
97fcaf92 235 /*
236 * POST the request, retrying as needed...
237 */
238
239 if (httpPost(http, resource))
c8f9565c 240 {
97fcaf92 241 fputs("INFO: Unable to POST get-printer-attributes request; retrying...\n", stderr);
242 sleep(10);
243 httpReconnect(http);
244 continue;
c8f9565c 245 }
c8f9565c 246
97fcaf92 247 fputs("INFO: POST successful, sending IPP request...\n", stderr);
c8f9565c 248
c8f9565c 249 /*
97fcaf92 250 * Send the IPP request...
c8f9565c 251 */
97fcaf92 252
253 request->state = IPP_IDLE;
254
255 if (ippWrite(http, request) == IPP_ERROR)
256 {
257 fputs("ERROR: Unable to send IPP request!\n", stderr);
258 status = HTTP_ERROR;
259 break;
260 }
261
262 fputs("INFO: IPP request sent, getting status...\n", stderr);
263
c8f9565c 264 /*
97fcaf92 265 * Finally, check the status from the HTTP server...
c8f9565c 266 */
267
97fcaf92 268 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
269
270 if (status == HTTP_OK)
c8f9565c 271 {
97fcaf92 272 response = ippNew();
273 ippRead(http, response);
274
275 ipp_status = response->request.status.status_code;
276
277 if (ipp_status > IPP_OK_CONFLICT)
278 {
279 if (ipp_status == IPP_PRINTER_BUSY ||
280 ipp_status == IPP_SERVICE_UNAVAILABLE)
281 {
282 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr);
283 sleep(10);
284 }
285 else
286 {
287 fprintf(stderr, "ERROR: Printer will not accept print file (%x)!\n",
288 ipp_status);
289 status = HTTP_ERROR;
290 }
291 }
292 else if ((copies_sup = ippFindAttribute(response, "copies-supported",
293 IPP_TAG_RANGE)) != NULL)
294 {
295 /*
296 * Has the "copies-supported" attribute - does it have an upper
297 * bound > 1?
298 */
299
300 if (copies_sup->values[0].range.upper <= 1)
301 copies_sup = NULL; /* No */
302 }
303
304 ippDelete(response);
c8f9565c 305 }
97fcaf92 306 else
c8f9565c 307 {
97fcaf92 308 if (status == HTTP_ERROR)
309 {
310 fprintf(stderr, "WARNING: Did not receive the IPP response (%d)\n",
311 errno);
312 status = HTTP_OK;
313 ipp_status = IPP_PRINTER_BUSY;
314 }
315 else
316 fprintf(stderr, "ERROR: Validate request was not accepted (%d)!\n", status);
c8f9565c 317 }
97fcaf92 318
319 httpFlush(http);
320
321 break;
322 }
323
324 if (status != HTTP_OK)
325 {
326 if (fp != stdin)
327 fclose(fp);
328
329 httpClose(http);
330
331 return (1);
c8f9565c 332 }
c8f9565c 333 }
97fcaf92 334 while (ipp_status > IPP_OK_CONFLICT);
c8f9565c 335
336 /*
97fcaf92 337 * See if the printer supports multiple copies...
c8f9565c 338 */
339
97fcaf92 340 if (copies_sup)
341 copies = 1;
0c5b0932 342 else
97fcaf92 343 copies = atoi(argv[4]);
0c5b0932 344
c8f9565c 345 /*
97fcaf92 346 * Then issue the print-job request...
c8f9565c 347 */
348
97fcaf92 349 while (copies > 0)
c8f9565c 350 {
351 /*
97fcaf92 352 * Build the IPP request...
c8f9565c 353 */
354
97fcaf92 355 request = ippNew();
356 request->request.op.operation_id = IPP_PRINT_JOB;
357 request->request.op.request_id = 1;
c8f9565c 358
97fcaf92 359 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
360 "attributes-charset", NULL, cupsLangEncoding(language));
8ce7c000 361
97fcaf92 362 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
363 "attributes-natural-language", NULL,
364 language != NULL ? language->language : "C");
c8f9565c 365
97fcaf92 366 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
367 NULL, uri);
c8f9565c 368
97fcaf92 369 fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri);
370
371 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
372 NULL, argv[2]);
373
374 fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]);
375
376 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
377 argv[3]);
c8f9565c 378
97fcaf92 379 fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]);
8ce7c000 380
c8f9565c 381 /*
97fcaf92 382 * Handle options on the command-line...
c8f9565c 383 */
384
97fcaf92 385 options = NULL;
386 num_options = cupsParseOptions(argv[5], 0, &options);
387
388 if (cupsGetOption("raw", num_options, options))
389 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
390 NULL, "application/vnd.cups-raw");
391 else
392 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
393 NULL, "application/octet-stream");
394
395 if (copies_sup)
396 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", atoi(argv[4]));
397
398 for (i = 0; i < num_options; i ++)
c8f9565c 399 {
97fcaf92 400 /*
401 * Skip the "raw" option - handled above...
402 */
403
404 if (strcasecmp(options[i].name, "raw") == 0)
405 continue;
406
407 /*
408 * See what the option value is; for compatibility with older interface
409 * scripts, we have to support single-argument options as well as
410 * option=value, option=low-high, and option=MxN.
411 */
412
413 option = options[i].name;
414 val = options[i].value;
c8f9565c 415
97fcaf92 416 if (*val == '\0')
417 val = NULL;
418
419 if (val != NULL)
c8f9565c 420 {
97fcaf92 421 if (strcasecmp(val, "true") == 0 ||
422 strcasecmp(val, "on") == 0 ||
423 strcasecmp(val, "yes") == 0)
424 {
425 /*
426 * Boolean value - true...
427 */
428
429 n = 1;
430 val = "";
431 }
432 else if (strcasecmp(val, "false") == 0 ||
433 strcasecmp(val, "off") == 0 ||
434 strcasecmp(val, "no") == 0)
435 {
436 /*
437 * Boolean value - false...
438 */
439
440 n = 0;
441 val = "";
442 }
443
444 n = strtol(val, &s, 0);
445 }
446 else
447 {
448 if (strncasecmp(option, "no", 2) == 0)
449 {
450 option += 2;
451 n = 0;
452 }
453 else
454 n = 1;
455
456 s = "";
c8f9565c 457 }
97fcaf92 458
459 if (*s != '\0' && *s != '-' && (*s != 'x' || s == val))
460 /*
461 * String value(s)...
462 */
463 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, option, NULL, val);
464 else if (val != NULL)
465 {
466 /*
467 * Numeric value, range, or resolution...
468 */
469
470 if (*s == '-')
471 {
472 n2 = strtol(s + 1, NULL, 0);
473 ippAddRange(request, IPP_TAG_JOB, option, n, n2);
474 }
475 else if (*s == 'x')
476 {
477 n2 = strtol(s + 1, &s, 0);
478
479 if (strcasecmp(s, "dpc") == 0)
480 ippAddResolution(request, IPP_TAG_JOB, option, IPP_RES_PER_CM, n, n2);
481 else if (strcasecmp(s, "dpi") == 0)
482 ippAddResolution(request, IPP_TAG_JOB, option, IPP_RES_PER_INCH, n, n2);
483 else
484 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, option, NULL, val);
485 }
486 else
487 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, option, n);
488 }
489 else
490 /*
491 * Boolean value...
492 */
493 ippAddBoolean(request, IPP_TAG_JOB, option, (char)n);
c8f9565c 494 }
495
cb555bcf 496 /*
97fcaf92 497 * Now fill in the HTTP request stuff...
cb555bcf 498 */
499
97fcaf92 500 httpClearFields(http);
501 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
502 if (username[0])
503 {
504 httpEncode64(password, username);
505 httpSetField(http, HTTP_FIELD_AUTHORIZATION, password);
506 }
cb555bcf 507
97fcaf92 508 sprintf(buffer, "%u", ippLength(request) + (size_t)fileinfo.st_size);
509 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, buffer);
8ce7c000 510
c8f9565c 511 /*
97fcaf92 512 * Do the request...
c8f9565c 513 */
514
97fcaf92 515 for (;;)
c8f9565c 516 {
97fcaf92 517 /*
518 * POST the request, retrying as needed...
519 */
520
521 httpReconnect(http);
c8f9565c 522
97fcaf92 523 if (httpPost(http, resource))
3f9cb6c6 524 {
97fcaf92 525 fputs("INFO: Unable to POST print request; retrying...\n", stderr);
526 sleep(10);
527 continue;
528 }
529
530 fputs("INFO: POST successful, sending IPP request...\n", stderr);
3f9cb6c6 531
97fcaf92 532 /*
533 * Send the IPP request...
534 */
535
536 request->state = IPP_IDLE;
3f9cb6c6 537
97fcaf92 538 if (ippWrite(http, request) == IPP_ERROR)
539 {
540 fputs("ERROR: Unable to send IPP request!\n", stderr);
541 status = HTTP_ERROR;
542 break;
3f9cb6c6 543 }
c8f9565c 544
97fcaf92 545 fputs("INFO: IPP request sent, sending print file...\n", stderr);
546
547 /*
548 * Then send the file...
549 */
550
551 rewind(fp);
552
553 tbytes = 0;
554 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
555 {
556 tbytes += nbytes;
557 fprintf(stderr, "INFO: Sending print file, %uk...\n", tbytes / 1024);
558
559 if (httpWrite(http, buffer, nbytes) < nbytes)
560 {
561 perror("ERROR: Unable to send print file to printer");
562 status = HTTP_ERROR;
563 break;
564 }
565 }
566
567 fputs("INFO: Print file sent; checking status...\n", stderr);
568
569 /*
570 * Finally, check the status from the HTTP server...
571 */
572
573 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
574
575 if (status == HTTP_OK)
6a536282 576 {
97fcaf92 577 response = ippNew();
578 ippRead(http, response);
579
580 if ((ipp_status = response->request.status.status_code) > IPP_OK_CONFLICT)
581 {
582 if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
583 ipp_status == IPP_PRINTER_BUSY)
584 {
585 fputs("INFO: Printer is busy; retrying print job...\n", stderr);
586 sleep(10);
587 }
588 else
589 fprintf(stderr, "ERROR: Print file was not accepted (%04x)!\n",
590 response->request.status.status_code);
591 }
592 else if ((job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL)
593 fputs("INFO: Print file accepted - job ID unknown.\n", stderr);
594 else
595 fprintf(stderr, "INFO: Print file accepted - job ID %d.\n",
596 job_id->values[0].integer);
6a536282 597 }
598 else
97fcaf92 599 {
600 response = NULL;
601 ipp_status = IPP_PRINTER_BUSY;
602
603 if (status == HTTP_ERROR)
604 {
605 fprintf(stderr, "WARNING: Did not receive the IPP response (%d)\n",
606 errno);
607 status = HTTP_OK;
608 }
609 else
610 fprintf(stderr, "ERROR: Print request was not accepted (%d)!\n", status);
611 }
612
613 httpFlush(http);
614
615 break;
c8f9565c 616 }
617
97fcaf92 618 if (request != NULL)
619 ippDelete(request);
620 if (response != NULL)
621 ippDelete(response);
622
623 if (ipp_status <= IPP_OK_CONFLICT)
624 {
625 fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
626 copies --;
627 }
c8f9565c 628 }
629
630 /*
631 * Free memory...
632 */
633
634 httpClose(http);
c8f9565c 635
636 /*
97fcaf92 637 * Close and remove the temporary file if necessary...
c8f9565c 638 */
639
97fcaf92 640 fclose(fp);
3f9cb6c6 641
97fcaf92 642 if (argc < 7)
643 unlink(filename);
c8f9565c 644
645 /*
646 * Return the queue status...
647 */
648
649 return (status != HTTP_OK);
650}
651
652
653/*
97fcaf92 654 * End of "$Id: ipp.c,v 1.15 1999/12/08 14:59:49 mike Exp $".
c8f9565c 655 */