]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsaccept.c
Merge changes from CUPS 1.5svn-r9049 (private header support)
[thirdparty/cups.git] / systemv / cupsaccept.c
1 /*
2 * "$Id$"
3 *
4 * "cupsaccept", "cupsdisable", "cupsenable", and "cupsreject" commands for
5 * CUPS.
6 *
7 * Copyright 2007-2010 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products.
9 *
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
15 *
16 * Contents:
17 *
18 * main() - Parse options and accept/reject jobs or disable/enable printers.
19 */
20
21 /*
22 * Include necessary headers...
23 */
24
25 #include <cups/cups-private.h>
26
27
28 /*
29 * 'main()' - Parse options and accept/reject jobs or disable/enable printers.
30 */
31
32 int /* O - Exit status */
33 main(int argc, /* I - Number of command-line arguments */
34 char *argv[]) /* I - Command-line arguments */
35 {
36 int i; /* Looping var */
37 char *command, /* Command to do */
38 uri[1024], /* Printer URI */
39 *reason; /* Reason for reject/disable */
40 ipp_t *request; /* IPP request */
41 ipp_op_t op; /* Operation */
42 int cancel; /* Cancel jobs? */
43
44
45 _cupsSetLocale(argv);
46
47 /*
48 * See what operation we're supposed to do...
49 */
50
51 if ((command = strrchr(argv[0], '/')) != NULL)
52 command ++;
53 else
54 command = argv[0];
55
56 cancel = 0;
57
58 if (!strcmp(command, "cupsaccept") || !strcmp(command, "accept"))
59 op = CUPS_ACCEPT_JOBS;
60 else if (!strcmp(command, "cupsreject") || !strcmp(command, "reject"))
61 op = CUPS_REJECT_JOBS;
62 else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable"))
63 op = IPP_PAUSE_PRINTER;
64 else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable"))
65 op = IPP_RESUME_PRINTER;
66 else
67 {
68 _cupsLangPrintf(stderr, _("%s: Don't know what to do\n"),
69 command);
70 return (1);
71 }
72
73 reason = NULL;
74
75 /*
76 * Process command-line arguments...
77 */
78
79 for (i = 1; i < argc; i ++)
80 if (argv[i][0] == '-')
81 {
82 switch (argv[i][1])
83 {
84 case 'E' : /* Encrypt */
85 #ifdef HAVE_SSL
86 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
87 #else
88 _cupsLangPrintf(stderr,
89 _("%s: Sorry, no encryption support compiled in\n"),
90 command);
91 #endif /* HAVE_SSL */
92 break;
93
94 case 'U' : /* Username */
95 if (argv[i][2] != '\0')
96 cupsSetUser(argv[i] + 2);
97 else
98 {
99 i ++;
100 if (i >= argc)
101 {
102 _cupsLangPrintf(stderr,
103 _("%s: Error - expected username after "
104 "\'-U\' option\n"),
105 command);
106 return (1);
107 }
108
109 cupsSetUser(argv[i]);
110 }
111 break;
112
113 case 'c' : /* Cancel jobs */
114 cancel = 1;
115 break;
116
117 case 'h' : /* Connect to host */
118 if (argv[i][2] != '\0')
119 cupsSetServer(argv[i] + 2);
120 else
121 {
122 i ++;
123 if (i >= argc)
124 {
125 _cupsLangPrintf(stderr,
126 _("%s: Error - expected hostname after "
127 "\'-h\' option\n"),
128 command);
129 return (1);
130 }
131
132 cupsSetServer(argv[i]);
133 }
134 break;
135
136 case 'r' : /* Reason for cancellation */
137 if (argv[i][2] != '\0')
138 reason = argv[i] + 2;
139 else
140 {
141 i ++;
142 if (i >= argc)
143 {
144 _cupsLangPrintf(stderr,
145 _("%s: Error - expected reason text after "
146 "\'-r\' option\n"),
147 command);
148 return (1);
149 }
150
151 reason = argv[i];
152 }
153 break;
154
155 case '-' :
156 if (!strcmp(argv[i], "--hold"))
157 op = IPP_HOLD_NEW_JOBS;
158 else if (!strcmp(argv[i], "--release"))
159 op = IPP_RELEASE_HELD_NEW_JOBS;
160 else
161 {
162 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%s\'\n"),
163 command, argv[i]);
164 return (1);
165 }
166 break;
167
168 default :
169 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'\n"),
170 command, argv[i][1]);
171 return (1);
172 }
173 }
174 else
175 {
176 /*
177 * Accept/disable/enable/reject a destination...
178 */
179
180 request = ippNewRequest(op);
181
182 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
183 "localhost", 0, "/printers/%s", argv[i]);
184 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
185 "printer-uri", NULL, uri);
186
187 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
188 "requesting-user-name", NULL, cupsUser());
189
190 if (reason != NULL)
191 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
192 "printer-state-message", NULL, reason);
193
194 /*
195 * Do the request and get back a response...
196 */
197
198 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
199
200 if (cupsLastError() > IPP_OK_CONFLICT)
201 {
202 _cupsLangPrintf(stderr,
203 _("%s: Operation failed: %s\n"),
204 command, ippErrorString(cupsLastError()));
205 return (1);
206 }
207
208 /*
209 * Cancel all jobs if requested...
210 */
211
212 if (cancel)
213 {
214 /*
215 * Build an IPP_PURGE_JOBS request, which requires the following
216 * attributes:
217 *
218 * attributes-charset
219 * attributes-natural-language
220 * printer-uri
221 */
222
223 request = ippNewRequest(IPP_PURGE_JOBS);
224
225 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
226 "printer-uri", NULL, uri);
227
228 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
229
230 if (cupsLastError() > IPP_OK_CONFLICT)
231 {
232 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
233 return (1);
234 }
235 }
236 }
237
238 return (0);
239 }
240
241
242 /*
243 * End of "$Id$".
244 */