1355b4669Sjacobs /*
2355b4669Sjacobs * CDDL HEADER START
3355b4669Sjacobs *
4355b4669Sjacobs * The contents of this file are subject to the terms of the
5355b4669Sjacobs * Common Development and Distribution License (the "License").
6355b4669Sjacobs * You may not use this file except in compliance with the License.
7355b4669Sjacobs *
8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs * See the License for the specific language governing permissions
11355b4669Sjacobs * and limitations under the License.
12355b4669Sjacobs *
13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs *
19355b4669Sjacobs * CDDL HEADER END
20355b4669Sjacobs */
21355b4669Sjacobs
22355b4669Sjacobs /*
23*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24355b4669Sjacobs * Use is subject to license terms.
25355b4669Sjacobs *
26355b4669Sjacobs */
27355b4669Sjacobs
28355b4669Sjacobs /* $Id: lpstat.c 173 2006-05-25 04:52:06Z njacobs $ */
29355b4669Sjacobs
30355b4669Sjacobs
31355b4669Sjacobs #include <stdio.h>
32355b4669Sjacobs #include <stdlib.h>
33355b4669Sjacobs #include <unistd.h>
34355b4669Sjacobs #include <string.h>
35355b4669Sjacobs #include <locale.h>
36355b4669Sjacobs #include <libintl.h>
3791216fe4Swendyp #include <ctype.h>
38355b4669Sjacobs #include <pwd.h>
39355b4669Sjacobs #include <papi.h>
40355b4669Sjacobs #include <uri.h>
41355b4669Sjacobs #include "common.h"
42375b28ffSGowtham Thommandra #include "lp.h"
43355b4669Sjacobs
44355b4669Sjacobs static void
usage(char * program)45355b4669Sjacobs usage(char *program)
46355b4669Sjacobs {
47355b4669Sjacobs char *name;
48355b4669Sjacobs
49355b4669Sjacobs if ((name = strrchr(program, '/')) == NULL)
50355b4669Sjacobs name = program;
51355b4669Sjacobs else
52355b4669Sjacobs name++;
53355b4669Sjacobs
54355b4669Sjacobs fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] "
55355b4669Sjacobs "[-c [list]] [-o [list] [-l]] [-R [list] [-l]] "
56355b4669Sjacobs "[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] "
57355b4669Sjacobs "[-f [list] [-l]] [-u list]\n"),
58355b4669Sjacobs name);
59355b4669Sjacobs exit(1);
60355b4669Sjacobs }
61355b4669Sjacobs
62355b4669Sjacobs static char *
nctime(time_t * t)63355b4669Sjacobs nctime(time_t *t)
64355b4669Sjacobs {
65355b4669Sjacobs static char buf[64];
66355b4669Sjacobs struct tm *tm = localtime(t);
67355b4669Sjacobs
68355b4669Sjacobs (void) strftime(buf, sizeof (buf), "%c", tm);
69355b4669Sjacobs
70355b4669Sjacobs return (buf);
71355b4669Sjacobs }
72355b4669Sjacobs
73355b4669Sjacobs static char *
printer_name(papi_printer_t printer)74355b4669Sjacobs printer_name(papi_printer_t printer)
75355b4669Sjacobs {
76355b4669Sjacobs papi_attribute_t **attributes = papiPrinterGetAttributeList(printer);
77355b4669Sjacobs char *result = NULL;
78355b4669Sjacobs
79355b4669Sjacobs if (attributes != NULL)
80355b4669Sjacobs papiAttributeListGetString(attributes, NULL,
81355b4669Sjacobs "printer-name", &result);
82355b4669Sjacobs
83355b4669Sjacobs return (result);
84355b4669Sjacobs }
85355b4669Sjacobs
86355b4669Sjacobs static int
lpstat_default_printer(papi_encryption_t encryption)87355b4669Sjacobs lpstat_default_printer(papi_encryption_t encryption)
88355b4669Sjacobs {
89355b4669Sjacobs papi_status_t status;
90355b4669Sjacobs papi_service_t svc = NULL;
91355b4669Sjacobs papi_printer_t p = NULL;
92355b4669Sjacobs char *name = NULL;
93355b4669Sjacobs
94355b4669Sjacobs status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback,
95355b4669Sjacobs encryption, NULL);
96355b4669Sjacobs if (status == PAPI_OK) {
97355b4669Sjacobs char *req[] = { "printer-name", NULL };
98355b4669Sjacobs
99355b4669Sjacobs status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p);
100355b4669Sjacobs if (p != NULL)
101355b4669Sjacobs name = printer_name(p);
102355b4669Sjacobs }
103355b4669Sjacobs if (name != NULL)
104355b4669Sjacobs printf(gettext("system default printer: %s\n"), name);
105355b4669Sjacobs else
106355b4669Sjacobs printf(gettext("no system default destination\n"));
107355b4669Sjacobs papiPrinterFree(p);
108355b4669Sjacobs papiServiceDestroy(svc);
109355b4669Sjacobs
110355b4669Sjacobs return (0);
111355b4669Sjacobs }
112355b4669Sjacobs
113355b4669Sjacobs static int
lpstat_service_status(papi_encryption_t encryption)114355b4669Sjacobs lpstat_service_status(papi_encryption_t encryption)
115355b4669Sjacobs {
116355b4669Sjacobs papi_status_t status;
117355b4669Sjacobs papi_service_t svc = NULL;
118355b4669Sjacobs char *name = NULL;
119355b4669Sjacobs
120355b4669Sjacobs if (((name = getenv("PAPI_SERVICE_URI")) == NULL) &&
121355b4669Sjacobs ((name = getenv("IPP_SERVER")) == NULL) &&
122355b4669Sjacobs ((name = getenv("CUPS_SERVER")) == NULL))
123355b4669Sjacobs name = DEFAULT_SERVICE_URI;
124355b4669Sjacobs
125355b4669Sjacobs status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
126355b4669Sjacobs encryption, NULL);
127355b4669Sjacobs if (status != PAPI_OK) {
128355b4669Sjacobs printf(gettext("scheduler is not running\n"));
129355b4669Sjacobs } else
130355b4669Sjacobs printf(gettext("scheduler is running\n"));
131355b4669Sjacobs papiServiceDestroy(svc);
132355b4669Sjacobs
133f154fbfeSGowtham Thommandra return (0);
134355b4669Sjacobs }
135355b4669Sjacobs
136355b4669Sjacobs static char *
get_device_uri(papi_service_t svc,char * name)137355b4669Sjacobs get_device_uri(papi_service_t svc, char *name)
138355b4669Sjacobs {
139355b4669Sjacobs papi_status_t status;
140355b4669Sjacobs papi_printer_t p = NULL;
141355b4669Sjacobs char *keys[] = { "device-uri", NULL };
142355b4669Sjacobs char *result = NULL;
143355b4669Sjacobs
144355b4669Sjacobs status = papiPrinterQuery(svc, name, keys, NULL, &p);
145355b4669Sjacobs if ((status == PAPI_OK) && (p != NULL)) {
146355b4669Sjacobs papi_attribute_t **attrs = papiPrinterGetAttributeList(p);
147355b4669Sjacobs
148355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
149355b4669Sjacobs "device-uri", &result);
150355b4669Sjacobs if (result != NULL)
151355b4669Sjacobs result = strdup(result);
152355b4669Sjacobs
153355b4669Sjacobs papiPrinterFree(p);
154355b4669Sjacobs }
155355b4669Sjacobs
156355b4669Sjacobs return (result);
157355b4669Sjacobs }
158355b4669Sjacobs
159f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India static void
print_description(papi_attribute_t ** list,char * printer_name)160f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India print_description(papi_attribute_t **list, char *printer_name)
161f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India {
162f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India char *str = "";
163f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India
164f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India (void) papiAttributeListGetString(list, NULL,
165f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India "printer-info", &str);
166f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India
167f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India /*
168f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India * If no printer-info is read then
169f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India * by default the printer-info is <printer-name>@<server>
170f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India */
171f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India if (str[0] == '\0') {
172f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India char *uri = NULL;
173f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India uri_t *u = NULL;
174f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India
175f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India (void) papiAttributeListGetString(list, NULL,
176f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India "printer-uri-supported", &uri);
177f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India
178f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
179f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India char *nodename = localhostname();
180f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India
181f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India if ((u->host == NULL) ||
182f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India (strcasecmp(u->host, "localhost") == 0) ||
183f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India (strcasecmp(u->host, nodename) == 0))
184f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext("\tDescription:\n"));
185f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India else
186f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext("\tDescription: %s@%s\n"),
187f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India printer_name, u->host);
188f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India
189f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India uri_free(u);
190f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India } else
191f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext("\tDescription:\n"));
192f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India } else
193f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext("\tDescription: %s\n"), str);
194f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India }
195f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India
196355b4669Sjacobs static char *report_device_keys[] = { "printer-name", "printer-uri-supported",
197355b4669Sjacobs NULL };
198355b4669Sjacobs /* ARGSUSED2 */
199355b4669Sjacobs static int
report_device(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)200355b4669Sjacobs report_device(papi_service_t svc, char *name, papi_printer_t printer,
201355b4669Sjacobs int verbose, int description)
202355b4669Sjacobs {
203355b4669Sjacobs papi_status_t status;
204355b4669Sjacobs papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
205355b4669Sjacobs char *uri = NULL;
206355b4669Sjacobs char *device = NULL;
207355b4669Sjacobs uri_t *u = NULL;
208355b4669Sjacobs
209355b4669Sjacobs if (name == NULL) {
210355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
211355b4669Sjacobs "printer-name", &name);
212355b4669Sjacobs if (status != PAPI_OK)
213355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
214355b4669Sjacobs "printer-uri-supported", &name);
215355b4669Sjacobs }
216355b4669Sjacobs
217355b4669Sjacobs if (name == NULL)
218355b4669Sjacobs return (-1);
219355b4669Sjacobs
220355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
221355b4669Sjacobs "printer-uri-supported", &uri);
222355b4669Sjacobs
223355b4669Sjacobs if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
224355b4669Sjacobs char *nodename = localhostname();
225355b4669Sjacobs
226355b4669Sjacobs if ((u->host == NULL) ||
227355b4669Sjacobs (strcasecmp(u->host, "localhost") == 0) ||
228355b4669Sjacobs (strcasecmp(u->host, nodename) == 0))
229355b4669Sjacobs device = get_device_uri(svc, name);
230355b4669Sjacobs
231355b4669Sjacobs if (device != NULL) {
232355b4669Sjacobs printf(gettext("device for %s: %s\n"), name, device);
233355b4669Sjacobs return (0);
234355b4669Sjacobs } else if (uri != NULL) {
235355b4669Sjacobs printf(gettext("system for %s: %s (as %s)\n"), name,
236e059026eSKeerthi Kondaka u->host?u->host:"localhost", uri);
237355b4669Sjacobs return (0);
238355b4669Sjacobs }
239355b4669Sjacobs
240355b4669Sjacobs uri_free(u);
241355b4669Sjacobs }
242355b4669Sjacobs
243355b4669Sjacobs return (0);
244355b4669Sjacobs }
245355b4669Sjacobs
246355b4669Sjacobs static char *report_accepting_keys[] = { "printer-name",
247355b4669Sjacobs "printer-uri-supported", "printer-is-accepting-jobs",
248355b4669Sjacobs "printer-up-time", "printer-state-time",
249355b4669Sjacobs "lpsched-reject-date", "lpsched-reject-reason", NULL };
250355b4669Sjacobs /* ARGSUSED2 */
251355b4669Sjacobs static int
report_accepting(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)252355b4669Sjacobs report_accepting(papi_service_t svc, char *name, papi_printer_t printer,
253355b4669Sjacobs int verbose, int description)
254355b4669Sjacobs {
255355b4669Sjacobs papi_status_t status;
256355b4669Sjacobs papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
257355b4669Sjacobs time_t curr;
258355b4669Sjacobs char boolean = PAPI_FALSE;
259355b4669Sjacobs
260355b4669Sjacobs if (name == NULL) {
261355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
262355b4669Sjacobs "printer-name", &name);
263355b4669Sjacobs if (status != PAPI_OK)
264355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
265355b4669Sjacobs "printer-uri-supported", &name);
266355b4669Sjacobs }
267355b4669Sjacobs if (name == NULL)
268355b4669Sjacobs return (-1);
269355b4669Sjacobs
270355b4669Sjacobs (void) papiAttributeListGetBoolean(attrs, NULL,
271355b4669Sjacobs "printer-is-accepting-jobs", &boolean);
272355b4669Sjacobs (void) time(&curr);
273355b4669Sjacobs (void) papiAttributeListGetDatetime(attrs, NULL,
274355b4669Sjacobs "printer-up-time", &curr);
275355b4669Sjacobs (void) papiAttributeListGetDatetime(attrs, NULL,
276355b4669Sjacobs "printer-state-time", &curr);
277355b4669Sjacobs (void) papiAttributeListGetDatetime(attrs, NULL,
278355b4669Sjacobs "lpsched-reject-date", &curr);
279355b4669Sjacobs
280355b4669Sjacobs if (boolean == PAPI_TRUE) {
281355b4669Sjacobs printf(gettext("%s accepting requests since %s\n"),
282355b4669Sjacobs name, nctime(&curr));
283355b4669Sjacobs } else {
284355b4669Sjacobs char *reason = "unknown reason";
285355b4669Sjacobs
286355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
287355b4669Sjacobs "lpsched-reject-reason", &reason);
288355b4669Sjacobs
289355b4669Sjacobs printf(gettext("%s not accepting requests since %s\n\t%s\n"),
290355b4669Sjacobs name, nctime(&curr), reason);
291355b4669Sjacobs }
292355b4669Sjacobs
293355b4669Sjacobs return (0);
294355b4669Sjacobs }
295355b4669Sjacobs
296355b4669Sjacobs static char *report_class_keys[] = { "printer-name", "printer-uri-supported",
297355b4669Sjacobs "member-names", NULL };
298355b4669Sjacobs /* ARGSUSED2 */
299355b4669Sjacobs static int
report_class(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)300355b4669Sjacobs report_class(papi_service_t svc, char *name, papi_printer_t printer,
301355b4669Sjacobs int verbose, int description)
302355b4669Sjacobs {
303355b4669Sjacobs papi_status_t status;
304355b4669Sjacobs papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
305355b4669Sjacobs char *member = NULL;
306355b4669Sjacobs void *iter = NULL;
307355b4669Sjacobs
308355b4669Sjacobs status = papiAttributeListGetString(attrs, &iter,
309355b4669Sjacobs "member-names", &member);
310355b4669Sjacobs if (status == PAPI_NOT_FOUND) /* it's not a class */
311355b4669Sjacobs return (0);
312355b4669Sjacobs
313355b4669Sjacobs if (name == NULL) {
314355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
315355b4669Sjacobs "printer-name", &name);
316355b4669Sjacobs if (status != PAPI_OK)
317355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
318355b4669Sjacobs "printer-uri-supported", &name);
319355b4669Sjacobs }
320355b4669Sjacobs if (name == NULL)
321355b4669Sjacobs return (-1);
322355b4669Sjacobs
323355b4669Sjacobs printf(gettext("members of class %s:\n\t%s\n"), name, member);
324355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter, NULL, &member)
325355b4669Sjacobs == PAPI_OK)
326355b4669Sjacobs printf("\t%s\n", member);
327355b4669Sjacobs
328355b4669Sjacobs return (0);
329355b4669Sjacobs }
330355b4669Sjacobs
331e9a20b61SKeerthi Kondaka static int
get_remote_hostname(papi_attribute_t ** attrs,char ** host)332e9a20b61SKeerthi Kondaka get_remote_hostname(papi_attribute_t **attrs, char **host)
333e9a20b61SKeerthi Kondaka {
334e9a20b61SKeerthi Kondaka char *uri = NULL;
335e9a20b61SKeerthi Kondaka uri_t *u;
336e9a20b61SKeerthi Kondaka char *nodename;
337e9a20b61SKeerthi Kondaka
338e9a20b61SKeerthi Kondaka *host = NULL;
339e9a20b61SKeerthi Kondaka (void) papiAttributeListGetString(attrs, NULL,
340e9a20b61SKeerthi Kondaka "job-originating-host-name", host);
341e9a20b61SKeerthi Kondaka (void) papiAttributeListGetString(attrs, NULL,
342e9a20b61SKeerthi Kondaka "printer-uri-supported", &uri);
343e9a20b61SKeerthi Kondaka if (*host == NULL) {
344e9a20b61SKeerthi Kondaka if (uri != NULL) {
345e9a20b61SKeerthi Kondaka if (uri_from_string(uri, &u) == 0) {
346e9a20b61SKeerthi Kondaka if (u->host == NULL) {
347e9a20b61SKeerthi Kondaka uri_free(u);
348e9a20b61SKeerthi Kondaka return (0);
349e9a20b61SKeerthi Kondaka }
350e9a20b61SKeerthi Kondaka *host = strdup(u->host);
351e9a20b61SKeerthi Kondaka uri_free(u);
352e9a20b61SKeerthi Kondaka } else {
353e9a20b61SKeerthi Kondaka return (0);
354e9a20b61SKeerthi Kondaka }
355e9a20b61SKeerthi Kondaka } else {
356e9a20b61SKeerthi Kondaka return (0);
357e9a20b61SKeerthi Kondaka }
358e9a20b61SKeerthi Kondaka }
359e9a20b61SKeerthi Kondaka nodename = localhostname();
360e9a20b61SKeerthi Kondaka if ((strcasecmp(*host, "localhost") == 0) ||
361e9a20b61SKeerthi Kondaka (strcasecmp(*host, nodename) == 0)) {
362e9a20b61SKeerthi Kondaka return (0);
363e9a20b61SKeerthi Kondaka }
364e9a20b61SKeerthi Kondaka return (1);
365e9a20b61SKeerthi Kondaka }
366e9a20b61SKeerthi Kondaka
367355b4669Sjacobs static char *report_printer_keys[] = { "printer-name",
368355b4669Sjacobs "printer-uri-supported", "printer-state",
369355b4669Sjacobs "printer-up-time", "printer-state-time",
370355b4669Sjacobs "lpsched-disable-date", "printer-state-reasons",
371355b4669Sjacobs "lpsched-disable-reason", NULL };
372355b4669Sjacobs /* ARGSUSED2 */
373355b4669Sjacobs static int
report_printer(papi_service_t svc,char * name,papi_printer_t printer,int verbose,int description)374355b4669Sjacobs report_printer(papi_service_t svc, char *name, papi_printer_t printer,
375355b4669Sjacobs int verbose, int description)
376355b4669Sjacobs {
377355b4669Sjacobs papi_status_t status;
378355b4669Sjacobs papi_attribute_t **attrs = papiPrinterGetAttributeList(printer);
379355b4669Sjacobs time_t curr;
380355b4669Sjacobs int32_t pstat = 0;
381355b4669Sjacobs char *member = NULL;
38254d5ddccSsonam gupta - Sun Microsystems - Bangalore India papi_job_t *j = NULL;
383355b4669Sjacobs
384355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
385355b4669Sjacobs "member-names", &member);
386355b4669Sjacobs if (status == PAPI_OK) /* it's a class */
387355b4669Sjacobs return (0);
388355b4669Sjacobs
389355b4669Sjacobs if (name == NULL) {
390355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
391355b4669Sjacobs "printer-name", &name);
392355b4669Sjacobs if (status != PAPI_OK)
393355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
394355b4669Sjacobs "printer-uri-supported", &name);
395355b4669Sjacobs }
396355b4669Sjacobs if (name == NULL)
397355b4669Sjacobs return (-1);
398355b4669Sjacobs
399355b4669Sjacobs printf(gettext("printer %s "), name);
400355b4669Sjacobs
401355b4669Sjacobs status = papiAttributeListGetInteger(attrs, NULL,
402355b4669Sjacobs "printer-state", &pstat);
403355b4669Sjacobs
404355b4669Sjacobs switch (pstat) {
405355b4669Sjacobs case 0x03: /* idle */
40638c3e53dSsonam gupta - Sun Microsystems - Bangalore India printf(gettext("is idle. enabled"));
407355b4669Sjacobs break;
40854d5ddccSsonam gupta - Sun Microsystems - Bangalore India case 0x04: /* processing */
409*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India case 0x06: /* faulted printing */
41054d5ddccSsonam gupta - Sun Microsystems - Bangalore India status = papiPrinterListJobs(svc, name, NULL,
41154d5ddccSsonam gupta - Sun Microsystems - Bangalore India 0, 0, &j);
41254d5ddccSsonam gupta - Sun Microsystems - Bangalore India
41354d5ddccSsonam gupta - Sun Microsystems - Bangalore India if (status == PAPI_OK) {
41454d5ddccSsonam gupta - Sun Microsystems - Bangalore India if (j != NULL) {
41554d5ddccSsonam gupta - Sun Microsystems - Bangalore India int i = 0;
416355b4669Sjacobs int32_t jobid = 0;
41754d5ddccSsonam gupta - Sun Microsystems - Bangalore India int32_t jstate = 0;
418355b4669Sjacobs
41954d5ddccSsonam gupta - Sun Microsystems - Bangalore India for (i = 0; j[i] != NULL; ++i) {
420356a8421Ssonam gupta - Sun Microsystems - Bangalore India papi_attribute_t **attr =
421356a8421Ssonam gupta - Sun Microsystems - Bangalore India papiJobGetAttributeList(j[i]);
42254d5ddccSsonam gupta - Sun Microsystems - Bangalore India
423356a8421Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(attr,
42454d5ddccSsonam gupta - Sun Microsystems - Bangalore India NULL, "job-state", &jstate);
425356a8421Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(attr,
42654d5ddccSsonam gupta - Sun Microsystems - Bangalore India NULL, "job-id", &jobid);
4274c64d8ccSsonam gupta - Sun Microsystems - Bangalore India /*
4284c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * For lpd protocol "job-id-requested"
4294c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * should be read.
4304c64d8ccSsonam gupta - Sun Microsystems - Bangalore India */
4314c64d8ccSsonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(attr,
4324c64d8ccSsonam gupta - Sun Microsystems - Bangalore India NULL, "job-id-requested", &jobid);
43354d5ddccSsonam gupta - Sun Microsystems - Bangalore India
43454d5ddccSsonam gupta - Sun Microsystems - Bangalore India /*
4354c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * When lpd protocol is used job-state
4364c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * cannot be retrieved, therefore
4374c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * job-state will be 0.
4384c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * When ipp protocol is used, the
4394c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * active/printing job-state will be
4404c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * RS_PRINTING (0x0008) post s10u5.
4414c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * For pre-s10u5 job-state will be
4424c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * RS_ACTIVE (0x05). So print only when
4434c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * the job-state is RS_PRINTING (0x0008)
4444c64d8ccSsonam gupta - Sun Microsystems - Bangalore India * or RS_ACTIVE (0x05) or 0
44554d5ddccSsonam gupta - Sun Microsystems - Bangalore India */
4464c64d8ccSsonam gupta - Sun Microsystems - Bangalore India if ((jstate == 0x0008) ||
4474c64d8ccSsonam gupta - Sun Microsystems - Bangalore India (jstate == 0x05) ||
4484c64d8ccSsonam gupta - Sun Microsystems - Bangalore India (jstate == 0)) {
449*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India if (pstat == 0x04)
45054d5ddccSsonam gupta - Sun Microsystems - Bangalore India printf(gettext
45154d5ddccSsonam gupta - Sun Microsystems - Bangalore India ("now printing"\
45254d5ddccSsonam gupta - Sun Microsystems - Bangalore India " %s-%d. enabled"),
45354d5ddccSsonam gupta - Sun Microsystems - Bangalore India name, jobid);
454*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India if (pstat == 0x06)
455*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext
456*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India ("faulted printing"\
457*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India " %s-%d. enabled"),
458*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India name, jobid);
4594c64d8ccSsonam gupta - Sun Microsystems - Bangalore India break;
46054d5ddccSsonam gupta - Sun Microsystems - Bangalore India }
46154d5ddccSsonam gupta - Sun Microsystems - Bangalore India }
462355b4669Sjacobs papiJobListFree(j);
46354d5ddccSsonam gupta - Sun Microsystems - Bangalore India }
464355b4669Sjacobs }
465355b4669Sjacobs break;
466355b4669Sjacobs case 0x05: /* stopped */
467355b4669Sjacobs printf(gettext("disabled"));
468355b4669Sjacobs break;
469*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India case 0x07: /* faulted printer */
470*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext("faulted. enabled"));
471*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India break;
472*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India case 0x08: /* waiting for auto retry */
473*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext("waiting for auto-retry."));
474*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India break;
475355b4669Sjacobs default:
476355b4669Sjacobs printf(gettext("unknown state(0x%x)."), pstat);
477355b4669Sjacobs break;
478355b4669Sjacobs }
479355b4669Sjacobs
480*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India if (pstat == 0x08)
481*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India printf(gettext(" available.\n"));
482*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India else {
483355b4669Sjacobs (void) time(&curr);
484355b4669Sjacobs (void) papiAttributeListGetDatetime(attrs, NULL,
485355b4669Sjacobs "printer-up-time", &curr);
486355b4669Sjacobs (void) papiAttributeListGetDatetime(attrs, NULL,
487355b4669Sjacobs "printer-state-time", &curr);
488355b4669Sjacobs (void) papiAttributeListGetDatetime(attrs, NULL,
489355b4669Sjacobs "lpsched-disable-date", &curr);
490355b4669Sjacobs printf(gettext(" since %s. available.\n"), nctime(&curr));
491*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India }
492355b4669Sjacobs
493*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India if ((pstat == 0x05) ||
494*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India (pstat == 0x06) ||
495*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India (pstat == 0x07) ||
496*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India (pstat == 0x08)) {
497355b4669Sjacobs char *reason = "unknown reason";
498355b4669Sjacobs
499355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
500355b4669Sjacobs "printer-state-reasons", &reason);
501355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
502355b4669Sjacobs "lpsched-disable-reason", &reason);
503355b4669Sjacobs printf(gettext("\t%s\n"), reason);
504355b4669Sjacobs }
505355b4669Sjacobs
506355b4669Sjacobs if (verbose == 1) {
507355b4669Sjacobs void *iter;
508355b4669Sjacobs char *str;
509e9a20b61SKeerthi Kondaka char *host = NULL;
510355b4669Sjacobs
511e9a20b61SKeerthi Kondaka if ((get_remote_hostname(attrs, &host)) != 0) {
512e9a20b61SKeerthi Kondaka (void) printf(
513e9a20b61SKeerthi Kondaka gettext("\tRemote Name: %s\n\tRemote Server: "
514e9a20b61SKeerthi Kondaka "%s\n"), name, host);
515e9a20b61SKeerthi Kondaka free(host);
516e9a20b61SKeerthi Kondaka return (0);
517e9a20b61SKeerthi Kondaka }
518355b4669Sjacobs str = "";
519355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
520355b4669Sjacobs "form-ready", &str);
521355b4669Sjacobs printf(gettext("\tForm mounted: %s\n"), str);
522355b4669Sjacobs
523355b4669Sjacobs str = "";
524355b4669Sjacobs iter = NULL;
525355b4669Sjacobs (void) papiAttributeListGetString(attrs, &iter,
526355b4669Sjacobs "document-format-supported", &str);
527355b4669Sjacobs printf(gettext("\tContent types: %s"), str);
528355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter, NULL, &str)
529355b4669Sjacobs == PAPI_OK)
530355b4669Sjacobs printf(", %s", str);
531355b4669Sjacobs printf("\n");
532355b4669Sjacobs
533f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India /* Display the printer description */
534f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India print_description(attrs, name);
535355b4669Sjacobs
536355b4669Sjacobs str = "";
537853389e3Swendyp iter = NULL;
538853389e3Swendyp (void) papiAttributeListGetString(attrs, &iter,
539853389e3Swendyp "lpsched-printer-type", &str);
540853389e3Swendyp printf(gettext("\tPrinter types: %s"), str);
541853389e3Swendyp while (papiAttributeListGetString(attrs, &iter, NULL, &str)
542853389e3Swendyp == PAPI_OK)
543853389e3Swendyp printf(", %s", str);
544853389e3Swendyp printf("\n");
545853389e3Swendyp
546853389e3Swendyp str = "";
547355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
548355b4669Sjacobs "lpsched-dial-info", &str);
549355b4669Sjacobs printf(gettext("\tConnection: %s\n"),
5505eb9a62cSJonathan Cowper-Andrewes ((str[0] == '\0') ? gettext("direct") : str));
551355b4669Sjacobs
552355b4669Sjacobs str = "";
553355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
554355b4669Sjacobs "lpsched-interface-script", &str);
555355b4669Sjacobs printf(gettext("\tInterface: %s\n"), str);
556355b4669Sjacobs
557355b4669Sjacobs str = NULL;
558355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
559355b4669Sjacobs "ppd-file-uri", &str);
560355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
561355b4669Sjacobs "lpsched-ppd-source-path", &str);
562355b4669Sjacobs if (str != NULL)
563355b4669Sjacobs printf(gettext("\tPPD: %s\n"), str);
564355b4669Sjacobs
565355b4669Sjacobs str = NULL;
566355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
567355b4669Sjacobs "lpsched-fault-alert-command", &str);
568355b4669Sjacobs if (str != NULL)
569355b4669Sjacobs printf(gettext("\tOn fault: %s\n"), str);
570355b4669Sjacobs
571355b4669Sjacobs str = "";
572355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
573355b4669Sjacobs "lpsched-fault-recovery", &str);
574355b4669Sjacobs printf(gettext("\tAfter fault: %s\n"),
575355b4669Sjacobs ((str[0] == '\0') ? gettext("continue") : str));
576355b4669Sjacobs
577355b4669Sjacobs str = "(all)";
578355b4669Sjacobs iter = NULL;
579355b4669Sjacobs (void) papiAttributeListGetString(attrs, &iter,
580355b4669Sjacobs "requesting-user-name-allowed", &str);
581355b4669Sjacobs printf(gettext("\tUsers allowed:\n\t\t%s\n"),
582355b4669Sjacobs ((str[0] == '\0') ? gettext("(none)") : str));
583355b4669Sjacobs if ((str != NULL) && (str[0] != '\0'))
584355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter, NULL,
585355b4669Sjacobs &str) == PAPI_OK)
586355b4669Sjacobs printf("\t\t%s\n", str);
587355b4669Sjacobs
588355b4669Sjacobs str = NULL;
589355b4669Sjacobs iter = NULL;
590355b4669Sjacobs (void) papiAttributeListGetString(attrs, &iter,
591355b4669Sjacobs "requesting-user-name-denied", &str);
592355b4669Sjacobs if (str != NULL) {
593355b4669Sjacobs printf(gettext("\tUsers denied:\n\t\t%s\n"),
594355b4669Sjacobs ((str[0] == '\0') ? gettext("(none)") : str));
595355b4669Sjacobs if ((str != NULL) && (str[0] != '\0'))
596355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter,
597355b4669Sjacobs NULL, &str) == PAPI_OK)
598355b4669Sjacobs printf("\t\t%s\n", str);
599355b4669Sjacobs }
600355b4669Sjacobs
601f06271beSsonam gupta - Sun Microsystems - Bangalore India str = "none";
602355b4669Sjacobs iter = NULL;
603355b4669Sjacobs (void) papiAttributeListGetString(attrs, &iter,
604355b4669Sjacobs "form-supported", &str);
605f06271beSsonam gupta - Sun Microsystems - Bangalore India printf(gettext("\tForms allowed:\n\t\t(%s)\n"),
606f06271beSsonam gupta - Sun Microsystems - Bangalore India ((str[0] == '\0') ? gettext("none") : str));
607355b4669Sjacobs if ((str != NULL) && (str[0] != '\0'))
608355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter, NULL,
609355b4669Sjacobs &str) == PAPI_OK)
610f06271beSsonam gupta - Sun Microsystems - Bangalore India printf("\t\t(%s)\n", str);
611355b4669Sjacobs
612355b4669Sjacobs str = "";
613355b4669Sjacobs iter = NULL;
614355b4669Sjacobs (void) papiAttributeListGetString(attrs, &iter,
615355b4669Sjacobs "media-supported", &str);
616355b4669Sjacobs printf(gettext("\tMedia supported:\n\t\t%s\n"),
617355b4669Sjacobs ((str[0] == '\0') ? gettext("(none)") : str));
618355b4669Sjacobs if ((str != NULL) && (str[0] != '\0'))
619355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter, NULL,
620355b4669Sjacobs &str) == PAPI_OK)
621355b4669Sjacobs printf("\t\t%s\n", str);
622355b4669Sjacobs
623355b4669Sjacobs str = "";
624355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
625355b4669Sjacobs "job-sheets-supported", &str);
626853389e3Swendyp if ((strcasecmp(str, "none")) == 0)
627853389e3Swendyp str = gettext("page never printed");
628853389e3Swendyp else if (strcasecmp(str, "optional") == 0)
629853389e3Swendyp str = gettext("not required");
630853389e3Swendyp else
631853389e3Swendyp str = gettext("required");
632853389e3Swendyp
633853389e3Swendyp printf(gettext("\tBanner %s\n"), str);
634853389e3Swendyp
635355b4669Sjacobs
636355b4669Sjacobs str = "";
637355b4669Sjacobs iter = NULL;
638355b4669Sjacobs (void) papiAttributeListGetString(attrs, &iter,
639355b4669Sjacobs "lpsched-print-wheels", &str);
640355b4669Sjacobs printf(gettext("\tCharacter sets:\n\t\t%s\n"),
641355b4669Sjacobs ((str[0] == '\0') ? gettext("(none)") : str));
642355b4669Sjacobs if ((str != NULL) && (str[0] != '\0'))
643355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter, NULL,
644355b4669Sjacobs &str) == PAPI_OK)
645355b4669Sjacobs printf("\t\t%s\n", str);
646355b4669Sjacobs
647355b4669Sjacobs printf(gettext("\tDefault pitch:\n"));
648355b4669Sjacobs printf(gettext("\tDefault page size:\n"));
649355b4669Sjacobs printf(gettext("\tDefault port setting:\n"));
650355b4669Sjacobs
651355b4669Sjacobs str = "";
652355b4669Sjacobs iter = NULL;
653355b4669Sjacobs (void) papiAttributeListGetString(attrs, &iter,
654355b4669Sjacobs "lpsched-options", &str);
655355b4669Sjacobs if (str != NULL) {
656355b4669Sjacobs printf(gettext("\tOptions: %s"), str);
657355b4669Sjacobs while (papiAttributeListGetString(attrs, &iter, NULL,
658355b4669Sjacobs &str) == PAPI_OK)
659355b4669Sjacobs printf(", %s", str);
660355b4669Sjacobs printf("\n");
661355b4669Sjacobs }
662355b4669Sjacobs
663f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India } else if (description == 1)
664f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India /* Display printer description */
665f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India print_description(attrs, name);
666f7c8cd01Ssonam gupta - Sun Microsystems - Bangalore India else if (verbose > 1)
667355b4669Sjacobs papiAttributeListPrint(stdout, attrs, "\t");
668355b4669Sjacobs
669355b4669Sjacobs if (verbose > 0)
670355b4669Sjacobs printf("\n");
671355b4669Sjacobs
672355b4669Sjacobs return (0);
673355b4669Sjacobs }
674355b4669Sjacobs
675355b4669Sjacobs static int
printer_query(char * name,int (* report)(papi_service_t,char *,papi_printer_t,int,int),papi_encryption_t encryption,int verbose,int description)676355b4669Sjacobs printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t,
677355b4669Sjacobs int, int), papi_encryption_t encryption,
678355b4669Sjacobs int verbose, int description)
679355b4669Sjacobs {
680375b28ffSGowtham Thommandra int result = 0, i = 0;
681355b4669Sjacobs papi_status_t status;
682355b4669Sjacobs papi_service_t svc = NULL;
683375b28ffSGowtham Thommandra char **list = getlist(name, LP_WS, LP_SEP);
684355b4669Sjacobs
685375b28ffSGowtham Thommandra if (list == NULL) {
686375b28ffSGowtham Thommandra list = (char **)malloc(sizeof (char *));
687375b28ffSGowtham Thommandra list[0] = name;
688375b28ffSGowtham Thommandra }
689375b28ffSGowtham Thommandra
690375b28ffSGowtham Thommandra /*
691375b28ffSGowtham Thommandra * The for loop executes once for every printer
692375b28ffSGowtham Thommandra * entry in list. If list is NULL that implies
693375b28ffSGowtham Thommandra * name is also NULL, the loop runs only one time.
694375b28ffSGowtham Thommandra */
695375b28ffSGowtham Thommandra
696375b28ffSGowtham Thommandra for (i = 0; name == NULL || list[i] != NULL; i++) {
697375b28ffSGowtham Thommandra name = list[i];
698375b28ffSGowtham Thommandra
699375b28ffSGowtham Thommandra status = papiServiceCreate(&svc, name, NULL, NULL,
700375b28ffSGowtham Thommandra cli_auth_callback, encryption, NULL);
701355b4669Sjacobs if (status != PAPI_OK) {
702022ba35cSjacobs if (status == PAPI_NOT_FOUND)
703375b28ffSGowtham Thommandra fprintf(stderr,
704375b28ffSGowtham Thommandra gettext("%s: unknown printer\n"),
705022ba35cSjacobs name ? name : "(NULL)");
706022ba35cSjacobs else
707355b4669Sjacobs fprintf(stderr, gettext(
708355b4669Sjacobs "Failed to contact service for %s: %s\n"),
709355b4669Sjacobs name ? name : "(NULL)",
710355b4669Sjacobs verbose_papi_message(svc, status));
711355b4669Sjacobs papiServiceDestroy(svc);
712375b28ffSGowtham Thommandra result--;
713375b28ffSGowtham Thommandra continue;
714355b4669Sjacobs }
715355b4669Sjacobs
716355b4669Sjacobs if (name == NULL) { /* all */
717355b4669Sjacobs char **interest = interest_list(svc);
718355b4669Sjacobs
719355b4669Sjacobs if (interest != NULL) {
720355b4669Sjacobs int i;
721355b4669Sjacobs
722355b4669Sjacobs for (i = 0; interest[i] != NULL; i++)
723375b28ffSGowtham Thommandra result += printer_query(interest[i],
724375b28ffSGowtham Thommandra report, encryption, verbose,
725355b4669Sjacobs description);
726355b4669Sjacobs }
727355b4669Sjacobs } else {
728355b4669Sjacobs papi_printer_t printer = NULL;
729355b4669Sjacobs char **keys = NULL;
730355b4669Sjacobs
731355b4669Sjacobs /*
732375b28ffSGowtham Thommandra * Limit the query to only required data
733375b28ffSGowtham Thommandra * to reduce the need to go remote for
734375b28ffSGowtham Thommandra * information.
735355b4669Sjacobs */
736355b4669Sjacobs if (report == report_device)
737355b4669Sjacobs keys = report_device_keys;
738355b4669Sjacobs else if (report == report_class)
739355b4669Sjacobs keys = report_class_keys;
740355b4669Sjacobs else if (report == report_accepting)
741355b4669Sjacobs keys = report_accepting_keys;
742355b4669Sjacobs else if ((report == report_printer) && (verbose == 0))
743355b4669Sjacobs keys = report_printer_keys;
744355b4669Sjacobs
745375b28ffSGowtham Thommandra status = papiPrinterQuery(svc, name, keys,
746375b28ffSGowtham Thommandra NULL, &printer);
747355b4669Sjacobs if (status != PAPI_OK) {
748355b4669Sjacobs fprintf(stderr, gettext(
749355b4669Sjacobs "Failed to get printer info for %s: %s\n"),
750355b4669Sjacobs name, verbose_papi_message(svc, status));
751355b4669Sjacobs papiServiceDestroy(svc);
752375b28ffSGowtham Thommandra result--;
753375b28ffSGowtham Thommandra continue;
754355b4669Sjacobs }
755355b4669Sjacobs
756355b4669Sjacobs if (printer != NULL)
757375b28ffSGowtham Thommandra result += report(svc, name, printer, verbose,
758355b4669Sjacobs description);
759355b4669Sjacobs
760355b4669Sjacobs papiPrinterFree(printer);
761355b4669Sjacobs }
762355b4669Sjacobs
763355b4669Sjacobs papiServiceDestroy(svc);
764355b4669Sjacobs
765375b28ffSGowtham Thommandra if (name == NULL)
766375b28ffSGowtham Thommandra break;
767375b28ffSGowtham Thommandra }
768375b28ffSGowtham Thommandra
769375b28ffSGowtham Thommandra freelist(list);
770375b28ffSGowtham Thommandra
771355b4669Sjacobs return (result);
772355b4669Sjacobs }
773355b4669Sjacobs
774355b4669Sjacobs static int
match_user(char * user,char ** list)775355b4669Sjacobs match_user(char *user, char **list)
776355b4669Sjacobs {
777355b4669Sjacobs int i;
778355b4669Sjacobs
779355b4669Sjacobs for (i = 0; list[i] != NULL; i++) {
780355b4669Sjacobs if (strcmp(user, list[i]) == 0)
781355b4669Sjacobs return (0);
782355b4669Sjacobs }
783355b4669Sjacobs
784355b4669Sjacobs return (-1);
785355b4669Sjacobs }
786355b4669Sjacobs
787355b4669Sjacobs static char **users = NULL;
788355b4669Sjacobs
789355b4669Sjacobs static int
report_job(char * printer,papi_job_t job,int show_rank,int verbose)79035a603adSsonam gupta - Sun Microsystems - Bangalore India report_job(char *printer, papi_job_t job, int show_rank, int verbose)
791355b4669Sjacobs {
792355b4669Sjacobs papi_attribute_t **attrs = papiJobGetAttributeList(job);
793355b4669Sjacobs time_t clock = 0;
794355b4669Sjacobs char date[24];
795355b4669Sjacobs char request[26];
796355b4669Sjacobs char *user = "unknown";
797b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India char *host = NULL;
798355b4669Sjacobs int32_t size = 0;
799355b4669Sjacobs int32_t jstate = 0;
800b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India char User[50];
801355b4669Sjacobs
802355b4669Sjacobs char *destination = "unknown";
803355b4669Sjacobs int32_t id = -1;
8043d09a4feSsonam gupta - Sun Microsystems - Bangalore India static int check = 0;
8053d09a4feSsonam gupta - Sun Microsystems - Bangalore India static char *uri = NULL;
8063333c089Ssonam gupta - Sun Microsystems - Bangalore India static char *puri = NULL; /* printer-uri */
8073333c089Ssonam gupta - Sun Microsystems - Bangalore India static char *pname = NULL; /* printer-name */
808355b4669Sjacobs
809355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
810355b4669Sjacobs "job-originating-user-name", &user);
811355b4669Sjacobs
812355b4669Sjacobs if ((users != NULL) && (match_user(user, users) < 0))
813355b4669Sjacobs return (0);
814355b4669Sjacobs
815b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India (void) papiAttributeListGetString(attrs, NULL,
816b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India "job-originating-host-name", &host);
817b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India
8183d09a4feSsonam gupta - Sun Microsystems - Bangalore India /*
8193333c089Ssonam gupta - Sun Microsystems - Bangalore India * When lpstat is called for multiple printers
8203333c089Ssonam gupta - Sun Microsystems - Bangalore India * internally the function 'report_job' gets
8213333c089Ssonam gupta - Sun Microsystems - Bangalore India * called multiple times with different printer-names.
8223333c089Ssonam gupta - Sun Microsystems - Bangalore India * The following block of code handles the case when lpstat is
8233333c089Ssonam gupta - Sun Microsystems - Bangalore India * executed for multiple printers. In other words when 'report_job'
8243333c089Ssonam gupta - Sun Microsystems - Bangalore India * is called multiple times for different printers for
8253333c089Ssonam gupta - Sun Microsystems - Bangalore India * one lpstat command
8263333c089Ssonam gupta - Sun Microsystems - Bangalore India * For e.g: lpstat printer1 printer2 printer3
8273333c089Ssonam gupta - Sun Microsystems - Bangalore India */
8283333c089Ssonam gupta - Sun Microsystems - Bangalore India if (pname == NULL) {
8293333c089Ssonam gupta - Sun Microsystems - Bangalore India /*
8303333c089Ssonam gupta - Sun Microsystems - Bangalore India * When lpstat is queried for the first time
8313333c089Ssonam gupta - Sun Microsystems - Bangalore India * pname is NULL so this part of the code gets executed.
8323d09a4feSsonam gupta - Sun Microsystems - Bangalore India * Read the attribute "job-printer-uri"
8333333c089Ssonam gupta - Sun Microsystems - Bangalore India * first time
8343d09a4feSsonam gupta - Sun Microsystems - Bangalore India */
8353d09a4feSsonam gupta - Sun Microsystems - Bangalore India (void) papiAttributeListGetString(attrs, NULL,
8363d09a4feSsonam gupta - Sun Microsystems - Bangalore India "job-printer-uri", &uri);
8373333c089Ssonam gupta - Sun Microsystems - Bangalore India
8383333c089Ssonam gupta - Sun Microsystems - Bangalore India if (printer != NULL) {
8393333c089Ssonam gupta - Sun Microsystems - Bangalore India /*
8403333c089Ssonam gupta - Sun Microsystems - Bangalore India * Set pname to the printer that is being
8413333c089Ssonam gupta - Sun Microsystems - Bangalore India * queried so that this can be used later
8423333c089Ssonam gupta - Sun Microsystems - Bangalore India * if 'report_job' is called multiple times for
8433333c089Ssonam gupta - Sun Microsystems - Bangalore India * different printers for one lpstat command
8443333c089Ssonam gupta - Sun Microsystems - Bangalore India */
8453333c089Ssonam gupta - Sun Microsystems - Bangalore India pname = printer;
8463333c089Ssonam gupta - Sun Microsystems - Bangalore India }
8473333c089Ssonam gupta - Sun Microsystems - Bangalore India
8483333c089Ssonam gupta - Sun Microsystems - Bangalore India if (uri != NULL) {
8493333c089Ssonam gupta - Sun Microsystems - Bangalore India /*
8503333c089Ssonam gupta - Sun Microsystems - Bangalore India * Set puri so that "job-printer-uri" corresponding
8513333c089Ssonam gupta - Sun Microsystems - Bangalore India * to a particular printer can be used later when
8523333c089Ssonam gupta - Sun Microsystems - Bangalore India * lpstat is queried for the same printer as
8533333c089Ssonam gupta - Sun Microsystems - Bangalore India * "job-printer-uri" for a printer is read just once.
8543333c089Ssonam gupta - Sun Microsystems - Bangalore India */
8553333c089Ssonam gupta - Sun Microsystems - Bangalore India puri = strdup(uri);
8563333c089Ssonam gupta - Sun Microsystems - Bangalore India }
8573333c089Ssonam gupta - Sun Microsystems - Bangalore India } else {
8583333c089Ssonam gupta - Sun Microsystems - Bangalore India /*
8593333c089Ssonam gupta - Sun Microsystems - Bangalore India * This part of the code will get executed when
8603333c089Ssonam gupta - Sun Microsystems - Bangalore India * 'report_job' is called more than once for the same
8613333c089Ssonam gupta - Sun Microsystems - Bangalore India * lpstat command
8623333c089Ssonam gupta - Sun Microsystems - Bangalore India */
8633333c089Ssonam gupta - Sun Microsystems - Bangalore India if (printer != NULL) {
8643333c089Ssonam gupta - Sun Microsystems - Bangalore India if (strcasecmp(pname, printer) != 0) {
8653333c089Ssonam gupta - Sun Microsystems - Bangalore India /*
8663333c089Ssonam gupta - Sun Microsystems - Bangalore India * Read the job-printer-uri as
8673333c089Ssonam gupta - Sun Microsystems - Bangalore India * it will be different for
8683333c089Ssonam gupta - Sun Microsystems - Bangalore India * different printers
8693333c089Ssonam gupta - Sun Microsystems - Bangalore India */
8703333c089Ssonam gupta - Sun Microsystems - Bangalore India uri = NULL;
8713333c089Ssonam gupta - Sun Microsystems - Bangalore India (void) papiAttributeListGetString(attrs,
8723333c089Ssonam gupta - Sun Microsystems - Bangalore India NULL, "job-printer-uri", &uri);
8733333c089Ssonam gupta - Sun Microsystems - Bangalore India pname = printer;
8743333c089Ssonam gupta - Sun Microsystems - Bangalore India if (uri != NULL)
8753333c089Ssonam gupta - Sun Microsystems - Bangalore India puri = strdup(uri);
8763333c089Ssonam gupta - Sun Microsystems - Bangalore India else
8773333c089Ssonam gupta - Sun Microsystems - Bangalore India puri = NULL;
8783333c089Ssonam gupta - Sun Microsystems - Bangalore India } else {
8793333c089Ssonam gupta - Sun Microsystems - Bangalore India /*
8803333c089Ssonam gupta - Sun Microsystems - Bangalore India * Same printer queried twice
8813333c089Ssonam gupta - Sun Microsystems - Bangalore India * uri should be the same as
8823333c089Ssonam gupta - Sun Microsystems - Bangalore India * already read in the previous call
8833333c089Ssonam gupta - Sun Microsystems - Bangalore India * to 'report_job'.
8843333c089Ssonam gupta - Sun Microsystems - Bangalore India * For the same printer 'job-printer-uri'
8853333c089Ssonam gupta - Sun Microsystems - Bangalore India * is read just once because only in the
8863333c089Ssonam gupta - Sun Microsystems - Bangalore India * first call it contains the host information
8873333c089Ssonam gupta - Sun Microsystems - Bangalore India */
8883333c089Ssonam gupta - Sun Microsystems - Bangalore India uri = puri;
8893333c089Ssonam gupta - Sun Microsystems - Bangalore India }
8903333c089Ssonam gupta - Sun Microsystems - Bangalore India }
8913d09a4feSsonam gupta - Sun Microsystems - Bangalore India }
8923d09a4feSsonam gupta - Sun Microsystems - Bangalore India
8933d09a4feSsonam gupta - Sun Microsystems - Bangalore India if (host) {
8943d09a4feSsonam gupta - Sun Microsystems - Bangalore India /* Check if it is local printer or remote printer */
8953d09a4feSsonam gupta - Sun Microsystems - Bangalore India uri_t *u = NULL;
8963d09a4feSsonam gupta - Sun Microsystems - Bangalore India
8973d09a4feSsonam gupta - Sun Microsystems - Bangalore India if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
8983d09a4feSsonam gupta - Sun Microsystems - Bangalore India char *nodename = localhostname();
8993d09a4feSsonam gupta - Sun Microsystems - Bangalore India
9003d09a4feSsonam gupta - Sun Microsystems - Bangalore India if ((u->host == NULL) ||
9013d09a4feSsonam gupta - Sun Microsystems - Bangalore India (strcasecmp(u->host, "localhost") == 0) ||
9023d09a4feSsonam gupta - Sun Microsystems - Bangalore India (strcasecmp(u->host, nodename) == 0)) {
9033d09a4feSsonam gupta - Sun Microsystems - Bangalore India
9043d09a4feSsonam gupta - Sun Microsystems - Bangalore India if (strcasecmp(host, nodename) == 0) {
9053d09a4feSsonam gupta - Sun Microsystems - Bangalore India /*
9063d09a4feSsonam gupta - Sun Microsystems - Bangalore India * Request submitted locally
9073d09a4feSsonam gupta - Sun Microsystems - Bangalore India * for the local queue.
9083d09a4feSsonam gupta - Sun Microsystems - Bangalore India * Hostname will not be displayed
9093d09a4feSsonam gupta - Sun Microsystems - Bangalore India */
9103d09a4feSsonam gupta - Sun Microsystems - Bangalore India snprintf(User, sizeof (User), "%s",
9113d09a4feSsonam gupta - Sun Microsystems - Bangalore India user);
9123d09a4feSsonam gupta - Sun Microsystems - Bangalore India }
9133d09a4feSsonam gupta - Sun Microsystems - Bangalore India else
9143d09a4feSsonam gupta - Sun Microsystems - Bangalore India snprintf(User, sizeof (User), "%s@%s",
9153d09a4feSsonam gupta - Sun Microsystems - Bangalore India user, host);
9163d09a4feSsonam gupta - Sun Microsystems - Bangalore India } else if (uri != NULL) {
9173d09a4feSsonam gupta - Sun Microsystems - Bangalore India /*
9183d09a4feSsonam gupta - Sun Microsystems - Bangalore India * It's a remote printer.
9193d09a4feSsonam gupta - Sun Microsystems - Bangalore India * In case of remote printers hostname is
9203d09a4feSsonam gupta - Sun Microsystems - Bangalore India * always displayed.
9213d09a4feSsonam gupta - Sun Microsystems - Bangalore India */
9223d09a4feSsonam gupta - Sun Microsystems - Bangalore India snprintf(User, sizeof (User), "%s@%s",
9233d09a4feSsonam gupta - Sun Microsystems - Bangalore India user, host);
9243d09a4feSsonam gupta - Sun Microsystems - Bangalore India }
9253d09a4feSsonam gupta - Sun Microsystems - Bangalore India uri_free(u);
9263d09a4feSsonam gupta - Sun Microsystems - Bangalore India } else {
9273d09a4feSsonam gupta - Sun Microsystems - Bangalore India /*
9283d09a4feSsonam gupta - Sun Microsystems - Bangalore India * If attribute "job-printer-uri"
9293d09a4feSsonam gupta - Sun Microsystems - Bangalore India * cannot be read
9303d09a4feSsonam gupta - Sun Microsystems - Bangalore India * by default append the hostname
9313d09a4feSsonam gupta - Sun Microsystems - Bangalore India */
932b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India snprintf(User, sizeof (User), "%s@%s", user, host);
9333d09a4feSsonam gupta - Sun Microsystems - Bangalore India }
9343d09a4feSsonam gupta - Sun Microsystems - Bangalore India } else {
9353d09a4feSsonam gupta - Sun Microsystems - Bangalore India /*
9363d09a4feSsonam gupta - Sun Microsystems - Bangalore India * When print server is s10u4 and ipp service is used
9373d09a4feSsonam gupta - Sun Microsystems - Bangalore India * "job-originating-hostname" attribute is not set
9383d09a4feSsonam gupta - Sun Microsystems - Bangalore India * So get the host information from the uri
9393d09a4feSsonam gupta - Sun Microsystems - Bangalore India */
9403d09a4feSsonam gupta - Sun Microsystems - Bangalore India uri_t *u = NULL;
9413d09a4feSsonam gupta - Sun Microsystems - Bangalore India if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) {
9423d09a4feSsonam gupta - Sun Microsystems - Bangalore India if ((u != NULL) && (u->host != NULL))
9433d09a4feSsonam gupta - Sun Microsystems - Bangalore India snprintf(User, sizeof (User), "%s@%s",
9443d09a4feSsonam gupta - Sun Microsystems - Bangalore India user, u->host);
945b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India else
946b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India snprintf(User, sizeof (User), "%s", user);
947b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India
9483d09a4feSsonam gupta - Sun Microsystems - Bangalore India uri_free(u);
9493d09a4feSsonam gupta - Sun Microsystems - Bangalore India } else
9503d09a4feSsonam gupta - Sun Microsystems - Bangalore India snprintf(User, sizeof (User), "%s", user);
9513d09a4feSsonam gupta - Sun Microsystems - Bangalore India }
952355b4669Sjacobs (void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size);
953355b4669Sjacobs size *= 1024; /* for the approximate byte size */
954355b4669Sjacobs (void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size);
955355b4669Sjacobs
956355b4669Sjacobs (void) time(&clock);
957355b4669Sjacobs (void) papiAttributeListGetInteger(attrs, NULL,
958355b4669Sjacobs "time-at-creation", (int32_t *)&clock);
959355b4669Sjacobs (void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock));
960355b4669Sjacobs
961355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
962355b4669Sjacobs "job-printer-uri", &destination);
963355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
964355b4669Sjacobs "printer-name", &destination);
965355b4669Sjacobs (void) papiAttributeListGetInteger(attrs, NULL,
966355b4669Sjacobs "job-id", &id);
96740e7ce05Ssonam gupta - Sun Microsystems - Bangalore India (void) papiAttributeListGetInteger(attrs, NULL,
96840e7ce05Ssonam gupta - Sun Microsystems - Bangalore India "job-id-requested", &id);
96935a603adSsonam gupta - Sun Microsystems - Bangalore India
970f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India
97135a603adSsonam gupta - Sun Microsystems - Bangalore India snprintf(request, sizeof (request), "%s-%d", printer, id);
972355b4669Sjacobs
973355b4669Sjacobs if (show_rank != 0) {
974355b4669Sjacobs int32_t rank = -1;
975355b4669Sjacobs
976355b4669Sjacobs (void) papiAttributeListGetInteger(attrs, NULL,
977355b4669Sjacobs "number-of-intervening-jobs", &rank);
978355b4669Sjacobs rank++;
979355b4669Sjacobs
980355b4669Sjacobs printf("%3d %-21s %-14s %7ld %s",
981b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India rank, request, User, size, date);
982355b4669Sjacobs } else
983b9c04f6bSsonam gupta - Sun Microsystems - Bangalore India printf("%-23s %-14s %7ld %s", request, User, size, date);
984355b4669Sjacobs
985355b4669Sjacobs (void) papiAttributeListGetInteger(attrs, NULL,
986355b4669Sjacobs "job-state", &jstate);
987f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India
988c389c7f8SGowtham Thommandra if (jstate == 0x0001)
989c389c7f8SGowtham Thommandra printf(gettext(" being held"));
990c389c7f8SGowtham Thommandra else if (jstate == 0x0800)
991c389c7f8SGowtham Thommandra printf(gettext(" notifying user"));
992c389c7f8SGowtham Thommandra else if (jstate == 0x0040)
993c389c7f8SGowtham Thommandra printf(gettext(" cancelled"));
994c389c7f8SGowtham Thommandra else if (jstate == 0x0010)
995c389c7f8SGowtham Thommandra printf(gettext(" finished printing"));
996c389c7f8SGowtham Thommandra else if (jstate == 0x0008)
997c389c7f8SGowtham Thommandra printf(gettext(" on %s"), destination);
998c389c7f8SGowtham Thommandra else if (jstate == 0x2000)
999c389c7f8SGowtham Thommandra printf(gettext(" held by admin"));
1000c389c7f8SGowtham Thommandra else if (jstate == 0x0002)
1001c389c7f8SGowtham Thommandra printf(gettext(" being filtered"));
1002c389c7f8SGowtham Thommandra else if (jstate == 0x0004)
1003c389c7f8SGowtham Thommandra printf(gettext(" filtered"));
1004c389c7f8SGowtham Thommandra else if (jstate == 0x0020)
1005c389c7f8SGowtham Thommandra printf(gettext(" held for change"));
1006355b4669Sjacobs
1007355b4669Sjacobs if (verbose == 1) {
10080a44ef6dSjacobs char *form = NULL;
10090a44ef6dSjacobs
1010355b4669Sjacobs (void) papiAttributeListGetString(attrs, NULL,
1011355b4669Sjacobs "output-device-assigned", &destination);
1012355b4669Sjacobs printf("\n\t assigned %s", destination);
10130a44ef6dSjacobs
10140a44ef6dSjacobs (void) papiAttributeListGetString(attrs, NULL, "form", &form);
10150a44ef6dSjacobs if (form != NULL)
10160a44ef6dSjacobs printf(", form %s", form);
1017355b4669Sjacobs } else if (verbose > 1) {
1018355b4669Sjacobs printf("\n");
1019355b4669Sjacobs papiAttributeListPrint(stdout, attrs, "\t");
1020355b4669Sjacobs }
1021355b4669Sjacobs
1022355b4669Sjacobs printf("\n");
1023355b4669Sjacobs
1024355b4669Sjacobs return (0);
1025355b4669Sjacobs }
1026355b4669Sjacobs
1027355b4669Sjacobs static int
job_query(char * request,int (* report)(char *,papi_job_t,int,int),papi_encryption_t encryption,int show_rank,int verbose)102835a603adSsonam gupta - Sun Microsystems - Bangalore India job_query(char *request, int (*report)(char *, papi_job_t, int, int),
1029355b4669Sjacobs papi_encryption_t encryption, int show_rank, int verbose)
1030355b4669Sjacobs {
1031355b4669Sjacobs int result = 0;
1032355b4669Sjacobs papi_status_t status;
1033355b4669Sjacobs papi_service_t svc = NULL;
10344749ecc3Ssonam gupta - Sun Microsystems - Bangalore India char *printer = request;
1035355b4669Sjacobs int32_t id = -1;
10364749ecc3Ssonam gupta - Sun Microsystems - Bangalore India int flag1 = 0;
10374749ecc3Ssonam gupta - Sun Microsystems - Bangalore India int flag = 1;
10384749ecc3Ssonam gupta - Sun Microsystems - Bangalore India int print_flag = 0;
1039355b4669Sjacobs
10404749ecc3Ssonam gupta - Sun Microsystems - Bangalore India do {
10414749ecc3Ssonam gupta - Sun Microsystems - Bangalore India status = papiServiceCreate(&svc, printer, NULL, NULL,
10424749ecc3Ssonam gupta - Sun Microsystems - Bangalore India cli_auth_callback, encryption, NULL);
1043355b4669Sjacobs
10444749ecc3Ssonam gupta - Sun Microsystems - Bangalore India if ((status == PAPI_OK) && (printer != NULL))
10454749ecc3Ssonam gupta - Sun Microsystems - Bangalore India print_flag = 1;
10464749ecc3Ssonam gupta - Sun Microsystems - Bangalore India
10474749ecc3Ssonam gupta - Sun Microsystems - Bangalore India /* <name>-# printer name does not exist */
10484749ecc3Ssonam gupta - Sun Microsystems - Bangalore India if (status != PAPI_OK) {
10494749ecc3Ssonam gupta - Sun Microsystems - Bangalore India /*
10504749ecc3Ssonam gupta - Sun Microsystems - Bangalore India * Check if <name>-# is a request-id
10514749ecc3Ssonam gupta - Sun Microsystems - Bangalore India * Once this check is done flag1 is set
10524749ecc3Ssonam gupta - Sun Microsystems - Bangalore India */
10534749ecc3Ssonam gupta - Sun Microsystems - Bangalore India if (flag1 == 1)
10544749ecc3Ssonam gupta - Sun Microsystems - Bangalore India break;
10554749ecc3Ssonam gupta - Sun Microsystems - Bangalore India
10564749ecc3Ssonam gupta - Sun Microsystems - Bangalore India get_printer_id(printer, &printer, &id);
10574749ecc3Ssonam gupta - Sun Microsystems - Bangalore India
10584749ecc3Ssonam gupta - Sun Microsystems - Bangalore India status = papiServiceCreate(&svc, printer, NULL, NULL,
10594749ecc3Ssonam gupta - Sun Microsystems - Bangalore India cli_auth_callback, encryption, NULL);
10604749ecc3Ssonam gupta - Sun Microsystems - Bangalore India
1061355b4669Sjacobs if (status != PAPI_OK) {
1062355b4669Sjacobs fprintf(stderr, gettext(
1063355b4669Sjacobs "Failed to contact service for %s: %s\n"),
1064355b4669Sjacobs (printer ? printer : "all"),
1065355b4669Sjacobs verbose_papi_message(svc, status));
1066355b4669Sjacobs return (-1);
1067355b4669Sjacobs }
10684749ecc3Ssonam gupta - Sun Microsystems - Bangalore India }
1069355b4669Sjacobs
1070355b4669Sjacobs if (printer == NULL) { /* all */
1071355b4669Sjacobs char **interest = interest_list(svc);
1072355b4669Sjacobs
1073355b4669Sjacobs if (interest != NULL) {
1074355b4669Sjacobs int i;
1075355b4669Sjacobs
1076355b4669Sjacobs for (i = 0; interest[i] != NULL; i++)
1077355b4669Sjacobs result += job_query(interest[i], report,
1078355b4669Sjacobs encryption, show_rank, verbose);
1079355b4669Sjacobs }
1080355b4669Sjacobs } else if (id == -1) { /* a printer */
1081355b4669Sjacobs papi_job_t *jobs = NULL;
1082355b4669Sjacobs
10834749ecc3Ssonam gupta - Sun Microsystems - Bangalore India status = papiPrinterListJobs(svc, printer, NULL,
10844749ecc3Ssonam gupta - Sun Microsystems - Bangalore India 0, 0, &jobs);
1085355b4669Sjacobs if (status != PAPI_OK) {
1086355b4669Sjacobs fprintf(stderr, gettext(
1087355b4669Sjacobs "Failed to get job list: %s\n"),
1088355b4669Sjacobs verbose_papi_message(svc, status));
1089355b4669Sjacobs papiServiceDestroy(svc);
1090355b4669Sjacobs return (-1);
1091355b4669Sjacobs }
1092355b4669Sjacobs
1093355b4669Sjacobs if (jobs != NULL) {
1094355b4669Sjacobs int i;
1095355b4669Sjacobs
1096355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++)
109735a603adSsonam gupta - Sun Microsystems - Bangalore India result += report(printer,
109835a603adSsonam gupta - Sun Microsystems - Bangalore India jobs[i], show_rank,
109935a603adSsonam gupta - Sun Microsystems - Bangalore India verbose);
1100355b4669Sjacobs }
1101355b4669Sjacobs
1102355b4669Sjacobs papiJobListFree(jobs);
1103355b4669Sjacobs } else { /* a job */
1104355b4669Sjacobs papi_job_t job = NULL;
1105355b4669Sjacobs
11064749ecc3Ssonam gupta - Sun Microsystems - Bangalore India /* Once a job has been found stop processing */
11074749ecc3Ssonam gupta - Sun Microsystems - Bangalore India flag = 0;
11084749ecc3Ssonam gupta - Sun Microsystems - Bangalore India
1109f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India /*
1110f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India * Job-id could be the job-id requested
1111f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India * Check if it is job-id or job-id-requested
1112f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India */
1113f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India id = job_to_be_queried(svc, printer, id);
1114f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India
111506f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (id >= 0)
1116375b28ffSGowtham Thommandra status = papiJobQuery(svc, printer, id,
1117375b28ffSGowtham Thommandra NULL, &job);
1118f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India else
111906f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* id not found */
112006f61b77Ssonam gupta - Sun Microsystems - Bangalore India status = PAPI_NOT_FOUND;
1121f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India
1122355b4669Sjacobs if (status != PAPI_OK) {
11234749ecc3Ssonam gupta - Sun Microsystems - Bangalore India if (!print_flag)
1124355b4669Sjacobs fprintf(stderr, gettext(
112554d5ddccSsonam gupta - Sun Microsystems - Bangalore India "Failed to get job"\
112654d5ddccSsonam gupta - Sun Microsystems - Bangalore India " info for %s: %s\n"),
112754d5ddccSsonam gupta - Sun Microsystems - Bangalore India request,
112854d5ddccSsonam gupta - Sun Microsystems - Bangalore India verbose_papi_message(svc, status));
1129355b4669Sjacobs papiServiceDestroy(svc);
1130355b4669Sjacobs return (-1);
1131355b4669Sjacobs }
1132355b4669Sjacobs
1133355b4669Sjacobs if (job != NULL)
113435a603adSsonam gupta - Sun Microsystems - Bangalore India result = report(printer, job,
113535a603adSsonam gupta - Sun Microsystems - Bangalore India show_rank, verbose);
1136355b4669Sjacobs
1137355b4669Sjacobs papiJobFree(job);
1138355b4669Sjacobs }
1139355b4669Sjacobs
11404749ecc3Ssonam gupta - Sun Microsystems - Bangalore India if (flag) {
11414749ecc3Ssonam gupta - Sun Microsystems - Bangalore India id = -1;
11424749ecc3Ssonam gupta - Sun Microsystems - Bangalore India get_printer_id(printer, &printer, &id);
11434749ecc3Ssonam gupta - Sun Microsystems - Bangalore India if (id == -1)
11444749ecc3Ssonam gupta - Sun Microsystems - Bangalore India flag = 0;
11454749ecc3Ssonam gupta - Sun Microsystems - Bangalore India else
11464749ecc3Ssonam gupta - Sun Microsystems - Bangalore India flag1 = 1;
11474749ecc3Ssonam gupta - Sun Microsystems - Bangalore India }
11484749ecc3Ssonam gupta - Sun Microsystems - Bangalore India } while (flag);
11494749ecc3Ssonam gupta - Sun Microsystems - Bangalore India
1150355b4669Sjacobs papiServiceDestroy(svc);
1151355b4669Sjacobs
1152355b4669Sjacobs return (result);
1153355b4669Sjacobs }
1154355b4669Sjacobs
1155355b4669Sjacobs static int
report_form(char * name,papi_attribute_t ** attrs,int verbose)1156355b4669Sjacobs report_form(char *name, papi_attribute_t **attrs, int verbose)
1157355b4669Sjacobs {
1158355b4669Sjacobs papi_status_t status;
1159355b4669Sjacobs char *form = NULL;
1160355b4669Sjacobs void *iter = NULL;
1161355b4669Sjacobs
1162355b4669Sjacobs for (status = papiAttributeListGetString(attrs, &iter,
1163355b4669Sjacobs "form-supported", &form);
1164355b4669Sjacobs status == PAPI_OK;
1165355b4669Sjacobs status = papiAttributeListGetString(attrs, &iter,
1166355b4669Sjacobs NULL, &form)) {
1167355b4669Sjacobs if ((name == NULL) || (strcmp(name, form) == 0)) {
1168355b4669Sjacobs printf(gettext("form %s is available to you\n"), form);
1169355b4669Sjacobs if (verbose != 0) {
1170355b4669Sjacobs char *detail = NULL;
1171355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
117254d5ddccSsonam gupta - Sun Microsystems - Bangalore India "form-supported-detail", &detail);
1173355b4669Sjacobs if (status == PAPI_OK)
1174355b4669Sjacobs printf("%s\n", detail);
1175355b4669Sjacobs }
1176355b4669Sjacobs }
1177355b4669Sjacobs }
1178355b4669Sjacobs
1179355b4669Sjacobs return (0);
1180355b4669Sjacobs }
1181355b4669Sjacobs
1182355b4669Sjacobs static int
report_print_wheels(char * name,papi_attribute_t ** attrs,int verbose)1183355b4669Sjacobs report_print_wheels(char *name, papi_attribute_t **attrs, int verbose)
1184355b4669Sjacobs {
1185355b4669Sjacobs papi_status_t status;
1186355b4669Sjacobs char *pw = NULL;
1187355b4669Sjacobs void *iter = NULL;
1188355b4669Sjacobs
1189355b4669Sjacobs for (status = papiAttributeListGetString(attrs, &iter,
1190355b4669Sjacobs "pw-supported", &pw);
1191355b4669Sjacobs status == PAPI_OK;
1192355b4669Sjacobs status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) {
1193355b4669Sjacobs if ((name == NULL) || (strcmp(name, pw) == 0)) {
1194355b4669Sjacobs printf(gettext("charset %s is available\n"), pw);
1195355b4669Sjacobs if (verbose != 0) {
1196355b4669Sjacobs char *info = NULL;
1197355b4669Sjacobs status = papiAttributeListGetString(attrs, NULL,
1198355b4669Sjacobs "pw-supported-extra", &info);
1199355b4669Sjacobs if (status == PAPI_OK)
1200355b4669Sjacobs printf("%s\n", info);
1201355b4669Sjacobs }
1202355b4669Sjacobs }
1203355b4669Sjacobs }
1204355b4669Sjacobs
1205355b4669Sjacobs return (0);
1206355b4669Sjacobs }
1207355b4669Sjacobs
1208355b4669Sjacobs static int
service_query(char * name,int (* report)(char *,papi_attribute_t **,int),papi_encryption_t encryption,int verbose)1209355b4669Sjacobs service_query(char *name, int (*report)(char *, papi_attribute_t **, int),
1210355b4669Sjacobs papi_encryption_t encryption, int verbose)
1211355b4669Sjacobs {
1212355b4669Sjacobs int result = 0;
1213355b4669Sjacobs papi_status_t status;
1214355b4669Sjacobs papi_service_t svc = NULL;
1215355b4669Sjacobs papi_attribute_t **attrs = NULL;
1216355b4669Sjacobs
1217355b4669Sjacobs status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback,
1218355b4669Sjacobs encryption, NULL);
1219355b4669Sjacobs if (status != PAPI_OK) {
1220355b4669Sjacobs papiServiceDestroy(svc);
1221355b4669Sjacobs return (-1);
1222355b4669Sjacobs }
1223355b4669Sjacobs
1224355b4669Sjacobs attrs = papiServiceGetAttributeList(svc);
1225355b4669Sjacobs if (attrs != NULL) {
1226355b4669Sjacobs result = report(name, attrs, verbose);
1227355b4669Sjacobs
1228355b4669Sjacobs if (verbose > 1) {
1229355b4669Sjacobs printf("\n");
1230355b4669Sjacobs papiAttributeListPrint(stdout, attrs, "\t");
1231355b4669Sjacobs printf("\n");
1232355b4669Sjacobs }
1233355b4669Sjacobs }
1234355b4669Sjacobs
1235355b4669Sjacobs papiServiceDestroy(svc);
1236355b4669Sjacobs
1237355b4669Sjacobs return (result);
1238355b4669Sjacobs }
1239355b4669Sjacobs
1240355b4669Sjacobs int
main(int ac,char * av[])1241355b4669Sjacobs main(int ac, char *av[])
1242355b4669Sjacobs {
1243355b4669Sjacobs int exit_code = 0;
1244355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
1245355b4669Sjacobs int rank = 0;
1246355b4669Sjacobs int verbose = 0;
1247355b4669Sjacobs int description = 0;
1248355b4669Sjacobs int c;
1249355b4669Sjacobs char **argv;
1250355b4669Sjacobs
1251355b4669Sjacobs (void) setlocale(LC_ALL, "");
1252355b4669Sjacobs (void) textdomain("SUNW_OST_OSCMD");
1253355b4669Sjacobs
1254355b4669Sjacobs argv = (char **)calloc((ac + 1), sizeof (char *));
125591216fe4Swendyp for (c = 0; c < ac; c++) {
125691216fe4Swendyp if ((av[c][0] == '-') && (av[c][1] == 'l') &&
125791216fe4Swendyp (isalpha(av[c][2]) != 0)) {
125891216fe4Swendyp /* preserve old "-l[po...]" behavior */
125991216fe4Swendyp argv[c] = &av[c][1];
126091216fe4Swendyp argv[c][0] = '-';
126191216fe4Swendyp verbose = 1;
126291216fe4Swendyp
126391216fe4Swendyp } else
1264355b4669Sjacobs argv[c] = av[c];
126591216fe4Swendyp }
126691216fe4Swendyp
1267355b4669Sjacobs argv[c++] = "--";
1268355b4669Sjacobs ac = c;
1269355b4669Sjacobs
1270355b4669Sjacobs /* preprocess argument list looking for '-l' or '-R' so it can trail */
12715abc75a2SKeerthi Kondaka while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
12725abc75a2SKeerthi Kondaka switch (c) { /* these may or may not have an option */
12735abc75a2SKeerthi Kondaka case 'a':
12745abc75a2SKeerthi Kondaka case 'c':
12755abc75a2SKeerthi Kondaka case 'p':
12765abc75a2SKeerthi Kondaka case 'o':
12775abc75a2SKeerthi Kondaka case 'R':
12785abc75a2SKeerthi Kondaka case 'u':
12795abc75a2SKeerthi Kondaka case 'v':
12805abc75a2SKeerthi Kondaka case 'l':
12815abc75a2SKeerthi Kondaka case 'f':
12825abc75a2SKeerthi Kondaka case 'S':
12835abc75a2SKeerthi Kondaka if (optarg[0] == '-') {
12845abc75a2SKeerthi Kondaka /* this check stop a possible infinite loop */
12855abc75a2SKeerthi Kondaka if ((optind > 1) && (argv[optind-1][1] != c))
12865abc75a2SKeerthi Kondaka optind--;
12875abc75a2SKeerthi Kondaka optarg = NULL;
12885abc75a2SKeerthi Kondaka } else if (strcmp(optarg, "all") == 0)
12895abc75a2SKeerthi Kondaka optarg = NULL;
12905abc75a2SKeerthi Kondaka }
12915abc75a2SKeerthi Kondaka
1292355b4669Sjacobs switch (c) {
1293355b4669Sjacobs case 'l':
1294355b4669Sjacobs if ((optarg == NULL) || (optarg[0] == '-'))
1295355b4669Sjacobs optarg = "1";
1296355b4669Sjacobs verbose = atoi(optarg);
1297355b4669Sjacobs break;
1298355b4669Sjacobs case 'D':
1299355b4669Sjacobs description = 1;
1300355b4669Sjacobs break;
1301355b4669Sjacobs case 'R':
1302355b4669Sjacobs rank = 1;
1303355b4669Sjacobs break;
1304355b4669Sjacobs case 'E':
1305355b4669Sjacobs encryption = PAPI_ENCRYPT_REQUIRED;
1306355b4669Sjacobs break;
1307355b4669Sjacobs default:
1308355b4669Sjacobs break;
1309355b4669Sjacobs }
13105abc75a2SKeerthi Kondaka }
1311355b4669Sjacobs optind = 1;
1312355b4669Sjacobs
1313355b4669Sjacobs /* process command line arguments */
1314355b4669Sjacobs while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) {
1315355b4669Sjacobs switch (c) { /* these may or may not have an option */
1316355b4669Sjacobs case 'a':
1317355b4669Sjacobs case 'c':
1318355b4669Sjacobs case 'p':
1319355b4669Sjacobs case 'o':
1320355b4669Sjacobs case 'R':
1321355b4669Sjacobs case 'u':
1322355b4669Sjacobs case 'v':
1323355b4669Sjacobs case 'l':
1324355b4669Sjacobs case 'f':
1325355b4669Sjacobs case 'S':
1326355b4669Sjacobs if (optarg[0] == '-') {
1327355b4669Sjacobs /* this check stop a possible infinite loop */
1328355b4669Sjacobs if ((optind > 1) && (argv[optind-1][1] != c))
1329355b4669Sjacobs optind--;
1330355b4669Sjacobs optarg = NULL;
1331355b4669Sjacobs } else if (strcmp(optarg, "all") == 0)
1332355b4669Sjacobs optarg = NULL;
1333355b4669Sjacobs }
1334355b4669Sjacobs
1335355b4669Sjacobs switch (c) {
1336355b4669Sjacobs case 'a':
1337355b4669Sjacobs exit_code += printer_query(optarg, report_accepting,
1338355b4669Sjacobs encryption, verbose, 0);
1339355b4669Sjacobs break;
1340355b4669Sjacobs case 'c':
1341355b4669Sjacobs exit_code += printer_query(optarg, report_class,
1342355b4669Sjacobs encryption, verbose, 0);
1343355b4669Sjacobs break;
1344355b4669Sjacobs case 'p':
1345355b4669Sjacobs exit_code += printer_query(optarg, report_printer,
134654d5ddccSsonam gupta - Sun Microsystems - Bangalore India encryption, verbose, description);
1347355b4669Sjacobs break;
1348355b4669Sjacobs case 'd':
1349355b4669Sjacobs exit_code += lpstat_default_printer(encryption);
1350355b4669Sjacobs break;
1351355b4669Sjacobs case 'r':
1352355b4669Sjacobs exit_code += lpstat_service_status(encryption);
1353355b4669Sjacobs break;
1354355b4669Sjacobs case 'u':
1355355b4669Sjacobs if (optarg != NULL)
1356355b4669Sjacobs users = strsplit(optarg, ", \n");
1357355b4669Sjacobs exit_code += job_query(NULL, report_job,
1358355b4669Sjacobs encryption, rank, verbose);
1359355b4669Sjacobs if (users != NULL) {
1360355b4669Sjacobs free(users);
1361355b4669Sjacobs users = NULL;
1362355b4669Sjacobs }
1363355b4669Sjacobs break;
1364355b4669Sjacobs case 'v':
1365355b4669Sjacobs exit_code += printer_query(optarg, report_device,
1366355b4669Sjacobs encryption, verbose, 0);
1367355b4669Sjacobs break;
1368c2765d20SGowtham Thommandra case 'R': /* set "rank" flag in first pass */
1369355b4669Sjacobs case 'o':
1370355b4669Sjacobs exit_code += job_query(optarg, report_job,
1371355b4669Sjacobs encryption, rank, verbose);
1372355b4669Sjacobs break;
1373355b4669Sjacobs case 'f':
1374355b4669Sjacobs exit_code += service_query(optarg, report_form,
1375355b4669Sjacobs encryption, verbose);
1376355b4669Sjacobs break;
1377355b4669Sjacobs case 'S':
1378355b4669Sjacobs exit_code += service_query(optarg, report_print_wheels,
1379355b4669Sjacobs encryption, verbose);
1380355b4669Sjacobs break;
1381355b4669Sjacobs case 's':
1382355b4669Sjacobs exit_code += lpstat_service_status(encryption);
1383355b4669Sjacobs exit_code += lpstat_default_printer(encryption);
1384355b4669Sjacobs exit_code += printer_query(NULL, report_class,
1385355b4669Sjacobs encryption, verbose, 0);
1386355b4669Sjacobs exit_code += printer_query(NULL, report_device,
1387355b4669Sjacobs encryption, verbose, 0);
1388355b4669Sjacobs exit_code += service_query(optarg, report_form,
1389355b4669Sjacobs encryption, verbose);
1390355b4669Sjacobs exit_code += service_query(optarg, report_print_wheels,
1391355b4669Sjacobs encryption, verbose);
1392355b4669Sjacobs break;
1393355b4669Sjacobs case 't':
1394355b4669Sjacobs exit_code += lpstat_service_status(encryption);
1395355b4669Sjacobs exit_code += lpstat_default_printer(encryption);
1396355b4669Sjacobs exit_code += printer_query(NULL, report_class,
1397355b4669Sjacobs encryption, verbose, 0);
1398355b4669Sjacobs exit_code += printer_query(NULL, report_device,
1399355b4669Sjacobs encryption, verbose, 0);
1400355b4669Sjacobs exit_code += printer_query(NULL, report_accepting,
1401355b4669Sjacobs encryption, verbose, 0);
1402355b4669Sjacobs exit_code += printer_query(NULL, report_printer,
1403355b4669Sjacobs encryption, verbose, 0);
1404355b4669Sjacobs exit_code += service_query(optarg, report_form,
1405355b4669Sjacobs encryption, verbose);
1406355b4669Sjacobs exit_code += service_query(optarg, report_print_wheels,
1407355b4669Sjacobs encryption, verbose);
1408355b4669Sjacobs exit_code += job_query(NULL, report_job,
1409355b4669Sjacobs encryption, rank, verbose);
1410355b4669Sjacobs break;
1411355b4669Sjacobs case 'L': /* local-only, ignored */
1412355b4669Sjacobs case 'l': /* increased verbose level in first pass */
1413355b4669Sjacobs case 'D': /* set "description" flag in first pass */
1414355b4669Sjacobs case 'E': /* set encryption in the first pass */
1415355b4669Sjacobs break;
1416355b4669Sjacobs default:
1417355b4669Sjacobs usage(av[0]);
1418355b4669Sjacobs }
1419355b4669Sjacobs }
1420355b4669Sjacobs ac--;
1421355b4669Sjacobs
1422355b4669Sjacobs if (ac == 1) { /* report on my jobs */
1423355b4669Sjacobs struct passwd *pw = getpwuid(getuid());
1424355b4669Sjacobs
1425355b4669Sjacobs if (pw != NULL)
1426355b4669Sjacobs users = strsplit(pw->pw_name, "");
1427355b4669Sjacobs exit_code += job_query(NULL, report_job, encryption,
1428355b4669Sjacobs rank, verbose);
1429355b4669Sjacobs if (users != NULL) {
1430355b4669Sjacobs free(users);
1431355b4669Sjacobs users = NULL;
1432355b4669Sjacobs }
1433355b4669Sjacobs } else {
1434355b4669Sjacobs for (c = optind; c < ac; c++)
1435355b4669Sjacobs exit_code += job_query(argv[c], report_job, encryption,
1436355b4669Sjacobs rank, verbose);
1437355b4669Sjacobs }
1438355b4669Sjacobs
1439355b4669Sjacobs
1440355b4669Sjacobs if (exit_code != 0)
1441355b4669Sjacobs exit_code = 1;
1442355b4669Sjacobs
1443355b4669Sjacobs return (exit_code);
1444355b4669Sjacobs }
1445