source: branches/samba-3.3.x/testsuite/printing/vlp.c

Last change on this file was 206, checked in by Herwig Bauernfeind, 16 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

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