]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/tbcp.c
2 * "$Id: tbcp.c 11558 2014-02-06 18:33:34Z msweet $"
4 * TBCP port monitor for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include <cups/cups-private.h>
30 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
31 static ssize_t
pswrite(const char *buf
, size_t bytes
);
35 * 'main()' - Main entry...
38 int /* O - Exit status */
39 main(int argc
, /* I - Number of command-line args */
40 char *argv
[]) /* I - Command-line arguments */
42 FILE *fp
; /* File to print */
43 int copies
; /* Number of copies left */
44 char line
[1024]; /* Line/buffer from stream/file */
45 size_t linelen
; /* Length of line */
49 * Check command-line...
52 if (argc
< 6 || argc
> 7)
54 _cupsLangPrintf(stderr
,
55 _("Usage: %s job-id user title copies options [file]"),
67 copies
= atoi(argv
[4]);
68 fp
= fopen(argv
[6], "rb");
78 * Copy the print file to stdout...
86 * Read the first line...
89 linelen
= sizeof(line
);
90 if (!psgets(line
, &linelen
, fp
))
94 * Handle leading PJL fun...
97 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
100 * Yup, we have leading PJL fun, so copy it until we hit a line
101 * with "ENTER LANGUAGE"...
104 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
106 fwrite(line
, 1, linelen
, stdout
);
108 linelen
= sizeof(line
);
109 if (psgets(line
, &linelen
, fp
) == NULL
)
116 * No PJL stuff, just add the UEL...
119 fputs("\033%-12345X", stdout
);
123 * Switch to TBCP mode...
126 fputs("\001M", stdout
);
129 * Loop until we see end-of-file...
132 while (pswrite(line
, linelen
) > 0)
134 linelen
= sizeof(line
);
135 if (psgets(line
, &linelen
, fp
) == NULL
)
147 * 'psgets()' - Get a line from a file.
151 * This function differs from the gets() function in that it
152 * handles any combination of CR, LF, or CR LF to end input
156 static char * /* O - String or NULL if EOF */
157 psgets(char *buf
, /* I - Buffer to read into */
158 size_t *bytes
, /* IO - Length of buffer */
159 FILE *fp
) /* I - File to read from */
161 char *bufptr
; /* Pointer into buffer */
162 int ch
; /* Character from file */
163 size_t len
; /* Max length of string */
170 while ((size_t)(bufptr
- buf
) < len
)
172 if ((ch
= getc(fp
)) == EOF
)
178 * Got a CR; see if there is a LF as well...
183 if (ch
!= EOF
&& ch
!= '\n')
185 ungetc(ch
, fp
); /* Nope, save it for later... */
195 *bufptr
++ = (char)ch
;
199 * Add a trailing newline if it is there...
202 if (ch
== '\n' || ch
== '\r')
204 if ((size_t)(bufptr
- buf
) < len
)
205 *bufptr
++ = (char)ch
;
211 * Nul-terminate the string and return it (or NULL for EOF).
215 *bytes
= (size_t)(bufptr
- buf
);
217 if (ch
== EOF
&& bufptr
== buf
)
225 * 'pswrite()' - Write data from a file.
228 static ssize_t
/* O - Number of bytes written */
229 pswrite(const char *buf
, /* I - Buffer to write */
230 size_t bytes
) /* I - Bytes to write */
232 size_t count
; /* Remaining bytes */
235 for (count
= bytes
; count
> 0; count
--, buf
++)
238 case 0x04 : /* CTRL-D */
242 * Don't quote the last CTRL-D...
249 case 0x01 : /* CTRL-A */
250 case 0x03 : /* CTRL-C */
251 case 0x05 : /* CTRL-E */
252 case 0x11 : /* CTRL-Q */
253 case 0x13 : /* CTRL-S */
254 case 0x14 : /* CTRL-T */
255 case 0x1b : /* CTRL-[ (aka ESC) */
256 case 0x1c : /* CTRL-\ */
257 if (putchar(0x01) < 0)
259 if (putchar(*buf
^ 0x40) < 0)
264 if (putchar(*buf
) < 0)
269 return ((ssize_t
)bytes
);
274 * End of "$Id: tbcp.c 11558 2014-02-06 18:33:34Z msweet $".