]>
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_prompt(const char *message
);
25 static void show_status(http_t
*, const char *);
29 * 'main()' - Parse options and commands.
33 main(int argc
, /* I - Number of command-line arguments */
34 char *argv
[]) /* I - Command-line arguments */
36 http_t
*http
; /* Connection to server */
37 char line
[1024], /* Input line from user */
38 *params
; /* Pointer to parameters */
44 * Connect to the scheduler...
47 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
52 * Process a single command on the command-line...
55 do_command(http
, argv
[1], argv
[2]);
60 * Do the command prompt thing...
63 show_prompt(_("lpc> "));
64 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
67 * Strip trailing whitespace...
70 for (params
= line
+ strlen(line
) - 1; params
>= line
;)
71 if (!isspace(*params
& 255))
77 * Strip leading whitespace...
80 for (params
= line
; isspace(*params
& 255); params
++);
83 _cups_strcpy(line
, params
);
88 * Nothing left, just show a prompt...
91 show_prompt(_("lpc> "));
96 * Find any options in the string...
99 for (params
= line
; *params
!= '\0'; params
++)
100 if (isspace(*params
& 255))
104 * Remove whitespace between the command and parameters...
107 while (isspace(*params
& 255))
111 * The "quit" and "exit" commands exit; otherwise, process as needed...
114 if (!compare_strings(line
, "quit", 1) ||
115 !compare_strings(line
, "exit", 2))
119 do_command(http
, line
, NULL
);
121 do_command(http
, line
, params
);
124 * Put another prompt out to the user...
127 show_prompt(_("lpc> "));
132 * Close the connection to the server and return...
142 * 'compare_strings()' - Compare two command-line strings.
145 static int /* O - -1 or 1 = no match, 0 = match */
146 compare_strings(const char *s
, /* I - Command-line string */
147 const char *t
, /* I - Option string */
148 size_t tmin
) /* I - Minimum number of unique chars in option */
150 size_t slen
; /* Length of command-line string */
157 return (strncmp(s
, t
, slen
));
162 * 'do_command()' - Do an lpc command...
166 do_command(http_t
*http
, /* I - HTTP connection to server */
167 const char *command
, /* I - Command string */
168 const char *params
) /* I - Parameters for command */
170 if (!compare_strings(command
, "status", 4))
171 show_status(http
, params
);
172 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
175 _cupsLangPrintf(stdout
,
176 _("%s is not implemented by the CUPS version of lpc."),
182 * 'show_help()' - Show help messages.
186 show_help(const char *command
) /* I - Command to describe or NULL */
190 _cupsLangPrintf(stdout
,
191 _("Commands may be abbreviated. Commands are:\n"
193 "exit help quit status ?"));
195 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
196 _cupsLangPrintf(stdout
, _("help\t\tGet help on commands."));
197 else if (!compare_strings(command
, "status", 4))
198 _cupsLangPrintf(stdout
, _("status\t\tShow status of daemon and queue."));
200 _cupsLangPrintf(stdout
, _("?Invalid help command unknown."));
205 * 'show_prompt()' - Show a localized prompt message.
209 show_prompt(const char *message
) /* I - Message string to use */
211 ssize_t bytes
; /* Number of bytes formatted */
212 char output
[8192]; /* Message buffer */
213 cups_lang_t
*lang
= cupsLangDefault();
214 /* Default language */
217 * Transcode to the destination charset and write the prompt...
220 if ((bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)_cupsLangString(lang
, message
), sizeof(output
), lang
->encoding
)) > 0)
222 fwrite(output
, 1, (size_t)bytes
, stdout
);
229 * 'show_status()' - Show printers.
233 show_status(http_t
*http
, /* I - HTTP connection to server */
234 const char *dests
) /* I - Destinations */
236 ipp_t
*request
, /* IPP Request */
237 *response
; /* IPP Response */
238 ipp_attribute_t
*attr
; /* Current attribute */
239 char *printer
, /* Printer name */
240 *device
, /* Device URI */
241 *delimiter
; /* Char search result */
242 ipp_pstate_t pstate
; /* Printer state */
243 int accepting
; /* Is printer accepting jobs? */
244 int jobcount
; /* Count of current jobs */
245 const char *dptr
, /* Pointer into destination list */
246 *ptr
; /* Pointer into printer name */
247 int match
; /* Non-zero if this job matches */
248 static const char *requested
[] = /* Requested attributes */
251 "printer-is-accepting-jobs",
262 * Build a CUPS_GET_PRINTERS request, which requires the following
266 * attributes-natural-language
269 request
= ippNewRequest(CUPS_GET_PRINTERS
);
271 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
272 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
276 * Do the request and get back a response...
279 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
282 * Loop through the printers returned in the list and display
286 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
289 * Skip leading attributes until we hit a job...
292 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
299 * Pull the needed attributes from this job...
303 device
= "file:/dev/null";
304 pstate
= IPP_PRINTER_IDLE
;
308 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
310 if (!strcmp(attr
->name
, "device-uri") &&
311 attr
->value_tag
== IPP_TAG_URI
)
312 device
= attr
->values
[0].string
.text
;
313 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
314 attr
->value_tag
== IPP_TAG_BOOLEAN
)
315 accepting
= attr
->values
[0].boolean
;
316 else if (!strcmp(attr
->name
, "printer-name") &&
317 attr
->value_tag
== IPP_TAG_NAME
)
318 printer
= attr
->values
[0].string
.text
;
319 else if (!strcmp(attr
->name
, "printer-state") &&
320 attr
->value_tag
== IPP_TAG_ENUM
)
321 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
322 else if (!strcmp(attr
->name
, "queued-job-count") &&
323 attr
->value_tag
== IPP_TAG_INTEGER
)
324 jobcount
= attr
->values
[0].integer
;
330 * See if we have everything needed...
342 * A single 'all' printer name is special, meaning all printers.
345 if (dests
!= NULL
&& !strcmp(dests
, "all"))
349 * See if this is a printer we're interested in...
352 match
= dests
== NULL
;
356 for (dptr
= dests
; *dptr
!= '\0';)
359 * Skip leading whitespace and commas...
362 while (isspace(*dptr
& 255) || *dptr
== ',')
373 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
377 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' ||
378 isspace(*dptr
& 255)))
385 * Skip trailing junk...
388 while (!isspace(*dptr
& 255) && *dptr
!= '\0')
390 while (isspace(*dptr
& 255) || *dptr
== ',')
399 * Display the printer entry if needed...
408 printf("%s:\n", printer
);
409 if (!strncmp(device
, "file:", 5))
410 _cupsLangPrintf(stdout
,
411 _("\tprinter is on device \'%s\' speed -1"),
416 * Just show the scheme...
419 if ((delimiter
= strchr(device
, ':')) != NULL
)
422 _cupsLangPrintf(stdout
,
423 _("\tprinter is on device \'%s\' speed -1"),
429 _cupsLangPuts(stdout
, _("\tqueuing is enabled"));
431 _cupsLangPuts(stdout
, _("\tqueuing is disabled"));
433 if (pstate
!= IPP_PRINTER_STOPPED
)
434 _cupsLangPuts(stdout
, _("\tprinting is enabled"));
436 _cupsLangPuts(stdout
, _("\tprinting is disabled"));
439 _cupsLangPuts(stdout
, _("\tno entries"));
441 _cupsLangPrintf(stdout
, _("\t%d entries"), jobcount
);
443 _cupsLangPuts(stdout
, _("\tdaemon present"));