]> git.ipfire.org Git - thirdparty/cups.git/blame - berkeley/lpc.c
Move debug printfs to internal usage only.
[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 *
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 21static int compare_strings(const char *, const char *, size_t);
ef416fc2 22static void do_command(http_t *, const char *, const char *);
23static void show_help(const char *);
24static void show_status(http_t *, const char *);
25
26
27/*
28 * 'main()' - Parse options and commands.
29 */
30
31int
32main(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
144static int /* O - -1 or 1 = no match, 0 = match */
145compare_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
164static void
165do_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
184static void
185show_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
207static void
208show_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
ef416fc2 233 if (http == NULL)
234 return;
235
236 /*
237 * Build a CUPS_GET_PRINTERS request, which requires the following
238 * attributes:
239 *
240 * attributes-charset
241 * attributes-natural-language
242 */
243
0a682745 244 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 245
246 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
247 "requested-attributes", sizeof(requested) / sizeof(requested[0]),
248 NULL, requested);
249
250 /*
251 * Do the request and get back a response...
252 */
253
254 if ((response = cupsDoRequest(http, request, "/")) != NULL)
255 {
ef416fc2 256 /*
257 * Loop through the printers returned in the list and display
258 * their status...
259 */
260
261 for (attr = response->attrs; attr != NULL; attr = attr->next)
262 {
263 /*
264 * Skip leading attributes until we hit a job...
265 */
266
267 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
268 attr = attr->next;
269
270 if (attr == NULL)
271 break;
272
273 /*
274 * Pull the needed attributes from this job...
275 */
276
277 printer = NULL;
278 device = "file:/dev/null";
279 pstate = IPP_PRINTER_IDLE;
280 jobcount = 0;
281 accepting = 1;
282
283 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
284 {
ef416fc2 285 if (!strcmp(attr->name, "device-uri") &&
286 attr->value_tag == IPP_TAG_URI)
287 device = attr->values[0].string.text;
26d47ec6 288 else if (!strcmp(attr->name, "printer-is-accepting-jobs") &&
289 attr->value_tag == IPP_TAG_BOOLEAN)
ef416fc2 290 accepting = attr->values[0].boolean;
26d47ec6 291 else if (!strcmp(attr->name, "printer-name") &&
292 attr->value_tag == IPP_TAG_NAME)
293 printer = attr->values[0].string.text;
294 else if (!strcmp(attr->name, "printer-state") &&
295 attr->value_tag == IPP_TAG_ENUM)
296 pstate = (ipp_pstate_t)attr->values[0].integer;
297 else if (!strcmp(attr->name, "queued-job-count") &&
298 attr->value_tag == IPP_TAG_INTEGER)
299 jobcount = attr->values[0].integer;
ef416fc2 300
301 attr = attr->next;
302 }
303
304 /*
305 * See if we have everything needed...
306 */
307
308 if (printer == NULL)
309 {
310 if (attr == NULL)
311 break;
312 else
313 continue;
314 }
315
316 /*
317 * A single 'all' printer name is special, meaning all printers.
318 */
319
320 if (dests != NULL && !strcmp(dests, "all"))
321 dests = NULL;
322
323 /*
324 * See if this is a printer we're interested in...
325 */
326
327 match = dests == NULL;
328
329 if (dests != NULL)
330 {
331 for (dptr = dests; *dptr != '\0';)
332 {
333 /*
334 * Skip leading whitespace and commas...
335 */
336
337 while (isspace(*dptr & 255) || *dptr == ',')
338 dptr ++;
339
340 if (*dptr == '\0')
341 break;
342
343 /*
344 * Compare names...
345 */
346
347 for (ptr = printer;
348 *ptr != '\0' && *dptr != '\0' && *ptr == *dptr;
82cc1f9a
MS
349 ptr ++, dptr ++)
350 /* do nothing */;
ef416fc2 351
26d47ec6 352 if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' ||
353 isspace(*dptr & 255)))
ef416fc2 354 {
355 match = 1;
356 break;
357 }
358
359 /*
360 * Skip trailing junk...
361 */
362
363 while (!isspace(*dptr & 255) && *dptr != '\0')
364 dptr ++;
365 while (isspace(*dptr & 255) || *dptr == ',')
366 dptr ++;
367
368 if (*dptr == '\0')
369 break;
370 }
371 }
372
373 /*
374 * Display the printer entry if needed...
375 */
376
377 if (match)
378 {
ef416fc2 379 /*
380 * Display it...
381 */
382
383 printf("%s:\n", printer);
384 if (!strncmp(device, "file:", 5))
fa73b229 385 _cupsLangPrintf(stdout,
0837b7e8 386 _("\tprinter is on device \'%s\' speed -1"),
ef416fc2 387 device + 5);
388 else
389 {
390 /*
391 * Just show the scheme...
392 */
393
394 if ((delimiter = strchr(device, ':')) != NULL )
395 {
396 *delimiter = '\0';
fa73b229 397 _cupsLangPrintf(stdout,
0837b7e8 398 _("\tprinter is on device \'%s\' speed -1"),
ef416fc2 399 device);
400 }
401 }
402
403 if (accepting)
0837b7e8 404 _cupsLangPuts(stdout, _("\tqueuing is enabled"));
ef416fc2 405 else
0837b7e8 406 _cupsLangPuts(stdout, _("\tqueuing is disabled"));
ef416fc2 407
408 if (pstate != IPP_PRINTER_STOPPED)
0837b7e8 409 _cupsLangPuts(stdout, _("\tprinting is enabled"));
ef416fc2 410 else
0837b7e8 411 _cupsLangPuts(stdout, _("\tprinting is disabled"));
ef416fc2 412
413 if (jobcount == 0)
0837b7e8 414 _cupsLangPuts(stdout, _("\tno entries"));
ef416fc2 415 else
0837b7e8 416 _cupsLangPrintf(stdout, _("\t%d entries"), jobcount);
ef416fc2 417
0837b7e8 418 _cupsLangPuts(stdout, _("\tdaemon present"));
ef416fc2 419 }
420
421 if (attr == NULL)
422 break;
423 }
424
425 ippDelete(response);
426 }
427}