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