]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/bcp.c
2 * TBCP port monitor for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1993-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
22 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
23 static ssize_t
pswrite(const char *buf
, size_t bytes
);
27 * 'main()' - Main entry...
30 int /* O - Exit status */
31 main(int argc
, /* I - Number of command-line args */
32 char *argv
[]) /* I - Command-line arguments */
34 FILE *fp
; /* File to print */
35 int copies
; /* Number of copies left */
36 char line
[1024]; /* Line/buffer from stream/file */
37 size_t linelen
; /* Length of line */
38 ppd_file_t
*ppd
; /* PPD file */
42 * Check command-line...
45 if (argc
< 6 || argc
> 7)
47 _cupsLangPrintf(stderr
,
48 _("Usage: %s job-id user title copies options [file]"),
60 copies
= atoi(argv
[4]);
61 fp
= fopen(argv
[6], "rb");
71 * Open the PPD file as needed...
74 ppd
= ppdOpenFile(getenv("PPD"));
77 * Copy the print file to stdout...
84 if (ppd
&& ppd
->jcl_begin
)
85 fputs(ppd
->jcl_begin
, stdout
);
86 if (ppd
&& ppd
->jcl_ps
)
87 fputs(ppd
->jcl_ps
, stdout
);
89 if (!ppd
|| ppd
->language_level
== 1)
92 * Use setsoftwareiomode for BCP mode...
95 puts("%!PS-Adobe-3.0 ExitServer");
96 puts("%%Title: (BCP - Level 1)");
97 puts("%%EndComments");
98 puts("%%BeginExitServer: 0");
99 puts("serverdict begin 0 exitserver");
100 puts("%%EndExitServer");
101 puts("statusdict begin");
102 puts("/setsoftwareiomode known {100 setsoftwareiomode}");
109 * Use setdevparams for BCP mode...
112 puts("%!PS-Adobe-3.0");
113 puts("%%Title: (BCP - Level 2)");
114 puts("%%EndComments");
115 puts("currentsysparams");
116 puts("/CurInputDevice 2 copy known {");
118 puts("<</Protocol /Binary>> setdevparams");
125 if (ppd
&& ppd
->jcl_end
)
126 fputs(ppd
->jcl_end
, stdout
);
127 else if (!ppd
|| ppd
->num_filters
== 0)
131 * Loop until we see end-of-file...
136 linelen
= sizeof(line
);
137 if (psgets(line
, &linelen
, fp
) == NULL
)
140 while (pswrite(line
, linelen
) > 0);
150 * 'psgets()' - Get a line from a file.
154 * This function differs from the gets() function in that it
155 * handles any combination of CR, LF, or CR LF to end input
159 static char * /* O - String or NULL if EOF */
160 psgets(char *buf
, /* I - Buffer to read into */
161 size_t *bytes
, /* IO - Length of buffer */
162 FILE *fp
) /* I - File to read from */
164 char *bufptr
; /* Pointer into buffer */
165 int ch
; /* Character from file */
166 size_t len
; /* Max length of string */
173 while ((size_t)(bufptr
- buf
) < len
)
175 if ((ch
= getc(fp
)) == EOF
)
181 * Got a CR; see if there is a LF as well...
186 if (ch
!= EOF
&& ch
!= '\n')
188 ungetc(ch
, fp
); /* Nope, save it for later... */
198 *bufptr
++ = (char)ch
;
202 * Add a trailing newline if it is there...
205 if (ch
== '\n' || ch
== '\r')
207 if ((size_t)(bufptr
- buf
) < len
)
208 *bufptr
++ = (char)ch
;
214 * Nul-terminate the string and return it (or NULL for EOF).
218 *bytes
= (size_t)(bufptr
- buf
);
220 if (ch
== EOF
&& bufptr
== buf
)
228 * 'pswrite()' - Write data from a file.
231 static ssize_t
/* O - Number of bytes written */
232 pswrite(const char *buf
, /* I - Buffer to write */
233 size_t bytes
) /* I - Bytes to write */
235 size_t count
; /* Remaining bytes */
238 for (count
= bytes
; count
> 0; count
--, buf
++)
241 case 0x04 : /* CTRL-D */
245 * Don't quote the last CTRL-D...
252 case 0x01 : /* CTRL-A */
253 case 0x03 : /* CTRL-C */
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)
271 return ((ssize_t
)bytes
);