4 * "lprm" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Parse options and cancel jobs.
30 * Include necessary headers...
36 #include <cups/cups.h>
37 #include <cups/i18n.h>
38 #include <cups/string.h>
42 * 'main()' - Parse options and cancel jobs.
45 int /* O - Exit status */
46 main(int argc
, /* I - Number of command-line arguments */
47 char *argv
[]) /* I - Command-line arguments */
49 http_t
*http
; /* HTTP connection to server */
50 int i
; /* Looping var */
51 int job_id
; /* Job ID */
52 const char *dest
; /* Destination printer */
53 char *instance
; /* Pointer to instance name */
54 char uri
[1024]; /* Printer or job URI */
55 ipp_t
*request
; /* IPP request */
56 ipp_t
*response
; /* IPP response */
57 ipp_op_t op
; /* Operation */
58 cups_lang_t
*language
; /* Language */
59 int num_dests
; /* Number of destinations */
60 cups_dest_t
*dests
; /* Destinations */
61 http_encryption_t encryption
; /* Encryption? */
65 * Setup to cancel individual print jobs...
73 encryption
= cupsEncryption();
74 language
= cupsLangDefault();
75 num_dests
= cupsGetDests(&dests
);
77 for (i
= 0; i
< num_dests
; i
++)
78 if (dests
[i
].is_default
)
82 * Open a connection to the server...
85 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(), encryption
)) == NULL
)
87 _cupsLangPuts(stderr
, _("lprm: Unable to contact server!\n"));
88 cupsFreeDests(num_dests
, dests
);
93 * Process command-line arguments...
96 for (i
= 1; i
< argc
; i
++)
97 if (argv
[i
][0] == '-' && argv
[i
][1] != '\0')
100 case 'E' : /* Encrypt */
102 encryption
= HTTP_ENCRYPT_REQUIRED
;
104 httpEncryption(http
, encryption
);
106 _cupsLangPrintf(stderr
,
107 _("%s: Sorry, no encryption support compiled in!\n"),
109 #endif /* HAVE_SSL */
112 case 'P' : /* Cancel jobs on a printer */
121 if ((instance
= strchr(dest
, '/')) != NULL
)
124 if (cupsGetDest(dest
, NULL
, num_dests
, dests
) == NULL
)
126 _cupsLangPrintf(stderr
,
127 _("%s: Error - unknown destination \"%s\"!\n"),
129 cupsFreeDests(num_dests
, dests
);
135 case 'U' : /* Username */
136 if (argv
[i
][2] != '\0')
137 cupsSetUser(argv
[i
] + 2);
143 _cupsLangPrintf(stderr
,
144 _("%s: Error - expected username after "
150 cupsSetUser(argv
[i
]);
154 case 'h' : /* Connect to host */
158 if (argv
[i
][2] != '\0')
159 cupsSetServer(argv
[i
] + 2);
166 _cupsLangPrintf(stderr
,
167 _("%s: Error - expected hostname after "
173 cupsSetServer(argv
[i
]);
178 _cupsLangPrintf(stderr
,
179 _("%s: Error - unknown option \'%c\'!\n"),
180 argv
[0], argv
[i
][1]);
181 cupsFreeDests(num_dests
, dests
);
188 * Cancel a job or printer...
191 if (isdigit(argv
[i
][0] & 255) &&
192 cupsGetDest(argv
[i
], NULL
, num_dests
, dests
) == NULL
)
196 job_id
= atoi(argv
[i
]);
198 else if (!strcmp(argv
[i
], "-"))
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]
222 request
= ippNewRequest(op
);
226 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
227 "localhost", 0, "/printers/%s", dest
);
228 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
229 "printer-uri", NULL
, uri
);
230 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
235 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
236 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
240 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
241 "requesting-user-name", NULL
, cupsUser());
244 * Do the request and get back a response...
247 if (op
== IPP_PURGE_JOBS
)
248 response
= cupsDoRequest(http
, request
, "/admin/");
250 response
= cupsDoRequest(http
, request
, "/jobs/");
254 if (cupsLastError() > IPP_OK_CONFLICT
)
256 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());
258 cupsFreeDests(num_dests
, dests
);
265 * If nothing has been cancelled yet, cancel the current job on the specified
266 * (or default) printer...
269 if (response
== NULL
)
270 if (!cupsCancelJob(dest
, 0))
272 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());
273 cupsFreeDests(num_dests
, dests
);
278 cupsFreeDests(num_dests
, dests
);