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