]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/bcp.c
2 * "$Id: bcp.c 6649 2007-07-11 21:46:42Z mike $"
4 * TBCP port monitor for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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.
22 * Include necessary headers...
25 #include <cups/string.h>
26 #include <cups/cups.h>
33 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
34 static size_t pswrite(const char *buf
, size_t bytes
, FILE *fp
);
38 * 'main()' - Main entry...
41 int /* O - Exit status */
42 main(int argc
, /* I - Number of command-line args */
43 char *argv
[]) /* I - Command-line arguments */
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 */
53 * Check command-line...
56 if (argc
< 6 || argc
> 7)
58 fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr
);
69 copies
= atoi(argv
[4]);
70 fp
= fopen(argv
[6], "rb");
80 * Open the PPD file as needed...
83 ppd
= ppdOpenFile(getenv("PPD"));
86 * Copy the print file to stdout...
93 if (ppd
&& ppd
->jcl_begin
)
94 fputs(ppd
->jcl_begin
, stdout
);
95 if (ppd
&& ppd
->jcl_ps
)
96 fputs(ppd
->jcl_ps
, stdout
);
98 if (!ppd
|| ppd
->language_level
== 1)
101 * Use setsoftwareiomode for BCP mode...
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}");
118 * Use setdevparams for BCP mode...
121 puts("%!PS-Adobe-3.0");
122 puts("%%Title: (BCP - Level 2)");
123 puts("%%EndComments");
124 puts("currentsysparams");
125 puts("/CurInputDevice 2 copy known {");
127 puts("<</Protocol /Binary>> setdevparams");
134 if (ppd
&& ppd
->jcl_end
)
135 fputs(ppd
->jcl_end
, stdout
);
136 else if (!ppd
|| ppd
->num_filters
== 0)
140 * Loop until we see end-of-file...
145 linelen
= sizeof(line
);
146 if (psgets(line
, &linelen
, fp
) == NULL
)
149 while (pswrite(line
, linelen
, stdout
) > 0);
159 * 'psgets()' - Get a line from a file.
163 * This function differs from the gets() function in that it
164 * handles any combination of CR, LF, or CR LF to end input
168 static char * /* O - String or NULL if EOF */
169 psgets(char *buf
, /* I - Buffer to read into */
170 size_t *bytes
, /* IO - Length of buffer */
171 FILE *fp
) /* I - File to read from */
173 char *bufptr
; /* Pointer into buffer */
174 int ch
; /* Character from file */
175 size_t len
; /* Max length of string */
182 while ((bufptr
- buf
) < len
)
184 if ((ch
= getc(fp
)) == EOF
)
190 * Got a CR; see if there is a LF as well...
195 if (ch
!= EOF
&& ch
!= '\n')
197 ungetc(ch
, fp
); /* Nope, save it for later... */
211 * Add a trailing newline if it is there...
214 if (ch
== '\n' || ch
== '\r')
216 if ((bufptr
- buf
) < len
)
223 * Nul-terminate the string and return it (or NULL for EOF).
227 *bytes
= bufptr
- buf
;
229 if (ch
== EOF
&& bufptr
== buf
)
237 * 'pswrite()' - Write data from a file.
240 static size_t /* O - Number of bytes written */
241 pswrite(const char *buf
, /* I - Buffer to write */
242 size_t bytes
, /* I - Bytes to write */
243 FILE *fp
) /* I - File to write to */
245 size_t count
; /* Remaining bytes */
248 for (count
= bytes
; count
> 0; count
--, buf
++)
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)
261 if (putchar(*buf
^ 0x40) < 0)
266 if (putchar(*buf
) < 0)
276 * End of "$Id: bcp.c 6649 2007-07-11 21:46:42Z mike $".