]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/tbcp.c
2 * "$Id: tbcp.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 */
55 * Check command-line...
58 if (argc
< 6 || argc
> 7)
60 fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr
);
71 copies
= atoi(argv
[4]);
72 fp
= fopen(argv
[6], "rb");
82 * Copy the print file to stdout...
90 * Read the first line...
93 linelen
= sizeof(line
);
94 if (psgets(line
, &linelen
, fp
) == NULL
)
96 fputs("ERROR: Empty print file!\n", stderr
);
101 * Handle leading PJL fun...
104 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
107 * Yup, we have leading PJL fun, so copy it until we hit a line
108 * with "ENTER LANGUAGE"...
111 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
113 fwrite(line
, 1, linelen
, stdout
);
115 linelen
= sizeof(line
);
116 if (psgets(line
, &linelen
, fp
) == NULL
)
123 * No PJL stuff, just add the UEL...
126 fputs("\033%-12345X", stdout
);
130 * Switch to TBCP mode...
133 fputs("\001M", stdout
);
136 * Loop until we see end-of-file...
139 while (pswrite(line
, linelen
, stdout
) > 0)
141 linelen
= sizeof(line
);
142 if (psgets(line
, &linelen
, fp
) == NULL
)
154 * 'psgets()' - Get a line from a file.
158 * This function differs from the gets() function in that it
159 * handles any combination of CR, LF, or CR LF to end input
163 static char * /* O - String or NULL if EOF */
164 psgets(char *buf
, /* I - Buffer to read into */
165 size_t *bytes
, /* IO - Length of buffer */
166 FILE *fp
) /* I - File to read from */
168 char *bufptr
; /* Pointer into buffer */
169 int ch
; /* Character from file */
170 size_t len
; /* Max length of string */
177 while ((bufptr
- buf
) < len
)
179 if ((ch
= getc(fp
)) == EOF
)
185 * Got a CR; see if there is a LF as well...
190 if (ch
!= EOF
&& ch
!= '\n')
192 ungetc(ch
, fp
); /* Nope, save it for later... */
206 * Add a trailing newline if it is there...
209 if (ch
== '\n' || ch
== '\r')
211 if ((bufptr
- buf
) < len
)
218 * Nul-terminate the string and return it (or NULL for EOF).
222 *bytes
= bufptr
- buf
;
224 if (ch
== EOF
&& bufptr
== buf
)
232 * 'pswrite()' - Write data from a file.
235 static size_t /* O - Number of bytes written */
236 pswrite(const char *buf
, /* I - Buffer to write */
237 size_t bytes
, /* I - Bytes to write */
238 FILE *fp
) /* I - File to write to */
240 size_t count
; /* Remaining bytes */
243 for (count
= bytes
; count
> 0; count
--, buf
++)
246 case 0x04 : /* CTRL-D */
250 * Don't quote the last CTRL-D...
257 case 0x01 : /* CTRL-A */
258 case 0x03 : /* CTRL-C */
259 case 0x05 : /* CTRL-E */
260 case 0x11 : /* CTRL-Q */
261 case 0x13 : /* CTRL-S */
262 case 0x14 : /* CTRL-T */
263 case 0x1b : /* CTRL-[ (aka ESC) */
264 case 0x1c : /* CTRL-\ */
265 if (putchar(0x01) < 0)
267 if (putchar(*buf
^ 0x40) < 0)
272 if (putchar(*buf
) < 0)
282 * End of "$Id: tbcp.c 6800 2007-08-16 18:28:44Z mike $".