2 * "$Id: lpc.c 10379 2012-03-23 22:16:22Z mike $"
4 * "lpc" command for CUPS.
6 * Copyright 2007-2012 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 * main() - Parse options and commands.
18 * compare_strings() - Compare two command-line strings.
19 * do_command() - Do an lpc command...
20 * show_help() - Show help messages.
21 * show_status() - Show printers.
25 * Include necessary headers...
28 #include <cups/cups-private.h>
35 static int compare_strings(const char *, const char *, int);
36 static void do_command(http_t *, const char *, const char *);
37 static void show_help(const char *);
38 static void show_status(http_t *, const char *);
42 * 'main()' - Parse options and commands.
46 main(int argc, /* I - Number of command-line arguments */
47 char *argv[]) /* I - Command-line arguments */
49 http_t *http; /* Connection to server */
50 char line[1024], /* Input line from user */
51 *params; /* Pointer to parameters */
57 * Connect to the scheduler...
60 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
65 * Process a single command on the command-line...
68 do_command(http, argv[1], argv[2]);
73 * Do the command prompt thing...
76 _cupsLangPuts(stdout, _("lpc> ")); /* TODO: Need no-newline version */
77 while (fgets(line, sizeof(line), stdin) != NULL)
80 * Strip trailing whitespace...
83 for (params = line + strlen(line) - 1; params >= line;)
84 if (!isspace(*params & 255))
90 * Strip leading whitespace...
93 for (params = line; isspace(*params & 255); params ++);
96 _cups_strcpy(line, params);
101 * Nothing left, just show a prompt...
104 _cupsLangPuts(stdout, _("lpc> ")); /* TODO: Need no newline version */
109 * Find any options in the string...
112 for (params = line; *params != '\0'; params ++)
113 if (isspace(*params & 255))
117 * Remove whitespace between the command and parameters...
120 while (isspace(*params & 255))
124 * The "quit" and "exit" commands exit; otherwise, process as needed...
127 if (!compare_strings(line, "quit", 1) ||
128 !compare_strings(line, "exit", 2))
132 do_command(http, line, NULL);
134 do_command(http, line, params);
137 * Put another prompt out to the user...
140 _cupsLangPuts(stdout, _("lpc> ")); /* TODO: Need no newline version */
145 * Close the connection to the server and return...
155 * 'compare_strings()' - Compare two command-line strings.
158 static int /* O - -1 or 1 = no match, 0 = match */
159 compare_strings(const char *s, /* I - Command-line string */
160 const char *t, /* I - Option string */
161 int tmin) /* I - Minimum number of unique chars in option */
163 int slen; /* Length of command-line string */
170 return (strncmp(s, t, slen));
175 * 'do_command()' - Do an lpc command...
179 do_command(http_t *http, /* I - HTTP connection to server */
180 const char *command, /* I - Command string */
181 const char *params) /* I - Parameters for command */
183 if (!compare_strings(command, "status", 4))
184 show_status(http, params);
185 else if (!compare_strings(command, "help", 1) || !strcmp(command, "?"))
188 _cupsLangPrintf(stdout,
189 _("%s is not implemented by the CUPS version of lpc."),
195 * 'show_help()' - Show help messages.
199 show_help(const char *command) /* I - Command to describe or NULL */
203 _cupsLangPrintf(stdout,
204 _("Commands may be abbreviated. Commands are:\n"
206 "exit help quit status ?"));
208 else if (!compare_strings(command, "help", 1) || !strcmp(command, "?"))
209 _cupsLangPrintf(stdout, _("help\t\tGet help on commands."));
210 else if (!compare_strings(command, "status", 4))
211 _cupsLangPrintf(stdout, _("status\t\tShow status of daemon and queue."));
213 _cupsLangPrintf(stdout, _("?Invalid help command unknown."));
218 * 'show_status()' - Show printers.
222 show_status(http_t *http, /* I - HTTP connection to server */
223 const char *dests) /* I - Destinations */
225 ipp_t *request, /* IPP Request */
226 *response; /* IPP Response */
227 ipp_attribute_t *attr; /* Current attribute */
228 char *printer, /* Printer name */
229 *device, /* Device URI */
230 *delimiter; /* Char search result */
231 ipp_pstate_t pstate; /* Printer state */
232 int accepting; /* Is printer accepting jobs? */
233 int jobcount; /* Count of current jobs */
234 const char *dptr, /* Pointer into destination list */
235 *ptr; /* Pointer into printer name */
236 int match; /* Non-zero if this job matches */
237 static const char *requested[] = /* Requested attributes */
240 "printer-is-accepting-jobs",
247 DEBUG_printf(("show_status(http=%p, dests=\"%s\")\n", http, dests));
253 * Build a CUPS_GET_PRINTERS request, which requires the following
257 * attributes-natural-language
260 request = ippNewRequest(CUPS_GET_PRINTERS);
262 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
263 "requested-attributes", sizeof(requested) / sizeof(requested[0]),
267 * Do the request and get back a response...
270 if ((response = cupsDoRequest(http, request, "/")) != NULL)
272 DEBUG_puts("show_status: request succeeded...");
275 * Loop through the printers returned in the list and display
279 for (attr = response->attrs; attr != NULL; attr = attr->next)
282 * Skip leading attributes until we hit a job...
285 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
292 * Pull the needed attributes from this job...
296 device = "file:/dev/null";
297 pstate = IPP_PRINTER_IDLE;
301 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
303 if (!strcmp(attr->name, "device-uri") &&
304 attr->value_tag == IPP_TAG_URI)
305 device = attr->values[0].string.text;
306 else if (!strcmp(attr->name, "printer-is-accepting-jobs") &&
307 attr->value_tag == IPP_TAG_BOOLEAN)
308 accepting = attr->values[0].boolean;
309 else if (!strcmp(attr->name, "printer-name") &&
310 attr->value_tag == IPP_TAG_NAME)
311 printer = attr->values[0].string.text;
312 else if (!strcmp(attr->name, "printer-state") &&
313 attr->value_tag == IPP_TAG_ENUM)
314 pstate = (ipp_pstate_t)attr->values[0].integer;
315 else if (!strcmp(attr->name, "queued-job-count") &&
316 attr->value_tag == IPP_TAG_INTEGER)
317 jobcount = attr->values[0].integer;
323 * See if we have everything needed...
335 * A single 'all' printer name is special, meaning all printers.
338 if (dests != NULL && !strcmp(dests, "all"))
342 * See if this is a printer we're interested in...
345 match = dests == NULL;
349 for (dptr = dests; *dptr != '\0';)
352 * Skip leading whitespace and commas...
355 while (isspace(*dptr & 255) || *dptr == ',')
366 *ptr != '\0' && *dptr != '\0' && *ptr == *dptr;
370 if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' ||
371 isspace(*dptr & 255)))
378 * Skip trailing junk...
381 while (!isspace(*dptr & 255) && *dptr != '\0')
383 while (isspace(*dptr & 255) || *dptr == ',')
392 * Display the printer entry if needed...
401 printf("%s:\n", printer);
402 if (!strncmp(device, "file:", 5))
403 _cupsLangPrintf(stdout,
404 _("\tprinter is on device \'%s\' speed -1"),
409 * Just show the scheme...
412 if ((delimiter = strchr(device, ':')) != NULL )
415 _cupsLangPrintf(stdout,
416 _("\tprinter is on device \'%s\' speed -1"),
422 _cupsLangPuts(stdout, _("\tqueuing is enabled"));
424 _cupsLangPuts(stdout, _("\tqueuing is disabled"));
426 if (pstate != IPP_PRINTER_STOPPED)
427 _cupsLangPuts(stdout, _("\tprinting is enabled"));
429 _cupsLangPuts(stdout, _("\tprinting is disabled"));
432 _cupsLangPuts(stdout, _("\tno entries"));
434 _cupsLangPrintf(stdout, _("\t%d entries"), jobcount);
436 _cupsLangPuts(stdout, _("\tdaemon present"));
449 * End of "$Id: lpc.c 10379 2012-03-23 22:16:22Z mike $".