1 /*
2  *   CANON BJ command filter for the Common UNIX Printing System.
3  *
4  *
5  * Contents:
6  *
7  *   main() - Main entry and command processing.
8  */
9 
10 /*
11  * Include necessary headers...
12  */
13 
14 #include <cups/cups.h>
15 #include <ctype.h>
16 #include <stdlib.h>
17 
18 
19 /*
20  * Macros...
21  */
22 
23 #define pwrite(s,n) fwrite((s), 1, (n), stdout)
24 
25 
26 /*
27  * 'main()' - Main entry and processing of driver.
28  */
29 
30 int			/* O - Exit status */
main(int argc,char * argv[])31 main(int  argc,		/* I - Number of command-line arguments */
32      char *argv[])	/* I - Command-line arguments */
33 {
34   FILE	*fp;		/* Command file */
35   char	line[1024],	/* Line from file */
36 	*lineptr;	/* Pointer into line */
37   int	feedpage;	/* Feed the page */
38 
39 
40  /*
41   * Check for valid arguments...
42   */
43 
44   if (argc < 6 || argc > 7)
45   {
46    /*
47     * We don't have the correct number of arguments; write an error message
48     * and return.
49     */
50 
51     fputs("ERROR: commandtocanon job-id user title copies options [file]\n", stderr);
52     return (1);
53   }
54 
55  /*
56   * Open the command file as needed...
57   */
58 
59   if (argc == 7)
60   {
61     if ((fp = fopen(argv[6], "r")) == NULL)
62     {
63       perror("ERROR: Unable to open command file - ");
64       return (1);
65     }
66   }
67   else
68     fp = stdin;
69 
70  /*
71   * Reset the printer and initiate BJL mode
72   */
73 
74   pwrite("\x1b\x5b\x4b\x02\x00\x00\x1f" "BJLSTART\x0a", 16);
75   feedpage = 0;
76 
77  /*
78   * Read the commands from the file and send the appropriate commands...
79   */
80 
81   while (fgets(line, sizeof(line), fp) != NULL)
82   {
83    /*
84     * Drop trailing newline...
85     */
86 
87     lineptr = line + strlen(line) - 1;
88     if (*lineptr == '\n')
89       *lineptr = '\0';
90 
91    /*
92     * Skip leading whitespace...
93     */
94 
95     for (lineptr = line; isspace(*lineptr); lineptr ++);
96 
97    /*
98     * Skip comments and blank lines...
99     */
100 
101     if (*lineptr == '#' || !*lineptr)
102       continue;
103 
104    /*
105     * Parse the command...
106     */
107 
108     if (strncasecmp(lineptr, "Clean", 5) == 0)
109     {
110      /*
111       * Clean heads...
112       */
113 
114       char *what;
115 
116       for (what = lineptr + 6; isspace(*what); what ++);
117 
118       if (*what == 0)                   pwrite("@Cleaning=1ALL\x0a", 15);
119       if (!strncasecmp(what,"all",3))   pwrite("@Cleaning=1ALL\x0a", 15);
120       if (!strncasecmp(what,"black",5)) pwrite("@Cleaning=1K\x0a", 13);
121 
122     }
123     else if (strncasecmp(lineptr, "PrintAlignmentPage", 18) == 0)
124     {
125      /*
126       * Print alignment page...
127       */
128 
129       int phase;
130 
131       phase = atoi(lineptr + 18);
132 
133       if (phase==0) pwrite("@TestPrint=Auto\x0a", 16);
134       if (phase==1) pwrite("@TestPrint=Manual1\x0a", 19);
135       if (phase==2) pwrite("@TestPrint=Manual2\x0a", 19);
136 
137       feedpage = 0;
138     }
139     else if (strncasecmp(lineptr, "PrintSelfTestPage", 17) == 0)
140     {
141      /*
142       * Print version info and nozzle check...
143       */
144 
145       pwrite("@TestPrint=NozzleCheck\x0a", 23);
146 
147       feedpage = 0;
148     }
149     else if (strncasecmp(lineptr, "ReportLevels", 12) == 0)
150     {
151      /*
152       * Report ink levels...
153       */
154 
155     }
156     else if (strncasecmp(lineptr, "SetAlignment", 12) == 0)
157     {
158      /*
159       * Set head alignment...
160       */
161 
162     }
163     else
164       fprintf(stderr, "ERROR: Invalid printer command \"%s\"!\n", lineptr);
165   }
166 
167  /*
168   * Exit remote mode...
169   */
170 
171   pwrite("BJLEND\x0a", 7);
172 
173  /*
174   * Eject the page as needed...
175   */
176 
177   if (feedpage)
178   {
179     putchar(13);
180     putchar(10);
181     putchar(12);
182   }
183 
184  /*
185   * Close the command file and return...
186   */
187 
188   if (fp != stdin)
189     fclose(fp);
190 
191   return (0);
192 }
193 
194 
195 /*
196  */
197