xref: /titanic_53/usr/src/cmd/print/bsd-sysv-commands/cancel.c (revision f0b87b902adb4cdfe4ad24ea596a7311ddbb2953)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
236b5764c3Ssonam gupta - Sun Microsystems - Bangalore India  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs 
31355b4669Sjacobs #include <stdio.h>
32355b4669Sjacobs #include <stdlib.h>
33355b4669Sjacobs #include <unistd.h>
34355b4669Sjacobs #include <string.h>
35355b4669Sjacobs #include <locale.h>
36355b4669Sjacobs #include <libintl.h>
37355b4669Sjacobs #include <papi.h>
38355b4669Sjacobs #include "common.h"
39355b4669Sjacobs 
40355b4669Sjacobs static void
41355b4669Sjacobs usage(char *program)
42355b4669Sjacobs {
43355b4669Sjacobs 	char *name;
44355b4669Sjacobs 
45355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
46355b4669Sjacobs 		name = program;
47355b4669Sjacobs 	else
48355b4669Sjacobs 		name++;
49355b4669Sjacobs 
50355b4669Sjacobs 	fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name);
51355b4669Sjacobs 	exit(1);
52355b4669Sjacobs }
53355b4669Sjacobs 
54*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India static int32_t
55*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India get_job_id_requested(papi_job_t job) {
56*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 	int32_t rid = -1;
57*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
58*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 	papi_attribute_t **list = papiJobGetAttributeList(job);
59*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 	papiAttributeListGetInteger(list, NULL,
60*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 	    "job-id-requested", &rid);
61*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
62*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 	return (rid);
63*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India }
64*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
65355b4669Sjacobs int
66d6083ba5Sjc144527 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) {
67d6083ba5Sjc144527 
68d6083ba5Sjc144527 	papi_status_t status;
69d6083ba5Sjc144527 	papi_service_t svc = NULL;
70d6083ba5Sjc144527 	char **printers = NULL;
71d6083ba5Sjc144527 	int i, exit_code;
72d6083ba5Sjc144527 
73d6083ba5Sjc144527 	if (pname == NULL) {
746b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		status = papiServiceCreate(&svc, NULL, NULL, NULL,
75d6083ba5Sjc144527 		    cli_auth_callback, encryption, NULL);
76d6083ba5Sjc144527 		printers = interest_list(svc);
77d6083ba5Sjc144527 		papiServiceDestroy(svc);
78d6083ba5Sjc144527 	} else {
79d6083ba5Sjc144527 		list_append(&printers, strdup(pname));
80d6083ba5Sjc144527 	}
81d6083ba5Sjc144527 
82d6083ba5Sjc144527 	if (printers == NULL)
83d6083ba5Sjc144527 		exit(0);
84d6083ba5Sjc144527 
85d6083ba5Sjc144527 	for (i = 0; printers[i] != NULL; i++) {
86d6083ba5Sjc144527 		char *printer = printers[i];
87d6083ba5Sjc144527 
886b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		status = papiServiceCreate(&svc, printer, NULL, NULL,
89d6083ba5Sjc144527 		    cli_auth_callback, encryption, NULL);
90d6083ba5Sjc144527 
91d6083ba5Sjc144527 		if (status != PAPI_OK) {
92d6083ba5Sjc144527 			fprintf(stderr, gettext(
93d6083ba5Sjc144527 			    "Failed to contact service for %s: %s\n"),
94d6083ba5Sjc144527 			    printer, verbose_papi_message(svc, status));
95d6083ba5Sjc144527 			exit(1);
96d6083ba5Sjc144527 		}
97d6083ba5Sjc144527 		exit_code = berkeley_cancel_request(svc, stdout, printer, 1,
98d6083ba5Sjc144527 		    &user);
99d6083ba5Sjc144527 
100d6083ba5Sjc144527 		papiServiceDestroy(svc);
101d6083ba5Sjc144527 		if (exit_code != 0)
102d6083ba5Sjc144527 			break;
103d6083ba5Sjc144527 	}
104d6083ba5Sjc144527 	free(printers);
105d6083ba5Sjc144527 	return (exit_code);
106d6083ba5Sjc144527 }
107d6083ba5Sjc144527 
108d6083ba5Sjc144527 int
109355b4669Sjacobs main(int ac, char *av[])
110355b4669Sjacobs {
111355b4669Sjacobs 	int exit_code = 0;
112355b4669Sjacobs 	char *user = NULL;
113355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
114355b4669Sjacobs 	int c;
115*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 	int32_t rid = 0;
116355b4669Sjacobs 
117355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
118355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
119355b4669Sjacobs 
120d6083ba5Sjc144527 	if (ac == 1)
121d6083ba5Sjc144527 		usage(av[0]);
122d6083ba5Sjc144527 
123355b4669Sjacobs 	while ((c = getopt(ac, av, "Eu:")) != EOF)
124355b4669Sjacobs 		switch (c) {
125355b4669Sjacobs 		case 'E':
126355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
127355b4669Sjacobs 			break;
128355b4669Sjacobs 		case 'u':
129355b4669Sjacobs 			user = optarg;
130355b4669Sjacobs 			break;
131355b4669Sjacobs 		default:
132355b4669Sjacobs 			usage(av[0]);
133355b4669Sjacobs 		}
134355b4669Sjacobs 
135355b4669Sjacobs 	for (c = optind; c < ac; c++) {
136355b4669Sjacobs 		papi_status_t status;
137355b4669Sjacobs 		papi_service_t svc = NULL;
138355b4669Sjacobs 		papi_job_t *jobs = NULL;
139355b4669Sjacobs 		char *printer = NULL;
140355b4669Sjacobs 		int32_t id = -1;
141355b4669Sjacobs 
142355b4669Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
143355b4669Sjacobs 
1446b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		status = papiServiceCreate(&svc, printer, NULL, NULL,
145355b4669Sjacobs 		    cli_auth_callback, encryption, NULL);
146355b4669Sjacobs 		if (status != PAPI_OK) {
147d6083ba5Sjc144527 			fprintf(stderr,
148d6083ba5Sjc144527 			    gettext("Failed to contact service for %s: %s\n"),
149355b4669Sjacobs 			    printer, verbose_papi_message(svc, status));
150355b4669Sjacobs 			exit(1);
151355b4669Sjacobs 		}
152355b4669Sjacobs 
153355b4669Sjacobs #define	OUT	((status == PAPI_OK) ? stdout : stderr)
154355b4669Sjacobs 
155355b4669Sjacobs 		if (id != -1) {	/* it's a job */
156355b4669Sjacobs 			char *mesg = "cancelled";
157355b4669Sjacobs 
158*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			/*
159*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			 * Check if the job-id is job-id-requested
160*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			 * or job-id. If it is job-id-requested then find
161*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			 * corresponding job-id and send it to cancel
162*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			 */
163*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			rid = job_to_be_queried(svc, printer, id);
164*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			if (rid > 0)
165*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 				status = papiJobCancel(svc, printer, rid);
166*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 			else
167355b4669Sjacobs 				status = papiJobCancel(svc, printer, id);
168*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 
169d6083ba5Sjc144527 			if (status == PAPI_NOT_AUTHORIZED) {
170d6083ba5Sjc144527 				mesg = papiStatusString(status);
171d6083ba5Sjc144527 				exit_code = 1;
172d6083ba5Sjc144527 			} else if (status != PAPI_OK) {
173355b4669Sjacobs 				mesg = verbose_papi_message(svc, status);
174355b4669Sjacobs 				exit_code = 1;
175355b4669Sjacobs 			}
176355b4669Sjacobs 			fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
177d6083ba5Sjc144527 
178355b4669Sjacobs 		} else {	/* it's a printer */
179d6083ba5Sjc144527 			if (user == NULL) {
180d6083ba5Sjc144527 
181d6083ba5Sjc144527 				/* Remove first job from printer */
182d6083ba5Sjc144527 
183d6083ba5Sjc144527 				status = papiPrinterListJobs(svc, printer,
184d6083ba5Sjc144527 				    NULL, NULL, 0, &jobs);
185d6083ba5Sjc144527 
186355b4669Sjacobs 				if (status != PAPI_OK) {
187d6083ba5Sjc144527 					fprintf(stderr, gettext(
188d6083ba5Sjc144527 					    "ListJobs %s: %s\n"), printer,
189355b4669Sjacobs 					    verbose_papi_message(svc, status));
190355b4669Sjacobs 					exit_code = 1;
191355b4669Sjacobs 				}
192355b4669Sjacobs 
193d6083ba5Sjc144527 				if (jobs != NULL && *jobs != NULL) {
194d03e4ae8SJonathan Cowper-Andrewes 					char *mesg = "cancelled";
195d03e4ae8SJonathan Cowper-Andrewes 					id = papiJobGetId(*jobs);
196355b4669Sjacobs 
197d03e4ae8SJonathan Cowper-Andrewes 					status = papiJobCancel(svc,
198d03e4ae8SJonathan Cowper-Andrewes 					    printer, id);
199d6083ba5Sjc144527 
200d03e4ae8SJonathan Cowper-Andrewes 					if (status == PAPI_NOT_AUTHORIZED) {
201d03e4ae8SJonathan Cowper-Andrewes 						mesg = papiStatusString(status);
202d03e4ae8SJonathan Cowper-Andrewes 						exit_code = 1;
203d03e4ae8SJonathan Cowper-Andrewes 					} else if (status != PAPI_OK) {
204d03e4ae8SJonathan Cowper-Andrewes 						mesg = verbose_papi_message(
205d03e4ae8SJonathan Cowper-Andrewes 						    svc, status);
206d03e4ae8SJonathan Cowper-Andrewes 						exit_code = 1;
207d03e4ae8SJonathan Cowper-Andrewes 					}
208*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 					/*
209*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 					 * If job-id-requested exists for this
210*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 					 * job-id then that should be displayed
211*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 					 */
212*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 					rid = get_job_id_requested(*jobs);
213*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 					if (rid > 0)
214*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 						fprintf(OUT, "%s-%d: %s\n",
215*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 						    printer, rid, mesg);
216*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 					else
217*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 						fprintf(OUT, "%s-%d: %s\n",
218*f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 						    printer, id, mesg);
219355b4669Sjacobs 				}
220d6083ba5Sjc144527 				papiJobListFree(jobs);
221355b4669Sjacobs 
222d6083ba5Sjc144527 			} else {
223d6083ba5Sjc144527 				/* Purging user's print jobs */
224d6083ba5Sjc144527 				exit_code = cancel_jobs_for_user(user,
225d6083ba5Sjc144527 				    encryption, printer);
226d6083ba5Sjc144527 			}
227d6083ba5Sjc144527 		}
228355b4669Sjacobs 		papiServiceDestroy(svc);
229355b4669Sjacobs 	}
230355b4669Sjacobs 
231d6083ba5Sjc144527 	if (optind == ac)
232d6083ba5Sjc144527 		exit_code = cancel_jobs_for_user(user, encryption, NULL);
233d6083ba5Sjc144527 
234355b4669Sjacobs 	return (exit_code);
235355b4669Sjacobs }
236