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