]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
93e029fa7fddd2f04d813896e1221d808612113c
2 * "lpc" command for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/cups-private.h>
25 static int compare_strings(const char *, const char *, size_t);
26 static void do_command(http_t
*, const char *, const char *);
27 static void show_help(const char *);
28 static void show_status(http_t
*, const char *);
32 * 'main()' - Parse options and commands.
36 main(int argc
, /* I - Number of command-line arguments */
37 char *argv
[]) /* I - Command-line arguments */
39 http_t
*http
; /* Connection to server */
40 char line
[1024], /* Input line from user */
41 *params
; /* Pointer to parameters */
47 * Connect to the scheduler...
50 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
55 * Process a single command on the command-line...
58 do_command(http
, argv
[1], argv
[2]);
63 * Do the command prompt thing...
66 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no-newline version */
67 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
70 * Strip trailing whitespace...
73 for (params
= line
+ strlen(line
) - 1; params
>= line
;)
74 if (!isspace(*params
& 255))
80 * Strip leading whitespace...
83 for (params
= line
; isspace(*params
& 255); params
++);
86 _cups_strcpy(line
, params
);
91 * Nothing left, just show a prompt...
94 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no newline version */
99 * Find any options in the string...
102 for (params
= line
; *params
!= '\0'; params
++)
103 if (isspace(*params
& 255))
107 * Remove whitespace between the command and parameters...
110 while (isspace(*params
& 255))
114 * The "quit" and "exit" commands exit; otherwise, process as needed...
117 if (!compare_strings(line
, "quit", 1) ||
118 !compare_strings(line
, "exit", 2))
122 do_command(http
, line
, NULL
);
124 do_command(http
, line
, params
);
127 * Put another prompt out to the user...
130 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no newline version */
135 * Close the connection to the server and return...
145 * 'compare_strings()' - Compare two command-line strings.
148 static int /* O - -1 or 1 = no match, 0 = match */
149 compare_strings(const char *s
, /* I - Command-line string */
150 const char *t
, /* I - Option string */
151 size_t tmin
) /* I - Minimum number of unique chars in option */
153 size_t slen
; /* Length of command-line string */
160 return (strncmp(s
, t
, slen
));
165 * 'do_command()' - Do an lpc command...
169 do_command(http_t
*http
, /* I - HTTP connection to server */
170 const char *command
, /* I - Command string */
171 const char *params
) /* I - Parameters for command */
173 if (!compare_strings(command
, "status", 4))
174 show_status(http
, params
);
175 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
178 _cupsLangPrintf(stdout
,
179 _("%s is not implemented by the CUPS version of lpc."),
185 * 'show_help()' - Show help messages.
189 show_help(const char *command
) /* I - Command to describe or NULL */
193 _cupsLangPrintf(stdout
,
194 _("Commands may be abbreviated. Commands are:\n"
196 "exit help quit status ?"));
198 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
199 _cupsLangPrintf(stdout
, _("help\t\tGet help on commands."));
200 else if (!compare_strings(command
, "status", 4))
201 _cupsLangPrintf(stdout
, _("status\t\tShow status of daemon and queue."));
203 _cupsLangPrintf(stdout
, _("?Invalid help command unknown."));
208 * 'show_status()' - Show printers.
212 show_status(http_t
*http
, /* I - HTTP connection to server */
213 const char *dests
) /* I - Destinations */
215 ipp_t
*request
, /* IPP Request */
216 *response
; /* IPP Response */
217 ipp_attribute_t
*attr
; /* Current attribute */
218 char *printer
, /* Printer name */
219 *device
, /* Device URI */
220 *delimiter
; /* Char search result */
221 ipp_pstate_t pstate
; /* Printer state */
222 int accepting
; /* Is printer accepting jobs? */
223 int jobcount
; /* Count of current jobs */
224 const char *dptr
, /* Pointer into destination list */
225 *ptr
; /* Pointer into printer name */
226 int match
; /* Non-zero if this job matches */
227 static const char *requested
[] = /* Requested attributes */
230 "printer-is-accepting-jobs",
237 DEBUG_printf(("show_status(http=%p, dests=\"%s\")\n", http
, dests
));
243 * Build a CUPS_GET_PRINTERS request, which requires the following
247 * attributes-natural-language
250 request
= ippNewRequest(CUPS_GET_PRINTERS
);
252 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
253 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
257 * Do the request and get back a response...
260 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
262 DEBUG_puts("show_status: request succeeded...");
265 * Loop through the printers returned in the list and display
269 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
272 * Skip leading attributes until we hit a job...
275 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
282 * Pull the needed attributes from this job...
286 device
= "file:/dev/null";
287 pstate
= IPP_PRINTER_IDLE
;
291 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
293 if (!strcmp(attr
->name
, "device-uri") &&
294 attr
->value_tag
== IPP_TAG_URI
)
295 device
= attr
->values
[0].string
.text
;
296 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
297 attr
->value_tag
== IPP_TAG_BOOLEAN
)
298 accepting
= attr
->values
[0].boolean
;
299 else if (!strcmp(attr
->name
, "printer-name") &&
300 attr
->value_tag
== IPP_TAG_NAME
)
301 printer
= attr
->values
[0].string
.text
;
302 else if (!strcmp(attr
->name
, "printer-state") &&
303 attr
->value_tag
== IPP_TAG_ENUM
)
304 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
305 else if (!strcmp(attr
->name
, "queued-job-count") &&
306 attr
->value_tag
== IPP_TAG_INTEGER
)
307 jobcount
= attr
->values
[0].integer
;
313 * See if we have everything needed...
325 * A single 'all' printer name is special, meaning all printers.
328 if (dests
!= NULL
&& !strcmp(dests
, "all"))
332 * See if this is a printer we're interested in...
335 match
= dests
== NULL
;
339 for (dptr
= dests
; *dptr
!= '\0';)
342 * Skip leading whitespace and commas...
345 while (isspace(*dptr
& 255) || *dptr
== ',')
356 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
360 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' ||
361 isspace(*dptr
& 255)))
368 * Skip trailing junk...
371 while (!isspace(*dptr
& 255) && *dptr
!= '\0')
373 while (isspace(*dptr
& 255) || *dptr
== ',')
382 * Display the printer entry if needed...
391 printf("%s:\n", printer
);
392 if (!strncmp(device
, "file:", 5))
393 _cupsLangPrintf(stdout
,
394 _("\tprinter is on device \'%s\' speed -1"),
399 * Just show the scheme...
402 if ((delimiter
= strchr(device
, ':')) != NULL
)
405 _cupsLangPrintf(stdout
,
406 _("\tprinter is on device \'%s\' speed -1"),
412 _cupsLangPuts(stdout
, _("\tqueuing is enabled"));
414 _cupsLangPuts(stdout
, _("\tqueuing is disabled"));
416 if (pstate
!= IPP_PRINTER_STOPPED
)
417 _cupsLangPuts(stdout
, _("\tprinting is enabled"));
419 _cupsLangPuts(stdout
, _("\tprinting is disabled"));
422 _cupsLangPuts(stdout
, _("\tno entries"));
424 _cupsLangPrintf(stdout
, _("\t%d entries"), jobcount
);
426 _cupsLangPuts(stdout
, _("\tdaemon present"));