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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "system/passwd.h"
30 #define PRINT_FIRSTJOB "100"
32 static TDB_CONTEXT
*tdb
;
46 static void usage(void)
48 printf("Usage: vlp tdbfile=/tmp/vlp.tdb lpq|lprm|print|queuepause|queueresume|"
49 "lppause|lpresume [args]\n");
52 /* Return an array of vlp jobs that is the printer queue */
54 static void get_job_list(char *printer
, struct vlp_job
**job_list
,
60 slprintf(keystr
, sizeof(keystr
) - 1, "LPQ/%s", printer
);
61 data
= tdb_fetch_bystring(tdb
, keystr
);
63 *job_list
= (struct vlp_job
*)data
.dptr
;
64 *num_jobs
= data
.dsize
/ sizeof(struct vlp_job
);
67 /* Store an array of vl jobs for the queue */
69 static void set_job_list(char *printer
, struct vlp_job
*job_list
,
75 slprintf(keystr
, sizeof(keystr
) - 1, "LPQ/%s", printer
);
77 data
.dptr
= (unsigned char *)job_list
;
78 data
.dsize
= num_jobs
* sizeof(struct vlp_job
);
79 tdb_store_bystring(tdb
, keystr
, data
, TDB_REPLACE
);
82 /* Return the next job number for a printer */
84 static int next_jobnum(char *printer
)
89 slprintf(keystr
, sizeof(keystr
) - 1, "JOBNUM/%s", printer
);
91 tdb_lock_bystring(tdb
, keystr
);
93 jobnum
= tdb_fetch_int32(tdb
, keystr
);
95 /* Create next job index if none exists */
98 jobnum
= atoi(PRINT_FIRSTJOB
);
102 tdb_store_int32(tdb
, keystr
, jobnum
);
104 tdb_unlock_bystring(tdb
, keystr
);
109 static void set_printer_status(char *printer
, int status
)
114 slprintf(keystr
, sizeof(keystr
) - 1, "STATUS/%s", printer
);
115 result
= tdb_store_int32(tdb
, keystr
, status
);
118 static int get_printer_status(char *printer
)
123 slprintf(keystr
, sizeof(keystr
) - 1, "STATUS/%s", printer
);
125 data
.dptr
= (unsigned char *)keystr
;
126 data
.dsize
= strlen(keystr
) + 1;
128 if (!tdb_exists(tdb
, data
)) {
129 set_printer_status(printer
, LPSTAT_OK
);
133 return tdb_fetch_int32(tdb
, keystr
);
136 /* Display printer queue */
138 static int lpq_command(int argc
, char **argv
)
141 struct vlp_job
*job_list
= NULL
;
142 int i
, num_jobs
, job_count
= 0;
145 printf("Usage: lpq <printername>\n");
151 /* Display printer status */
153 switch (get_printer_status(printer
)) {
158 printf("disabled\n");
166 /* Print queued documents */
168 get_job_list(printer
, &job_list
, &num_jobs
);
170 for (i
= 0; i
< num_jobs
; i
++) {
171 if (job_list
[i
].deleted
) continue;
172 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list
[i
].jobid
,
174 (i
== 0 && job_list
[i
].status
== LPQ_QUEUED
) ?
175 LPQ_SPOOLING
: job_list
[i
].status
,
176 (long int)job_list
[i
].submit_time
, job_list
[i
].owner
,
177 job_list
[i
].jobname
);
188 static int lprm_command(int argc
, char **argv
)
191 int jobid
, num_jobs
, i
;
192 struct vlp_job
*job_list
;
195 printf("Usage: lprm <printername> <jobid>\n");
200 jobid
= atoi(argv
[2]);
202 get_job_list(printer
, &job_list
, &num_jobs
);
204 for (i
= 0; i
< num_jobs
; i
++) {
205 if (job_list
[i
].jobid
== jobid
) {
206 job_list
[i
].deleted
= 1;
207 set_job_list(printer
, job_list
, num_jobs
);
215 /* print command = print-test %p %s */
217 static int print_command(int argc
, char **argv
)
222 TDB_DATA value
, queue
;
226 printf("Usage: print <printername> <jobname>\n");
234 /* Create a job record */
236 slprintf(job
.jobname
, sizeof(job
.jobname
) - 1, "%s", argv
[2]);
238 if (!(pw
= getpwuid(getuid()))) {
242 slprintf(job
.owner
, sizeof(job
.owner
) - 1, "%s", 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_bystring(tdb
, keystr
);
256 /* Add job to end of queue */
258 queue
.dptr
= (unsigned char *)malloc(value
.dsize
+ sizeof(struct vlp_job
));
259 if (!queue
.dptr
) return 1;
261 memcpy(queue
.dptr
, value
.dptr
, value
.dsize
);
262 memcpy(queue
.dptr
+ value
.dsize
, &job
, sizeof(struct vlp_job
));
264 queue
.dsize
= value
.dsize
+ sizeof(struct vlp_job
);
266 tdb_store_bystring(tdb
, keystr
, queue
, TDB_REPLACE
);
272 /* Create new queue */
273 queue
.dptr
= (unsigned char *)&job
;
274 queue
.dsize
= sizeof(struct vlp_job
);
276 tdb_store_bystring(tdb
, keystr
, queue
, TDB_REPLACE
);
282 /* Pause the queue */
284 static int queuepause_command(int argc
, char **argv
)
289 printf("Usage: queuepause <printername>\n");
294 set_printer_status(printer
, LPSTAT_STOPPED
);
299 /* Resume the queue */
301 static int queueresume_command(int argc
, char **argv
)
306 printf("Usage: queueresume <printername>\n");
311 set_printer_status(printer
, LPSTAT_OK
);
318 static int lppause_command(int argc
, char **argv
)
320 struct vlp_job
*job_list
;
322 int jobid
, num_jobs
, i
;
325 printf("Usage: lppause <printername> <jobid>\n");
330 jobid
= atoi(argv
[2]);
332 get_job_list(printer
, &job_list
, &num_jobs
);
334 for (i
= 0; i
< num_jobs
; i
++) {
335 if (job_list
[i
].jobid
== jobid
) {
336 job_list
[i
].status
= LPQ_PAUSED
;
337 set_job_list(printer
, job_list
, num_jobs
);
347 static int lpresume_command(int argc
, char **argv
)
349 struct vlp_job
*job_list
;
351 int jobid
, num_jobs
, i
;
354 printf("Usage: lpresume <printername> <jobid>\n");
359 jobid
= atoi(argv
[2]);
361 get_job_list(printer
, &job_list
, &num_jobs
);
363 for (i
= 0; i
< num_jobs
; i
++) {
364 if (job_list
[i
].jobid
== jobid
) {
365 job_list
[i
].status
= LPQ_QUEUED
;
366 set_job_list(printer
, job_list
, num_jobs
);
374 int main(int argc
, char **argv
)
376 /* Parameter check */
377 const char *printdb_path
= NULL
;
384 if (strncmp(argv
[1], "tdbfile", strlen("tdbfile")) != 0) {
389 printdb_path
= get_string_param(argv
[1]);
394 if (!(tdb
= tdb_open(printdb_path
, 0, 0, O_RDWR
| O_CREAT
,
396 printf("%s: unable to open %s\n", argv
[0], printdb_path
);
400 /* Ensure we are modes 666 */
402 chmod(printdb_path
, 0666);
406 if (strcmp(argv
[2], "lpq") == 0) {
407 return lpq_command(argc
- 2, &argv
[2]);
410 if (strcmp(argv
[2], "lprm") == 0) {
411 return lprm_command(argc
- 2, &argv
[2]);
414 if (strcmp(argv
[2], "print") == 0) {
415 return print_command(argc
- 2, &argv
[2]);
418 if (strcmp(argv
[2], "queuepause") == 0) {
419 return queuepause_command(argc
- 2, &argv
[2]);
422 if (strcmp(argv
[2], "queueresume") == 0) {
423 return queueresume_command(argc
- 2, &argv
[2]);
426 if (strcmp(argv
[2], "lppause") == 0) {
427 return lppause_command(argc
- 2, &argv
[2]);
430 if (strcmp(argv
[2], "lpresume") == 0) {
431 return lpresume_command(argc
- 2, &argv
[2]);
434 /* Unknown command */
436 printf("%s: invalid command %s\n", argv
[0], argv
[1]);