]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/tbcp.c
be7a788470269f86564d577dd021d293362f6bb2
2 * TBCP port monitor for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1993-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include <cups/cups-private.h>
28 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
29 static ssize_t
pswrite(const char *buf
, size_t bytes
);
33 * 'main()' - Main entry...
36 int /* O - Exit status */
37 main(int argc
, /* I - Number of command-line args */
38 char *argv
[]) /* I - Command-line arguments */
40 FILE *fp
; /* File to print */
41 int copies
; /* Number of copies left */
42 char line
[1024]; /* Line/buffer from stream/file */
43 size_t linelen
; /* Length of line */
47 * Check command-line...
50 if (argc
< 6 || argc
> 7)
52 _cupsLangPrintf(stderr
,
53 _("Usage: %s job-id user title copies options [file]"),
65 copies
= atoi(argv
[4]);
66 fp
= fopen(argv
[6], "rb");
76 * Copy the print file to stdout...
84 * Read the first line...
87 linelen
= sizeof(line
);
88 if (!psgets(line
, &linelen
, fp
))
92 * Handle leading PJL fun...
95 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
98 * Yup, we have leading PJL fun, so copy it until we hit a line
99 * with "ENTER LANGUAGE"...
102 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
104 fwrite(line
, 1, linelen
, stdout
);
106 linelen
= sizeof(line
);
107 if (psgets(line
, &linelen
, fp
) == NULL
)
114 * No PJL stuff, just add the UEL...
117 fputs("\033%-12345X", stdout
);
121 * Switch to TBCP mode...
124 fputs("\001M", stdout
);
127 * Loop until we see end-of-file...
130 while (pswrite(line
, linelen
) > 0)
132 linelen
= sizeof(line
);
133 if (psgets(line
, &linelen
, fp
) == NULL
)
145 * 'psgets()' - Get a line from a file.
149 * This function differs from the gets() function in that it
150 * handles any combination of CR, LF, or CR LF to end input
154 static char * /* O - String or NULL if EOF */
155 psgets(char *buf
, /* I - Buffer to read into */
156 size_t *bytes
, /* IO - Length of buffer */
157 FILE *fp
) /* I - File to read from */
159 char *bufptr
; /* Pointer into buffer */
160 int ch
; /* Character from file */
161 size_t len
; /* Max length of string */
168 while ((size_t)(bufptr
- buf
) < len
)
170 if ((ch
= getc(fp
)) == EOF
)
176 * Got a CR; see if there is a LF as well...
181 if (ch
!= EOF
&& ch
!= '\n')
183 ungetc(ch
, fp
); /* Nope, save it for later... */
193 *bufptr
++ = (char)ch
;
197 * Add a trailing newline if it is there...
200 if (ch
== '\n' || ch
== '\r')
202 if ((size_t)(bufptr
- buf
) < len
)
203 *bufptr
++ = (char)ch
;
209 * Nul-terminate the string and return it (or NULL for EOF).
213 *bytes
= (size_t)(bufptr
- buf
);
215 if (ch
== EOF
&& bufptr
== buf
)
223 * 'pswrite()' - Write data from a file.
226 static ssize_t
/* O - Number of bytes written */
227 pswrite(const char *buf
, /* I - Buffer to write */
228 size_t bytes
) /* I - Bytes to write */
230 size_t count
; /* Remaining bytes */
233 for (count
= bytes
; count
> 0; count
--, buf
++)
236 case 0x04 : /* CTRL-D */
240 * Don't quote the last CTRL-D...
247 case 0x01 : /* CTRL-A */
248 case 0x03 : /* CTRL-C */
249 case 0x05 : /* CTRL-E */
250 case 0x11 : /* CTRL-Q */
251 case 0x13 : /* CTRL-S */
252 case 0x14 : /* CTRL-T */
253 case 0x1b : /* CTRL-[ (aka ESC) */
254 case 0x1c : /* CTRL-\ */
255 if (putchar(0x01) < 0)
257 if (putchar(*buf
^ 0x40) < 0)
262 if (putchar(*buf
) < 0)
267 return ((ssize_t
)bytes
);