]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
e81f166a83ccbf580cd8302eb5e12a721726d257
2 * "lpc" command for CUPS.
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright 2007-2014 by Apple Inc.
6 * Copyright 1997-2006 by Easy Software Products.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
12 * Include necessary headers...
15 #include <cups/cups-private.h>
22 static int compare_strings(const char *, const char *, size_t);
23 static void do_command(http_t
*, const char *, const char *);
24 static void show_help(const char *);
25 static void show_prompt(const char *message
);
26 static void show_status(http_t
*, const char *);
30 * 'main()' - Parse options and commands.
34 main(int argc
, /* I - Number of command-line arguments */
35 char *argv
[]) /* I - Command-line arguments */
37 http_t
*http
; /* Connection to server */
38 char line
[1024], /* Input line from user */
39 *params
; /* Pointer to parameters */
45 * Connect to the scheduler...
48 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
53 * Process a single command on the command-line...
56 do_command(http
, argv
[1], argv
[2]);
61 * Do the command prompt thing...
64 show_prompt(_("lpc> "));
65 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
68 * Strip trailing whitespace...
71 for (params
= line
+ strlen(line
) - 1; params
>= line
;)
72 if (!isspace(*params
& 255))
78 * Strip leading whitespace...
81 for (params
= line
; isspace(*params
& 255); params
++);
84 _cups_strcpy(line
, params
);
89 * Nothing left, just show a prompt...
92 show_prompt(_("lpc> "));
97 * Find any options in the string...
100 for (params
= line
; *params
!= '\0'; params
++)
101 if (isspace(*params
& 255))
105 * Remove whitespace between the command and parameters...
108 while (isspace(*params
& 255))
112 * The "quit" and "exit" commands exit; otherwise, process as needed...
115 if (!compare_strings(line
, "quit", 1) ||
116 !compare_strings(line
, "exit", 2))
120 do_command(http
, line
, NULL
);
122 do_command(http
, line
, params
);
125 * Put another prompt out to the user...
128 show_prompt(_("lpc> "));
133 * Close the connection to the server and return...
143 * 'compare_strings()' - Compare two command-line strings.
146 static int /* O - -1 or 1 = no match, 0 = match */
147 compare_strings(const char *s
, /* I - Command-line string */
148 const char *t
, /* I - Option string */
149 size_t tmin
) /* I - Minimum number of unique chars in option */
151 size_t slen
; /* Length of command-line string */
158 return (strncmp(s
, t
, slen
));
163 * 'do_command()' - Do an lpc command...
167 do_command(http_t
*http
, /* I - HTTP connection to server */
168 const char *command
, /* I - Command string */
169 const char *params
) /* I - Parameters for command */
171 if (!compare_strings(command
, "status", 4))
172 show_status(http
, params
);
173 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
176 _cupsLangPrintf(stdout
,
177 _("%s is not implemented by the CUPS version of lpc."),
183 * 'show_help()' - Show help messages.
187 show_help(const char *command
) /* I - Command to describe or NULL */
191 _cupsLangPrintf(stdout
,
192 _("Commands may be abbreviated. Commands are:\n"
194 "exit help quit status ?"));
196 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
197 _cupsLangPrintf(stdout
, _("help\t\tGet help on commands."));
198 else if (!compare_strings(command
, "status", 4))
199 _cupsLangPrintf(stdout
, _("status\t\tShow status of daemon and queue."));
201 _cupsLangPrintf(stdout
, _("?Invalid help command unknown."));
206 * 'show_prompt()' - Show a localized prompt message.
210 show_prompt(const char *message
) /* I - Message string to use */
212 ssize_t bytes
; /* Number of bytes formatted */
213 char output
[8192]; /* Message buffer */
214 cups_lang_t
*lang
= cupsLangDefault();
215 /* Default language */
218 * Transcode to the destination charset and write the prompt...
221 if ((bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)_cupsLangString(lang
, message
), sizeof(output
), lang
->encoding
)) > 0)
223 fwrite(output
, 1, (size_t)bytes
, stdout
);
230 * 'show_status()' - Show printers.
234 show_status(http_t
*http
, /* I - HTTP connection to server */
235 const char *dests
) /* I - Destinations */
237 ipp_t
*request
, /* IPP Request */
238 *response
; /* IPP Response */
239 ipp_attribute_t
*attr
; /* Current attribute */
240 char *printer
, /* Printer name */
241 *device
, /* Device URI */
242 *delimiter
; /* Char search result */
243 ipp_pstate_t pstate
; /* Printer state */
244 int accepting
; /* Is printer accepting jobs? */
245 int jobcount
; /* Count of current jobs */
246 const char *dptr
, /* Pointer into destination list */
247 *ptr
; /* Pointer into printer name */
248 int match
; /* Non-zero if this job matches */
249 static const char *requested
[] = /* Requested attributes */
252 "printer-is-accepting-jobs",
263 * Build a CUPS_GET_PRINTERS request, which requires the following
267 * attributes-natural-language
270 request
= ippNewRequest(IPP_OP_CUPS_GET_PRINTERS
);
272 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
273 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
277 * Do the request and get back a response...
280 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
283 * Loop through the printers returned in the list and display
287 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
290 * Skip leading attributes until we hit a job...
293 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
300 * Pull the needed attributes from this job...
304 device
= "file:/dev/null";
305 pstate
= IPP_PSTATE_IDLE
;
309 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
311 if (!strcmp(attr
->name
, "device-uri") &&
312 attr
->value_tag
== IPP_TAG_URI
)
313 device
= attr
->values
[0].string
.text
;
314 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
315 attr
->value_tag
== IPP_TAG_BOOLEAN
)
316 accepting
= attr
->values
[0].boolean
;
317 else if (!strcmp(attr
->name
, "printer-name") &&
318 attr
->value_tag
== IPP_TAG_NAME
)
319 printer
= attr
->values
[0].string
.text
;
320 else if (!strcmp(attr
->name
, "printer-state") &&
321 attr
->value_tag
== IPP_TAG_ENUM
)
322 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
323 else if (!strcmp(attr
->name
, "queued-job-count") &&
324 attr
->value_tag
== IPP_TAG_INTEGER
)
325 jobcount
= attr
->values
[0].integer
;
331 * See if we have everything needed...
343 * A single 'all' printer name is special, meaning all printers.
346 if (dests
!= NULL
&& !strcmp(dests
, "all"))
350 * See if this is a printer we're interested in...
353 match
= dests
== NULL
;
357 for (dptr
= dests
; *dptr
!= '\0';)
360 * Skip leading whitespace and commas...
363 while (isspace(*dptr
& 255) || *dptr
== ',')
374 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
378 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' ||
379 isspace(*dptr
& 255)))
386 * Skip trailing junk...
389 while (!isspace(*dptr
& 255) && *dptr
!= '\0')
391 while (isspace(*dptr
& 255) || *dptr
== ',')
400 * Display the printer entry if needed...
409 printf("%s:\n", printer
);
410 if (!strncmp(device
, "file:", 5))
411 _cupsLangPrintf(stdout
,
412 _("\tprinter is on device \'%s\' speed -1"),
417 * Just show the scheme...
420 if ((delimiter
= strchr(device
, ':')) != NULL
)
423 _cupsLangPrintf(stdout
,
424 _("\tprinter is on device \'%s\' speed -1"),
430 _cupsLangPuts(stdout
, _("\tqueuing is enabled"));
432 _cupsLangPuts(stdout
, _("\tqueuing is disabled"));
434 if (pstate
!= IPP_PSTATE_STOPPED
)
435 _cupsLangPuts(stdout
, _("\tprinting is enabled"));
437 _cupsLangPuts(stdout
, _("\tprinting is disabled"));
440 _cupsLangPuts(stdout
, _("\tno entries"));
442 _cupsLangPrintf(stdout
, _("\t%d entries"), jobcount
);
444 _cupsLangPuts(stdout
, _("\tdaemon present"));