]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testlpd.c
2 * "$Id: testlpd.c 6705 2007-07-20 22:25:31Z 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 */
72 cupslpd_status
; /* Status of cups-lpd process */
76 * Collect command-line arguments...
83 cupslpd_argv
[0] = (char *)"cups-lpd";
85 for (i
= 1; i
< argc
; i
++)
86 if (!strncmp(argv
[i
], "-o", 2))
88 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
97 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
100 else if (argv
[i
][0] == '-')
113 (!strcmp(op
, "print-job") && (!dest
|| !opargs
)) ||
114 (!strcmp(op
, "remove-job") && (!dest
|| !opargs
)) ||
115 (strcmp(op
, "print-job") && strcmp(op
, "print-waiting") &&
116 strcmp(op
, "remove-job") && strcmp(op
, "status-long") &&
117 strcmp(op
, "status-short")))
119 printf("op=\"%s\", dest=\"%s\", opargs=%p\n", op
, dest
, opargs
);
124 * Run the cups-lpd program using pipes...
127 cupslpd_argv
[cupslpd_argc
] = NULL
;
130 pipe(cupslpd_stdout
);
132 if ((cupslpd_pid
= fork()) < 0)
138 perror("testlpd: Unable to fork");
141 else if (cupslpd_pid
== 0)
148 dup(cupslpd_stdin
[0]);
149 close(cupslpd_stdin
[0]);
150 close(cupslpd_stdin
[1]);
153 dup(cupslpd_stdout
[1]);
154 close(cupslpd_stdout
[0]);
155 close(cupslpd_stdout
[1]);
157 execv("./cups-lpd", cupslpd_argv
);
159 perror("testlpd: Unable to exec ./cups-lpd");
164 close(cupslpd_stdin
[0]);
165 close(cupslpd_stdout
[1]);
169 * Do the operation test...
172 if (!strcmp(op
, "print-job"))
173 status
= print_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
174 else if (!strcmp(op
, "print-waiting"))
175 status
= print_waiting(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
);
176 else if (!strcmp(op
, "remove-job"))
177 status
= remove_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
178 else if (!strcmp(op
, "status-long"))
179 status
= status_long(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
180 else if (!strcmp(op
, "status-short"))
181 status
= status_short(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
184 printf("Unknown operation \"%s\"!\n", op
);
189 * Kill the test program...
192 close(cupslpd_stdin
[1]);
193 close(cupslpd_stdout
[0]);
195 while (wait(&cupslpd_status
) != cupslpd_pid
);
197 printf("cups-lpd exit status was %d...\n", cupslpd_status
);
200 * Return the test status...
208 * 'do_command()' - Send the LPD command and wait for a response.
211 static int /* O - Status from cups-lpd */
212 do_command(int outfd
, /* I - Command file descriptor */
213 int infd
, /* I - Response file descriptor */
214 const char *command
) /* I - Command line to send */
216 int len
; /* Length of command line */
217 char status
; /* Status byte */
220 printf("COMMAND: %02X %s", command
[0], command
+ 1);
222 len
= strlen(command
);
224 if (write(outfd
, command
, len
) < len
)
226 puts(" Write failed!");
230 if (read(infd
, &status
, 1) < 1)
231 puts("STATUS: ERROR");
233 printf("STATUS: %d\n", status
);
240 * 'print_job()' - Submit a file for printing.
243 static int /* O - Status from cups-lpd */
244 print_job(int outfd
, /* I - Command file descriptor */
245 int infd
, /* I - Response file descriptor */
246 char *dest
, /* I - Destination */
247 char **args
) /* I - Arguments */
249 int fd
; /* Print file descriptor */
250 char command
[1024], /* Command buffer */
251 control
[1024], /* Control file */
252 buffer
[8192]; /* Print buffer */
253 int status
; /* Status of command */
254 struct stat fileinfo
; /* File information */
255 char *jobname
; /* Job name */
256 int sequence
; /* Sequence number */
257 int bytes
; /* Bytes read/written */
261 * Check the print file...
264 if (stat(args
[0], &fileinfo
))
270 if ((fd
= open(args
[0], O_RDONLY
)) < 0)
277 * Send the "receive print job" command...
280 snprintf(command
, sizeof(command
), "\002%s\n", dest
);
281 if ((status
= do_command(outfd
, infd
, command
)) != 0)
288 * Format a control file string that will be used to submit the job...
291 if ((jobname
= strrchr(args
[0], '/')) != NULL
)
296 sequence
= (int)getpid() % 1000;
298 snprintf(control
, sizeof(control
),
302 "ldfA%03dlocalhost\n"
303 "UdfA%03dlocalhost\n"
305 cupsUser(), jobname
, sequence
, sequence
, jobname
);
308 * Send the control file...
311 bytes
= strlen(control
);
313 snprintf(command
, sizeof(command
), "\002%d cfA%03dlocalhost\n",
316 if ((status
= do_command(outfd
, infd
, command
)) != 0)
324 if (write(outfd
, control
, bytes
) < bytes
)
326 printf("CONTROL: Unable to write %d bytes!\n", bytes
);
331 printf("CONTROL: Wrote %d bytes.\n", bytes
);
333 if (read(infd
, command
, 1) < 1)
335 puts("STATUS: ERROR");
343 printf("STATUS: %d\n", status
);
347 * Send the data file...
350 snprintf(command
, sizeof(command
), "\003%d dfA%03dlocalhost\n",
351 (int)fileinfo
.st_size
, sequence
);
353 if ((status
= do_command(outfd
, infd
, command
)) != 0)
359 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
361 if (write(outfd
, buffer
, bytes
) < bytes
)
363 printf("DATA: Unable to write %d bytes!\n", bytes
);
373 printf("DATA: Wrote %d bytes.\n", (int)fileinfo
.st_size
);
375 if (read(infd
, command
, 1) < 1)
377 puts("STATUS: ERROR");
385 printf("STATUS: %d\n", status
);
393 * 'print_waiting()' - Print waiting jobs.
396 static int /* O - Status from cups-lpd */
397 print_waiting(int outfd
, /* I - Command file descriptor */
398 int infd
, /* I - Response file descriptor */
399 char *dest
) /* I - Destination */
401 char command
[1024]; /* Command buffer */
405 * Send the "print waiting jobs" command...
408 snprintf(command
, sizeof(command
), "\001%s\n", dest
);
410 return (do_command(outfd
, infd
, command
));
415 * 'remove_job()' - Cancel a print job.
418 static int /* O - Status from cups-lpd */
419 remove_job(int outfd
, /* I - Command file descriptor */
420 int infd
, /* I - Response file descriptor */
421 char *dest
, /* I - Destination */
422 char **args
) /* I - Arguments */
424 int i
; /* Looping var */
425 char command
[1024]; /* Command buffer */
428 * Send the "remove jobs" command...
431 snprintf(command
, sizeof(command
), "\005%s", dest
);
433 for (i
= 0; args
[i
]; i
++)
435 strlcat(command
, " ", sizeof(command
));
436 strlcat(command
, args
[i
], sizeof(command
));
439 strlcat(command
, "\n", sizeof(command
));
441 return (do_command(outfd
, infd
, command
));
446 * 'status_long()' - Show the long printer status.
449 static int /* O - Status from cups-lpd */
450 status_long(int outfd
, /* I - Command file descriptor */
451 int infd
, /* I - Response file descriptor */
452 char *dest
, /* I - Destination */
453 char **args
) /* I - Arguments */
455 char command
[1024], /* Command buffer */
456 buffer
[8192]; /* Status buffer */
457 int bytes
; /* Bytes read/written */
461 * Send the "send short status" command...
465 snprintf(command
, sizeof(command
), "\004%s %s\n", dest
, args
[0]);
467 snprintf(command
, sizeof(command
), "\004%s\n", dest
);
469 bytes
= strlen(command
);
471 if (write(outfd
, command
, bytes
) < bytes
)
475 * Read the status back...
478 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
480 fwrite(buffer
, 1, bytes
, stdout
);
489 * 'status_short()' - Show the short printer status.
492 static int /* O - Status from cups-lpd */
493 status_short(int outfd
, /* I - Command file descriptor */
494 int infd
, /* I - Response file descriptor */
495 char *dest
, /* I - Destination */
496 char **args
) /* I - Arguments */
498 char command
[1024], /* Command buffer */
499 buffer
[8192]; /* Status buffer */
500 int bytes
; /* Bytes read/written */
504 * Send the "send short status" command...
508 snprintf(command
, sizeof(command
), "\003%s %s\n", dest
, args
[0]);
510 snprintf(command
, sizeof(command
), "\003%s\n", dest
);
512 bytes
= strlen(command
);
514 if (write(outfd
, command
, bytes
) < bytes
)
518 * Read the status back...
521 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
523 fwrite(buffer
, 1, bytes
, stdout
);
532 * 'usage()' - Show program usage...
538 puts("Usage: testlpd [options] print-job printer filename [... filename]");
539 puts(" testlpd [options] print-waiting [printer or user]");
540 puts(" testlpd [options] remove-job printer [user [job-id]]");
541 puts(" testlpd [options] status-long [printer or user]");
542 puts(" testlpd [options] status-short [printer or user]");
545 puts(" -o name=value");
552 * End of "$Id: testlpd.c 6705 2007-07-20 22:25:31Z mike $".