]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
2 * "$Id: lpc.c 5833 2006-08-16 20:05:58Z mike $"
4 * "lpc" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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 USA
20 * Voice: (301) 373-9600
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...
39 #include <cups/cups.h>
40 #include <cups/i18n.h>
41 #include <cups/debug.h>
42 #include <cups/string.h>
49 static int compare_strings(const char *, const char *, int);
50 static void do_command(http_t
*, const char *, const char *);
51 static void show_help(const char *);
52 static void show_status(http_t
*, const 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 */
71 * Connect to the scheduler...
74 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
79 * Process a single command on the command-line...
82 do_command(http
, argv
[1], argv
[2]);
87 * Do the command prompt thing...
90 _cupsLangPuts(stdout
, _("lpc> "));
91 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
94 * Strip trailing whitespace...
97 for (params
= line
+ strlen(line
) - 1; params
>= line
;)
98 if (!isspace(*params
& 255))
104 * Strip leading whitespace...
107 for (params
= line
; isspace(*params
& 255); params
++);
110 _cups_strcpy(line
, params
);
115 * Nothing left, just show a prompt...
118 _cupsLangPuts(stdout
, _("lpc> "));
123 * Find any options in the string...
126 for (params
= line
; *params
!= '\0'; params
++)
127 if (isspace(*params
& 255))
131 * Remove whitespace between the command and parameters...
134 while (isspace(*params
& 255))
138 * The "quit" and "exit" commands exit; otherwise, process as needed...
141 if (!compare_strings(line
, "quit", 1) ||
142 !compare_strings(line
, "exit", 2))
146 do_command(http
, line
, NULL
);
148 do_command(http
, line
, params
);
151 * Put another prompt out to the user...
154 _cupsLangPuts(stdout
, _("lpc> "));
159 * Close the connection to the server and return...
169 * 'compare_strings()' - Compare two command-line strings.
172 static int /* O - -1 or 1 = no match, 0 = match */
173 compare_strings(const char *s
, /* I - Command-line string */
174 const char *t
, /* I - Option string */
175 int tmin
) /* I - Minimum number of unique chars in option */
177 int slen
; /* Length of command-line string */
184 return (strncmp(s
, t
, slen
));
189 * 'do_command()' - Do an lpc command...
193 do_command(http_t
*http
, /* I - HTTP connection to server */
194 const char *command
, /* I - Command string */
195 const char *params
) /* I - Parameters for command */
197 if (!compare_strings(command
, "status", 4))
198 show_status(http
, params
);
199 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
202 _cupsLangPrintf(stdout
,
203 _("%s is not implemented by the CUPS version of lpc.\n"),
209 * 'show_help()' - Show help messages.
213 show_help(const char *command
) /* I - Command to describe or NULL */
217 _cupsLangPrintf(stdout
,
218 _("Commands may be abbreviated. Commands are:\n"
220 "exit help quit status ?\n"));
222 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
223 _cupsLangPrintf(stdout
, _("help\t\tget help on commands\n"));
224 else if (!compare_strings(command
, "status", 4))
225 _cupsLangPrintf(stdout
, _("status\t\tshow status of daemon and queue\n"));
227 _cupsLangPrintf(stdout
, _("?Invalid help command unknown\n"));
232 * 'show_status()' - Show printers.
236 show_status(http_t
*http
, /* I - HTTP connection to server */
237 const char *dests
) /* I - Destinations */
239 ipp_t
*request
, /* IPP Request */
240 *response
, /* IPP Response */
241 *jobs
; /* IPP Get Jobs response */
242 ipp_attribute_t
*attr
, /* Current attribute */
243 *jattr
; /* Current job attribute */
244 cups_lang_t
*language
; /* Default language */
245 char *printer
, /* Printer name */
246 *device
, /* Device URI */
247 *delimiter
; /* Char search result */
248 ipp_pstate_t pstate
; /* Printer state */
249 int accepting
; /* Is printer accepting jobs? */
250 int jobcount
; /* Count of current jobs */
251 const char *dptr
, /* Pointer into destination list */
252 *ptr
; /* Pointer into printer name */
253 int match
; /* Non-zero if this job matches */
254 char printer_uri
[HTTP_MAX_URI
];
256 static const char *requested
[] = /* Requested attributes */
261 "printer-is-accepting-jobs"
265 DEBUG_printf(("show_status(http=%p, dests=\"%s\")\n", http
, dests
));
271 * Build a CUPS_GET_PRINTERS request, which requires the following
275 * attributes-natural-language
280 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
281 request
->request
.op
.request_id
= 1;
283 language
= cupsLangDefault();
285 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
286 "attributes-charset", NULL
, cupsLangEncoding(language
));
288 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
289 "attributes-natural-language", NULL
, language
->language
);
291 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
292 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
296 * Do the request and get back a response...
299 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
301 DEBUG_puts("show_status: request succeeded...");
304 * Loop through the printers returned in the list and display
308 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
311 * Skip leading attributes until we hit a job...
314 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
321 * Pull the needed attributes from this job...
325 device
= "file:/dev/null";
326 pstate
= IPP_PRINTER_IDLE
;
330 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
332 if (!strcmp(attr
->name
, "printer-name") &&
333 attr
->value_tag
== IPP_TAG_NAME
)
334 printer
= attr
->values
[0].string
.text
;
336 if (!strcmp(attr
->name
, "device-uri") &&
337 attr
->value_tag
== IPP_TAG_URI
)
338 device
= attr
->values
[0].string
.text
;
340 if (!strcmp(attr
->name
, "printer-state") &&
341 attr
->value_tag
== IPP_TAG_ENUM
)
342 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
344 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
345 attr
->value_tag
== IPP_TAG_BOOLEAN
)
346 accepting
= attr
->values
[0].boolean
;
352 * See if we have everything needed...
364 * A single 'all' printer name is special, meaning all printers.
367 if (dests
!= NULL
&& !strcmp(dests
, "all"))
371 * See if this is a printer we're interested in...
374 match
= dests
== NULL
;
378 for (dptr
= dests
; *dptr
!= '\0';)
381 * Skip leading whitespace and commas...
384 while (isspace(*dptr
& 255) || *dptr
== ',')
395 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
398 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' || isspace(*dptr
& 255)))
405 * Skip trailing junk...
408 while (!isspace(*dptr
& 255) && *dptr
!= '\0')
410 while (isspace(*dptr
& 255) || *dptr
== ',')
419 * Display the printer entry if needed...
425 * Build an IPP_GET_JOBS request, which requires the following
429 * attributes-natural-language
436 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
437 request
->request
.op
.request_id
= 1;
439 language
= cupsLangDefault();
441 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
442 "attributes-charset", NULL
,
443 cupsLangEncoding(language
));
445 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
446 "attributes-natural-language", NULL
,
449 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
),
450 "ipp", NULL
, "localhost", 631, "/printers/%s",
452 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
453 "printer-uri", NULL
, printer_uri
);
455 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
456 "requested-attributes", NULL
, "job-id");
458 if ((jobs
= cupsDoRequest(http
, request
, "/")) != NULL
)
461 * Grab the number of jobs for the printer.
464 for (jattr
= jobs
->attrs
; jattr
!= NULL
; jattr
= jattr
->next
)
465 if (jattr
->name
&& !strcmp(jattr
->name
, "job-id"))
475 printf("%s:\n", printer
);
476 if (!strncmp(device
, "file:", 5))
477 _cupsLangPrintf(stdout
,
478 _("\tprinter is on device \'%s\' speed -1\n"),
483 * Just show the scheme...
486 if ((delimiter
= strchr(device
, ':')) != NULL
)
489 _cupsLangPrintf(stdout
,
490 _("\tprinter is on device \'%s\' speed -1\n"),
496 _cupsLangPuts(stdout
, _("\tqueuing is enabled\n"));
498 _cupsLangPuts(stdout
, _("\tqueuing is disabled\n"));
500 if (pstate
!= IPP_PRINTER_STOPPED
)
501 _cupsLangPuts(stdout
, _("\tprinting is enabled\n"));
503 _cupsLangPuts(stdout
, _("\tprinting is disabled\n"));
506 _cupsLangPuts(stdout
, _("\tno entries\n"));
508 _cupsLangPrintf(stdout
, _("\t%d entries\n"), jobcount
);
510 _cupsLangPuts(stdout
, _("\tdaemon present\n"));
523 * End of "$Id: lpc.c 5833 2006-08-16 20:05:58Z mike $".