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*dd850934SKeerthi Kondaka  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpc.c 146 2006-03-24 00:26:54Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #include <stdio.h>
31355b4669Sjacobs #include <stdlib.h>
32355b4669Sjacobs #include <unistd.h>
33355b4669Sjacobs #include <string.h>
34355b4669Sjacobs #include <locale.h>
35355b4669Sjacobs #include <libintl.h>
36355b4669Sjacobs #include <papi.h>
37355b4669Sjacobs #include "common.h"
38355b4669Sjacobs 
39355b4669Sjacobs typedef int (cmd_handler_t)(papi_service_t, char **);
40355b4669Sjacobs 
41355b4669Sjacobs static papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
42355b4669Sjacobs 
43355b4669Sjacobs /* ARGSUSED0 */
44355b4669Sjacobs static int
lpc_exit(papi_service_t svc,char ** args)45355b4669Sjacobs lpc_exit(papi_service_t svc, char **args)
46355b4669Sjacobs {
47355b4669Sjacobs 	exit(0);
48355b4669Sjacobs 	/* NOTREACHED */
49355b4669Sjacobs 	return (0);
50355b4669Sjacobs }
51355b4669Sjacobs 
52355b4669Sjacobs static int
lpc_status(papi_service_t svc,char ** args)53355b4669Sjacobs lpc_status(papi_service_t svc, char **args)
54355b4669Sjacobs {
55355b4669Sjacobs 	papi_status_t status;
56355b4669Sjacobs 	papi_printer_t p = NULL;
57355b4669Sjacobs 	char *pattrs[] = { "printer-state", "printer-state-reasons",
58355b4669Sjacobs 				"printer-is-accepting-jobs", NULL };
59355b4669Sjacobs 	char *destination = args[1];
60355b4669Sjacobs 
61355b4669Sjacobs 	status = papiPrinterQuery(svc, destination, pattrs, NULL, &p);
62355b4669Sjacobs 	if (status == PAPI_OK) {
63355b4669Sjacobs 		papi_attribute_t **list = papiPrinterGetAttributeList(p);
64355b4669Sjacobs 		char accepting = 0;
65355b4669Sjacobs 		int32_t state = 0;
66355b4669Sjacobs 
67355b4669Sjacobs 		printf("%s:\n", destination);
68355b4669Sjacobs 
69355b4669Sjacobs 		(void) papiAttributeListGetBoolean(list, NULL,
70355b4669Sjacobs 				"printer-is-accepting-jobs", &accepting);
71355b4669Sjacobs 		printf(gettext("\tqueueing is %s\n"),
72355b4669Sjacobs 			(accepting ? gettext("enabled") : gettext("disabled")));
73355b4669Sjacobs 
74355b4669Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
75355b4669Sjacobs 					"printer-state", &state);
76355b4669Sjacobs 		printf("\tprinting is %s\n",
77355b4669Sjacobs 			((state != 0x05) ? gettext("enabled") :
78355b4669Sjacobs 				gettext("disabled")));
79355b4669Sjacobs 
80355b4669Sjacobs 		if (state != 0x03) {	/* !idle */
81355b4669Sjacobs 			papi_job_t *jobs = NULL;
82355b4669Sjacobs 			int i = 0;
83355b4669Sjacobs 
84355b4669Sjacobs 			(void) papiPrinterListJobs(svc, destination, NULL,
85355b4669Sjacobs 					PAPI_LIST_JOBS_ALL, 0, &jobs);
86355b4669Sjacobs 			if (jobs != NULL) {
87355b4669Sjacobs 				for (i = 0; jobs[i] != NULL; i++);
88355b4669Sjacobs 				papiJobListFree(jobs);
89355b4669Sjacobs 			}
90355b4669Sjacobs 			printf(gettext("\t%d entries in spool area\n"), i);
91355b4669Sjacobs 		} else
92355b4669Sjacobs 			printf(gettext("\tno entries\n"));
93355b4669Sjacobs 
94355b4669Sjacobs 		if (state == 0x04)
95355b4669Sjacobs 			printf(gettext("\tdaemon present\n"));
96355b4669Sjacobs 
97355b4669Sjacobs 	} else {
98355b4669Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
99355b4669Sjacobs 			verbose_papi_message(svc, status));
100355b4669Sjacobs 		return (-1);
101355b4669Sjacobs 	}
102355b4669Sjacobs 
103355b4669Sjacobs 	papiPrinterFree(p);
104355b4669Sjacobs 
105355b4669Sjacobs 	return (0);
106355b4669Sjacobs }
107355b4669Sjacobs 
108355b4669Sjacobs static int
lpc_abort(papi_service_t svc,char ** args)109355b4669Sjacobs lpc_abort(papi_service_t svc, char **args)
110355b4669Sjacobs {
111355b4669Sjacobs 	papi_status_t status;
112355b4669Sjacobs 	char *destination = args[1];
113355b4669Sjacobs 
114355b4669Sjacobs 	if (destination == NULL) {
115355b4669Sjacobs 		fprintf(stderr, gettext("Usage: abort (destination)\n"));
116355b4669Sjacobs 		return (-1);
117355b4669Sjacobs 	}
118355b4669Sjacobs 
119355b4669Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc abort");
120355b4669Sjacobs 	if (status == PAPI_OK) {
121355b4669Sjacobs 		printf(gettext("%s: processing disabled after current job\n"),
122355b4669Sjacobs 			destination);
123355b4669Sjacobs 	} else {
124355b4669Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
125355b4669Sjacobs 			verbose_papi_message(svc, status));
126355b4669Sjacobs 	}
127355b4669Sjacobs 
128355b4669Sjacobs 	return (0);
129355b4669Sjacobs }
130355b4669Sjacobs 
131355b4669Sjacobs static int
lpc_clean(papi_service_t svc,char ** args)132355b4669Sjacobs lpc_clean(papi_service_t svc, char **args)
133355b4669Sjacobs {
134355b4669Sjacobs 	papi_status_t status;
135355b4669Sjacobs 	papi_job_t *jobs = NULL;
136355b4669Sjacobs 	char *destination = args[1];
137355b4669Sjacobs 
138355b4669Sjacobs 	if (destination == NULL) {
139355b4669Sjacobs 		fprintf(stderr, gettext("Usage: clean (destination)\n"));
140355b4669Sjacobs 		return (-1);
141355b4669Sjacobs 	}
142355b4669Sjacobs 
143355b4669Sjacobs 	status = papiPrinterPurgeJobs(svc, destination, &jobs);
144355b4669Sjacobs 	if (status != PAPI_OK) {
145355b4669Sjacobs 		fprintf(stderr, gettext("clean: %s: %s\n"), destination,
146355b4669Sjacobs 			verbose_papi_message(svc, status));
147355b4669Sjacobs 		return (-1);
148355b4669Sjacobs 	}
149355b4669Sjacobs 
150355b4669Sjacobs 	if (jobs != NULL) {
151355b4669Sjacobs 		int i;
152355b4669Sjacobs 
153355b4669Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
154355b4669Sjacobs 			printf(gettext("\t%s-%d: cancelled\n"), destination,
155355b4669Sjacobs 				papiJobGetId(jobs[i]));
156355b4669Sjacobs 
157355b4669Sjacobs 		papiJobListFree(jobs);
158355b4669Sjacobs 	}
159355b4669Sjacobs 
160355b4669Sjacobs 	return (0);
161355b4669Sjacobs }
162355b4669Sjacobs 
163355b4669Sjacobs static int
lpc_disable(papi_service_t svc,char ** args)164355b4669Sjacobs lpc_disable(papi_service_t svc, char **args)
165355b4669Sjacobs {
166355b4669Sjacobs 	papi_status_t status;
167355b4669Sjacobs 	char *destination = args[1];
168355b4669Sjacobs 
169355b4669Sjacobs 	if (destination == NULL) {
170355b4669Sjacobs 		fprintf(stderr, gettext("Usage: disable: (destination)\n"));
171355b4669Sjacobs 		return (-1);
172355b4669Sjacobs 	}
173355b4669Sjacobs 
174355b4669Sjacobs 	status = papiPrinterDisable(svc, destination, NULL);
175355b4669Sjacobs 	if (status != PAPI_OK) {
176355b4669Sjacobs 		fprintf(stderr, gettext("disable: %s: %s\n"), destination,
177355b4669Sjacobs 			verbose_papi_message(svc, status));
178355b4669Sjacobs 		return (-1);
179355b4669Sjacobs 	}
180355b4669Sjacobs 
181355b4669Sjacobs 	return (0);
182355b4669Sjacobs }
183355b4669Sjacobs 
184355b4669Sjacobs static int
lpc_enable(papi_service_t svc,char ** args)185355b4669Sjacobs lpc_enable(papi_service_t svc, char **args)
186355b4669Sjacobs {
187355b4669Sjacobs 	papi_status_t status;
188355b4669Sjacobs 	char *destination = args[1];
189355b4669Sjacobs 
190355b4669Sjacobs 	if (destination == NULL) {
191355b4669Sjacobs 		fprintf(stderr, gettext("Usage: enable: (destination)\n"));
192355b4669Sjacobs 		return (-1);
193355b4669Sjacobs 	}
194355b4669Sjacobs 
195355b4669Sjacobs 	status = papiPrinterEnable(svc, destination);
196355b4669Sjacobs 	if (status != PAPI_OK) {
197355b4669Sjacobs 		fprintf(stderr, gettext("enable: %s: %s\n"), destination,
198355b4669Sjacobs 			verbose_papi_message(svc, status));
199355b4669Sjacobs 		return (-1);
200355b4669Sjacobs 	}
201355b4669Sjacobs 
202355b4669Sjacobs 	return (0);
203355b4669Sjacobs }
204355b4669Sjacobs 
205355b4669Sjacobs static int
lpc_restart(papi_service_t svc,char ** args)206355b4669Sjacobs lpc_restart(papi_service_t svc, char **args)
207355b4669Sjacobs {
208355b4669Sjacobs 	int rc = 0;
209355b4669Sjacobs 
210355b4669Sjacobs 	rc += lpc_disable(svc, args);
211355b4669Sjacobs 	rc += lpc_enable(svc, args);
212355b4669Sjacobs 
213355b4669Sjacobs 	return (rc);
214355b4669Sjacobs }
215355b4669Sjacobs 
216355b4669Sjacobs static int
lpc_start(papi_service_t svc,char ** args)217355b4669Sjacobs lpc_start(papi_service_t svc, char **args)
218355b4669Sjacobs {
219355b4669Sjacobs 	papi_status_t status;
220355b4669Sjacobs 	char *destination = args[1];
221355b4669Sjacobs 
222355b4669Sjacobs 	if (destination == NULL) {
223355b4669Sjacobs 		fprintf(stderr, gettext("Usage: start (destination)\n"));
224355b4669Sjacobs 		return (-1);
225355b4669Sjacobs 	}
226355b4669Sjacobs 
227355b4669Sjacobs 	status = papiPrinterResume(svc, destination);
228355b4669Sjacobs 	if (status != PAPI_OK) {
229355b4669Sjacobs 		fprintf(stderr, gettext("start: %s: %s\n"), destination,
230355b4669Sjacobs 			verbose_papi_message(svc, status));
231355b4669Sjacobs 		return (-1);
232355b4669Sjacobs 	}
233355b4669Sjacobs 
234355b4669Sjacobs 	return (0);
235355b4669Sjacobs }
236355b4669Sjacobs 
237355b4669Sjacobs static int
lpc_stop(papi_service_t svc,char ** args)238355b4669Sjacobs lpc_stop(papi_service_t svc, char **args)
239355b4669Sjacobs {
240355b4669Sjacobs 	papi_status_t status;
241355b4669Sjacobs 	char *destination = args[1];
242355b4669Sjacobs 
243355b4669Sjacobs 	if (destination == NULL) {
244355b4669Sjacobs 		fprintf(stderr, gettext("Usage: stop (destination)\n"));
245355b4669Sjacobs 		return (-1);
246355b4669Sjacobs 	}
247355b4669Sjacobs 
248355b4669Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc");
249355b4669Sjacobs 	if (status != PAPI_OK) {
250355b4669Sjacobs 		fprintf(stderr, gettext("stop: %s: %s\n"), destination,
251355b4669Sjacobs 			verbose_papi_message(svc, status));
252355b4669Sjacobs 		return (-1);
253355b4669Sjacobs 	}
254355b4669Sjacobs 
255355b4669Sjacobs 	return (0);
256355b4669Sjacobs }
257355b4669Sjacobs 
258355b4669Sjacobs static int
lpc_topq(papi_service_t svc,char ** args)259355b4669Sjacobs lpc_topq(papi_service_t svc, char **args)
260355b4669Sjacobs {
261355b4669Sjacobs 	papi_status_t status;
262355b4669Sjacobs 	char *destination = args[1];
263*dd850934SKeerthi Kondaka 	char *idstr = args[2];
264*dd850934SKeerthi Kondaka 	int32_t id;
265355b4669Sjacobs 
266*dd850934SKeerthi Kondaka 	if (destination == NULL || idstr == NULL) {
267355b4669Sjacobs 		fprintf(stderr, gettext("Usage: topq (destination) (id)\n"));
268355b4669Sjacobs 		return (-1);
269355b4669Sjacobs 	}
270*dd850934SKeerthi Kondaka 	id = atoi(idstr);
271355b4669Sjacobs 
272355b4669Sjacobs 	status = papiJobPromote(svc, destination, id);
273355b4669Sjacobs 	if (status != PAPI_OK) {
274*dd850934SKeerthi Kondaka 		fprintf(stderr, gettext("topq: %s-%d: %s\n"), destination, id,
275*dd850934SKeerthi Kondaka 		    verbose_papi_message(svc, status));
276355b4669Sjacobs 		return (-1);
277355b4669Sjacobs 	}
278355b4669Sjacobs 
279355b4669Sjacobs 	return (0);
280355b4669Sjacobs }
281355b4669Sjacobs 
282355b4669Sjacobs static int
lpc_up(papi_service_t svc,char ** args)283355b4669Sjacobs lpc_up(papi_service_t svc, char **args)
284355b4669Sjacobs {
285355b4669Sjacobs 	int rc = 0;
286355b4669Sjacobs 
287355b4669Sjacobs 	rc += lpc_enable(svc, args);
288355b4669Sjacobs 	rc += lpc_start(svc, args);
289355b4669Sjacobs 
290355b4669Sjacobs 	return (rc);
291355b4669Sjacobs }
292355b4669Sjacobs 
293355b4669Sjacobs static int
lpc_down(papi_service_t svc,char ** args)294355b4669Sjacobs lpc_down(papi_service_t svc, char **args)
295355b4669Sjacobs {
296355b4669Sjacobs 	int rc = 0;
297355b4669Sjacobs 
298355b4669Sjacobs 	rc += lpc_disable(svc, args);
299355b4669Sjacobs 	rc += lpc_stop(svc, args);
300355b4669Sjacobs 
301355b4669Sjacobs 	return (rc);
302355b4669Sjacobs }
303355b4669Sjacobs 
304355b4669Sjacobs static int lpc_help(papi_service_t svc, char **args);	/* forward reference */
305355b4669Sjacobs 
306355b4669Sjacobs static char help_help[] = "get help on commands";
307355b4669Sjacobs static char help_exit[] = "exit lpc";
308355b4669Sjacobs static char help_status[] = "show status of daemon and queue";
309355b4669Sjacobs static char help_abort[] =
310355b4669Sjacobs 		"disable print queue terminating any active job processing";
311355b4669Sjacobs static char help_clean[] = "remove all jobs from a queue";
312355b4669Sjacobs static char help_disable[] = "turn off spooling to a queue";
313355b4669Sjacobs static char help_down[] =
314355b4669Sjacobs 		"turn off queueing and printing for a queue and set a reason";
315355b4669Sjacobs static char help_enable[] = "turn on spooling to a queue";
316355b4669Sjacobs static char help_restart[] = "restart job processing for a queue";
317355b4669Sjacobs static char help_start[] = "turn on printing from a queue";
318355b4669Sjacobs static char help_stop[] = "turn off printing from a queue";
319355b4669Sjacobs static char help_up[] = "turn on queueing and printing for a queue";
320355b4669Sjacobs static char help_topq[] = "put a job at the top of the queue";
321355b4669Sjacobs 
322355b4669Sjacobs static struct {
323355b4669Sjacobs 	char *cmd;
324355b4669Sjacobs 	int (*handler)(papi_service_t svc, char **args);
325355b4669Sjacobs 	char *help_string;
326355b4669Sjacobs 	int num_args;
327355b4669Sjacobs } cmd_tab[] = {
328355b4669Sjacobs 	{ "?",		lpc_help,	help_help,	0 },
329355b4669Sjacobs 	{ "help",	lpc_help,	help_help,	0 },
330355b4669Sjacobs 	{ "exit",	lpc_exit,	help_exit,	0 },
331355b4669Sjacobs 	{ "quit",	lpc_exit,	help_exit,	0 },
332355b4669Sjacobs 	{ "status",	lpc_status,	help_status,	1 },
333355b4669Sjacobs 	{ "abort",	lpc_abort,	help_abort,	1 },
334355b4669Sjacobs 	{ "clean",	lpc_clean,	help_clean,	1 },
335355b4669Sjacobs 	{ "disable",	lpc_disable,	help_disable,	1 },
336355b4669Sjacobs 	{ "down",	lpc_down,	help_down,	2 },
337355b4669Sjacobs 	{ "enable",	lpc_enable,	help_enable,	1 },
338355b4669Sjacobs 	{ "restart",	lpc_restart,	help_restart,	1 },
339355b4669Sjacobs 	{ "start",	lpc_start,	help_start,	1 },
340355b4669Sjacobs 	{ "stop",	lpc_stop,	help_stop,	1 },
341355b4669Sjacobs 	{ "up",		lpc_up,		help_up,	1 },
342355b4669Sjacobs 	{ "topq",	lpc_topq,	help_topq,	2 },
343355b4669Sjacobs 	{ NULL,		NULL,		NULL,		0 }
344355b4669Sjacobs };
345355b4669Sjacobs 
346355b4669Sjacobs static int
lpc_handler(char * cmd,cmd_handler_t ** handler)347355b4669Sjacobs lpc_handler(char *cmd, cmd_handler_t **handler)
348355b4669Sjacobs {
349355b4669Sjacobs 	int i;
350355b4669Sjacobs 
351355b4669Sjacobs 	for (i = 0; cmd_tab[i].cmd != NULL; i++)
352355b4669Sjacobs 		if (strcmp(cmd, cmd_tab[i].cmd) == 0) {
353355b4669Sjacobs 			*handler = cmd_tab[i].handler;
354355b4669Sjacobs 			return (cmd_tab[i].num_args);
355355b4669Sjacobs 		}
356355b4669Sjacobs 	return (-1);
357355b4669Sjacobs }
358355b4669Sjacobs 
359355b4669Sjacobs static char *
lpc_helptext(char * cmd)360355b4669Sjacobs lpc_helptext(char *cmd)
361355b4669Sjacobs {
362355b4669Sjacobs 	int i;
363355b4669Sjacobs 
364355b4669Sjacobs 	for (i = 0; cmd_tab[i].cmd != NULL; i++)
365355b4669Sjacobs 		if (strcmp(cmd, cmd_tab[i].cmd) == 0)
366355b4669Sjacobs 			return (gettext(cmd_tab[i].help_string));
367355b4669Sjacobs 	return (NULL);
368355b4669Sjacobs }
369355b4669Sjacobs 
370355b4669Sjacobs /* ARGSUSED0 */
371355b4669Sjacobs static int
lpc_help(papi_service_t svc,char ** args)372355b4669Sjacobs lpc_help(papi_service_t svc, char **args)
373355b4669Sjacobs {
374355b4669Sjacobs 	if (args[1] == NULL) {
375355b4669Sjacobs 		int i;
376355b4669Sjacobs 
377355b4669Sjacobs 		printf(gettext("Commands are:\n\n"));
378355b4669Sjacobs 		for (i = 0; cmd_tab[i].cmd != NULL; i++) {
379355b4669Sjacobs 			printf("\t%s", cmd_tab[i].cmd);
380355b4669Sjacobs 			if ((i % 7) == 6)
381355b4669Sjacobs 				printf("\n");
382355b4669Sjacobs 		}
383355b4669Sjacobs 		if ((i % 7) != 6)
384355b4669Sjacobs 			printf("\n");
385355b4669Sjacobs 	} else {
386355b4669Sjacobs 		char *helptext = lpc_helptext(args[1]);
387355b4669Sjacobs 
388355b4669Sjacobs 		if (helptext == NULL)
389355b4669Sjacobs 			helptext = gettext("no such command");
390355b4669Sjacobs 
391355b4669Sjacobs 		printf("%s: %s\n", args[1], helptext);
392355b4669Sjacobs 	}
393355b4669Sjacobs 
394355b4669Sjacobs 	return (0);
395355b4669Sjacobs }
396355b4669Sjacobs 
397355b4669Sjacobs static int
process_one(int (* handler)(papi_service_t,char **),char ** av,int expected)398355b4669Sjacobs process_one(int (*handler)(papi_service_t, char **), char **av, int expected)
399355b4669Sjacobs {
400355b4669Sjacobs 	int rc = -1;
401355b4669Sjacobs 	papi_status_t status = PAPI_OK;
402355b4669Sjacobs 	papi_service_t svc = NULL;
403355b4669Sjacobs 	char *printer = av[1];
404355b4669Sjacobs 
405355b4669Sjacobs 	if ((printer != NULL) && (expected != 0)) {
406355b4669Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
407355b4669Sjacobs 					cli_auth_callback, encryption, NULL);
408355b4669Sjacobs 		if (status != PAPI_OK) {
409355b4669Sjacobs 			fprintf(stderr, gettext(
410355b4669Sjacobs 				"Failed to contact service for %s: %s\n"),
411355b4669Sjacobs 				printer, verbose_papi_message(svc, status));
412355b4669Sjacobs 		}
413355b4669Sjacobs 	}
414355b4669Sjacobs 
415355b4669Sjacobs 	if (status == PAPI_OK)
416355b4669Sjacobs 		rc = handler(svc, av);
417355b4669Sjacobs 
418355b4669Sjacobs 	if (svc != NULL)
419355b4669Sjacobs 		papiServiceDestroy(svc);
420355b4669Sjacobs 
421355b4669Sjacobs 	return (rc);
422355b4669Sjacobs }
423355b4669Sjacobs 
424355b4669Sjacobs static int
process_all(int (* handler)(papi_service_t,char **),char ** av,int expected)425355b4669Sjacobs process_all(int (*handler)(papi_service_t, char **), char **av, int expected)
426355b4669Sjacobs {
427355b4669Sjacobs 	papi_status_t status;
428355b4669Sjacobs 	papi_service_t svc = NULL;
429355b4669Sjacobs 	char **printers;
430355b4669Sjacobs 	int rc = 0;
431355b4669Sjacobs 
432355b4669Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, NULL,
433355b4669Sjacobs 				encryption, NULL);
434355b4669Sjacobs 	if (status != PAPI_OK) {
435355b4669Sjacobs 		fprintf(stderr, gettext("Failed to contact service: %s\n"),
436355b4669Sjacobs 			verbose_papi_message(svc, status));
437355b4669Sjacobs 		return (-1);
438355b4669Sjacobs 	}
439355b4669Sjacobs 
440355b4669Sjacobs 	if ((printers = interest_list(svc)) != NULL) {
441355b4669Sjacobs 		int i;
442355b4669Sjacobs 
443355b4669Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
444355b4669Sjacobs 			av[1] = printers[i];
445355b4669Sjacobs 			rc += process_one(handler, av, expected);
446355b4669Sjacobs 		}
447355b4669Sjacobs 	}
448355b4669Sjacobs 
449355b4669Sjacobs 	papiServiceDestroy(svc);
450355b4669Sjacobs 
451355b4669Sjacobs 	return (rc);
452355b4669Sjacobs }
453355b4669Sjacobs 
454355b4669Sjacobs static int
process(int ac,char ** av)455355b4669Sjacobs process(int ac, char **av)
456355b4669Sjacobs {
457355b4669Sjacobs 	int (*handler)(papi_service_t, char **) = NULL;
458355b4669Sjacobs 	int num_args = -1;
459355b4669Sjacobs 
460355b4669Sjacobs 	char *printer = av[1];
461355b4669Sjacobs 	int rc = -1;
462355b4669Sjacobs 
463355b4669Sjacobs 	if ((num_args = lpc_handler(av[0], &handler)) < 0) {
464355b4669Sjacobs 		printf(gettext("%s: invalid command\n"), av[0]);
465355b4669Sjacobs 		return (-1);
466355b4669Sjacobs 	}
467355b4669Sjacobs 
468*dd850934SKeerthi Kondaka 	if (((ac == 0) && (num_args == 1)) ||
469b51e021dSjacobs 	    ((printer != NULL) && strcmp(printer, "all") == 0))
470355b4669Sjacobs 		rc = process_all(handler, av, num_args);
471b51e021dSjacobs 	else if (num_args < ac) {
472b51e021dSjacobs 		int i;
473b51e021dSjacobs 		char *argv[4];
474b51e021dSjacobs 
475b51e021dSjacobs 		memset(argv, 0, sizeof (argv));
476b51e021dSjacobs 		argv[0] = av[0];
477b51e021dSjacobs 
478b51e021dSjacobs 		if (strcmp(av[0], "topq") == 0) {
479b51e021dSjacobs 			argv[1] = av[1];
480b51e021dSjacobs 			for (i = 2; i <= ac; i++) {
481b51e021dSjacobs 				argv[2] = av[i];
482b51e021dSjacobs 				process_one(handler, argv, num_args);
483b51e021dSjacobs 			}
484b51e021dSjacobs 		} else
485b51e021dSjacobs 			for (i = 1; i <= ac; i++) {
486b51e021dSjacobs 				argv[1] = av[i];
487b51e021dSjacobs 				process_one(handler, argv, num_args);
488b51e021dSjacobs 			}
489b51e021dSjacobs 	} else
490b51e021dSjacobs 		rc = process_one(handler, av, num_args);
491355b4669Sjacobs 
492355b4669Sjacobs 	return (rc);
493355b4669Sjacobs }
494355b4669Sjacobs 
495355b4669Sjacobs static void
usage(char * program)496355b4669Sjacobs usage(char *program)
497355b4669Sjacobs {
498355b4669Sjacobs 	char *name;
499355b4669Sjacobs 
500355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
501355b4669Sjacobs 		name = program;
502355b4669Sjacobs 	else
503355b4669Sjacobs 		name++;
504355b4669Sjacobs 
505355b4669Sjacobs 	fprintf(stdout,
506355b4669Sjacobs 		gettext("Usage: %s [ command [ parameter...]]\n"),
507355b4669Sjacobs 		name);
508355b4669Sjacobs 	exit(1);
509355b4669Sjacobs }
510355b4669Sjacobs 
511355b4669Sjacobs static void
lpc_shell()512355b4669Sjacobs lpc_shell()
513355b4669Sjacobs {
514355b4669Sjacobs 	for (;;) {
515355b4669Sjacobs 		char line[256];
516355b4669Sjacobs 		char **av = NULL;
517355b4669Sjacobs 		int ac = 0;
518355b4669Sjacobs 
519355b4669Sjacobs 		/* prompt */
520355b4669Sjacobs 		fprintf(stdout, "lpc> ");
521355b4669Sjacobs 		fflush(stdout);
522355b4669Sjacobs 
523355b4669Sjacobs 		/* get command */
524355b4669Sjacobs 		if (fgets(line, sizeof (line), stdin) == NULL)
525355b4669Sjacobs 			exit(1);
526355b4669Sjacobs 		if ((av = strsplit(line, " \t\n")) != NULL)
527355b4669Sjacobs 			for (ac = 0; av[ac] != NULL; ac++);
528*dd850934SKeerthi Kondaka 		else
529*dd850934SKeerthi Kondaka 			continue;
530355b4669Sjacobs 
531*dd850934SKeerthi Kondaka 		if (ac > 0)
532*dd850934SKeerthi Kondaka 			(void) process(ac - 1, av);
533355b4669Sjacobs 		free(av);
534355b4669Sjacobs 	}
535355b4669Sjacobs }
536355b4669Sjacobs 
537355b4669Sjacobs int
main(int ac,char * av[])538355b4669Sjacobs main(int ac, char *av[])
539355b4669Sjacobs {
540355b4669Sjacobs 	int result = 0;
541355b4669Sjacobs 	int c;
542355b4669Sjacobs 
543355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
544355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
545355b4669Sjacobs 
546355b4669Sjacobs 	while ((c = getopt(ac, av, "E")) != EOF)
547355b4669Sjacobs 		switch (c) {
548355b4669Sjacobs 		case 'E':
549355b4669Sjacobs 			encryption = PAPI_ENCRYPT_ALWAYS;
550355b4669Sjacobs 			break;
551355b4669Sjacobs 		default:
552355b4669Sjacobs 			usage(av[0]);
553355b4669Sjacobs 		}
554355b4669Sjacobs 
555355b4669Sjacobs 	if (optind == ac)
556355b4669Sjacobs 		lpc_shell();
557355b4669Sjacobs 	else
558b51e021dSjacobs 		result = process(ac - optind - 1, &av[optind]);
559355b4669Sjacobs 
560355b4669Sjacobs 	return (result);
561355b4669Sjacobs }
562