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 /*
232264Sjacobs  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
312264Sjacobs 
322264Sjacobs #include <stdio.h>
332264Sjacobs #include <stdlib.h>
342264Sjacobs #include <unistd.h>
352264Sjacobs #include <string.h>
362264Sjacobs #include <locale.h>
372264Sjacobs #include <libintl.h>
382264Sjacobs #include <papi.h>
392264Sjacobs #include "common.h"
402264Sjacobs 
412264Sjacobs typedef int (cmd_handler_t)(papi_service_t, char **);
422264Sjacobs 
432264Sjacobs static papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
442264Sjacobs 
452264Sjacobs /* ARGSUSED0 */
462264Sjacobs static int
472264Sjacobs lpc_exit(papi_service_t svc, char **args)
482264Sjacobs {
492264Sjacobs 	exit(0);
502264Sjacobs 	/* NOTREACHED */
512264Sjacobs 	return (0);
522264Sjacobs }
532264Sjacobs 
542264Sjacobs static int
552264Sjacobs lpc_status(papi_service_t svc, char **args)
562264Sjacobs {
572264Sjacobs 	papi_status_t status;
582264Sjacobs 	papi_printer_t p = NULL;
592264Sjacobs 	char *pattrs[] = { "printer-state", "printer-state-reasons",
602264Sjacobs 				"printer-is-accepting-jobs", NULL };
612264Sjacobs 	char *destination = args[1];
622264Sjacobs 
632264Sjacobs 	status = papiPrinterQuery(svc, destination, pattrs, NULL, &p);
642264Sjacobs 	if (status == PAPI_OK) {
652264Sjacobs 		papi_attribute_t **list = papiPrinterGetAttributeList(p);
662264Sjacobs 		char accepting = 0;
672264Sjacobs 		int32_t state = 0;
682264Sjacobs 
692264Sjacobs 		printf("%s:\n", destination);
702264Sjacobs 
712264Sjacobs 		(void) papiAttributeListGetBoolean(list, NULL,
722264Sjacobs 				"printer-is-accepting-jobs", &accepting);
732264Sjacobs 		printf(gettext("\tqueueing is %s\n"),
742264Sjacobs 			(accepting ? gettext("enabled") : gettext("disabled")));
752264Sjacobs 
762264Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
772264Sjacobs 					"printer-state", &state);
782264Sjacobs 		printf("\tprinting is %s\n",
792264Sjacobs 			((state != 0x05) ? gettext("enabled") :
802264Sjacobs 				gettext("disabled")));
812264Sjacobs 
822264Sjacobs 		if (state != 0x03) {	/* !idle */
832264Sjacobs 			papi_job_t *jobs = NULL;
842264Sjacobs 			int i = 0;
852264Sjacobs 
862264Sjacobs 			(void) papiPrinterListJobs(svc, destination, NULL,
872264Sjacobs 					PAPI_LIST_JOBS_ALL, 0, &jobs);
882264Sjacobs 			if (jobs != NULL) {
892264Sjacobs 				for (i = 0; jobs[i] != NULL; i++);
902264Sjacobs 				papiJobListFree(jobs);
912264Sjacobs 			}
922264Sjacobs 			printf(gettext("\t%d entries in spool area\n"), i);
932264Sjacobs 		} else
942264Sjacobs 			printf(gettext("\tno entries\n"));
952264Sjacobs 
962264Sjacobs 		if (state == 0x04)
972264Sjacobs 			printf(gettext("\tdaemon present\n"));
982264Sjacobs 
992264Sjacobs 	} else {
1002264Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
1012264Sjacobs 			verbose_papi_message(svc, status));
1022264Sjacobs 		return (-1);
1032264Sjacobs 	}
1042264Sjacobs 
1052264Sjacobs 	papiPrinterFree(p);
1062264Sjacobs 
1072264Sjacobs 	return (0);
1082264Sjacobs }
1092264Sjacobs 
1102264Sjacobs static int
1112264Sjacobs lpc_abort(papi_service_t svc, char **args)
1122264Sjacobs {
1132264Sjacobs 	papi_status_t status;
1142264Sjacobs 	char *destination = args[1];
1152264Sjacobs 
1162264Sjacobs 	if (destination == NULL) {
1172264Sjacobs 		fprintf(stderr, gettext("Usage: abort (destination)\n"));
1182264Sjacobs 		return (-1);
1192264Sjacobs 	}
1202264Sjacobs 
1212264Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc abort");
1222264Sjacobs 	if (status == PAPI_OK) {
1232264Sjacobs 		printf(gettext("%s: processing disabled after current job\n"),
1242264Sjacobs 			destination);
1252264Sjacobs 	} else {
1262264Sjacobs 		fprintf(stderr, "%s: %s\n", destination,
1272264Sjacobs 			verbose_papi_message(svc, status));
1282264Sjacobs 	}
1292264Sjacobs 
1302264Sjacobs 	return (0);
1312264Sjacobs }
1322264Sjacobs 
1332264Sjacobs static int
1342264Sjacobs lpc_clean(papi_service_t svc, char **args)
1352264Sjacobs {
1362264Sjacobs 	papi_status_t status;
1372264Sjacobs 	papi_job_t *jobs = NULL;
1382264Sjacobs 	char *destination = args[1];
1392264Sjacobs 
1402264Sjacobs 	if (destination == NULL) {
1412264Sjacobs 		fprintf(stderr, gettext("Usage: clean (destination)\n"));
1422264Sjacobs 		return (-1);
1432264Sjacobs 	}
1442264Sjacobs 
1452264Sjacobs 	status = papiPrinterPurgeJobs(svc, destination, &jobs);
1462264Sjacobs 	if (status != PAPI_OK) {
1472264Sjacobs 		fprintf(stderr, gettext("clean: %s: %s\n"), destination,
1482264Sjacobs 			verbose_papi_message(svc, status));
1492264Sjacobs 		return (-1);
1502264Sjacobs 	}
1512264Sjacobs 
1522264Sjacobs 	if (jobs != NULL) {
1532264Sjacobs 		int i;
1542264Sjacobs 
1552264Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
1562264Sjacobs 			printf(gettext("\t%s-%d: cancelled\n"), destination,
1572264Sjacobs 				papiJobGetId(jobs[i]));
1582264Sjacobs 
1592264Sjacobs 		papiJobListFree(jobs);
1602264Sjacobs 	}
1612264Sjacobs 
1622264Sjacobs 	return (0);
1632264Sjacobs }
1642264Sjacobs 
1652264Sjacobs static int
1662264Sjacobs lpc_disable(papi_service_t svc, char **args)
1672264Sjacobs {
1682264Sjacobs 	papi_status_t status;
1692264Sjacobs 	char *destination = args[1];
1702264Sjacobs 
1712264Sjacobs 	if (destination == NULL) {
1722264Sjacobs 		fprintf(stderr, gettext("Usage: disable: (destination)\n"));
1732264Sjacobs 		return (-1);
1742264Sjacobs 	}
1752264Sjacobs 
1762264Sjacobs 	status = papiPrinterDisable(svc, destination, NULL);
1772264Sjacobs 	if (status != PAPI_OK) {
1782264Sjacobs 		fprintf(stderr, gettext("disable: %s: %s\n"), destination,
1792264Sjacobs 			verbose_papi_message(svc, status));
1802264Sjacobs 		return (-1);
1812264Sjacobs 	}
1822264Sjacobs 
1832264Sjacobs 	return (0);
1842264Sjacobs }
1852264Sjacobs 
1862264Sjacobs static int
1872264Sjacobs lpc_enable(papi_service_t svc, char **args)
1882264Sjacobs {
1892264Sjacobs 	papi_status_t status;
1902264Sjacobs 	char *destination = args[1];
1912264Sjacobs 
1922264Sjacobs 	if (destination == NULL) {
1932264Sjacobs 		fprintf(stderr, gettext("Usage: enable: (destination)\n"));
1942264Sjacobs 		return (-1);
1952264Sjacobs 	}
1962264Sjacobs 
1972264Sjacobs 	status = papiPrinterEnable(svc, destination);
1982264Sjacobs 	if (status != PAPI_OK) {
1992264Sjacobs 		fprintf(stderr, gettext("enable: %s: %s\n"), destination,
2002264Sjacobs 			verbose_papi_message(svc, status));
2012264Sjacobs 		return (-1);
2022264Sjacobs 	}
2032264Sjacobs 
2042264Sjacobs 	return (0);
2052264Sjacobs }
2062264Sjacobs 
2072264Sjacobs static int
2082264Sjacobs lpc_restart(papi_service_t svc, char **args)
2092264Sjacobs {
2102264Sjacobs 	int rc = 0;
2112264Sjacobs 
2122264Sjacobs 	rc += lpc_disable(svc, args);
2132264Sjacobs 	rc += lpc_enable(svc, args);
2142264Sjacobs 
2152264Sjacobs 	return (rc);
2162264Sjacobs }
2172264Sjacobs 
2182264Sjacobs static int
2192264Sjacobs lpc_start(papi_service_t svc, char **args)
2202264Sjacobs {
2212264Sjacobs 	papi_status_t status;
2222264Sjacobs 	char *destination = args[1];
2232264Sjacobs 
2242264Sjacobs 	if (destination == NULL) {
2252264Sjacobs 		fprintf(stderr, gettext("Usage: start (destination)\n"));
2262264Sjacobs 		return (-1);
2272264Sjacobs 	}
2282264Sjacobs 
2292264Sjacobs 	status = papiPrinterResume(svc, destination);
2302264Sjacobs 	if (status != PAPI_OK) {
2312264Sjacobs 		fprintf(stderr, gettext("start: %s: %s\n"), destination,
2322264Sjacobs 			verbose_papi_message(svc, status));
2332264Sjacobs 		return (-1);
2342264Sjacobs 	}
2352264Sjacobs 
2362264Sjacobs 	return (0);
2372264Sjacobs }
2382264Sjacobs 
2392264Sjacobs static int
2402264Sjacobs lpc_stop(papi_service_t svc, char **args)
2412264Sjacobs {
2422264Sjacobs 	papi_status_t status;
2432264Sjacobs 	char *destination = args[1];
2442264Sjacobs 
2452264Sjacobs 	if (destination == NULL) {
2462264Sjacobs 		fprintf(stderr, gettext("Usage: stop (destination)\n"));
2472264Sjacobs 		return (-1);
2482264Sjacobs 	}
2492264Sjacobs 
2502264Sjacobs 	status = papiPrinterPause(svc, destination, "paused via lpc");
2512264Sjacobs 	if (status != PAPI_OK) {
2522264Sjacobs 		fprintf(stderr, gettext("stop: %s: %s\n"), destination,
2532264Sjacobs 			verbose_papi_message(svc, status));
2542264Sjacobs 		return (-1);
2552264Sjacobs 	}
2562264Sjacobs 
2572264Sjacobs 	return (0);
2582264Sjacobs }
2592264Sjacobs 
2602264Sjacobs static int
2612264Sjacobs lpc_topq(papi_service_t svc, char **args)
2622264Sjacobs {
2632264Sjacobs 	papi_status_t status;
2642264Sjacobs 	char *destination = args[1];
2652264Sjacobs 	int32_t id = atoi(args[2]);
2662264Sjacobs 
2672264Sjacobs 	if (destination == NULL) {
2682264Sjacobs 		fprintf(stderr, gettext("Usage: topq (destination) (id)\n"));
2692264Sjacobs 		return (-1);
2702264Sjacobs 	}
2712264Sjacobs 
2722264Sjacobs 	status = papiJobPromote(svc, destination, id);
2732264Sjacobs 	if (status != PAPI_OK) {
2742264Sjacobs 		fprintf(stderr, gettext("topq: %s %d: %s\n"), destination, id,
2752264Sjacobs 			verbose_papi_message(svc, status));
2762264Sjacobs 		return (-1);
2772264Sjacobs 	}
2782264Sjacobs 
2792264Sjacobs 	return (0);
2802264Sjacobs }
2812264Sjacobs 
2822264Sjacobs static int
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
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
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 *
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
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
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
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
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*2792Sjacobs 	if (((ac == 0) && (num_args != 0)) ||
469*2792Sjacobs 	    ((printer != NULL) && strcmp(printer, "all") == 0))
470*2792Sjacobs 		rc = process_all(handler, av, num_args);
471*2792Sjacobs 	else if (num_args < ac) {
472*2792Sjacobs 		int i;
473*2792Sjacobs 		char *argv[4];
474*2792Sjacobs 
475*2792Sjacobs 		memset(argv, 0, sizeof (argv));
476*2792Sjacobs 		argv[0] = av[0];
477*2792Sjacobs 
478*2792Sjacobs 		if (strcmp(av[0], "topq") == 0) {
479*2792Sjacobs 			argv[1] = av[1];
480*2792Sjacobs 			for (i = 2; i <= ac; i++) {
481*2792Sjacobs 				argv[2] = av[i];
482*2792Sjacobs 				process_one(handler, argv, num_args);
483*2792Sjacobs 			}
484*2792Sjacobs 		} else
485*2792Sjacobs 			for (i = 1; i <= ac; i++) {
486*2792Sjacobs 				argv[1] = av[i];
487*2792Sjacobs 				process_one(handler, argv, num_args);
488*2792Sjacobs 			}
489*2792Sjacobs 	} else
4902264Sjacobs 		rc = process_one(handler, av, num_args);
4912264Sjacobs 
4922264Sjacobs 	return (rc);
4932264Sjacobs }
4942264Sjacobs 
4952264Sjacobs static void
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
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++);
5282264Sjacobs 
529*2792Sjacobs 		(void) process(ac - 1, av);
5302264Sjacobs 		free(av);
5312264Sjacobs 	}
5322264Sjacobs }
5332264Sjacobs 
5342264Sjacobs int
5352264Sjacobs main(int ac, char *av[])
5362264Sjacobs {
5372264Sjacobs 	int result = 0;
5382264Sjacobs 	int c;
5392264Sjacobs 
5402264Sjacobs 	(void) setlocale(LC_ALL, "");
5412264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
5422264Sjacobs 
5432264Sjacobs 	while ((c = getopt(ac, av, "E")) != EOF)
5442264Sjacobs 		switch (c) {
5452264Sjacobs 		case 'E':
5462264Sjacobs 			encryption = PAPI_ENCRYPT_ALWAYS;
5472264Sjacobs 			break;
5482264Sjacobs 		default:
5492264Sjacobs 			usage(av[0]);
5502264Sjacobs 		}
5512264Sjacobs 
5522264Sjacobs 	if (optind == ac)
5532264Sjacobs 		lpc_shell();
5542264Sjacobs 	else
555*2792Sjacobs 		result = process(ac - optind - 1, &av[optind]);
5562264Sjacobs 
5572264Sjacobs 	return (result);
5582264Sjacobs }
559