xref: /titanic_50/usr/src/cmd/print/bsd-sysv-commands/cancel.c (revision d6083ba53b4a1af428be83ca08578cf93a9038c5)
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 /*
23*d6083ba5Sjc144527  * Copyright 2008 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
34355b4669Sjacobs #include <unistd.h>
35355b4669Sjacobs #include <string.h>
36355b4669Sjacobs #include <locale.h>
37355b4669Sjacobs #include <libintl.h>
38355b4669Sjacobs #include <papi.h>
39355b4669Sjacobs #include "common.h"
40355b4669Sjacobs 
41355b4669Sjacobs static void
42355b4669Sjacobs usage(char *program)
43355b4669Sjacobs {
44355b4669Sjacobs 	char *name;
45355b4669Sjacobs 
46355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
47355b4669Sjacobs 		name = program;
48355b4669Sjacobs 	else
49355b4669Sjacobs 		name++;
50355b4669Sjacobs 
51355b4669Sjacobs 	fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name);
52355b4669Sjacobs 	exit(1);
53355b4669Sjacobs }
54355b4669Sjacobs 
55355b4669Sjacobs int
56*d6083ba5Sjc144527 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) {
57*d6083ba5Sjc144527 
58*d6083ba5Sjc144527 	papi_status_t status;
59*d6083ba5Sjc144527 	papi_service_t svc = NULL;
60*d6083ba5Sjc144527 	char **printers = NULL;
61*d6083ba5Sjc144527 	int i, exit_code;
62*d6083ba5Sjc144527 
63*d6083ba5Sjc144527 	if (pname == NULL) {
64*d6083ba5Sjc144527 		status = papiServiceCreate(&svc, NULL, user, NULL,
65*d6083ba5Sjc144527 		    cli_auth_callback, encryption, NULL);
66*d6083ba5Sjc144527 		printers = interest_list(svc);
67*d6083ba5Sjc144527 		papiServiceDestroy(svc);
68*d6083ba5Sjc144527 	} else {
69*d6083ba5Sjc144527 		list_append(&printers, strdup(pname));
70*d6083ba5Sjc144527 	}
71*d6083ba5Sjc144527 
72*d6083ba5Sjc144527 	if (printers == NULL)
73*d6083ba5Sjc144527 		exit(0);
74*d6083ba5Sjc144527 
75*d6083ba5Sjc144527 	for (i = 0; printers[i] != NULL; i++) {
76*d6083ba5Sjc144527 		char *printer = printers[i];
77*d6083ba5Sjc144527 
78*d6083ba5Sjc144527 		status = papiServiceCreate(&svc, printer, user, NULL,
79*d6083ba5Sjc144527 		    cli_auth_callback, encryption, NULL);
80*d6083ba5Sjc144527 
81*d6083ba5Sjc144527 		if (status != PAPI_OK) {
82*d6083ba5Sjc144527 			fprintf(stderr, gettext(
83*d6083ba5Sjc144527 			    "Failed to contact service for %s: %s\n"),
84*d6083ba5Sjc144527 			    printer, verbose_papi_message(svc, status));
85*d6083ba5Sjc144527 			exit(1);
86*d6083ba5Sjc144527 		}
87*d6083ba5Sjc144527 		exit_code = berkeley_cancel_request(svc, stdout, printer, 1,
88*d6083ba5Sjc144527 		    &user);
89*d6083ba5Sjc144527 
90*d6083ba5Sjc144527 		papiServiceDestroy(svc);
91*d6083ba5Sjc144527 		if (exit_code != 0)
92*d6083ba5Sjc144527 			break;
93*d6083ba5Sjc144527 	}
94*d6083ba5Sjc144527 	free(printers);
95*d6083ba5Sjc144527 	return (exit_code);
96*d6083ba5Sjc144527 }
97*d6083ba5Sjc144527 
98*d6083ba5Sjc144527 int
99355b4669Sjacobs main(int ac, char *av[])
100355b4669Sjacobs {
101355b4669Sjacobs 	int exit_code = 0;
102355b4669Sjacobs 	char *user = NULL;
103355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
104355b4669Sjacobs 	int c;
105355b4669Sjacobs 
106355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
107355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
108355b4669Sjacobs 
109*d6083ba5Sjc144527 	if (ac == 1)
110*d6083ba5Sjc144527 		usage(av[0]);
111*d6083ba5Sjc144527 
112355b4669Sjacobs 	while ((c = getopt(ac, av, "Eu:")) != EOF)
113355b4669Sjacobs 		switch (c) {
114355b4669Sjacobs 		case 'E':
115355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
116355b4669Sjacobs 			break;
117355b4669Sjacobs 		case 'u':
118355b4669Sjacobs 			user = optarg;
119355b4669Sjacobs 			break;
120355b4669Sjacobs 		default:
121355b4669Sjacobs 			usage(av[0]);
122355b4669Sjacobs 		}
123355b4669Sjacobs 
124355b4669Sjacobs 	for (c = optind; c < ac; c++) {
125355b4669Sjacobs 		papi_status_t status;
126355b4669Sjacobs 		papi_service_t svc = NULL;
127355b4669Sjacobs 		papi_job_t *jobs = NULL;
128355b4669Sjacobs 		char *printer = NULL;
129355b4669Sjacobs 		int32_t id = -1;
130355b4669Sjacobs 
131355b4669Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
132355b4669Sjacobs 
133355b4669Sjacobs 		status = papiServiceCreate(&svc, printer, user, NULL,
134355b4669Sjacobs 		    cli_auth_callback, encryption, NULL);
135355b4669Sjacobs 		if (status != PAPI_OK) {
136*d6083ba5Sjc144527 			fprintf(stderr,
137*d6083ba5Sjc144527 			    gettext("Failed to contact service for %s: %s\n"),
138355b4669Sjacobs 			    printer, verbose_papi_message(svc, status));
139355b4669Sjacobs 			exit(1);
140355b4669Sjacobs 		}
141355b4669Sjacobs 
142355b4669Sjacobs #define	OUT	((status == PAPI_OK) ? stdout : stderr)
143355b4669Sjacobs 
144355b4669Sjacobs 		if (id != -1) {	/* it's a job */
145355b4669Sjacobs 			char *mesg = "cancelled";
146355b4669Sjacobs 
147355b4669Sjacobs 			status = papiJobCancel(svc, printer, id);
148*d6083ba5Sjc144527 			if (status == PAPI_NOT_AUTHORIZED) {
149*d6083ba5Sjc144527 				mesg = papiStatusString(status);
150*d6083ba5Sjc144527 				exit_code = 1;
151*d6083ba5Sjc144527 			} else if (status != PAPI_OK) {
152355b4669Sjacobs 				mesg = verbose_papi_message(svc, status);
153355b4669Sjacobs 				exit_code = 1;
154355b4669Sjacobs 			}
155355b4669Sjacobs 			fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
156*d6083ba5Sjc144527 
157355b4669Sjacobs 		} else {	/* it's a printer */
158*d6083ba5Sjc144527 			if (user == NULL) {
159*d6083ba5Sjc144527 
160*d6083ba5Sjc144527 				/* Remove first job from printer */
161*d6083ba5Sjc144527 
162*d6083ba5Sjc144527 				status = papiPrinterListJobs(svc, printer,
163*d6083ba5Sjc144527 				    NULL, NULL, 0, &jobs);
164*d6083ba5Sjc144527 
165355b4669Sjacobs 				if (status != PAPI_OK) {
166*d6083ba5Sjc144527 					fprintf(stderr, gettext(
167*d6083ba5Sjc144527 					    "ListJobs %s: %s\n"), printer,
168355b4669Sjacobs 					    verbose_papi_message(svc, status));
169355b4669Sjacobs 					exit_code = 1;
170355b4669Sjacobs 				}
171355b4669Sjacobs 
172*d6083ba5Sjc144527 				if (jobs != NULL && *jobs != NULL) {
173*d6083ba5Sjc144527 					char jobid[32];
174*d6083ba5Sjc144527 					char *jid;
175355b4669Sjacobs 
176*d6083ba5Sjc144527 					snprintf(jobid, sizeof (jobid), "%u",
177*d6083ba5Sjc144527 					    papiJobGetId(*jobs));
178*d6083ba5Sjc144527 
179*d6083ba5Sjc144527 					jid = jobid;
180*d6083ba5Sjc144527 					exit_code = berkeley_cancel_request(svc,
181*d6083ba5Sjc144527 					    stdout, printer, 1, &jid);
182*d6083ba5Sjc144527 
183355b4669Sjacobs 				}
184*d6083ba5Sjc144527 				papiJobListFree(jobs);
185355b4669Sjacobs 
186*d6083ba5Sjc144527 			} else {
187*d6083ba5Sjc144527 				/* Purging user's print jobs */
188*d6083ba5Sjc144527 				exit_code = cancel_jobs_for_user(user,
189*d6083ba5Sjc144527 				    encryption, printer);
190*d6083ba5Sjc144527 			}
191*d6083ba5Sjc144527 		}
192355b4669Sjacobs 		papiServiceDestroy(svc);
193355b4669Sjacobs 	}
194355b4669Sjacobs 
195*d6083ba5Sjc144527 	if (optind == ac)
196*d6083ba5Sjc144527 		exit_code = cancel_jobs_for_user(user, encryption, NULL);
197*d6083ba5Sjc144527 
198355b4669Sjacobs 	return (exit_code);
199355b4669Sjacobs }
200