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