1 /*
2 * Advanced EPSON ESC/P 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 "escp.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 * Some EPSON printers need an additional command issued at the
88 * beginning of each job to exit from USB "packet" mode...
89 */
90
91 if (ppd->model_number & ESCP_USB)
92 cupsWritePrintData("\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@", 29);
93
94 /*
95 * Reset the printer...
96 */
97
98 cupsWritePrintData("\033@", 2);
99
100 /*
101 * Enter remote mode...
102 */
103
104 cupsWritePrintData("\033(R\010\000\000REMOTE1", 13);
105 feedpage = 0;
106
107 /*
108 * Read the commands from the file and send the appropriate commands...
109 */
110
111 while (fgets(line, sizeof(line), fp) != NULL)
112 {
113 /*
114 * Drop trailing newline...
115 */
116
117 lineptr = line + strlen(line) - 1;
118 if (*lineptr == '\n')
119 *lineptr = '\0';
120
121 /*
122 * Skip leading whitespace...
123 */
124
125 for (lineptr = line; isspace(*lineptr); lineptr ++);
126
127 /*
128 * Skip comments and blank lines...
129 */
130
131 if (*lineptr == '#' || !*lineptr)
132 continue;
133
134 /*
135 * Parse the command...
136 */
137
138 if (strncasecmp(lineptr, "Clean", 5) == 0)
139 {
140 /*
141 * Clean heads...
142 */
143
144 cupsWritePrintData("CH\002\000\000\000", 6);
145 }
146 else if (strncasecmp(lineptr, "PrintAlignmentPage", 18) == 0)
147 {
148 /*
149 * Print alignment page...
150 */
151
152 int phase;
153
154 phase = atoi(lineptr + 18);
155
156 cupsWritePrintData("DT\003\000\000", 5);
157 putchar(phase & 255);
158 putchar(phase >> 8);
159 feedpage = 1;
160 }
161 else if (strncasecmp(lineptr, "PrintSelfTestPage", 17) == 0)
162 {
163 /*
164 * Print version info and nozzle check...
165 */
166
167 cupsWritePrintData("VI\002\000\000\000", 6);
168 cupsWritePrintData("NC\002\000\000\000", 6);
169 feedpage = 1;
170 }
171 else if (strncasecmp(lineptr, "ReportLevels", 12) == 0)
172 {
173 /*
174 * Report ink levels...
175 */
176
177 cupsWritePrintData("IQ\001\000\001", 5);
178 }
179 else if (strncasecmp(lineptr, "SetAlignment", 12) == 0)
180 {
181 /*
182 * Set head alignment...
183 */
184
185 int phase, x;
186
187 if (sscanf(lineptr + 12, "%d%d", &phase, &x) != 2)
188 {
189 fprintf(stderr, "ERROR: Invalid printer command \"%s\"!\n", lineptr);
190 continue;
191 }
192
193 cupsWritePrintData("DA\004\000", 4);
194 putchar(0);
195 putchar(phase);
196 putchar(0);
197 putchar(x);
198 cupsWritePrintData("SV\000\000", 4);
199 }
200 else
201 fprintf(stderr, "ERROR: Invalid printer command \"%s\"!\n", lineptr);
202 }
203
204 /*
205 * Exit remote mode...
206 */
207
208 cupsWritePrintData("\033\000\000\000", 4);
209
210 /*
211 * Eject the page as needed...
212 */
213
214 if (feedpage)
215 {
216 fputs("PAGE: 1 1\n", stderr);
217
218 putchar(13);
219 putchar(10);
220 putchar(12);
221 }
222
223 /*
224 * Reset the printer...
225 */
226
227 cupsWritePrintData("\033@", 2);
228
229 /*
230 * Close the command file and return...
231 */
232
233 ppdClose(ppd);
234
235 if (fp != stdin)
236 fclose(fp);
237
238 return (0);
239 }
240
241