xref: /titanic_44/usr/src/cmd/print/bsd-sysv-commands/lpc.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs 
22*355b4669Sjacobs /*
23*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*355b4669Sjacobs  * Use is subject to license terms.
25*355b4669Sjacobs  *
26*355b4669Sjacobs  */
27*355b4669Sjacobs 
28*355b4669Sjacobs /* $Id: lpc.c 146 2006-03-24 00:26:54Z njacobs $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*355b4669Sjacobs 
32*355b4669Sjacobs #include <stdio.h>
33*355b4669Sjacobs #include <stdlib.h>
34*355b4669Sjacobs #include <unistd.h>
35*355b4669Sjacobs #include <string.h>
36*355b4669Sjacobs #include <locale.h>
37*355b4669Sjacobs #include <libintl.h>
38*355b4669Sjacobs #include <papi.h>
39*355b4669Sjacobs #include "common.h"
40*355b4669Sjacobs 
41*355b4669Sjacobs typedef int (cmd_handler_t)(papi_service_t, char **);
42*355b4669Sjacobs 
43*355b4669Sjacobs static papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
44*355b4669Sjacobs 
45*355b4669Sjacobs /* ARGSUSED0 */
46*355b4669Sjacobs static int
47*355b4669Sjacobs lpc_exit(papi_service_t svc, char **args)
48*355b4669Sjacobs {
49*355b4669Sjacobs 	exit(0);
50*355b4669Sjacobs 	/* NOTREACHED */
51*355b4669Sjacobs 	return (0);
52*355b4669Sjacobs }
53*355b4669Sjacobs 
54*355b4669Sjacobs static int
55*355b4669Sjacobs lpc_status(papi_service_t svc, char **args)
56*355b4669Sjacobs {
57*355b4669Sjacobs 	papi_status_t status;
58*355b4669Sjacobs 	papi_printer_t p = NULL;
59*355b4669Sjacobs 	char *pattrs[] = { "printer-state", "printer-state-reasons",
60*355b4669Sjacobs 				"printer-is-accepting-jobs", NULL };
61*355b4669Sjacobs 	char *destination = args[1];
62*355b4669Sjacobs 
63*355b4669Sjacobs 	status = papiPrinterQuery(svc, destination, pattrs, NULL, &p);
64*355b4669Sjacobs 	if (status == PAPI_OK) {
65*355b4669Sjacobs 		papi_attribute_t **list = papiPrinterGetAttributeList(p);
66*355b4669Sjacobs 		char accepting = 0;
67*355b4669Sjacobs 		int32_t state = 0;
68*355b4669Sjacobs 
69*355b4669Sjacobs 		printf("%s:\n", destination);
70*355b4669Sjacobs 
71*355b4669Sjacobs 		(void) papiAttributeListGetBoolean(list, NULL,
72*355b4669Sjacobs 				"printer-is-accepting-jobs", &accepting);
73*355b4669Sjacobs 		printf(gettext("\tqueueing is %s\n"),
74*355b4669Sjacobs 			(accepting ? gettext("enabled") : gettext("disabled")));
75*355b4669Sjacobs 
76*355b4669Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
77*355b4669Sjacobs 					"printer-state", &state);
78*355b4669Sjacobs 		printf("\tprinting is %s\n",
79*355b4669Sjacobs 			((state != 0x05) ? gettext("enabled") :
80*355b4669Sjacobs 				gettext("disabled")));
81*355b4669Sjacobs 
82*355b4669Sjacobs 		if (state != 0x03) {	/* !idle */
83*355b4669Sjacobs 			papi_job_t *jobs = NULL;
84*355b4669Sjacobs 			int i = 0;
85*355b4669Sjacobs 
86*355b4669Sjacobs 			(void) papiPrinterListJobs(svc, destination, NULL,
87*355b4669Sjacobs 					PAPI_LIST_JOBS_ALL, 0, &jobs);
88*355b4669Sjacobs 			if (jobs != NULL) {
89*355b4669Sjacobs 				for (i = 0; jobs[i] != NULL; i++);
90*355b4669Sjacobs 				papiJobListFree(jobs);
91*355b4669Sjacobs 			}
92*355b4669Sjacobs 			printf(gettext("\t%d entries in spool area\n"), i);
93*355b4669Sjacobs 		} else
94*355b4669Sjacobs 			printf(gettext("\tno entries\n"));
95*355b4669Sjacobs 
96*355b4669Sjacobs 		if (state == 0x04)
97*355b4669Sjacobs 			printf(gettext("\tdaemon present\n"));
98*355b4669Sjacobs 
99*355b4669Sjacobs 	} else {
100*355b4669Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
101*355b4669Sjacobs 			verbose_papi_message(svc, status));
102*355b4669Sjacobs 		return (-1);
103*355b4669Sjacobs 	}
104*355b4669Sjacobs 
105*355b4669Sjacobs 	papiPrinterFree(p);
106*355b4669Sjacobs 
107*355b4669Sjacobs 	return (0);
108*355b4669Sjacobs }
109*355b4669Sjacobs 
110*355b4669Sjacobs static int
111*355b4669Sjacobs lpc_abort(papi_service_t svc, char **args)
112*355b4669Sjacobs {
113*355b4669Sjacobs 	papi_status_t status;
114*355b4669Sjacobs 	char *destination = args[1];
115*355b4669Sjacobs 
116*355b4669Sjacobs 	if (destination == NULL) {
117*355b4669Sjacobs 		fprintf(stderr, gettext("Usage: abort (destination)\n"));
118*355b4669Sjacobs 		return (-1);
119*355b4669Sjacobs 	}
120*355b4669Sjacobs 
121*355b4669Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc abort");
122*355b4669Sjacobs 	if (status == PAPI_OK) {
123*355b4669Sjacobs 		printf(gettext("%s: processing disabled after current job\n"),
124*355b4669Sjacobs 			destination);
125*355b4669Sjacobs 	} else {
126*355b4669Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
127*355b4669Sjacobs 			verbose_papi_message(svc, status));
128*355b4669Sjacobs 	}
129*355b4669Sjacobs 
130*355b4669Sjacobs 	return (0);
131*355b4669Sjacobs }
132*355b4669Sjacobs 
133*355b4669Sjacobs static int
134*355b4669Sjacobs lpc_clean(papi_service_t svc, char **args)
135*355b4669Sjacobs {
136*355b4669Sjacobs 	papi_status_t status;
137*355b4669Sjacobs 	papi_job_t *jobs = NULL;
138*355b4669Sjacobs 	char *destination = args[1];
139*355b4669Sjacobs 
140*355b4669Sjacobs 	if (destination == NULL) {
141*355b4669Sjacobs 		fprintf(stderr, gettext("Usage: clean (destination)\n"));
142*355b4669Sjacobs 		return (-1);
143*355b4669Sjacobs 	}
144*355b4669Sjacobs 
145*355b4669Sjacobs 	status = papiPrinterPurgeJobs(svc, destination, &jobs);
146*355b4669Sjacobs 	if (status != PAPI_OK) {
147*355b4669Sjacobs 		fprintf(stderr, gettext("clean: %s: %s\n"), destination,
148*355b4669Sjacobs 			verbose_papi_message(svc, status));
149*355b4669Sjacobs 		return (-1);
150*355b4669Sjacobs 	}
151*355b4669Sjacobs 
152*355b4669Sjacobs 	if (jobs != NULL) {
153*355b4669Sjacobs 		int i;
154*355b4669Sjacobs 
155*355b4669Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
156*355b4669Sjacobs 			printf(gettext("\t%s-%d: cancelled\n"), destination,
157*355b4669Sjacobs 				papiJobGetId(jobs[i]));
158*355b4669Sjacobs 
159*355b4669Sjacobs 		papiJobListFree(jobs);
160*355b4669Sjacobs 	}
161*355b4669Sjacobs 
162*355b4669Sjacobs 	return (0);
163*355b4669Sjacobs }
164*355b4669Sjacobs 
165*355b4669Sjacobs static int
166*355b4669Sjacobs lpc_disable(papi_service_t svc, char **args)
167*355b4669Sjacobs {
168*355b4669Sjacobs 	papi_status_t status;
169*355b4669Sjacobs 	char *destination = args[1];
170*355b4669Sjacobs 
171*355b4669Sjacobs 	if (destination == NULL) {
172*355b4669Sjacobs 		fprintf(stderr, gettext("Usage: disable: (destination)\n"));
173*355b4669Sjacobs 		return (-1);
174*355b4669Sjacobs 	}
175*355b4669Sjacobs 
176*355b4669Sjacobs 	status = papiPrinterDisable(svc, destination, NULL);
177*355b4669Sjacobs 	if (status != PAPI_OK) {
178*355b4669Sjacobs 		fprintf(stderr, gettext("disable: %s: %s\n"), destination,
179*355b4669Sjacobs 			verbose_papi_message(svc, status));
180*355b4669Sjacobs 		return (-1);
181*355b4669Sjacobs 	}
182*355b4669Sjacobs 
183*355b4669Sjacobs 	return (0);
184*355b4669Sjacobs }
185*355b4669Sjacobs 
186*355b4669Sjacobs static int
187*355b4669Sjacobs lpc_enable(papi_service_t svc, char **args)
188*355b4669Sjacobs {
189*355b4669Sjacobs 	papi_status_t status;
190*355b4669Sjacobs 	char *destination = args[1];
191*355b4669Sjacobs 
192*355b4669Sjacobs 	if (destination == NULL) {
193*355b4669Sjacobs 		fprintf(stderr, gettext("Usage: enable: (destination)\n"));
194*355b4669Sjacobs 		return (-1);
195*355b4669Sjacobs 	}
196*355b4669Sjacobs 
197*355b4669Sjacobs 	status = papiPrinterEnable(svc, destination);
198*355b4669Sjacobs 	if (status != PAPI_OK) {
199*355b4669Sjacobs 		fprintf(stderr, gettext("enable: %s: %s\n"), destination,
200*355b4669Sjacobs 			verbose_papi_message(svc, status));
201*355b4669Sjacobs 		return (-1);
202*355b4669Sjacobs 	}
203*355b4669Sjacobs 
204*355b4669Sjacobs 	return (0);
205*355b4669Sjacobs }
206*355b4669Sjacobs 
207*355b4669Sjacobs static int
208*355b4669Sjacobs lpc_restart(papi_service_t svc, char **args)
209*355b4669Sjacobs {
210*355b4669Sjacobs 	int rc = 0;
211*355b4669Sjacobs 
212*355b4669Sjacobs 	rc += lpc_disable(svc, args);
213*355b4669Sjacobs 	rc += lpc_enable(svc, args);
214*355b4669Sjacobs 
215*355b4669Sjacobs 	return (rc);
216*355b4669Sjacobs }
217*355b4669Sjacobs 
218*355b4669Sjacobs static int
219*355b4669Sjacobs lpc_start(papi_service_t svc, char **args)
220*355b4669Sjacobs {
221*355b4669Sjacobs 	papi_status_t status;
222*355b4669Sjacobs 	char *destination = args[1];
223*355b4669Sjacobs 
224*355b4669Sjacobs 	if (destination == NULL) {
225*355b4669Sjacobs 		fprintf(stderr, gettext("Usage: start (destination)\n"));
226*355b4669Sjacobs 		return (-1);
227*355b4669Sjacobs 	}
228*355b4669Sjacobs 
229*355b4669Sjacobs 	status = papiPrinterResume(svc, destination);
230*355b4669Sjacobs 	if (status != PAPI_OK) {
231*355b4669Sjacobs 		fprintf(stderr, gettext("start: %s: %s\n"), destination,
232*355b4669Sjacobs 			verbose_papi_message(svc, status));
233*355b4669Sjacobs 		return (-1);
234*355b4669Sjacobs 	}
235*355b4669Sjacobs 
236*355b4669Sjacobs 	return (0);
237*355b4669Sjacobs }
238*355b4669Sjacobs 
239*355b4669Sjacobs static int
240*355b4669Sjacobs lpc_stop(papi_service_t svc, char **args)
241*355b4669Sjacobs {
242*355b4669Sjacobs 	papi_status_t status;
243*355b4669Sjacobs 	char *destination = args[1];
244*355b4669Sjacobs 
245*355b4669Sjacobs 	if (destination == NULL) {
246*355b4669Sjacobs 		fprintf(stderr, gettext("Usage: stop (destination)\n"));
247*355b4669Sjacobs 		return (-1);
248*355b4669Sjacobs 	}
249*355b4669Sjacobs 
250*355b4669Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc");
251*355b4669Sjacobs 	if (status != PAPI_OK) {
252*355b4669Sjacobs 		fprintf(stderr, gettext("stop: %s: %s\n"), destination,
253*355b4669Sjacobs 			verbose_papi_message(svc, status));
254*355b4669Sjacobs 		return (-1);
255*355b4669Sjacobs 	}
256*355b4669Sjacobs 
257*355b4669Sjacobs 	return (0);
258*355b4669Sjacobs }
259*355b4669Sjacobs 
260*355b4669Sjacobs static int
261*355b4669Sjacobs lpc_topq(papi_service_t svc, char **args)
262*355b4669Sjacobs {
263*355b4669Sjacobs 	papi_status_t status;
264*355b4669Sjacobs 	char *destination = args[1];
265*355b4669Sjacobs 	int32_t id = atoi(args[2]);
266*355b4669Sjacobs 
267*355b4669Sjacobs 	if (destination == NULL) {
268*355b4669Sjacobs 		fprintf(stderr, gettext("Usage: topq (destination) (id)\n"));
269*355b4669Sjacobs 		return (-1);
270*355b4669Sjacobs 	}
271*355b4669Sjacobs 
272*355b4669Sjacobs 	status = papiJobPromote(svc, destination, id);
273*355b4669Sjacobs 	if (status != PAPI_OK) {
274*355b4669Sjacobs 		fprintf(stderr, gettext("topq: %s %d: %s\n"), destination, id,
275*355b4669Sjacobs 			verbose_papi_message(svc, status));
276*355b4669Sjacobs 		return (-1);
277*355b4669Sjacobs 	}
278*355b4669Sjacobs 
279*355b4669Sjacobs 	return (0);
280*355b4669Sjacobs }
281*355b4669Sjacobs 
282*355b4669Sjacobs static int
283*355b4669Sjacobs lpc_up(papi_service_t svc, char **args)
284*355b4669Sjacobs {
285*355b4669Sjacobs 	int rc = 0;
286*355b4669Sjacobs 
287*355b4669Sjacobs 	rc += lpc_enable(svc, args);
288*355b4669Sjacobs 	rc += lpc_start(svc, args);
289*355b4669Sjacobs 
290*355b4669Sjacobs 	return (rc);
291*355b4669Sjacobs }
292*355b4669Sjacobs 
293*355b4669Sjacobs static int
294*355b4669Sjacobs lpc_down(papi_service_t svc, char **args)
295*355b4669Sjacobs {
296*355b4669Sjacobs 	int rc = 0;
297*355b4669Sjacobs 
298*355b4669Sjacobs 	rc += lpc_disable(svc, args);
299*355b4669Sjacobs 	rc += lpc_stop(svc, args);
300*355b4669Sjacobs 
301*355b4669Sjacobs 	return (rc);
302*355b4669Sjacobs }
303*355b4669Sjacobs 
304*355b4669Sjacobs static int lpc_help(papi_service_t svc, char **args);	/* forward reference */
305*355b4669Sjacobs 
306*355b4669Sjacobs static char help_help[] = "get help on commands";
307*355b4669Sjacobs static char help_exit[] = "exit lpc";
308*355b4669Sjacobs static char help_status[] = "show status of daemon and queue";
309*355b4669Sjacobs static char help_abort[] =
310*355b4669Sjacobs 		"disable print queue terminating any active job processing";
311*355b4669Sjacobs static char help_clean[] = "remove all jobs from a queue";
312*355b4669Sjacobs static char help_disable[] = "turn off spooling to a queue";
313*355b4669Sjacobs static char help_down[] =
314*355b4669Sjacobs 		"turn off queueing and printing for a queue and set a reason";
315*355b4669Sjacobs static char help_enable[] = "turn on spooling to a queue";
316*355b4669Sjacobs static char help_restart[] = "restart job processing for a queue";
317*355b4669Sjacobs static char help_start[] = "turn on printing from a queue";
318*355b4669Sjacobs static char help_stop[] = "turn off printing from a queue";
319*355b4669Sjacobs static char help_up[] = "turn on queueing and printing for a queue";
320*355b4669Sjacobs static char help_topq[] = "put a job at the top of the queue";
321*355b4669Sjacobs 
322*355b4669Sjacobs static struct {
323*355b4669Sjacobs 	char *cmd;
324*355b4669Sjacobs 	int (*handler)(papi_service_t svc, char **args);
325*355b4669Sjacobs 	char *help_string;
326*355b4669Sjacobs 	int num_args;
327*355b4669Sjacobs } cmd_tab[] = {
328*355b4669Sjacobs 	{ "?",		lpc_help,	help_help,	0 },
329*355b4669Sjacobs 	{ "help",	lpc_help,	help_help,	0 },
330*355b4669Sjacobs 	{ "exit",	lpc_exit,	help_exit,	0 },
331*355b4669Sjacobs 	{ "quit",	lpc_exit,	help_exit,	0 },
332*355b4669Sjacobs 	{ "status",	lpc_status,	help_status,	1 },
333*355b4669Sjacobs 	{ "abort",	lpc_abort,	help_abort,	1 },
334*355b4669Sjacobs 	{ "clean",	lpc_clean,	help_clean,	1 },
335*355b4669Sjacobs 	{ "disable",	lpc_disable,	help_disable,	1 },
336*355b4669Sjacobs 	{ "down",	lpc_down,	help_down,	2 },
337*355b4669Sjacobs 	{ "enable",	lpc_enable,	help_enable,	1 },
338*355b4669Sjacobs 	{ "restart",	lpc_restart,	help_restart,	1 },
339*355b4669Sjacobs 	{ "start",	lpc_start,	help_start,	1 },
340*355b4669Sjacobs 	{ "stop",	lpc_stop,	help_stop,	1 },
341*355b4669Sjacobs 	{ "up",		lpc_up,		help_up,	1 },
342*355b4669Sjacobs 	{ "topq",	lpc_topq,	help_topq,	2 },
343*355b4669Sjacobs 	{ NULL,		NULL,		NULL,		0 }
344*355b4669Sjacobs };
345*355b4669Sjacobs 
346*355b4669Sjacobs static int
347*355b4669Sjacobs lpc_handler(char *cmd, cmd_handler_t **handler)
348*355b4669Sjacobs {
349*355b4669Sjacobs 	int i;
350*355b4669Sjacobs 
351*355b4669Sjacobs 	for (i = 0; cmd_tab[i].cmd != NULL; i++)
352*355b4669Sjacobs 		if (strcmp(cmd, cmd_tab[i].cmd) == 0) {
353*355b4669Sjacobs 			*handler = cmd_tab[i].handler;
354*355b4669Sjacobs 			return (cmd_tab[i].num_args);
355*355b4669Sjacobs 		}
356*355b4669Sjacobs 	return (-1);
357*355b4669Sjacobs }
358*355b4669Sjacobs 
359*355b4669Sjacobs static char *
360*355b4669Sjacobs lpc_helptext(char *cmd)
361*355b4669Sjacobs {
362*355b4669Sjacobs 	int i;
363*355b4669Sjacobs 
364*355b4669Sjacobs 	for (i = 0; cmd_tab[i].cmd != NULL; i++)
365*355b4669Sjacobs 		if (strcmp(cmd, cmd_tab[i].cmd) == 0)
366*355b4669Sjacobs 			return (gettext(cmd_tab[i].help_string));
367*355b4669Sjacobs 	return (NULL);
368*355b4669Sjacobs }
369*355b4669Sjacobs 
370*355b4669Sjacobs /* ARGSUSED0 */
371*355b4669Sjacobs static int
372*355b4669Sjacobs lpc_help(papi_service_t svc, char **args)
373*355b4669Sjacobs {
374*355b4669Sjacobs 	if (args[1] == NULL) {
375*355b4669Sjacobs 		int i;
376*355b4669Sjacobs 
377*355b4669Sjacobs 		printf(gettext("Commands are:\n\n"));
378*355b4669Sjacobs 		for (i = 0; cmd_tab[i].cmd != NULL; i++) {
379*355b4669Sjacobs 			printf("\t%s", cmd_tab[i].cmd);
380*355b4669Sjacobs 			if ((i % 7) == 6)
381*355b4669Sjacobs 				printf("\n");
382*355b4669Sjacobs 		}
383*355b4669Sjacobs 		if ((i % 7) != 6)
384*355b4669Sjacobs 			printf("\n");
385*355b4669Sjacobs 	} else {
386*355b4669Sjacobs 		char *helptext = lpc_helptext(args[1]);
387*355b4669Sjacobs 
388*355b4669Sjacobs 		if (helptext == NULL)
389*355b4669Sjacobs 			helptext = gettext("no such command");
390*355b4669Sjacobs 
391*355b4669Sjacobs 		printf("%s: %s\n", args[1], helptext);
392*355b4669Sjacobs 	}
393*355b4669Sjacobs 
394*355b4669Sjacobs 	return (0);
395*355b4669Sjacobs }
396*355b4669Sjacobs 
397*355b4669Sjacobs static int
398*355b4669Sjacobs process_one(int (*handler)(papi_service_t, char **), char **av, int expected)
399*355b4669Sjacobs {
400*355b4669Sjacobs 	int rc = -1;
401*355b4669Sjacobs 	papi_status_t status = PAPI_OK;
402*355b4669Sjacobs 	papi_service_t svc = NULL;
403*355b4669Sjacobs 	char *printer = av[1];
404*355b4669Sjacobs 
405*355b4669Sjacobs 	if ((printer != NULL) && (expected != 0)) {
406*355b4669Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
407*355b4669Sjacobs 					cli_auth_callback, encryption, NULL);
408*355b4669Sjacobs 		if (status != PAPI_OK) {
409*355b4669Sjacobs 			fprintf(stderr, gettext(
410*355b4669Sjacobs 				"Failed to contact service for %s: %s\n"),
411*355b4669Sjacobs 				printer, verbose_papi_message(svc, status));
412*355b4669Sjacobs 		}
413*355b4669Sjacobs 	}
414*355b4669Sjacobs 
415*355b4669Sjacobs 	if (status == PAPI_OK)
416*355b4669Sjacobs 		rc = handler(svc, av);
417*355b4669Sjacobs 
418*355b4669Sjacobs 	if (svc != NULL)
419*355b4669Sjacobs 		papiServiceDestroy(svc);
420*355b4669Sjacobs 
421*355b4669Sjacobs 	return (rc);
422*355b4669Sjacobs }
423*355b4669Sjacobs 
424*355b4669Sjacobs static int
425*355b4669Sjacobs process_all(int (*handler)(papi_service_t, char **), char **av, int expected)
426*355b4669Sjacobs {
427*355b4669Sjacobs 	papi_status_t status;
428*355b4669Sjacobs 	papi_service_t svc = NULL;
429*355b4669Sjacobs 	char **printers;
430*355b4669Sjacobs 	int rc = 0;
431*355b4669Sjacobs 
432*355b4669Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, NULL,
433*355b4669Sjacobs 				encryption, NULL);
434*355b4669Sjacobs 	if (status != PAPI_OK) {
435*355b4669Sjacobs 		fprintf(stderr, gettext("Failed to contact service: %s\n"),
436*355b4669Sjacobs 			verbose_papi_message(svc, status));
437*355b4669Sjacobs 		return (-1);
438*355b4669Sjacobs 	}
439*355b4669Sjacobs 
440*355b4669Sjacobs 	if ((printers = interest_list(svc)) != NULL) {
441*355b4669Sjacobs 		int i;
442*355b4669Sjacobs 
443*355b4669Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
444*355b4669Sjacobs 			av[1] = printers[i];
445*355b4669Sjacobs 			rc += process_one(handler, av, expected);
446*355b4669Sjacobs 		}
447*355b4669Sjacobs 	}
448*355b4669Sjacobs 
449*355b4669Sjacobs 	papiServiceDestroy(svc);
450*355b4669Sjacobs 
451*355b4669Sjacobs 	return (rc);
452*355b4669Sjacobs }
453*355b4669Sjacobs 
454*355b4669Sjacobs static int
455*355b4669Sjacobs process(int ac, char **av)
456*355b4669Sjacobs {
457*355b4669Sjacobs 	int (*handler)(papi_service_t, char **) = NULL;
458*355b4669Sjacobs 	int num_args = -1;
459*355b4669Sjacobs 
460*355b4669Sjacobs 	char *printer = av[1];
461*355b4669Sjacobs 	int rc = -1;
462*355b4669Sjacobs 
463*355b4669Sjacobs 	if ((num_args = lpc_handler(av[0], &handler)) < 0) {
464*355b4669Sjacobs 		printf(gettext("%s: invalid command\n"), av[0]);
465*355b4669Sjacobs 		return (-1);
466*355b4669Sjacobs 	}
467*355b4669Sjacobs 
468*355b4669Sjacobs 	if (((printer != NULL) && (strcmp(printer, "all") != 0)) ||
469*355b4669Sjacobs 	    (num_args <= ac))
470*355b4669Sjacobs 		rc = process_one(handler, av, num_args);
471*355b4669Sjacobs 	else
472*355b4669Sjacobs 		rc = process_all(handler, av, num_args);
473*355b4669Sjacobs 
474*355b4669Sjacobs 	return (rc);
475*355b4669Sjacobs }
476*355b4669Sjacobs 
477*355b4669Sjacobs static void
478*355b4669Sjacobs usage(char *program)
479*355b4669Sjacobs {
480*355b4669Sjacobs 	char *name;
481*355b4669Sjacobs 
482*355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
483*355b4669Sjacobs 		name = program;
484*355b4669Sjacobs 	else
485*355b4669Sjacobs 		name++;
486*355b4669Sjacobs 
487*355b4669Sjacobs 	fprintf(stdout,
488*355b4669Sjacobs 		gettext("Usage: %s [ command [ parameter...]]\n"),
489*355b4669Sjacobs 		name);
490*355b4669Sjacobs 	exit(1);
491*355b4669Sjacobs }
492*355b4669Sjacobs 
493*355b4669Sjacobs static void
494*355b4669Sjacobs lpc_shell()
495*355b4669Sjacobs {
496*355b4669Sjacobs 	for (;;) {
497*355b4669Sjacobs 		char line[256];
498*355b4669Sjacobs 		char **av = NULL;
499*355b4669Sjacobs 		int ac = 0;
500*355b4669Sjacobs 
501*355b4669Sjacobs 		/* prompt */
502*355b4669Sjacobs 		fprintf(stdout, "lpc> ");
503*355b4669Sjacobs 		fflush(stdout);
504*355b4669Sjacobs 
505*355b4669Sjacobs 		/* get command */
506*355b4669Sjacobs 		if (fgets(line, sizeof (line), stdin) == NULL)
507*355b4669Sjacobs 			exit(1);
508*355b4669Sjacobs 		if ((av = strsplit(line, " \t\n")) != NULL)
509*355b4669Sjacobs 			for (ac = 0; av[ac] != NULL; ac++);
510*355b4669Sjacobs 
511*355b4669Sjacobs 		(void) process(ac, av);
512*355b4669Sjacobs 		free(av);
513*355b4669Sjacobs 	}
514*355b4669Sjacobs }
515*355b4669Sjacobs 
516*355b4669Sjacobs int
517*355b4669Sjacobs main(int ac, char *av[])
518*355b4669Sjacobs {
519*355b4669Sjacobs 	int result = 0;
520*355b4669Sjacobs 	int c;
521*355b4669Sjacobs 
522*355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
523*355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
524*355b4669Sjacobs 
525*355b4669Sjacobs 	while ((c = getopt(ac, av, "E")) != EOF)
526*355b4669Sjacobs 		switch (c) {
527*355b4669Sjacobs 		case 'E':
528*355b4669Sjacobs 			encryption = PAPI_ENCRYPT_ALWAYS;
529*355b4669Sjacobs 			break;
530*355b4669Sjacobs 		default:
531*355b4669Sjacobs 			usage(av[0]);
532*355b4669Sjacobs 		}
533*355b4669Sjacobs 
534*355b4669Sjacobs 	if (optind == ac)
535*355b4669Sjacobs 		lpc_shell();
536*355b4669Sjacobs 	else
537*355b4669Sjacobs 		result = process(optind - 2, &av[optind]);
538*355b4669Sjacobs 
539*355b4669Sjacobs 	return (result);
540*355b4669Sjacobs }
541