2 * "$Id: lprm.c 6649 2007-07-11 21:46:42Z mike $"
4 * "lprm" command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
17 * main() - Parse options and cancel jobs.
21 * Include necessary headers...
27 #include <cups/cups.h>
28 #include <cups/i18n.h>
29 #include <cups/string.h>
33 * 'main()' - Parse options and cancel jobs.
36 int /* O - Exit status */
37 main(int argc
, /* I - Number of command-line arguments */
38 char *argv
[]) /* I - Command-line arguments */
40 http_t
*http
; /* HTTP connection to server */
41 int i
; /* Looping var */
42 int job_id
; /* Job ID */
43 const char *dest
; /* Destination printer */
44 char *instance
; /* Pointer to instance name */
45 char uri
[1024]; /* Printer or job URI */
46 ipp_t
*request
; /* IPP request */
47 ipp_t
*response
; /* IPP response */
48 ipp_op_t op
; /* Operation */
49 int num_dests
; /* Number of destinations */
50 cups_dest_t
*dests
, /* Destinations */
51 *defdest
; /* Default destination */
52 http_encryption_t encryption
; /* Encryption? */
58 * Setup to cancel individual print jobs...
66 encryption
= cupsEncryption();
69 * Open a connection to the server...
72 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(), encryption
)) == NULL
)
74 _cupsLangPuts(stderr
, _("lprm: Unable to contact server!\n"));
78 num_dests
= cupsGetDests2(http
, &dests
);
79 defdest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
);
80 dest
= defdest
? defdest
->name
: NULL
;
83 * Process command-line arguments...
86 for (i
= 1; i
< argc
; i
++)
87 if (argv
[i
][0] == '-' && argv
[i
][1] != '\0')
90 case 'E' : /* Encrypt */
92 encryption
= HTTP_ENCRYPT_REQUIRED
;
94 httpEncryption(http
, encryption
);
95 cupsSetEncryption(encryption
);
97 _cupsLangPrintf(stderr
,
98 _("%s: Sorry, no encryption support compiled in!\n"),
100 #endif /* HAVE_SSL */
103 case 'P' : /* Cancel jobs on a printer */
112 if ((instance
= strchr(dest
, '/')) != NULL
)
115 if (cupsGetDest(dest
, NULL
, num_dests
, dests
) == NULL
)
117 _cupsLangPrintf(stderr
,
118 _("%s: Error - unknown destination \"%s\"!\n"),
120 cupsFreeDests(num_dests
, dests
);
126 case 'U' : /* Username */
127 if (argv
[i
][2] != '\0')
128 cupsSetUser(argv
[i
] + 2);
134 _cupsLangPrintf(stderr
,
135 _("%s: Error - expected username after "
141 cupsSetUser(argv
[i
]);
145 case 'h' : /* Connect to host */
146 if (argv
[i
][2] != '\0')
147 cupsSetServer(argv
[i
] + 2);
154 _cupsLangPrintf(stderr
,
155 _("%s: Error - expected hostname after "
161 cupsSetServer(argv
[i
]);
165 cupsFreeDests(num_dests
, dests
);
167 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
168 encryption
)) == NULL
)
170 _cupsLangPuts(stderr
, _("lprm: Unable to contact server!\n"));
174 num_dests
= cupsGetDests2(http
, &dests
);
175 defdest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
);
176 dest
= defdest
? defdest
->name
: NULL
;
180 _cupsLangPrintf(stderr
,
181 _("%s: Error - unknown option \'%c\'!\n"),
182 argv
[0], argv
[i
][1]);
183 cupsFreeDests(num_dests
, dests
);
190 * Cancel a job or printer...
193 if (isdigit(argv
[i
][0] & 255) &&
194 cupsGetDest(argv
[i
], NULL
, num_dests
, dests
) == NULL
)
198 job_id
= atoi(argv
[i
]);
200 else if (!strcmp(argv
[i
], "-"))
215 * Build an IPP request, which requires the following
219 * attributes-natural-language
220 * printer-uri + job-id *or* job-uri
221 * [requesting-user-name]
224 request
= ippNewRequest(op
);
228 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
229 "localhost", 0, "/printers/%s", dest
);
230 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
231 "printer-uri", NULL
, uri
);
232 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
237 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
238 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
242 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
243 "requesting-user-name", NULL
, cupsUser());
246 * Do the request and get back a response...
249 if (op
== IPP_PURGE_JOBS
)
250 response
= cupsDoRequest(http
, request
, "/admin/");
252 response
= cupsDoRequest(http
, request
, "/jobs/");
256 if (cupsLastError() > IPP_OK_CONFLICT
)
258 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());
260 cupsFreeDests(num_dests
, dests
);
267 * If nothing has been canceled yet, cancel the current job on the specified
268 * (or default) printer...
271 if (response
== NULL
)
272 if (!cupsCancelJob(dest
, 0))
274 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());
275 cupsFreeDests(num_dests
, dests
);
280 cupsFreeDests(num_dests
, dests
);
288 * End of "$Id: lprm.c 6649 2007-07-11 21:46:42Z mike $".