]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
2 * "$Id: lpc.c 7079 2007-11-30 01:58:31Z mike $"
4 * "lpc" command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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...
30 #include <cups/cups.h>
31 #include <cups/i18n.h>
32 #include <cups/debug.h>
33 #include <cups/string.h>
40 static int compare_strings(const char *, const char *, int);
41 static void do_command(http_t
*, const char *, const char *);
42 static void show_help(const char *);
43 static void show_status(http_t
*, const char *);
47 * 'main()' - Parse options and commands.
51 main(int argc
, /* I - Number of command-line arguments */
52 char *argv
[]) /* I - Command-line arguments */
54 http_t
*http
; /* Connection to server */
55 char line
[1024], /* Input line from user */
56 *params
; /* Pointer to parameters */
62 * Connect to the scheduler...
65 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
70 * Process a single command on the command-line...
73 do_command(http
, argv
[1], argv
[2]);
78 * Do the command prompt thing...
81 _cupsLangPuts(stdout
, _("lpc> "));
82 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
85 * Strip trailing whitespace...
88 for (params
= line
+ strlen(line
) - 1; params
>= line
;)
89 if (!isspace(*params
& 255))
95 * Strip leading whitespace...
98 for (params
= line
; isspace(*params
& 255); params
++);
101 _cups_strcpy(line
, params
);
106 * Nothing left, just show a prompt...
109 _cupsLangPuts(stdout
, _("lpc> "));
114 * Find any options in the string...
117 for (params
= line
; *params
!= '\0'; params
++)
118 if (isspace(*params
& 255))
122 * Remove whitespace between the command and parameters...
125 while (isspace(*params
& 255))
129 * The "quit" and "exit" commands exit; otherwise, process as needed...
132 if (!compare_strings(line
, "quit", 1) ||
133 !compare_strings(line
, "exit", 2))
137 do_command(http
, line
, NULL
);
139 do_command(http
, line
, params
);
142 * Put another prompt out to the user...
145 _cupsLangPuts(stdout
, _("lpc> "));
150 * Close the connection to the server and return...
160 * 'compare_strings()' - Compare two command-line strings.
163 static int /* O - -1 or 1 = no match, 0 = match */
164 compare_strings(const char *s
, /* I - Command-line string */
165 const char *t
, /* I - Option string */
166 int tmin
) /* I - Minimum number of unique chars in option */
168 int slen
; /* Length of command-line string */
175 return (strncmp(s
, t
, slen
));
180 * 'do_command()' - Do an lpc command...
184 do_command(http_t
*http
, /* I - HTTP connection to server */
185 const char *command
, /* I - Command string */
186 const char *params
) /* I - Parameters for command */
188 if (!compare_strings(command
, "status", 4))
189 show_status(http
, params
);
190 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
193 _cupsLangPrintf(stdout
,
194 _("%s is not implemented by the CUPS version of lpc.\n"),
200 * 'show_help()' - Show help messages.
204 show_help(const char *command
) /* I - Command to describe or NULL */
208 _cupsLangPrintf(stdout
,
209 _("Commands may be abbreviated. Commands are:\n"
211 "exit help quit status ?\n"));
213 else if (!compare_strings(command
, "help", 1) || !strcmp(command
, "?"))
214 _cupsLangPrintf(stdout
, _("help\t\tget help on commands\n"));
215 else if (!compare_strings(command
, "status", 4))
216 _cupsLangPrintf(stdout
, _("status\t\tshow status of daemon and queue\n"));
218 _cupsLangPrintf(stdout
, _("?Invalid help command unknown\n"));
223 * 'show_status()' - Show printers.
227 show_status(http_t
*http
, /* I - HTTP connection to server */
228 const char *dests
) /* I - Destinations */
230 ipp_t
*request
, /* IPP Request */
231 *response
; /* IPP Response */
232 ipp_attribute_t
*attr
; /* Current attribute */
233 char *printer
, /* Printer name */
234 *device
, /* Device URI */
235 *delimiter
; /* Char search result */
236 ipp_pstate_t pstate
; /* Printer state */
237 int accepting
; /* Is printer accepting jobs? */
238 int jobcount
; /* Count of current jobs */
239 const char *dptr
, /* Pointer into destination list */
240 *ptr
; /* Pointer into printer name */
241 int match
; /* Non-zero if this job matches */
242 static const char *requested
[] = /* Requested attributes */
245 "printer-is-accepting-jobs",
252 DEBUG_printf(("show_status(http=%p, dests=\"%s\")\n", http
, dests
));
258 * Build a CUPS_GET_PRINTERS request, which requires the following
262 * attributes-natural-language
265 request
= ippNewRequest(CUPS_GET_PRINTERS
);
267 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
268 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
272 * Do the request and get back a response...
275 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
277 DEBUG_puts("show_status: request succeeded...");
280 * Loop through the printers returned in the list and display
284 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
287 * Skip leading attributes until we hit a job...
290 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
297 * Pull the needed attributes from this job...
301 device
= "file:/dev/null";
302 pstate
= IPP_PRINTER_IDLE
;
306 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
308 if (!strcmp(attr
->name
, "device-uri") &&
309 attr
->value_tag
== IPP_TAG_URI
)
310 device
= attr
->values
[0].string
.text
;
311 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
312 attr
->value_tag
== IPP_TAG_BOOLEAN
)
313 accepting
= attr
->values
[0].boolean
;
314 else if (!strcmp(attr
->name
, "printer-name") &&
315 attr
->value_tag
== IPP_TAG_NAME
)
316 printer
= attr
->values
[0].string
.text
;
317 else if (!strcmp(attr
->name
, "printer-state") &&
318 attr
->value_tag
== IPP_TAG_ENUM
)
319 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
320 else if (!strcmp(attr
->name
, "queued-job-count") &&
321 attr
->value_tag
== IPP_TAG_INTEGER
)
322 jobcount
= attr
->values
[0].integer
;
328 * See if we have everything needed...
340 * A single 'all' printer name is special, meaning all printers.
343 if (dests
!= NULL
&& !strcmp(dests
, "all"))
347 * See if this is a printer we're interested in...
350 match
= dests
== NULL
;
354 for (dptr
= dests
; *dptr
!= '\0';)
357 * Skip leading whitespace and commas...
360 while (isspace(*dptr
& 255) || *dptr
== ',')
371 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
374 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' ||
375 isspace(*dptr
& 255)))
382 * Skip trailing junk...
385 while (!isspace(*dptr
& 255) && *dptr
!= '\0')
387 while (isspace(*dptr
& 255) || *dptr
== ',')
396 * Display the printer entry if needed...
405 printf("%s:\n", printer
);
406 if (!strncmp(device
, "file:", 5))
407 _cupsLangPrintf(stdout
,
408 _("\tprinter is on device \'%s\' speed -1\n"),
413 * Just show the scheme...
416 if ((delimiter
= strchr(device
, ':')) != NULL
)
419 _cupsLangPrintf(stdout
,
420 _("\tprinter is on device \'%s\' speed -1\n"),
426 _cupsLangPuts(stdout
, _("\tqueuing is enabled\n"));
428 _cupsLangPuts(stdout
, _("\tqueuing is disabled\n"));
430 if (pstate
!= IPP_PRINTER_STOPPED
)
431 _cupsLangPuts(stdout
, _("\tprinting is enabled\n"));
433 _cupsLangPuts(stdout
, _("\tprinting is disabled\n"));
436 _cupsLangPuts(stdout
, _("\tno entries\n"));
438 _cupsLangPrintf(stdout
, _("\t%d entries\n"), jobcount
);
440 _cupsLangPuts(stdout
, _("\tdaemon present\n"));
453 * End of "$Id: lpc.c 7079 2007-11-30 01:58:31Z mike $".