]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
2 * "$Id: lpc.c,v 1.12 2001/05/06 00:11:22 mike Exp $"
4 * "lpc" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2001 by Easy Software Products.
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() - Parse options and commands.
27 * compare_strings() - Compare two command-line strings.
28 * do_command() - Do an lpc command...
29 * show_help() - Show help messages.
30 * show_status() - Show printers.
34 * Include necessary headers...
40 #include <cups/cups.h>
41 #include <cups/language.h>
42 #include <cups/debug.h>
49 static int compare_strings(char *, char *, int);
50 static void do_command(http_t
*, char *, char *);
51 static void show_help(char *);
52 static void show_status(http_t
*, char *);
56 * 'main()' - Parse options and commands.
60 main(int argc
, /* I - Number of command-line arguments */
61 char *argv
[]) /* I - Command-line arguments */
63 http_t
*http
; /* Connection to server */
64 char line
[1024], /* Input line from user */
65 *params
; /* Pointer to parameters */
69 * Connect to the scheduler...
72 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
77 * Process a single command on the command-line...
80 do_command(http
, argv
[1], argv
[2]);
85 * Do the command prompt thing...
89 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
92 * Strip the trailing newline...
95 line
[strlen(line
) - 1] = '\0';
98 * Find any options in the string...
101 while (isspace(line
[0]))
102 strcpy(line
, line
+ 1);
104 for (params
= line
; *params
!= '\0'; params
++)
105 if (isspace(*params
))
109 * Remove whitespace between the command and parameters...
112 while (isspace(*params
))
116 * The "quit" and "exit" commands exit; otherwise, process as needed...
119 if (compare_strings(line
, "quit", 1) == 0 ||
120 compare_strings(line
, "exit", 2) == 0)
124 do_command(http
, line
, NULL
);
126 do_command(http
, line
, params
);
129 * Put another prompt out to the user...
137 * Close the connection to the server and return...
147 * 'compare_strings()' - Compare two command-line strings.
150 static int /* O - -1 or 1 = no match, 0 = match */
151 compare_strings(char *s
, /* I - Command-line string */
152 char *t
, /* I - Option string */
153 int tmin
) /* I - Minimum number of unique chars in option */
155 int slen
; /* Length of command-line string */
162 return (strncmp(s
, t
, slen
));
167 * 'do_command()' - Do an lpc command...
171 do_command(http_t
*http
, /* I - HTTP connection to server */
172 char *command
, /* I - Command string */
173 char *params
) /* I - Parameters for command */
175 if (compare_strings(command
, "status", 4) == 0)
176 show_status(http
, params
);
177 else if (compare_strings(command
, "help", 1) == 0 ||
178 strcmp(command
, "?") == 0)
181 printf("%s is not implemented by the CUPS version of lpc.\n", command
);
186 * 'show_help()' - Show help messages.
190 show_help(char *command
) /* I - Command to describe or NULL */
194 puts("Commands may be abbreviated. Commands are:");
196 puts("exit help quit status ?");
198 else if (compare_strings(command
, "help", 1) == 0 ||
199 strcmp(command
, "?") == 0)
200 puts("help\t\tget help on commands");
201 else if (compare_strings(command
, "status", 4) == 0)
202 puts("status\t\tshow status of daemon and queue");
204 puts("?Invalid help command unknown");
209 * 'show_status()' - Show printers.
213 show_status(http_t
*http
, /* I - HTTP connection to server */
214 char *dests
) /* I - Destinations */
216 ipp_t
*request
, /* IPP Request */
217 *response
, /* IPP Response */
218 *jobs
; /* IPP Get Jobs response */
219 ipp_attribute_t
*attr
, /* Current attribute */
220 *jattr
; /* Current job attribute */
221 cups_lang_t
*language
; /* Default language */
222 char *printer
, /* Printer name */
223 *device
; /* Device URI */
224 ipp_pstate_t pstate
; /* Printer state */
225 int accepting
; /* Is printer accepting jobs? */
226 int jobcount
; /* Count of current jobs */
227 char *dptr
, /* Pointer into destination list */
228 *ptr
; /* Pointer into printer name */
229 int match
; /* Non-zero if this job matches */
230 char printer_uri
[HTTP_MAX_URI
];
232 static const char *requested
[] =
233 { /* Requested attributes */
237 "printer-is-accepting-jobs"
241 DEBUG_printf(("show_status(%08x, %08x)\n", http
, dests
));
247 * Build a CUPS_GET_PRINTERS request, which requires the following
251 * attributes-natural-language
256 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
257 request
->request
.op
.request_id
= 1;
259 language
= cupsLangDefault();
261 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
262 "attributes-charset", NULL
, cupsLangEncoding(language
));
264 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
265 "attributes-natural-language", NULL
, language
->language
);
267 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
268 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
272 * Do the request and get back a response...
275 if ((response
= cupsDoRequest(http
, request
, "/printers/")) != NULL
)
277 DEBUG_puts("show_status: request succeeded...");
280 * Loop through the printers returned in the list and display
284 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
287 * Skip leading attributes until we hit a job...
290 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
297 * Pull the needed attributes from this job...
301 device
= "file:/dev/null";
302 pstate
= IPP_PRINTER_IDLE
;
306 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
308 if (strcmp(attr
->name
, "printer-name") == 0 &&
309 attr
->value_tag
== IPP_TAG_NAME
)
310 printer
= attr
->values
[0].string
.text
;
312 if (strcmp(attr
->name
, "device-uri") == 0 &&
313 attr
->value_tag
== IPP_TAG_URI
)
314 device
= attr
->values
[0].string
.text
;
316 if (strcmp(attr
->name
, "printer-state") == 0 &&
317 attr
->value_tag
== IPP_TAG_ENUM
)
318 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
320 if (strcmp(attr
->name
, "printer-is-accepting-jobs") == 0 &&
321 attr
->value_tag
== IPP_TAG_BOOLEAN
)
322 accepting
= attr
->values
[0].boolean
;
328 * See if we have everything needed...
340 * See if this is a printer we're interested in...
343 match
= dests
== NULL
;
347 for (dptr
= dests
; *dptr
!= '\0';)
350 * Skip leading whitespace and commas...
353 while (isspace(*dptr
) || *dptr
== ',')
364 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
367 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' || isspace(*dptr
)))
374 * Skip trailing junk...
377 while (!isspace(*dptr
) && *dptr
!= '\0')
379 while (isspace(*dptr
) || *dptr
== ',')
388 * Display the printer entry if needed...
394 * If the printer state is "IPP_PRINTER_PROCESSING", then grab the
395 * current job for the printer.
398 if (pstate
== IPP_PRINTER_PROCESSING
)
401 * Build an IPP_GET_JOBS request, which requires the following
405 * attributes-natural-language
412 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
413 request
->request
.op
.request_id
= 1;
415 language
= cupsLangDefault();
417 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
418 "attributes-charset", NULL
,
419 cupsLangEncoding(language
));
421 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
422 "attributes-natural-language", NULL
,
425 snprintf(printer_uri
, sizeof(printer_uri
),
426 "ipp://localhost/printers/%s", printer
);
427 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
428 "printer-uri", NULL
, printer_uri
);
430 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
431 "requested-attributes", NULL
, "job-id");
433 if ((jobs
= cupsDoRequest(http
, request
, "/jobs/")) != NULL
)
435 for (jattr
= jobs
->attrs
; jattr
!= NULL
; jattr
= jattr
->next
)
436 if (jattr
->name
&& strcmp(jattr
->name
, "job-id") == 0)
447 printf("%s:\n", printer
);
448 if (strncmp(device
, "file:", 5) == 0)
449 printf("\tprinter is on device \'%s\' speed -1\n", device
+ 5);
453 * Just show the method...
456 *strchr(device
, ':') = '\0';
457 printf("\tprinter is on device \'%s\' speed -1\n", device
);
460 printf("\tqueuing is %sabled\n", accepting
? "en" : "dis");
461 printf("\tprinting is %sabled\n",
462 pstate
== IPP_PRINTER_STOPPED
? "dis" : "en");
464 puts("\tno entries");
466 printf("\t%d entries\n", jobcount
);
467 puts("\tdaemon present");
480 * End of "$Id: lpc.c,v 1.12 2001/05/06 00:11:22 mike Exp $".