]> git.ipfire.org Git - thirdparty/cups.git/blame - berkeley/lprm.c
Add --help usage for all Berkeley commands (Issue #5326)
[thirdparty/cups.git] / berkeley / lprm.c
CommitLineData
ef416fc2 1/*
503b54c9 2 * "lprm" command for CUPS.
ef416fc2 3 *
860f3d3b
MS
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
ef416fc2 6 *
860f3d3b
MS
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
ef416fc2 9 */
10
11/*
12 * Include necessary headers...
13 */
14
71e16022 15#include <cups/cups-private.h>
ef416fc2 16
17
860f3d3b
MS
18/*
19 * Local functions...
20 */
21
22static void usage(void) _CUPS_NORETURN;
23
24
ef416fc2 25/*
26 * 'main()' - Parse options and cancel jobs.
27 */
28
29int /* O - Exit status */
30main(int argc, /* I - Number of command-line arguments */
31 char *argv[]) /* I - Command-line arguments */
32{
ef416fc2 33 int i; /* Looping var */
34 int job_id; /* Job ID */
5a738aea 35 const char *name; /* Destination printer */
bdbfacc7
MS
36 char *instance, /* Pointer to instance name */
37 *opt; /* Option pointer */
5a738aea 38 cups_dest_t *dest, /* Destination */
8ca02f3c 39 *defdest; /* Default destination */
5a738aea 40 int did_cancel; /* Did we cancel something? */
ef416fc2 41
42
07725fee 43 _cupsSetLocale(argv);
d09495fa 44
ef416fc2 45 /*
46 * Setup to cancel individual print jobs...
47 */
48
5a738aea
MS
49 did_cancel = 0;
50 defdest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL);
51 name = defdest ? defdest->name : NULL;
8ca02f3c 52
ef416fc2 53 /*
54 * Process command-line arguments...
55 */
56
57 for (i = 1; i < argc; i ++)
bdbfacc7 58 {
860f3d3b
MS
59 if (!strcmp(argv[i], "--help"))
60 usage();
61 else if (argv[i][0] == '-' && argv[i][1] != '\0')
bdbfacc7
MS
62 {
63 for (opt = argv[i] + 1; *opt; opt ++)
ef416fc2 64 {
bdbfacc7
MS
65 switch (*opt)
66 {
67 case 'E' : /* Encrypt */
ef416fc2 68#ifdef HAVE_SSL
bdbfacc7 69 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
ef416fc2 70#else
bdbfacc7 71 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]);
ef416fc2 72#endif /* HAVE_SSL */
bdbfacc7
MS
73 break;
74
75 case 'P' : /* Cancel jobs on a printer */
76 if (opt[1] != '\0')
fa73b229 77 {
bdbfacc7
MS
78 name = opt + 1;
79 opt += strlen(opt) - 1;
80 }
81 else
82 {
83 i ++;
84 name = argv[i];
fa73b229 85 }
86
bdbfacc7
MS
87 if ((instance = strchr(name, '/')) != NULL)
88 *instance = '\0';
fa73b229 89
bdbfacc7 90 if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, name, NULL)) == NULL)
fa73b229 91 {
bdbfacc7 92 _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"."), argv[0], name);
5a738aea 93 goto error;
bdbfacc7
MS
94 }
95
96 cupsFreeDests(1, dest);
97 break;
98
99 case 'U' : /* Username */
100 if (opt[1] != '\0')
101 {
102 cupsSetUser(opt + 1);
103 opt += strlen(opt) - 1;
104 }
105 else
106 {
107 i ++;
108 if (i >= argc)
109 {
110 _cupsLangPrintf(stderr, _("%s: Error - expected username after \"-U\" option."), argv[0]);
860f3d3b 111 usage();
bdbfacc7
MS
112 }
113
114 cupsSetUser(argv[i]);
115 }
116 break;
117
118 case 'h' : /* Connect to host */
119 if (opt[1] != '\0')
120 {
121 cupsSetServer(opt + 1);
122 opt += strlen(opt) - 1;
123 }
fa73b229 124 else
bdbfacc7
MS
125 {
126 i ++;
127
128 if (i >= argc)
129 {
130 _cupsLangPrintf(stderr, _("%s: Error - expected hostname after \"-h\" option."), argv[0]);
860f3d3b 131 usage();
bdbfacc7
MS
132 }
133 else
134 cupsSetServer(argv[i]);
135 }
8ca02f3c 136
bdbfacc7
MS
137 if (defdest)
138 cupsFreeDests(1, defdest);
8ca02f3c 139
bdbfacc7
MS
140 defdest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL);
141 name = defdest ? defdest->name : NULL;
142 break;
fa73b229 143
bdbfacc7
MS
144 default :
145 _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."), argv[0], *opt);
860f3d3b 146 usage();
bdbfacc7 147 }
ef416fc2 148 }
bdbfacc7 149 }
ef416fc2 150 else
151 {
152 /*
153 * Cancel a job or printer...
154 */
155
5a738aea
MS
156 if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, argv[i], NULL)) != NULL)
157 cupsFreeDests(1, dest);
158
159 if (dest)
ef416fc2 160 {
5a738aea
MS
161 name = argv[i];
162 job_id = 0;
163 }
164 else if (isdigit(argv[i][0] & 255))
165 {
166 name = NULL;
ef416fc2 167 job_id = atoi(argv[i]);
168 }
fa73b229 169 else if (!strcmp(argv[i], "-"))
ef416fc2 170 {
171 /*
172 * Cancel all jobs
173 */
174
5a738aea 175 job_id = -1;
ef416fc2 176 }
177 else
178 {
0837b7e8 179 _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"."),
5a738aea
MS
180 argv[0], argv[i]);
181 goto error;
ef416fc2 182 }
183
5a738aea 184 if (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) != IPP_OK)
ef416fc2 185 {
0837b7e8 186 _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
5a738aea 187 goto error;
ef416fc2 188 }
5a738aea
MS
189
190 did_cancel = 1;
ef416fc2 191 }
bdbfacc7 192 }
ef416fc2 193
194 /*
d09495fa 195 * If nothing has been canceled yet, cancel the current job on the specified
ef416fc2 196 * (or default) printer...
197 */
198
5a738aea 199 if (!did_cancel && cupsCancelJob2(CUPS_HTTP_DEFAULT, name, 0, 0) != IPP_OK)
ef416fc2 200 {
0837b7e8 201 _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
5a738aea 202 goto error;
ef416fc2 203 }
204
5a738aea
MS
205 if (defdest)
206 cupsFreeDests(1, defdest);
ef416fc2 207
208 return (0);
5a738aea
MS
209
210 /*
211 * If we get here there was an error, so clean up...
212 */
213
214 error:
215
216 if (defdest)
217 cupsFreeDests(1, defdest);
218
219 return (1);
ef416fc2 220}
860f3d3b
MS
221
222
223/*
224 * 'usage()' - Show program usage and exit.
225 */
226
227static void
228usage(void)
229{
230 _cupsLangPuts(stdout, _("Usage: lprm [options] [id]\n"
231 " lprm [options] -"));
232 _cupsLangPuts(stdout, _("Options:"));
233 _cupsLangPuts(stdout, _("- Cancel all jobs"));
234 _cupsLangPuts(stdout, _("-E Encrypt the connection to the server"));
235 _cupsLangPuts(stdout, _("-h server[:port] Connect to the named server and port"));
236 _cupsLangPuts(stdout, _("-P destination Specify the destination"));
237 _cupsLangPuts(stdout, _("-U username Specify the username to use for authentication"));
238
239 exit(1);
240}