2 * "$Id: testlpd.c 11173 2013-07-23 12:31:34Z msweet $"
4 * cups-lpd test program for CUPS.
6 * Copyright 2007-2010 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-private.h>
44 static int do_command(int outfd, int infd, const char *command);
45 static int print_job(int outfd, int infd, char *dest, char **args);
46 static int print_waiting(int outfd, int infd, char *dest);
47 static int remove_job(int outfd, int infd, char *dest, char **args);
48 static int status_long(int outfd, int infd, char *dest, char **args);
49 static int status_short(int outfd, int infd, char *dest, char **args);
50 static void usage(void) __attribute__((noreturn));
54 * 'main()' - Simulate an LPD client.
57 int /* O - Exit status */
58 main(int argc, /* I - Number of command-line arguments */
59 char *argv[]) /* I - Command-line arguments */
61 int i; /* Looping var */
62 int status; /* Test status */
63 char *op, /* Operation to test */
64 **opargs, /* Remaining arguments */
65 *dest; /* Destination */
66 int cupslpd_argc; /* Argument count for cups-lpd */
67 char *cupslpd_argv[1000]; /* Arguments for cups-lpd */
68 int cupslpd_stdin[2], /* Standard input for cups-lpd */
69 cupslpd_stdout[2], /* Standard output for cups-lpd */
70 cupslpd_pid, /* Process ID for cups-lpd */
71 cupslpd_status; /* Status of cups-lpd process */
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")))
118 printf("op=\"%s\", dest=\"%s\", opargs=%p\n", op, dest, opargs);
123 * Run the cups-lpd program using pipes...
126 cupslpd_argv[cupslpd_argc] = NULL;
129 pipe(cupslpd_stdout);
131 if ((cupslpd_pid = fork()) < 0)
137 perror("testlpd: Unable to fork");
140 else if (cupslpd_pid == 0)
146 dup2(cupslpd_stdin[0], 0);
147 close(cupslpd_stdin[0]);
148 close(cupslpd_stdin[1]);
150 dup2(cupslpd_stdout[1], 1);
151 close(cupslpd_stdout[0]);
152 close(cupslpd_stdout[1]);
154 execv("./cups-lpd", cupslpd_argv);
156 perror("testlpd: Unable to exec ./cups-lpd");
161 close(cupslpd_stdin[0]);
162 close(cupslpd_stdout[1]);
166 * Do the operation test...
169 if (!strcmp(op, "print-job"))
170 status = print_job(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs);
171 else if (!strcmp(op, "print-waiting"))
172 status = print_waiting(cupslpd_stdin[1], cupslpd_stdout[0], dest);
173 else if (!strcmp(op, "remove-job"))
174 status = remove_job(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs);
175 else if (!strcmp(op, "status-long"))
176 status = status_long(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs);
177 else if (!strcmp(op, "status-short"))
178 status = status_short(cupslpd_stdin[1], cupslpd_stdout[0], dest, opargs);
181 printf("Unknown operation \"%s\"!\n", op);
186 * Kill the test program...
189 close(cupslpd_stdin[1]);
190 close(cupslpd_stdout[0]);
192 while (wait(&cupslpd_status) != cupslpd_pid);
194 printf("cups-lpd exit status was %d...\n", cupslpd_status);
197 * Return the test status...
205 * 'do_command()' - Send the LPD command and wait for a response.
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 */
213 int len; /* Length of command line */
214 char status; /* Status byte */
217 printf("COMMAND: %02X %s", command[0], command + 1);
219 len = strlen(command);
221 if (write(outfd, command, len) < len)
223 puts(" Write failed!");
227 if (read(infd, &status, 1) < 1)
228 puts("STATUS: ERROR");
230 printf("STATUS: %d\n", status);
237 * 'print_job()' - Submit a file for printing.
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 */
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 */
258 * Check the print file...
261 if (stat(args[0], &fileinfo))
267 if ((fd = open(args[0], O_RDONLY)) < 0)
274 * Send the "receive print job" command...
277 snprintf(command, sizeof(command), "\002%s\n", dest);
278 if ((status = do_command(outfd, infd, command)) != 0)
285 * Format a control file string that will be used to submit the job...
288 if ((jobname = strrchr(args[0], '/')) != NULL)
293 sequence = (int)getpid() % 1000;
295 snprintf(control, sizeof(control),
299 "ldfA%03dlocalhost\n"
300 "UdfA%03dlocalhost\n"
302 cupsUser(), jobname, sequence, sequence, jobname);
305 * Send the control file...
308 bytes = strlen(control);
310 snprintf(command, sizeof(command), "\002%d cfA%03dlocalhost\n",
313 if ((status = do_command(outfd, infd, command)) != 0)
321 if (write(outfd, control, bytes) < bytes)
323 printf("CONTROL: Unable to write %d bytes!\n", bytes);
328 printf("CONTROL: Wrote %d bytes.\n", bytes);
330 if (read(infd, command, 1) < 1)
332 puts("STATUS: ERROR");
340 printf("STATUS: %d\n", status);
344 * Send the data file...
347 snprintf(command, sizeof(command), "\003%d dfA%03dlocalhost\n",
348 (int)fileinfo.st_size, sequence);
350 if ((status = do_command(outfd, infd, command)) != 0)
356 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
358 if (write(outfd, buffer, bytes) < bytes)
360 printf("DATA: Unable to write %d bytes!\n", bytes);
370 printf("DATA: Wrote %d bytes.\n", (int)fileinfo.st_size);
372 if (read(infd, command, 1) < 1)
374 puts("STATUS: ERROR");
382 printf("STATUS: %d\n", status);
390 * 'print_waiting()' - Print waiting jobs.
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 */
398 char command[1024]; /* Command buffer */
402 * Send the "print waiting jobs" command...
405 snprintf(command, sizeof(command), "\001%s\n", dest);
407 return (do_command(outfd, infd, command));
412 * 'remove_job()' - Cancel a print job.
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 */
421 int i; /* Looping var */
422 char command[1024]; /* Command buffer */
425 * Send the "remove jobs" command...
428 snprintf(command, sizeof(command), "\005%s", dest);
430 for (i = 0; args[i]; i ++)
432 strlcat(command, " ", sizeof(command));
433 strlcat(command, args[i], sizeof(command));
436 strlcat(command, "\n", sizeof(command));
438 return (do_command(outfd, infd, command));
443 * 'status_long()' - Show the long printer status.
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 */
452 char command[1024], /* Command buffer */
453 buffer[8192]; /* Status buffer */
454 int bytes; /* Bytes read/written */
458 * Send the "send short status" command...
462 snprintf(command, sizeof(command), "\004%s %s\n", dest, args[0]);
464 snprintf(command, sizeof(command), "\004%s\n", dest);
466 bytes = strlen(command);
468 if (write(outfd, command, bytes) < bytes)
472 * Read the status back...
475 while ((bytes = read(infd, buffer, sizeof(buffer))) > 0)
477 fwrite(buffer, 1, bytes, stdout);
486 * 'status_short()' - Show the short printer status.
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 */
495 char command[1024], /* Command buffer */
496 buffer[8192]; /* Status buffer */
497 int bytes; /* Bytes read/written */
501 * Send the "send short status" command...
505 snprintf(command, sizeof(command), "\003%s %s\n", dest, args[0]);
507 snprintf(command, sizeof(command), "\003%s\n", dest);
509 bytes = strlen(command);
511 if (write(outfd, command, bytes) < bytes)
515 * Read the status back...
518 while ((bytes = read(infd, buffer, sizeof(buffer))) > 0)
520 fwrite(buffer, 1, bytes, stdout);
529 * 'usage()' - Show program usage...
535 puts("Usage: testlpd [options] print-job printer 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]");
542 puts(" -o name=value");
549 * End of "$Id: testlpd.c 11173 2013-07-23 12:31:34Z msweet $".