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