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