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