]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testlpd.c
2 * "$Id: testlpd.c 6649 2007-07-11 21:46:42Z 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>
45 static int do_command(int outfd
, int infd
, const char *command
);
46 static int print_job(int outfd
, int infd
, char *dest
, char **args
);
47 static int print_waiting(int outfd
, int infd
, char *dest
);
48 static int remove_job(int outfd
, int infd
, char *dest
, char **args
);
49 static int status_long(int outfd
, int infd
, char *dest
, char **args
);
50 static int status_short(int outfd
, int infd
, char *dest
, char **args
);
51 static void usage(void);
55 * 'main()' - Simulate an LPD client.
58 int /* O - Exit status */
59 main(int argc
, /* I - Number of command-line arguments */
60 char *argv
[]) /* I - Command-line arguments */
62 int i
; /* Looping var */
63 int status
; /* Test status */
64 char *op
, /* Operation to test */
65 **opargs
, /* Remaining arguments */
66 *dest
; /* Destination */
67 int cupslpd_argc
; /* Argument count for cups-lpd */
68 char *cupslpd_argv
[1000]; /* Arguments for cups-lpd */
69 int cupslpd_stdin
[2], /* Standard input for cups-lpd */
70 cupslpd_stdout
[2], /* Standard output for cups-lpd */
71 cupslpd_pid
; /* Process ID for cups-lpd */
75 * Collect command-line arguments...
82 cupslpd_argv
[0] = (char *)"cups-lpd";
84 for (i
= 1; i
< argc
; i
++)
85 if (!strncmp(argv
[i
], "-o", 2))
87 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
96 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
99 else if (argv
[i
][0] == '-')
112 (!strcmp(op
, "print-job") && (!dest
|| !opargs
)) ||
113 (!strcmp(op
, "remove-job") && (!dest
|| !opargs
)) ||
114 (strcmp(op
, "print-job") && strcmp(op
, "print-waiting") &&
115 strcmp(op
, "remove-job") && strcmp(op
, "status-long") &&
116 strcmp(op
, "status-short")))
120 * Run the cups-lpd program using pipes...
123 cupslpd_argv
[cupslpd_argc
] = NULL
;
126 pipe(cupslpd_stdout
);
128 if ((cupslpd_pid
= fork()) < 0)
134 perror("testlpd: Unable to fork");
137 else if (cupslpd_pid
== 0)
144 dup(cupslpd_stdin
[0]);
145 close(cupslpd_stdin
[0]);
146 close(cupslpd_stdin
[1]);
149 dup(cupslpd_stdout
[1]);
150 close(cupslpd_stdout
[0]);
151 close(cupslpd_stdout
[1]);
153 execv("./cups-lpd", cupslpd_argv
);
155 perror("testlpd: Unable to exec ./cups-lpd");
160 close(cupslpd_stdin
[0]);
161 close(cupslpd_stdout
[1]);
165 * Do the operation test...
168 if (!strcmp(op
, "print-job"))
169 status
= print_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
170 else if (!strcmp(op
, "print-waiting"))
171 status
= print_waiting(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
);
172 else if (!strcmp(op
, "remove-job"))
173 status
= remove_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
174 else if (!strcmp(op
, "status-long"))
175 status
= status_long(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
176 else if (!strcmp(op
, "status-short"))
177 status
= status_short(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
180 printf("Unknown operation \"%s\"!\n", op
);
185 * Kill the test program...
188 close(cupslpd_stdin
[1]);
189 close(cupslpd_stdout
[0]);
190 kill(cupslpd_pid
, SIGTERM
);
193 * Return the test status...
201 * 'do_command()' - Send the LPD command and wait for a response.
204 static int /* O - Status from cups-lpd */
205 do_command(int outfd
, /* I - Command file descriptor */
206 int infd
, /* I - Response file descriptor */
207 const char *command
) /* I - Command line to send */
209 int len
; /* Length of command line */
210 char status
; /* Status byte */
213 printf("COMMAND: %02X %s", command
[0], command
+ 1);
215 len
= strlen(command
);
217 if (write(outfd
, command
, len
) < len
)
219 puts(" Write failed!");
223 if (read(infd
, &status
, 1) < 1)
226 printf("IN: %d\n", status
);
233 * 'print_job()' - Submit a file for printing.
236 static int /* O - Status from cups-lpd */
237 print_job(int outfd
, /* I - Command file descriptor */
238 int infd
, /* I - Response file descriptor */
239 char *dest
, /* I - Destination */
240 char **args
) /* I - Arguments */
242 int fd
; /* Print file descriptor */
243 char command
[1024], /* Command buffer */
244 control
[1024], /* Control file */
245 buffer
[8192]; /* Print buffer */
246 int status
; /* Status of command */
247 struct stat fileinfo
; /* File information */
248 char *jobname
; /* Job name */
249 int sequence
; /* Sequence number */
250 int bytes
; /* Bytes read/written */
254 * Check the print file...
257 if (stat(args
[0], &fileinfo
))
263 if ((fd
= open(args
[0], O_RDONLY
)) < 0)
270 * Send the "receive print job" command...
273 snprintf(command
, sizeof(command
), "\002%s\n", dest
);
274 if ((status
= do_command(outfd
, infd
, command
)) != 0)
281 * Format a control file string that will be used to submit the job...
284 if ((jobname
= strrchr(args
[0], '/')) != NULL
)
289 sequence
= (int)getpid() % 1000;
291 snprintf(control
, sizeof(control
),
295 "ldfA%03dlocalhost\n"
296 "UdfA%03dlocalhost\n"
298 cupsUser(), jobname
, sequence
, sequence
, jobname
);
301 * Send the control file...
304 bytes
= strlen(control
);
306 snprintf(command
, sizeof(command
), "\002%d cfA%03dlocalhost\n",
309 if ((status
= do_command(outfd
, infd
, command
)) != 0)
317 if (write(outfd
, control
, bytes
) < bytes
)
319 printf("CONTROL: Unable to write %d bytes!\n", bytes
);
324 printf("CONTROL: Wrote %d bytes.\n", bytes
);
326 if (read(infd
, command
, 1) < 1)
336 printf("IN: %d\n", status
);
340 * Send the data file...
343 snprintf(command
, sizeof(command
), "\003%d dfA%03dlocalhost\n",
344 (int)fileinfo
.st_size
, sequence
);
346 if ((status
= do_command(outfd
, infd
, command
)) != 0)
352 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
354 if (write(outfd
, buffer
, bytes
) < bytes
)
356 printf("DATA: Unable to write %d bytes!\n", bytes
);
366 printf("DATA: Wrote %d bytes.\n", (int)fileinfo
.st_size
);
368 if (read(infd
, command
, 1) < 1)
378 printf("IN: %d\n", status
);
386 * 'print_waiting()' - Print waiting jobs.
389 static int /* O - Status from cups-lpd */
390 print_waiting(int outfd
, /* I - Command file descriptor */
391 int infd
, /* I - Response file descriptor */
392 char *dest
) /* I - Destination */
394 char command
[1024]; /* Command buffer */
398 * Send the "print waiting jobs" command...
401 snprintf(command
, sizeof(command
), "\001%s\n", dest
);
403 return (do_command(outfd
, infd
, command
));
408 * 'remove_job()' - Cancel a print job.
411 static int /* O - Status from cups-lpd */
412 remove_job(int outfd
, /* I - Command file descriptor */
413 int infd
, /* I - Response file descriptor */
414 char *dest
, /* I - Destination */
415 char **args
) /* I - Arguments */
417 int i
; /* Looping var */
418 char command
[1024]; /* Command buffer */
421 * Send the "remove jobs" command...
424 snprintf(command
, sizeof(command
), "\005%s", dest
);
426 for (i
= 0; args
[i
]; i
++)
428 strlcat(command
, " ", sizeof(command
));
429 strlcat(command
, args
[i
], sizeof(command
));
432 strlcat(command
, "\n", sizeof(command
));
434 return (do_command(outfd
, infd
, command
));
439 * 'status_long()' - Show the long printer status.
442 static int /* O - Status from cups-lpd */
443 status_long(int outfd
, /* I - Command file descriptor */
444 int infd
, /* I - Response file descriptor */
445 char *dest
, /* I - Destination */
446 char **args
) /* I - Arguments */
448 char command
[1024], /* Command buffer */
449 buffer
[8192]; /* Status buffer */
450 int bytes
; /* Bytes read/written */
454 * Send the "send short status" command...
458 snprintf(command
, sizeof(command
), "\004%s %s\n", dest
, args
[0]);
460 snprintf(command
, sizeof(command
), "\004%s\n", dest
);
462 bytes
= strlen(command
);
464 if (write(outfd
, command
, bytes
) < bytes
)
468 * Read the status back...
471 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
473 fwrite(buffer
, 1, bytes
, stdout
);
482 * 'status_short()' - Show the short printer status.
485 static int /* O - Status from cups-lpd */
486 status_short(int outfd
, /* I - Command file descriptor */
487 int infd
, /* I - Response file descriptor */
488 char *dest
, /* I - Destination */
489 char **args
) /* I - Arguments */
491 char command
[1024], /* Command buffer */
492 buffer
[8192]; /* Status buffer */
493 int bytes
; /* Bytes read/written */
497 * Send the "send short status" command...
501 snprintf(command
, sizeof(command
), "\003%s %s\n", dest
, args
[0]);
503 snprintf(command
, sizeof(command
), "\003%s\n", dest
);
505 bytes
= strlen(command
);
507 if (write(outfd
, command
, bytes
) < bytes
)
511 * Read the status back...
514 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
516 fwrite(buffer
, 1, bytes
, stdout
);
525 * 'usage()' - Show program usage...
531 puts("Usage: testlpd [options] print-job printer user filename [... filename]");
532 puts(" testlpd [options] print-waiting [printer or user]");
533 puts(" testlpd [options] remove-job printer [user [job-id]]");
534 puts(" testlpd [options] status-long [printer or user]");
535 puts(" testlpd [options] status-short [printer or user]");
538 puts(" -o name=value");
545 * End of "$Id: testlpd.c 6649 2007-07-11 21:46:42Z mike $".