xref: /titanic_44/usr/src/cmd/print/bsd-sysv-commands/cancel.c (revision a4aeef46cda1835da2b19f8f62b4526de6521e6c)
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 				/*
177 				 * Either it is a remote job which cannot be
178 				 * cancelled based on job-id or job-id is
179 				 * not found
180 				 */
181 				exit_code = 1;
182 				fprintf(OUT, "%s-%d: not-found\n", printer, id);
183 			} else {
184 				status = papiJobCancel(svc, printer, rid);
185 				if (status == PAPI_NOT_AUTHORIZED) {
186 					mesg = papiStatusString(status);
187 					exit_code = 1;
188 				} else if (status != PAPI_OK) {
189 					mesg =
190 					    verbose_papi_message(svc, status);
191 					exit_code = 1;
192 				}
193 				fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
194 			}
195 
196 		} else {	/* it's a printer */
197 			if (user == NULL) {
198 
199 				/* Remove first job from printer */
200 
201 				status = papiPrinterListJobs(svc, printer,
202 				    NULL, NULL, 0, &jobs);
203 
204 				if (status != PAPI_OK) {
205 					fprintf(stderr, gettext(
206 					    "ListJobs %s: %s\n"), printer,
207 					    verbose_papi_message(svc, status));
208 					exit_code = 1;
209 				}
210 
211 				if (jobs != NULL && *jobs != NULL) {
212 					char *mesg = "cancelled";
213 					id = papiJobGetId(*jobs);
214 
215 					status = papiJobCancel(svc,
216 					    printer, id);
217 
218 					if (status == PAPI_NOT_AUTHORIZED) {
219 						mesg = papiStatusString(status);
220 						exit_code = 1;
221 					} else if (status != PAPI_OK) {
222 						mesg = verbose_papi_message(
223 						    svc, status);
224 						exit_code = 1;
225 					}
226 					/*
227 					 * If job-id-requested exists for this
228 					 * job-id then that should be displayed
229 					 */
230 					rid = get_job_id_requested(*jobs);
231 					if (rid >= 0)
232 						fprintf(OUT, "%s-%d: %s\n",
233 						    printer, rid, mesg);
234 					else
235 						fprintf(OUT, "%s-%d: %s\n",
236 						    printer, id, mesg);
237 				}
238 				papiJobListFree(jobs);
239 
240 			} else {
241 				/* Purging user's print jobs */
242 				exit_code = cancel_jobs_for_user(user,
243 				    encryption, printer);
244 			}
245 		}
246 		papiServiceDestroy(svc);
247 	}
248 
249 	if (optind == ac)
250 		exit_code = cancel_jobs_for_user(user, encryption, NULL);
251 
252 	return (exit_code);
253 }
254