]> 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 5926 2006-09-05 20:45:47Z 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 _cupsSetLocale(argv);
70
71 dest = NULL;
72 dests = NULL;
73 http = NULL;
74 job = NULL;
75 jobid = 0;
76 num_dests = 0;
77 src = NULL;
78
79 for (i = 1; i < argc; i ++)
80 if (argv[i][0] == '-')
81 switch (argv[i][1])
82 {
83 case 'E' : /* Encrypt */
84 #ifdef HAVE_SSL
85 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
86
87 if (http)
88 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
89 #else
90 _cupsLangPrintf(stderr,
91 _("%s: Sorry, no encryption support compiled in!\n"),
92 argv[0]);
93 #endif /* HAVE_SSL */
94 break;
95
96 case 'h' : /* Connect to host */
97 if (http)
98 {
99 httpClose(http);
100 http = NULL;
101 }
102
103 if (argv[i][2] != '\0')
104 cupsSetServer(argv[i] + 2);
105 else
106 {
107 i ++;
108
109 if (i >= argc)
110 {
111 _cupsLangPuts(stderr,
112 _("Error: need hostname after \'-h\' option!\n"));
113 return (1);
114 }
115
116 cupsSetServer(argv[i]);
117 }
118 break;
119
120 default :
121 _cupsLangPrintf(stderr, _("lpmove: Unknown option \'%c\'!\n"),
122 argv[i][1]);
123 return (1);
124 }
125 else if (!jobid && !src)
126 {
127 if (num_dests == 0)
128 num_dests = cupsGetDests(&dests);
129
130 if ((job = strrchr(argv[i], '-')) != NULL &&
131 cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
132 jobid = atoi(job + 1);
133 else
134 src = argv[i];
135 }
136 else if (dest == NULL)
137 dest = argv[i];
138 else
139 {
140 _cupsLangPrintf(stderr, _("lpmove: Unknown argument \'%s\'!\n"),
141 argv[i]);
142 return (1);
143 }
144
145 if ((!jobid && !src) || !dest)
146 {
147 _cupsLangPuts(stdout, _("Usage: lpmove job/src dest\n"));
148 return (1);
149 }
150
151 if (!http)
152 {
153 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
154
155 if (http == NULL)
156 {
157 _cupsLangPrintf(stderr,
158 _("lpmove: Unable to connect to server: %s\n"),
159 strerror(errno));
160 return (1);
161 }
162 }
163
164 return (move_job(http, src, jobid, dest));
165 }
166
167
168 /*
169 * 'move_job()' - Move a job.
170 */
171
172 static int /* O - 0 on success, 1 on error */
173 move_job(http_t *http, /* I - HTTP connection to server */
174 const char *src, /* I - Source queue */
175 int jobid, /* I - Job ID */
176 const char *dest) /* I - Destination queue */
177 {
178 ipp_t *request; /* IPP Request */
179 char job_uri[HTTP_MAX_URI], /* job-uri */
180 printer_uri[HTTP_MAX_URI]; /* job-printer-uri */
181
182
183 if (!http)
184 return (1);
185
186 /*
187 * Build a CUPS_MOVE_JOB request, which requires the following
188 * attributes:
189 *
190 * attributes-charset
191 * attributes-natural-language
192 * job-uri/printer-uri
193 * job-printer-uri
194 * requesting-user-name
195 */
196
197 request = ippNewRequest(CUPS_MOVE_JOB);
198
199 if (jobid)
200 {
201 snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", jobid);
202 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
203 job_uri);
204 }
205 else
206 {
207 httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
208 "localhost", 0, "/printers/%s", src);
209 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
210 job_uri);
211 }
212
213 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
214 NULL, cupsUser());
215
216 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
217 "ipp", NULL, "localhost", 0, "/printers/%s", dest);
218 ippAddString(request, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri",
219 NULL, printer_uri);
220
221 /*
222 * Do the request and get back a response...
223 */
224
225 ippDelete(cupsDoRequest(http, request, "/jobs"));
226
227 if (cupsLastError() > IPP_OK_CONFLICT)
228 {
229 _cupsLangPrintf(stderr, "lpmove: %s\n", cupsLastErrorString());
230 return (1);
231 }
232 else
233 return (0);
234 }
235
236
237 /*
238 * End of "$Id: lpmove.c 5926 2006-09-05 20:45:47Z mike $".
239 */