]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
4 * "lpc" command for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * Include necessary headers...
20 #include <cups/cups-private.h>
27 static int compare_strings(const char *, const char *, size_t);
28 static void do_command(http_t
*, const char *, const char *);
29 static void show_help(const char *);
30 static void show_status(http_t
*, const char *);
34 * 'main()' - Parse options and commands.
38 main(int argc
, /* I - Number of command-line arguments */
39 char *argv
[]) /* I - Command-line arguments */
41 http_t
*http
; /* Connection to server */
42 char line
[1024], /* Input line from user */
43 *params
; /* Pointer to parameters */
49 * Connect to the scheduler...
52 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
57 * Process a single command on the command-line...
60 do_command(http
, argv
[1], argv
[2]);
65 * Do the command prompt thing...
68 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no-newline version */
69 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
72 * Strip trailing whitespace...
75 for (params
= line
+ strlen(line
) - 1; params
>= line
;)
76 if (!isspace(*params
& 255))
82 * Strip leading whitespace...
85 for (params
= line
; isspace(*params
& 255); params
++);
88 _cups_strcpy(line
, params
);
93 * Nothing left, just show a prompt...
96 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no newline version */
101 * Find any options in the string...
104 for (params
= line
; *params
!= '\0'; params
++)
105 if (isspace(*params
& 255))
109 * Remove whitespace between the command and parameters...
112 while (isspace(*params
& 255))
116 * The "quit" and "exit" commands exit; otherwise, process as needed...
119 if (!compare_strings(line
, "quit", 1) ||
120 !compare_strings(line
, "exit", 2))
124 do_command(http
, line
, NULL
);
126 do_command(http
, line
, params
);
129 * Put another prompt out to the user...
132 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no newline version */
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(const char *s
, /* I - Command-line string */
152 const char *t
, /* I - Option string */
153 size_t tmin
) /* I - Minimum number of unique chars in option */
155 size_t 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 const char *command
, /* I - Command string */
173 const char *params
) /* I - Parameters for command */
175 if (!compare_strings(command
, "status", 4))
176 show_status(http
, params
);
177 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
180 _cupsLangPrintf(stdout
,
181 _("%s is not implemented by the CUPS version of lpc."),
187 * 'show_help()' - Show help messages.
191 show_help(const char *command
) /* I - Command to describe or NULL */
195 _cupsLangPrintf(stdout
,
196 _("Commands may be abbreviated. Commands are:\n"
198 "exit help quit status ?"));
200 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
201 _cupsLangPrintf(stdout
, _("help\t\tGet help on commands."));
202 else if (!compare_strings(command
, "status", 4))
203 _cupsLangPrintf(stdout
, _("status\t\tShow status of daemon and queue."));
205 _cupsLangPrintf(stdout
, _("?Invalid help command unknown."));
210 * 'show_status()' - Show printers.
214 show_status(http_t
*http
, /* I - HTTP connection to server */
215 const char *dests
) /* I - Destinations */
217 ipp_t
*request
, /* IPP Request */
218 *response
; /* IPP Response */
219 ipp_attribute_t
*attr
; /* Current attribute */
220 char *printer
, /* Printer name */
221 *device
, /* Device URI */
222 *delimiter
; /* Char search result */
223 ipp_pstate_t pstate
; /* Printer state */
224 int accepting
; /* Is printer accepting jobs? */
225 int jobcount
; /* Count of current jobs */
226 const char *dptr
, /* Pointer into destination list */
227 *ptr
; /* Pointer into printer name */
228 int match
; /* Non-zero if this job matches */
229 static const char *requested
[] = /* Requested attributes */
232 "printer-is-accepting-jobs",
239 DEBUG_printf(("show_status(http=%p, dests=\"%s\")\n", http
, dests
));
245 * Build a CUPS_GET_PRINTERS request, which requires the following
249 * attributes-natural-language
252 request
= ippNewRequest(CUPS_GET_PRINTERS
);
254 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
255 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
259 * Do the request and get back a response...
262 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
264 DEBUG_puts("show_status: request succeeded...");
267 * Loop through the printers returned in the list and display
271 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
274 * Skip leading attributes until we hit a job...
277 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
284 * Pull the needed attributes from this job...
288 device
= "file:/dev/null";
289 pstate
= IPP_PRINTER_IDLE
;
293 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
295 if (!strcmp(attr
->name
, "device-uri") &&
296 attr
->value_tag
== IPP_TAG_URI
)
297 device
= attr
->values
[0].string
.text
;
298 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
299 attr
->value_tag
== IPP_TAG_BOOLEAN
)
300 accepting
= attr
->values
[0].boolean
;
301 else if (!strcmp(attr
->name
, "printer-name") &&
302 attr
->value_tag
== IPP_TAG_NAME
)
303 printer
= attr
->values
[0].string
.text
;
304 else if (!strcmp(attr
->name
, "printer-state") &&
305 attr
->value_tag
== IPP_TAG_ENUM
)
306 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
307 else if (!strcmp(attr
->name
, "queued-job-count") &&
308 attr
->value_tag
== IPP_TAG_INTEGER
)
309 jobcount
= attr
->values
[0].integer
;
315 * See if we have everything needed...
327 * A single 'all' printer name is special, meaning all printers.
330 if (dests
!= NULL
&& !strcmp(dests
, "all"))
334 * See if this is a printer we're interested in...
337 match
= dests
== NULL
;
341 for (dptr
= dests
; *dptr
!= '\0';)
344 * Skip leading whitespace and commas...
347 while (isspace(*dptr
& 255) || *dptr
== ',')
358 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
362 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' ||
363 isspace(*dptr
& 255)))
370 * Skip trailing junk...
373 while (!isspace(*dptr
& 255) && *dptr
!= '\0')
375 while (isspace(*dptr
& 255) || *dptr
== ',')
384 * Display the printer entry if needed...
393 printf("%s:\n", printer
);
394 if (!strncmp(device
, "file:", 5))
395 _cupsLangPrintf(stdout
,
396 _("\tprinter is on device \'%s\' speed -1"),
401 * Just show the scheme...
404 if ((delimiter
= strchr(device
, ':')) != NULL
)
407 _cupsLangPrintf(stdout
,
408 _("\tprinter is on device \'%s\' speed -1"),
414 _cupsLangPuts(stdout
, _("\tqueuing is enabled"));
416 _cupsLangPuts(stdout
, _("\tqueuing is disabled"));
418 if (pstate
!= IPP_PRINTER_STOPPED
)
419 _cupsLangPuts(stdout
, _("\tprinting is enabled"));
421 _cupsLangPuts(stdout
, _("\tprinting is disabled"));
424 _cupsLangPuts(stdout
, _("\tno entries"));
426 _cupsLangPrintf(stdout
, _("\t%d entries"), jobcount
);
428 _cupsLangPuts(stdout
, _("\tdaemon present"));