xref: /titanic_53/usr/src/cmd/print/bsd-sysv-commands/lpmove.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs 
22*355b4669Sjacobs /*
23*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*355b4669Sjacobs  * Use is subject to license terms.
25*355b4669Sjacobs  *
26*355b4669Sjacobs  */
27*355b4669Sjacobs 
28*355b4669Sjacobs /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*355b4669Sjacobs 
32*355b4669Sjacobs #include <stdio.h>
33*355b4669Sjacobs #include <stdlib.h>
34*355b4669Sjacobs #include <unistd.h>
35*355b4669Sjacobs #include <string.h>
36*355b4669Sjacobs #include <locale.h>
37*355b4669Sjacobs #include <libintl.h>
38*355b4669Sjacobs #include <papi.h>
39*355b4669Sjacobs #include "common.h"
40*355b4669Sjacobs 
41*355b4669Sjacobs static void
42*355b4669Sjacobs usage(char *program)
43*355b4669Sjacobs {
44*355b4669Sjacobs 	char *name;
45*355b4669Sjacobs 
46*355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
47*355b4669Sjacobs 		name = program;
48*355b4669Sjacobs 	else
49*355b4669Sjacobs 		name++;
50*355b4669Sjacobs 
51*355b4669Sjacobs 	fprintf(stdout,
52*355b4669Sjacobs 		gettext("Usage: %s [request-id] (destination)\n"
53*355b4669Sjacobs 			"       %s (source) (destination)\n"), name);
54*355b4669Sjacobs 	exit(1);
55*355b4669Sjacobs }
56*355b4669Sjacobs 
57*355b4669Sjacobs static int
58*355b4669Sjacobs move_job(papi_service_t svc, char *src, int32_t id, char *dest)
59*355b4669Sjacobs {
60*355b4669Sjacobs 	int result = 0;
61*355b4669Sjacobs 	papi_status_t status;
62*355b4669Sjacobs 	char *mesg = gettext("moved");
63*355b4669Sjacobs 
64*355b4669Sjacobs 	status = papiJobMove(svc, src, id, dest);
65*355b4669Sjacobs 	if (status != PAPI_OK) {
66*355b4669Sjacobs 		mesg = (char *)verbose_papi_message(svc, status);
67*355b4669Sjacobs 		result = -1;
68*355b4669Sjacobs 	}
69*355b4669Sjacobs 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
70*355b4669Sjacobs 
71*355b4669Sjacobs 	return (result);
72*355b4669Sjacobs }
73*355b4669Sjacobs 
74*355b4669Sjacobs int
75*355b4669Sjacobs main(int ac, char *av[])
76*355b4669Sjacobs {
77*355b4669Sjacobs 	int exit_code = 0;
78*355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
79*355b4669Sjacobs 	char *destination = NULL;
80*355b4669Sjacobs 	int c;
81*355b4669Sjacobs 
82*355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
83*355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
84*355b4669Sjacobs 
85*355b4669Sjacobs 	while ((c = getopt(ac, av, "E:")) != EOF)
86*355b4669Sjacobs 		switch (c) {
87*355b4669Sjacobs 		case 'E':
88*355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
89*355b4669Sjacobs 			break;
90*355b4669Sjacobs 		default:
91*355b4669Sjacobs 			usage(av[0]);
92*355b4669Sjacobs 		}
93*355b4669Sjacobs 
94*355b4669Sjacobs 	if (optind >= ac - 1)
95*355b4669Sjacobs 		usage(av[0]);
96*355b4669Sjacobs 
97*355b4669Sjacobs 	destination = av[--ac];
98*355b4669Sjacobs 
99*355b4669Sjacobs 	for (c = optind; c < ac; c++) {
100*355b4669Sjacobs 		papi_status_t status;
101*355b4669Sjacobs 		papi_service_t svc = NULL;
102*355b4669Sjacobs 		papi_job_t *jobs = NULL;
103*355b4669Sjacobs 		char *printer = NULL;
104*355b4669Sjacobs 		int32_t id = -1;
105*355b4669Sjacobs 
106*355b4669Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
107*355b4669Sjacobs 
108*355b4669Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
109*355b4669Sjacobs 					cli_auth_callback, encryption, NULL);
110*355b4669Sjacobs 		if (status != PAPI_OK) {
111*355b4669Sjacobs 			fprintf(stderr, gettext(
112*355b4669Sjacobs 				"Failed to contact service for %s: %s\n"),
113*355b4669Sjacobs 				printer, verbose_papi_message(svc, status));
114*355b4669Sjacobs 			exit(1);
115*355b4669Sjacobs 		}
116*355b4669Sjacobs 
117*355b4669Sjacobs 		if (id != -1) {	/* it's a job */
118*355b4669Sjacobs 			if (move_job(svc, printer, id, destination) < 0)
119*355b4669Sjacobs 				exit_code = 1;
120*355b4669Sjacobs 		} else {	/* it's a printer */
121*355b4669Sjacobs 			char message[128];
122*355b4669Sjacobs 			int count = 0;
123*355b4669Sjacobs 
124*355b4669Sjacobs 			snprintf(message, sizeof (message), "moved jobs to %s",
125*355b4669Sjacobs 					destination);
126*355b4669Sjacobs 			status = papiPrinterDisable(svc, printer, message);
127*355b4669Sjacobs 			if (status != PAPI_OK) {
128*355b4669Sjacobs 				fprintf(stderr, gettext("Disable %s: %s\n"),
129*355b4669Sjacobs 					printer,
130*355b4669Sjacobs 					verbose_papi_message(svc, status));
131*355b4669Sjacobs 				exit_code = 1;
132*355b4669Sjacobs 			} else
133*355b4669Sjacobs 				printf(gettext(
134*355b4669Sjacobs 				"destination %s is not accepting requests\n"),
135*355b4669Sjacobs 					printer);
136*355b4669Sjacobs 
137*355b4669Sjacobs 			status = papiPrinterListJobs(svc, printer, NULL,
138*355b4669Sjacobs 							0, 0, &jobs);
139*355b4669Sjacobs 			if (status != PAPI_OK) {
140*355b4669Sjacobs 				fprintf(stderr, gettext("Jobs %s: %s\n"),
141*355b4669Sjacobs 					printer,
142*355b4669Sjacobs 					verbose_papi_message(svc, status));
143*355b4669Sjacobs 				exit_code = 1;
144*355b4669Sjacobs 			}
145*355b4669Sjacobs 
146*355b4669Sjacobs 			printf(gettext("move in progress ...\n"));
147*355b4669Sjacobs 			while ((jobs != NULL) && (*jobs != NULL)) {
148*355b4669Sjacobs 				id = papiJobGetId(*jobs++);
149*355b4669Sjacobs 				if (move_job(svc, printer, id, destination) < 0)
150*355b4669Sjacobs 					exit_code = 1;
151*355b4669Sjacobs 				else
152*355b4669Sjacobs 					count++;
153*355b4669Sjacobs 			}
154*355b4669Sjacobs 			printf(gettext(
155*355b4669Sjacobs 			    "total of %d requests moved from %s to %s\n"),
156*355b4669Sjacobs 			    count, printer, destination);
157*355b4669Sjacobs 
158*355b4669Sjacobs 			papiJobListFree(jobs);
159*355b4669Sjacobs 		}
160*355b4669Sjacobs 
161*355b4669Sjacobs 		papiServiceDestroy(svc);
162*355b4669Sjacobs 	}
163*355b4669Sjacobs 
164*355b4669Sjacobs 	return (exit_code);
165*355b4669Sjacobs }
166