xref: /titanic_50/usr/src/cmd/print/bsd-sysv-commands/disable.c (revision 843e19887f64dde75055cf8842fc4db2171eff45)
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 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <locale.h>
37 #include <libintl.h>
38 #include <papi.h>
39 #include "common.h"
40 
41 static void
42 usage(char *program)
43 {
44 	char *name;
45 
46 	if ((name = strrchr(program, '/')) == NULL)
47 		name = program;
48 	else
49 		name++;
50 
51 	fprintf(stdout,
52 		gettext("Usage: %s [-c] [-W] [-r reason] destination ...\n"),
53 		name);
54 	exit(1);
55 }
56 
57 static void
58 cancel_active_job(papi_service_t svc, char *dest)
59 {
60 	papi_status_t status;
61 	papi_job_t *j = NULL;
62 	char *req_attrs[] = { "job-state", "job-id", NULL };
63 
64 	status = papiPrinterListJobs(svc, dest, req_attrs, 0, 0, &j);
65 	if ((status == PAPI_OK) && (j != NULL)) {
66 		int i;
67 
68 		for (i = 0; j[i] != NULL; j++) {
69 			papi_attribute_t **a = papiJobGetAttributeList(j[i]);
70 			int state = 0;
71 
72 			if (a == NULL)
73 				continue;
74 
75 			(void) papiAttributeListGetInteger(a, NULL,
76 					"job-state", &state);
77 			if (state == 0x05) { /* processing */
78 				int32_t id = papiJobGetId(j[i]);
79 
80 				(void) papiJobCancel(svc, dest, id);
81 			}
82 		}
83 		papiJobListFree(j);
84 	}
85 }
86 
87 int
88 main(int ac, char *av[])
89 {
90 	papi_status_t status;
91 	papi_service_t svc = NULL;
92 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
93 	int exit_status = 0;
94 	int cancel = 0;
95 	int pending = 0;	/* not implemented */
96 	char *reason = NULL;
97 	int c;
98 
99 	(void) setlocale(LC_ALL, "");
100 	(void) textdomain("SUNW_OST_OSCMD");
101 
102 	while ((c = getopt(ac, av, "EcWr:")) != EOF)
103 		switch (c) {
104 		case 'c':	/* cancel active job first */
105 			cancel = 1;
106 			break;
107 		case 'W':	/* wait for active request, not implemented */
108 			pending = 1;
109 			break;
110 		case 'r':	/* reason */
111 			reason = optarg;
112 			break;
113 		case 'E':
114 			encryption = PAPI_ENCRYPT_NEVER;
115 			break;
116 		default:
117 			usage(av[0]);
118 		}
119 
120 	if (ac <= optind)
121 		usage(av[0]);
122 
123 	while (optind < ac) {
124 		char *printer = av[optind++];
125 
126 		status = papiServiceCreate(&svc, printer, NULL, NULL,
127 					cli_auth_callback, encryption, NULL);
128 		if (status != PAPI_OK) {
129 			fprintf(stderr, gettext(
130 				"Failed to contact service for %s: %s\n"),
131 				printer, verbose_papi_message(svc, status));
132 			exit_status = 1;
133 		}
134 
135 		status = papiPrinterDisable(svc, printer, reason);
136 		if (status != PAPI_OK) {
137 			fprintf(stderr, gettext("disable: %s: %s\n"), printer,
138 				verbose_papi_message(svc, status));
139 			exit_status = 1;
140 		}
141 
142 		if (cancel != 0)
143 			cancel_active_job(svc, printer);
144 
145 		papiServiceDestroy(svc);
146 	}
147 
148 	return (exit_status);
149 }
150