xref: /onnv-gate/usr/src/cmd/print/bsd-sysv-commands/lpmove.c (revision 12427:08c46c2548be)
12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
23*12427SKeerthi.Kondaka@Sun.COM  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
242264Sjacobs  */
252264Sjacobs 
262264Sjacobs /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
272264Sjacobs 
282264Sjacobs #include <stdio.h>
292264Sjacobs #include <stdlib.h>
302264Sjacobs #include <unistd.h>
312264Sjacobs #include <string.h>
322264Sjacobs #include <locale.h>
332264Sjacobs #include <libintl.h>
342264Sjacobs #include <papi.h>
352264Sjacobs #include "common.h"
362264Sjacobs 
372264Sjacobs static void
usage(char * program)382264Sjacobs usage(char *program)
392264Sjacobs {
402264Sjacobs 	char *name;
412264Sjacobs 
422264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
432264Sjacobs 		name = program;
442264Sjacobs 	else
452264Sjacobs 		name++;
462264Sjacobs 
472264Sjacobs 	fprintf(stdout,
489116SNagaraj.Yedathore@Sun.COM 	    gettext("Usage: %s [request-id] (destination)\n"
499116SNagaraj.Yedathore@Sun.COM 	    "       %s (source) (destination)\n"), name, name);
502264Sjacobs 	exit(1);
512264Sjacobs }
522264Sjacobs 
532264Sjacobs static int
move_job(papi_service_t svc,char * src,int32_t id,char * dest)542264Sjacobs move_job(papi_service_t svc, char *src, int32_t id, char *dest)
552264Sjacobs {
562264Sjacobs 	int result = 0;
572264Sjacobs 	papi_status_t status;
582264Sjacobs 	char *mesg = gettext("moved");
592264Sjacobs 
602264Sjacobs 	status = papiJobMove(svc, src, id, dest);
612264Sjacobs 	if (status != PAPI_OK) {
622264Sjacobs 		mesg = (char *)verbose_papi_message(svc, status);
632264Sjacobs 		result = -1;
642264Sjacobs 	}
652264Sjacobs 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
662264Sjacobs 
672264Sjacobs 	return (result);
682264Sjacobs }
692264Sjacobs 
702264Sjacobs int
main(int ac,char * av[])712264Sjacobs main(int ac, char *av[])
722264Sjacobs {
732264Sjacobs 	int exit_code = 0;
742264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
752264Sjacobs 	char *destination = NULL;
762264Sjacobs 	int c;
772264Sjacobs 
782264Sjacobs 	(void) setlocale(LC_ALL, "");
792264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
802264Sjacobs 
812264Sjacobs 	while ((c = getopt(ac, av, "E:")) != EOF)
822264Sjacobs 		switch (c) {
832264Sjacobs 		case 'E':
842264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
852264Sjacobs 			break;
862264Sjacobs 		default:
872264Sjacobs 			usage(av[0]);
882264Sjacobs 		}
892264Sjacobs 
902264Sjacobs 	if (optind >= ac - 1)
912264Sjacobs 		usage(av[0]);
922264Sjacobs 
932264Sjacobs 	destination = av[--ac];
942264Sjacobs 
952264Sjacobs 	for (c = optind; c < ac; c++) {
962264Sjacobs 		papi_status_t status;
972264Sjacobs 		papi_service_t svc = NULL;
982264Sjacobs 		papi_job_t *jobs = NULL;
992264Sjacobs 		char *printer = NULL;
1002264Sjacobs 		int32_t id = -1;
1012264Sjacobs 
1022264Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
1032264Sjacobs 
1042264Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
1059116SNagaraj.Yedathore@Sun.COM 		    cli_auth_callback, encryption, NULL);
1062264Sjacobs 		if (status != PAPI_OK) {
1072264Sjacobs 			fprintf(stderr, gettext(
1089116SNagaraj.Yedathore@Sun.COM 			    "Failed to contact service for %s: %s\n"),
1099116SNagaraj.Yedathore@Sun.COM 			    printer, verbose_papi_message(svc, status));
1102264Sjacobs 			exit(1);
1112264Sjacobs 		}
1122264Sjacobs 
1132264Sjacobs 		if (id != -1) {	/* it's a job */
1142264Sjacobs 			if (move_job(svc, printer, id, destination) < 0)
1152264Sjacobs 				exit_code = 1;
1162264Sjacobs 		} else {	/* it's a printer */
1172264Sjacobs 			char message[128];
1182264Sjacobs 			int count = 0;
1192264Sjacobs 
1202264Sjacobs 			snprintf(message, sizeof (message), "moved jobs to %s",
1219116SNagaraj.Yedathore@Sun.COM 			    destination);
1229282SKeerthi.Kondaka@Sun.COM 			status = papiPrinterPause(svc, printer, message);
1232264Sjacobs 			if (status != PAPI_OK) {
1249118SSonam.Gupta@Sun.COM 				/*
1259118SSonam.Gupta@Sun.COM 				 * If the user is denied the permission
1269118SSonam.Gupta@Sun.COM 				 * to disable then return appropriate msg
1279118SSonam.Gupta@Sun.COM 				 */
1289118SSonam.Gupta@Sun.COM 				char *result = NULL;
1299118SSonam.Gupta@Sun.COM 
1309118SSonam.Gupta@Sun.COM 				result = papiServiceGetStatusMessage(svc);
1319118SSonam.Gupta@Sun.COM 
1329118SSonam.Gupta@Sun.COM 				if (result != NULL) {
1339118SSonam.Gupta@Sun.COM 					/*
1349118SSonam.Gupta@Sun.COM 					 * Check if user is denied
1359118SSonam.Gupta@Sun.COM 					 * the permission
1369118SSonam.Gupta@Sun.COM 					 */
1379118SSonam.Gupta@Sun.COM 					if (strstr(result, "permission denied")
1389118SSonam.Gupta@Sun.COM 					    != NULL) {
1399118SSonam.Gupta@Sun.COM 						/*
1409118SSonam.Gupta@Sun.COM 						 * user is denied
1419118SSonam.Gupta@Sun.COM 						 * permission
1429118SSonam.Gupta@Sun.COM 						 */
143*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr, "UX:lpmove: ");
144*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr,
145*12427SKeerthi.Kondaka@Sun.COM 						    gettext("ERROR: "));
146*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr, gettext("You "
147*12427SKeerthi.Kondaka@Sun.COM 						    "aren't allowed to do"
148*12427SKeerthi.Kondaka@Sun.COM 						    " that."));
149*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr, "\n\t");
150*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr,
151*12427SKeerthi.Kondaka@Sun.COM 						    gettext("TO FIX"));
152*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr, ": ");
153*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr, gettext("You "
154*12427SKeerthi.Kondaka@Sun.COM 						    "must be logged in as "
155*12427SKeerthi.Kondaka@Sun.COM 						    "\"lp\" or \"root\"."));
156*12427SKeerthi.Kondaka@Sun.COM 						fprintf(stderr, "\n");
1579118SSonam.Gupta@Sun.COM 						exit_code = 1;
1589118SSonam.Gupta@Sun.COM 					} else {
1599118SSonam.Gupta@Sun.COM 						fprintf(stderr, gettext(
1609282SKeerthi.Kondaka@Sun.COM 						    "Reject %s: %s\n"),
1619118SSonam.Gupta@Sun.COM 						    printer,
1629118SSonam.Gupta@Sun.COM 						    verbose_papi_message(
1639118SSonam.Gupta@Sun.COM 						    svc, status));
1649118SSonam.Gupta@Sun.COM 						exit_code = 1;
1659118SSonam.Gupta@Sun.COM 					}
1669118SSonam.Gupta@Sun.COM 				} else {
1679118SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
1689282SKeerthi.Kondaka@Sun.COM 					    "Reject %s: %s\n"),
1699118SSonam.Gupta@Sun.COM 					    printer,
1709118SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
1719118SSonam.Gupta@Sun.COM 					exit_code = 1;
1729118SSonam.Gupta@Sun.COM 				}
1739118SSonam.Gupta@Sun.COM 			} else {
1742264Sjacobs 				printf(gettext(
1759118SSonam.Gupta@Sun.COM 				    "destination %s is not accepting"\
1769118SSonam.Gupta@Sun.COM 				    " requests\n"), printer);
1779118SSonam.Gupta@Sun.COM 
1789118SSonam.Gupta@Sun.COM 				status = papiPrinterListJobs(svc, printer, NULL,
1799118SSonam.Gupta@Sun.COM 				    0, 0, &jobs);
1809118SSonam.Gupta@Sun.COM 				if (status != PAPI_OK) {
1819118SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext("Jobs %s:"\
1829118SSonam.Gupta@Sun.COM 					    " %s\n"),
1839118SSonam.Gupta@Sun.COM 					    printer,
1849118SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
1859118SSonam.Gupta@Sun.COM 					exit_code = 1;
1869118SSonam.Gupta@Sun.COM 				}
1872264Sjacobs 
1889118SSonam.Gupta@Sun.COM 				printf(gettext("move in progress ...\n"));
1899118SSonam.Gupta@Sun.COM 				while ((jobs != NULL) && (*jobs != NULL)) {
1909118SSonam.Gupta@Sun.COM 					id = papiJobGetId(*jobs++);
1919118SSonam.Gupta@Sun.COM 					if (move_job(svc, printer,
1929118SSonam.Gupta@Sun.COM 					    id, destination) < 0)
1939118SSonam.Gupta@Sun.COM 						exit_code = 1;
1949118SSonam.Gupta@Sun.COM 					else
1959118SSonam.Gupta@Sun.COM 						count++;
1969118SSonam.Gupta@Sun.COM 				}
1979118SSonam.Gupta@Sun.COM 				printf(gettext(
1989118SSonam.Gupta@Sun.COM 				    "total of %d requests moved"\
1999118SSonam.Gupta@Sun.COM 				    " from %s to %s\n"),
2009118SSonam.Gupta@Sun.COM 				    count, printer, destination);
2019118SSonam.Gupta@Sun.COM 
2029118SSonam.Gupta@Sun.COM 				papiJobListFree(jobs);
2032264Sjacobs 			}
2042264Sjacobs 		}
2052264Sjacobs 
2062264Sjacobs 		papiServiceDestroy(svc);
2072264Sjacobs 	}
2082264Sjacobs 
2092264Sjacobs 	return (exit_code);
2102264Sjacobs }
211