]>
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/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
) == NULL
)
98 fputs("ERROR: Empty print file!\n", stderr
);
103 * Handle leading PJL fun...
106 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
109 * Yup, we have leading PJL fun, so copy it until we hit a line
110 * with "ENTER LANGUAGE"...
113 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
115 fwrite(line
, 1, linelen
, stdout
);
117 linelen
= sizeof(line
);
118 if (psgets(line
, &linelen
, fp
) == NULL
)
125 * No PJL stuff, just add the UEL...
128 fputs("\033%-12345X", stdout
);
132 * Switch to TBCP mode...
135 fputs("\001M", stdout
);
138 * Loop until we see end-of-file...
141 while (pswrite(line
, linelen
, stdout
) > 0)
143 linelen
= sizeof(line
);
144 if (psgets(line
, &linelen
, fp
) == NULL
)
156 * 'psgets()' - Get a line from a file.
160 * This function differs from the gets() function in that it
161 * handles any combination of CR, LF, or CR LF to end input
165 static char * /* O - String or NULL if EOF */
166 psgets(char *buf
, /* I - Buffer to read into */
167 size_t *bytes
, /* IO - Length of buffer */
168 FILE *fp
) /* I - File to read from */
170 char *bufptr
; /* Pointer into buffer */
171 int ch
; /* Character from file */
172 size_t len
; /* Max length of string */
179 while ((bufptr
- buf
) < len
)
181 if ((ch
= getc(fp
)) == EOF
)
187 * Got a CR; see if there is a LF as well...
192 if (ch
!= EOF
&& ch
!= '\n')
194 ungetc(ch
, fp
); /* Nope, save it for later... */
208 * Add a trailing newline if it is there...
211 if (ch
== '\n' || ch
== '\r')
213 if ((bufptr
- buf
) < len
)
220 * Nul-terminate the string and return it (or NULL for EOF).
224 *bytes
= bufptr
- buf
;
226 if (ch
== EOF
&& bufptr
== buf
)
234 * 'pswrite()' - Write data from a file.
237 static size_t /* O - Number of bytes written */
238 pswrite(const char *buf
, /* I - Buffer to write */
239 size_t bytes
, /* I - Bytes to write */
240 FILE *fp
) /* I - File to write to */
242 size_t count
; /* Remaining bytes */
245 for (count
= bytes
; count
> 0; count
--, buf
++)
248 case 0x04 : /* CTRL-D */
252 * Don't quote the last CTRL-D...
259 case 0x01 : /* CTRL-A */
260 case 0x03 : /* CTRL-C */
261 case 0x05 : /* CTRL-E */
262 case 0x11 : /* CTRL-Q */
263 case 0x13 : /* CTRL-S */
264 case 0x14 : /* CTRL-T */
265 case 0x1b : /* CTRL-[ (aka ESC) */
266 case 0x1c : /* CTRL-\ */
267 if (putchar(0x01) < 0)
269 if (putchar(*buf
^ 0x40) < 0)
274 if (putchar(*buf
) < 0)
284 * End of "$Id: tbcp.c 6800 2007-08-16 18:28:44Z mike $".