]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/bcp.c
2 * "$Id: bcp.c 6800 2007-08-16 18:28:44Z mike $"
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/string-private.h>
29 #include <cups/cups.h>
37 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
38 static size_t pswrite(const char *buf
, size_t bytes
, FILE *fp
);
42 * 'main()' - Main entry...
45 int /* O - Exit status */
46 main(int argc
, /* I - Number of command-line args */
47 char *argv
[]) /* I - Command-line arguments */
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 */
57 * Check command-line...
60 if (argc
< 6 || argc
> 7)
62 fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr
);
73 copies
= atoi(argv
[4]);
74 fp
= fopen(argv
[6], "rb");
84 * Open the PPD file as needed...
87 ppd
= ppdOpenFile(getenv("PPD"));
90 * Copy the print file to stdout...
97 if (ppd
&& ppd
->jcl_begin
)
98 fputs(ppd
->jcl_begin
, stdout
);
99 if (ppd
&& ppd
->jcl_ps
)
100 fputs(ppd
->jcl_ps
, stdout
);
102 if (!ppd
|| ppd
->language_level
== 1)
105 * Use setsoftwareiomode for BCP mode...
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}");
122 * Use setdevparams for BCP mode...
125 puts("%!PS-Adobe-3.0");
126 puts("%%Title: (BCP - Level 2)");
127 puts("%%EndComments");
128 puts("currentsysparams");
129 puts("/CurInputDevice 2 copy known {");
131 puts("<</Protocol /Binary>> setdevparams");
138 if (ppd
&& ppd
->jcl_end
)
139 fputs(ppd
->jcl_end
, stdout
);
140 else if (!ppd
|| ppd
->num_filters
== 0)
144 * Loop until we see end-of-file...
149 linelen
= sizeof(line
);
150 if (psgets(line
, &linelen
, fp
) == NULL
)
153 while (pswrite(line
, linelen
, stdout
) > 0);
163 * 'psgets()' - Get a line from a file.
167 * This function differs from the gets() function in that it
168 * handles any combination of CR, LF, or CR LF to end input
172 static char * /* O - String or NULL if EOF */
173 psgets(char *buf
, /* I - Buffer to read into */
174 size_t *bytes
, /* IO - Length of buffer */
175 FILE *fp
) /* I - File to read from */
177 char *bufptr
; /* Pointer into buffer */
178 int ch
; /* Character from file */
179 size_t len
; /* Max length of string */
186 while ((bufptr
- buf
) < len
)
188 if ((ch
= getc(fp
)) == EOF
)
194 * Got a CR; see if there is a LF as well...
199 if (ch
!= EOF
&& ch
!= '\n')
201 ungetc(ch
, fp
); /* Nope, save it for later... */
215 * Add a trailing newline if it is there...
218 if (ch
== '\n' || ch
== '\r')
220 if ((bufptr
- buf
) < len
)
227 * Nul-terminate the string and return it (or NULL for EOF).
231 *bytes
= bufptr
- buf
;
233 if (ch
== EOF
&& bufptr
== buf
)
241 * 'pswrite()' - Write data from a file.
244 static size_t /* O - Number of bytes written */
245 pswrite(const char *buf
, /* I - Buffer to write */
246 size_t bytes
, /* I - Bytes to write */
247 FILE *fp
) /* I - File to write to */
249 size_t count
; /* Remaining bytes */
252 for (count
= bytes
; count
> 0; count
--, buf
++)
255 case 0x04 : /* CTRL-D */
259 * Don't quote the last CTRL-D...
266 case 0x01 : /* CTRL-A */
267 case 0x03 : /* CTRL-C */
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)
275 if (putchar(*buf
^ 0x40) < 0)
280 if (putchar(*buf
) < 0)
290 * End of "$Id: bcp.c 6800 2007-08-16 18:28:44Z mike $".