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