]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/bcp.c
4 * TBCP port monitor for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include <cups/cups-private.h>
30 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
31 static ssize_t
pswrite(const char *buf
, size_t bytes
);
35 * 'main()' - Main entry...
38 int /* O - Exit status */
39 main(int argc
, /* I - Number of command-line args */
40 char *argv
[]) /* I - Command-line arguments */
42 FILE *fp
; /* File to print */
43 int copies
; /* Number of copies left */
44 char line
[1024]; /* Line/buffer from stream/file */
45 size_t linelen
; /* Length of line */
46 ppd_file_t
*ppd
; /* PPD file */
50 * Check command-line...
53 if (argc
< 6 || argc
> 7)
55 _cupsLangPrintf(stderr
,
56 _("Usage: %s job-id user title copies options [file]"),
68 copies
= atoi(argv
[4]);
69 fp
= fopen(argv
[6], "rb");
79 * Open the PPD file as needed...
82 ppd
= ppdOpenFile(getenv("PPD"));
85 * Copy the print file to stdout...
92 if (ppd
&& ppd
->jcl_begin
)
93 fputs(ppd
->jcl_begin
, stdout
);
94 if (ppd
&& ppd
->jcl_ps
)
95 fputs(ppd
->jcl_ps
, stdout
);
97 if (!ppd
|| ppd
->language_level
== 1)
100 * Use setsoftwareiomode for BCP mode...
103 puts("%!PS-Adobe-3.0 ExitServer");
104 puts("%%Title: (BCP - Level 1)");
105 puts("%%EndComments");
106 puts("%%BeginExitServer: 0");
107 puts("serverdict begin 0 exitserver");
108 puts("%%EndExitServer");
109 puts("statusdict begin");
110 puts("/setsoftwareiomode known {100 setsoftwareiomode}");
117 * Use setdevparams for BCP mode...
120 puts("%!PS-Adobe-3.0");
121 puts("%%Title: (BCP - Level 2)");
122 puts("%%EndComments");
123 puts("currentsysparams");
124 puts("/CurInputDevice 2 copy known {");
126 puts("<</Protocol /Binary>> setdevparams");
133 if (ppd
&& ppd
->jcl_end
)
134 fputs(ppd
->jcl_end
, stdout
);
135 else if (!ppd
|| ppd
->num_filters
== 0)
139 * Loop until we see end-of-file...
144 linelen
= sizeof(line
);
145 if (psgets(line
, &linelen
, fp
) == NULL
)
148 while (pswrite(line
, linelen
) > 0);
158 * 'psgets()' - Get a line from a file.
162 * This function differs from the gets() function in that it
163 * handles any combination of CR, LF, or CR LF to end input
167 static char * /* O - String or NULL if EOF */
168 psgets(char *buf
, /* I - Buffer to read into */
169 size_t *bytes
, /* IO - Length of buffer */
170 FILE *fp
) /* I - File to read from */
172 char *bufptr
; /* Pointer into buffer */
173 int ch
; /* Character from file */
174 size_t len
; /* Max length of string */
181 while ((size_t)(bufptr
- buf
) < len
)
183 if ((ch
= getc(fp
)) == EOF
)
189 * Got a CR; see if there is a LF as well...
194 if (ch
!= EOF
&& ch
!= '\n')
196 ungetc(ch
, fp
); /* Nope, save it for later... */
206 *bufptr
++ = (char)ch
;
210 * Add a trailing newline if it is there...
213 if (ch
== '\n' || ch
== '\r')
215 if ((size_t)(bufptr
- buf
) < len
)
216 *bufptr
++ = (char)ch
;
222 * Nul-terminate the string and return it (or NULL for EOF).
226 *bytes
= (size_t)(bufptr
- buf
);
228 if (ch
== EOF
&& bufptr
== buf
)
236 * 'pswrite()' - Write data from a file.
239 static ssize_t
/* O - Number of bytes written */
240 pswrite(const char *buf
, /* I - Buffer to write */
241 size_t bytes
) /* I - Bytes to write */
243 size_t count
; /* Remaining bytes */
246 for (count
= bytes
; count
> 0; count
--, buf
++)
249 case 0x04 : /* CTRL-D */
253 * Don't quote the last CTRL-D...
260 case 0x01 : /* CTRL-A */
261 case 0x03 : /* CTRL-C */
262 case 0x05 : /* CTRL-E */
263 case 0x11 : /* CTRL-Q */
264 case 0x13 : /* CTRL-S */
265 case 0x14 : /* CTRL-T */
266 case 0x1c : /* CTRL-\ */
267 if (putchar(0x01) < 0)
269 if (putchar(*buf
^ 0x40) < 0)
274 if (putchar(*buf
) < 0)
279 return ((ssize_t
)bytes
);