4 * Line Printer Daemon interface for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Process an incoming LPD request...
27 * print_file() - Print a file to a printer or class.
28 * recv_print_job() - Receive a print job from the client.
29 * remove_jobs() - Cancel one or more jobs.
30 * send_state() - Send the queue state.
31 * smart_gets() - Get a line of text, removing the trailing CR
36 * Include necessary headers...
39 #include <cups/cups.h>
40 #include <cups/string.h>
41 #include <cups/language.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
56 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
57 * with inetd or another similar program that monitors ports and starts
58 * daemons for each client connection. A typical configuration is:
60 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
62 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
65 * - This daemon does not check to make sure that the source port is
66 * between 721 and 731, since it isn't necessary for proper
67 * functioning and port-based security is no security at all!
69 * - The "Print any waiting jobs" command is a no-op.
71 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
72 * currently match the Solaris LPD mini-daemon.
79 int print_file(const char *name
, const char *file
,
80 const char *title
, const char *docname
,
81 const char *user
, int num_options
,
82 cups_option_t
*options
);
83 int recv_print_job(const char *dest
, int num_defaults
, cups_option_t
*defaults
);
84 int remove_jobs(const char *dest
, const char *agent
, const char *list
);
85 int send_state(const char *dest
, const char *list
, int longstatus
);
86 char *smart_gets(char *s
, int len
, FILE *fp
);
90 * 'main()' - Process an incoming LPD request...
93 int /* O - Exit status */
94 main(int argc
, /* I - Number of command-line arguments */
95 char *argv
[]) /* I - Command-line arguments */
97 int i
; /* Looping var */
98 int num_defaults
; /* Number of default options */
99 cups_option_t
*defaults
; /* Default options */
100 char line
[256], /* Command string */
101 command
, /* Command code */
102 *dest
, /* Pointer to destination */
103 *list
, /* Pointer to list */
104 *agent
, /* Pointer to user */
105 status
; /* Status for client */
106 int hostlen
; /* Size of client address */
107 http_addr_t hostaddr
; /* Address of client */
108 char hostname
[256], /* Name of client */
109 hostip
[256], /* IP address */
110 *hostfamily
; /* Address family */
114 * Don't buffer the output...
117 setbuf(stdout
, NULL
);
120 * Log things using the "cups-lpd" name...
123 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
126 * Get the address of the client...
129 hostlen
= sizeof(hostaddr
);
131 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
133 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
134 strcpy(hostname
, "unknown");
138 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
139 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
142 if (hostaddr
.addr
.sa_family
== AF_INET6
)
145 #endif /* AF_INET6 */
148 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
, hostip
);
152 * Scan the command-line for options...
158 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
159 num_defaults
, &defaults
);
161 for (i
= 1; i
< argc
; i
++)
162 if (argv
[i
][0] == '-')
166 case 'o' : /* Option */
168 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
174 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
, &defaults
);
176 syslog(LOG_WARNING
, "Expected option string after -o option!");
180 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
185 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!", argv
[i
]);
188 * RFC1179 specifies that only 1 daemon command can be received for
192 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
195 * Unable to get command from client! Send an error status and return.
198 syslog(LOG_ERR
, "Unable to get command line from client!");
204 * The first byte is the command byte. After that will be the queue name,
205 * resource list, and/or user name.
211 for (list
= dest
+ 1; *list
&& !isspace(*list
& 255); list
++);
213 while (isspace(*list
& 255))
222 default : /* Unknown command */
223 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
224 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
230 case 0x01 : /* Print any waiting jobs */
231 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
237 case 0x02 : /* Receive a printer job */
238 syslog(LOG_INFO
, "Receive print job for %s", dest
);
239 /* recv_print_job() sends initial status byte */
241 status
= recv_print_job(dest
, num_defaults
, defaults
);
244 case 0x03 : /* Send queue state (short) */
245 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
246 /* no status byte for this command */
248 status
= send_state(dest
, list
, 0);
251 case 0x04 : /* Send queue state (long) */
252 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
253 /* no status byte for this command */
255 status
= send_state(dest
, list
, 1);
258 case 0x05 : /* Remove jobs */
260 * Grab the agent and skip to the list of users and/or jobs.
265 for (; *list
&& !isspace(*list
& 255); list
++);
266 while (isspace(*list
& 255))
269 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
271 status
= remove_jobs(dest
, agent
, list
);
277 syslog(LOG_INFO
, "Closing connection");
285 * 'check_printer()' - Check that a printer exists and is accepting jobs.
289 check_printer(const char *name
) /* I - Printer or class name */
291 http_t
*http
; /* Connection to server */
292 ipp_t
*request
; /* IPP request */
293 ipp_t
*response
; /* IPP response */
294 ipp_attribute_t
*attr
; /* IPP job-id attribute */
295 char uri
[HTTP_MAX_URI
]; /* Printer URI */
296 cups_lang_t
*language
; /* Language to use */
297 int accepting
; /* printer-is-accepting-jobs value */
301 * Setup a connection and request data...
304 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
305 cupsEncryption())) == NULL
)
307 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
313 * Build a standard CUPS URI for the printer and fill the standard IPP
317 if ((request
= ippNew()) == NULL
)
319 syslog(LOG_ERR
, "Unable to create request: %s", strerror(errno
));
324 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
325 request
->request
.op
.request_id
= 1;
327 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", name
);
329 language
= cupsLangDefault();
331 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
332 "attributes-charset", NULL
, cupsLangEncoding(language
));
334 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
335 "attributes-natural-language", NULL
,
336 language
!= NULL
? language
->language
: "C");
338 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
341 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requested-attributes",
342 NULL
, "printer-is-accepting-jobs");
348 response
= cupsDoRequest(http
, request
, "/");
350 if (response
== NULL
)
352 syslog(LOG_ERR
, "Unable to check printer status - %s",
353 ippErrorString(cupsLastError()));
356 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
358 syslog(LOG_ERR
, "Unable to check printer status - %s",
359 ippErrorString(response
->request
.status
.status_code
));
362 else if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
363 IPP_TAG_BOOLEAN
)) == NULL
)
365 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in response from server!");
369 accepting
= attr
->values
[0].boolean
;
371 if (response
!= NULL
)
375 cupsLangFree(language
);
382 * 'print_file()' - Print a file to a printer or class.
386 print_file(const char *name
, /* I - Printer or class name */
387 const char *file
, /* I - File to print */
388 const char *title
, /* I - Title of job */
389 const char *docname
, /* I - Name of job file */
390 const char *user
, /* I - Owner of job */
391 int num_options
, /* I - Number of options */
392 cups_option_t
*options
) /* I - Options */
394 http_t
*http
; /* Connection to server */
395 ipp_t
*request
; /* IPP request */
396 ipp_t
*response
; /* IPP response */
397 ipp_attribute_t
*attr
; /* IPP job-id attribute */
398 char uri
[HTTP_MAX_URI
]; /* Printer URI */
399 cups_lang_t
*language
; /* Language to use */
400 int jobid
; /* New job ID */
404 * Setup a connection and request data...
407 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
408 cupsEncryption())) == NULL
)
410 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
416 * Build a standard CUPS URI for the printer and fill the standard IPP
420 if ((request
= ippNew()) == NULL
)
422 syslog(LOG_ERR
, "Unable to create request: %s", strerror(errno
));
427 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
428 request
->request
.op
.request_id
= 1;
430 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", name
);
432 language
= cupsLangDefault();
434 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
435 "attributes-charset", NULL
, cupsLangEncoding(language
));
437 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
438 "attributes-natural-language", NULL
,
439 language
!= NULL
? language
->language
: "C");
441 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
444 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
448 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
, title
);
450 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name", NULL
, docname
);
453 * Then add all options on the command-line...
456 cupsEncodeOptions(request
, num_options
, options
);
462 snprintf(uri
, sizeof(uri
), "/printers/%s", name
);
464 response
= cupsDoFileRequest(http
, request
, uri
, file
);
466 if (response
== NULL
)
468 syslog(LOG_ERR
, "Unable to print file - %s",
469 ippErrorString(cupsLastError()));
472 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
474 syslog(LOG_ERR
, "Unable to print file - %s",
475 ippErrorString(response
->request
.status
.status_code
));
478 else if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
480 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
485 jobid
= attr
->values
[0].integer
;
487 syslog(LOG_INFO
, "Print file - job ID = %d", jobid
);
490 if (response
!= NULL
)
494 cupsLangFree(language
);
501 * 'recv_print_job()' - Receive a print job from the client.
504 int /* O - Command status */
505 recv_print_job(const char *dest
, /* I - Destination */
507 /* I - Number of default options */
508 cups_option_t
*defaults
) /* I - Default options */
510 int i
; /* Looping var */
511 int status
; /* Command status */
512 int fd
; /* Temporary file */
513 FILE *fp
; /* File pointer */
514 char filename
[1024]; /* Temporary filename */
515 int bytes
; /* Bytes received */
516 char line
[256], /* Line from file/stdin */
517 command
, /* Command from line */
518 *count
, /* Number of bytes */
519 *name
; /* Name of file */
520 const char *job_sheets
; /* Job sheets */
521 int num_data
; /* Number of data files */
522 char control
[1024], /* Control filename */
523 data
[32][256], /* Data files */
524 temp
[32][1024]; /* Temporary files */
525 char user
[1024], /* User name */
526 title
[1024], /* Job title */
527 docname
[1024], /* Document name */
528 queue
[256], /* Printer/class queue */
529 *instance
; /* Printer/class instance */
530 int num_dests
; /* Number of destinations */
531 cups_dest_t
*dests
, /* Destinations */
532 *destptr
; /* Current destination */
533 int num_options
; /* Number of options */
534 cups_option_t
*options
; /* Options */
535 int banner
; /* Print banner? */
544 strlcpy(queue
, dest
, sizeof(queue
));
546 if ((instance
= strrchr(queue
, '/')) != NULL
)
549 num_dests
= cupsGetDests(&dests
);
550 if ((destptr
= cupsGetDest(queue
, instance
, num_dests
, dests
)) == NULL
)
553 * If the queue name is blank or "lp" then use the default queue.
556 if (!queue
[0] || !strcmp(queue
, "lp"))
557 if ((destptr
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
558 strlcpy(queue
, destptr
->name
, sizeof(queue
));
563 syslog(LOG_ERR
, "Unknown destination %s/%s!", queue
, instance
);
565 syslog(LOG_ERR
, "Unknown destination %s!", queue
);
567 cupsFreeDests(num_dests
, dests
);
575 if (!check_printer(queue
))
577 cupsFreeDests(num_dests
, dests
);
586 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
588 if (strlen(line
) < 2)
597 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
598 while (isspace(*name
& 255))
604 case 0x01 : /* Abort */
608 case 0x02 : /* Receive control file */
609 if (strlen(name
) < 2)
611 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
620 * Append to the existing control file - the LPD spec is
621 * not entirely clear, but at least the OS/2 LPD code sends
622 * multiple control files per connection...
625 if ((fd
= open(control
, O_WRONLY
)) < 0)
628 "Unable to append to temporary control file \"%s\" - %s",
629 control
, strerror(errno
));
635 lseek(fd
, 0, SEEK_END
);
639 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
641 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
642 control
, strerror(errno
));
648 strcpy(filename
, control
);
652 case 0x03 : /* Receive data file */
653 if (strlen(name
) < 2)
655 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
661 if (num_data
>= (sizeof(data
) / sizeof(data
[0])))
664 * Too many data files...
667 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
673 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
675 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
677 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
678 temp
[num_data
], strerror(errno
));
684 strcpy(filename
, temp
[num_data
]);
696 * Copy the data or control file from the client...
699 for (i
= atoi(count
); i
> 0; i
-= bytes
)
701 if (i
> sizeof(line
))
702 bytes
= sizeof(line
);
706 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
707 bytes
= write(fd
, line
, bytes
);
711 syslog(LOG_ERR
, "Error while reading file - %s",
719 * Read trailing nul...
724 if (fread(line
, 1, 1, stdin
) < 1)
727 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
733 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
739 * Close the file and send an acknowledgement...
753 * Process the control file and print stuff...
756 if ((fp
= fopen(control
, "rb")) == NULL
)
761 * Grab the job information first...
769 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
772 * Process control lines...
777 case 'J' : /* Job name */
778 strlcpy(title
, line
+ 1, sizeof(title
));
781 case 'N' : /* Document name */
782 strlcpy(docname
, line
+ 1, sizeof(docname
));
785 case 'P' : /* User identification */
786 strlcpy(user
, line
+ 1, sizeof(user
));
789 case 'L' : /* Print banner page */
799 * Then print the jobs...
804 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
807 * Process control lines...
812 case 'c' : /* Plot CIF file */
813 case 'd' : /* Print DVI file */
814 case 'f' : /* Print formatted file */
815 case 'g' : /* Plot file */
816 case 'l' : /* Print file leaving control characters (raw) */
817 case 'n' : /* Print ditroff output file */
818 case 'o' : /* Print PostScript output file */
819 case 'p' : /* Print file with 'pr' format (prettyprint) */
820 case 'r' : /* File to print with FORTRAN carriage control */
821 case 't' : /* Print troff output file */
822 case 'v' : /* Print raster file */
824 * Check that we have a username...
829 syslog(LOG_WARNING
, "No username specified by client! "
830 "Using \"anonymous\"...");
831 strcpy(user
, "anonymous");
835 * Copy the default options...
841 for (i
= 0; i
< destptr
->num_options
; i
++)
842 num_options
= cupsAddOption(destptr
->options
[i
].name
,
843 destptr
->options
[i
].value
,
844 num_options
, &options
);
845 for (i
= 0; i
< num_defaults
; i
++)
846 num_options
= cupsAddOption(defaults
[i
].name
,
848 num_options
, &options
);
851 * If a banner was requested and it's not overridden by a
852 * command line option and the destination's default is none
853 * then add the standard banner...
857 cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
858 ((job_sheets
= cupsGetOption("job-sheets",
859 destptr
->num_options
,
860 destptr
->options
)) == NULL
||
861 !strcmp(job_sheets
, "none,none")))
863 num_options
= cupsAddOption("job-sheets", "standard",
864 num_options
, &options
);
868 * Add additional options as needed...
872 num_options
= cupsAddOption("raw", "", num_options
, &options
);
875 num_options
= cupsAddOption("prettyprint", "", num_options
,
879 * Figure out which file we are printing...
882 for (i
= 0; i
< num_data
; i
++)
883 if (strcmp(data
[i
], line
+ 1) == 0)
893 * Send the print request...
896 if (print_file(queue
, temp
[i
], title
, docname
, user
, num_options
,
902 cupsFreeOptions(num_options
, options
);
915 * Clean up all temporary files and return...
920 for (i
= 0; i
< num_data
; i
++)
923 cupsFreeDests(num_dests
, dests
);
930 * 'remove_jobs()' - Cancel one or more jobs.
933 int /* O - Command status */
934 remove_jobs(const char *dest
, /* I - Destination */
935 const char *agent
, /* I - User agent */
936 const char *list
) /* I - List of jobs or users */
939 http_t
*http
; /* HTTP server connection */
940 ipp_t
*request
, /* IPP Request */
941 *response
; /* IPP Response */
942 cups_lang_t
*language
; /* Default language */
943 char uri
[HTTP_MAX_URI
]; /* Job URI */
946 (void)dest
; /* Suppress compiler warnings... */
949 * Try connecting to the local server...
952 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
953 cupsEncryption())) == NULL
)
955 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
960 language
= cupsLangDefault();
963 * Loop for each job...
966 while ((id
= atoi(list
)) > 0)
969 * Skip job ID in list...
972 while (isdigit(*list
& 255))
974 while (isspace(*list
& 255))
978 * Build an IPP_CANCEL_JOB request, which requires the following
982 * attributes-natural-language
984 * requesting-user-name
989 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
990 request
->request
.op
.request_id
= 1;
992 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
993 "attributes-charset", NULL
, cupsLangEncoding(language
));
995 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
996 "attributes-natural-language", NULL
, language
->language
);
998 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
999 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1001 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1002 "requesting-user-name", NULL
, agent
);
1005 * Do the request and get back a response...
1008 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
)
1010 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1012 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1013 ippErrorString(response
->request
.status
.status_code
));
1014 ippDelete(response
);
1015 cupsLangFree(language
);
1020 syslog(LOG_INFO
, "Job ID %d cancelled", id
);
1022 ippDelete(response
);
1026 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1027 ippErrorString(cupsLastError()));
1028 cupsLangFree(language
);
1034 cupsLangFree(language
);
1042 * 'send_state()' - Send the queue state.
1045 int /* O - Command status */
1046 send_state(const char *dest
, /* I - Destination */
1047 const char *list
, /* I - Job or user */
1048 int longstatus
) /* I - List of jobs or users */
1050 int id
; /* Job ID from list */
1051 http_t
*http
; /* HTTP server connection */
1052 ipp_t
*request
, /* IPP Request */
1053 *response
; /* IPP Response */
1054 ipp_attribute_t
*attr
; /* Current attribute */
1055 cups_lang_t
*language
; /* Default language */
1056 ipp_pstate_t state
; /* Printer state */
1057 const char *jobdest
, /* Pointer into job-printer-uri */
1058 *jobuser
, /* Pointer to job-originating-user-name */
1059 *jobname
; /* Pointer to job-name */
1060 ipp_jstate_t jobstate
; /* job-state */
1061 int jobid
, /* job-id */
1062 jobsize
, /* job-k-octets */
1063 jobcount
, /* Number of jobs */
1064 jobcopies
, /* Number of copies */
1065 rank
; /* Rank of job */
1066 char rankstr
[255]; /* Rank string */
1067 char namestr
[1024]; /* Job name string */
1068 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1069 char queue
[256], /* Printer/class queue */
1070 *instance
; /* Printer/class instance */
1071 static const char * const ranks
[10] = /* Ranking strings */
1084 static const char * const requested
[] =
1085 { /* Requested attributes */
1090 "job-originating-user-name",
1097 * Remove instance from destination, if any...
1100 strlcpy(queue
, dest
, sizeof(queue
));
1102 if ((instance
= strrchr(queue
, '/')) != NULL
)
1106 * Try connecting to the local server...
1109 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1110 cupsEncryption())) == NULL
)
1112 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1114 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1119 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1122 * attributes-charset
1123 * attributes-natural-language
1129 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1130 request
->request
.op
.request_id
= 1;
1132 language
= cupsLangDefault();
1134 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1135 "attributes-charset", NULL
, cupsLangEncoding(language
));
1137 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1138 "attributes-natural-language", NULL
, language
->language
);
1140 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", queue
);
1141 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1142 "printer-uri", NULL
, uri
);
1144 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1145 "requested-attributes", NULL
, "printer-state");
1148 * Do the request and get back a response...
1151 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1153 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1155 syslog(LOG_WARNING
, "Unable to get printer list: %s\n",
1156 ippErrorString(response
->request
.status
.status_code
));
1157 printf("Unable to get printer list: %s\n",
1158 ippErrorString(response
->request
.status
.status_code
));
1159 ippDelete(response
);
1163 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
1164 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1166 state
= IPP_PRINTER_STOPPED
;
1170 case IPP_PRINTER_IDLE
:
1171 printf("%s is ready\n", dest
);
1173 case IPP_PRINTER_PROCESSING
:
1174 printf("%s is ready and printing\n", dest
);
1176 case IPP_PRINTER_STOPPED
:
1177 printf("%s is not ready\n", dest
);
1181 ippDelete(response
);
1185 syslog(LOG_WARNING
, "Unable to get printer list: %s\n",
1186 ippErrorString(cupsLastError()));
1187 printf("Unable to get printer list: %s\n",
1188 ippErrorString(cupsLastError()));
1193 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1194 * the following attributes:
1196 * attributes-charset
1197 * attributes-natural-language
1198 * job-uri or printer-uri
1205 request
->request
.op
.operation_id
= id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
1206 request
->request
.op
.request_id
= 1;
1208 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1209 "attributes-charset", NULL
, cupsLangEncoding(language
));
1211 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1212 "attributes-natural-language", NULL
, language
->language
);
1214 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", queue
);
1216 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1220 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1223 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1224 "requesting-user-name", NULL
, list
);
1225 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1228 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1229 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
1233 * Do the request and get back a response...
1238 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1240 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1242 printf("get-jobs failed: %s\n",
1243 ippErrorString(response
->request
.status
.status_code
));
1244 ippDelete(response
);
1251 * Loop through the job list and display them...
1254 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1257 * Skip leading attributes until we hit a job...
1260 while (attr
!= NULL
&&
1261 (attr
->group_tag
!= IPP_TAG_JOB
|| attr
->name
== NULL
))
1268 * Pull the needed attributes from this job...
1273 jobstate
= IPP_JOB_PENDING
;
1274 jobname
= "untitled";
1279 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
1281 if (strcmp(attr
->name
, "job-id") == 0 &&
1282 attr
->value_tag
== IPP_TAG_INTEGER
)
1283 jobid
= attr
->values
[0].integer
;
1285 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1286 attr
->value_tag
== IPP_TAG_INTEGER
)
1287 jobsize
= attr
->values
[0].integer
* 1024;
1289 if (strcmp(attr
->name
, "job-state") == 0 &&
1290 attr
->value_tag
== IPP_TAG_ENUM
)
1291 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1293 if (strcmp(attr
->name
, "job-printer-uri") == 0 &&
1294 attr
->value_tag
== IPP_TAG_URI
)
1295 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1298 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1299 attr
->value_tag
== IPP_TAG_NAME
)
1300 jobuser
= attr
->values
[0].string
.text
;
1302 if (strcmp(attr
->name
, "job-name") == 0 &&
1303 attr
->value_tag
== IPP_TAG_NAME
)
1304 jobname
= attr
->values
[0].string
.text
;
1306 if (strcmp(attr
->name
, "copies") == 0 &&
1307 attr
->value_tag
== IPP_TAG_INTEGER
)
1308 jobcopies
= attr
->values
[0].integer
;
1314 * See if we have everything needed...
1317 if (jobdest
== NULL
|| jobid
== 0)
1325 if (!longstatus
&& jobcount
== 0)
1326 puts("Rank Owner Job File(s) Total Size");
1331 * Display the job...
1334 if (jobstate
== IPP_JOB_PROCESSING
)
1335 strcpy(rankstr
, "active");
1338 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1347 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1350 strlcpy(namestr
, jobname
, sizeof(namestr
));
1352 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1353 printf(" %-39.39s %d bytes\n", namestr
, jobsize
);
1356 printf("%-7s %-7.7s %-7d %-31.31s %d bytes\n", rankstr
, jobuser
,
1357 jobid
, jobname
, jobsize
);
1363 ippDelete(response
);
1367 printf("get-jobs failed: %s\n", ippErrorString(cupsLastError()));
1374 cupsLangFree(language
);
1382 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1385 char * /* O - Line read or NULL */
1386 smart_gets(char *s
, /* I - Pointer to line buffer */
1387 int len
, /* I - Size of line buffer */
1388 FILE *fp
) /* I - File to read from */
1390 char *ptr
, /* Pointer into line */
1391 *end
; /* End of line */
1392 int ch
; /* Character from file */
1396 * Read the line; unlike fgets(), we read the entire line but dump
1397 * characters that go past the end of the buffer. Also, we accept
1398 * CR, LF, or CR LF for the line endings to be "safe", although
1399 * RFC 1179 specifically says "just use LF".
1405 while ((ch
= getc(fp
)) != EOF
)
1409 else if (ch
== '\r')
1412 * See if a LF follows...
1428 if (ch
== EOF
&& ptr
== s
)