]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpc.c
2dd21ba26c39e482fdad9f536ff3f9eea1450fb2
2 * "$Id: lpc.c,v 1.14 2002/06/05 20:13:12 mike Exp $"
4 * "lpc" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2002 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Parse options and commands.
27 * compare_strings() - Compare two command-line strings.
28 * do_command() - Do an lpc command...
29 * show_help() - Show help messages.
30 * show_status() - Show printers.
34 * Include necessary headers...
40 #include <cups/cups.h>
41 #include <cups/language.h>
42 #include <cups/debug.h>
43 #include <cups/string.h>
50 static int compare_strings(char *, char *, int);
51 static void do_command(http_t
*, char *, char *);
52 static void show_help(char *);
53 static void show_status(http_t
*, char *);
57 * 'main()' - Parse options and commands.
61 main(int argc
, /* I - Number of command-line arguments */
62 char *argv
[]) /* I - Command-line arguments */
64 http_t
*http
; /* Connection to server */
65 char line
[1024], /* Input line from user */
66 *params
; /* Pointer to parameters */
70 * Connect to the scheduler...
73 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
78 * Process a single command on the command-line...
81 do_command(http
, argv
[1], argv
[2]);
86 * Do the command prompt thing...
90 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
93 * Strip the trailing newline...
96 line
[strlen(line
) - 1] = '\0';
99 * Find any options in the string...
102 while (isspace(line
[0]))
103 strcpy(line
, line
+ 1);
105 for (params
= line
; *params
!= '\0'; params
++)
106 if (isspace(*params
))
110 * Remove whitespace between the command and parameters...
113 while (isspace(*params
))
117 * The "quit" and "exit" commands exit; otherwise, process as needed...
120 if (compare_strings(line
, "quit", 1) == 0 ||
121 compare_strings(line
, "exit", 2) == 0)
125 do_command(http
, line
, NULL
);
127 do_command(http
, line
, params
);
130 * Put another prompt out to the user...
138 * Close the connection to the server and return...
148 * 'compare_strings()' - Compare two command-line strings.
151 static int /* O - -1 or 1 = no match, 0 = match */
152 compare_strings(char *s
, /* I - Command-line string */
153 char *t
, /* I - Option string */
154 int tmin
) /* I - Minimum number of unique chars in option */
156 int slen
; /* Length of command-line string */
163 return (strncmp(s
, t
, slen
));
168 * 'do_command()' - Do an lpc command...
172 do_command(http_t
*http
, /* I - HTTP connection to server */
173 char *command
, /* I - Command string */
174 char *params
) /* I - Parameters for command */
176 if (compare_strings(command
, "status", 4) == 0)
177 show_status(http
, params
);
178 else if (compare_strings(command
, "help", 1) == 0 ||
179 strcmp(command
, "?") == 0)
182 printf("%s is not implemented by the CUPS version of lpc.\n", command
);
187 * 'show_help()' - Show help messages.
191 show_help(char *command
) /* I - Command to describe or NULL */
195 puts("Commands may be abbreviated. Commands are:");
197 puts("exit help quit status ?");
199 else if (compare_strings(command
, "help", 1) == 0 ||
200 strcmp(command
, "?") == 0)
201 puts("help\t\tget help on commands");
202 else if (compare_strings(command
, "status", 4) == 0)
203 puts("status\t\tshow status of daemon and queue");
205 puts("?Invalid help command unknown");
210 * 'show_status()' - Show printers.
214 show_status(http_t
*http
, /* I - HTTP connection to server */
215 char *dests
) /* I - Destinations */
217 ipp_t
*request
, /* IPP Request */
218 *response
, /* IPP Response */
219 *jobs
; /* IPP Get Jobs response */
220 ipp_attribute_t
*attr
, /* Current attribute */
221 *jattr
; /* Current job attribute */
222 cups_lang_t
*language
; /* Default language */
223 char *printer
, /* Printer name */
224 *device
; /* Device URI */
225 ipp_pstate_t pstate
; /* Printer state */
226 int accepting
; /* Is printer accepting jobs? */
227 int jobcount
; /* Count of current jobs */
228 char *dptr
, /* Pointer into destination list */
229 *ptr
; /* Pointer into printer name */
230 int match
; /* Non-zero if this job matches */
231 char printer_uri
[HTTP_MAX_URI
];
233 static const char *requested
[] =
234 { /* Requested attributes */
238 "printer-is-accepting-jobs"
242 DEBUG_printf(("show_status(%08x, %08x)\n", http
, dests
));
248 * Build a CUPS_GET_PRINTERS request, which requires the following
252 * attributes-natural-language
257 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
258 request
->request
.op
.request_id
= 1;
260 language
= cupsLangDefault();
262 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
263 "attributes-charset", NULL
, cupsLangEncoding(language
));
265 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
266 "attributes-natural-language", NULL
, language
->language
);
268 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
269 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
273 * Do the request and get back a response...
276 if ((response
= cupsDoRequest(http
, request
, "/printers/")) != NULL
)
278 DEBUG_puts("show_status: request succeeded...");
281 * Loop through the printers returned in the list and display
285 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
288 * Skip leading attributes until we hit a job...
291 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
298 * Pull the needed attributes from this job...
302 device
= "file:/dev/null";
303 pstate
= IPP_PRINTER_IDLE
;
307 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
309 if (strcmp(attr
->name
, "printer-name") == 0 &&
310 attr
->value_tag
== IPP_TAG_NAME
)
311 printer
= attr
->values
[0].string
.text
;
313 if (strcmp(attr
->name
, "device-uri") == 0 &&
314 attr
->value_tag
== IPP_TAG_URI
)
315 device
= attr
->values
[0].string
.text
;
317 if (strcmp(attr
->name
, "printer-state") == 0 &&
318 attr
->value_tag
== IPP_TAG_ENUM
)
319 pstate
= (ipp_pstate_t
)attr
->values
[0].integer
;
321 if (strcmp(attr
->name
, "printer-is-accepting-jobs") == 0 &&
322 attr
->value_tag
== IPP_TAG_BOOLEAN
)
323 accepting
= attr
->values
[0].boolean
;
329 * See if we have everything needed...
341 * See if this is a printer we're interested in...
344 match
= dests
== NULL
;
348 for (dptr
= dests
; *dptr
!= '\0';)
351 * Skip leading whitespace and commas...
354 while (isspace(*dptr
) || *dptr
== ',')
365 *ptr
!= '\0' && *dptr
!= '\0' && *ptr
== *dptr
;
368 if (*ptr
== '\0' && (*dptr
== '\0' || *dptr
== ',' || isspace(*dptr
)))
375 * Skip trailing junk...
378 while (!isspace(*dptr
) && *dptr
!= '\0')
380 while (isspace(*dptr
) || *dptr
== ',')
389 * Display the printer entry if needed...
395 * If the printer state is "IPP_PRINTER_PROCESSING", then grab the
396 * current job for the printer.
399 if (pstate
== IPP_PRINTER_PROCESSING
)
402 * Build an IPP_GET_JOBS request, which requires the following
406 * attributes-natural-language
413 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
414 request
->request
.op
.request_id
= 1;
416 language
= cupsLangDefault();
418 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
419 "attributes-charset", NULL
,
420 cupsLangEncoding(language
));
422 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
423 "attributes-natural-language", NULL
,
426 snprintf(printer_uri
, sizeof(printer_uri
),
427 "ipp://localhost/printers/%s", printer
);
428 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
429 "printer-uri", NULL
, printer_uri
);
431 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
432 "requested-attributes", NULL
, "job-id");
434 if ((jobs
= cupsDoRequest(http
, request
, "/jobs/")) != NULL
)
436 for (jattr
= jobs
->attrs
; jattr
!= NULL
; jattr
= jattr
->next
)
437 if (jattr
->name
&& strcmp(jattr
->name
, "job-id") == 0)
448 printf("%s:\n", printer
);
449 if (strncmp(device
, "file:", 5) == 0)
450 printf("\tprinter is on device \'%s\' speed -1\n", device
+ 5);
454 * Just show the method...
457 *strchr(device
, ':') = '\0';
458 printf("\tprinter is on device \'%s\' speed -1\n", device
);
461 printf("\tqueuing is %sabled\n", accepting
? "en" : "dis");
462 printf("\tprinting is %sabled\n",
463 pstate
== IPP_PRINTER_STOPPED
? "dis" : "en");
465 puts("\tno entries");
467 printf("\t%d entries\n", jobcount
);
468 puts("\tdaemon present");
481 * End of "$Id: lpc.c,v 1.14 2002/06/05 20:13:12 mike Exp $".