xref: /titanic_51/usr/src/cmd/print/bsd-sysv-commands/lpmove.c (revision 37acf26adb79d43bb16f72774829c6f4655d0cc4)
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 2009 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 #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"
38 
39 static void
40 usage(char *program)
41 {
42 	char *name;
43 
44 	if ((name = strrchr(program, '/')) == NULL)
45 		name = program;
46 	else
47 		name++;
48 
49 	fprintf(stdout,
50 	    gettext("Usage: %s [request-id] (destination)\n"
51 	    "       %s (source) (destination)\n"), name, name);
52 	exit(1);
53 }
54 
55 static int
56 move_job(papi_service_t svc, char *src, int32_t id, char *dest)
57 {
58 	int result = 0;
59 	papi_status_t status;
60 	char *mesg = gettext("moved");
61 
62 	status = papiJobMove(svc, src, id, dest);
63 	if (status != PAPI_OK) {
64 		mesg = (char *)verbose_papi_message(svc, status);
65 		result = -1;
66 	}
67 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
68 
69 	return (result);
70 }
71 
72 int
73 main(int ac, char *av[])
74 {
75 	int exit_code = 0;
76 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
77 	char *destination = NULL;
78 	int c;
79 
80 	(void) setlocale(LC_ALL, "");
81 	(void) textdomain("SUNW_OST_OSCMD");
82 
83 	while ((c = getopt(ac, av, "E:")) != EOF)
84 		switch (c) {
85 		case 'E':
86 			encryption = PAPI_ENCRYPT_REQUIRED;
87 			break;
88 		default:
89 			usage(av[0]);
90 		}
91 
92 	if (optind >= ac - 1)
93 		usage(av[0]);
94 
95 	destination = av[--ac];
96 
97 	for (c = optind; c < ac; c++) {
98 		papi_status_t status;
99 		papi_service_t svc = NULL;
100 		papi_job_t *jobs = NULL;
101 		char *printer = NULL;
102 		int32_t id = -1;
103 
104 		(void) get_printer_id(av[c], &printer, &id);
105 
106 		status = papiServiceCreate(&svc, printer, NULL, NULL,
107 		    cli_auth_callback, encryption, NULL);
108 		if (status != PAPI_OK) {
109 			fprintf(stderr, gettext(
110 			    "Failed to contact service for %s: %s\n"),
111 			    printer, verbose_papi_message(svc, status));
112 			exit(1);
113 		}
114 
115 		if (id != -1) {	/* it's a job */
116 			if (move_job(svc, printer, id, destination) < 0)
117 				exit_code = 1;
118 		} else {	/* it's a printer */
119 			char message[128];
120 			int count = 0;
121 
122 			snprintf(message, sizeof (message), "moved jobs to %s",
123 			    destination);
124 			status = papiPrinterDisable(svc, printer, message);
125 			if (status != PAPI_OK) {
126 				fprintf(stderr, gettext("Disable %s: %s\n"),
127 				    printer, verbose_papi_message(svc, status));
128 				exit_code = 1;
129 			} else
130 				printf(gettext(
131 				    "destination %s is not accepting "
132 				    "requests\n"), printer);
133 
134 			status = papiPrinterListJobs(svc, printer, NULL,
135 			    0, 0, &jobs);
136 			if (status != PAPI_OK) {
137 				fprintf(stderr, gettext("Jobs %s: %s\n"),
138 				    printer, verbose_papi_message(svc, status));
139 				exit_code = 1;
140 			}
141 
142 			printf(gettext("move in progress ...\n"));
143 			while ((jobs != NULL) && (*jobs != NULL)) {
144 				id = papiJobGetId(*jobs++);
145 				if (move_job(svc, printer, id, destination) < 0)
146 					exit_code = 1;
147 				else
148 					count++;
149 			}
150 			printf(gettext(
151 			    "total of %d requests moved from %s to %s\n"),
152 			    count, printer, destination);
153 
154 			papiJobListFree(jobs);
155 		}
156 
157 		papiServiceDestroy(svc);
158 	}
159 
160 	return (exit_code);
161 }
162