2 * "$Id: cups-lpd.c,v 1.24.2.15 2004/02/17 21:32:58 mike Exp $"
4 * Line Printer Daemon interface for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
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
); list
++);
213 while (isspace(*list
))
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
); list
++);
266 while (isspace(*list
))
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 * 'print_file()' - Print a file to a printer or class.
289 print_file(const char *name
, /* I - Printer or class name */
290 const char *file
, /* I - File to print */
291 const char *title
, /* I - Title of job */
292 const char *docname
, /* I - Name of job file */
293 const char *user
, /* I - Owner of job */
294 int num_options
, /* I - Number of options */
295 cups_option_t
*options
) /* I - Options */
297 http_t
*http
; /* Connection to server */
298 ipp_t
*request
; /* IPP request */
299 ipp_t
*response
; /* IPP response */
300 ipp_attribute_t
*attr
; /* IPP job-id attribute */
301 char uri
[HTTP_MAX_URI
]; /* Printer URI */
302 cups_lang_t
*language
; /* Language to use */
303 int jobid
; /* New job ID */
307 * Setup a connection and request data...
310 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
311 cupsEncryption())) == NULL
)
313 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
318 language
= cupsLangDefault();
321 * Build a standard CUPS URI for the printer and fill the standard IPP
325 if ((request
= ippNew()) == NULL
)
327 syslog(LOG_ERR
, "Unable to create request: %s", strerror(errno
));
331 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
332 request
->request
.op
.request_id
= 1;
334 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", name
);
336 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
337 "attributes-charset", NULL
, cupsLangEncoding(language
));
339 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
340 "attributes-natural-language", NULL
,
341 language
!= NULL
? language
->language
: "C");
343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
346 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
, title
);
352 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name", NULL
, docname
);
355 * Then add all options on the command-line...
358 cupsEncodeOptions(request
, num_options
, options
);
364 snprintf(uri
, sizeof(uri
), "/printers/%s", name
);
366 response
= cupsDoFileRequest(http
, request
, uri
, file
);
368 if (response
== NULL
)
370 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
372 else if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
375 jobid
= attr
->values
[0].integer
;
378 syslog(LOG_INFO
, "Print file - job ID = %d", jobid
);
380 syslog(LOG_ERR
, "Unable to print file - %s",
381 ippErrorString(response
->request
.status
.status_code
));
383 syslog(LOG_ERR
, "Unable to print file - %s",
384 ippErrorString(cupsLastError()));
386 if (response
!= NULL
)
390 cupsLangFree(language
);
397 * 'recv_print_job()' - Receive a print job from the client.
400 int /* O - Command status */
401 recv_print_job(const char *dest
, /* I - Destination */
402 int num_defaults
,/* I - Number of default options */
403 cups_option_t
*defaults
) /* I - Default options */
405 int i
; /* Looping var */
406 int status
; /* Command status */
407 int fd
; /* Temporary file */
408 FILE *fp
; /* File pointer */
409 char filename
[1024]; /* Temporary filename */
410 int bytes
; /* Bytes received */
411 char line
[256], /* Line from file/stdin */
412 command
, /* Command from line */
413 *count
, /* Number of bytes */
414 *name
; /* Name of file */
415 int num_data
; /* Number of data files */
416 char control
[1024], /* Control filename */
417 data
[32][256], /* Data files */
418 temp
[32][1024]; /* Temporary files */
419 char user
[1024], /* User name */
420 title
[1024], /* Job title */
421 docname
[1024], /* Document name */
422 queue
[256], /* Printer/class queue */
423 *instance
; /* Printer/class instance */
424 int num_dests
; /* Number of destinations */
425 cups_dest_t
*dests
, /* Destinations */
426 *destptr
; /* Current destination */
427 int num_options
; /* Number of options */
428 cups_option_t
*options
; /* Options */
429 int banner
; /* Print banner? */
438 strlcpy(queue
, dest
, sizeof(queue
));
440 if ((instance
= strrchr(queue
, '/')) != NULL
)
443 num_dests
= cupsGetDests(&dests
);
444 if ((destptr
= cupsGetDest(queue
, instance
, num_dests
, dests
)) == NULL
)
447 * If the queue name is blank or "lp" then use the default queue.
450 if (!queue
[0] || !strcmp(queue
, "lp"))
451 if ((destptr
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
452 strlcpy(queue
, destptr
->name
, sizeof(queue
));
457 syslog(LOG_ERR
, "Unknown destination %s/%s!", queue
, instance
);
459 syslog(LOG_ERR
, "Unknown destination %s!", queue
);
461 cupsFreeDests(num_dests
, dests
);
471 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
473 if (strlen(line
) < 2)
482 for (name
= count
+ 1; *name
&& !isspace(*name
); name
++);
483 while (isspace(*name
))
489 case 0x01 : /* Abort */
492 case 0x02 : /* Receive control file */
493 if (strlen(name
) < 2)
495 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
504 * Append to the existing control file - the LPD spec is
505 * not entirely clear, but at least the OS/2 LPD code sends
506 * multiple control files per connection...
509 if ((fd
= open(control
, O_WRONLY
)) < 0)
512 "Unable to append to temporary control file \"%s\" - %s",
513 control
, strerror(errno
));
519 lseek(fd
, 0, SEEK_END
);
523 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
525 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
526 control
, strerror(errno
));
532 strcpy(filename
, control
);
535 case 0x03 : /* Receive data file */
536 if (strlen(name
) < 2)
538 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
544 if (num_data
>= (sizeof(data
) / sizeof(data
[0])))
547 * Too many data files...
550 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
556 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
558 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
560 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
561 temp
[num_data
], strerror(errno
));
567 strcpy(filename
, temp
[num_data
]);
579 * Copy the data or control file from the client...
582 for (i
= atoi(count
); i
> 0; i
-= bytes
)
584 if (i
> sizeof(line
))
585 bytes
= sizeof(line
);
589 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
590 bytes
= write(fd
, line
, bytes
);
594 syslog(LOG_ERR
, "Error while reading file - %s",
602 * Read trailing nul...
607 if (fread(line
, 1, 1, stdin
) < 1)
610 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
616 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
622 * Close the file and send an acknowledgement...
636 * Process the control file and print stuff...
639 if ((fp
= fopen(control
, "rb")) == NULL
)
644 * Grab the job information first...
652 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
655 * Process control lines...
660 case 'J' : /* Job name */
661 strlcpy(title
, line
+ 1, sizeof(title
));
663 case 'N' : /* Document name */
664 strlcpy(docname
, line
+ 1, sizeof(docname
));
666 case 'P' : /* User identification */
667 strlcpy(user
, line
+ 1, sizeof(user
));
669 case 'L' : /* Print banner page */
679 * Then print the jobs...
684 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
687 * Process control lines...
692 case 'c' : /* Plot CIF file */
693 case 'd' : /* Print DVI file */
694 case 'f' : /* Print formatted file */
695 case 'g' : /* Plot file */
696 case 'l' : /* Print file leaving control characters (raw) */
697 case 'n' : /* Print ditroff output file */
698 case 'o' : /* Print PostScript output file */
699 case 'p' : /* Print file with 'pr' format (prettyprint) */
700 case 'r' : /* File to print with FORTRAN carriage control */
701 case 't' : /* Print troff output file */
702 case 'v' : /* Print raster file */
704 * Check that we have a username...
709 syslog(LOG_WARNING
, "No username specified by client! "
710 "Using \"anonymous\"...");
711 strcpy(user
, "anonymous");
715 * Copy the default options...
721 for (i
= 0; i
< num_defaults
; i
++)
722 num_options
= cupsAddOption(defaults
[i
].name
,
724 num_options
, &options
);
725 for (i
= 0; i
< destptr
->num_options
; i
++)
726 num_options
= cupsAddOption(destptr
->options
[i
].name
,
727 destptr
->options
[i
].value
,
728 num_options
, &options
);
731 * Add additional options as needed...
735 num_options
= cupsAddOption("job-sheets", "none",
736 num_options
, &options
);
739 num_options
= cupsAddOption("raw", "", num_options
, &options
);
742 num_options
= cupsAddOption("prettyprint", "", num_options
,
746 * Figure out which file we are printing...
749 for (i
= 0; i
< num_data
; i
++)
750 if (strcmp(data
[i
], line
+ 1) == 0)
760 * Send the print request...
763 if (print_file(queue
, temp
[i
], title
, docname
, user
, num_options
,
769 cupsFreeOptions(num_options
, options
);
782 * Clean up all temporary files and return...
787 for (i
= 0; i
< num_data
; i
++)
790 cupsFreeDests(num_dests
, dests
);
797 * 'remove_jobs()' - Cancel one or more jobs.
800 int /* O - Command status */
801 remove_jobs(const char *dest
, /* I - Destination */
802 const char *agent
, /* I - User agent */
803 const char *list
) /* I - List of jobs or users */
806 http_t
*http
; /* HTTP server connection */
807 ipp_t
*request
, /* IPP Request */
808 *response
; /* IPP Response */
809 cups_lang_t
*language
; /* Default language */
810 char uri
[HTTP_MAX_URI
]; /* Job URI */
813 (void)dest
; /* Suppress compiler warnings... */
816 * Try connecting to the local server...
819 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
820 cupsEncryption())) == NULL
)
822 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
827 language
= cupsLangDefault();
830 * Loop for each job...
833 while ((id
= atoi(list
)) > 0)
836 * Skip job ID in list...
839 while (isdigit(*list
))
841 while (isspace(*list
))
845 * Build an IPP_CANCEL_JOB request, which requires the following
849 * attributes-natural-language
851 * requesting-user-name
856 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
857 request
->request
.op
.request_id
= 1;
859 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
860 "attributes-charset", NULL
, cupsLangEncoding(language
));
862 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
863 "attributes-natural-language", NULL
, language
->language
);
865 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
866 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
868 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
869 "requesting-user-name", NULL
, agent
);
872 * Do the request and get back a response...
875 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
)
877 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
879 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
880 ippErrorString(response
->request
.status
.status_code
));
882 cupsLangFree(language
);
887 syslog(LOG_INFO
, "Job ID %d cancelled", id
);
893 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
894 ippErrorString(cupsLastError()));
895 cupsLangFree(language
);
901 cupsLangFree(language
);
909 * 'send_state()' - Send the queue state.
912 int /* O - Command status */
913 send_state(const char *dest
, /* I - Destination */
914 const char *list
, /* I - Job or user */
915 int longstatus
) /* I - List of jobs or users */
917 int id
; /* Job ID from list */
918 http_t
*http
; /* HTTP server connection */
919 ipp_t
*request
, /* IPP Request */
920 *response
; /* IPP Response */
921 ipp_attribute_t
*attr
; /* Current attribute */
922 cups_lang_t
*language
; /* Default language */
923 ipp_pstate_t state
; /* Printer state */
924 const char *jobdest
, /* Pointer into job-printer-uri */
925 *jobuser
, /* Pointer to job-originating-user-name */
926 *jobname
; /* Pointer to job-name */
927 ipp_jstate_t jobstate
; /* job-state */
928 int jobid
, /* job-id */
929 jobsize
, /* job-k-octets */
930 jobcount
, /* Number of jobs */
931 jobcopies
, /* Number of copies */
932 rank
; /* Rank of job */
933 char rankstr
[255]; /* Rank string */
934 char namestr
[1024]; /* Job name string */
935 char uri
[HTTP_MAX_URI
]; /* Printer URI */
936 char queue
[256], /* Printer/class queue */
937 *instance
; /* Printer/class instance */
938 static const char * const ranks
[10] = /* Ranking strings */
951 static const char * const requested
[] =
952 { /* Requested attributes */
957 "job-originating-user-name",
964 * Remove instance from destination, if any...
967 strlcpy(queue
, dest
, sizeof(queue
));
969 if ((instance
= strrchr(queue
, '/')) != NULL
)
973 * Try connecting to the local server...
976 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
977 cupsEncryption())) == NULL
)
979 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
981 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
986 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
990 * attributes-natural-language
996 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
997 request
->request
.op
.request_id
= 1;
999 language
= cupsLangDefault();
1001 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1002 "attributes-charset", NULL
, cupsLangEncoding(language
));
1004 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1005 "attributes-natural-language", NULL
, language
->language
);
1007 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", queue
);
1008 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1009 "printer-uri", NULL
, uri
);
1011 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1012 "requested-attributes", NULL
, "printer-state");
1015 * Do the request and get back a response...
1018 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1020 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1022 syslog(LOG_WARNING
, "Unable to get printer list: %s\n",
1023 ippErrorString(response
->request
.status
.status_code
));
1024 printf("Unable to get printer list: %s\n",
1025 ippErrorString(response
->request
.status
.status_code
));
1026 ippDelete(response
);
1030 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
1031 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1033 state
= IPP_PRINTER_STOPPED
;
1037 case IPP_PRINTER_IDLE
:
1038 printf("%s is ready\n", dest
);
1040 case IPP_PRINTER_PROCESSING
:
1041 printf("%s is ready and printing\n", dest
);
1043 case IPP_PRINTER_STOPPED
:
1044 printf("%s is not ready\n", dest
);
1048 ippDelete(response
);
1052 syslog(LOG_WARNING
, "Unable to get printer list: %s\n",
1053 ippErrorString(cupsLastError()));
1054 printf("Unable to get printer list: %s\n",
1055 ippErrorString(cupsLastError()));
1060 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1061 * the following attributes:
1063 * attributes-charset
1064 * attributes-natural-language
1065 * job-uri or printer-uri
1072 request
->request
.op
.operation_id
= id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
1073 request
->request
.op
.request_id
= 1;
1075 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1076 "attributes-charset", NULL
, cupsLangEncoding(language
));
1078 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1079 "attributes-natural-language", NULL
, language
->language
);
1081 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", queue
);
1083 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1087 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1090 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1091 "requesting-user-name", NULL
, list
);
1092 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1095 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1096 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
1100 * Do the request and get back a response...
1105 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1107 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1109 printf("get-jobs failed: %s\n",
1110 ippErrorString(response
->request
.status
.status_code
));
1111 ippDelete(response
);
1118 * Loop through the job list and display them...
1121 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1124 * Skip leading attributes until we hit a job...
1127 while (attr
!= NULL
&&
1128 (attr
->group_tag
!= IPP_TAG_JOB
|| attr
->name
== NULL
))
1135 * Pull the needed attributes from this job...
1140 jobstate
= IPP_JOB_PENDING
;
1141 jobname
= "untitled";
1146 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
1148 if (strcmp(attr
->name
, "job-id") == 0 &&
1149 attr
->value_tag
== IPP_TAG_INTEGER
)
1150 jobid
= attr
->values
[0].integer
;
1152 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1153 attr
->value_tag
== IPP_TAG_INTEGER
)
1154 jobsize
= attr
->values
[0].integer
* 1024;
1156 if (strcmp(attr
->name
, "job-state") == 0 &&
1157 attr
->value_tag
== IPP_TAG_ENUM
)
1158 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1160 if (strcmp(attr
->name
, "job-printer-uri") == 0 &&
1161 attr
->value_tag
== IPP_TAG_URI
)
1162 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1165 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1166 attr
->value_tag
== IPP_TAG_NAME
)
1167 jobuser
= attr
->values
[0].string
.text
;
1169 if (strcmp(attr
->name
, "job-name") == 0 &&
1170 attr
->value_tag
== IPP_TAG_NAME
)
1171 jobname
= attr
->values
[0].string
.text
;
1173 if (strcmp(attr
->name
, "copies") == 0 &&
1174 attr
->value_tag
== IPP_TAG_INTEGER
)
1175 jobcopies
= attr
->values
[0].integer
;
1181 * See if we have everything needed...
1184 if (jobdest
== NULL
|| jobid
== 0)
1192 if (!longstatus
&& jobcount
== 0)
1193 puts("Rank Owner Job File(s) Total Size");
1198 * Display the job...
1201 if (jobstate
== IPP_JOB_PROCESSING
)
1202 strcpy(rankstr
, "active");
1205 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1214 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1217 strlcpy(namestr
, jobname
, sizeof(namestr
));
1219 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1220 printf(" %-39.39s %d bytes\n", namestr
, jobsize
);
1223 printf("%-7s %-7.7s %-7d %-31.31s %d bytes\n", rankstr
, jobuser
,
1224 jobid
, jobname
, jobsize
);
1230 ippDelete(response
);
1234 printf("get-jobs failed: %s\n", ippErrorString(cupsLastError()));
1241 cupsLangFree(language
);
1249 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1252 char * /* O - Line read or NULL */
1253 smart_gets(char *s
, /* I - Pointer to line buffer */
1254 int len
, /* I - Size of line buffer */
1255 FILE *fp
) /* I - File to read from */
1257 char *ptr
, /* Pointer into line */
1258 *end
; /* End of line */
1259 int ch
; /* Character from file */
1263 * Read the line; unlike fgets(), we read the entire line but dump
1264 * characters that go past the end of the buffer. Also, we accept
1265 * CR, LF, or CR LF for the line endings to be "safe", although
1266 * RFC 1179 specifically says "just use LF".
1272 while ((ch
= getc(fp
)) != EOF
)
1276 else if (ch
== '\r')
1279 * See if a LF follows...
1295 if (ch
== EOF
&& ptr
== s
)
1303 * End of "$Id: cups-lpd.c,v 1.24.2.15 2004/02/17 21:32:58 mike Exp $".