]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
503b54c9 | 2 | * "lprm" 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 | * 'main()' - Parse options and cancel jobs. | |
19 | */ | |
20 | ||
21 | int /* O - Exit status */ | |
22 | main(int argc, /* I - Number of command-line arguments */ | |
23 | char *argv[]) /* I - Command-line arguments */ | |
24 | { | |
ef416fc2 | 25 | int i; /* Looping var */ |
26 | int job_id; /* Job ID */ | |
5a738aea | 27 | const char *name; /* Destination printer */ |
bdbfacc7 MS |
28 | char *instance, /* Pointer to instance name */ |
29 | *opt; /* Option pointer */ | |
5a738aea | 30 | cups_dest_t *dest, /* Destination */ |
8ca02f3c | 31 | *defdest; /* Default destination */ |
5a738aea | 32 | int did_cancel; /* Did we cancel something? */ |
ef416fc2 | 33 | |
34 | ||
07725fee | 35 | _cupsSetLocale(argv); |
d09495fa | 36 | |
ef416fc2 | 37 | /* |
38 | * Setup to cancel individual print jobs... | |
39 | */ | |
40 | ||
5a738aea MS |
41 | did_cancel = 0; |
42 | defdest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL); | |
43 | name = defdest ? defdest->name : NULL; | |
8ca02f3c | 44 | |
ef416fc2 | 45 | /* |
46 | * Process command-line arguments... | |
47 | */ | |
48 | ||
49 | for (i = 1; i < argc; i ++) | |
bdbfacc7 | 50 | { |
ef416fc2 | 51 | if (argv[i][0] == '-' && argv[i][1] != '\0') |
bdbfacc7 MS |
52 | { |
53 | for (opt = argv[i] + 1; *opt; opt ++) | |
ef416fc2 | 54 | { |
bdbfacc7 MS |
55 | switch (*opt) |
56 | { | |
57 | case 'E' : /* Encrypt */ | |
ef416fc2 | 58 | #ifdef HAVE_SSL |
bdbfacc7 | 59 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); |
ef416fc2 | 60 | #else |
bdbfacc7 | 61 | _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]); |
ef416fc2 | 62 | #endif /* HAVE_SSL */ |
bdbfacc7 MS |
63 | break; |
64 | ||
65 | case 'P' : /* Cancel jobs on a printer */ | |
66 | if (opt[1] != '\0') | |
fa73b229 | 67 | { |
bdbfacc7 MS |
68 | name = opt + 1; |
69 | opt += strlen(opt) - 1; | |
70 | } | |
71 | else | |
72 | { | |
73 | i ++; | |
74 | name = argv[i]; | |
fa73b229 | 75 | } |
76 | ||
bdbfacc7 MS |
77 | if ((instance = strchr(name, '/')) != NULL) |
78 | *instance = '\0'; | |
fa73b229 | 79 | |
bdbfacc7 | 80 | if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, name, NULL)) == NULL) |
fa73b229 | 81 | { |
bdbfacc7 | 82 | _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"."), argv[0], name); |
5a738aea | 83 | goto error; |
bdbfacc7 MS |
84 | } |
85 | ||
86 | cupsFreeDests(1, dest); | |
87 | break; | |
88 | ||
89 | case 'U' : /* Username */ | |
90 | if (opt[1] != '\0') | |
91 | { | |
92 | cupsSetUser(opt + 1); | |
93 | opt += strlen(opt) - 1; | |
94 | } | |
95 | else | |
96 | { | |
97 | i ++; | |
98 | if (i >= argc) | |
99 | { | |
100 | _cupsLangPrintf(stderr, _("%s: Error - expected username after \"-U\" option."), argv[0]); | |
101 | goto error; | |
102 | } | |
103 | ||
104 | cupsSetUser(argv[i]); | |
105 | } | |
106 | break; | |
107 | ||
108 | case 'h' : /* Connect to host */ | |
109 | if (opt[1] != '\0') | |
110 | { | |
111 | cupsSetServer(opt + 1); | |
112 | opt += strlen(opt) - 1; | |
113 | } | |
fa73b229 | 114 | else |
bdbfacc7 MS |
115 | { |
116 | i ++; | |
117 | ||
118 | if (i >= argc) | |
119 | { | |
120 | _cupsLangPrintf(stderr, _("%s: Error - expected hostname after \"-h\" option."), argv[0]); | |
121 | goto error; | |
122 | } | |
123 | else | |
124 | cupsSetServer(argv[i]); | |
125 | } | |
8ca02f3c | 126 | |
bdbfacc7 MS |
127 | if (defdest) |
128 | cupsFreeDests(1, defdest); | |
8ca02f3c | 129 | |
bdbfacc7 MS |
130 | defdest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL); |
131 | name = defdest ? defdest->name : NULL; | |
132 | break; | |
fa73b229 | 133 | |
bdbfacc7 MS |
134 | default : |
135 | _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."), argv[0], *opt); | |
136 | goto error; | |
137 | } | |
ef416fc2 | 138 | } |
bdbfacc7 | 139 | } |
ef416fc2 | 140 | else |
141 | { | |
142 | /* | |
143 | * Cancel a job or printer... | |
144 | */ | |
145 | ||
5a738aea MS |
146 | if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, argv[i], NULL)) != NULL) |
147 | cupsFreeDests(1, dest); | |
148 | ||
149 | if (dest) | |
ef416fc2 | 150 | { |
5a738aea MS |
151 | name = argv[i]; |
152 | job_id = 0; | |
153 | } | |
154 | else if (isdigit(argv[i][0] & 255)) | |
155 | { | |
156 | name = NULL; | |
ef416fc2 | 157 | job_id = atoi(argv[i]); |
158 | } | |
fa73b229 | 159 | else if (!strcmp(argv[i], "-")) |
ef416fc2 | 160 | { |
161 | /* | |
162 | * Cancel all jobs | |
163 | */ | |
164 | ||
5a738aea | 165 | job_id = -1; |
ef416fc2 | 166 | } |
167 | else | |
168 | { | |
0837b7e8 | 169 | _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"."), |
5a738aea MS |
170 | argv[0], argv[i]); |
171 | goto error; | |
ef416fc2 | 172 | } |
173 | ||
5a738aea | 174 | if (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) != IPP_OK) |
ef416fc2 | 175 | { |
0837b7e8 | 176 | _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString()); |
5a738aea | 177 | goto error; |
ef416fc2 | 178 | } |
5a738aea MS |
179 | |
180 | did_cancel = 1; | |
ef416fc2 | 181 | } |
bdbfacc7 | 182 | } |
ef416fc2 | 183 | |
184 | /* | |
d09495fa | 185 | * If nothing has been canceled yet, cancel the current job on the specified |
ef416fc2 | 186 | * (or default) printer... |
187 | */ | |
188 | ||
5a738aea | 189 | if (!did_cancel && cupsCancelJob2(CUPS_HTTP_DEFAULT, name, 0, 0) != IPP_OK) |
ef416fc2 | 190 | { |
0837b7e8 | 191 | _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString()); |
5a738aea | 192 | goto error; |
ef416fc2 | 193 | } |
194 | ||
5a738aea MS |
195 | if (defdest) |
196 | cupsFreeDests(1, defdest); | |
ef416fc2 | 197 | |
198 | return (0); | |
5a738aea MS |
199 | |
200 | /* | |
201 | * If we get here there was an error, so clean up... | |
202 | */ | |
203 | ||
204 | error: | |
205 | ||
206 | if (defdest) | |
207 | cupsFreeDests(1, defdest); | |
208 | ||
209 | return (1); | |
ef416fc2 | 210 | } |