xref: /titanic_51/usr/src/cmd/print/bsd-sysv-commands/disable.c (revision c389c7f85e1e94d7078f66fbb37362b0387243d0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: disable.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <locale.h>
36 #include <libintl.h>
37 #include <papi.h>
38 #include "common.h"
39 
40 static void
41 usage(char *program)
42 {
43 	char *name;
44 
45 	if ((name = strrchr(program, '/')) == NULL)
46 		name = program;
47 	else
48 		name++;
49 
50 	fprintf(stdout,
51 	    gettext("Usage: %s [-c] [-W] [-r reason] destination ...\n"),
52 	    name);
53 	exit(1);
54 }
55 
56 static void
57 cancel_active_job(papi_service_t svc, char *dest)
58 {
59 	papi_status_t status;
60 	papi_job_t *j = NULL;
61 	char *req_attrs[] = { "job-state", "job-id", NULL };
62 
63 	status = papiPrinterListJobs(svc, dest, req_attrs, 0, 0, &j);
64 	if ((status == PAPI_OK) && (j != NULL)) {
65 		int i;
66 
67 		for (i = 0; j[i] != NULL; j++) {
68 			papi_attribute_t **a = papiJobGetAttributeList(j[i]);
69 			int state = 0;
70 
71 			if (a == NULL)
72 				continue;
73 
74 			(void) papiAttributeListGetInteger(a, NULL,
75 			    "job-state", &state);
76 			if (state & 0x082A) { /* If state is RS_ACTIVE */
77 				int32_t id = papiJobGetId(j[i]);
78 
79 				(void) papiJobCancel(svc, dest, id);
80 			}
81 		}
82 		papiJobListFree(j);
83 	}
84 }
85 
86 int
87 main(int ac, char *av[])
88 {
89 	papi_status_t status;
90 	papi_service_t svc = NULL;
91 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
92 	int exit_status = 0;
93 	int cancel = 0;
94 	int pending = 0;	/* not implemented */
95 	char *reason = NULL;
96 	int c;
97 
98 	(void) setlocale(LC_ALL, "");
99 	(void) textdomain("SUNW_OST_OSCMD");
100 
101 	while ((c = getopt(ac, av, "EcWr:")) != EOF)
102 		switch (c) {
103 		case 'c':	/* cancel active job first */
104 			cancel = 1;
105 			break;
106 		case 'W':	/* wait for active request, not implemented */
107 			pending = 1;
108 			break;
109 		case 'r':	/* reason */
110 			reason = optarg;
111 			break;
112 		case 'E':
113 			encryption = PAPI_ENCRYPT_NEVER;
114 			break;
115 		default:
116 			usage(av[0]);
117 		}
118 
119 	if (ac <= optind)
120 		usage(av[0]);
121 
122 	while (optind < ac) {
123 		char *printer = av[optind++];
124 
125 		status = papiServiceCreate(&svc, printer, NULL, NULL,
126 		    cli_auth_callback, encryption, NULL);
127 		if (status != PAPI_OK) {
128 			fprintf(stderr, gettext(
129 			    "Failed to contact service for %s: %s\n"),
130 			    printer, verbose_papi_message(svc, status));
131 			exit_status = 1;
132 		}
133 
134 		status = papiPrinterDisable(svc, printer, reason);
135 		if (status != PAPI_OK) {
136 			fprintf(stderr, gettext("disable: %s: %s\n"), printer,
137 			    verbose_papi_message(svc, status));
138 			exit_status = 1;
139 		}
140 
141 		if (cancel != 0)
142 			cancel_active_job(svc, printer);
143 
144 		papiServiceDestroy(svc);
145 	}
146 
147 	return (exit_status);
148 }
149