• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Advanced PCL command filter for CUPS.
3  *
4  *   Copyright 2007-2011 by Apple Inc.
5  *   Copyright 1993-2005 by Easy Software Products.
6  *
7  *   These coded instructions, statements, and computer programs are the
8  *   property of Apple Inc. and are protected by Federal copyright
9  *   law.  Distribution and use rights are outlined in the file "COPYING"
10  *   which should have been included with this file.
11  *
12  *
13  * Contents:
14  *
15  *   main() - Main entry and command processing.
16  */
17 
18 /*
19  * Include necessary headers...
20  */
21 
22 #include <cups/cups.h>
23 #include <cups/ppd.h>
24 #include <cupsfilters/driver.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "pcl.h"
28 
29 
30 /*
31  * 'main()' - Main entry and processing of driver.
32  */
33 
34 int						/* O - Exit status */
main(int argc,char * argv[])35 main(int  argc,					/* I - Number of command-line arguments */
36      char *argv[])				/* I - Command-line arguments */
37 {
38   FILE		*fp;				/* Command file */
39   char		line[1024],			/* Line from file */
40 		*lineptr;			/* Pointer into line */
41   int		feedpage;			/* Feed the page */
42   ppd_file_t	*ppd;				/* PPD file */
43 
44 
45  /*
46   * Check for valid arguments...
47   */
48 
49   if (argc < 6 || argc > 7)
50   {
51    /*
52     * We don't have the correct number of arguments; write an error message
53     * and return.
54     */
55 
56     fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
57 	    argv[0]);
58     return (1);
59   }
60 
61  /*
62   * Open the PPD file...
63   */
64 
65   if ((ppd = ppdOpenFile(getenv("PPD"))) == NULL)
66   {
67     fputs("ERROR: Unable to open PPD file!\n", stderr);
68     return (1);
69   }
70 
71  /*
72   * Open the command file as needed...
73   */
74 
75   if (argc == 7)
76   {
77     if ((fp = fopen(argv[6], "r")) == NULL)
78     {
79       perror("ERROR: Unable to open command file - ");
80       return (1);
81     }
82   }
83   else
84     fp = stdin;
85 
86  /*
87   * Reset the printer...
88   */
89 
90   cupsWritePrintData("\033E", 2);
91 
92  /*
93   * Read the commands from the file and send the appropriate commands...
94   */
95 
96   feedpage = 0;
97 
98   while (fgets(line, sizeof(line), fp) != NULL)
99   {
100    /*
101     * Drop trailing newline...
102     */
103 
104     lineptr = line + strlen(line) - 1;
105     if (*lineptr == '\n')
106       *lineptr = '\0';
107 
108    /*
109     * Skip leading whitespace...
110     */
111 
112     for (lineptr = line; isspace(*lineptr); lineptr ++);
113 
114    /*
115     * Skip comments and blank lines...
116     */
117 
118     if (*lineptr == '#' || !*lineptr)
119       continue;
120 
121    /*
122     * Parse the command...
123     */
124 
125     if (strncasecmp(lineptr, "Clean", 5) == 0 &&
126         (ppd->model_number & PCL_INKJET))
127     {
128      /*
129       * Clean heads...
130       */
131 
132       cupsWritePrintData("\033&b16WPML \004\000\006\001\004\001\005\001"
133                          "\001\004\001\144", 22);
134     }
135     else
136       fprintf(stderr, "ERROR: Invalid printer command \"%s\"!\n", lineptr);
137   }
138 
139  /*
140   * Eject the page as needed...
141   */
142 
143   if (feedpage)
144   {
145     fputs("PAGE: 1 1\n", stderr);
146 
147     putchar(12);
148   }
149 
150  /*
151   * Reset the printer...
152   */
153 
154   cupsWritePrintData("\033E", 2);
155 
156  /*
157   * Close the command file and return...
158   */
159 
160   ppdClose(ppd);
161 
162   if (fp != stdin)
163     fclose(fp);
164 
165   return (0);
166 }
167 
168