]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testlpd.c
2 * "$Id: testlpd.c 5868 2006-08-23 19:39:39Z mike $"
4 * cups-lpd test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2006 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
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...
37 * Include necessary headers...
40 #include <cups/cups.h>
41 #include <cups/string.h>
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);
64 * 'main()' - Simulate an LPD client.
67 int /* O - Exit status */
68 main(int argc
, /* I - Number of command-line arguments */
69 char *argv
[]) /* I - Command-line arguments */
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 */
84 * Collect command-line arguments...
91 cupslpd_argv
[0] = (char *)"cups-lpd";
93 for (i
= 1; i
< argc
; i
++)
94 if (!strncmp(argv
[i
], "-o", 2))
96 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
105 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
108 else if (argv
[i
][0] == '-')
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")))
129 * Run the cups-lpd program using pipes...
132 cupslpd_argv
[cupslpd_argc
] = NULL
;
135 pipe(cupslpd_stdout
);
137 if ((cupslpd_pid
= fork()) < 0)
143 perror("testlpd: Unable to fork");
146 else if (cupslpd_pid
== 0)
153 dup(cupslpd_stdin
[0]);
154 close(cupslpd_stdin
[0]);
155 close(cupslpd_stdin
[1]);
158 dup(cupslpd_stdout
[1]);
159 close(cupslpd_stdout
[0]);
160 close(cupslpd_stdout
[1]);
162 execv("./cups-lpd", cupslpd_argv
);
164 perror("testlpd: Unable to exec ./cups-lpd");
169 close(cupslpd_stdin
[0]);
170 close(cupslpd_stdout
[1]);
174 * Do the operation test...
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
);
189 * Kill the test program...
192 close(cupslpd_stdin
[1]);
193 close(cupslpd_stdout
[0]);
194 kill(cupslpd_pid
, SIGTERM
);
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)
230 printf("IN: %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%03.3dlocalhost\n"
300 "UdfA%03.3dlocalhost\n"
302 cupsUser(), jobname
, sequence
, sequence
, jobname
);
305 * Send the control file...
308 bytes
= strlen(control
);
310 snprintf(command
, sizeof(command
), "\002%d cfA%03.3dlocalhost\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)
340 printf("IN: %d\n", status
);
344 * Send the data file...
347 snprintf(command
, sizeof(command
), "\003%d dfA%03.3dlocalhost\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)
382 printf("IN: %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 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]");
542 puts(" -o name=value");
549 * End of "$Id: testlpd.c 5868 2006-08-23 19:39:39Z mike $".