]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/tbcp.c
4 * TBCP port monitor for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Main entry...
29 * psgets() - Get a line from a file.
30 * pswrite() - Write data from a file.
34 * Include necessary headers...
37 #include <cups/string.h>
38 #include <cups/cups.h>
45 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
46 static size_t pswrite(const char *buf
, size_t bytes
, FILE *fp
);
50 * 'main()' - Main entry...
53 int /* O - Exit status */
54 main(int argc
, /* I - Number of command-line args */
55 char *argv
[]) /* I - Command-line arguments */
57 FILE *fp
; /* File to print */
58 int copies
; /* Number of copies left */
59 char line
[1024]; /* Line/buffer from stream/file */
60 size_t linelen
; /* Length of line */
64 * Check command-line...
67 if (argc
< 6 || argc
> 7)
69 fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr
);
80 copies
= atoi(argv
[4]);
81 fp
= fopen(argv
[6], "rb");
91 * Copy the print file to stdout...
99 * Read the first line...
102 linelen
= sizeof(line
);
103 if (psgets(line
, &linelen
, fp
) == NULL
)
105 fputs("ERROR: Empty print file!\n", stderr
);
110 * Handle leading PJL fun...
113 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
116 * Yup, we have leading PJL fun, so copy it until we hit a line
117 * with "ENTER LANGUAGE"...
120 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
122 fwrite(line
, 1, linelen
, stdout
);
124 linelen
= sizeof(line
);
125 if (psgets(line
, &linelen
, fp
) == NULL
)
132 * No PJL stuff, add it...
135 puts("\033%-12345X@PJL");
136 puts("@PJL ENTER LANGUAGE = POSTSCRIPT");
140 * Switch to TBCP mode...
143 fputs("\001M", stdout
);
146 * Loop until we see end-of-file...
149 while (pswrite(line
, linelen
, stdout
) > 0)
151 linelen
= sizeof(line
);
152 if (psgets(line
, &linelen
, fp
) == NULL
)
164 * 'psgets()' - Get a line from a file.
168 * This function differs from the gets() function in that it
169 * handles any combination of CR, LF, or CR LF to end input
173 static char * /* O - String or NULL if EOF */
174 psgets(char *buf
, /* I - Buffer to read into */
175 size_t *bytes
, /* IO - Length of buffer */
176 FILE *fp
) /* I - File to read from */
178 char *bufptr
; /* Pointer into buffer */
179 int ch
; /* Character from file */
180 size_t len
; /* Max length of string */
187 while ((bufptr
- buf
) < len
)
189 if ((ch
= getc(fp
)) == EOF
)
195 * Got a CR; see if there is a LF as well...
200 if (ch
!= EOF
&& ch
!= '\n')
202 ungetc(ch
, fp
); /* Nope, save it for later... */
216 * Add a trailing newline if it is there...
219 if (ch
== '\n' || ch
== '\r')
221 if ((bufptr
- buf
) < len
)
228 * Nul-terminate the string and return it (or NULL for EOF).
232 *bytes
= bufptr
- buf
;
234 if (ch
== EOF
&& bufptr
== buf
)
242 * 'pswrite()' - Write data from a file.
245 static size_t /* O - Number of bytes written */
246 pswrite(const char *buf
, /* I - Buffer to write */
247 size_t bytes
, /* I - Bytes to write */
248 FILE *fp
) /* I - File to write to */
250 size_t count
; /* Remaining bytes */
253 for (count
= bytes
; count
> 0; count
--, buf
++)
256 case 0x01 : /* CTRL-A */
257 case 0x03 : /* CTRL-C */
258 case 0x04 : /* CTRL-D */
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)