xref: /onnv-gate/usr/src/cmd/print/bsd-sysv-commands/lpc.c (revision 9551:6d03166bbef4)
12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
23*9551SKeerthi.Kondaka@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: lpc.c 146 2006-03-24 00:26:54Z njacobs $ */
292264Sjacobs 
302264Sjacobs #include <stdio.h>
312264Sjacobs #include <stdlib.h>
322264Sjacobs #include <unistd.h>
332264Sjacobs #include <string.h>
342264Sjacobs #include <locale.h>
352264Sjacobs #include <libintl.h>
362264Sjacobs #include <papi.h>
372264Sjacobs #include "common.h"
382264Sjacobs 
392264Sjacobs typedef int (cmd_handler_t)(papi_service_t, char **);
402264Sjacobs 
412264Sjacobs static papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
422264Sjacobs 
432264Sjacobs /* ARGSUSED0 */
442264Sjacobs static int
lpc_exit(papi_service_t svc,char ** args)452264Sjacobs lpc_exit(papi_service_t svc, char **args)
462264Sjacobs {
472264Sjacobs 	exit(0);
482264Sjacobs 	/* NOTREACHED */
492264Sjacobs 	return (0);
502264Sjacobs }
512264Sjacobs 
522264Sjacobs static int
lpc_status(papi_service_t svc,char ** args)532264Sjacobs lpc_status(papi_service_t svc, char **args)
542264Sjacobs {
552264Sjacobs 	papi_status_t status;
562264Sjacobs 	papi_printer_t p = NULL;
572264Sjacobs 	char *pattrs[] = { "printer-state", "printer-state-reasons",
582264Sjacobs 				"printer-is-accepting-jobs", NULL };
592264Sjacobs 	char *destination = args[1];
602264Sjacobs 
612264Sjacobs 	status = papiPrinterQuery(svc, destination, pattrs, NULL, &p);
622264Sjacobs 	if (status == PAPI_OK) {
632264Sjacobs 		papi_attribute_t **list = papiPrinterGetAttributeList(p);
642264Sjacobs 		char accepting = 0;
652264Sjacobs 		int32_t state = 0;
662264Sjacobs 
672264Sjacobs 		printf("%s:\n", destination);
682264Sjacobs 
692264Sjacobs 		(void) papiAttributeListGetBoolean(list, NULL,
702264Sjacobs 				"printer-is-accepting-jobs", &accepting);
712264Sjacobs 		printf(gettext("\tqueueing is %s\n"),
722264Sjacobs 			(accepting ? gettext("enabled") : gettext("disabled")));
732264Sjacobs 
742264Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
752264Sjacobs 					"printer-state", &state);
762264Sjacobs 		printf("\tprinting is %s\n",
772264Sjacobs 			((state != 0x05) ? gettext("enabled") :
782264Sjacobs 				gettext("disabled")));
792264Sjacobs 
802264Sjacobs 		if (state != 0x03) {	/* !idle */
812264Sjacobs 			papi_job_t *jobs = NULL;
822264Sjacobs 			int i = 0;
832264Sjacobs 
842264Sjacobs 			(void) papiPrinterListJobs(svc, destination, NULL,
852264Sjacobs 					PAPI_LIST_JOBS_ALL, 0, &jobs);
862264Sjacobs 			if (jobs != NULL) {
872264Sjacobs 				for (i = 0; jobs[i] != NULL; i++);
882264Sjacobs 				papiJobListFree(jobs);
892264Sjacobs 			}
902264Sjacobs 			printf(gettext("\t%d entries in spool area\n"), i);
912264Sjacobs 		} else
922264Sjacobs 			printf(gettext("\tno entries\n"));
932264Sjacobs 
942264Sjacobs 		if (state == 0x04)
952264Sjacobs 			printf(gettext("\tdaemon present\n"));
962264Sjacobs 
972264Sjacobs 	} else {
982264Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
992264Sjacobs 			verbose_papi_message(svc, status));
1002264Sjacobs 		return (-1);
1012264Sjacobs 	}
1022264Sjacobs 
1032264Sjacobs 	papiPrinterFree(p);
1042264Sjacobs 
1052264Sjacobs 	return (0);
1062264Sjacobs }
1072264Sjacobs 
1082264Sjacobs static int
lpc_abort(papi_service_t svc,char ** args)1092264Sjacobs lpc_abort(papi_service_t svc, char **args)
1102264Sjacobs {
1112264Sjacobs 	papi_status_t status;
1122264Sjacobs 	char *destination = args[1];
1132264Sjacobs 
1142264Sjacobs 	if (destination == NULL) {
1152264Sjacobs 		fprintf(stderr, gettext("Usage: abort (destination)\n"));
1162264Sjacobs 		return (-1);
1172264Sjacobs 	}
1182264Sjacobs 
1192264Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc abort");
1202264Sjacobs 	if (status == PAPI_OK) {
1212264Sjacobs 		printf(gettext("%s: processing disabled after current job\n"),
1222264Sjacobs 			destination);
1232264Sjacobs 	} else {
1242264Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
1252264Sjacobs 			verbose_papi_message(svc, status));
1262264Sjacobs 	}
1272264Sjacobs 
1282264Sjacobs 	return (0);
1292264Sjacobs }
1302264Sjacobs 
1312264Sjacobs static int
lpc_clean(papi_service_t svc,char ** args)1322264Sjacobs lpc_clean(papi_service_t svc, char **args)
1332264Sjacobs {
1342264Sjacobs 	papi_status_t status;
1352264Sjacobs 	papi_job_t *jobs = NULL;
1362264Sjacobs 	char *destination = args[1];
1372264Sjacobs 
1382264Sjacobs 	if (destination == NULL) {
1392264Sjacobs 		fprintf(stderr, gettext("Usage: clean (destination)\n"));
1402264Sjacobs 		return (-1);
1412264Sjacobs 	}
1422264Sjacobs 
1432264Sjacobs 	status = papiPrinterPurgeJobs(svc, destination, &jobs);
1442264Sjacobs 	if (status != PAPI_OK) {
1452264Sjacobs 		fprintf(stderr, gettext("clean: %s: %s\n"), destination,
1462264Sjacobs 			verbose_papi_message(svc, status));
1472264Sjacobs 		return (-1);
1482264Sjacobs 	}
1492264Sjacobs 
1502264Sjacobs 	if (jobs != NULL) {
1512264Sjacobs 		int i;
1522264Sjacobs 
1532264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
1542264Sjacobs 			printf(gettext("\t%s-%d: cancelled\n"), destination,
1552264Sjacobs 				papiJobGetId(jobs[i]));
1562264Sjacobs 
1572264Sjacobs 		papiJobListFree(jobs);
1582264Sjacobs 	}
1592264Sjacobs 
1602264Sjacobs 	return (0);
1612264Sjacobs }
1622264Sjacobs 
1632264Sjacobs static int
lpc_disable(papi_service_t svc,char ** args)1642264Sjacobs lpc_disable(papi_service_t svc, char **args)
1652264Sjacobs {
1662264Sjacobs 	papi_status_t status;
1672264Sjacobs 	char *destination = args[1];
1682264Sjacobs 
1692264Sjacobs 	if (destination == NULL) {
1702264Sjacobs 		fprintf(stderr, gettext("Usage: disable: (destination)\n"));
1712264Sjacobs 		return (-1);
1722264Sjacobs 	}
1732264Sjacobs 
1742264Sjacobs 	status = papiPrinterDisable(svc, destination, NULL);
1752264Sjacobs 	if (status != PAPI_OK) {
1762264Sjacobs 		fprintf(stderr, gettext("disable: %s: %s\n"), destination,
1772264Sjacobs 			verbose_papi_message(svc, status));
1782264Sjacobs 		return (-1);
1792264Sjacobs 	}
1802264Sjacobs 
1812264Sjacobs 	return (0);
1822264Sjacobs }
1832264Sjacobs 
1842264Sjacobs static int
lpc_enable(papi_service_t svc,char ** args)1852264Sjacobs lpc_enable(papi_service_t svc, char **args)
1862264Sjacobs {
1872264Sjacobs 	papi_status_t status;
1882264Sjacobs 	char *destination = args[1];
1892264Sjacobs 
1902264Sjacobs 	if (destination == NULL) {
1912264Sjacobs 		fprintf(stderr, gettext("Usage: enable: (destination)\n"));
1922264Sjacobs 		return (-1);
1932264Sjacobs 	}
1942264Sjacobs 
1952264Sjacobs 	status = papiPrinterEnable(svc, destination);
1962264Sjacobs 	if (status != PAPI_OK) {
1972264Sjacobs 		fprintf(stderr, gettext("enable: %s: %s\n"), destination,
1982264Sjacobs 			verbose_papi_message(svc, status));
1992264Sjacobs 		return (-1);
2002264Sjacobs 	}
2012264Sjacobs 
2022264Sjacobs 	return (0);
2032264Sjacobs }
2042264Sjacobs 
2052264Sjacobs static int
lpc_restart(papi_service_t svc,char ** args)2062264Sjacobs lpc_restart(papi_service_t svc, char **args)
2072264Sjacobs {
2082264Sjacobs 	int rc = 0;
2092264Sjacobs 
2102264Sjacobs 	rc += lpc_disable(svc, args);
2112264Sjacobs 	rc += lpc_enable(svc, args);
2122264Sjacobs 
2132264Sjacobs 	return (rc);
2142264Sjacobs }
2152264Sjacobs 
2162264Sjacobs static int
lpc_start(papi_service_t svc,char ** args)2172264Sjacobs lpc_start(papi_service_t svc, char **args)
2182264Sjacobs {
2192264Sjacobs 	papi_status_t status;
2202264Sjacobs 	char *destination = args[1];
2212264Sjacobs 
2222264Sjacobs 	if (destination == NULL) {
2232264Sjacobs 		fprintf(stderr, gettext("Usage: start (destination)\n"));
2242264Sjacobs 		return (-1);
2252264Sjacobs 	}
2262264Sjacobs 
2272264Sjacobs 	status = papiPrinterResume(svc, destination);
2282264Sjacobs 	if (status != PAPI_OK) {
2292264Sjacobs 		fprintf(stderr, gettext("start: %s: %s\n"), destination,
2302264Sjacobs 			verbose_papi_message(svc, status));
2312264Sjacobs 		return (-1);
2322264Sjacobs 	}
2332264Sjacobs 
2342264Sjacobs 	return (0);
2352264Sjacobs }
2362264Sjacobs 
2372264Sjacobs static int
lpc_stop(papi_service_t svc,char ** args)2382264Sjacobs lpc_stop(papi_service_t svc, char **args)
2392264Sjacobs {
2402264Sjacobs 	papi_status_t status;
2412264Sjacobs 	char *destination = args[1];
2422264Sjacobs 
2432264Sjacobs 	if (destination == NULL) {
2442264Sjacobs 		fprintf(stderr, gettext("Usage: stop (destination)\n"));
2452264Sjacobs 		return (-1);
2462264Sjacobs 	}
2472264Sjacobs 
2482264Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc");
2492264Sjacobs 	if (status != PAPI_OK) {
2502264Sjacobs 		fprintf(stderr, gettext("stop: %s: %s\n"), destination,
2512264Sjacobs 			verbose_papi_message(svc, status));
2522264Sjacobs 		return (-1);
2532264Sjacobs 	}
2542264Sjacobs 
2552264Sjacobs 	return (0);
2562264Sjacobs }
2572264Sjacobs 
2582264Sjacobs static int
lpc_topq(papi_service_t svc,char ** args)2592264Sjacobs lpc_topq(papi_service_t svc, char **args)
2602264Sjacobs {
2612264Sjacobs 	papi_status_t status;
2622264Sjacobs 	char *destination = args[1];
263*9551SKeerthi.Kondaka@Sun.COM 	char *idstr = args[2];
264*9551SKeerthi.Kondaka@Sun.COM 	int32_t id;
2652264Sjacobs 
266*9551SKeerthi.Kondaka@Sun.COM 	if (destination == NULL || idstr == NULL) {
2672264Sjacobs 		fprintf(stderr, gettext("Usage: topq (destination) (id)\n"));
2682264Sjacobs 		return (-1);
2692264Sjacobs 	}
270*9551SKeerthi.Kondaka@Sun.COM 	id = atoi(idstr);
2712264Sjacobs 
2722264Sjacobs 	status = papiJobPromote(svc, destination, id);
2732264Sjacobs 	if (status != PAPI_OK) {
274*9551SKeerthi.Kondaka@Sun.COM 		fprintf(stderr, gettext("topq: %s-%d: %s\n"), destination, id,
275*9551SKeerthi.Kondaka@Sun.COM 		    verbose_papi_message(svc, status));
2762264Sjacobs 		return (-1);
2772264Sjacobs 	}
2782264Sjacobs 
2792264Sjacobs 	return (0);
2802264Sjacobs }
2812264Sjacobs 
2822264Sjacobs static int
lpc_up(papi_service_t svc,char ** args)2832264Sjacobs lpc_up(papi_service_t svc, char **args)
2842264Sjacobs {
2852264Sjacobs 	int rc = 0;
2862264Sjacobs 
2872264Sjacobs 	rc += lpc_enable(svc, args);
2882264Sjacobs 	rc += lpc_start(svc, args);
2892264Sjacobs 
2902264Sjacobs 	return (rc);
2912264Sjacobs }
2922264Sjacobs 
2932264Sjacobs static int
lpc_down(papi_service_t svc,char ** args)2942264Sjacobs lpc_down(papi_service_t svc, char **args)
2952264Sjacobs {
2962264Sjacobs 	int rc = 0;
2972264Sjacobs 
2982264Sjacobs 	rc += lpc_disable(svc, args);
2992264Sjacobs 	rc += lpc_stop(svc, args);
3002264Sjacobs 
3012264Sjacobs 	return (rc);
3022264Sjacobs }
3032264Sjacobs 
3042264Sjacobs static int lpc_help(papi_service_t svc, char **args);	/* forward reference */
3052264Sjacobs 
3062264Sjacobs static char help_help[] = "get help on commands";
3072264Sjacobs static char help_exit[] = "exit lpc";
3082264Sjacobs static char help_status[] = "show status of daemon and queue";
3092264Sjacobs static char help_abort[] =
3102264Sjacobs 		"disable print queue terminating any active job processing";
3112264Sjacobs static char help_clean[] = "remove all jobs from a queue";
3122264Sjacobs static char help_disable[] = "turn off spooling to a queue";
3132264Sjacobs static char help_down[] =
3142264Sjacobs 		"turn off queueing and printing for a queue and set a reason";
3152264Sjacobs static char help_enable[] = "turn on spooling to a queue";
3162264Sjacobs static char help_restart[] = "restart job processing for a queue";
3172264Sjacobs static char help_start[] = "turn on printing from a queue";
3182264Sjacobs static char help_stop[] = "turn off printing from a queue";
3192264Sjacobs static char help_up[] = "turn on queueing and printing for a queue";
3202264Sjacobs static char help_topq[] = "put a job at the top of the queue";
3212264Sjacobs 
3222264Sjacobs static struct {
3232264Sjacobs 	char *cmd;
3242264Sjacobs 	int (*handler)(papi_service_t svc, char **args);
3252264Sjacobs 	char *help_string;
3262264Sjacobs 	int num_args;
3272264Sjacobs } cmd_tab[] = {
3282264Sjacobs 	{ "?",		lpc_help,	help_help,	0 },
3292264Sjacobs 	{ "help",	lpc_help,	help_help,	0 },
3302264Sjacobs 	{ "exit",	lpc_exit,	help_exit,	0 },
3312264Sjacobs 	{ "quit",	lpc_exit,	help_exit,	0 },
3322264Sjacobs 	{ "status",	lpc_status,	help_status,	1 },
3332264Sjacobs 	{ "abort",	lpc_abort,	help_abort,	1 },
3342264Sjacobs 	{ "clean",	lpc_clean,	help_clean,	1 },
3352264Sjacobs 	{ "disable",	lpc_disable,	help_disable,	1 },
3362264Sjacobs 	{ "down",	lpc_down,	help_down,	2 },
3372264Sjacobs 	{ "enable",	lpc_enable,	help_enable,	1 },
3382264Sjacobs 	{ "restart",	lpc_restart,	help_restart,	1 },
3392264Sjacobs 	{ "start",	lpc_start,	help_start,	1 },
3402264Sjacobs 	{ "stop",	lpc_stop,	help_stop,	1 },
3412264Sjacobs 	{ "up",		lpc_up,		help_up,	1 },
3422264Sjacobs 	{ "topq",	lpc_topq,	help_topq,	2 },
3432264Sjacobs 	{ NULL,		NULL,		NULL,		0 }
3442264Sjacobs };
3452264Sjacobs 
3462264Sjacobs static int
lpc_handler(char * cmd,cmd_handler_t ** handler)3472264Sjacobs lpc_handler(char *cmd, cmd_handler_t **handler)
3482264Sjacobs {
3492264Sjacobs 	int i;
3502264Sjacobs 
3512264Sjacobs 	for (i = 0; cmd_tab[i].cmd != NULL; i++)
3522264Sjacobs 		if (strcmp(cmd, cmd_tab[i].cmd) == 0) {
3532264Sjacobs 			*handler = cmd_tab[i].handler;
3542264Sjacobs 			return (cmd_tab[i].num_args);
3552264Sjacobs 		}
3562264Sjacobs 	return (-1);
3572264Sjacobs }
3582264Sjacobs 
3592264Sjacobs static char *
lpc_helptext(char * cmd)3602264Sjacobs lpc_helptext(char *cmd)
3612264Sjacobs {
3622264Sjacobs 	int i;
3632264Sjacobs 
3642264Sjacobs 	for (i = 0; cmd_tab[i].cmd != NULL; i++)
3652264Sjacobs 		if (strcmp(cmd, cmd_tab[i].cmd) == 0)
3662264Sjacobs 			return (gettext(cmd_tab[i].help_string));
3672264Sjacobs 	return (NULL);
3682264Sjacobs }
3692264Sjacobs 
3702264Sjacobs /* ARGSUSED0 */
3712264Sjacobs static int
lpc_help(papi_service_t svc,char ** args)3722264Sjacobs lpc_help(papi_service_t svc, char **args)
3732264Sjacobs {
3742264Sjacobs 	if (args[1] == NULL) {
3752264Sjacobs 		int i;
3762264Sjacobs 
3772264Sjacobs 		printf(gettext("Commands are:\n\n"));
3782264Sjacobs 		for (i = 0; cmd_tab[i].cmd != NULL; i++) {
3792264Sjacobs 			printf("\t%s", cmd_tab[i].cmd);
3802264Sjacobs 			if ((i % 7) == 6)
3812264Sjacobs 				printf("\n");
3822264Sjacobs 		}
3832264Sjacobs 		if ((i % 7) != 6)
3842264Sjacobs 			printf("\n");
3852264Sjacobs 	} else {
3862264Sjacobs 		char *helptext = lpc_helptext(args[1]);
3872264Sjacobs 
3882264Sjacobs 		if (helptext == NULL)
3892264Sjacobs 			helptext = gettext("no such command");
3902264Sjacobs 
3912264Sjacobs 		printf("%s: %s\n", args[1], helptext);
3922264Sjacobs 	}
3932264Sjacobs 
3942264Sjacobs 	return (0);
3952264Sjacobs }
3962264Sjacobs 
3972264Sjacobs static int
process_one(int (* handler)(papi_service_t,char **),char ** av,int expected)3982264Sjacobs process_one(int (*handler)(papi_service_t, char **), char **av, int expected)
3992264Sjacobs {
4002264Sjacobs 	int rc = -1;
4012264Sjacobs 	papi_status_t status = PAPI_OK;
4022264Sjacobs 	papi_service_t svc = NULL;
4032264Sjacobs 	char *printer = av[1];
4042264Sjacobs 
4052264Sjacobs 	if ((printer != NULL) && (expected != 0)) {
4062264Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
4072264Sjacobs 					cli_auth_callback, encryption, NULL);
4082264Sjacobs 		if (status != PAPI_OK) {
4092264Sjacobs 			fprintf(stderr, gettext(
4102264Sjacobs 				"Failed to contact service for %s: %s\n"),
4112264Sjacobs 				printer, verbose_papi_message(svc, status));
4122264Sjacobs 		}
4132264Sjacobs 	}
4142264Sjacobs 
4152264Sjacobs 	if (status == PAPI_OK)
4162264Sjacobs 		rc = handler(svc, av);
4172264Sjacobs 
4182264Sjacobs 	if (svc != NULL)
4192264Sjacobs 		papiServiceDestroy(svc);
4202264Sjacobs 
4212264Sjacobs 	return (rc);
4222264Sjacobs }
4232264Sjacobs 
4242264Sjacobs static int
process_all(int (* handler)(papi_service_t,char **),char ** av,int expected)4252264Sjacobs process_all(int (*handler)(papi_service_t, char **), char **av, int expected)
4262264Sjacobs {
4272264Sjacobs 	papi_status_t status;
4282264Sjacobs 	papi_service_t svc = NULL;
4292264Sjacobs 	char **printers;
4302264Sjacobs 	int rc = 0;
4312264Sjacobs 
4322264Sjacobs 	status = papiServiceCreate(&svc, NULL, NULL, NULL, NULL,
4332264Sjacobs 				encryption, NULL);
4342264Sjacobs 	if (status != PAPI_OK) {
4352264Sjacobs 		fprintf(stderr, gettext("Failed to contact service: %s\n"),
4362264Sjacobs 			verbose_papi_message(svc, status));
4372264Sjacobs 		return (-1);
4382264Sjacobs 	}
4392264Sjacobs 
4402264Sjacobs 	if ((printers = interest_list(svc)) != NULL) {
4412264Sjacobs 		int i;
4422264Sjacobs 
4432264Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
4442264Sjacobs 			av[1] = printers[i];
4452264Sjacobs 			rc += process_one(handler, av, expected);
4462264Sjacobs 		}
4472264Sjacobs 	}
4482264Sjacobs 
4492264Sjacobs 	papiServiceDestroy(svc);
4502264Sjacobs 
4512264Sjacobs 	return (rc);
4522264Sjacobs }
4532264Sjacobs 
4542264Sjacobs static int
process(int ac,char ** av)4552264Sjacobs process(int ac, char **av)
4562264Sjacobs {
4572264Sjacobs 	int (*handler)(papi_service_t, char **) = NULL;
4582264Sjacobs 	int num_args = -1;
4592264Sjacobs 
4602264Sjacobs 	char *printer = av[1];
4612264Sjacobs 	int rc = -1;
4622264Sjacobs 
4632264Sjacobs 	if ((num_args = lpc_handler(av[0], &handler)) < 0) {
4642264Sjacobs 		printf(gettext("%s: invalid command\n"), av[0]);
4652264Sjacobs 		return (-1);
4662264Sjacobs 	}
4672264Sjacobs 
468*9551SKeerthi.Kondaka@Sun.COM 	if (((ac == 0) && (num_args == 1)) ||
4692792Sjacobs 	    ((printer != NULL) && strcmp(printer, "all") == 0))
4702792Sjacobs 		rc = process_all(handler, av, num_args);
4712792Sjacobs 	else if (num_args < ac) {
4722792Sjacobs 		int i;
4732792Sjacobs 		char *argv[4];
4742792Sjacobs 
4752792Sjacobs 		memset(argv, 0, sizeof (argv));
4762792Sjacobs 		argv[0] = av[0];
4772792Sjacobs 
4782792Sjacobs 		if (strcmp(av[0], "topq") == 0) {
4792792Sjacobs 			argv[1] = av[1];
4802792Sjacobs 			for (i = 2; i <= ac; i++) {
4812792Sjacobs 				argv[2] = av[i];
4822792Sjacobs 				process_one(handler, argv, num_args);
4832792Sjacobs 			}
4842792Sjacobs 		} else
4852792Sjacobs 			for (i = 1; i <= ac; i++) {
4862792Sjacobs 				argv[1] = av[i];
4872792Sjacobs 				process_one(handler, argv, num_args);
4882792Sjacobs 			}
4892792Sjacobs 	} else
4902264Sjacobs 		rc = process_one(handler, av, num_args);
4912264Sjacobs 
4922264Sjacobs 	return (rc);
4932264Sjacobs }
4942264Sjacobs 
4952264Sjacobs static void
usage(char * program)4962264Sjacobs usage(char *program)
4972264Sjacobs {
4982264Sjacobs 	char *name;
4992264Sjacobs 
5002264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
5012264Sjacobs 		name = program;
5022264Sjacobs 	else
5032264Sjacobs 		name++;
5042264Sjacobs 
5052264Sjacobs 	fprintf(stdout,
5062264Sjacobs 		gettext("Usage: %s [ command [ parameter...]]\n"),
5072264Sjacobs 		name);
5082264Sjacobs 	exit(1);
5092264Sjacobs }
5102264Sjacobs 
5112264Sjacobs static void
lpc_shell()5122264Sjacobs lpc_shell()
5132264Sjacobs {
5142264Sjacobs 	for (;;) {
5152264Sjacobs 		char line[256];
5162264Sjacobs 		char **av = NULL;
5172264Sjacobs 		int ac = 0;
5182264Sjacobs 
5192264Sjacobs 		/* prompt */
5202264Sjacobs 		fprintf(stdout, "lpc> ");
5212264Sjacobs 		fflush(stdout);
5222264Sjacobs 
5232264Sjacobs 		/* get command */
5242264Sjacobs 		if (fgets(line, sizeof (line), stdin) == NULL)
5252264Sjacobs 			exit(1);
5262264Sjacobs 		if ((av = strsplit(line, " \t\n")) != NULL)
5272264Sjacobs 			for (ac = 0; av[ac] != NULL; ac++);
528*9551SKeerthi.Kondaka@Sun.COM 		else
529*9551SKeerthi.Kondaka@Sun.COM 			continue;
5302264Sjacobs 
531*9551SKeerthi.Kondaka@Sun.COM 		if (ac > 0)
532*9551SKeerthi.Kondaka@Sun.COM 			(void) process(ac - 1, av);
5332264Sjacobs 		free(av);
5342264Sjacobs 	}
5352264Sjacobs }
5362264Sjacobs 
5372264Sjacobs int
main(int ac,char * av[])5382264Sjacobs main(int ac, char *av[])
5392264Sjacobs {
5402264Sjacobs 	int result = 0;
5412264Sjacobs 	int c;
5422264Sjacobs 
5432264Sjacobs 	(void) setlocale(LC_ALL, "");
5442264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
5452264Sjacobs 
5462264Sjacobs 	while ((c = getopt(ac, av, "E")) != EOF)
5472264Sjacobs 		switch (c) {
5482264Sjacobs 		case 'E':
5492264Sjacobs 			encryption = PAPI_ENCRYPT_ALWAYS;
5502264Sjacobs 			break;
5512264Sjacobs 		default:
5522264Sjacobs 			usage(av[0]);
5532264Sjacobs 		}
5542264Sjacobs 
5552264Sjacobs 	if (optind == ac)
5562264Sjacobs 		lpc_shell();
5572264Sjacobs 	else
5582792Sjacobs 		result = process(ac - optind - 1, &av[optind]);
5592264Sjacobs 
5602264Sjacobs 	return (result);
5612264Sjacobs }
562