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_by_string(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
,
69 slprintf(keystr
, sizeof(keystr
) - 1, "LPQ/%s", printer
);
71 tdb_store_by_string(tdb
, keystr
, job_list
,
72 num_jobs
* sizeof(struct vlp_job
));
75 /* Return the next job number for a printer */
77 static int next_jobnum(char *printer
)
82 slprintf(keystr
, sizeof(keystr
) - 1, "JOBNUM/%s", printer
);
84 tdb_lock_bystring(tdb
, keystr
);
86 jobnum
= tdb_fetch_int(tdb
, keystr
);
88 /* Create next job index if none exists */
91 jobnum
= atoi(PRINT_FIRSTJOB
);
95 tdb_store_int(tdb
, keystr
, jobnum
);
97 tdb_unlock_bystring(tdb
, keystr
);
102 static void set_printer_status(char *printer
, int status
)
107 slprintf(keystr
, sizeof(keystr
) - 1, "STATUS/%s", printer
);
108 result
= tdb_store_int(tdb
, keystr
, status
);
111 static int get_printer_status(char *printer
)
116 slprintf(keystr
, sizeof(keystr
) - 1, "STATUS/%s", printer
);
119 data
.dsize
= strlen(keystr
) + 1;
121 if (!tdb_exists(tdb
, data
)) {
122 set_printer_status(printer
, LPSTAT_OK
);
126 return tdb_fetch_int(tdb
, keystr
);
129 /* Display printer queue */
131 static int lpq_command(int argc
, char **argv
)
134 struct vlp_job
*job_list
= NULL
;
135 int i
, num_jobs
, job_count
= 0;
138 printf("Usage: lpq <printername>\n");
144 /* Display printer status */
146 switch (get_printer_status(printer
)) {
151 printf("disabled\n");
159 /* Print queued documents */
161 get_job_list(printer
, &job_list
, &num_jobs
);
163 for (i
= 0; i
< num_jobs
; i
++) {
164 if (job_list
[i
].deleted
) continue;
165 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list
[i
].jobid
,
167 (i
== 0 && job_list
[i
].status
== LPQ_QUEUED
) ?
168 LPQ_SPOOLING
: job_list
[i
].status
,
169 job_list
[i
].submit_time
, job_list
[i
].owner
,
170 job_list
[i
].jobname
);
181 static int lprm_command(int argc
, char **argv
)
184 int jobid
, num_jobs
, i
;
185 struct vlp_job
*job_list
;
188 printf("Usage: lprm <printername> <jobid>\n");
193 jobid
= atoi(argv
[2]);
195 get_job_list(printer
, &job_list
, &num_jobs
);
197 for (i
= 0; i
< num_jobs
; i
++) {
198 if (job_list
[i
].jobid
== jobid
) {
199 job_list
[i
].deleted
= 1;
200 set_job_list(printer
, job_list
, num_jobs
);
208 /* print command = print-test %p %s */
210 static int print_command(int argc
, char **argv
)
220 printf("Usage: print <printername> <jobname>\n");
228 /* Create a job record */
230 for (i
= 2; i
< argc
; i
++) {
231 fstrcat(job
.jobname
, argv
[i
]);
233 fstrcat(job
.jobname
, " ");
237 if (!(pw
= getpwuid(getuid()))) {
241 fstrcpy(job
.owner
, pw
->pw_name
);
243 job
.jobid
= next_jobnum(printer
);
245 job
.submit_time
= time(NULL
);
247 /* Store job entry in queue */
249 slprintf(keystr
, sizeof(keystr
) - 1, "LPQ/%s", printer
);
251 value
= tdb_fetch_by_string(tdb
, keystr
);
255 /* Add job to end of queue */
257 value
.dptr
= realloc(value
.dptr
, value
.dsize
+
258 sizeof(struct vlp_job
));
259 if (!value
.dptr
) return 1;
261 memcpy(value
.dptr
+ value
.dsize
, &job
, sizeof(struct vlp_job
));
263 tdb_store_by_string(tdb
, keystr
, value
.dptr
, value
.dsize
+
264 sizeof(struct vlp_job
));
270 /* Create new queue */
272 tdb_store_by_string(tdb
, keystr
, &job
, sizeof(struct vlp_job
));
278 /* Pause the queue */
280 static int queuepause_command(int argc
, char **argv
)
285 printf("Usage: queuepause <printername>\n");
290 set_printer_status(printer
, LPSTAT_STOPPED
);
295 /* Resume the queue */
297 static int queueresume_command(int argc
, char **argv
)
302 printf("Usage: queueresume <printername>\n");
307 set_printer_status(printer
, LPSTAT_OK
);
314 static int lppause_command(int argc
, char **argv
)
316 struct vlp_job
*job_list
;
318 int jobid
, num_jobs
, i
;
321 printf("Usage: lppause <printername> <jobid>\n");
326 jobid
= atoi(argv
[2]);
328 get_job_list(printer
, &job_list
, &num_jobs
);
330 for (i
= 0; i
< num_jobs
; i
++) {
331 if (job_list
[i
].jobid
== jobid
) {
332 job_list
[i
].status
= LPQ_PAUSED
;
333 set_job_list(printer
, job_list
, num_jobs
);
343 static int lpresume_command(int argc
, char **argv
)
345 struct vlp_job
*job_list
;
347 int jobid
, num_jobs
, i
;
350 printf("Usage: lpresume <printername> <jobid>\n");
355 jobid
= atoi(argv
[2]);
357 get_job_list(printer
, &job_list
, &num_jobs
);
359 for (i
= 0; i
< num_jobs
; i
++) {
360 if (job_list
[i
].jobid
== jobid
) {
361 job_list
[i
].status
= LPQ_QUEUED
;
362 set_job_list(printer
, job_list
, num_jobs
);
370 int main(int argc
, char **argv
)
372 /* Parameter check */
381 if (!(tdb
= tdb_open(PRINT_TDB
, 0, 0, O_RDWR
| O_CREAT
,
383 printf("%s: unable to open %s\n", argv
[0], PRINT_TDB
);
387 /* Ensure we are modes 666 */
389 chmod(PRINT_TDB
, 0666);
393 if (strcmp(argv
[1], "lpq") == 0) {
394 return lpq_command(argc
- 1, &argv
[1]);
397 if (strcmp(argv
[1], "lprm") == 0) {
398 return lprm_command(argc
- 1, &argv
[1]);
401 if (strcmp(argv
[1], "print") == 0) {
402 return print_command(argc
- 1, &argv
[1]);
405 if (strcmp(argv
[1], "queuepause") == 0) {
406 return queuepause_command(argc
- 1, &argv
[1]);
409 if (strcmp(argv
[1], "queueresume") == 0) {
410 return queueresume_command(argc
- 1, &argv
[1]);
413 if (strcmp(argv
[1], "lppause") == 0) {
414 return lppause_command(argc
- 1, &argv
[1]);
417 if (strcmp(argv
[1], "lpresume") == 0) {
418 return lpresume_command(argc
- 1, &argv
[1]);
421 /* Unknown command */
423 printf("%s: invalid command %s\n", argv
[0], argv
[1]);