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 = (unsigned char *)SMB_MALLOC(value.dsize +
260 sizeof(struct vlp_job));
261 if (!queue.dptr) return 1;
263 memcpy(queue.dptr, value.dptr, value.dsize);
264 memcpy(queue.dptr + value.dsize, &job, sizeof(struct vlp_job));
266 queue.dsize = value.dsize + sizeof(struct vlp_job);
268 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
274 /* Create new queue */
275 queue.dptr = (unsigned char *)&job;
276 queue.dsize = sizeof(struct vlp_job);
278 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
284 /* Pause the queue */
286 static int queuepause_command(int argc, char **argv)
291 printf("Usage: queuepause <printername>\n");
296 set_printer_status(printer, LPSTAT_STOPPED);
301 /* Resume the queue */
303 static int queueresume_command(int argc, char **argv)
308 printf("Usage: queueresume <printername>\n");
313 set_printer_status(printer, LPSTAT_OK);
320 static int lppause_command(int argc, char **argv)
322 struct vlp_job *job_list;
324 int jobid, num_jobs, i;
327 printf("Usage: lppause <printername> <jobid>\n");
332 jobid = atoi(argv[2]);
334 get_job_list(printer, &job_list, &num_jobs);
336 for (i = 0; i < num_jobs; i++) {
337 if (job_list[i].jobid == jobid) {
338 job_list[i].status = LPQ_PAUSED;
339 set_job_list(printer, job_list, num_jobs);
349 static int lpresume_command(int argc, char **argv)
351 struct vlp_job *job_list;
353 int jobid, num_jobs, i;
356 printf("Usage: lpresume <printername> <jobid>\n");
361 jobid = atoi(argv[2]);
363 get_job_list(printer, &job_list, &num_jobs);
365 for (i = 0; i < num_jobs; i++) {
366 if (job_list[i].jobid == jobid) {
367 job_list[i].status = LPQ_QUEUED;
368 set_job_list(printer, job_list, num_jobs);
376 int main(int argc, char **argv)
378 /* Parameter check */
387 if (!(tdb = tdb_open(PRINT_TDB, 0, 0, O_RDWR | O_CREAT,
389 printf("%s: unable to open %s\n", argv[0], PRINT_TDB);
393 /* Ensure we are modes 666 */
395 chmod(PRINT_TDB, 0666);
399 if (strcmp(argv[1], "lpq") == 0) {
400 return lpq_command(argc - 1, &argv[1]);
403 if (strcmp(argv[1], "lprm") == 0) {
404 return lprm_command(argc - 1, &argv[1]);
407 if (strcmp(argv[1], "print") == 0) {
408 return print_command(argc - 1, &argv[1]);
411 if (strcmp(argv[1], "queuepause") == 0) {
412 return queuepause_command(argc - 1, &argv[1]);
415 if (strcmp(argv[1], "queueresume") == 0) {
416 return queueresume_command(argc - 1, &argv[1]);
419 if (strcmp(argv[1], "lppause") == 0) {
420 return lppause_command(argc - 1, &argv[1]);
423 if (strcmp(argv[1], "lpresume") == 0) {
424 return lpresume_command(argc - 1, &argv[1]);
427 /* Unknown command */
429 printf("%s: invalid command %s\n", argv[0], argv[1]);