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