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