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