]>
Commit | Line | Data |
---|---|---|
bd0b97ff | 1 | /* |
efb2f309 | 2 | * "$Id: lpc.c,v 1.13 2002/01/02 17:58:35 mike Exp $" |
bd0b97ff | 3 | * |
4 | * "lpc" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
efb2f309 | 6 | * Copyright 1997-2002 by Easy Software Products. |
bd0b97ff | 7 | * |
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 | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
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. | |
31 | */ | |
32 | ||
33 | /* | |
34 | * Include necessary headers... | |
35 | */ | |
36 | ||
37 | #include <stdio.h> | |
38 | #include <stdlib.h> | |
39 | #include <ctype.h> | |
40 | #include <cups/cups.h> | |
41 | #include <cups/language.h> | |
42 | #include <cups/debug.h> | |
43 | ||
44 | ||
45 | /* | |
46 | * Local functions... | |
47 | */ | |
48 | ||
49 | static int compare_strings(char *, char *, int); | |
50 | static void do_command(http_t *, char *, char *); | |
51 | static void show_help(char *); | |
52 | static void show_status(http_t *, char *); | |
53 | ||
54 | ||
55 | /* | |
56 | * 'main()' - Parse options and commands. | |
57 | */ | |
58 | ||
59 | int | |
60 | main(int argc, /* I - Number of command-line arguments */ | |
61 | char *argv[]) /* I - Command-line arguments */ | |
62 | { | |
bd0b97ff | 63 | http_t *http; /* Connection to server */ |
64 | char line[1024], /* Input line from user */ | |
65 | *params; /* Pointer to parameters */ | |
66 | ||
67 | ||
68 | /* | |
69 | * Connect to the scheduler... | |
70 | */ | |
71 | ||
a1793153 | 72 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); |
bd0b97ff | 73 | |
74 | if (argc > 1) | |
75 | { | |
76 | /* | |
77 | * Process a single command on the command-line... | |
78 | */ | |
79 | ||
80 | do_command(http, argv[1], argv[2]); | |
81 | } | |
82 | else | |
83 | { | |
84 | /* | |
85 | * Do the command prompt thing... | |
86 | */ | |
87 | ||
88 | printf("lpc> "); | |
9c255021 | 89 | while (fgets(line, sizeof(line), stdin) != NULL) |
bd0b97ff | 90 | { |
9c255021 | 91 | /* |
92 | * Strip the trailing newline... | |
93 | */ | |
94 | ||
95 | line[strlen(line) - 1] = '\0'; | |
96 | ||
bd0b97ff | 97 | /* |
98 | * Find any options in the string... | |
99 | */ | |
100 | ||
101 | while (isspace(line[0])) | |
102 | strcpy(line, line + 1); | |
103 | ||
104 | for (params = line; *params != '\0'; params ++) | |
105 | if (isspace(*params)) | |
106 | break; | |
107 | ||
108 | /* | |
109 | * Remove whitespace between the command and parameters... | |
110 | */ | |
111 | ||
112 | while (isspace(*params)) | |
113 | *params++ = '\0'; | |
114 | ||
115 | /* | |
116 | * The "quit" and "exit" commands exit; otherwise, process as needed... | |
117 | */ | |
118 | ||
119 | if (compare_strings(line, "quit", 1) == 0 || | |
120 | compare_strings(line, "exit", 2) == 0) | |
121 | break; | |
122 | ||
123 | if (*params == '\0') | |
124 | do_command(http, line, NULL); | |
125 | else | |
126 | do_command(http, line, params); | |
127 | ||
128 | /* | |
129 | * Put another prompt out to the user... | |
130 | */ | |
131 | ||
132 | printf("lpc> "); | |
133 | } | |
134 | } | |
135 | ||
136 | /* | |
137 | * Close the connection to the server and return... | |
138 | */ | |
139 | ||
140 | httpClose(http); | |
141 | ||
142 | return (0); | |
143 | } | |
144 | ||
145 | ||
146 | /* | |
147 | * 'compare_strings()' - Compare two command-line strings. | |
148 | */ | |
149 | ||
150 | static int /* O - -1 or 1 = no match, 0 = match */ | |
151 | compare_strings(char *s, /* I - Command-line string */ | |
152 | char *t, /* I - Option string */ | |
153 | int tmin) /* I - Minimum number of unique chars in option */ | |
154 | { | |
155 | int slen; /* Length of command-line string */ | |
156 | ||
157 | ||
158 | slen = strlen(s); | |
159 | if (slen < tmin) | |
160 | return (-1); | |
161 | else | |
162 | return (strncmp(s, t, slen)); | |
163 | } | |
164 | ||
165 | ||
166 | /* | |
167 | * 'do_command()' - Do an lpc command... | |
168 | */ | |
169 | ||
170 | static void | |
171 | do_command(http_t *http, /* I - HTTP connection to server */ | |
172 | char *command, /* I - Command string */ | |
173 | char *params) /* I - Parameters for command */ | |
174 | { | |
175 | if (compare_strings(command, "status", 4) == 0) | |
176 | show_status(http, params); | |
177 | else if (compare_strings(command, "help", 1) == 0 || | |
178 | strcmp(command, "?") == 0) | |
179 | show_help(params); | |
180 | else | |
1cb10d96 | 181 | printf("%s is not implemented by the CUPS version of lpc.\n", command); |
bd0b97ff | 182 | } |
183 | ||
184 | ||
185 | /* | |
186 | * 'show_help()' - Show help messages. | |
187 | */ | |
188 | ||
189 | static void | |
190 | show_help(char *command) /* I - Command to describe or NULL */ | |
191 | { | |
192 | if (command == NULL) | |
193 | { | |
194 | puts("Commands may be abbreviated. Commands are:"); | |
195 | puts(""); | |
196 | puts("exit help quit status ?"); | |
197 | } | |
198 | else if (compare_strings(command, "help", 1) == 0 || | |
199 | strcmp(command, "?") == 0) | |
200 | puts("help\t\tget help on commands"); | |
201 | else if (compare_strings(command, "status", 4) == 0) | |
202 | puts("status\t\tshow status of daemon and queue"); | |
203 | else | |
204 | puts("?Invalid help command unknown"); | |
205 | } | |
206 | ||
207 | ||
208 | /* | |
209 | * 'show_status()' - Show printers. | |
210 | */ | |
211 | ||
212 | static void | |
213 | show_status(http_t *http, /* I - HTTP connection to server */ | |
214 | char *dests) /* I - Destinations */ | |
215 | { | |
216 | ipp_t *request, /* IPP Request */ | |
217 | *response, /* IPP Response */ | |
218 | *jobs; /* IPP Get Jobs response */ | |
92db514a | 219 | ipp_attribute_t *attr, /* Current attribute */ |
220 | *jattr; /* Current job attribute */ | |
bd0b97ff | 221 | cups_lang_t *language; /* Default language */ |
222 | char *printer, /* Printer name */ | |
223 | *device; /* Device URI */ | |
224 | ipp_pstate_t pstate; /* Printer state */ | |
225 | int accepting; /* Is printer accepting jobs? */ | |
226 | int jobcount; /* Count of current jobs */ | |
227 | char *dptr, /* Pointer into destination list */ | |
228 | *ptr; /* Pointer into printer name */ | |
229 | int match; /* Non-zero if this job matches */ | |
230 | char printer_uri[HTTP_MAX_URI]; | |
231 | /* Printer URI */ | |
92db514a | 232 | static const char *requested[] = |
233 | { /* Requested attributes */ | |
234 | "printer-name", | |
235 | "device-uri", | |
236 | "printer-state", | |
237 | "printer-is-accepting-jobs" | |
238 | }; | |
bd0b97ff | 239 | |
240 | ||
f8ab8b97 | 241 | DEBUG_printf(("show_status(%08x, %08x)\n", http, dests)); |
bd0b97ff | 242 | |
243 | if (http == NULL) | |
244 | return; | |
245 | ||
246 | /* | |
247 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
248 | * attributes: | |
249 | * | |
250 | * attributes-charset | |
251 | * attributes-natural-language | |
252 | */ | |
253 | ||
254 | request = ippNew(); | |
255 | ||
256 | request->request.op.operation_id = CUPS_GET_PRINTERS; | |
257 | request->request.op.request_id = 1; | |
258 | ||
259 | language = cupsLangDefault(); | |
260 | ||
92db514a | 261 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
262 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
bd0b97ff | 263 | |
92db514a | 264 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, |
265 | "attributes-natural-language", NULL, language->language); | |
266 | ||
267 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
268 | "requested-attributes", sizeof(requested) / sizeof(requested[0]), | |
269 | NULL, requested); | |
bd0b97ff | 270 | |
271 | /* | |
272 | * Do the request and get back a response... | |
273 | */ | |
274 | ||
275 | if ((response = cupsDoRequest(http, request, "/printers/")) != NULL) | |
276 | { | |
f8ab8b97 | 277 | DEBUG_puts("show_status: request succeeded..."); |
bd0b97ff | 278 | |
279 | /* | |
280 | * Loop through the printers returned in the list and display | |
281 | * their status... | |
282 | */ | |
283 | ||
284 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
285 | { | |
286 | /* | |
287 | * Skip leading attributes until we hit a job... | |
288 | */ | |
289 | ||
290 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
291 | attr = attr->next; | |
292 | ||
293 | if (attr == NULL) | |
294 | break; | |
295 | ||
296 | /* | |
297 | * Pull the needed attributes from this job... | |
298 | */ | |
299 | ||
300 | printer = NULL; | |
301 | device = "file:/dev/null"; | |
302 | pstate = IPP_PRINTER_IDLE; | |
303 | jobcount = 0; | |
304 | accepting = 1; | |
305 | ||
306 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
307 | { | |
308 | if (strcmp(attr->name, "printer-name") == 0 && | |
309 | attr->value_tag == IPP_TAG_NAME) | |
310 | printer = attr->values[0].string.text; | |
311 | ||
312 | if (strcmp(attr->name, "device-uri") == 0 && | |
313 | attr->value_tag == IPP_TAG_URI) | |
314 | device = attr->values[0].string.text; | |
315 | ||
316 | if (strcmp(attr->name, "printer-state") == 0 && | |
317 | attr->value_tag == IPP_TAG_ENUM) | |
318 | pstate = (ipp_pstate_t)attr->values[0].integer; | |
319 | ||
320 | if (strcmp(attr->name, "printer-is-accepting-jobs") == 0 && | |
321 | attr->value_tag == IPP_TAG_BOOLEAN) | |
322 | accepting = attr->values[0].boolean; | |
323 | ||
324 | attr = attr->next; | |
325 | } | |
326 | ||
327 | /* | |
328 | * See if we have everything needed... | |
329 | */ | |
330 | ||
331 | if (printer == NULL) | |
332 | { | |
333 | if (attr == NULL) | |
334 | break; | |
335 | else | |
336 | continue; | |
337 | } | |
338 | ||
339 | /* | |
340 | * See if this is a printer we're interested in... | |
341 | */ | |
342 | ||
343 | match = dests == NULL; | |
344 | ||
345 | if (dests != NULL) | |
346 | { | |
347 | for (dptr = dests; *dptr != '\0';) | |
348 | { | |
349 | /* | |
350 | * Skip leading whitespace and commas... | |
351 | */ | |
352 | ||
353 | while (isspace(*dptr) || *dptr == ',') | |
354 | dptr ++; | |
355 | ||
356 | if (*dptr == '\0') | |
357 | break; | |
358 | ||
359 | /* | |
360 | * Compare names... | |
361 | */ | |
362 | ||
363 | for (ptr = printer; | |
364 | *ptr != '\0' && *dptr != '\0' && *ptr == *dptr; | |
365 | ptr ++, dptr ++); | |
366 | ||
367 | if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' || isspace(*dptr))) | |
368 | { | |
369 | match = 1; | |
370 | break; | |
371 | } | |
372 | ||
373 | /* | |
374 | * Skip trailing junk... | |
375 | */ | |
376 | ||
377 | while (!isspace(*dptr) && *dptr != '\0') | |
378 | dptr ++; | |
379 | while (isspace(*dptr) || *dptr == ',') | |
380 | dptr ++; | |
381 | ||
382 | if (*dptr == '\0') | |
383 | break; | |
384 | } | |
385 | } | |
386 | ||
387 | /* | |
388 | * Display the printer entry if needed... | |
389 | */ | |
390 | ||
391 | if (match) | |
392 | { | |
393 | /* | |
394 | * If the printer state is "IPP_PRINTER_PROCESSING", then grab the | |
395 | * current job for the printer. | |
396 | */ | |
397 | ||
398 | if (pstate == IPP_PRINTER_PROCESSING) | |
399 | { | |
400 | /* | |
401 | * Build an IPP_GET_JOBS request, which requires the following | |
402 | * attributes: | |
403 | * | |
404 | * attributes-charset | |
405 | * attributes-natural-language | |
406 | * printer-uri | |
407 | * limit | |
408 | */ | |
409 | ||
410 | request = ippNew(); | |
411 | ||
412 | request->request.op.operation_id = IPP_GET_JOBS; | |
413 | request->request.op.request_id = 1; | |
414 | ||
415 | language = cupsLangDefault(); | |
416 | ||
f8ab8b97 | 417 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
418 | "attributes-charset", NULL, | |
419 | cupsLangEncoding(language)); | |
bd0b97ff | 420 | |
f8ab8b97 | 421 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, |
422 | "attributes-natural-language", NULL, | |
423 | language->language); | |
bd0b97ff | 424 | |
970017a4 | 425 | snprintf(printer_uri, sizeof(printer_uri), |
426 | "ipp://localhost/printers/%s", printer); | |
92db514a | 427 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
428 | "printer-uri", NULL, printer_uri); | |
429 | ||
430 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
431 | "requested-attributes", NULL, "job-id"); | |
bd0b97ff | 432 | |
433 | if ((jobs = cupsDoRequest(http, request, "/jobs/")) != NULL) | |
434 | { | |
92db514a | 435 | for (jattr = jobs->attrs; jattr != NULL; jattr = jattr->next) |
436 | if (jattr->name && strcmp(jattr->name, "job-id") == 0) | |
bd0b97ff | 437 | jobcount ++; |
438 | ||
439 | ippDelete(jobs); | |
440 | } | |
441 | } | |
442 | ||
443 | /* | |
444 | * Display it... | |
445 | */ | |
446 | ||
447 | printf("%s:\n", printer); | |
448 | if (strncmp(device, "file:", 5) == 0) | |
449 | printf("\tprinter is on device \'%s\' speed -1\n", device + 5); | |
450 | else | |
344de3be | 451 | { |
452 | /* | |
453 | * Just show the method... | |
454 | */ | |
455 | ||
456 | *strchr(device, ':') = '\0'; | |
bd0b97ff | 457 | printf("\tprinter is on device \'%s\' speed -1\n", device); |
344de3be | 458 | } |
459 | ||
bd0b97ff | 460 | printf("\tqueuing is %sabled\n", accepting ? "en" : "dis"); |
461 | printf("\tprinting is %sabled\n", | |
462 | pstate == IPP_PRINTER_STOPPED ? "dis" : "en"); | |
463 | if (jobcount == 0) | |
464 | puts("\tno entries"); | |
465 | else | |
466 | printf("\t%d entries\n", jobcount); | |
467 | puts("\tdaemon present"); | |
468 | } | |
469 | ||
470 | if (attr == NULL) | |
471 | break; | |
472 | } | |
473 | ||
474 | ippDelete(response); | |
475 | } | |
476 | } | |
477 | ||
478 | ||
479 | /* | |
efb2f309 | 480 | * End of "$Id: lpc.c,v 1.13 2002/01/02 17:58:35 mike Exp $". |
bd0b97ff | 481 | */ |