]> git.ipfire.org Git - thirdparty/cups.git/blob - driver/commandtoescpx.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / driver / commandtoescpx.c
1 /*
2 * "$Id$"
3 *
4 * Advanced EPSON ESC/P command filter for CUPS.
5 *
6 * Copyright 2007 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 "driver.h"
27 #include <cups/string.h>
28 #include "data/escp.h"
29
30
31 /*
32 * 'main()' - Main entry and processing of driver.
33 */
34
35 int /* O - Exit status */
36 main(int argc, /* I - Number of command-line arguments */
37 char *argv[]) /* I - Command-line arguments */
38 {
39 FILE *fp; /* Command file */
40 char line[1024], /* Line from file */
41 *lineptr; /* Pointer into line */
42 int feedpage; /* Feed the page */
43 ppd_file_t *ppd; /* PPD file */
44
45
46 /*
47 * Check for valid arguments...
48 */
49
50 if (argc < 6 || argc > 7)
51 {
52 /*
53 * We don't have the correct number of arguments; write an error message
54 * and return.
55 */
56
57 fputs("ERROR: commandtoescpx job-id user title copies options [file]\n", stderr);
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
242 /*
243 * End of "$Id$".
244 */