]>
git.ipfire.org Git - thirdparty/cups.git/blob - monitor/tbcp.c
2 * TBCP port monitor for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1993-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
22 static char *psgets(char *buf
, size_t *bytes
, FILE *fp
);
23 static ssize_t
pswrite(const char *buf
, size_t bytes
);
27 * 'main()' - Main entry...
30 int /* O - Exit status */
31 main(int argc
, /* I - Number of command-line args */
32 char *argv
[]) /* I - Command-line arguments */
34 FILE *fp
; /* File to print */
35 int copies
; /* Number of copies left */
36 char line
[1024]; /* Line/buffer from stream/file */
37 size_t linelen
; /* Length of line */
41 * Check command-line...
44 if (argc
< 6 || argc
> 7)
46 _cupsLangPrintf(stderr
,
47 _("Usage: %s job-id user title copies options [file]"),
59 copies
= atoi(argv
[4]);
60 fp
= fopen(argv
[6], "rb");
70 * Copy the print file to stdout...
78 * Read the first line...
81 linelen
= sizeof(line
);
82 if (!psgets(line
, &linelen
, fp
))
86 * Handle leading PJL fun...
89 if (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
92 * Yup, we have leading PJL fun, so copy it until we hit a line
93 * with "ENTER LANGUAGE"...
96 while (strstr(line
, "ENTER LANGUAGE") == NULL
)
98 fwrite(line
, 1, linelen
, stdout
);
100 linelen
= sizeof(line
);
101 if (psgets(line
, &linelen
, fp
) == NULL
)
108 * No PJL stuff, just add the UEL...
111 fputs("\033%-12345X", stdout
);
115 * Switch to TBCP mode...
118 fputs("\001M", stdout
);
121 * Loop until we see end-of-file...
124 while (pswrite(line
, linelen
) > 0)
126 linelen
= sizeof(line
);
127 if (psgets(line
, &linelen
, fp
) == NULL
)
139 * 'psgets()' - Get a line from a file.
143 * This function differs from the gets() function in that it
144 * handles any combination of CR, LF, or CR LF to end input
148 static char * /* O - String or NULL if EOF */
149 psgets(char *buf
, /* I - Buffer to read into */
150 size_t *bytes
, /* IO - Length of buffer */
151 FILE *fp
) /* I - File to read from */
153 char *bufptr
; /* Pointer into buffer */
154 int ch
; /* Character from file */
155 size_t len
; /* Max length of string */
162 while ((size_t)(bufptr
- buf
) < len
)
164 if ((ch
= getc(fp
)) == EOF
)
170 * Got a CR; see if there is a LF as well...
175 if (ch
!= EOF
&& ch
!= '\n')
177 ungetc(ch
, fp
); /* Nope, save it for later... */
187 *bufptr
++ = (char)ch
;
191 * Add a trailing newline if it is there...
194 if (ch
== '\n' || ch
== '\r')
196 if ((size_t)(bufptr
- buf
) < len
)
197 *bufptr
++ = (char)ch
;
203 * Nul-terminate the string and return it (or NULL for EOF).
207 *bytes
= (size_t)(bufptr
- buf
);
209 if (ch
== EOF
&& bufptr
== buf
)
217 * 'pswrite()' - Write data from a file.
220 static ssize_t
/* O - Number of bytes written */
221 pswrite(const char *buf
, /* I - Buffer to write */
222 size_t bytes
) /* I - Bytes to write */
224 size_t count
; /* Remaining bytes */
227 for (count
= bytes
; count
> 0; count
--, buf
++)
230 case 0x04 : /* CTRL-D */
234 * Don't quote the last CTRL-D...
241 case 0x01 : /* CTRL-A */
242 case 0x03 : /* CTRL-C */
243 case 0x05 : /* CTRL-E */
244 case 0x11 : /* CTRL-Q */
245 case 0x13 : /* CTRL-S */
246 case 0x14 : /* CTRL-T */
247 case 0x1b : /* CTRL-[ (aka ESC) */
248 case 0x1c : /* CTRL-\ */
249 if (putchar(0x01) < 0)
251 if (putchar(*buf
^ 0x40) < 0)
256 if (putchar(*buf
) < 0)
261 return ((ssize_t
)bytes
);