xref: /illumos-gate/usr/src/cmd/print/bsd-sysv-commands/cancel.c (revision de81e71e031139a0a7f13b7bf64152c3faa76698)
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 = 0;
116 
117 	(void) setlocale(LC_ALL, "");
118 	(void) textdomain("SUNW_OST_OSCMD");
119 
120 	if (ac == 1)
121 		usage(av[0]);
122 
123 	while ((c = getopt(ac, av, "Eu:")) != EOF)
124 		switch (c) {
125 		case 'E':
126 			encryption = PAPI_ENCRYPT_REQUIRED;
127 			break;
128 		case 'u':
129 			user = optarg;
130 			break;
131 		default:
132 			usage(av[0]);
133 		}
134 
135 	for (c = optind; c < ac; c++) {
136 		papi_status_t status;
137 		papi_service_t svc = NULL;
138 		papi_job_t *jobs = NULL;
139 		char *printer = NULL;
140 		int32_t id = -1;
141 
142 		(void) get_printer_id(av[c], &printer, &id);
143 
144 		status = papiServiceCreate(&svc, printer, NULL, NULL,
145 		    cli_auth_callback, encryption, NULL);
146 		if (status != PAPI_OK) {
147 			fprintf(stderr,
148 			    gettext("Failed to contact service for %s: %s\n"),
149 			    printer, verbose_papi_message(svc, status));
150 			exit(1);
151 		}
152 
153 #define	OUT	((status == PAPI_OK) ? stdout : stderr)
154 
155 		if (id != -1) {	/* it's a job */
156 			char *mesg = "cancelled";
157 
158 			/*
159 			 * Check if the job-id is job-id-requested
160 			 * or job-id. If it is job-id-requested then find
161 			 * corresponding job-id and send it to cancel
162 			 */
163 			rid = job_to_be_queried(svc, printer, id);
164 			if (rid > 0)
165 				status = papiJobCancel(svc, printer, rid);
166 			else
167 				status = papiJobCancel(svc, printer, id);
168 
169 			if (status == PAPI_NOT_AUTHORIZED) {
170 				mesg = papiStatusString(status);
171 				exit_code = 1;
172 			} else if (status != PAPI_OK) {
173 				mesg = verbose_papi_message(svc, status);
174 				exit_code = 1;
175 			}
176 			fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
177 
178 		} else {	/* it's a printer */
179 			if (user == NULL) {
180 
181 				/* Remove first job from printer */
182 
183 				status = papiPrinterListJobs(svc, printer,
184 				    NULL, NULL, 0, &jobs);
185 
186 				if (status != PAPI_OK) {
187 					fprintf(stderr, gettext(
188 					    "ListJobs %s: %s\n"), printer,
189 					    verbose_papi_message(svc, status));
190 					exit_code = 1;
191 				}
192 
193 				if (jobs != NULL && *jobs != NULL) {
194 					char *mesg = "cancelled";
195 					id = papiJobGetId(*jobs);
196 
197 					status = papiJobCancel(svc,
198 					    printer, id);
199 
200 					if (status == PAPI_NOT_AUTHORIZED) {
201 						mesg = papiStatusString(status);
202 						exit_code = 1;
203 					} else if (status != PAPI_OK) {
204 						mesg = verbose_papi_message(
205 						    svc, status);
206 						exit_code = 1;
207 					}
208 					/*
209 					 * If job-id-requested exists for this
210 					 * job-id then that should be displayed
211 					 */
212 					rid = get_job_id_requested(*jobs);
213 					if (rid > 0)
214 						fprintf(OUT, "%s-%d: %s\n",
215 						    printer, rid, mesg);
216 					else
217 						fprintf(OUT, "%s-%d: %s\n",
218 						    printer, id, mesg);
219 				}
220 				papiJobListFree(jobs);
221 
222 			} else {
223 				/* Purging user's print jobs */
224 				exit_code = cancel_jobs_for_user(user,
225 				    encryption, printer);
226 			}
227 		}
228 		papiServiceDestroy(svc);
229 	}
230 
231 	if (optind == ac)
232 		exit_code = cancel_jobs_for_user(user, encryption, NULL);
233 
234 	return (exit_code);
235 }
236