]>
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 CUPS.
6 * Copyright 2007-2010 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-private.h>
29 #include <cups/cups.h>
37 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
38 static size_t pswrite(const char *buf
, size_t bytes
, FILE *fp
);
42 * 'main()' - Main entry...
45 int /* O - Exit status */
46 main(int argc
, /* I - Number of command-line args */
47 char *argv
[]) /* I - Command-line arguments */
49 FILE *fp
; /* File to print */
50 int copies
; /* Number of copies left */
51 char line
[1024]; /* Line/buffer from stream/file */
52 size_t linelen
; /* Length of line */
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 * Copy the print file to stdout...
91 * Read the first line...
94 linelen
= sizeof(line
);
95 if (psgets(line
, &linelen
, fp
) == NULL
)
97 fputs("ERROR: Empty print file!\n", stderr
);
102 * Handle leading PJL fun...
105 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
108 * Yup, we have leading PJL fun, so copy it until we hit a line
109 * with "ENTER LANGUAGE"...
112 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
114 fwrite(line
, 1, linelen
, stdout
);
116 linelen
= sizeof(line
);
117 if (psgets(line
, &linelen
, fp
) == NULL
)
124 * No PJL stuff, just add the UEL...
127 fputs("\033%-12345X", stdout
);
131 * Switch to TBCP mode...
134 fputs("\001M", stdout
);
137 * Loop until we see end-of-file...
140 while (pswrite(line
, linelen
, stdout
) > 0)
142 linelen
= sizeof(line
);
143 if (psgets(line
, &linelen
, fp
) == NULL
)
155 * 'psgets()' - Get a line from a file.
159 * This function differs from the gets() function in that it
160 * handles any combination of CR, LF, or CR LF to end input
164 static char * /* O - String or NULL if EOF */
165 psgets(char *buf
, /* I - Buffer to read into */
166 size_t *bytes
, /* IO - Length of buffer */
167 FILE *fp
) /* I - File to read from */
169 char *bufptr
; /* Pointer into buffer */
170 int ch
; /* Character from file */
171 size_t len
; /* Max length of string */
178 while ((bufptr
- buf
) < len
)
180 if ((ch
= getc(fp
)) == EOF
)
186 * Got a CR; see if there is a LF as well...
191 if (ch
!= EOF
&& ch
!= '\n')
193 ungetc(ch
, fp
); /* Nope, save it for later... */
207 * Add a trailing newline if it is there...
210 if (ch
== '\n' || ch
== '\r')
212 if ((bufptr
- buf
) < len
)
219 * Nul-terminate the string and return it (or NULL for EOF).
223 *bytes
= bufptr
- buf
;
225 if (ch
== EOF
&& bufptr
== buf
)
233 * 'pswrite()' - Write data from a file.
236 static size_t /* O - Number of bytes written */
237 pswrite(const char *buf
, /* I - Buffer to write */
238 size_t bytes
, /* I - Bytes to write */
239 FILE *fp
) /* I - File to write to */
241 size_t count
; /* Remaining bytes */
244 for (count
= bytes
; count
> 0; count
--, buf
++)
247 case 0x04 : /* CTRL-D */
251 * Don't quote the last CTRL-D...
258 case 0x01 : /* CTRL-A */
259 case 0x03 : /* CTRL-C */
260 case 0x05 : /* CTRL-E */
261 case 0x11 : /* CTRL-Q */
262 case 0x13 : /* CTRL-S */
263 case 0x14 : /* CTRL-T */
264 case 0x1b : /* CTRL-[ (aka ESC) */
265 case 0x1c : /* CTRL-\ */
266 if (putchar(0x01) < 0)
268 if (putchar(*buf
^ 0x40) < 0)
273 if (putchar(*buf
) < 0)
283 * End of "$Id: tbcp.c 6800 2007-08-16 18:28:44Z mike $".