2 Unix SMB/Netbios implementation.
4 Virtual lp system for printer testing
6 Copyright (C) Tim Potter 2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define PRINT_TDB "/tmp/vlp.tdb"
26 #define PRINT_FIRSTJOB "100"
28 static TDB_CONTEXT *tdb;
42 static void usage(void)
44 printf("Usage: print-test lpq|lprm|print|queuepause|queueresume|"
45 "lppause|lpresume [args]\n");
48 /* Return an array of vlp jobs that is the printer queue */
50 static void get_job_list(char *printer, struct vlp_job **job_list,
56 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
57 data = tdb_fetch_by_string(tdb, keystr);
59 *job_list = (struct vlp_job *)data.dptr;
60 *num_jobs = data.dsize / sizeof(struct vlp_job);
63 /* Store an array of vl jobs for the queue */
65 static void set_job_list(char *printer, struct vlp_job *job_list,
70 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
72 tdb_store_by_string(tdb, keystr, job_list,
73 num_jobs * sizeof(struct vlp_job));
76 /* Return the next job number for a printer */
78 static int next_jobnum(char *printer)
83 slprintf(keystr, sizeof(keystr) - 1, "JOBNUM/%s", printer);
85 tdb_lock_bystring(tdb, keystr);
87 jobnum = tdb_fetch_int(tdb, keystr);
89 /* Create next job index if none exists */
92 jobnum = atoi(PRINT_FIRSTJOB);
96 tdb_store_int(tdb, keystr, jobnum);
98 tdb_unlock_bystring(tdb, keystr);
103 static void set_printer_status(char *printer, int status)
108 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
109 result = tdb_store_int(tdb, keystr, status);
112 static int get_printer_status(char *printer)
117 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
120 data.dsize = strlen(keystr) + 1;
122 if (!tdb_exists(tdb, data)) {
123 set_printer_status(printer, LPSTAT_OK);
127 return tdb_fetch_int(tdb, keystr);
130 /* Display printer queue */
132 static int lpq_command(int argc, char **argv)
135 struct vlp_job *job_list = NULL;
136 int i, num_jobs, job_count = 0;
139 printf("Usage: lpq <printername>\n");
145 /* Display printer status */
147 switch (get_printer_status(printer)) {
152 printf("disabled\n");
160 /* Print queued documents */
162 get_job_list(printer, &job_list, &num_jobs);
164 for (i = 0; i < num_jobs; i++) {
165 if (job_list[i].deleted) continue;
166 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list[i].jobid,
168 (i == 0 && job_list[i].status == LPQ_QUEUED) ?
169 LPQ_SPOOLING : job_list[i].status,
170 job_list[i].submit_time, job_list[i].owner,
171 job_list[i].jobname);
182 static int lprm_command(int argc, char **argv)
185 int jobid, num_jobs, i;
186 struct vlp_job *job_list;
189 printf("Usage: lprm <printername> <jobid>\n");
194 jobid = atoi(argv[2]);
196 get_job_list(printer, &job_list, &num_jobs);
198 for (i = 0; i < num_jobs; i++) {
199 if (job_list[i].jobid == jobid) {
200 job_list[i].deleted = 1;
201 set_job_list(printer, job_list, num_jobs);
209 /* print command = print-test %p %s */
211 static int print_command(int argc, char **argv)
221 printf("Usage: print <printername> <jobname>\n");
229 /* Create a job record */
231 for (i = 2; i < argc; i++) {
232 fstrcat(job.jobname, argv[i]);
234 fstrcat(job.jobname, " ");
238 if (!(pw = getpwuid(getuid()))) {
242 fstrcpy(job.owner, pw->pw_name);
244 job.jobid = next_jobnum(printer);
246 job.submit_time = time(NULL);
248 /* Store job entry in queue */
250 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
252 value = tdb_fetch_by_string(tdb, keystr);
256 /* Add job to end of queue */
258 value.dptr = realloc(value.dptr, value.dsize +
259 sizeof(struct vlp_job));
260 if (!value.dptr) return 1;
262 memcpy(value.dptr + value.dsize, &job, sizeof(struct vlp_job));
264 tdb_store_by_string(tdb, keystr, value.dptr, value.dsize +
265 sizeof(struct vlp_job));
271 /* Create new queue */
273 tdb_store_by_string(tdb, keystr, &job, sizeof(struct vlp_job));
279 /* Pause the queue */
281 static int queuepause_command(int argc, char **argv)
286 printf("Usage: queuepause <printername>\n");
291 set_printer_status(printer, LPSTAT_STOPPED);
296 /* Resume the queue */
298 static int queueresume_command(int argc, char **argv)
303 printf("Usage: queueresume <printername>\n");
308 set_printer_status(printer, LPSTAT_OK);
315 static int lppause_command(int argc, char **argv)
317 struct vlp_job *job_list;
319 int jobid, num_jobs, i;
322 printf("Usage: lppause <printername> <jobid>\n");
327 jobid = atoi(argv[2]);
329 get_job_list(printer, &job_list, &num_jobs);
331 for (i = 0; i < num_jobs; i++) {
332 if (job_list[i].jobid == jobid) {
333 job_list[i].status = LPQ_PAUSED;
334 set_job_list(printer, job_list, num_jobs);
344 static int lpresume_command(int argc, char **argv)
346 struct vlp_job *job_list;
348 int jobid, num_jobs, i;
351 printf("Usage: lpresume <printername> <jobid>\n");
356 jobid = atoi(argv[2]);
358 get_job_list(printer, &job_list, &num_jobs);
360 for (i = 0; i < num_jobs; i++) {
361 if (job_list[i].jobid == jobid) {
362 job_list[i].status = LPQ_QUEUED;
363 set_job_list(printer, job_list, num_jobs);
371 int main(int argc, char **argv)
373 /* Parameter check */
382 if (!(tdb = tdb_open(PRINT_TDB, 0, 0, O_RDWR | O_CREAT,
384 printf("%s: unable to open %s\n", argv[0], PRINT_TDB);
388 /* Ensure we are modes 666 */
390 chmod(PRINT_TDB, 0666);
394 if (strcmp(argv[1], "lpq") == 0) {
395 return lpq_command(argc - 1, &argv[1]);
398 if (strcmp(argv[1], "lprm") == 0) {
399 return lprm_command(argc - 1, &argv[1]);
402 if (strcmp(argv[1], "print") == 0) {
403 return print_command(argc - 1, &argv[1]);
406 if (strcmp(argv[1], "queuepause") == 0) {
407 return queuepause_command(argc - 1, &argv[1]);
410 if (strcmp(argv[1], "queueresume") == 0) {
411 return queueresume_command(argc - 1, &argv[1]);
414 if (strcmp(argv[1], "lppause") == 0) {
415 return lppause_command(argc - 1, &argv[1]);
418 if (strcmp(argv[1], "lpresume") == 0) {
419 return lpresume_command(argc - 1, &argv[1]);
422 /* Unknown command */
424 printf("%s: invalid command %s\n", argv[0], argv[1]);