xref: /titanic_41/usr/src/cmd/print/bsd-sysv-commands/lpmove.c (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <locale.h>
37 #include <libintl.h>
38 #include <papi.h>
39 #include "common.h"
40 
41 static void
42 usage(char *program)
43 {
44 	char *name;
45 
46 	if ((name = strrchr(program, '/')) == NULL)
47 		name = program;
48 	else
49 		name++;
50 
51 	fprintf(stdout,
52 		gettext("Usage: %s [request-id] (destination)\n"
53 			"       %s (source) (destination)\n"), name);
54 	exit(1);
55 }
56 
57 static int
58 move_job(papi_service_t svc, char *src, int32_t id, char *dest)
59 {
60 	int result = 0;
61 	papi_status_t status;
62 	char *mesg = gettext("moved");
63 
64 	status = papiJobMove(svc, src, id, dest);
65 	if (status != PAPI_OK) {
66 		mesg = (char *)verbose_papi_message(svc, status);
67 		result = -1;
68 	}
69 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
70 
71 	return (result);
72 }
73 
74 int
75 main(int ac, char *av[])
76 {
77 	int exit_code = 0;
78 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
79 	char *destination = NULL;
80 	int c;
81 
82 	(void) setlocale(LC_ALL, "");
83 	(void) textdomain("SUNW_OST_OSCMD");
84 
85 	while ((c = getopt(ac, av, "E:")) != EOF)
86 		switch (c) {
87 		case 'E':
88 			encryption = PAPI_ENCRYPT_REQUIRED;
89 			break;
90 		default:
91 			usage(av[0]);
92 		}
93 
94 	if (optind >= ac - 1)
95 		usage(av[0]);
96 
97 	destination = av[--ac];
98 
99 	for (c = optind; c < ac; c++) {
100 		papi_status_t status;
101 		papi_service_t svc = NULL;
102 		papi_job_t *jobs = NULL;
103 		char *printer = NULL;
104 		int32_t id = -1;
105 
106 		(void) get_printer_id(av[c], &printer, &id);
107 
108 		status = papiServiceCreate(&svc, printer, NULL, NULL,
109 					cli_auth_callback, encryption, NULL);
110 		if (status != PAPI_OK) {
111 			fprintf(stderr, gettext(
112 				"Failed to contact service for %s: %s\n"),
113 				printer, verbose_papi_message(svc, status));
114 			exit(1);
115 		}
116 
117 		if (id != -1) {	/* it's a job */
118 			if (move_job(svc, printer, id, destination) < 0)
119 				exit_code = 1;
120 		} else {	/* it's a printer */
121 			char message[128];
122 			int count = 0;
123 
124 			snprintf(message, sizeof (message), "moved jobs to %s",
125 					destination);
126 			status = papiPrinterDisable(svc, printer, message);
127 			if (status != PAPI_OK) {
128 				fprintf(stderr, gettext("Disable %s: %s\n"),
129 					printer,
130 					verbose_papi_message(svc, status));
131 				exit_code = 1;
132 			} else
133 				printf(gettext(
134 				"destination %s is not accepting requests\n"),
135 					printer);
136 
137 			status = papiPrinterListJobs(svc, printer, NULL,
138 							0, 0, &jobs);
139 			if (status != PAPI_OK) {
140 				fprintf(stderr, gettext("Jobs %s: %s\n"),
141 					printer,
142 					verbose_papi_message(svc, status));
143 				exit_code = 1;
144 			}
145 
146 			printf(gettext("move in progress ...\n"));
147 			while ((jobs != NULL) && (*jobs != NULL)) {
148 				id = papiJobGetId(*jobs++);
149 				if (move_job(svc, printer, id, destination) < 0)
150 					exit_code = 1;
151 				else
152 					count++;
153 			}
154 			printf(gettext(
155 			    "total of %d requests moved from %s to %s\n"),
156 			    count, printer, destination);
157 
158 			papiJobListFree(jobs);
159 		}
160 
161 		papiServiceDestroy(svc);
162 	}
163 
164 	return (exit_code);
165 }
166