494a811f019940a5d00607c3504f10100dbbc0c0
[kai/samba.git] / source3 / printing / tests / vlp.c
1 /* 
2    Unix SMB/Netbios implementation.
3
4    Virtual lp system for printer testing
5
6    Copyright (C) Tim Potter 2000
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "printing.h"
24
25 #ifdef malloc
26 #undef malloc
27 #endif
28
29 #define PRINT_FIRSTJOB "100"
30
31 static TDB_CONTEXT *tdb;
32
33 struct vlp_job {
34         fstring owner;
35         int jobid;
36         fstring jobname;
37         int size;
38         int status;
39         time_t submit_time;
40         int deleted;
41 };
42
43 /* Print usage */
44
45 static void usage(void)
46 {
47         printf("Usage: vlp tdbfile=/tmp/vlp.tdb lpq|lprm|print|queuepause|queueresume|"
48                "lppause|lpresume [args]\n");
49 }
50
51 /* Return an array of vlp jobs that is the printer queue */
52
53 static void get_job_list(char *printer, struct vlp_job **job_list, 
54                          int *num_jobs)
55 {
56         fstring keystr;
57         TDB_DATA data;
58
59         slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
60         data = tdb_fetch_bystring(tdb, keystr);
61
62         *job_list = (struct vlp_job *)data.dptr;
63         *num_jobs = data.dsize / sizeof(struct vlp_job);
64 }
65
66 /* Store an array of vl jobs for the queue */
67
68 static void set_job_list(char *printer, struct vlp_job *job_list, 
69                          int num_jobs)
70 {
71         fstring keystr;
72         TDB_DATA data;
73
74         slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
75
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);
79 }
80
81 /* Return the next job number for a printer */
82
83 static int next_jobnum(char *printer)
84 {
85         fstring keystr;
86         int jobnum;
87
88         slprintf(keystr, sizeof(keystr) - 1, "JOBNUM/%s", printer);
89
90         tdb_lock_bystring(tdb, keystr);
91
92         jobnum = tdb_fetch_int32(tdb, keystr);
93
94         /* Create next job index if none exists */
95
96         if (jobnum == -1) {
97                 jobnum = atoi(PRINT_FIRSTJOB);
98         }
99
100         jobnum++;
101         tdb_store_int32(tdb, keystr, jobnum);
102
103         tdb_unlock_bystring(tdb, keystr);
104
105         return jobnum;
106 }
107
108 static void set_printer_status(char *printer, int status)
109 {
110         fstring keystr;
111         int result;
112
113         slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
114         result = tdb_store_int32(tdb, keystr, status);
115 }
116
117 static int get_printer_status(char *printer)
118 {
119         fstring keystr;
120         TDB_DATA data;
121
122         slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
123
124         data.dptr = (unsigned char *)keystr;
125         data.dsize = strlen(keystr) + 1;
126
127         if (!tdb_exists(tdb, data)) {
128                 set_printer_status(printer, LPSTAT_OK);
129                 return LPSTAT_OK;
130         }
131
132         return tdb_fetch_int32(tdb, keystr);
133 }
134
135 /* Display printer queue */
136
137 static int lpq_command(int argc, char **argv)
138 {
139         char *printer;
140         struct vlp_job *job_list = NULL;
141         int i, num_jobs, job_count = 0;
142
143         if (argc != 2) {
144                 printf("Usage: lpq <printername>\n");
145                 return 1;
146         }
147
148         printer = argv[1];
149
150         /* Display printer status */
151
152         switch (get_printer_status(printer)) {
153         case LPSTAT_OK:
154                 printf("enabled\n");
155                 break;
156         case LPSTAT_STOPPED:
157                 printf("disabled\n");
158                 break;
159         case LPSTAT_ERROR:
160         default:
161                 printf("error\n");
162                 break;
163         }
164
165         /* Print queued documents */
166
167         get_job_list(printer, &job_list, &num_jobs);
168
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,
172                        job_list[i].size, 
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);
177                 job_count++;
178         }
179
180         free(job_list);
181
182         return 0;
183 }
184
185 /* Remove a job */
186
187 static int lprm_command(int argc, char **argv)
188 {
189         char *printer;
190         int jobid, num_jobs, i;
191         struct vlp_job *job_list;
192
193         if (argc < 3) {
194                 printf("Usage: lprm <printername> <jobid>\n");
195                 return 1;
196         }
197
198         printer = argv[1];
199         jobid = atoi(argv[2]);
200
201         get_job_list(printer, &job_list, &num_jobs);
202
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);
207                         break;
208                 }
209         }
210
211         return 0;
212 }
213
214 /* print command = print-test %p %s */
215
216 static int print_command(int argc, char **argv)
217 {
218         char *printer;
219         fstring keystr;
220         struct passwd *pw;
221         TDB_DATA value, queue;
222         struct vlp_job job;
223
224         if (argc < 3) {
225                 printf("Usage: print <printername> <jobname>\n");
226                 return 1;
227         }
228
229         printer = argv[1];
230
231         ZERO_STRUCT(job);
232
233         /* Create a job record */
234
235         slprintf(job.jobname, sizeof(job.jobname) - 1, "%s", argv[2]);
236
237         if (!(pw = getpwuid(getuid()))) {
238                 return 1;
239         }
240
241         slprintf(job.owner, sizeof(job.owner) - 1, "%s", pw->pw_name);
242
243         job.jobid = next_jobnum(printer);
244         job.size = 666;
245         job.submit_time = time(NULL);
246
247         /* Store job entry in queue */
248
249         slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
250
251         value = tdb_fetch_bystring(tdb, keystr);
252
253         if (value.dptr) {
254
255                 /* Add job to end of queue */
256
257                 queue.dptr = (unsigned char *)malloc(value.dsize + sizeof(struct vlp_job));
258                 if (!queue.dptr) return 1;
259
260                 memcpy(queue.dptr, value.dptr, value.dsize);
261                 memcpy(queue.dptr + value.dsize, &job, sizeof(struct vlp_job));
262
263                 queue.dsize = value.dsize + sizeof(struct vlp_job);
264
265                 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
266
267                 free(queue.dptr);
268
269         } else {
270
271                 /* Create new queue */
272                 queue.dptr = (unsigned char *)&job;
273                 queue.dsize = sizeof(struct vlp_job);
274
275                 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
276         }
277
278         return 0;
279 }
280
281 /* Pause the queue */
282
283 static int queuepause_command(int argc, char **argv)
284 {
285         char *printer;
286
287         if (argc != 2) {
288                 printf("Usage: queuepause <printername>\n");
289                 return 1;
290         }
291
292         printer = argv[1];
293         set_printer_status(printer, LPSTAT_STOPPED);
294
295         return 0;
296 }
297
298 /* Resume the queue */
299
300 static int queueresume_command(int argc, char **argv)
301 {
302         char *printer;
303
304         if (argc != 2) {
305                 printf("Usage: queueresume <printername>\n");
306                 return 1;
307         }
308
309         printer = argv[1];
310         set_printer_status(printer, LPSTAT_OK);
311
312         return 0;
313 }
314
315 /* Pause a job */
316
317 static int lppause_command(int argc, char **argv)
318 {
319         struct vlp_job *job_list;
320         char *printer;
321         int jobid, num_jobs, i;
322
323         if (argc != 3) {
324                 printf("Usage: lppause <printername> <jobid>\n");
325                 return 1;
326         }
327
328         printer = argv[1];
329         jobid = atoi(argv[2]);
330
331         get_job_list(printer, &job_list, &num_jobs);
332
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);
337                         return 0;
338                 }
339         }
340
341         return 1;
342 }
343
344 /* Resume a job */
345
346 static int lpresume_command(int argc, char **argv)
347 {
348         struct vlp_job *job_list;
349         char *printer;
350         int jobid, num_jobs, i;
351
352         if (argc != 3) {
353                 printf("Usage: lpresume <printername> <jobid>\n");
354                 return 1;
355         }
356
357         printer = argv[1];
358         jobid = atoi(argv[2]);
359
360         get_job_list(printer, &job_list, &num_jobs);
361
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);
366                         return 0;
367                 }
368         }
369
370         return 1;
371 }
372
373 int main(int argc, char **argv)
374 {
375         /* Parameter check */
376         const char *printdb_path = NULL;
377
378         if (argc < 2) {
379                 usage();
380                 return 1;
381         }
382
383         if (strncmp(argv[1], "tdbfile", strlen("tdbfile")) != 0) {
384                 usage();
385                 return 1;
386         }
387
388         printdb_path = get_string_param(argv[1]);
389         if (!printdb_path) {
390                 return 1;
391         }
392
393         if (!(tdb = tdb_open(printdb_path, 0, 0, O_RDWR | O_CREAT,
394                              0666))) {
395                 printf("%s: unable to open %s\n", argv[0], printdb_path);
396                 return 1;
397         }
398
399         /* Ensure we are modes 666 */
400
401         chmod(printdb_path, 0666);
402
403         /* Do commands */
404
405         if (strcmp(argv[2], "lpq") == 0) {
406                 return lpq_command(argc - 2, &argv[2]);
407         }
408
409         if (strcmp(argv[2], "lprm") == 0) {
410                 return lprm_command(argc - 2, &argv[2]);
411         }
412
413         if (strcmp(argv[2], "print") == 0) {
414                 return print_command(argc - 2, &argv[2]);
415         }
416
417         if (strcmp(argv[2], "queuepause") == 0) {
418                 return queuepause_command(argc - 2, &argv[2]);
419         }
420
421         if (strcmp(argv[2], "queueresume") == 0) {
422                 return queueresume_command(argc - 2, &argv[2]);
423         }
424
425         if (strcmp(argv[2], "lppause") == 0) {
426                 return lppause_command(argc - 2, &argv[2]);
427         }
428
429         if (strcmp(argv[2], "lpresume") == 0) {
430                 return lpresume_command(argc - 2, &argv[2]);
431         }
432
433         /* Unknown command */
434
435         printf("%s: invalid command %s\n", argv[0], argv[1]);
436         return 1;
437 }