]>
Commit | Line | Data |
---|---|---|
bd7854cb | 1 | /* |
bc44d920 | 2 | * "$Id: tbcp.c 6649 2007-07-11 21:46:42Z mike $" |
bd7854cb | 3 | * |
4 | * TBCP port monitor for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
bd7854cb | 7 | * Copyright 1993-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
bd7854cb | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * main() - Main entry... | |
20 | * psgets() - Get a line from a file. | |
21 | * pswrite() - Write data from a file. | |
22 | */ | |
23 | ||
24 | /* | |
25 | * Include necessary headers... | |
26 | */ | |
27 | ||
28 | #include <cups/string.h> | |
29 | #include <cups/cups.h> | |
30 | ||
31 | ||
32 | /* | |
33 | * Local functions... | |
34 | */ | |
35 | ||
36 | static char *psgets(char *buf, size_t *bytes, FILE *fp); | |
37 | static size_t pswrite(const char *buf, size_t bytes, FILE *fp); | |
38 | ||
39 | ||
40 | /* | |
41 | * 'main()' - Main entry... | |
42 | */ | |
43 | ||
44 | int /* O - Exit status */ | |
45 | main(int argc, /* I - Number of command-line args */ | |
46 | char *argv[]) /* I - Command-line arguments */ | |
47 | { | |
48 | FILE *fp; /* File to print */ | |
49 | int copies; /* Number of copies left */ | |
50 | char line[1024]; /* Line/buffer from stream/file */ | |
51 | size_t linelen; /* Length of line */ | |
52 | ||
53 | ||
54 | /* | |
55 | * Check command-line... | |
56 | */ | |
57 | ||
58 | if (argc < 6 || argc > 7) | |
59 | { | |
60 | fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr); | |
61 | return (1); | |
62 | } | |
63 | ||
64 | if (argc == 6) | |
65 | { | |
66 | copies = 1; | |
67 | fp = stdin; | |
68 | } | |
69 | else | |
70 | { | |
71 | copies = atoi(argv[4]); | |
72 | fp = fopen(argv[6], "rb"); | |
73 | ||
74 | if (!fp) | |
75 | { | |
76 | perror(argv[6]); | |
77 | return (1); | |
78 | } | |
79 | } | |
80 | ||
81 | /* | |
82 | * Copy the print file to stdout... | |
83 | */ | |
84 | ||
85 | while (copies > 0) | |
86 | { | |
87 | copies --; | |
88 | ||
89 | /* | |
90 | * Read the first line... | |
91 | */ | |
92 | ||
93 | linelen = sizeof(line); | |
94 | if (psgets(line, &linelen, fp) == NULL) | |
95 | { | |
96 | fputs("ERROR: Empty print file!\n", stderr); | |
97 | return (1); | |
98 | } | |
99 | ||
100 | /* | |
101 | * Handle leading PJL fun... | |
102 | */ | |
103 | ||
104 | if (!strncmp(line, "\033%-12345X", 9) || !strncmp(line, "@PJL ", 5)) | |
105 | { | |
106 | /* | |
107 | * Yup, we have leading PJL fun, so copy it until we hit a line | |
108 | * with "ENTER LANGUAGE"... | |
109 | */ | |
110 | ||
111 | while (strstr(line, "ENTER LANGUAGE") == NULL) | |
112 | { | |
113 | fwrite(line, 1, linelen, stdout); | |
114 | ||
115 | linelen = sizeof(line); | |
116 | if (psgets(line, &linelen, fp) == NULL) | |
117 | break; | |
118 | } | |
119 | } | |
120 | else | |
121 | { | |
122 | /* | |
123 | * No PJL stuff, add it... | |
124 | */ | |
125 | ||
126 | puts("\033%-12345X@PJL"); | |
127 | puts("@PJL ENTER LANGUAGE = POSTSCRIPT"); | |
128 | } | |
129 | ||
130 | /* | |
131 | * Switch to TBCP mode... | |
132 | */ | |
133 | ||
134 | fputs("\001M", stdout); | |
135 | ||
136 | /* | |
137 | * Loop until we see end-of-file... | |
138 | */ | |
139 | ||
140 | while (pswrite(line, linelen, stdout) > 0) | |
141 | { | |
142 | linelen = sizeof(line); | |
143 | if (psgets(line, &linelen, fp) == NULL) | |
144 | break; | |
145 | } | |
146 | ||
147 | fflush(stdout); | |
148 | } | |
149 | ||
150 | return (0); | |
151 | } | |
152 | ||
153 | ||
154 | /* | |
155 | * 'psgets()' - Get a line from a file. | |
156 | * | |
157 | * Note: | |
158 | * | |
159 | * This function differs from the gets() function in that it | |
160 | * handles any combination of CR, LF, or CR LF to end input | |
161 | * lines. | |
162 | */ | |
163 | ||
164 | static char * /* O - String or NULL if EOF */ | |
165 | psgets(char *buf, /* I - Buffer to read into */ | |
166 | size_t *bytes, /* IO - Length of buffer */ | |
167 | FILE *fp) /* I - File to read from */ | |
168 | { | |
169 | char *bufptr; /* Pointer into buffer */ | |
170 | int ch; /* Character from file */ | |
171 | size_t len; /* Max length of string */ | |
172 | ||
173 | ||
174 | len = *bytes - 1; | |
175 | bufptr = buf; | |
176 | ch = EOF; | |
177 | ||
178 | while ((bufptr - buf) < len) | |
179 | { | |
180 | if ((ch = getc(fp)) == EOF) | |
181 | break; | |
182 | ||
183 | if (ch == '\r') | |
184 | { | |
185 | /* | |
186 | * Got a CR; see if there is a LF as well... | |
187 | */ | |
188 | ||
189 | ch = getc(fp); | |
190 | ||
191 | if (ch != EOF && ch != '\n') | |
192 | { | |
193 | ungetc(ch, fp); /* Nope, save it for later... */ | |
194 | ch = '\r'; | |
195 | } | |
196 | else | |
197 | *bufptr++ = '\r'; | |
198 | break; | |
199 | } | |
200 | else if (ch == '\n') | |
201 | break; | |
202 | else | |
203 | *bufptr++ = ch; | |
204 | } | |
205 | ||
206 | /* | |
207 | * Add a trailing newline if it is there... | |
208 | */ | |
209 | ||
210 | if (ch == '\n' || ch == '\r') | |
211 | { | |
212 | if ((bufptr - buf) < len) | |
213 | *bufptr++ = ch; | |
214 | else | |
215 | ungetc(ch, fp); | |
216 | } | |
217 | ||
218 | /* | |
219 | * Nul-terminate the string and return it (or NULL for EOF). | |
220 | */ | |
221 | ||
222 | *bufptr = '\0'; | |
223 | *bytes = bufptr - buf; | |
224 | ||
225 | if (ch == EOF && bufptr == buf) | |
226 | return (NULL); | |
227 | else | |
228 | return (buf); | |
229 | } | |
230 | ||
231 | ||
232 | /* | |
233 | * 'pswrite()' - Write data from a file. | |
234 | */ | |
235 | ||
236 | static size_t /* O - Number of bytes written */ | |
237 | pswrite(const char *buf, /* I - Buffer to write */ | |
238 | size_t bytes, /* I - Bytes to write */ | |
239 | FILE *fp) /* I - File to write to */ | |
240 | { | |
241 | size_t count; /* Remaining bytes */ | |
242 | ||
243 | ||
244 | for (count = bytes; count > 0; count --, buf ++) | |
245 | switch (*buf) | |
246 | { | |
247 | case 0x01 : /* CTRL-A */ | |
248 | case 0x03 : /* CTRL-C */ | |
249 | case 0x04 : /* CTRL-D */ | |
250 | case 0x05 : /* CTRL-E */ | |
251 | case 0x11 : /* CTRL-Q */ | |
252 | case 0x13 : /* CTRL-S */ | |
253 | case 0x14 : /* CTRL-T */ | |
254 | case 0x1b : /* CTRL-[ (aka ESC) */ | |
255 | case 0x1c : /* CTRL-\ */ | |
256 | if (putchar(0x01) < 0) | |
257 | return (-1); | |
258 | if (putchar(*buf ^ 0x40) < 0) | |
259 | return (-1); | |
260 | break; | |
261 | ||
262 | default : | |
263 | if (putchar(*buf) < 0) | |
264 | return (-1); | |
265 | break; | |
266 | } | |
267 | ||
268 | return (bytes); | |
269 | } | |
270 | ||
271 | ||
272 | /* | |
bc44d920 | 273 | * End of "$Id: tbcp.c 6649 2007-07-11 21:46:42Z mike $". |
bd7854cb | 274 | */ |