]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * "lpc" command for CUPS. |
ef416fc2 | 3 | * |
7e86f2f6 MS |
4 | * Copyright 2007-2014 by Apple Inc. |
5 | * Copyright 1997-2006 by Easy Software Products. | |
ef416fc2 | 6 | * |
e3101897 | 7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 8 | */ |
9 | ||
10 | /* | |
11 | * Include necessary headers... | |
12 | */ | |
13 | ||
71e16022 | 14 | #include <cups/cups-private.h> |
ef416fc2 | 15 | |
16 | ||
17 | /* | |
18 | * Local functions... | |
19 | */ | |
20 | ||
7e86f2f6 | 21 | static int compare_strings(const char *, const char *, size_t); |
ef416fc2 | 22 | static void do_command(http_t *, const char *, const char *); |
23 | static void show_help(const char *); | |
24 | static void show_status(http_t *, const char *); | |
25 | ||
26 | ||
27 | /* | |
28 | * 'main()' - Parse options and commands. | |
29 | */ | |
30 | ||
31 | int | |
32 | main(int argc, /* I - Number of command-line arguments */ | |
33 | char *argv[]) /* I - Command-line arguments */ | |
34 | { | |
35 | http_t *http; /* Connection to server */ | |
36 | char line[1024], /* Input line from user */ | |
37 | *params; /* Pointer to parameters */ | |
38 | ||
39 | ||
07725fee | 40 | _cupsSetLocale(argv); |
d09495fa | 41 | |
ef416fc2 | 42 | /* |
43 | * Connect to the scheduler... | |
44 | */ | |
45 | ||
46 | http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption()); | |
47 | ||
48 | if (argc > 1) | |
49 | { | |
50 | /* | |
51 | * Process a single command on the command-line... | |
52 | */ | |
53 | ||
54 | do_command(http, argv[1], argv[2]); | |
55 | } | |
56 | else | |
57 | { | |
58 | /* | |
59 | * Do the command prompt thing... | |
60 | */ | |
61 | ||
0837b7e8 | 62 | _cupsLangPuts(stdout, _("lpc> ")); /* TODO: Need no-newline version */ |
ef416fc2 | 63 | while (fgets(line, sizeof(line), stdin) != NULL) |
64 | { | |
65 | /* | |
66 | * Strip trailing whitespace... | |
67 | */ | |
68 | ||
69 | for (params = line + strlen(line) - 1; params >= line;) | |
70 | if (!isspace(*params & 255)) | |
71 | break; | |
72 | else | |
73 | *params-- = '\0'; | |
74 | ||
75 | /* | |
76 | * Strip leading whitespace... | |
77 | */ | |
78 | ||
79 | for (params = line; isspace(*params & 255); params ++); | |
80 | ||
81 | if (params > line) | |
82 | _cups_strcpy(line, params); | |
83 | ||
84 | if (!line[0]) | |
85 | { | |
86 | /* | |
87 | * Nothing left, just show a prompt... | |
88 | */ | |
89 | ||
0837b7e8 | 90 | _cupsLangPuts(stdout, _("lpc> ")); /* TODO: Need no newline version */ |
ef416fc2 | 91 | continue; |
92 | } | |
93 | ||
94 | /* | |
95 | * Find any options in the string... | |
96 | */ | |
97 | ||
98 | for (params = line; *params != '\0'; params ++) | |
99 | if (isspace(*params & 255)) | |
100 | break; | |
101 | ||
102 | /* | |
103 | * Remove whitespace between the command and parameters... | |
104 | */ | |
105 | ||
106 | while (isspace(*params & 255)) | |
107 | *params++ = '\0'; | |
108 | ||
109 | /* | |
110 | * The "quit" and "exit" commands exit; otherwise, process as needed... | |
111 | */ | |
112 | ||
113 | if (!compare_strings(line, "quit", 1) || | |
114 | !compare_strings(line, "exit", 2)) | |
115 | break; | |
116 | ||
117 | if (*params == '\0') | |
118 | do_command(http, line, NULL); | |
119 | else | |
120 | do_command(http, line, params); | |
121 | ||
122 | /* | |
123 | * Put another prompt out to the user... | |
124 | */ | |
125 | ||
0837b7e8 | 126 | _cupsLangPuts(stdout, _("lpc> ")); /* TODO: Need no newline version */ |
ef416fc2 | 127 | } |
128 | } | |
129 | ||
130 | /* | |
131 | * Close the connection to the server and return... | |
132 | */ | |
133 | ||
134 | httpClose(http); | |
135 | ||
136 | return (0); | |
137 | } | |
138 | ||
139 | ||
140 | /* | |
141 | * 'compare_strings()' - Compare two command-line strings. | |
142 | */ | |
143 | ||
144 | static int /* O - -1 or 1 = no match, 0 = match */ | |
145 | compare_strings(const char *s, /* I - Command-line string */ | |
146 | const char *t, /* I - Option string */ | |
7e86f2f6 | 147 | size_t tmin) /* I - Minimum number of unique chars in option */ |
ef416fc2 | 148 | { |
7e86f2f6 | 149 | size_t slen; /* Length of command-line string */ |
ef416fc2 | 150 | |
151 | ||
152 | slen = strlen(s); | |
153 | if (slen < tmin) | |
154 | return (-1); | |
155 | else | |
156 | return (strncmp(s, t, slen)); | |
157 | } | |
158 | ||
159 | ||
160 | /* | |
161 | * 'do_command()' - Do an lpc command... | |
162 | */ | |
163 | ||
164 | static void | |
165 | do_command(http_t *http, /* I - HTTP connection to server */ | |
166 | const char *command, /* I - Command string */ | |
167 | const char *params) /* I - Parameters for command */ | |
168 | { | |
169 | if (!compare_strings(command, "status", 4)) | |
170 | show_status(http, params); | |
171 | else if (!compare_strings(command, "help", 1) || !strcmp(command, "?")) | |
172 | show_help(params); | |
173 | else | |
fa73b229 | 174 | _cupsLangPrintf(stdout, |
0837b7e8 | 175 | _("%s is not implemented by the CUPS version of lpc."), |
ef416fc2 | 176 | command); |
177 | } | |
178 | ||
179 | ||
180 | /* | |
181 | * 'show_help()' - Show help messages. | |
182 | */ | |
183 | ||
184 | static void | |
185 | show_help(const char *command) /* I - Command to describe or NULL */ | |
186 | { | |
187 | if (!command) | |
188 | { | |
fa73b229 | 189 | _cupsLangPrintf(stdout, |
ef416fc2 | 190 | _("Commands may be abbreviated. Commands are:\n" |
191 | "\n" | |
0837b7e8 | 192 | "exit help quit status ?")); |
ef416fc2 | 193 | } |
194 | else if (!compare_strings(command, "help", 1) || !strcmp(command, "?")) | |
0837b7e8 | 195 | _cupsLangPrintf(stdout, _("help\t\tGet help on commands.")); |
ef416fc2 | 196 | else if (!compare_strings(command, "status", 4)) |
0837b7e8 | 197 | _cupsLangPrintf(stdout, _("status\t\tShow status of daemon and queue.")); |
ef416fc2 | 198 | else |
0837b7e8 | 199 | _cupsLangPrintf(stdout, _("?Invalid help command unknown.")); |
ef416fc2 | 200 | } |
201 | ||
202 | ||
203 | /* | |
204 | * 'show_status()' - Show printers. | |
205 | */ | |
206 | ||
207 | static void | |
208 | show_status(http_t *http, /* I - HTTP connection to server */ | |
209 | const char *dests) /* I - Destinations */ | |
210 | { | |
211 | ipp_t *request, /* IPP Request */ | |
26d47ec6 | 212 | *response; /* IPP Response */ |
213 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 214 | char *printer, /* Printer name */ |
215 | *device, /* Device URI */ | |
216 | *delimiter; /* Char search result */ | |
217 | ipp_pstate_t pstate; /* Printer state */ | |
218 | int accepting; /* Is printer accepting jobs? */ | |
219 | int jobcount; /* Count of current jobs */ | |
220 | const char *dptr, /* Pointer into destination list */ | |
221 | *ptr; /* Pointer into printer name */ | |
222 | int match; /* Non-zero if this job matches */ | |
ef416fc2 | 223 | static const char *requested[] = /* Requested attributes */ |
224 | { | |
ef416fc2 | 225 | "device-uri", |
26d47ec6 | 226 | "printer-is-accepting-jobs", |
227 | "printer-name", | |
ef416fc2 | 228 | "printer-state", |
26d47ec6 | 229 | "queued-job-count" |
ef416fc2 | 230 | }; |
231 | ||
232 | ||
233 | DEBUG_printf(("show_status(http=%p, dests=\"%s\")\n", http, dests)); | |
234 | ||
235 | if (http == NULL) | |
236 | return; | |
237 | ||
238 | /* | |
239 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
240 | * attributes: | |
241 | * | |
242 | * attributes-charset | |
243 | * attributes-natural-language | |
244 | */ | |
245 | ||
0a682745 | 246 | request = ippNewRequest(CUPS_GET_PRINTERS); |
ef416fc2 | 247 | |
248 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
249 | "requested-attributes", sizeof(requested) / sizeof(requested[0]), | |
250 | NULL, requested); | |
251 | ||
252 | /* | |
253 | * Do the request and get back a response... | |
254 | */ | |
255 | ||
256 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
257 | { | |
258 | DEBUG_puts("show_status: request succeeded..."); | |
259 | ||
260 | /* | |
261 | * Loop through the printers returned in the list and display | |
262 | * their status... | |
263 | */ | |
264 | ||
265 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
266 | { | |
267 | /* | |
268 | * Skip leading attributes until we hit a job... | |
269 | */ | |
270 | ||
271 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
272 | attr = attr->next; | |
273 | ||
274 | if (attr == NULL) | |
275 | break; | |
276 | ||
277 | /* | |
278 | * Pull the needed attributes from this job... | |
279 | */ | |
280 | ||
281 | printer = NULL; | |
282 | device = "file:/dev/null"; | |
283 | pstate = IPP_PRINTER_IDLE; | |
284 | jobcount = 0; | |
285 | accepting = 1; | |
286 | ||
287 | while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) | |
288 | { | |
ef416fc2 | 289 | if (!strcmp(attr->name, "device-uri") && |
290 | attr->value_tag == IPP_TAG_URI) | |
291 | device = attr->values[0].string.text; | |
26d47ec6 | 292 | else if (!strcmp(attr->name, "printer-is-accepting-jobs") && |
293 | attr->value_tag == IPP_TAG_BOOLEAN) | |
ef416fc2 | 294 | accepting = attr->values[0].boolean; |
26d47ec6 | 295 | else if (!strcmp(attr->name, "printer-name") && |
296 | attr->value_tag == IPP_TAG_NAME) | |
297 | printer = attr->values[0].string.text; | |
298 | else if (!strcmp(attr->name, "printer-state") && | |
299 | attr->value_tag == IPP_TAG_ENUM) | |
300 | pstate = (ipp_pstate_t)attr->values[0].integer; | |
301 | else if (!strcmp(attr->name, "queued-job-count") && | |
302 | attr->value_tag == IPP_TAG_INTEGER) | |
303 | jobcount = attr->values[0].integer; | |
ef416fc2 | 304 | |
305 | attr = attr->next; | |
306 | } | |
307 | ||
308 | /* | |
309 | * See if we have everything needed... | |
310 | */ | |
311 | ||
312 | if (printer == NULL) | |
313 | { | |
314 | if (attr == NULL) | |
315 | break; | |
316 | else | |
317 | continue; | |
318 | } | |
319 | ||
320 | /* | |
321 | * A single 'all' printer name is special, meaning all printers. | |
322 | */ | |
323 | ||
324 | if (dests != NULL && !strcmp(dests, "all")) | |
325 | dests = NULL; | |
326 | ||
327 | /* | |
328 | * See if this is a printer we're interested in... | |
329 | */ | |
330 | ||
331 | match = dests == NULL; | |
332 | ||
333 | if (dests != NULL) | |
334 | { | |
335 | for (dptr = dests; *dptr != '\0';) | |
336 | { | |
337 | /* | |
338 | * Skip leading whitespace and commas... | |
339 | */ | |
340 | ||
341 | while (isspace(*dptr & 255) || *dptr == ',') | |
342 | dptr ++; | |
343 | ||
344 | if (*dptr == '\0') | |
345 | break; | |
346 | ||
347 | /* | |
348 | * Compare names... | |
349 | */ | |
350 | ||
351 | for (ptr = printer; | |
352 | *ptr != '\0' && *dptr != '\0' && *ptr == *dptr; | |
82cc1f9a MS |
353 | ptr ++, dptr ++) |
354 | /* do nothing */; | |
ef416fc2 | 355 | |
26d47ec6 | 356 | if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' || |
357 | isspace(*dptr & 255))) | |
ef416fc2 | 358 | { |
359 | match = 1; | |
360 | break; | |
361 | } | |
362 | ||
363 | /* | |
364 | * Skip trailing junk... | |
365 | */ | |
366 | ||
367 | while (!isspace(*dptr & 255) && *dptr != '\0') | |
368 | dptr ++; | |
369 | while (isspace(*dptr & 255) || *dptr == ',') | |
370 | dptr ++; | |
371 | ||
372 | if (*dptr == '\0') | |
373 | break; | |
374 | } | |
375 | } | |
376 | ||
377 | /* | |
378 | * Display the printer entry if needed... | |
379 | */ | |
380 | ||
381 | if (match) | |
382 | { | |
ef416fc2 | 383 | /* |
384 | * Display it... | |
385 | */ | |
386 | ||
387 | printf("%s:\n", printer); | |
388 | if (!strncmp(device, "file:", 5)) | |
fa73b229 | 389 | _cupsLangPrintf(stdout, |
0837b7e8 | 390 | _("\tprinter is on device \'%s\' speed -1"), |
ef416fc2 | 391 | device + 5); |
392 | else | |
393 | { | |
394 | /* | |
395 | * Just show the scheme... | |
396 | */ | |
397 | ||
398 | if ((delimiter = strchr(device, ':')) != NULL ) | |
399 | { | |
400 | *delimiter = '\0'; | |
fa73b229 | 401 | _cupsLangPrintf(stdout, |
0837b7e8 | 402 | _("\tprinter is on device \'%s\' speed -1"), |
ef416fc2 | 403 | device); |
404 | } | |
405 | } | |
406 | ||
407 | if (accepting) | |
0837b7e8 | 408 | _cupsLangPuts(stdout, _("\tqueuing is enabled")); |
ef416fc2 | 409 | else |
0837b7e8 | 410 | _cupsLangPuts(stdout, _("\tqueuing is disabled")); |
ef416fc2 | 411 | |
412 | if (pstate != IPP_PRINTER_STOPPED) | |
0837b7e8 | 413 | _cupsLangPuts(stdout, _("\tprinting is enabled")); |
ef416fc2 | 414 | else |
0837b7e8 | 415 | _cupsLangPuts(stdout, _("\tprinting is disabled")); |
ef416fc2 | 416 | |
417 | if (jobcount == 0) | |
0837b7e8 | 418 | _cupsLangPuts(stdout, _("\tno entries")); |
ef416fc2 | 419 | else |
0837b7e8 | 420 | _cupsLangPrintf(stdout, _("\t%d entries"), jobcount); |
ef416fc2 | 421 | |
0837b7e8 | 422 | _cupsLangPuts(stdout, _("\tdaemon present")); |
ef416fc2 | 423 | } |
424 | ||
425 | if (attr == NULL) | |
426 | break; | |
427 | } | |
428 | ||
429 | ippDelete(response); | |
430 | } | |
431 | } |