8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / print / bsd-sysv-commands / cancel.c
blob23359d9b5ece6fb658daeab568bd3cd5cf323d5f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
27 /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <locale.h>
35 #include <libintl.h>
36 #include <papi.h>
37 #include "common.h"
39 static void
40 usage(char *program)
42 char *name;
44 if ((name = strrchr(program, '/')) == NULL)
45 name = program;
46 else
47 name++;
49 fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name);
50 exit(1);
53 static int32_t
54 get_job_id_requested(papi_job_t job) {
55 int32_t rid = -1;
57 papi_attribute_t **list = papiJobGetAttributeList(job);
58 papiAttributeListGetInteger(list, NULL,
59 "job-id-requested", &rid);
61 return (rid);
64 int
65 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) {
67 papi_status_t status;
68 papi_service_t svc = NULL;
69 char **printers = NULL;
70 int i, exit_code;
72 if (pname == NULL) {
73 status = papiServiceCreate(&svc, NULL, NULL, NULL,
74 cli_auth_callback, encryption, NULL);
75 printers = interest_list(svc);
76 papiServiceDestroy(svc);
77 } else {
78 list_append(&printers, strdup(pname));
81 if (printers == NULL)
82 exit(0);
84 for (i = 0; printers[i] != NULL; i++) {
85 char *printer = printers[i];
87 status = papiServiceCreate(&svc, printer, NULL, NULL,
88 cli_auth_callback, encryption, NULL);
90 if (status != PAPI_OK) {
91 fprintf(stderr, gettext(
92 "Failed to contact service for %s: %s\n"),
93 printer, verbose_papi_message(svc, status));
94 exit(1);
96 exit_code = berkeley_cancel_request(svc, stdout, printer, 1,
97 &user);
99 papiServiceDestroy(svc);
100 if (exit_code != 0)
101 break;
103 free(printers);
104 return (exit_code);
108 main(int ac, char *av[])
110 int exit_code = 0;
111 char *user = NULL;
112 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
113 int c;
114 int32_t rid = -1;
115 int first_dest = 0;
118 (void) setlocale(LC_ALL, "");
119 (void) textdomain("SUNW_OST_OSCMD");
121 if (ac == 1)
122 usage(av[0]);
124 while ((c = getopt(ac, av, "Eu:")) != EOF)
125 switch (c) {
126 case 'E':
127 encryption = PAPI_ENCRYPT_REQUIRED;
128 break;
129 case 'u':
130 user = optarg;
131 break;
132 default:
133 usage(av[0]);
136 for (c = optind; c < ac; c++) {
137 papi_status_t status;
138 papi_service_t svc = NULL;
139 papi_job_t *jobs = NULL;
140 char *printer = NULL;
141 int32_t id = -1;
143 status = papiServiceCreate(&svc, av[c], NULL, NULL,
144 cli_auth_callback, encryption, NULL);
145 if (status != PAPI_OK) {
146 if (first_dest == 0) {
147 (void) get_printer_id(av[c], &printer, &id);
148 status = papiServiceCreate(&svc, printer, NULL,
149 NULL, cli_auth_callback, encryption, NULL);
151 if (status != PAPI_OK) {
152 fprintf(stderr, gettext(
153 "Failed to contact service for %s: %s\n"),
154 printer,
155 verbose_papi_message(svc, status));
156 exit(1);
158 } else {
159 first_dest = 1;
160 printer = av[c];
163 #define OUT ((status == PAPI_OK) ? stdout : stderr)
165 if (id != -1) { /* it's a job */
166 char *mesg = gettext("cancelled");
169 * Check if the job-id is job-id-requested
170 * or job-id. If it is job-id-requested then find
171 * corresponding job-id and send it to cancel
173 rid = job_to_be_queried(svc, printer, id);
174 if (rid < 0) {
176 * Either it is a remote job which cannot be
177 * cancelled based on job-id or job-id is
178 * not found
180 exit_code = 1;
181 fprintf(OUT, "%s-%d: %s\n",
182 printer, id, gettext("not-found"));
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 = gettext(
190 verbose_papi_message(
191 svc, status));
192 exit_code = 1;
194 fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
197 } else { /* it's a printer */
198 if (user == NULL) {
200 /* Remove first job from printer */
202 status = papiPrinterListJobs(svc, printer,
203 NULL, NULL, 0, &jobs);
205 if (status != PAPI_OK) {
206 fprintf(stderr, gettext(
207 "ListJobs %s: %s\n"), printer,
208 verbose_papi_message(svc, status));
209 exit_code = 1;
212 if (jobs != NULL && *jobs != NULL) {
213 char *mesg = gettext("cancelled");
214 id = papiJobGetId(*jobs);
216 status = papiJobCancel(svc,
217 printer, id);
219 if (status == PAPI_NOT_AUTHORIZED) {
220 mesg = papiStatusString(status);
221 exit_code = 1;
222 } else if (status != PAPI_OK) {
223 mesg = gettext(
224 verbose_papi_message(
225 svc, status));
226 exit_code = 1;
229 * If job-id-requested exists for this
230 * job-id then that should be displayed
232 rid = get_job_id_requested(*jobs);
233 if (rid >= 0)
234 fprintf(OUT, "%s-%d: %s\n",
235 printer, rid, mesg);
236 else
237 fprintf(OUT, "%s-%d: %s\n",
238 printer, id, mesg);
240 papiJobListFree(jobs);
242 } else {
243 /* Purging user's print jobs */
244 exit_code = cancel_jobs_for_user(user,
245 encryption, printer);
248 papiServiceDestroy(svc);
251 if (optind == ac)
252 exit_code = cancel_jobs_for_user(user, encryption, NULL);
254 return (exit_code);