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/>.
29 #define PRINT_FIRSTJOB "100"
31 static TDB_CONTEXT *tdb;
45 static void usage(void)
47 printf("Usage: vlp tdbfile=/tmp/vlp.tdb lpq|lprm|print|queuepause|queueresume|"
48 "lppause|lpresume [args]\n");
51 /* Return an array of vlp jobs that is the printer queue */
53 static void get_job_list(char *printer, struct vlp_job **job_list,
59 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
60 data = tdb_fetch_bystring(tdb, keystr);
62 *job_list = (struct vlp_job *)data.dptr;
63 *num_jobs = data.dsize / sizeof(struct vlp_job);
66 /* Store an array of vl jobs for the queue */
68 static void set_job_list(char *printer, struct vlp_job *job_list,
74 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
76 data.dptr = (unsigned char *)job_list;
77 data.dsize = num_jobs * sizeof(struct vlp_job);
78 tdb_store_bystring(tdb, keystr, data, TDB_REPLACE);
81 /* Return the next job number for a printer */
83 static int next_jobnum(char *printer)
88 slprintf(keystr, sizeof(keystr) - 1, "JOBNUM/%s", printer);
90 tdb_lock_bystring(tdb, keystr);
92 jobnum = tdb_fetch_int32(tdb, keystr);
94 /* Create next job index if none exists */
97 jobnum = atoi(PRINT_FIRSTJOB);
101 tdb_store_int32(tdb, keystr, jobnum);
103 tdb_unlock_bystring(tdb, keystr);
108 static void set_printer_status(char *printer, int status)
113 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
114 result = tdb_store_int32(tdb, keystr, status);
117 static int get_printer_status(char *printer)
122 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
124 data.dptr = (unsigned char *)keystr;
125 data.dsize = strlen(keystr) + 1;
127 if (!tdb_exists(tdb, data)) {
128 set_printer_status(printer, LPSTAT_OK);
132 return tdb_fetch_int32(tdb, keystr);
135 /* Display printer queue */
137 static int lpq_command(int argc, char **argv)
140 struct vlp_job *job_list = NULL;
141 int i, num_jobs, job_count = 0;
144 printf("Usage: lpq <printername>\n");
150 /* Display printer status */
152 switch (get_printer_status(printer)) {
157 printf("disabled\n");
165 /* Print queued documents */
167 get_job_list(printer, &job_list, &num_jobs);
169 for (i = 0; i < num_jobs; i++) {
170 if (job_list[i].deleted) continue;
171 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list[i].jobid,
173 (i == 0 && job_list[i].status == LPQ_QUEUED) ?
174 LPQ_SPOOLING : job_list[i].status,
175 (long int)job_list[i].submit_time, job_list[i].owner,
176 job_list[i].jobname);
187 static int lprm_command(int argc, char **argv)
190 int jobid, num_jobs, i;
191 struct vlp_job *job_list;
194 printf("Usage: lprm <printername> <jobid>\n");
199 jobid = atoi(argv[2]);
201 get_job_list(printer, &job_list, &num_jobs);
203 for (i = 0; i < num_jobs; i++) {
204 if (job_list[i].jobid == jobid) {
205 job_list[i].deleted = 1;
206 set_job_list(printer, job_list, num_jobs);
214 /* print command = print-test %p %s */
216 static int print_command(int argc, char **argv)
221 TDB_DATA value, queue;
225 printf("Usage: print <printername> <jobname>\n");
233 /* Create a job record */
235 slprintf(job.jobname, sizeof(job.jobname) - 1, "%s", argv[2]);
237 if (!(pw = getpwuid(getuid()))) {
241 slprintf(job.owner, sizeof(job.owner) - 1, "%s", 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_bystring(tdb, keystr);
255 /* Add job to end of queue */
257 queue.dptr = (unsigned char *)malloc(value.dsize + sizeof(struct vlp_job));
258 if (!queue.dptr) return 1;
260 memcpy(queue.dptr, value.dptr, value.dsize);
261 memcpy(queue.dptr + value.dsize, &job, sizeof(struct vlp_job));
263 queue.dsize = value.dsize + sizeof(struct vlp_job);
265 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
271 /* Create new queue */
272 queue.dptr = (unsigned char *)&job;
273 queue.dsize = sizeof(struct vlp_job);
275 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
281 /* Pause the queue */
283 static int queuepause_command(int argc, char **argv)
288 printf("Usage: queuepause <printername>\n");
293 set_printer_status(printer, LPSTAT_STOPPED);
298 /* Resume the queue */
300 static int queueresume_command(int argc, char **argv)
305 printf("Usage: queueresume <printername>\n");
310 set_printer_status(printer, LPSTAT_OK);
317 static int lppause_command(int argc, char **argv)
319 struct vlp_job *job_list;
321 int jobid, num_jobs, i;
324 printf("Usage: lppause <printername> <jobid>\n");
329 jobid = atoi(argv[2]);
331 get_job_list(printer, &job_list, &num_jobs);
333 for (i = 0; i < num_jobs; i++) {
334 if (job_list[i].jobid == jobid) {
335 job_list[i].status = LPQ_PAUSED;
336 set_job_list(printer, job_list, num_jobs);
346 static int lpresume_command(int argc, char **argv)
348 struct vlp_job *job_list;
350 int jobid, num_jobs, i;
353 printf("Usage: lpresume <printername> <jobid>\n");
358 jobid = atoi(argv[2]);
360 get_job_list(printer, &job_list, &num_jobs);
362 for (i = 0; i < num_jobs; i++) {
363 if (job_list[i].jobid == jobid) {
364 job_list[i].status = LPQ_QUEUED;
365 set_job_list(printer, job_list, num_jobs);
373 int main(int argc, char **argv)
375 /* Parameter check */
376 const char *printdb_path = NULL;
383 if (strncmp(argv[1], "tdbfile", strlen("tdbfile")) != 0) {
388 printdb_path = get_string_param(argv[1]);
393 if (!(tdb = tdb_open(printdb_path, 0, 0, O_RDWR | O_CREAT,
395 printf("%s: unable to open %s\n", argv[0], printdb_path);
399 /* Ensure we are modes 666 */
401 chmod(printdb_path, 0666);
405 if (strcmp(argv[2], "lpq") == 0) {
406 return lpq_command(argc - 2, &argv[2]);
409 if (strcmp(argv[2], "lprm") == 0) {
410 return lprm_command(argc - 2, &argv[2]);
413 if (strcmp(argv[2], "print") == 0) {
414 return print_command(argc - 2, &argv[2]);
417 if (strcmp(argv[2], "queuepause") == 0) {
418 return queuepause_command(argc - 2, &argv[2]);
421 if (strcmp(argv[2], "queueresume") == 0) {
422 return queueresume_command(argc - 2, &argv[2]);
425 if (strcmp(argv[2], "lppause") == 0) {
426 return lppause_command(argc - 2, &argv[2]);
429 if (strcmp(argv[2], "lpresume") == 0) {
430 return lpresume_command(argc - 2, &argv[2]);
433 /* Unknown command */
435 printf("%s: invalid command %s\n", argv[0], argv[1]);