]> git.ipfire.org Git - thirdparty/cups.git/blame - berkeley/lpq.c
Changed references to http: to ipp:.
[thirdparty/cups.git] / berkeley / lpq.c
CommitLineData
f8ab8b97 1/*
2 * "$Id: lpq.c,v 1.1 1999/06/09 20:03:47 mike Exp $"
3 *
4 * "lpq" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-1999 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-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 */
27
28/*
29 * Include necessary headers...
30 */
31
32/*
33 * Include necessary headers...
34 */
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <ctype.h>
39#include <cups/cups.h>
40#include <cups/language.h>
41#include <cups/debug.h>
42
43
44/*
45 * Local functions...
46 */
47
48static int show_jobs(http_t *, char *, char *, int, int);
49
50
51/*
52 * 'main()' - Parse options and commands.
53 */
54
55int
56main(int argc, /* I - Number of command-line arguments */
57 char *argv[]) /* I - Command-line arguments */
58{
59 int i; /* Looping var */
60 http_t *http; /* Connection to server */
61 char *dest, /* Desired printer */
62 *user; /* Desired user */
63 int id, /* Desired job ID */
64 interval, /* Reporting interval */
65 longstatus; /* Show file details */
66
67 /*
68 * Connect to the scheduler...
69 */
70
71 http = httpConnect("localhost", ippPort());
72
73 /*
74 * Check for command-line options...
75 */
76
77 dest = NULL;
78 user = NULL;
79 id = 0;
80 interval = 0;
81 longstatus = 0;
82
83 for (i = 1; i < argc; i ++)
84 if (argv[i][0] == '+')
85 interval = atoi(argv[i] + 1);
86 else if (argv[i][0] == '-')
87 {
88 switch (argv[i][1])
89 {
90 case 'P' : /* Printer */
91 if (argv[i][2])
92 dest = argv[i] + 2;
93 else
94 {
95 i ++;
96 dest = argv[i];
97 }
98 break;
99
100 case 'l' : /* Long status */
101 longstatus = 1;
102 break;
103
104 default :
105 fputs("Usage: lpq [-P dest] [-l] [+interval]\n", stderr);
106 return (1);
107 }
108 }
109 else if (isdigit(argv[i][0]))
110 id = atoi(argv[i]);
111 else
112 user = argv[i];
113
114 /*
115 * Show the status in a loop...
116 */
117
118 for (;;)
119 {
120 i = show_jobs(http, dest, user, id, longstatus);
121
122 if (i && interval)
123 sleep(interval);
124 else
125 break;
126 }
127
128 /*
129 * Close the connection to the server and return...
130 */
131
132 httpClose(http);
133
134 return (0);
135}
136
137
138/*
139 * 'show_jobs()' - Show printers.
140 */
141
142static int /* O - Number of jobs in queue */
143show_jobs(http_t *http, /* I - HTTP connection to server */
144 char *dest, /* I - Destination */
145 char *user, /* I - User */
146 int id, /* I - Job ID */
147 int longstatus) /* I - 1 if long report desired */
148{
149 ipp_t *request, /* IPP Request */
150 *response; /* IPP Response */
151 ipp_attribute_t *attr; /* Current attribute */
152 cups_lang_t *language; /* Default language */
153 char *jobdest, /* Pointer into job-printer-uri */
154 *jobuser, /* Pointer to job-originating-user-name */
155 *jobname; /* Pointer to job-name */
156 ipp_jstate_t jobstate; /* job-state */
157 int jobid, /* job-id */
158 jobsize, /* job-k-octets */
159 jobpriority, /* job-priority */
160 jobcount, /* Number of jobs */
161 rank; /* Rank of job */
162 char resource[1024]; /* Resource string */
163 static char *ranks[10] = /* Ranking strings */
164 {
165 "th",
166 "st",
167 "nd",
168 "rd",
169 "th",
170 "th",
171 "th",
172 "th",
173 "th",
174 "th"
175 };
176
177
178 DEBUG_printf(("show_jobs(%08x, %08x, %08x, %d, %d)\n", http, dest, user, id,
179 longstatus));
180
181 if (http == NULL)
182 return;
183
184 /*
185 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
186 * the following attributes:
187 *
188 * attributes-charset
189 * attributes-natural-language
190 * job-uri or printer-uri
191 * [
192 */
193
194 request = ippNew();
195
196 request->request.op.operation_id = id ? IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS;
197 request->request.op.request_id = 1;
198
199 language = cupsLangDefault();
200
201 attr = ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
202 "attributes-charset", NULL, cupsLangEncoding(language));
203
204 attr = ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
205 "attributes-natural-language", NULL, language->language);
206
207 if (dest == NULL)
208 {
209 if (id)
210 sprintf(resource, "ipp://localhost/jobs/%d", id);
211 else
212 strcpy(resource, "ipp://localhost/jobs");
213
214 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
215 NULL, resource);
216 }
217 else
218 {
219 sprintf(resource, "ipp://localhost/printers/%s", dest);
220
221 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
222 NULL, resource);
223 }
224
225 if (user)
226 {
227 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
228 "requesting-user-name", NULL, user);
229 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
230 }
231
232 /*
233 * Do the request and get back a response...
234 */
235
236 if (!longstatus)
237 puts("Rank\tPri Owner Job Files Total Size");
238
239 jobcount = 0;
240
241 if ((response = cupsDoRequest(http, request, "/jobs/")) != NULL)
242 {
243 rank = 1;
244
245 /*
246 * Loop through the job list and display them...
247 */
248
249 for (attr = response->attrs; attr != NULL; attr = attr->next)
250 {
251 /*
252 * Skip leading attributes until we hit a job...
253 */
254
255 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
256 attr = attr->next;
257
258 if (attr == NULL)
259 break;
260
261 /*
262 * Pull the needed attributes from this job...
263 */
264
265 jobid = 0;
266 jobsize = 0;
267 jobpriority = 50;
268 jobstate = IPP_JOB_PENDING;
269 jobname = "untitled";
270 jobuser = NULL;
271 jobdest = NULL;
272
273 while (attr != NULL && attr->group_tag == IPP_TAG_JOB)
274 {
275 if (strcmp(attr->name, "job-id") == 0 &&
276 attr->value_tag == IPP_TAG_INTEGER)
277 jobid = attr->values[0].integer;
278
279 if (strcmp(attr->name, "job-k-octets") == 0 &&
280 attr->value_tag == IPP_TAG_INTEGER)
281 jobsize = attr->values[0].integer * 1024;
282
283 if (strcmp(attr->name, "job-priority") == 0 &&
284 attr->value_tag == IPP_TAG_INTEGER)
285 jobpriority = attr->values[0].integer;
286
287 if (strcmp(attr->name, "job-state") == 0 &&
288 attr->value_tag == IPP_TAG_ENUM)
289 jobstate = (ipp_jstate_t)attr->values[0].integer;
290
291 if (strcmp(attr->name, "job-printer-uri") == 0 &&
292 attr->value_tag == IPP_TAG_URI)
293 if ((jobdest = strrchr(attr->values[0].string.text, '/')) != NULL)
294 jobdest ++;
295
296 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
297 attr->value_tag == IPP_TAG_NAME)
298 jobuser = attr->values[0].string.text;
299
300 if (strcmp(attr->name, "job-name") == 0 &&
301 attr->value_tag == IPP_TAG_NAME)
302 jobname = attr->values[0].string.text;
303
304 attr = attr->next;
305 }
306
307 /*
308 * See if we have everything needed...
309 */
310
311 if (jobdest == NULL || jobid == 0)
312 {
313 if (attr == NULL)
314 break;
315 else
316 continue;
317 }
318
319 jobcount ++;
320
321 /*
322 * Display the job...
323 */
324
325 if (longstatus)
326 {
327 puts("");
328
329 if (jobstate == IPP_JOB_PROCESSING)
330 printf("%s: active\t\t\t\t ", jobuser);
331 else
332 {
333 printf("%s: %d%s\t\t\t\t ", jobuser, rank, ranks[rank % 10]);
334 rank ++;
335 }
336
337 printf("[job %03dlocalhost]\n", jobid);
338 printf("\t%-33s%d bytes\n", jobname, jobsize);
339 }
340 else
341 {
342 if (jobstate == IPP_JOB_PROCESSING)
343 printf("active\t");
344 else
345 {
346 printf("%d%s\t", rank, ranks[rank % 10]);
347 rank ++;
348 }
349
350 printf(" %-5d%-7.7s%-7d%-19s%d bytes\n", jobpriority, jobuser, jobid,
351 jobname, jobsize);
352 }
353 if (attr == NULL)
354 break;
355 }
356
357 ippDelete(response);
358 }
359
360 return (jobcount);
361}
362
363
364/*
365 * End of "$Id: lpq.c,v 1.1 1999/06/09 20:03:47 mike Exp $".
366 */