]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testlpd.c
2 * "$Id: testlpd.c 6789 2007-08-13 19:52:43Z mike $"
4 * cups-lpd test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2006 by Easy Software Products, all rights reserved.
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/".
17 * main() - Simulate an LPD client.
18 * do_command() - Send the LPD command and wait for a response.
19 * print_job() - Submit a file for printing.
20 * print_waiting() - Print waiting jobs.
21 * remove_job() - Cancel a print job.
22 * status_long() - Show the long printer status.
23 * status_short() - Show the short printer status.
24 * usage() - Show program usage...
28 * Include necessary headers...
31 #include <cups/cups.h>
32 #include <cups/string.h>
46 static int do_command(int outfd
, int infd
, const char *command
);
47 static int print_job(int outfd
, int infd
, char *dest
, char **args
);
48 static int print_waiting(int outfd
, int infd
, char *dest
);
49 static int remove_job(int outfd
, int infd
, char *dest
, char **args
);
50 static int status_long(int outfd
, int infd
, char *dest
, char **args
);
51 static int status_short(int outfd
, int infd
, char *dest
, char **args
);
52 static void usage(void);
56 * 'main()' - Simulate an LPD client.
59 int /* O - Exit status */
60 main(int argc
, /* I - Number of command-line arguments */
61 char *argv
[]) /* I - Command-line arguments */
63 int i
; /* Looping var */
64 int status
; /* Test status */
65 char *op
, /* Operation to test */
66 **opargs
, /* Remaining arguments */
67 *dest
; /* Destination */
68 int cupslpd_argc
; /* Argument count for cups-lpd */
69 char *cupslpd_argv
[1000]; /* Arguments for cups-lpd */
70 int cupslpd_stdin
[2], /* Standard input for cups-lpd */
71 cupslpd_stdout
[2], /* Standard output for cups-lpd */
72 cupslpd_pid
, /* Process ID for cups-lpd */
73 cupslpd_status
; /* Status of cups-lpd process */
77 * Collect command-line arguments...
84 cupslpd_argv
[0] = (char *)"cups-lpd";
86 for (i
= 1; i
< argc
; i
++)
87 if (!strncmp(argv
[i
], "-o", 2))
89 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
98 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
101 else if (argv
[i
][0] == '-')
114 (!strcmp(op
, "print-job") && (!dest
|| !opargs
)) ||
115 (!strcmp(op
, "remove-job") && (!dest
|| !opargs
)) ||
116 (strcmp(op
, "print-job") && strcmp(op
, "print-waiting") &&
117 strcmp(op
, "remove-job") && strcmp(op
, "status-long") &&
118 strcmp(op
, "status-short")))
120 printf("op=\"%s\", dest=\"%s\", opargs=%p\n", op
, dest
, opargs
);
125 * Run the cups-lpd program using pipes...
128 cupslpd_argv
[cupslpd_argc
] = NULL
;
131 pipe(cupslpd_stdout
);
133 if ((cupslpd_pid
= fork()) < 0)
139 perror("testlpd: Unable to fork");
142 else if (cupslpd_pid
== 0)
149 dup(cupslpd_stdin
[0]);
150 close(cupslpd_stdin
[0]);
151 close(cupslpd_stdin
[1]);
154 dup(cupslpd_stdout
[1]);
155 close(cupslpd_stdout
[0]);
156 close(cupslpd_stdout
[1]);
158 execv("./cups-lpd", cupslpd_argv
);
160 perror("testlpd: Unable to exec ./cups-lpd");
165 close(cupslpd_stdin
[0]);
166 close(cupslpd_stdout
[1]);
170 * Do the operation test...
173 if (!strcmp(op
, "print-job"))
174 status
= print_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
175 else if (!strcmp(op
, "print-waiting"))
176 status
= print_waiting(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
);
177 else if (!strcmp(op
, "remove-job"))
178 status
= remove_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
179 else if (!strcmp(op
, "status-long"))
180 status
= status_long(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
181 else if (!strcmp(op
, "status-short"))
182 status
= status_short(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
185 printf("Unknown operation \"%s\"!\n", op
);
190 * Kill the test program...
193 close(cupslpd_stdin
[1]);
194 close(cupslpd_stdout
[0]);
196 while (wait(&cupslpd_status
) != cupslpd_pid
);
198 printf("cups-lpd exit status was %d...\n", cupslpd_status
);
201 * Return the test status...
209 * 'do_command()' - Send the LPD command and wait for a response.
212 static int /* O - Status from cups-lpd */
213 do_command(int outfd
, /* I - Command file descriptor */
214 int infd
, /* I - Response file descriptor */
215 const char *command
) /* I - Command line to send */
217 int len
; /* Length of command line */
218 char status
; /* Status byte */
221 printf("COMMAND: %02X %s", command
[0], command
+ 1);
223 len
= strlen(command
);
225 if (write(outfd
, command
, len
) < len
)
227 puts(" Write failed!");
231 if (read(infd
, &status
, 1) < 1)
232 puts("STATUS: ERROR");
234 printf("STATUS: %d\n", status
);
241 * 'print_job()' - Submit a file for printing.
244 static int /* O - Status from cups-lpd */
245 print_job(int outfd
, /* I - Command file descriptor */
246 int infd
, /* I - Response file descriptor */
247 char *dest
, /* I - Destination */
248 char **args
) /* I - Arguments */
250 int fd
; /* Print file descriptor */
251 char command
[1024], /* Command buffer */
252 control
[1024], /* Control file */
253 buffer
[8192]; /* Print buffer */
254 int status
; /* Status of command */
255 struct stat fileinfo
; /* File information */
256 char *jobname
; /* Job name */
257 int sequence
; /* Sequence number */
258 int bytes
; /* Bytes read/written */
262 * Check the print file...
265 if (stat(args
[0], &fileinfo
))
271 if ((fd
= open(args
[0], O_RDONLY
)) < 0)
278 * Send the "receive print job" command...
281 snprintf(command
, sizeof(command
), "\002%s\n", dest
);
282 if ((status
= do_command(outfd
, infd
, command
)) != 0)
289 * Format a control file string that will be used to submit the job...
292 if ((jobname
= strrchr(args
[0], '/')) != NULL
)
297 sequence
= (int)getpid() % 1000;
299 snprintf(control
, sizeof(control
),
303 "ldfA%03dlocalhost\n"
304 "UdfA%03dlocalhost\n"
306 cupsUser(), jobname
, sequence
, sequence
, jobname
);
309 * Send the control file...
312 bytes
= strlen(control
);
314 snprintf(command
, sizeof(command
), "\002%d cfA%03dlocalhost\n",
317 if ((status
= do_command(outfd
, infd
, command
)) != 0)
325 if (write(outfd
, control
, bytes
) < bytes
)
327 printf("CONTROL: Unable to write %d bytes!\n", bytes
);
332 printf("CONTROL: Wrote %d bytes.\n", bytes
);
334 if (read(infd
, command
, 1) < 1)
336 puts("STATUS: ERROR");
344 printf("STATUS: %d\n", status
);
348 * Send the data file...
351 snprintf(command
, sizeof(command
), "\003%d dfA%03dlocalhost\n",
352 (int)fileinfo
.st_size
, sequence
);
354 if ((status
= do_command(outfd
, infd
, command
)) != 0)
360 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
362 if (write(outfd
, buffer
, bytes
) < bytes
)
364 printf("DATA: Unable to write %d bytes!\n", bytes
);
374 printf("DATA: Wrote %d bytes.\n", (int)fileinfo
.st_size
);
376 if (read(infd
, command
, 1) < 1)
378 puts("STATUS: ERROR");
386 printf("STATUS: %d\n", status
);
394 * 'print_waiting()' - Print waiting jobs.
397 static int /* O - Status from cups-lpd */
398 print_waiting(int outfd
, /* I - Command file descriptor */
399 int infd
, /* I - Response file descriptor */
400 char *dest
) /* I - Destination */
402 char command
[1024]; /* Command buffer */
406 * Send the "print waiting jobs" command...
409 snprintf(command
, sizeof(command
), "\001%s\n", dest
);
411 return (do_command(outfd
, infd
, command
));
416 * 'remove_job()' - Cancel a print job.
419 static int /* O - Status from cups-lpd */
420 remove_job(int outfd
, /* I - Command file descriptor */
421 int infd
, /* I - Response file descriptor */
422 char *dest
, /* I - Destination */
423 char **args
) /* I - Arguments */
425 int i
; /* Looping var */
426 char command
[1024]; /* Command buffer */
429 * Send the "remove jobs" command...
432 snprintf(command
, sizeof(command
), "\005%s", dest
);
434 for (i
= 0; args
[i
]; i
++)
436 strlcat(command
, " ", sizeof(command
));
437 strlcat(command
, args
[i
], sizeof(command
));
440 strlcat(command
, "\n", sizeof(command
));
442 return (do_command(outfd
, infd
, command
));
447 * 'status_long()' - Show the long printer status.
450 static int /* O - Status from cups-lpd */
451 status_long(int outfd
, /* I - Command file descriptor */
452 int infd
, /* I - Response file descriptor */
453 char *dest
, /* I - Destination */
454 char **args
) /* I - Arguments */
456 char command
[1024], /* Command buffer */
457 buffer
[8192]; /* Status buffer */
458 int bytes
; /* Bytes read/written */
462 * Send the "send short status" command...
466 snprintf(command
, sizeof(command
), "\004%s %s\n", dest
, args
[0]);
468 snprintf(command
, sizeof(command
), "\004%s\n", dest
);
470 bytes
= strlen(command
);
472 if (write(outfd
, command
, bytes
) < bytes
)
476 * Read the status back...
479 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
481 fwrite(buffer
, 1, bytes
, stdout
);
490 * 'status_short()' - Show the short printer status.
493 static int /* O - Status from cups-lpd */
494 status_short(int outfd
, /* I - Command file descriptor */
495 int infd
, /* I - Response file descriptor */
496 char *dest
, /* I - Destination */
497 char **args
) /* I - Arguments */
499 char command
[1024], /* Command buffer */
500 buffer
[8192]; /* Status buffer */
501 int bytes
; /* Bytes read/written */
505 * Send the "send short status" command...
509 snprintf(command
, sizeof(command
), "\003%s %s\n", dest
, args
[0]);
511 snprintf(command
, sizeof(command
), "\003%s\n", dest
);
513 bytes
= strlen(command
);
515 if (write(outfd
, command
, bytes
) < bytes
)
519 * Read the status back...
522 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
524 fwrite(buffer
, 1, bytes
, stdout
);
533 * 'usage()' - Show program usage...
539 puts("Usage: testlpd [options] print-job printer filename [... filename]");
540 puts(" testlpd [options] print-waiting [printer or user]");
541 puts(" testlpd [options] remove-job printer [user [job-id]]");
542 puts(" testlpd [options] status-long [printer or user]");
543 puts(" testlpd [options] status-short [printer or user]");
546 puts(" -o name=value");
553 * End of "$Id: testlpd.c 6789 2007-08-13 19:52:43Z mike $".