]>
Commit | Line | Data |
---|---|---|
d09495fa | 1 | /* |
2 | * "$Id: testlpd.c 5868 2006-08-23 19:39:39Z mike $" | |
3 | * | |
4 | * cups-lpd test program for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2006 by Easy Software Products, all rights reserved. | |
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 | * Contents: | |
25 | * | |
26 | * main() - Simulate an LPD client. | |
27 | * do_command() - Send the LPD command and wait for a response. | |
28 | * print_job() - Submit a file for printing. | |
29 | * print_waiting() - Print waiting jobs. | |
30 | * remove_job() - Cancel a print job. | |
31 | * status_long() - Show the long printer status. | |
32 | * status_short() - Show the short printer status. | |
33 | * usage() - Show program usage... | |
34 | */ | |
35 | ||
36 | /* | |
37 | * Include necessary headers... | |
38 | */ | |
39 | ||
40 | #include <cups/cups.h> | |
41 | #include <cups/string.h> | |
42 | #include <stdlib.h> | |
43 | #include <sys/stat.h> | |
44 | #include <errno.h> | |
45 | #include <signal.h> | |
46 | #include <unistd.h> | |
47 | #include <fcntl.h> | |
48 | ||
49 | ||
50 | /* | |
51 | * Local functions... | |
52 | */ | |
53 | ||
54 | static int do_command(int outfd, int infd, const char *command); | |
55 | static int print_job(int outfd, int infd, char *dest, char **args); | |
56 | static int print_waiting(int outfd, int infd, char *dest); | |
57 | static int remove_job(int outfd, int infd, char *dest, char **args); | |
58 | static int status_long(int outfd, int infd, char *dest, char **args); | |
59 | static int status_short(int outfd, int infd, char *dest, char **args); | |
60 | static void usage(void); | |
61 | ||
62 | ||
63 | /* | |
64 | * 'main()' - Simulate an LPD client. | |
65 | */ | |
66 | ||
67 | int /* O - Exit status */ | |
68 | main(int argc, /* I - Number of command-line arguments */ | |
69 | char *argv[]) /* I - Command-line arguments */ | |
70 | { | |
71 | int i; /* Looping var */ | |
72 | int status; /* Test status */ | |
73 | char *op, /* Operation to test */ | |
74 | **opargs, /* Remaining arguments */ | |
75 | *dest; /* Destination */ | |
76 | int cupslpd_argc; /* Argument count for cups-lpd */ | |
77 | char *cupslpd_argv[1000]; /* Arguments for cups-lpd */ | |
78 | int cupslpd_stdin[2], /* Standard input for cups-lpd */ | |
79 | cupslpd_stdout[2], /* Standard output for cups-lpd */ | |
80 | cupslpd_pid; /* Process ID for cups-lpd */ | |
81 | ||
82 | ||
83 | /* | |
84 | * Collect command-line arguments... | |
85 | */ | |
86 | ||
87 | op = NULL; | |
88 | opargs = NULL; | |
89 | dest = NULL; | |
90 | cupslpd_argc = 1; | |
91 | cupslpd_argv[0] = (char *)"cups-lpd"; | |
92 | ||
93 | for (i = 1; i < argc; i ++) | |
94 | if (!strncmp(argv[i], "-o", 2)) | |
95 | { | |
96 | cupslpd_argv[cupslpd_argc++] = argv[i]; | |
97 | ||
98 | if (argv[i][2]) | |
99 | { | |
100 | i ++; | |
101 | ||
102 | if (i >= argc) | |
103 | usage(); | |
104 | ||
105 | cupslpd_argv[cupslpd_argc++] = argv[i]; | |
106 | } | |
107 | } | |
108 | else if (argv[i][0] == '-') | |
109 | usage(); | |
110 | else if (!op) | |
111 | op = argv[i]; | |
112 | else if (!dest) | |
113 | dest = argv[i]; | |
114 | else | |
115 | { | |
116 | opargs = argv + i; | |
117 | break; | |
118 | } | |
119 | ||
120 | if (!op || | |
121 | (!strcmp(op, "print-job") && (!dest || !opargs)) || | |
122 | (!strcmp(op, "remove-job") && (!dest || !opargs)) || | |
123 | (strcmp(op, "print-job") && strcmp(op, "print-waiting") && | |
124 | strcmp(op, "remove-job") && strcmp(op, "status-long") && | |
125 | strcmp(op, "status-short"))) | |
126 | usage(); | |
127 | ||
128 | /* | |
129 | * Run the cups-lpd program using pipes... | |
130 | */ | |
131 | ||
132 | cupslpd_argv[cupslpd_argc] = NULL; | |
133 | ||
134 | pipe(cupslpd_stdin); | |
135 | pipe(cupslpd_stdout); | |
136 | ||
137 | if ((cupslpd_pid = fork()) < 0) | |
138 | { | |
139 | /* | |
140 | * Error! | |
141 | */ | |
142 | ||
143 | perror("testlpd: Unable to fork"); | |
144 | return (1); | |
145 | } | |
146 | else if (cupslpd_pid == 0) | |
147 | { | |
148 | /* | |
149 | * Child goes here... | |
150 | */ | |
151 | ||
152 | close(0); | |
153 | dup(cupslpd_stdin[0]); | |
154 | close(cupslpd_stdin[0]); | |
155 | close(cupslpd_stdin[1]); | |
156 | ||
157 | close(1); | |
158 | dup(cupslpd_stdout[1]); | |
159 | close(cupslpd_stdout[0]); | |
160 | close(cupslpd_stdout[1]); | |
161 | ||
162 | execv("./cups-lpd", cupslpd_argv); | |
163 | ||
164 | perror("testlpd: Unable to exec ./cups-lpd"); | |
165 | exit(errno); | |
166 | } | |
167 | else | |
168 | { | |
169 | close(cupslpd_stdin[0]); | |
170 | close(cupslpd_stdout[1]); | |
171 | } | |
172 | ||
173 | /* | |
174 | * Do the operation test... | |
175 | */ | |
176 | ||
177 | if (!strcmp(op, "print-job")) | |
178 | status = print_job(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs); | |
179 | else if (!strcmp(op, "print-waiting")) | |
180 | status = print_waiting(cupslpd_stdin[1], cupslpd_stdout[0], dest); | |
181 | else if (!strcmp(op, "remove-job")) | |
182 | status = remove_job(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs); | |
183 | else if (!strcmp(op, "status-long")) | |
184 | status = status_long(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs); | |
185 | else if (!strcmp(op, "status-short")) | |
186 | status = status_short(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs); | |
187 | ||
188 | /* | |
189 | * Kill the test program... | |
190 | */ | |
191 | ||
192 | close(cupslpd_stdin[1]); | |
193 | close(cupslpd_stdout[0]); | |
194 | kill(cupslpd_pid, SIGTERM); | |
195 | ||
196 | /* | |
197 | * Return the test status... | |
198 | */ | |
199 | ||
200 | return (status); | |
201 | } | |
202 | ||
203 | ||
204 | /* | |
205 | * 'do_command()' - Send the LPD command and wait for a response. | |
206 | */ | |
207 | ||
208 | static int /* O - Status from cups-lpd */ | |
209 | do_command(int outfd, /* I - Command file descriptor */ | |
210 | int infd, /* I - Response file descriptor */ | |
211 | const char *command) /* I - Command line to send */ | |
212 | { | |
213 | int len; /* Length of command line */ | |
214 | char status; /* Status byte */ | |
215 | ||
216 | ||
217 | printf("COMMAND: %02X %s", command[0], command + 1); | |
218 | ||
219 | len = strlen(command); | |
220 | ||
221 | if (write(outfd, command, len) < len) | |
222 | { | |
223 | puts(" Write failed!"); | |
224 | return (-1); | |
225 | } | |
226 | ||
227 | if (read(infd, &status, 1) < 1) | |
228 | puts("IN: ERROR"); | |
229 | else | |
230 | printf("IN: %d\n", status); | |
231 | ||
232 | return (status); | |
233 | } | |
234 | ||
235 | ||
236 | /* | |
237 | * 'print_job()' - Submit a file for printing. | |
238 | */ | |
239 | ||
240 | static int /* O - Status from cups-lpd */ | |
241 | print_job(int outfd, /* I - Command file descriptor */ | |
242 | int infd, /* I - Response file descriptor */ | |
243 | char *dest, /* I - Destination */ | |
244 | char **args) /* I - Arguments */ | |
245 | { | |
246 | int fd; /* Print file descriptor */ | |
247 | char command[1024], /* Command buffer */ | |
248 | control[1024], /* Control file */ | |
249 | buffer[8192]; /* Print buffer */ | |
250 | int status; /* Status of command */ | |
251 | struct stat fileinfo; /* File information */ | |
252 | char *jobname; /* Job name */ | |
253 | int sequence; /* Sequence number */ | |
254 | int bytes; /* Bytes read/written */ | |
255 | ||
256 | ||
257 | /* | |
258 | * Check the print file... | |
259 | */ | |
260 | ||
261 | if (stat(args[0], &fileinfo)) | |
262 | { | |
263 | perror(args[0]); | |
264 | return (-1); | |
265 | } | |
266 | ||
267 | if ((fd = open(args[0], O_RDONLY)) < 0) | |
268 | { | |
269 | perror(args[0]); | |
270 | return (-1); | |
271 | } | |
272 | ||
273 | /* | |
274 | * Send the "receive print job" command... | |
275 | */ | |
276 | ||
277 | snprintf(command, sizeof(command), "\002%s\n", dest); | |
278 | if ((status = do_command(outfd, infd, command)) != 0) | |
279 | { | |
280 | close(fd); | |
281 | return (status); | |
282 | } | |
283 | ||
284 | /* | |
285 | * Format a control file string that will be used to submit the job... | |
286 | */ | |
287 | ||
288 | if ((jobname = strrchr(args[0], '/')) != NULL) | |
289 | jobname ++; | |
290 | else | |
291 | jobname = args[0]; | |
292 | ||
293 | sequence = (int)getpid() % 1000; | |
294 | ||
295 | snprintf(control, sizeof(control), | |
296 | "Hlocalhost\n" | |
297 | "P%s\n" | |
298 | "J%s\n" | |
299 | "ldfA%03.3dlocalhost\n" | |
300 | "UdfA%03.3dlocalhost\n" | |
301 | "N%s\n", | |
302 | cupsUser(), jobname, sequence, sequence, jobname); | |
303 | ||
304 | /* | |
305 | * Send the control file... | |
306 | */ | |
307 | ||
308 | bytes = strlen(control); | |
309 | ||
310 | snprintf(command, sizeof(command), "\002%d cfA%03.3dlocalhost\n", | |
311 | bytes, sequence); | |
312 | ||
313 | if ((status = do_command(outfd, infd, command)) != 0) | |
314 | { | |
315 | close(fd); | |
316 | return (status); | |
317 | } | |
318 | ||
319 | bytes ++; | |
320 | ||
321 | if (write(outfd, control, bytes) < bytes) | |
322 | { | |
323 | printf("CONTROL: Unable to write %d bytes!\n", bytes); | |
324 | close(fd); | |
325 | return (-1); | |
326 | } | |
327 | ||
328 | printf("CONTROL: Wrote %d bytes.\n", bytes); | |
329 | ||
330 | if (read(infd, command, 1) < 1) | |
331 | { | |
332 | puts("IN: ERROR"); | |
333 | close(fd); | |
334 | return (-1); | |
335 | } | |
336 | else | |
337 | { | |
338 | status = command[0]; | |
339 | ||
340 | printf("IN: %d\n", status); | |
341 | } | |
342 | ||
343 | /* | |
344 | * Send the data file... | |
345 | */ | |
346 | ||
347 | snprintf(command, sizeof(command), "\003%d dfA%03.3dlocalhost\n", | |
348 | (int)fileinfo.st_size, sequence); | |
349 | ||
350 | if ((status = do_command(outfd, infd, command)) != 0) | |
351 | { | |
352 | close(fd); | |
353 | return (status); | |
354 | } | |
355 | ||
356 | while ((bytes = read(fd, buffer, sizeof(buffer))) > 0) | |
357 | { | |
358 | if (write(outfd, buffer, bytes) < bytes) | |
359 | { | |
360 | printf("DATA: Unable to write %d bytes!\n", bytes); | |
361 | close(fd); | |
362 | return (-1); | |
363 | } | |
364 | } | |
365 | ||
366 | write(outfd, "", 1); | |
367 | ||
368 | close(fd); | |
369 | ||
370 | printf("DATA: Wrote %d bytes.\n", (int)fileinfo.st_size); | |
371 | ||
372 | if (read(infd, command, 1) < 1) | |
373 | { | |
374 | puts("IN: ERROR"); | |
375 | close(fd); | |
376 | return (-1); | |
377 | } | |
378 | else | |
379 | { | |
380 | status = command[0]; | |
381 | ||
382 | printf("IN: %d\n", status); | |
383 | } | |
384 | ||
385 | return (status); | |
386 | } | |
387 | ||
388 | ||
389 | /* | |
390 | * 'print_waiting()' - Print waiting jobs. | |
391 | */ | |
392 | ||
393 | static int /* O - Status from cups-lpd */ | |
394 | print_waiting(int outfd, /* I - Command file descriptor */ | |
395 | int infd, /* I - Response file descriptor */ | |
396 | char *dest) /* I - Destination */ | |
397 | { | |
398 | char command[1024]; /* Command buffer */ | |
399 | ||
400 | ||
401 | /* | |
402 | * Send the "print waiting jobs" command... | |
403 | */ | |
404 | ||
405 | snprintf(command, sizeof(command), "\001%s\n", dest); | |
406 | ||
407 | return (do_command(outfd, infd, command)); | |
408 | } | |
409 | ||
410 | ||
411 | /* | |
412 | * 'remove_job()' - Cancel a print job. | |
413 | */ | |
414 | ||
415 | static int /* O - Status from cups-lpd */ | |
416 | remove_job(int outfd, /* I - Command file descriptor */ | |
417 | int infd, /* I - Response file descriptor */ | |
418 | char *dest, /* I - Destination */ | |
419 | char **args) /* I - Arguments */ | |
420 | { | |
421 | int i; /* Looping var */ | |
422 | char command[1024]; /* Command buffer */ | |
423 | ||
424 | /* | |
425 | * Send the "remove jobs" command... | |
426 | */ | |
427 | ||
428 | snprintf(command, sizeof(command), "\005%s", dest); | |
429 | ||
430 | for (i = 0; args[i]; i ++) | |
431 | { | |
432 | strlcat(command, " ", sizeof(command)); | |
433 | strlcat(command, args[i], sizeof(command)); | |
434 | } | |
435 | ||
436 | strlcat(command, "\n", sizeof(command)); | |
437 | ||
438 | return (do_command(outfd, infd, command)); | |
439 | } | |
440 | ||
441 | ||
442 | /* | |
443 | * 'status_long()' - Show the long printer status. | |
444 | */ | |
445 | ||
446 | static int /* O - Status from cups-lpd */ | |
447 | status_long(int outfd, /* I - Command file descriptor */ | |
448 | int infd, /* I - Response file descriptor */ | |
449 | char *dest, /* I - Destination */ | |
450 | char **args) /* I - Arguments */ | |
451 | { | |
452 | char command[1024], /* Command buffer */ | |
453 | buffer[8192]; /* Status buffer */ | |
454 | int bytes; /* Bytes read/written */ | |
455 | ||
456 | ||
457 | /* | |
458 | * Send the "send short status" command... | |
459 | */ | |
460 | ||
461 | if (args) | |
462 | snprintf(command, sizeof(command), "\004%s %s\n", dest, args[0]); | |
463 | else | |
464 | snprintf(command, sizeof(command), "\004%s\n", dest); | |
465 | ||
466 | bytes = strlen(command); | |
467 | ||
468 | if (write(outfd, command, bytes) < bytes) | |
469 | return (-1); | |
470 | ||
471 | /* | |
472 | * Read the status back... | |
473 | */ | |
474 | ||
475 | while ((bytes = read(infd, buffer, sizeof(buffer))) > 0) | |
476 | { | |
477 | fwrite(buffer, 1, bytes, stdout); | |
478 | fflush(stdout); | |
479 | } | |
480 | ||
481 | return (0); | |
482 | } | |
483 | ||
484 | ||
485 | /* | |
486 | * 'status_short()' - Show the short printer status. | |
487 | */ | |
488 | ||
489 | static int /* O - Status from cups-lpd */ | |
490 | status_short(int outfd, /* I - Command file descriptor */ | |
491 | int infd, /* I - Response file descriptor */ | |
492 | char *dest, /* I - Destination */ | |
493 | char **args) /* I - Arguments */ | |
494 | { | |
495 | char command[1024], /* Command buffer */ | |
496 | buffer[8192]; /* Status buffer */ | |
497 | int bytes; /* Bytes read/written */ | |
498 | ||
499 | ||
500 | /* | |
501 | * Send the "send short status" command... | |
502 | */ | |
503 | ||
504 | if (args) | |
505 | snprintf(command, sizeof(command), "\003%s %s\n", dest, args[0]); | |
506 | else | |
507 | snprintf(command, sizeof(command), "\003%s\n", dest); | |
508 | ||
509 | bytes = strlen(command); | |
510 | ||
511 | if (write(outfd, command, bytes) < bytes) | |
512 | return (-1); | |
513 | ||
514 | /* | |
515 | * Read the status back... | |
516 | */ | |
517 | ||
518 | while ((bytes = read(infd, buffer, sizeof(buffer))) > 0) | |
519 | { | |
520 | fwrite(buffer, 1, bytes, stdout); | |
521 | fflush(stdout); | |
522 | } | |
523 | ||
524 | return (0); | |
525 | } | |
526 | ||
527 | ||
528 | /* | |
529 | * 'usage()' - Show program usage... | |
530 | */ | |
531 | ||
532 | static void | |
533 | usage(void) | |
534 | { | |
535 | puts("Usage: testlpd [options] print-job printer user filename [... filename]"); | |
536 | puts(" testlpd [options] print-waiting [printer or user]"); | |
537 | puts(" testlpd [options] remove-job printer [user [job-id]]"); | |
538 | puts(" testlpd [options] status-long [printer or user]"); | |
539 | puts(" testlpd [options] status-short [printer or user]"); | |
540 | puts(""); | |
541 | puts("Options:"); | |
542 | puts(" -o name=value"); | |
543 | ||
544 | exit(0); | |
545 | } | |
546 | ||
547 | ||
548 | /* | |
549 | * End of "$Id: testlpd.c 5868 2006-08-23 19:39:39Z mike $". | |
550 | */ |