xref: /titanic_44/usr/src/cmd/print/bsd-sysv-commands/cancel.c (revision 0a0e9771ca0211c15f3ac4466b661c145feeb9e4)
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: cancel.c 147 2006-04-25 16:51:06Z 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, "Usage: %s [-u user] (printer|request-id ...)\n", name);
51 	exit(1);
52 }
53 
54 static int32_t
55 get_job_id_requested(papi_job_t job) {
56 	int32_t rid = -1;
57 
58 	papi_attribute_t **list = papiJobGetAttributeList(job);
59 	papiAttributeListGetInteger(list, NULL,
60 	    "job-id-requested", &rid);
61 
62 	return (rid);
63 }
64 
65 int
66 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) {
67 
68 	papi_status_t status;
69 	papi_service_t svc = NULL;
70 	char **printers = NULL;
71 	int i, exit_code;
72 
73 	if (pname == NULL) {
74 		status = papiServiceCreate(&svc, NULL, NULL, NULL,
75 		    cli_auth_callback, encryption, NULL);
76 		printers = interest_list(svc);
77 		papiServiceDestroy(svc);
78 	} else {
79 		list_append(&printers, strdup(pname));
80 	}
81 
82 	if (printers == NULL)
83 		exit(0);
84 
85 	for (i = 0; printers[i] != NULL; i++) {
86 		char *printer = printers[i];
87 
88 		status = papiServiceCreate(&svc, printer, NULL, NULL,
89 		    cli_auth_callback, encryption, NULL);
90 
91 		if (status != PAPI_OK) {
92 			fprintf(stderr, gettext(
93 			    "Failed to contact service for %s: %s\n"),
94 			    printer, verbose_papi_message(svc, status));
95 			exit(1);
96 		}
97 		exit_code = berkeley_cancel_request(svc, stdout, printer, 1,
98 		    &user);
99 
100 		papiServiceDestroy(svc);
101 		if (exit_code != 0)
102 			break;
103 	}
104 	free(printers);
105 	return (exit_code);
106 }
107 
108 int
109 main(int ac, char *av[])
110 {
111 	int exit_code = 0;
112 	char *user = NULL;
113 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
114 	int c;
115 	int32_t rid = -1;
116 	int first_dest = 0;
117 
118 
119 	(void) setlocale(LC_ALL, "");
120 	(void) textdomain("SUNW_OST_OSCMD");
121 
122 	if (ac == 1)
123 		usage(av[0]);
124 
125 	while ((c = getopt(ac, av, "Eu:")) != EOF)
126 		switch (c) {
127 		case 'E':
128 			encryption = PAPI_ENCRYPT_REQUIRED;
129 			break;
130 		case 'u':
131 			user = optarg;
132 			break;
133 		default:
134 			usage(av[0]);
135 		}
136 
137 	for (c = optind; c < ac; c++) {
138 		papi_status_t status;
139 		papi_service_t svc = NULL;
140 		papi_job_t *jobs = NULL;
141 		char *printer = NULL;
142 		int32_t id = -1;
143 
144 		status = papiServiceCreate(&svc, av[c], NULL, NULL,
145 		    cli_auth_callback, encryption, NULL);
146 		if (status != PAPI_OK) {
147 			if (first_dest == 0) {
148 				(void) get_printer_id(av[c], &printer, &id);
149 				status = papiServiceCreate(&svc, printer, NULL,
150 				    NULL, cli_auth_callback, encryption, NULL);
151 			}
152 			if (status != PAPI_OK) {
153 				fprintf(stderr,
154 				    gettext("Failed to contact service for %s:"
155 				    " %s\n"), printer,
156 				    verbose_papi_message(svc, status));
157 				exit(1);
158 			}
159 		} else {
160 			first_dest = 1;
161 			printer = av[c];
162 		}
163 
164 #define	OUT	((status == PAPI_OK) ? stdout : stderr)
165 
166 		if (id != -1) {	/* it's a job */
167 			char *mesg = "cancelled";
168 
169 			/*
170 			 * Check if the job-id is job-id-requested
171 			 * or job-id. If it is job-id-requested then find
172 			 * corresponding job-id and send it to cancel
173 			 */
174 			rid = job_to_be_queried(svc, printer, id);
175 			if (rid >= 0)
176 				status = papiJobCancel(svc, printer, rid);
177 			else
178 				status = papiJobCancel(svc, printer, id);
179 
180 			if (status == PAPI_NOT_AUTHORIZED) {
181 				mesg = papiStatusString(status);
182 				exit_code = 1;
183 			} else if (status != PAPI_OK) {
184 				mesg = verbose_papi_message(svc, status);
185 				exit_code = 1;
186 			}
187 			fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
188 
189 		} else {	/* it's a printer */
190 			if (user == NULL) {
191 
192 				/* Remove first job from printer */
193 
194 				status = papiPrinterListJobs(svc, printer,
195 				    NULL, NULL, 0, &jobs);
196 
197 				if (status != PAPI_OK) {
198 					fprintf(stderr, gettext(
199 					    "ListJobs %s: %s\n"), printer,
200 					    verbose_papi_message(svc, status));
201 					exit_code = 1;
202 				}
203 
204 				if (jobs != NULL && *jobs != NULL) {
205 					char *mesg = "cancelled";
206 					id = papiJobGetId(*jobs);
207 
208 					status = papiJobCancel(svc,
209 					    printer, id);
210 
211 					if (status == PAPI_NOT_AUTHORIZED) {
212 						mesg = papiStatusString(status);
213 						exit_code = 1;
214 					} else if (status != PAPI_OK) {
215 						mesg = verbose_papi_message(
216 						    svc, status);
217 						exit_code = 1;
218 					}
219 					/*
220 					 * If job-id-requested exists for this
221 					 * job-id then that should be displayed
222 					 */
223 					rid = get_job_id_requested(*jobs);
224 					if (rid >= 0)
225 						fprintf(OUT, "%s-%d: %s\n",
226 						    printer, rid, mesg);
227 					else
228 						fprintf(OUT, "%s-%d: %s\n",
229 						    printer, id, mesg);
230 				}
231 				papiJobListFree(jobs);
232 
233 			} else {
234 				/* Purging user's print jobs */
235 				exit_code = cancel_jobs_for_user(user,
236 				    encryption, printer);
237 			}
238 		}
239 		papiServiceDestroy(svc);
240 	}
241 
242 	if (optind == ac)
243 		exit_code = cancel_jobs_for_user(user, encryption, NULL);
244 
245 	return (exit_code);
246 }
247