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