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