]>
Commit | Line | Data |
---|---|---|
bd7854cb | 1 | /* |
db1f069b | 2 | * "$Id: bcp.c 6802 2007-08-16 18:44:46Z 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 | * | |
db1f069b MS |
19 | * main() - Main entry... |
20 | * psgets() - Get a line from a file. | |
21 | * pswrite() - Write data from a file. | |
bd7854cb | 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 | ppd_file_t *ppd; /* PPD file */ | |
53 | ||
54 | ||
55 | /* | |
56 | * Check command-line... | |
57 | */ | |
58 | ||
59 | if (argc < 6 || argc > 7) | |
60 | { | |
61 | fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr); | |
62 | return (1); | |
63 | } | |
64 | ||
65 | if (argc == 6) | |
66 | { | |
67 | copies = 1; | |
68 | fp = stdin; | |
69 | } | |
70 | else | |
71 | { | |
72 | copies = atoi(argv[4]); | |
73 | fp = fopen(argv[6], "rb"); | |
74 | ||
75 | if (!fp) | |
76 | { | |
77 | perror(argv[6]); | |
78 | return (1); | |
79 | } | |
80 | } | |
81 | ||
82 | /* | |
83 | * Open the PPD file as needed... | |
84 | */ | |
85 | ||
86 | ppd = ppdOpenFile(getenv("PPD")); | |
87 | ||
88 | /* | |
89 | * Copy the print file to stdout... | |
90 | */ | |
91 | ||
92 | while (copies > 0) | |
93 | { | |
94 | copies --; | |
95 | ||
96 | if (ppd && ppd->jcl_begin) | |
97 | fputs(ppd->jcl_begin, stdout); | |
98 | if (ppd && ppd->jcl_ps) | |
99 | fputs(ppd->jcl_ps, stdout); | |
100 | ||
101 | if (!ppd || ppd->language_level == 1) | |
102 | { | |
103 | /* | |
104 | * Use setsoftwareiomode for BCP mode... | |
105 | */ | |
106 | ||
107 | puts("%!PS-Adobe-3.0 ExitServer"); | |
108 | puts("%%Title: (BCP - Level 1)"); | |
109 | puts("%%EndComments"); | |
110 | puts("%%BeginExitServer: 0"); | |
111 | puts("serverdict begin 0 exitserver"); | |
112 | puts("%%EndExitServer"); | |
113 | puts("statusdict begin"); | |
114 | puts("/setsoftwareiomode known {100 setsoftwareiomode}"); | |
115 | puts("end"); | |
116 | puts("%EOF"); | |
117 | } | |
118 | else | |
119 | { | |
120 | /* | |
121 | * Use setdevparams for BCP mode... | |
122 | */ | |
123 | ||
124 | puts("%!PS-Adobe-3.0"); | |
125 | puts("%%Title: (BCP - Level 2)"); | |
126 | puts("%%EndComments"); | |
127 | puts("currentsysparams"); | |
128 | puts("/CurInputDevice 2 copy known {"); | |
129 | puts("get"); | |
130 | puts("<</Protocol /Binary>> setdevparams"); | |
131 | puts("}{"); | |
132 | puts("pop pop"); | |
133 | puts("} ifelse"); | |
134 | puts("%EOF"); | |
135 | } | |
136 | ||
137 | if (ppd && ppd->jcl_end) | |
138 | fputs(ppd->jcl_end, stdout); | |
139 | else if (!ppd || ppd->num_filters == 0) | |
140 | putchar(0x04); | |
141 | ||
142 | /* | |
143 | * Loop until we see end-of-file... | |
144 | */ | |
145 | ||
146 | do | |
147 | { | |
148 | linelen = sizeof(line); | |
149 | if (psgets(line, &linelen, fp) == NULL) | |
150 | break; | |
151 | } | |
152 | while (pswrite(line, linelen, stdout) > 0); | |
153 | ||
154 | fflush(stdout); | |
155 | } | |
156 | ||
157 | return (0); | |
158 | } | |
159 | ||
160 | ||
161 | /* | |
162 | * 'psgets()' - Get a line from a file. | |
163 | * | |
164 | * Note: | |
165 | * | |
166 | * This function differs from the gets() function in that it | |
167 | * handles any combination of CR, LF, or CR LF to end input | |
168 | * lines. | |
169 | */ | |
170 | ||
171 | static char * /* O - String or NULL if EOF */ | |
172 | psgets(char *buf, /* I - Buffer to read into */ | |
173 | size_t *bytes, /* IO - Length of buffer */ | |
174 | FILE *fp) /* I - File to read from */ | |
175 | { | |
176 | char *bufptr; /* Pointer into buffer */ | |
177 | int ch; /* Character from file */ | |
178 | size_t len; /* Max length of string */ | |
179 | ||
180 | ||
181 | len = *bytes - 1; | |
182 | bufptr = buf; | |
183 | ch = EOF; | |
184 | ||
185 | while ((bufptr - buf) < len) | |
186 | { | |
187 | if ((ch = getc(fp)) == EOF) | |
188 | break; | |
189 | ||
190 | if (ch == '\r') | |
191 | { | |
192 | /* | |
193 | * Got a CR; see if there is a LF as well... | |
194 | */ | |
195 | ||
196 | ch = getc(fp); | |
197 | ||
198 | if (ch != EOF && ch != '\n') | |
199 | { | |
200 | ungetc(ch, fp); /* Nope, save it for later... */ | |
201 | ch = '\r'; | |
202 | } | |
203 | else | |
204 | *bufptr++ = '\r'; | |
205 | break; | |
206 | } | |
207 | else if (ch == '\n') | |
208 | break; | |
209 | else | |
210 | *bufptr++ = ch; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Add a trailing newline if it is there... | |
215 | */ | |
216 | ||
217 | if (ch == '\n' || ch == '\r') | |
218 | { | |
219 | if ((bufptr - buf) < len) | |
220 | *bufptr++ = ch; | |
221 | else | |
222 | ungetc(ch, fp); | |
223 | } | |
224 | ||
225 | /* | |
226 | * Nul-terminate the string and return it (or NULL for EOF). | |
227 | */ | |
228 | ||
229 | *bufptr = '\0'; | |
230 | *bytes = bufptr - buf; | |
231 | ||
232 | if (ch == EOF && bufptr == buf) | |
233 | return (NULL); | |
234 | else | |
235 | return (buf); | |
236 | } | |
237 | ||
238 | ||
239 | /* | |
240 | * 'pswrite()' - Write data from a file. | |
241 | */ | |
242 | ||
243 | static size_t /* O - Number of bytes written */ | |
244 | pswrite(const char *buf, /* I - Buffer to write */ | |
245 | size_t bytes, /* I - Bytes to write */ | |
246 | FILE *fp) /* I - File to write to */ | |
247 | { | |
248 | size_t count; /* Remaining bytes */ | |
249 | ||
250 | ||
251 | for (count = bytes; count > 0; count --, buf ++) | |
252 | switch (*buf) | |
253 | { | |
db1f069b MS |
254 | case 0x04 : /* CTRL-D */ |
255 | if (bytes == 1) | |
256 | { | |
257 | /* | |
258 | * Don't quote the last CTRL-D... | |
259 | */ | |
260 | ||
261 | putchar(0x04); | |
262 | break; | |
263 | } | |
264 | ||
bd7854cb | 265 | case 0x01 : /* CTRL-A */ |
266 | case 0x03 : /* CTRL-C */ | |
bd7854cb | 267 | case 0x05 : /* CTRL-E */ |
268 | case 0x11 : /* CTRL-Q */ | |
269 | case 0x13 : /* CTRL-S */ | |
270 | case 0x14 : /* CTRL-T */ | |
271 | case 0x1c : /* CTRL-\ */ | |
272 | if (putchar(0x01) < 0) | |
273 | return (-1); | |
274 | if (putchar(*buf ^ 0x40) < 0) | |
275 | return (-1); | |
276 | break; | |
277 | ||
278 | default : | |
279 | if (putchar(*buf) < 0) | |
280 | return (-1); | |
281 | break; | |
282 | } | |
283 | ||
284 | return (bytes); | |
285 | } | |
286 | ||
287 | ||
288 | /* | |
db1f069b | 289 | * End of "$Id: bcp.c 6802 2007-08-16 18:44:46Z mike $". |
bd7854cb | 290 | */ |