1 /*
2  *   EPSON ESC/P2 command filter for the Common UNIX Printing System.
3  *
4  *   Copyright 1993-2000 by Easy Software Products.
5  *
6  *   This program is free software; you can redistribute it and/or modify it
7  *   under the terms of the GNU General Public License as published by the Free
8  *   Software Foundation; either version 2 of the License, or (at your option)
9  *   any later version.
10  *
11  *   This program is distributed in the hope that it will be useful, but
12  *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  *   for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  *
19  * Contents:
20  *
21  *   main() - Main entry and command processing.
22  */
23 
24 /*
25  * Include necessary headers...
26  */
27 
28 #include <cups/cups.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <strings.h>
34 
35 /*
36  * Macros...
37  */
38 
39 #define pwrite(s,n) fwrite((s), 1, (n), stdout)
40 
41 
42 /*
43  * 'main()' - Main entry and processing of driver.
44  */
45 
46 int			/* O - Exit status */
main(int argc,char * argv[])47 main(int  argc,		/* I - Number of command-line arguments */
48      char *argv[])	/* I - Command-line arguments */
49 {
50   FILE	*fp;		/* Command file */
51   char	line[1024],	/* Line from file */
52 	*lineptr;	/* Pointer into line */
53   int	feedpage;	/* Feed the page */
54 
55 
56  /*
57   * Check for valid arguments...
58   */
59 
60   if (argc < 6 || argc > 7)
61   {
62    /*
63     * We don't have the correct number of arguments; write an error message
64     * and return.
65     */
66 
67     fputs("ERROR: commandtoepson job-id user title copies options [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   pwrite("\033@", 2);
91 
92  /*
93   * Enter remote mode...
94   */
95 
96   pwrite("\033(R\010\000\000REMOTE1", 13);
97   feedpage = 0;
98 
99  /*
100   * Read the commands from the file and send the appropriate commands...
101   */
102 
103   while (fgets(line, sizeof(line), fp) != NULL)
104   {
105    /*
106     * Drop trailing newline...
107     */
108 
109     lineptr = line + strlen(line) - 1;
110     if (*lineptr == '\n')
111       *lineptr = '\0';
112 
113    /*
114     * Skip leading whitespace...
115     */
116 
117     for (lineptr = line; isspace(*lineptr); lineptr ++);
118 
119    /*
120     * Skip comments and blank lines...
121     */
122 
123     if (*lineptr == '#' || !*lineptr)
124       continue;
125 
126    /*
127     * Parse the command...
128     */
129 
130     if (strncasecmp(lineptr, "Clean", 5) == 0)
131     {
132      /*
133       * Clean heads...
134       */
135 
136       pwrite("CH\002\000\000\000", 6);
137     }
138     else if (strncasecmp(lineptr, "PrintAlignmentPage", 18) == 0)
139     {
140      /*
141       * Print alignment page...
142       */
143 
144       int phase;
145 
146       phase = atoi(lineptr + 18);
147 
148       pwrite("DT\003\000\000", 5);
149       putchar(phase & 255);
150       putchar(phase >> 8);
151       feedpage = 1;
152     }
153     else if (strncasecmp(lineptr, "PrintSelfTestPage", 17) == 0)
154     {
155      /*
156       * Print version info and nozzle check...
157       */
158 
159       pwrite("VI\002\000\000\000", 6);
160       pwrite("NC\002\000\000\000", 6);
161       feedpage = 1;
162     }
163     else if (strncasecmp(lineptr, "ReportLevels", 12) == 0)
164     {
165      /*
166       * Report ink levels...
167       */
168 
169       pwrite("IQ\001\000\001", 5);
170     }
171     else if (strncasecmp(lineptr, "SetAlignment", 12) == 0)
172     {
173      /*
174       * Set head alignment...
175       */
176 
177       int phase, x;
178 
179       if (sscanf(lineptr + 12, "%d%d", &phase, &x) != 2)
180       {
181         fprintf(stderr, "ERROR: Invalid printer command \"%s\"!\n", lineptr);
182         continue;
183       }
184 
185       pwrite("DA\004\000", 4);
186       putchar(0);
187       putchar(phase);
188       putchar(0);
189       putchar(x);
190       pwrite("SV\000\000", 4);
191     }
192     else
193       fprintf(stderr, "ERROR: Invalid printer command \"%s\"!\n", lineptr);
194   }
195 
196  /*
197   * Exit remote mode...
198   */
199 
200   pwrite("\033\000\000\000", 4);
201 
202  /*
203   * Eject the page as needed...
204   */
205 
206   if (feedpage)
207   {
208     putchar(13);
209     putchar(10);
210     putchar(12);
211   }
212 
213  /*
214   * Reset the printer...
215   */
216 
217   pwrite("\033@", 2);
218 
219  /*
220   * Close the command file and return...
221   */
222 
223   if (fp != stdin)
224     fclose(fp);
225 
226   return (0);
227 }
228