]> git.ipfire.org Git - thirdparty/cups.git/blame - monitor/tbcp.c
Remove svn:keywords since they cause svn_load_dirs.pl to complain about every file.
[thirdparty/cups.git] / monitor / tbcp.c
CommitLineData
bd7854cb 1/*
c07d5b2d 2 * "$Id: tbcp.c 177 2006-06-21 00:20:03Z jlovell $"
bd7854cb 3 *
4 * TBCP port monitor for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2006 by Easy Software Products.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * main() - Main entry...
29 * psgets() - Get a line from a file.
30 * pswrite() - Write data from a file.
31 */
32
33/*
34 * Include necessary headers...
35 */
36
37#include <cups/string.h>
38#include <cups/cups.h>
39
40
41/*
42 * Local functions...
43 */
44
45static char *psgets(char *buf, size_t *bytes, FILE *fp);
46static size_t pswrite(const char *buf, size_t bytes, FILE *fp);
47
48
49/*
50 * 'main()' - Main entry...
51 */
52
53int /* O - Exit status */
54main(int argc, /* I - Number of command-line args */
55 char *argv[]) /* I - Command-line arguments */
56{
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 */
61
62
63 /*
64 * Check command-line...
65 */
66
67 if (argc < 6 || argc > 7)
68 {
69 fputs("ERROR: tbcp job-id user title copies options [file]\n", stderr);
70 return (1);
71 }
72
73 if (argc == 6)
74 {
75 copies = 1;
76 fp = stdin;
77 }
78 else
79 {
80 copies = atoi(argv[4]);
81 fp = fopen(argv[6], "rb");
82
83 if (!fp)
84 {
85 perror(argv[6]);
86 return (1);
87 }
88 }
89
90 /*
91 * Copy the print file to stdout...
92 */
93
94 while (copies > 0)
95 {
96 copies --;
97
98 /*
99 * Read the first line...
100 */
101
102 linelen = sizeof(line);
103 if (psgets(line, &linelen, fp) == NULL)
104 {
105 fputs("ERROR: Empty print file!\n", stderr);
106 return (1);
107 }
108
109 /*
110 * Handle leading PJL fun...
111 */
112
113 if (!strncmp(line, "\033%-12345X", 9) || !strncmp(line, "@PJL ", 5))
114 {
115 /*
116 * Yup, we have leading PJL fun, so copy it until we hit a line
117 * with "ENTER LANGUAGE"...
118 */
119
120 while (strstr(line, "ENTER LANGUAGE") == NULL)
121 {
122 fwrite(line, 1, linelen, stdout);
123
124 linelen = sizeof(line);
125 if (psgets(line, &linelen, fp) == NULL)
126 break;
127 }
128 }
129 else
130 {
131 /*
132 * No PJL stuff, add it...
133 */
134
135 puts("\033%-12345X@PJL");
136 puts("@PJL ENTER LANGUAGE = POSTSCRIPT");
137 }
138
139 /*
140 * Switch to TBCP mode...
141 */
142
143 fputs("\001M", stdout);
144
145 /*
146 * Loop until we see end-of-file...
147 */
148
149 while (pswrite(line, linelen, stdout) > 0)
150 {
151 linelen = sizeof(line);
152 if (psgets(line, &linelen, fp) == NULL)
153 break;
154 }
155
156 fflush(stdout);
157 }
158
159 return (0);
160}
161
162
163/*
164 * 'psgets()' - Get a line from a file.
165 *
166 * Note:
167 *
168 * This function differs from the gets() function in that it
169 * handles any combination of CR, LF, or CR LF to end input
170 * lines.
171 */
172
173static char * /* O - String or NULL if EOF */
174psgets(char *buf, /* I - Buffer to read into */
175 size_t *bytes, /* IO - Length of buffer */
176 FILE *fp) /* I - File to read from */
177{
178 char *bufptr; /* Pointer into buffer */
179 int ch; /* Character from file */
180 size_t len; /* Max length of string */
181
182
183 len = *bytes - 1;
184 bufptr = buf;
185 ch = EOF;
186
187 while ((bufptr - buf) < len)
188 {
189 if ((ch = getc(fp)) == EOF)
190 break;
191
192 if (ch == '\r')
193 {
194 /*
195 * Got a CR; see if there is a LF as well...
196 */
197
198 ch = getc(fp);
199
200 if (ch != EOF && ch != '\n')
201 {
202 ungetc(ch, fp); /* Nope, save it for later... */
203 ch = '\r';
204 }
205 else
206 *bufptr++ = '\r';
207 break;
208 }
209 else if (ch == '\n')
210 break;
211 else
212 *bufptr++ = ch;
213 }
214
215 /*
216 * Add a trailing newline if it is there...
217 */
218
219 if (ch == '\n' || ch == '\r')
220 {
221 if ((bufptr - buf) < len)
222 *bufptr++ = ch;
223 else
224 ungetc(ch, fp);
225 }
226
227 /*
228 * Nul-terminate the string and return it (or NULL for EOF).
229 */
230
231 *bufptr = '\0';
232 *bytes = bufptr - buf;
233
234 if (ch == EOF && bufptr == buf)
235 return (NULL);
236 else
237 return (buf);
238}
239
240
241/*
242 * 'pswrite()' - Write data from a file.
243 */
244
245static size_t /* O - Number of bytes written */
246pswrite(const char *buf, /* I - Buffer to write */
247 size_t bytes, /* I - Bytes to write */
248 FILE *fp) /* I - File to write to */
249{
250 size_t count; /* Remaining bytes */
251
252
253 for (count = bytes; count > 0; count --, buf ++)
254 switch (*buf)
255 {
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)
266 return (-1);
267 if (putchar(*buf ^ 0x40) < 0)
268 return (-1);
269 break;
270
271 default :
272 if (putchar(*buf) < 0)
273 return (-1);
274 break;
275 }
276
277 return (bytes);
278}
279
280
281/*
c07d5b2d 282 * End of "$Id: tbcp.c 177 2006-06-21 00:20:03Z jlovell $".
bd7854cb 283 */