PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/vp_plugins/sptsd/samples/lpr.c.smp

http://cupsfilter.googlecode.com/
Unknown | 431 lines | 371 code | 60 blank | 0 comment | 0 complexity | 0c03bba9b5e36919ec607089bcdfa43d MD5 | raw file
  1/*
  2 * "$Id: lpr.c 7720 2008-07-11 22:46:21Z mike $"
  3 *
  4 *   "lpr" command for the Common UNIX Printing System (CUPS).
  5 *
  6 *   Copyright 2007-2008 by Apple Inc.
  7 *   Copyright 1997-2007 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 send files for printing.
 18 */
 19
 20/*
 21 * Include necessary headers...
 22 */
 23
 24#include <stdio.h>
 25#include <stdlib.h>
 26#include <errno.h>
 27
 28#include <cups/string.h>
 29#include <cups/cups.h>
 30#include <cups/i18n.h>
 31
 32
 33/*
 34 * 'main()' - Parse options and send files for printing.
 35 */
 36
 37int
 38main(int  argc,				/* I - Number of command-line arguments */
 39     char *argv[])			/* I - Command-line arguments */
 40{
 41  int		i, j;			/* Looping var */
 42  int		job_id;			/* Job ID */
 43  char		ch;			/* Option character */
 44  char		*printer,		/* Destination printer or class */
 45		*instance;		/* Instance */
 46  const char	*title,			/* Job title */
 47		*val;			/* Environment variable name */
 48  int		num_copies;		/* Number of copies per file */
 49  int		num_files;		/* Number of files to print */
 50  const char	*files[1000];		/* Files to print */
 51  cups_dest_t	*dest;			/* Selected destination */
 52  int		num_options;		/* Number of options */
 53  cups_option_t	*options;		/* Options */
 54  int		deletefile;		/* Delete file after print? */
 55  char		buffer[8192];		/* Copy buffer */
 56
 57
 58  _cupsSetLocale(argv);
 59
 60  deletefile  = 0;
 61  printer     = NULL;
 62  dest        = NULL;
 63  num_options = 0;
 64  options     = NULL;
 65  num_files   = 0;
 66  title       = NULL;
 67
 68  for (i = 1; i < argc; i ++)
 69    if (argv[i][0] == '-')
 70      switch (ch = argv[i][1])
 71      {
 72        case 'E' : /* Encrypt */
 73#ifdef HAVE_SSL
 74	    cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
 75#else
 76            _cupsLangPrintf(stderr,
 77	                    _("%s: Sorry, no encryption support compiled in!\n"),
 78	                    argv[0]);
 79#endif /* HAVE_SSL */
 80	    break;
 81
 82        case 'U' : /* Username */
 83	    if (argv[i][2] != '\0')
 84	      cupsSetUser(argv[i] + 2);
 85	    else
 86	    {
 87	      i ++;
 88	      if (i >= argc)
 89	      {
 90	        _cupsLangPrintf(stderr,
 91		                _("%s: Error - expected username after "
 92				  "\'-U\' option!\n"),
 93		        	argv[0]);
 94	        return (1);
 95	      }
 96
 97              cupsSetUser(argv[i]);
 98	    }
 99	    break;
100	    
101        case 'H' : /* Connect to host */
102	    if (argv[i][2] != '\0')
103              cupsSetServer(argv[i] + 2);
104	    else
105	    {
106	      i ++;
107
108	      if (i >= argc)
109	      {
110	        _cupsLangPrintf(stderr,
111		        	_("%s: Error - expected hostname after "
112			          "\'-H\' option!\n"),
113				argv[0]);
114		return (1);
115              }
116	      else
117                cupsSetServer(argv[i]);
118	    }
119	    break;
120
121	case '1' : /* TROFF font set 1 */
122	case '2' : /* TROFF font set 2 */
123	case '3' : /* TROFF font set 3 */
124	case '4' : /* TROFF font set 4 */
125	case 'i' : /* indent */
126	case 'w' : /* width */
127	    if (argv[i][2] == '\0')
128	    {
129	      i ++;
130
131	      if (i >= argc)
132	      {
133		_cupsLangPrintf(stderr,
134		                _("%s: Error - expected value after \'-%c\' "
135				  "option!\n"), argv[0], ch);
136		return (1);
137	      }
138	    }
139
140        case 'c' : /* CIFPLOT */
141	case 'd' : /* DVI */
142	case 'f' : /* FORTRAN */
143	case 'g' : /* plot */
144	case 'n' : /* Ditroff */
145	case 't' : /* Troff */
146	case 'v' : /* Raster image */
147	    _cupsLangPrintf(stderr,
148	                    _("%s: Warning - \'%c\' format modifier not "
149			      "supported - output may not be correct!\n"),
150			    argv[0], ch);
151	    break;
152
153	case 'o' : /* Option */
154	    if (argv[i][2] != '\0')
155	      num_options = cupsParseOptions(argv[i] + 2, num_options, &options);
156	    else
157	    {
158	      i ++;
159	      if (i >= argc)
160	      {
161	        _cupsLangPrintf(stderr,
162		                _("%s: error - expected option=value after "
163			          "\'-o\' option!\n"),
164				argv[0]);
165		return (1);
166	      }
167
168	      num_options = cupsParseOptions(argv[i], num_options, &options);
169	    }
170	    break;
171
172	case 'l' : /* Literal/raw */
173            num_options = cupsAddOption("raw", "true", num_options, &options);
174	    break;
175
176	case 'p' : /* Prettyprint */
177            num_options = cupsAddOption("prettyprint", "true", num_options,
178	                                &options);
179	    break;
180
181	case 'h' : /* Suppress burst page */
182            num_options = cupsAddOption("job-sheets", "none", num_options,
183	                                &options);
184	    break;
185
186	case 's' : /* Don't use symlinks */
187	    break;
188
189	case 'm' : /* Mail on completion */
190	    {
191	      char	email[1024];	/* EMail address */
192
193
194	      snprintf(email, sizeof(email), "mailto:%s@%s", cupsUser(),
195	               httpGetHostname(NULL, buffer, sizeof(buffer)));
196	      num_options = cupsAddOption("notify-recipient-uri", email,
197	                                  num_options, &options);
198	    }
199	    break;
200
201	case 'q' : /* Queue file but don't print */
202            num_options = cupsAddOption("job-hold-until", "indefinite",
203	                                num_options, &options);
204	    break;
205
206	case 'r' : /* Remove file after printing */
207	    deletefile = 1;
208	    break;
209
210        case 'P' : /* Destination printer or class */
211	    if (argv[i][2] != '\0')
212	      printer = argv[i] + 2;
213	    else
214	    {
215	      i ++;
216	      if (i >= argc)
217	      {
218	        _cupsLangPrintf(stderr,
219		        	_("%s: Error - expected destination after "
220			          "\'-P\' option!\n"),
221				argv[0]);
222		return (1);
223	      }
224
225	      printer = argv[i];
226	    }
227
228            if ((instance = strrchr(printer, '/')) != NULL)
229	      *instance++ = '\0';
230
231            if ((dest = cupsGetNamedDest(NULL, printer, instance)) != NULL)
232	    {
233	      for (j = 0; j < dest->num_options; j ++)
234	        if (cupsGetOption(dest->options[j].name, num_options,
235		                  options) == NULL)
236	          num_options = cupsAddOption(dest->options[j].name,
237		                              dest->options[j].value,
238					      num_options, &options);
239	    }
240	    break;
241
242	case '#' : /* Number of copies */
243	    if (argv[i][2] != '\0')
244	      num_copies = atoi(argv[i] + 2);
245	    else
246	    {
247	      i ++;
248	      if (i >= argc)
249	      {
250	        _cupsLangPrintf(stderr,
251		        	_("%s: Error - expected copy count after "
252			          "\'-#\' option!\n"),
253				argv[0]);
254		return (1);
255	      }
256
257	      num_copies = atoi(argv[i]);
258	    }
259
260            sprintf(buffer, "%d", num_copies);
261            num_options = cupsAddOption("copies", buffer, num_options, &options);
262	    break;
263
264	case 'C' : /* Class */
265	case 'J' : /* Job name */
266	case 'T' : /* Title */
267	    if (argv[i][2] != '\0')
268	      title = argv[i] + 2;
269	    else
270	    {
271	      i ++;
272	      if (i >= argc)
273	      {
274		_cupsLangPrintf(stderr,
275		                _("%s: Error - expected name after \'-%c\' "
276				  "option!\n"), argv[0], ch);
277		return (1);
278	      }
279
280	      title = argv[i];
281	    }
282	    break;
283
284	default :
285	    _cupsLangPrintf(stderr,
286	                    _("%s: Error - unknown option \'%c\'!\n"),
287			    argv[0], argv[i][1]);
288	    return (1);
289      }
290    else if (num_files < 1000)
291    {
292     /*
293      * Print a file...
294      */
295
296      if (access(argv[i], R_OK) != 0)
297      {
298        _cupsLangPrintf(stderr,
299	                _("%s: Error - unable to access \"%s\" - %s\n"),
300		        argv[0], argv[i], strerror(errno));
301        return (1);
302      }
303
304      files[num_files] = argv[i];
305      num_files ++;
306
307      if (title == NULL)
308      {
309        if ((title = strrchr(argv[i], '/')) != NULL)
310	  title ++;
311	else
312          title = argv[i];
313      }
314    }
315    else
316      _cupsLangPrintf(stderr,
317                      _("%s: Error - too many files - \"%s\"\n"),
318		      argv[0], argv[i]);
319 /*
320  * See if we have any files to print; if not, print from stdin...
321  */
322
323  if (printer == NULL)
324  {
325    if ((dest = cupsGetNamedDest(NULL, NULL, NULL)) != NULL)
326    {
327      printer = dest->name;
328
329      for (j = 0; j < dest->num_options; j ++)
330	if (cupsGetOption(dest->options[j].name, num_options, options) == NULL)
331	  num_options = cupsAddOption(dest->options[j].name,
332		                      dest->options[j].value,
333				      num_options, &options);
334    }
335  }
336
337  if (printer == NULL)
338  {
339    val = NULL;
340
341    if ((printer = getenv("LPDEST")) == NULL)
342    {
343      if ((printer = getenv("PRINTER")) != NULL)
344      {
345        if (!strcmp(printer, "lp"))
346          printer = NULL;
347	else
348	  val = "PRINTER";
349      }
350    }
351    else
352      val = "LPDEST";
353
354    if (printer && !cupsGetNamedDest(NULL, printer, NULL))
355      _cupsLangPrintf(stderr,
356                      _("%s: Error - %s environment variable names "
357		        "non-existent destination \"%s\"!\n"),
358        	      argv[0], val, printer);
359    else if (cupsLastError() == IPP_NOT_FOUND)
360      _cupsLangPrintf(stderr,
361                      _("%s: Error - no default destination available.\n"),
362		      argv[0]);
363    else
364      _cupsLangPrintf(stderr,
365                      _("%s: Error - scheduler not responding!\n"),
366		      argv[0]);
367
368    return (1);
369  }
370
371  if (num_files > 0)
372  {
373    job_id = cupsPrintFiles(printer, num_files, files, title, num_options, options);
374
375    if (deletefile && job_id > 0)
376    {
377     /*
378      * Delete print files after printing...
379      */
380
381      for (i = 0; i < num_files; i ++)
382        unlink(files[i]);
383    }
384  }
385  else if ((job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, printer,
386                                   title ? title : "(stdin)",
387                                   num_options, options)) > 0)
388  {
389    http_status_t	status;		/* Write status */
390    const char		*format;	/* Document format */
391    ssize_t		bytes;		/* Bytes read */
392
393
394    if (cupsGetOption("raw", num_options, options))
395      format = CUPS_FORMAT_RAW;
396    else if ((format = cupsGetOption("document-format", num_options,
397                                     options)) == NULL)
398      format = CUPS_FORMAT_AUTO;
399
400    status = cupsStartDocument(CUPS_HTTP_DEFAULT, printer, job_id, NULL,
401                               format, 1);
402
403    while (status == HTTP_CONTINUE &&
404           (bytes = read(0, buffer, sizeof(buffer))) > 0)
405      status = cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, bytes);
406
407    if (status != HTTP_CONTINUE)
408    {
409      _cupsLangPrintf(stderr,
410		      _("%s: Error - unable to queue from stdin - %s\n"),
411		      argv[0], httpStatus(status));
412      return (1);
413    }
414
415    if (cupsFinishDocument(CUPS_HTTP_DEFAULT, printer) != IPP_OK)
416      job_id = 0;
417  }
418
419  if (job_id < 1)
420  {
421    _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
422    return (1);
423  }
424
425  return (0);
426}
427
428
429/*
430 * End of "$Id: lpr.c 7720 2008-07-11 22:46:21Z mike $".
431 */