]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
2 * "lpc" command for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
21 static int compare_strings(const char *, const char *, size_t);
22 static void do_command(http_t
*, const char *, const char *);
23 static void show_help(const char *);
24 static void show_status(http_t
*, const char *);
28 * 'main()' - Parse options and commands.
32 main(int argc
, /* I - Number of command-line arguments */
33 char *argv
[]) /* I - Command-line arguments */
35 http_t
*http
; /* Connection to server */
36 char line
[1024], /* Input line from user */
37 *params
; /* Pointer to parameters */
43 * Connect to the scheduler...
46 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
51 * Process a single command on the command-line...
54 do_command(http
, argv
[1], argv
[2]);
59 * Do the command prompt thing...
62 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no-newline version */
63 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
66 * Strip trailing whitespace...
69 for (params
= line
+ strlen(line
) - 1; params
>= line
;)
70 if (!isspace(*params
& 255))
76 * Strip leading whitespace...
79 for (params
= line
; isspace(*params
& 255); params
++);
82 _cups_strcpy(line
, params
);
87 * Nothing left, just show a prompt...
90 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no newline version */
95 * Find any options in the string...
98 for (params
= line
; *params
!= '\0'; params
++)
99 if (isspace(*params
& 255))
103 * Remove whitespace between the command and parameters...
106 while (isspace(*params
& 255))
110 * The "quit" and "exit" commands exit; otherwise, process as needed...
113 if (!compare_strings(line
, "quit", 1) ||
114 !compare_strings(line
, "exit", 2))
118 do_command(http
, line
, NULL
);
120 do_command(http
, line
, params
);
123 * Put another prompt out to the user...
126 _cupsLangPuts(stdout
, _("lpc> ")); /* TODO: Need no newline version */
131 * Close the connection to the server and return...
141 * 'compare_strings()' - Compare two command-line strings.
144 static int /* O - -1 or 1 = no match, 0 = match */
145 compare_strings(const char *s
, /* I - Command-line string */
146 const char *t
, /* I - Option string */
147 size_t tmin
) /* I - Minimum number of unique chars in option */
149 size_t slen
; /* Length of command-line string */
156 return (strncmp(s
, t
, slen
));
161 * 'do_command()' - Do an lpc command...
165 do_command(http_t
*http
, /* I - HTTP connection to server */
166 const char *command
, /* I - Command string */
167 const char *params
) /* I - Parameters for command */
169 if (!compare_strings(command
, "status", 4))
170 show_status(http
, params
);
171 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
174 _cupsLangPrintf(stdout
,
175 _("%s is not implemented by the CUPS version of lpc."),
181 * 'show_help()' - Show help messages.
185 show_help(const char *command
) /* I - Command to describe or NULL */
189 _cupsLangPrintf(stdout
,
190 _("Commands may be abbreviated. Commands are:\n"
192 "exit help quit status ?"));
194 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
195 _cupsLangPrintf(stdout
, _("help\t\tGet help on commands."));
196 else if (!compare_strings(command
, "status", 4))
197 _cupsLangPrintf(stdout
, _("status\t\tShow status of daemon and queue."));
199 _cupsLangPrintf(stdout
, _("?Invalid help command unknown."));
204 * 'show_status()' - Show printers.
208 show_status(http_t
*http
, /* I - HTTP connection to server */
209 const char *dests
) /* I - Destinations */
211 ipp_t
*request
, /* IPP Request */
212 *response
; /* IPP Response */
213 ipp_attribute_t
*attr
; /* Current attribute */
214 char *printer
, /* Printer name */
215 *device
, /* Device URI */
216 *delimiter
; /* Char search result */
217 ipp_pstate_t pstate
; /* Printer state */
218 int accepting
; /* Is printer accepting jobs? */
219 int jobcount
; /* Count of current jobs */
220 const char *dptr
, /* Pointer into destination list */
221 *ptr
; /* Pointer into printer name */
222 int match
; /* Non-zero if this job matches */
223 static const char *requested
[] = /* Requested attributes */
226 "printer-is-accepting-jobs",
237 * Build a CUPS_GET_PRINTERS request, which requires the following
241 * attributes-natural-language
244 request
= ippNewRequest(CUPS_GET_PRINTERS
);
246 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
247 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
251 * Do the request and get back a response...
254 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
257 * Loop through the printers returned in the list and display
261 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
264 * Skip leading attributes until we hit a job...
267 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
274 * Pull the needed attributes from this job...
278 device
= "file:/dev/null";
279 pstate
= IPP_PRINTER_IDLE
;
283 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
285 if (!strcmp(attr
->name
, "device-uri") &&
286 attr
->value_tag
== IPP_TAG_URI
)
287 device
= attr
->values
[0].string
.text
;
288 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
289 attr
->value_tag
== IPP_TAG_BOOLEAN
)
290 accepting
= attr
->values
[0].boolean
;
291 else if (!strcmp(attr
->name
, "printer-name") &&
292 attr
->value_tag
== IPP_TAG_NAME
)
293 printer
= attr
->values
[0].string
.text
;
294 else if (!strcmp(attr
->name
, "printer-state") &&
295 attr
->value_tag
== IPP_TAG_ENUM
)
296 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
297 else if (!strcmp(attr
->name
, "queued-job-count") &&
298 attr
->value_tag
== IPP_TAG_INTEGER
)
299 jobcount
= attr
->values
[0].integer
;
305 * See if we have everything needed...
317 * A single 'all' printer name is special, meaning all printers.
320 if (dests
!= NULL
&& !strcmp(dests
, "all"))
324 * See if this is a printer we're interested in...
327 match
= dests
== NULL
;
331 for (dptr
= dests
; *dptr
!= '\0';)
334 * Skip leading whitespace and commas...
337 while (isspace(*dptr
& 255) || *dptr
== ',')
348 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
352 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' ||
353 isspace(*dptr
& 255)))
360 * Skip trailing junk...
363 while (!isspace(*dptr
& 255) && *dptr
!= '\0')
365 while (isspace(*dptr
& 255) || *dptr
== ',')
374 * Display the printer entry if needed...
383 printf("%s:\n", printer
);
384 if (!strncmp(device
, "file:", 5))
385 _cupsLangPrintf(stdout
,
386 _("\tprinter is on device \'%s\' speed -1"),
391 * Just show the scheme...
394 if ((delimiter
= strchr(device
, ':')) != NULL
)
397 _cupsLangPrintf(stdout
,
398 _("\tprinter is on device \'%s\' speed -1"),
404 _cupsLangPuts(stdout
, _("\tqueuing is enabled"));
406 _cupsLangPuts(stdout
, _("\tqueuing is disabled"));
408 if (pstate
!= IPP_PRINTER_STOPPED
)
409 _cupsLangPuts(stdout
, _("\tprinting is enabled"));
411 _cupsLangPuts(stdout
, _("\tprinting is disabled"));
414 _cupsLangPuts(stdout
, _("\tno entries"));
416 _cupsLangPrintf(stdout
, _("\t%d entries"), jobcount
);
418 _cupsLangPuts(stdout
, _("\tdaemon present"));