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