]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpmove.c
Load cups into easysw/current.
[thirdparty/cups.git] / systemv / lpmove.c
1 /*
2 * "$Id: lpmove.c 5023 2006-01-29 14:39:44Z mike $"
3 *
4 * "lpmove" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Parse options and move jobs.
27 * move_job() - Move a job.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <cups/string.h>
38 #include <cups/cups.h>
39 #include <cups/i18n.h>
40 #include <cups/debug.h>
41
42
43 /*
44 * Local functions...
45 */
46
47 static int move_job(http_t *http, const char *src, int jobid,
48 const char *dest);
49
50
51 /*
52 * 'main()' - Parse options and show status information.
53 */
54
55 int
56 main(int argc, /* I - Number of command-line arguments */
57 char *argv[]) /* I - Command-line arguments */
58 {
59 int i; /* Looping var */
60 http_t *http; /* Connection to server */
61 const char *job; /* Job name */
62 int jobid; /* Job ID */
63 int num_dests; /* Number of destinations */
64 cups_dest_t *dests; /* Destinations */
65 const char *src, /* Original queue */
66 *dest; /* New destination */
67
68
69 dest = NULL;
70 dests = NULL;
71 http = NULL;
72 job = NULL;
73 jobid = 0;
74 num_dests = 0;
75 src = NULL;
76
77 for (i = 1; i < argc; i ++)
78 if (argv[i][0] == '-')
79 switch (argv[i][1])
80 {
81 case 'E' : /* Encrypt */
82 #ifdef HAVE_SSL
83 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
84
85 if (http)
86 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
87 #else
88 _cupsLangPrintf(stderr,
89 _("%s: Sorry, no encryption support compiled in!\n"),
90 argv[0]);
91 #endif /* HAVE_SSL */
92 break;
93
94 case 'h' : /* Connect to host */
95 if (http)
96 {
97 httpClose(http);
98 http = NULL;
99 }
100
101 if (argv[i][2] != '\0')
102 cupsSetServer(argv[i] + 2);
103 else
104 {
105 i ++;
106
107 if (i >= argc)
108 {
109 _cupsLangPuts(stderr,
110 _("Error: need hostname after \'-h\' option!\n"));
111 return (1);
112 }
113
114 cupsSetServer(argv[i]);
115 }
116 break;
117
118 default :
119 _cupsLangPrintf(stderr, _("lpmove: Unknown option \'%c\'!\n"),
120 argv[i][1]);
121 return (1);
122 }
123 else if (!jobid && !src)
124 {
125 if (num_dests == 0)
126 num_dests = cupsGetDests(&dests);
127
128 if ((job = strrchr(argv[i], '-')) != NULL &&
129 cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
130 jobid = atoi(job + 1);
131 else
132 src = argv[i];
133 }
134 else if (dest == NULL)
135 dest = argv[i];
136 else
137 {
138 _cupsLangPrintf(stderr, _("lpmove: Unknown argument \'%s\'!\n"),
139 argv[i]);
140 return (1);
141 }
142
143 if ((!jobid && !src) || !dest)
144 {
145 _cupsLangPuts(stdout, _("Usage: lpmove job/src dest\n"));
146 return (1);
147 }
148
149 if (!http)
150 {
151 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
152
153 if (http == NULL)
154 {
155 _cupsLangPrintf(stderr,
156 _("lpmove: Unable to connect to server: %s\n"),
157 strerror(errno));
158 return (1);
159 }
160 }
161
162 return (move_job(http, src, jobid, dest));
163 }
164
165
166 /*
167 * 'move_job()' - Move a job.
168 */
169
170 static int /* O - 0 on success, 1 on error */
171 move_job(http_t *http, /* I - HTTP connection to server */
172 const char *src, /* I - Source queue */
173 int jobid, /* I - Job ID */
174 const char *dest) /* I - Destination queue */
175 {
176 ipp_t *request; /* IPP Request */
177 char job_uri[HTTP_MAX_URI], /* job-uri */
178 printer_uri[HTTP_MAX_URI]; /* job-printer-uri */
179
180
181 if (!http)
182 return (1);
183
184 /*
185 * Build a CUPS_MOVE_JOB request, which requires the following
186 * attributes:
187 *
188 * attributes-charset
189 * attributes-natural-language
190 * job-uri/printer-uri
191 * job-printer-uri
192 * requesting-user-name
193 */
194
195 request = ippNewRequest(CUPS_MOVE_JOB);
196
197 if (jobid)
198 {
199 snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", jobid);
200 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
201 job_uri);
202 }
203 else
204 {
205 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
206 "localhost", 0, "/printers/%s", src);
207 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
208 job_uri);
209 }
210
211 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
212 NULL, cupsUser());
213
214 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
215 "ipp", NULL, "localhost", 0, "/printers/%s", dest);
216 ippAddString(request, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri",
217 NULL, printer_uri);
218
219 /*
220 * Do the request and get back a response...
221 */
222
223 ippDelete(cupsDoRequest(http, request, "/jobs"));
224
225 if (cupsLastError() > IPP_OK_CONFLICT)
226 {
227 _cupsLangPrintf(stderr, "lpmove: %s\n", cupsLastErrorString());
228 return (1);
229 }
230 else
231 return (0);
232 }
233
234
235 /*
236 * End of "$Id: lpmove.c 5023 2006-01-29 14:39:44Z mike $".
237 */