]>
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 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.
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.h>
29 #include <cups/cups.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 fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr
);
72 copies
= atoi(argv
[4]);
73 fp
= fopen(argv
[6], "rb");
83 * Open the PPD file as needed...
86 ppd
= ppdOpenFile(getenv("PPD"));
89 * Copy the print file to stdout...
96 if (ppd
&& ppd
->jcl_begin
)
97 fputs(ppd
->jcl_begin
, stdout
);
98 if (ppd
&& ppd
->jcl_ps
)
99 fputs(ppd
->jcl_ps
, stdout
);
101 if (!ppd
|| ppd
->language_level
== 1)
104 * Use setsoftwareiomode for BCP mode...
107 puts("%!PS-Adobe-3.0 ExitServer");
108 puts("%%Title: (BCP - Level 1)");
109 puts("%%EndComments");
110 puts("%%BeginExitServer: 0");
111 puts("serverdict begin 0 exitserver");
112 puts("%%EndExitServer");
113 puts("statusdict begin");
114 puts("/setsoftwareiomode known {100 setsoftwareiomode}");
121 * Use setdevparams for BCP mode...
124 puts("%!PS-Adobe-3.0");
125 puts("%%Title: (BCP - Level 2)");
126 puts("%%EndComments");
127 puts("currentsysparams");
128 puts("/CurInputDevice 2 copy known {");
130 puts("<</Protocol /Binary>> setdevparams");
137 if (ppd
&& ppd
->jcl_end
)
138 fputs(ppd
->jcl_end
, stdout
);
139 else if (!ppd
|| ppd
->num_filters
== 0)
143 * Loop until we see end-of-file...
148 linelen
= sizeof(line
);
149 if (psgets(line
, &linelen
, fp
) == NULL
)
152 while (pswrite(line
, linelen
, stdout
) > 0);
162 * 'psgets()' - Get a line from a file.
166 * This function differs from the gets() function in that it
167 * handles any combination of CR, LF, or CR LF to end input
171 static char * /* O - String or NULL if EOF */
172 psgets(char *buf
, /* I - Buffer to read into */
173 size_t *bytes
, /* IO - Length of buffer */
174 FILE *fp
) /* I - File to read from */
176 char *bufptr
; /* Pointer into buffer */
177 int ch
; /* Character from file */
178 size_t len
; /* Max length of string */
185 while ((bufptr
- buf
) < len
)
187 if ((ch
= getc(fp
)) == EOF
)
193 * Got a CR; see if there is a LF as well...
198 if (ch
!= EOF
&& ch
!= '\n')
200 ungetc(ch
, fp
); /* Nope, save it for later... */
214 * Add a trailing newline if it is there...
217 if (ch
== '\n' || ch
== '\r')
219 if ((bufptr
- buf
) < len
)
226 * Nul-terminate the string and return it (or NULL for EOF).
230 *bytes
= bufptr
- buf
;
232 if (ch
== EOF
&& bufptr
== buf
)
240 * 'pswrite()' - Write data from a file.
243 static size_t /* O - Number of bytes written */
244 pswrite(const char *buf
, /* I - Buffer to write */
245 size_t bytes
, /* I - Bytes to write */
246 FILE *fp
) /* I - File to write to */
248 size_t count
; /* Remaining bytes */
251 for (count
= bytes
; count
> 0; count
--, buf
++)
254 case 0x04 : /* CTRL-D */
258 * Don't quote the last CTRL-D...
265 case 0x01 : /* CTRL-A */
266 case 0x03 : /* CTRL-C */
267 case 0x05 : /* CTRL-E */
268 case 0x11 : /* CTRL-Q */
269 case 0x13 : /* CTRL-S */
270 case 0x14 : /* CTRL-T */
271 case 0x1c : /* CTRL-\ */
272 if (putchar(0x01) < 0)
274 if (putchar(*buf
^ 0x40) < 0)
279 if (putchar(*buf
) < 0)
289 * End of "$Id: bcp.c 6800 2007-08-16 18:28:44Z mike $".