]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/bcp.c
4 * TBCP port monitor for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1993-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry...
20 * psgets() - Get a line from a file.
21 * pswrite() - Write data from a file.
25 * Include necessary headers...
28 #include <cups/cups-private.h>
36 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
37 static size_t pswrite(const char *buf
, size_t bytes
, FILE *fp
);
41 * 'main()' - Main entry...
44 int /* O - Exit status */
45 main(int argc
, /* I - Number of command-line args */
46 char *argv
[]) /* I - Command-line arguments */
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 */
56 * Check command-line...
59 if (argc
< 6 || argc
> 7)
61 _cupsLangPrintf(stderr
,
62 _("Usage: %s job-id user title copies options [file]"),
74 copies
= atoi(argv
[4]);
75 fp
= fopen(argv
[6], "rb");
85 * Open the PPD file as needed...
88 ppd
= ppdOpenFile(getenv("PPD"));
91 * Copy the print file to stdout...
98 if (ppd
&& ppd
->jcl_begin
)
99 fputs(ppd
->jcl_begin
, stdout
);
100 if (ppd
&& ppd
->jcl_ps
)
101 fputs(ppd
->jcl_ps
, stdout
);
103 if (!ppd
|| ppd
->language_level
== 1)
106 * Use setsoftwareiomode for BCP mode...
109 puts("%!PS-Adobe-3.0 ExitServer");
110 puts("%%Title: (BCP - Level 1)");
111 puts("%%EndComments");
112 puts("%%BeginExitServer: 0");
113 puts("serverdict begin 0 exitserver");
114 puts("%%EndExitServer");
115 puts("statusdict begin");
116 puts("/setsoftwareiomode known {100 setsoftwareiomode}");
123 * Use setdevparams for BCP mode...
126 puts("%!PS-Adobe-3.0");
127 puts("%%Title: (BCP - Level 2)");
128 puts("%%EndComments");
129 puts("currentsysparams");
130 puts("/CurInputDevice 2 copy known {");
132 puts("<</Protocol /Binary>> setdevparams");
139 if (ppd
&& ppd
->jcl_end
)
140 fputs(ppd
->jcl_end
, stdout
);
141 else if (!ppd
|| ppd
->num_filters
== 0)
145 * Loop until we see end-of-file...
150 linelen
= sizeof(line
);
151 if (psgets(line
, &linelen
, fp
) == NULL
)
154 while (pswrite(line
, linelen
, stdout
) > 0);
164 * 'psgets()' - Get a line from a file.
168 * This function differs from the gets() function in that it
169 * handles any combination of CR, LF, or CR LF to end input
173 static char * /* O - String or NULL if EOF */
174 psgets(char *buf
, /* I - Buffer to read into */
175 size_t *bytes
, /* IO - Length of buffer */
176 FILE *fp
) /* I - File to read from */
178 char *bufptr
; /* Pointer into buffer */
179 int ch
; /* Character from file */
180 size_t len
; /* Max length of string */
187 while ((bufptr
- buf
) < len
)
189 if ((ch
= getc(fp
)) == EOF
)
195 * Got a CR; see if there is a LF as well...
200 if (ch
!= EOF
&& ch
!= '\n')
202 ungetc(ch
, fp
); /* Nope, save it for later... */
216 * Add a trailing newline if it is there...
219 if (ch
== '\n' || ch
== '\r')
221 if ((bufptr
- buf
) < len
)
228 * Nul-terminate the string and return it (or NULL for EOF).
232 *bytes
= bufptr
- buf
;
234 if (ch
== EOF
&& bufptr
== buf
)
242 * 'pswrite()' - Write data from a file.
245 static size_t /* O - Number of bytes written */
246 pswrite(const char *buf
, /* I - Buffer to write */
247 size_t bytes
, /* I - Bytes to write */
248 FILE *fp
) /* I - File to write to */
250 size_t count
; /* Remaining bytes */
253 for (count
= bytes
; count
> 0; count
--, buf
++)
256 case 0x04 : /* CTRL-D */
260 * Don't quote the last CTRL-D...
267 case 0x01 : /* CTRL-A */
268 case 0x03 : /* CTRL-C */
269 case 0x05 : /* CTRL-E */
270 case 0x11 : /* CTRL-Q */
271 case 0x13 : /* CTRL-S */
272 case 0x14 : /* CTRL-T */
273 case 0x1c : /* CTRL-\ */
274 if (putchar(0x01) < 0)
276 if (putchar(*buf
^ 0x40) < 0)
281 if (putchar(*buf
) < 0)