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