]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/tbcp.c
4 * TBCP port monitor for CUPS.
6 * Copyright 2007-2012 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/cups-private.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 */
55 * Check command-line...
58 if (argc
< 6 || argc
> 7)
60 _cupsLangPrintf(stderr
,
61 _("Usage: %s job-id user title copies options [file]"),
73 copies
= atoi(argv
[4]);
74 fp
= fopen(argv
[6], "rb");
84 * Copy the print file to stdout...
92 * Read the first line...
95 linelen
= sizeof(line
);
96 if (!psgets(line
, &linelen
, fp
))
100 * Handle leading PJL fun...
103 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
106 * Yup, we have leading PJL fun, so copy it until we hit a line
107 * with "ENTER LANGUAGE"...
110 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
112 fwrite(line
, 1, linelen
, stdout
);
114 linelen
= sizeof(line
);
115 if (psgets(line
, &linelen
, fp
) == NULL
)
122 * No PJL stuff, just add the UEL...
125 fputs("\033%-12345X", stdout
);
129 * Switch to TBCP mode...
132 fputs("\001M", stdout
);
135 * Loop until we see end-of-file...
138 while (pswrite(line
, linelen
, stdout
) > 0)
140 linelen
= sizeof(line
);
141 if (psgets(line
, &linelen
, fp
) == NULL
)
153 * 'psgets()' - Get a line from a file.
157 * This function differs from the gets() function in that it
158 * handles any combination of CR, LF, or CR LF to end input
162 static char * /* O - String or NULL if EOF */
163 psgets(char *buf
, /* I - Buffer to read into */
164 size_t *bytes
, /* IO - Length of buffer */
165 FILE *fp
) /* I - File to read from */
167 char *bufptr
; /* Pointer into buffer */
168 int ch
; /* Character from file */
169 size_t len
; /* Max length of string */
176 while ((bufptr
- buf
) < len
)
178 if ((ch
= getc(fp
)) == EOF
)
184 * Got a CR; see if there is a LF as well...
189 if (ch
!= EOF
&& ch
!= '\n')
191 ungetc(ch
, fp
); /* Nope, save it for later... */
205 * Add a trailing newline if it is there...
208 if (ch
== '\n' || ch
== '\r')
210 if ((bufptr
- buf
) < len
)
217 * Nul-terminate the string and return it (or NULL for EOF).
221 *bytes
= bufptr
- buf
;
223 if (ch
== EOF
&& bufptr
== buf
)
231 * 'pswrite()' - Write data from a file.
234 static size_t /* O - Number of bytes written */
235 pswrite(const char *buf
, /* I - Buffer to write */
236 size_t bytes
, /* I - Bytes to write */
237 FILE *fp
) /* I - File to write to */
239 size_t count
; /* Remaining bytes */
242 for (count
= bytes
; count
> 0; count
--, buf
++)
245 case 0x04 : /* CTRL-D */
249 * Don't quote the last CTRL-D...
256 case 0x01 : /* CTRL-A */
257 case 0x03 : /* CTRL-C */
258 case 0x05 : /* CTRL-E */
259 case 0x11 : /* CTRL-Q */
260 case 0x13 : /* CTRL-S */
261 case 0x14 : /* CTRL-T */
262 case 0x1b : /* CTRL-[ (aka ESC) */
263 case 0x1c : /* CTRL-\ */
264 if (putchar(0x01) < 0)
266 if (putchar(*buf
^ 0x40) < 0)
271 if (putchar(*buf
) < 0)