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/>.
24 #define PRINT_TDB "/tmp/vlp.tdb"
25 #define PRINT_FIRSTJOB "100"
27 static TDB_CONTEXT
*tdb
;
41 static void usage(void)
43 printf("Usage: print-test lpq|lprm|print|queuepause|queueresume|"
44 "lppause|lpresume [args]\n");
47 /* Return an array of vlp jobs that is the printer queue */
49 static void get_job_list(char *printer
, struct vlp_job
**job_list
,
55 slprintf(keystr
, sizeof(keystr
) - 1, "LPQ/%s", printer
);
56 data
= tdb_fetch_bystring(tdb
, keystr
);
58 *job_list
= (struct vlp_job
*)data
.dptr
;
59 *num_jobs
= data
.dsize
/ sizeof(struct vlp_job
);
62 /* Store an array of vl jobs for the queue */
64 static void set_job_list(char *printer
, struct vlp_job
*job_list
,
70 slprintf(keystr
, sizeof(keystr
) - 1, "LPQ/%s", printer
);
72 data
.dptr
= (unsigned char *)job_list
;
73 data
.dsize
= num_jobs
* sizeof(struct vlp_job
);
74 tdb_store_bystring(tdb
, keystr
, data
, TDB_REPLACE
);
77 /* Return the next job number for a printer */
79 static int next_jobnum(char *printer
)
84 slprintf(keystr
, sizeof(keystr
) - 1, "JOBNUM/%s", printer
);
86 tdb_lock_bystring(tdb
, keystr
);
88 jobnum
= tdb_fetch_int32(tdb
, keystr
);
90 /* Create next job index if none exists */
93 jobnum
= atoi(PRINT_FIRSTJOB
);
97 tdb_store_int32(tdb
, keystr
, jobnum
);
99 tdb_unlock_bystring(tdb
, keystr
);
104 static void set_printer_status(char *printer
, int status
)
109 slprintf(keystr
, sizeof(keystr
) - 1, "STATUS/%s", printer
);
110 result
= tdb_store_int32(tdb
, keystr
, status
);
113 static int get_printer_status(char *printer
)
118 slprintf(keystr
, sizeof(keystr
) - 1, "STATUS/%s", printer
);
120 data
.dptr
= (unsigned char *)keystr
;
121 data
.dsize
= strlen(keystr
) + 1;
123 if (!tdb_exists(tdb
, data
)) {
124 set_printer_status(printer
, LPSTAT_OK
);
128 return tdb_fetch_int32(tdb
, keystr
);
131 /* Display printer queue */
133 static int lpq_command(int argc
, char **argv
)
136 struct vlp_job
*job_list
= NULL
;
137 int i
, num_jobs
, job_count
= 0;
140 printf("Usage: lpq <printername>\n");
146 /* Display printer status */
148 switch (get_printer_status(printer
)) {
153 printf("disabled\n");
161 /* Print queued documents */
163 get_job_list(printer
, &job_list
, &num_jobs
);
165 for (i
= 0; i
< num_jobs
; i
++) {
166 if (job_list
[i
].deleted
) continue;
167 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list
[i
].jobid
,
169 (i
== 0 && job_list
[i
].status
== LPQ_QUEUED
) ?
170 LPQ_SPOOLING
: job_list
[i
].status
,
171 job_list
[i
].submit_time
, job_list
[i
].owner
,
172 job_list
[i
].jobname
);
183 static int lprm_command(int argc
, char **argv
)
186 int jobid
, num_jobs
, i
;
187 struct vlp_job
*job_list
;
190 printf("Usage: lprm <printername> <jobid>\n");
195 jobid
= atoi(argv
[2]);
197 get_job_list(printer
, &job_list
, &num_jobs
);
199 for (i
= 0; i
< num_jobs
; i
++) {
200 if (job_list
[i
].jobid
== jobid
) {
201 job_list
[i
].deleted
= 1;
202 set_job_list(printer
, job_list
, num_jobs
);
210 /* print command = print-test %p %s */
212 static int print_command(int argc
, char **argv
)
217 TDB_DATA value
, queue
;
222 printf("Usage: print <printername> <jobname>\n");
230 /* Create a job record */
232 for (i
= 2; i
< argc
; i
++) {
233 fstrcat(job
.jobname
, argv
[i
]);
235 fstrcat(job
.jobname
, " ");
239 if (!(pw
= getpwuid(getuid()))) {
243 fstrcpy(job
.owner
, pw
->pw_name
);
245 job
.jobid
= next_jobnum(printer
);
247 job
.submit_time
= time(NULL
);
249 /* Store job entry in queue */
251 slprintf(keystr
, sizeof(keystr
) - 1, "LPQ/%s", printer
);
253 value
= tdb_fetch_bystring(tdb
, keystr
);
257 /* Add job to end of queue */
259 queue
.dptr
= SMB_MALLOC(value
.dsize
+ sizeof(struct vlp_job
));
260 if (!queue
.dptr
) return 1;
262 memcpy(queue
.dptr
, value
.dptr
, value
.dsize
);
263 memcpy(queue
.dptr
+ value
.dsize
, &job
, sizeof(struct vlp_job
));
265 queue
.dsize
= value
.dsize
+ sizeof(struct vlp_job
);
267 tdb_store_bystring(tdb
, keystr
, queue
, TDB_REPLACE
);
273 /* Create new queue */
274 queue
.dptr
= (unsigned char *)&job
;
275 queue
.dsize
= sizeof(struct vlp_job
);
277 tdb_store_bystring(tdb
, keystr
, queue
, TDB_REPLACE
);
283 /* Pause the queue */
285 static int queuepause_command(int argc
, char **argv
)
290 printf("Usage: queuepause <printername>\n");
295 set_printer_status(printer
, LPSTAT_STOPPED
);
300 /* Resume the queue */
302 static int queueresume_command(int argc
, char **argv
)
307 printf("Usage: queueresume <printername>\n");
312 set_printer_status(printer
, LPSTAT_OK
);
319 static int lppause_command(int argc
, char **argv
)
321 struct vlp_job
*job_list
;
323 int jobid
, num_jobs
, i
;
326 printf("Usage: lppause <printername> <jobid>\n");
331 jobid
= atoi(argv
[2]);
333 get_job_list(printer
, &job_list
, &num_jobs
);
335 for (i
= 0; i
< num_jobs
; i
++) {
336 if (job_list
[i
].jobid
== jobid
) {
337 job_list
[i
].status
= LPQ_PAUSED
;
338 set_job_list(printer
, job_list
, num_jobs
);
348 static int lpresume_command(int argc
, char **argv
)
350 struct vlp_job
*job_list
;
352 int jobid
, num_jobs
, i
;
355 printf("Usage: lpresume <printername> <jobid>\n");
360 jobid
= atoi(argv
[2]);
362 get_job_list(printer
, &job_list
, &num_jobs
);
364 for (i
= 0; i
< num_jobs
; i
++) {
365 if (job_list
[i
].jobid
== jobid
) {
366 job_list
[i
].status
= LPQ_QUEUED
;
367 set_job_list(printer
, job_list
, num_jobs
);
375 int main(int argc
, char **argv
)
377 /* Parameter check */
386 if (!(tdb
= tdb_open(PRINT_TDB
, 0, 0, O_RDWR
| O_CREAT
,
388 printf("%s: unable to open %s\n", argv
[0], PRINT_TDB
);
392 /* Ensure we are modes 666 */
394 chmod(PRINT_TDB
, 0666);
398 if (strcmp(argv
[1], "lpq") == 0) {
399 return lpq_command(argc
- 1, &argv
[1]);
402 if (strcmp(argv
[1], "lprm") == 0) {
403 return lprm_command(argc
- 1, &argv
[1]);
406 if (strcmp(argv
[1], "print") == 0) {
407 return print_command(argc
- 1, &argv
[1]);
410 if (strcmp(argv
[1], "queuepause") == 0) {
411 return queuepause_command(argc
- 1, &argv
[1]);
414 if (strcmp(argv
[1], "queueresume") == 0) {
415 return queueresume_command(argc
- 1, &argv
[1]);
418 if (strcmp(argv
[1], "lppause") == 0) {
419 return lppause_command(argc
- 1, &argv
[1]);
422 if (strcmp(argv
[1], "lpresume") == 0) {
423 return lpresume_command(argc
- 1, &argv
[1]);
426 /* Unknown command */
428 printf("%s: invalid command %s\n", argv
[0], argv
[1]);