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*9257SGowtham.Thommandra@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: disable.c 146 2006-03-24 00:26:54Z njacobs $ */
292264Sjacobs 
302264Sjacobs 
312264Sjacobs #include <stdio.h>
322264Sjacobs #include <stdlib.h>
332264Sjacobs #include <unistd.h>
342264Sjacobs #include <string.h>
352264Sjacobs #include <locale.h>
362264Sjacobs #include <libintl.h>
372264Sjacobs #include <papi.h>
382264Sjacobs #include "common.h"
392264Sjacobs 
402264Sjacobs static void
412264Sjacobs usage(char *program)
422264Sjacobs {
432264Sjacobs 	char *name;
442264Sjacobs 
452264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
462264Sjacobs 		name = program;
472264Sjacobs 	else
482264Sjacobs 		name++;
492264Sjacobs 
502264Sjacobs 	fprintf(stdout,
51*9257SGowtham.Thommandra@Sun.COM 	    gettext("Usage: %s [-c] [-W] [-r reason] destination ...\n"),
52*9257SGowtham.Thommandra@Sun.COM 	    name);
532264Sjacobs 	exit(1);
542264Sjacobs }
552264Sjacobs 
562264Sjacobs static void
572264Sjacobs cancel_active_job(papi_service_t svc, char *dest)
582264Sjacobs {
592264Sjacobs 	papi_status_t status;
602264Sjacobs 	papi_job_t *j = NULL;
612264Sjacobs 	char *req_attrs[] = { "job-state", "job-id", NULL };
622264Sjacobs 
632264Sjacobs 	status = papiPrinterListJobs(svc, dest, req_attrs, 0, 0, &j);
642264Sjacobs 	if ((status == PAPI_OK) && (j != NULL)) {
652264Sjacobs 		int i;
662264Sjacobs 
672264Sjacobs 		for (i = 0; j[i] != NULL; j++) {
682264Sjacobs 			papi_attribute_t **a = papiJobGetAttributeList(j[i]);
692264Sjacobs 			int state = 0;
702264Sjacobs 
712264Sjacobs 			if (a == NULL)
722264Sjacobs 				continue;
732264Sjacobs 
742264Sjacobs 			(void) papiAttributeListGetInteger(a, NULL,
75*9257SGowtham.Thommandra@Sun.COM 			    "job-state", &state);
76*9257SGowtham.Thommandra@Sun.COM 			if (state & 0x082A) { /* If state is RS_ACTIVE */
772264Sjacobs 				int32_t id = papiJobGetId(j[i]);
782264Sjacobs 
792264Sjacobs 				(void) papiJobCancel(svc, dest, id);
802264Sjacobs 			}
812264Sjacobs 		}
822264Sjacobs 		papiJobListFree(j);
832264Sjacobs 	}
842264Sjacobs }
852264Sjacobs 
862264Sjacobs int
872264Sjacobs main(int ac, char *av[])
882264Sjacobs {
892264Sjacobs 	papi_status_t status;
902264Sjacobs 	papi_service_t svc = NULL;
912264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
922264Sjacobs 	int exit_status = 0;
932264Sjacobs 	int cancel = 0;
942264Sjacobs 	int pending = 0;	/* not implemented */
952264Sjacobs 	char *reason = NULL;
962264Sjacobs 	int c;
972264Sjacobs 
982264Sjacobs 	(void) setlocale(LC_ALL, "");
992264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
1002264Sjacobs 
1012264Sjacobs 	while ((c = getopt(ac, av, "EcWr:")) != EOF)
1022264Sjacobs 		switch (c) {
1032264Sjacobs 		case 'c':	/* cancel active job first */
1042264Sjacobs 			cancel = 1;
1052264Sjacobs 			break;
1062264Sjacobs 		case 'W':	/* wait for active request, not implemented */
1072264Sjacobs 			pending = 1;
1082264Sjacobs 			break;
1092264Sjacobs 		case 'r':	/* reason */
1102264Sjacobs 			reason = optarg;
1112264Sjacobs 			break;
1122264Sjacobs 		case 'E':
1132264Sjacobs 			encryption = PAPI_ENCRYPT_NEVER;
1142264Sjacobs 			break;
1152264Sjacobs 		default:
1162264Sjacobs 			usage(av[0]);
1172264Sjacobs 		}
1182264Sjacobs 
1192264Sjacobs 	if (ac <= optind)
1202264Sjacobs 		usage(av[0]);
1212264Sjacobs 
1222264Sjacobs 	while (optind < ac) {
1232264Sjacobs 		char *printer = av[optind++];
1242264Sjacobs 
1252264Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
126*9257SGowtham.Thommandra@Sun.COM 		    cli_auth_callback, encryption, NULL);
1272264Sjacobs 		if (status != PAPI_OK) {
1282264Sjacobs 			fprintf(stderr, gettext(
129*9257SGowtham.Thommandra@Sun.COM 			    "Failed to contact service for %s: %s\n"),
130*9257SGowtham.Thommandra@Sun.COM 			    printer, verbose_papi_message(svc, status));
1312264Sjacobs 			exit_status = 1;
1322264Sjacobs 		}
1332264Sjacobs 
1342264Sjacobs 		status = papiPrinterDisable(svc, printer, reason);
1352264Sjacobs 		if (status != PAPI_OK) {
1362264Sjacobs 			fprintf(stderr, gettext("disable: %s: %s\n"), printer,
137*9257SGowtham.Thommandra@Sun.COM 			    verbose_papi_message(svc, status));
1382264Sjacobs 			exit_status = 1;
1392264Sjacobs 		}
1402264Sjacobs 
1412264Sjacobs 		if (cancel != 0)
1422264Sjacobs 			cancel_active_job(svc, printer);
1432264Sjacobs 
1442264Sjacobs 		papiServiceDestroy(svc);
1452264Sjacobs 	}
1462264Sjacobs 
1472264Sjacobs 	return (exit_status);
1482264Sjacobs }
149