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