2 * "$Id: cancel.c,v 1.23 2002/05/16 13:45:04 mike Exp $"
4 * "cancel" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2002 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Parse options and cancel jobs.
30 * Include necessary headers...
38 #include <cups/cups.h>
39 #include <cups/string.h>
40 #include <cups/language.h>
44 * 'main()' - Parse options and cancel jobs.
47 int /* O - Exit status */
48 main(int argc
, /* I - Number of command-line arguments */
49 char *argv
[]) /* I - Command-line arguments */
51 http_t
*http
; /* HTTP connection to server */
52 int i
; /* Looping var */
53 int job_id
; /* Job ID */
54 char *dest
, /* Destination printer */
55 *host
, /* Host name */
56 *job
; /* Job ID pointer */
57 char name
[255]; /* Printer name */
58 char uri
[1024]; /* Printer or job URI */
59 ipp_t
*request
; /* IPP request */
60 ipp_t
*response
; /* IPP response */
61 ipp_op_t op
; /* Operation */
62 cups_lang_t
*language
; /* Language */
63 http_encryption_t encryption
; /* Encryption? */
67 * Setup to cancel individual print jobs...
74 encryption
= cupsEncryption();
77 * Process command-line arguments...
80 for (i
= 1; i
< argc
; i
++)
81 if (argv
[i
][0] == '-' && argv
[i
][1])
84 case 'E' : /* Encrypt */
86 encryption
= HTTP_ENCRYPT_REQUIRED
;
89 httpEncryption(http
, encryption
);
91 fprintf(stderr
, "%s: Sorry, no encryption support compiled in!\n",
93 #endif /* HAVE_LIBSSL */
96 case 'a' : /* Cancel all jobs */
100 case 'h' : /* Connect to host */
104 if (argv
[i
][2] != '\0')
105 http
= httpConnectEncrypt(argv
[i
] + 2, ippPort(), encryption
);
112 fputs("cancel: Error - expected hostname after \'-h\' option!\n", stderr
);
116 http
= httpConnectEncrypt(argv
[i
], ippPort(), encryption
);
121 perror("cancel: Unable to connect to server");
126 case 'u' : /* Username */
127 if (argv
[i
][2] != '\0')
128 cupsSetUser(argv
[i
] + 2);
135 fputs("cancel: Error - expected username after \'-u\' option!\n", stderr
);
139 cupsSetUser(argv
[i
]);
144 fprintf(stderr
, "cancel: Unknown option \'%c\'!\n", argv
[i
][1]);
150 * Cancel a job or printer...
153 if (isdigit(argv
[i
][0]))
157 job_id
= atoi(argv
[i
]);
159 else if (argv
[i
][0] == '-')
166 strlcpy(name
, argv
[i
], sizeof(name
));
171 if ((job
= strrchr(name
, '-')) != NULL
)
181 if ((host
= strchr(name
, '@')) != NULL
)
184 * Reconnect to the named host...
192 if ((http
= httpConnectEncrypt(host
, ippPort(), encryption
)) == NULL
)
194 perror("cancel: Unable to connect to server");
201 * Open a connection to the server...
205 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
206 encryption
)) == NULL
)
208 fputs("cancel: Unable to contact server!\n", stderr
);
213 * Build an IPP request, which requires the following
217 * attributes-natural-language
218 * printer-uri + job-id *or* job-uri
219 * [requesting-user-name]
224 request
->request
.op
.operation_id
= op
;
225 request
->request
.op
.request_id
= 1;
227 language
= cupsLangDefault();
229 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
230 "attributes-charset", NULL
, cupsLangEncoding(language
));
232 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
233 "attributes-natural-language", NULL
, language
->language
);
237 snprintf(uri
, sizeof(uri
), "ipp://localhost/printers/%s", dest
);
238 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
239 "printer-uri", NULL
, uri
);
240 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
245 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
246 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
250 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
251 "requesting-user-name", NULL
, cupsUser());
254 * Do the request and get back a response...
257 if (op
== IPP_PURGE_JOBS
)
258 response
= cupsDoRequest(http
, request
, "/admin/");
260 response
= cupsDoRequest(http
, request
, "/jobs/");
262 if (response
== NULL
||
263 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
265 fprintf(stderr
, "cancel: %s failed: %s\n",
266 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
267 response
? ippErrorString(response
->request
.status
.status_code
) :
268 ippErrorString(cupsLastError()));
284 * End of "$Id: cancel.c,v 1.23 2002/05/16 13:45:04 mike Exp $".