]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/testlpd.c
2 * cups-lpd test program for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 2006 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups.h>
15 #include <cups/string-private.h>
27 static int do_command(int outfd
, int infd
, const char *command
);
28 static int print_job(int outfd
, int infd
, char *dest
, char **args
) _CUPS_NONNULL(4);
29 static int print_waiting(int outfd
, int infd
, char *dest
);
30 static int remove_job(int outfd
, int infd
, char *dest
, char **args
) _CUPS_NONNULL(4);
31 static int status_long(int outfd
, int infd
, char *dest
, char **args
) _CUPS_NONNULL(4);
32 static int status_short(int outfd
, int infd
, char *dest
, char **args
) _CUPS_NONNULL(4);
33 static void usage(void) _CUPS_NORETURN
;
37 * 'main()' - Simulate an LPD client.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line arguments */
42 char *argv
[]) /* I - Command-line arguments */
44 int i
; /* Looping var */
45 int status
; /* Test status */
46 char *op
, /* Operation to test */
47 **opargs
, /* Remaining arguments */
48 *dest
; /* Destination */
49 int cupslpd_argc
; /* Argument count for cups-lpd */
50 char *cupslpd_argv
[1000]; /* Arguments for cups-lpd */
51 int cupslpd_stdin
[2], /* Standard input for cups-lpd */
52 cupslpd_stdout
[2], /* Standard output for cups-lpd */
53 cupslpd_pid
, /* Process ID for cups-lpd */
54 cupslpd_status
; /* Status of cups-lpd process */
58 * Collect command-line arguments...
65 cupslpd_argv
[0] = (char *)"cups-lpd";
67 for (i
= 1; i
< argc
; i
++)
68 if (!strncmp(argv
[i
], "-o", 2))
70 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
79 cupslpd_argv
[cupslpd_argc
++] = argv
[i
];
82 else if (argv
[i
][0] == '-')
95 (!strcmp(op
, "print-job") && (!dest
|| !opargs
)) ||
96 (!strcmp(op
, "remove-job") && (!dest
|| !opargs
)) ||
97 (strcmp(op
, "print-job") && strcmp(op
, "print-waiting") &&
98 strcmp(op
, "remove-job") && strcmp(op
, "status-long") &&
99 strcmp(op
, "status-short")))
101 printf("op=\"%s\", dest=\"%s\", opargs=%p\n", op
, dest
, opargs
);
106 * Run the cups-lpd program using pipes...
109 cupslpd_argv
[cupslpd_argc
] = NULL
;
112 pipe(cupslpd_stdout
);
114 if ((cupslpd_pid
= fork()) < 0)
120 perror("testlpd: Unable to fork");
123 else if (cupslpd_pid
== 0)
129 dup2(cupslpd_stdin
[0], 0);
130 close(cupslpd_stdin
[0]);
131 close(cupslpd_stdin
[1]);
133 dup2(cupslpd_stdout
[1], 1);
134 close(cupslpd_stdout
[0]);
135 close(cupslpd_stdout
[1]);
137 execv("./cups-lpd", cupslpd_argv
);
139 perror("testlpd: Unable to exec ./cups-lpd");
144 close(cupslpd_stdin
[0]);
145 close(cupslpd_stdout
[1]);
149 * Do the operation test...
152 if (!strcmp(op
, "print-job"))
153 status
= print_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
154 else if (!strcmp(op
, "print-waiting"))
155 status
= print_waiting(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
);
156 else if (!strcmp(op
, "remove-job"))
157 status
= remove_job(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
158 else if (!strcmp(op
, "status-long"))
159 status
= status_long(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
160 else if (!strcmp(op
, "status-short"))
161 status
= status_short(cupslpd_stdin
[1], cupslpd_stdout
[0], dest
, opargs
);
164 printf("Unknown operation \"%s\"!\n", op
);
169 * Kill the test program...
172 close(cupslpd_stdin
[1]);
173 close(cupslpd_stdout
[0]);
175 while (wait(&cupslpd_status
) != cupslpd_pid
);
177 printf("cups-lpd exit status was %d...\n", cupslpd_status
);
180 * Return the test status...
188 * 'do_command()' - Send the LPD command and wait for a response.
191 static int /* O - Status from cups-lpd */
192 do_command(int outfd
, /* I - Command file descriptor */
193 int infd
, /* I - Response file descriptor */
194 const char *command
) /* I - Command line to send */
196 size_t len
; /* Length of command line */
197 char status
; /* Status byte */
200 printf("COMMAND: %02X %s", command
[0], command
+ 1);
202 len
= strlen(command
);
204 if ((size_t)write(outfd
, command
, len
) < len
)
206 puts(" Write failed!");
210 if (read(infd
, &status
, 1) < 1)
211 puts("STATUS: ERROR");
213 printf("STATUS: %d\n", status
);
220 * 'print_job()' - Submit a file for printing.
223 static int /* O - Status from cups-lpd */
224 print_job(int outfd
, /* I - Command file descriptor */
225 int infd
, /* I - Response file descriptor */
226 char *dest
, /* I - Destination */
227 char **args
) /* I - Arguments */
229 int fd
; /* Print file descriptor */
230 char command
[1024], /* Command buffer */
231 control
[1024], /* Control file */
232 buffer
[8192]; /* Print buffer */
233 int status
; /* Status of command */
234 struct stat fileinfo
; /* File information */
235 char *jobname
; /* Job name */
236 int sequence
; /* Sequence number */
237 ssize_t bytes
; /* Bytes read/written */
241 * Check the print file...
244 if (stat(args
[0], &fileinfo
))
250 if ((fd
= open(args
[0], O_RDONLY
)) < 0)
257 * Send the "receive print job" command...
260 snprintf(command
, sizeof(command
), "\002%s\n", dest
);
261 if ((status
= do_command(outfd
, infd
, command
)) != 0)
268 * Format a control file string that will be used to submit the job...
271 if ((jobname
= strrchr(args
[0], '/')) != NULL
)
276 sequence
= (int)getpid() % 1000;
278 snprintf(control
, sizeof(control
),
282 "ldfA%03dlocalhost\n"
283 "UdfA%03dlocalhost\n"
285 cupsUser(), jobname
, sequence
, sequence
, jobname
);
288 * Send the control file...
291 bytes
= (ssize_t
)strlen(control
);
293 snprintf(command
, sizeof(command
), "\002%d cfA%03dlocalhost\n",
294 (int)bytes
, sequence
);
296 if ((status
= do_command(outfd
, infd
, command
)) != 0)
304 if (write(outfd
, control
, (size_t)bytes
) < bytes
)
306 printf("CONTROL: Unable to write %d bytes!\n", (int)bytes
);
311 printf("CONTROL: Wrote %d bytes.\n", (int)bytes
);
313 if (read(infd
, command
, 1) < 1)
315 puts("STATUS: ERROR");
323 printf("STATUS: %d\n", status
);
327 * Send the data file...
330 snprintf(command
, sizeof(command
), "\003%d dfA%03dlocalhost\n",
331 (int)fileinfo
.st_size
, sequence
);
333 if ((status
= do_command(outfd
, infd
, command
)) != 0)
339 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
341 if (write(outfd
, buffer
, (size_t)bytes
) < bytes
)
343 printf("DATA: Unable to write %d bytes!\n", (int)bytes
);
353 printf("DATA: Wrote %d bytes.\n", (int)fileinfo
.st_size
);
355 if (read(infd
, command
, 1) < 1)
357 puts("STATUS: ERROR");
365 printf("STATUS: %d\n", status
);
373 * 'print_waiting()' - Print waiting jobs.
376 static int /* O - Status from cups-lpd */
377 print_waiting(int outfd
, /* I - Command file descriptor */
378 int infd
, /* I - Response file descriptor */
379 char *dest
) /* I - Destination */
381 char command
[1024]; /* Command buffer */
385 * Send the "print waiting jobs" command...
388 snprintf(command
, sizeof(command
), "\001%s\n", dest
);
390 return (do_command(outfd
, infd
, command
));
395 * 'remove_job()' - Cancel a print job.
398 static int /* O - Status from cups-lpd */
399 remove_job(int outfd
, /* I - Command file descriptor */
400 int infd
, /* I - Response file descriptor */
401 char *dest
, /* I - Destination */
402 char **args
) /* I - Arguments */
404 int i
; /* Looping var */
405 char command
[1024]; /* Command buffer */
408 * Send the "remove jobs" command...
411 snprintf(command
, sizeof(command
), "\005%s", dest
);
413 for (i
= 0; args
[i
]; i
++)
415 strlcat(command
, " ", sizeof(command
));
416 strlcat(command
, args
[i
], sizeof(command
));
419 strlcat(command
, "\n", sizeof(command
));
421 return (do_command(outfd
, infd
, command
));
426 * 'status_long()' - Show the long printer status.
429 static int /* O - Status from cups-lpd */
430 status_long(int outfd
, /* I - Command file descriptor */
431 int infd
, /* I - Response file descriptor */
432 char *dest
, /* I - Destination */
433 char **args
) /* I - Arguments */
435 char command
[1024], /* Command buffer */
436 buffer
[8192]; /* Status buffer */
437 ssize_t bytes
; /* Bytes read/written */
441 * Send the "send short status" command...
445 snprintf(command
, sizeof(command
), "\004%s %s\n", dest
, args
[0]);
447 snprintf(command
, sizeof(command
), "\004%s\n", dest
);
449 bytes
= (ssize_t
)strlen(command
);
451 if (write(outfd
, command
, (size_t)bytes
) < bytes
)
455 * Read the status back...
458 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
460 fwrite(buffer
, 1, (size_t)bytes
, stdout
);
469 * 'status_short()' - Show the short printer status.
472 static int /* O - Status from cups-lpd */
473 status_short(int outfd
, /* I - Command file descriptor */
474 int infd
, /* I - Response file descriptor */
475 char *dest
, /* I - Destination */
476 char **args
) /* I - Arguments */
478 char command
[1024], /* Command buffer */
479 buffer
[8192]; /* Status buffer */
480 ssize_t bytes
; /* Bytes read/written */
484 * Send the "send short status" command...
488 snprintf(command
, sizeof(command
), "\003%s %s\n", dest
, args
[0]);
490 snprintf(command
, sizeof(command
), "\003%s\n", dest
);
492 bytes
= (ssize_t
)strlen(command
);
494 if (write(outfd
, command
, (size_t)bytes
) < bytes
)
498 * Read the status back...
501 while ((bytes
= read(infd
, buffer
, sizeof(buffer
))) > 0)
503 fwrite(buffer
, 1, (size_t)bytes
, stdout
);
512 * 'usage()' - Show program usage...
518 puts("Usage: testlpd [options] print-job printer filename [... filename]");
519 puts(" testlpd [options] print-waiting [printer or user]");
520 puts(" testlpd [options] remove-job printer [user [job-id]]");
521 puts(" testlpd [options] status-long [printer or user]");
522 puts(" testlpd [options] status-short [printer or user]");
525 puts(" -o name=value");