1/*
2 * "$Id: lprm.c 11093 2013-07-03 20:48:42Z msweet $"
3 *
4 *   "lprm" command for CUPS.
5 *
6 *   Copyright 2007-2010 by Apple Inc.
7 *   Copyright 1997-2006 by Easy Software Products.
8 *
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/".
14 *
15 * Contents:
16 *
17 *   main() - Parse options and cancel jobs.
18 */
19
20/*
21 * Include necessary headers...
22 */
23
24#include <cups/cups-private.h>
25
26
27/*
28 * 'main()' - Parse options and cancel jobs.
29 */
30
31int				/* O - Exit status */
32main(int  argc,			/* I - Number of command-line arguments */
33     char *argv[])		/* I - Command-line arguments */
34{
35  int		i;		/* Looping var */
36  int		job_id;		/* Job ID */
37  const char	*name;		/* Destination printer */
38  char		*instance;	/* Pointer to instance name */
39  cups_dest_t	*dest,		/* Destination */
40		*defdest;	/* Default destination */
41  int		did_cancel;	/* Did we cancel something? */
42
43
44  _cupsSetLocale(argv);
45
46 /*
47  * Setup to cancel individual print jobs...
48  */
49
50  did_cancel = 0;
51  defdest    = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL);
52  name       = defdest ? defdest->name : NULL;
53
54 /*
55  * Process command-line arguments...
56  */
57
58  for (i = 1; i < argc; i ++)
59    if (argv[i][0] == '-' && argv[i][1] != '\0')
60      switch (argv[i][1])
61      {
62        case 'E' : /* Encrypt */
63#ifdef HAVE_SSL
64	    cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
65#else
66            _cupsLangPrintf(stderr,
67	                    _("%s: Sorry, no encryption support."), argv[0]);
68#endif /* HAVE_SSL */
69	    break;
70
71        case 'P' : /* Cancel jobs on a printer */
72	    if (argv[i][2])
73	      name = argv[i] + 2;
74	    else
75	    {
76	      i ++;
77	      name = argv[i];
78	    }
79
80	    if ((instance = strchr(name, '/')) != NULL)
81	      *instance = '\0';
82
83	    if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, name,
84	                                 NULL)) == NULL)
85	    {
86	      _cupsLangPrintf(stderr,
87	                      _("%s: Error - unknown destination \"%s\"."),
88			      argv[0], name);
89              goto error;
90	    }
91
92	    cupsFreeDests(1, dest);
93	    break;
94
95        case 'U' : /* Username */
96	    if (argv[i][2] != '\0')
97	      cupsSetUser(argv[i] + 2);
98	    else
99	    {
100	      i ++;
101	      if (i >= argc)
102	      {
103	        _cupsLangPrintf(stderr,
104		                _("%s: Error - expected username after "
105				  "\"-U\" option."), argv[0]);
106	        goto error;
107	      }
108
109              cupsSetUser(argv[i]);
110	    }
111	    break;
112
113        case 'h' : /* Connect to host */
114	    if (argv[i][2] != '\0')
115              cupsSetServer(argv[i] + 2);
116	    else
117	    {
118	      i ++;
119
120	      if (i >= argc)
121	      {
122	        _cupsLangPrintf(stderr,
123		        	_("%s: Error - expected hostname after "
124			          "\"-h\" option."), argv[0]);
125		goto error;
126              }
127	      else
128                cupsSetServer(argv[i]);
129	    }
130
131            if (defdest)
132	      cupsFreeDests(1, defdest);
133
134	    defdest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL);
135	    name    = defdest ? defdest->name : NULL;
136	    break;
137
138	default :
139	    _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."),
140			    argv[0], argv[i][1]);
141            goto error;
142      }
143    else
144    {
145     /*
146      * Cancel a job or printer...
147      */
148
149      if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, argv[i], NULL)) != NULL)
150        cupsFreeDests(1, dest);
151
152      if (dest)
153      {
154        name   = argv[i];
155        job_id = 0;
156      }
157      else if (isdigit(argv[i][0] & 255))
158      {
159        name   = NULL;
160        job_id = atoi(argv[i]);
161      }
162      else if (!strcmp(argv[i], "-"))
163      {
164       /*
165        * Cancel all jobs
166        */
167
168        job_id = -1;
169      }
170      else
171      {
172	_cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"."),
173			argv[0], argv[i]);
174	goto error;
175      }
176
177      if (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) != IPP_OK)
178      {
179        _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
180	goto error;
181      }
182
183      did_cancel = 1;
184    }
185
186 /*
187  * If nothing has been canceled yet, cancel the current job on the specified
188  * (or default) printer...
189  */
190
191  if (!did_cancel && cupsCancelJob2(CUPS_HTTP_DEFAULT, name, 0, 0) != IPP_OK)
192    {
193      _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
194      goto error;
195    }
196
197  if (defdest)
198    cupsFreeDests(1, defdest);
199
200  return (0);
201
202 /*
203  * If we get here there was an error, so clean up...
204  */
205
206  error:
207
208  if (defdest)
209    cupsFreeDests(1, defdest);
210
211  return (1);
212}
213
214
215/*
216 * End of "$Id: lprm.c 11093 2013-07-03 20:48:42Z msweet $".
217 */
218