xref: /titanic_53/usr/src/cmd/print/bsd-sysv-commands/lpmove.c (revision 3ac6dcfa0bf841831d4cfde190aa15173622957c)
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 /*
2337acf26aS"Nagaraj Yedathore - Sun Microsystems - Bangalore India"  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #include <stdio.h>
31355b4669Sjacobs #include <stdlib.h>
32355b4669Sjacobs #include <unistd.h>
33355b4669Sjacobs #include <string.h>
34355b4669Sjacobs #include <locale.h>
35355b4669Sjacobs #include <libintl.h>
36355b4669Sjacobs #include <papi.h>
37355b4669Sjacobs #include "common.h"
38355b4669Sjacobs 
39355b4669Sjacobs static void
40355b4669Sjacobs usage(char *program)
41355b4669Sjacobs {
42355b4669Sjacobs 	char *name;
43355b4669Sjacobs 
44355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
45355b4669Sjacobs 		name = program;
46355b4669Sjacobs 	else
47355b4669Sjacobs 		name++;
48355b4669Sjacobs 
49355b4669Sjacobs 	fprintf(stdout,
50355b4669Sjacobs 	    gettext("Usage: %s [request-id] (destination)\n"
5137acf26aS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	    "       %s (source) (destination)\n"), name, name);
52355b4669Sjacobs 	exit(1);
53355b4669Sjacobs }
54355b4669Sjacobs 
55355b4669Sjacobs static int
56355b4669Sjacobs move_job(papi_service_t svc, char *src, int32_t id, char *dest)
57355b4669Sjacobs {
58355b4669Sjacobs 	int result = 0;
59355b4669Sjacobs 	papi_status_t status;
60355b4669Sjacobs 	char *mesg = gettext("moved");
61355b4669Sjacobs 
62355b4669Sjacobs 	status = papiJobMove(svc, src, id, dest);
63355b4669Sjacobs 	if (status != PAPI_OK) {
64355b4669Sjacobs 		mesg = (char *)verbose_papi_message(svc, status);
65355b4669Sjacobs 		result = -1;
66355b4669Sjacobs 	}
67355b4669Sjacobs 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
68355b4669Sjacobs 
69355b4669Sjacobs 	return (result);
70355b4669Sjacobs }
71355b4669Sjacobs 
72355b4669Sjacobs int
73355b4669Sjacobs main(int ac, char *av[])
74355b4669Sjacobs {
75355b4669Sjacobs 	int exit_code = 0;
76355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
77355b4669Sjacobs 	char *destination = NULL;
78355b4669Sjacobs 	int c;
79355b4669Sjacobs 
80355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
81355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
82355b4669Sjacobs 
83355b4669Sjacobs 	while ((c = getopt(ac, av, "E:")) != EOF)
84355b4669Sjacobs 		switch (c) {
85355b4669Sjacobs 		case 'E':
86355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
87355b4669Sjacobs 			break;
88355b4669Sjacobs 		default:
89355b4669Sjacobs 			usage(av[0]);
90355b4669Sjacobs 		}
91355b4669Sjacobs 
92355b4669Sjacobs 	if (optind >= ac - 1)
93355b4669Sjacobs 		usage(av[0]);
94355b4669Sjacobs 
95355b4669Sjacobs 	destination = av[--ac];
96355b4669Sjacobs 
97355b4669Sjacobs 	for (c = optind; c < ac; c++) {
98355b4669Sjacobs 		papi_status_t status;
99355b4669Sjacobs 		papi_service_t svc = NULL;
100355b4669Sjacobs 		papi_job_t *jobs = NULL;
101355b4669Sjacobs 		char *printer = NULL;
102355b4669Sjacobs 		int32_t id = -1;
103355b4669Sjacobs 
104355b4669Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
105355b4669Sjacobs 
106355b4669Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
107355b4669Sjacobs 		    cli_auth_callback, encryption, NULL);
108355b4669Sjacobs 		if (status != PAPI_OK) {
109355b4669Sjacobs 			fprintf(stderr, gettext(
110355b4669Sjacobs 			    "Failed to contact service for %s: %s\n"),
111355b4669Sjacobs 			    printer, verbose_papi_message(svc, status));
112355b4669Sjacobs 			exit(1);
113355b4669Sjacobs 		}
114355b4669Sjacobs 
115355b4669Sjacobs 		if (id != -1) {	/* it's a job */
116355b4669Sjacobs 			if (move_job(svc, printer, id, destination) < 0)
117355b4669Sjacobs 				exit_code = 1;
118355b4669Sjacobs 		} else {	/* it's a printer */
119355b4669Sjacobs 			char message[128];
120355b4669Sjacobs 			int count = 0;
121355b4669Sjacobs 
122355b4669Sjacobs 			snprintf(message, sizeof (message), "moved jobs to %s",
123355b4669Sjacobs 			    destination);
124355b4669Sjacobs 			status = papiPrinterDisable(svc, printer, message);
125355b4669Sjacobs 			if (status != PAPI_OK) {
126*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				/*
127*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				 * If the user is denied the permission
128*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				 * to disable then return appropriate msg
129*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				 */
130*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				char *result = NULL;
131*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 
132*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				result = papiServiceGetStatusMessage(svc);
133*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 
134*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				if (result != NULL) {
135*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					/*
136*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					 * Check if user is denied
137*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					 * the permission
138*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					 */
139*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					if (strstr(result, "permission denied")
140*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    != NULL) {
141*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						/*
142*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						 * user is denied
143*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						 * permission
144*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						 */
145*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						fprintf(stderr, gettext(
146*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    "UX:lpmove: ERROR:"\
147*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    " You aren't allowed"\
148*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    " to do that.\n\t"\
149*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    "  TO FIX: You must"\
150*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    " be logged in as"\
151*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    " \"lp\" or \"root\".\n"));
152355b4669Sjacobs 						exit_code = 1;
153*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					} else {
154*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						fprintf(stderr, gettext(
155*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    "Disable %s: %s\n"),
156*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    printer,
157*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    verbose_papi_message(
158*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						    svc, status));
159*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 						exit_code = 1;
160*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					}
161*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				} else {
162*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					fprintf(stderr, gettext(
163*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    "Disable %s: %s\n"),
164*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    printer,
165*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    verbose_papi_message(svc, status));
166*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					exit_code = 1;
167*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				}
168*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 			} else {
169355b4669Sjacobs 				printf(gettext(
170*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				    "destination %s is not accepting"\
17137acf26aS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 				    " requests\n"), printer);
172355b4669Sjacobs 
173355b4669Sjacobs 				status = papiPrinterListJobs(svc, printer, NULL,
174355b4669Sjacobs 				    0, 0, &jobs);
175355b4669Sjacobs 				if (status != PAPI_OK) {
176*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					fprintf(stderr, gettext("Jobs %s:"\
177*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    " %s\n"),
178*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    printer,
179*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    verbose_papi_message(svc, status));
180355b4669Sjacobs 					exit_code = 1;
181355b4669Sjacobs 				}
182355b4669Sjacobs 
183355b4669Sjacobs 				printf(gettext("move in progress ...\n"));
184355b4669Sjacobs 				while ((jobs != NULL) && (*jobs != NULL)) {
185355b4669Sjacobs 					id = papiJobGetId(*jobs++);
186*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					if (move_job(svc, printer,
187*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 					    id, destination) < 0)
188355b4669Sjacobs 						exit_code = 1;
189355b4669Sjacobs 					else
190355b4669Sjacobs 						count++;
191355b4669Sjacobs 				}
192355b4669Sjacobs 				printf(gettext(
193*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				    "total of %d requests moved"\
194*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 				    " from %s to %s\n"),
195355b4669Sjacobs 				    count, printer, destination);
196355b4669Sjacobs 
197355b4669Sjacobs 				papiJobListFree(jobs);
198355b4669Sjacobs 			}
199*3ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 		}
200355b4669Sjacobs 
201355b4669Sjacobs 		papiServiceDestroy(svc);
202355b4669Sjacobs 	}
203355b4669Sjacobs 
204355b4669Sjacobs 	return (exit_code);
205355b4669Sjacobs }
206