RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / printing / lpq_parse.c
1 /* 
2    Unix SMB/CIFS implementation.
3    lpq parsing routines
4    Copyright (C) Andrew Tridgell 2000
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 static const char *Months[13] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
23                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Err"};
24
25
26 /*******************************************************************
27  Process time fields
28 ********************************************************************/
29
30 static time_t EntryTime(fstring tok[], int ptr, int count, int minimum)
31 {
32         time_t jobtime,jobtime1;
33
34         jobtime = time(NULL);           /* default case: take current time */
35         if (count >= minimum) {
36                 struct tm *t;
37                 int i, day, hour, min, sec;
38                 char   *c;
39
40                 for (i=0; i<13; i++) {
41                         if (!strncmp(tok[ptr], Months[i],3)) {
42                                 break; /* Find month */
43                         }
44                 }
45
46                 if (i<12) {
47                         t = localtime(&jobtime);
48                         if (!t) {
49                                 return (time_t)-1;
50                         }
51                         day = atoi(tok[ptr+1]);
52                         c=(char *)(tok[ptr+2]);
53                         *(c+2)=0;
54                         hour = atoi(c);
55                         *(c+5)=0;
56                         min = atoi(c+3);
57                         if(*(c+6) != 0) {
58                                 sec = atoi(c+6);
59                         } else {
60                                 sec=0;
61                         }
62
63                         if ((t->tm_mon < i)|| ((t->tm_mon == i)&&
64                                         ((t->tm_mday < day)||
65                                         ((t->tm_mday == day)&&
66                                         (t->tm_hour*60+t->tm_min < hour*60+min))))) {
67                                 t->tm_year--;           /* last year's print job */
68                         }
69
70                         t->tm_mon = i;
71                         t->tm_mday = day;
72                         t->tm_hour = hour;
73                         t->tm_min = min;
74                         t->tm_sec = sec;
75                         jobtime1 = mktime(t);
76                         if (jobtime1 != (time_t)-1) {
77                                 jobtime = jobtime1;
78                         }
79                 }
80         }
81         return jobtime;
82 }
83
84 /****************************************************************************
85 parse a lpq line
86
87 here is an example of lpq output under bsd
88
89 Warning: no daemon present
90 Rank   Owner      Job  Files                                 Total Size
91 1st    tridge     148  README                                8096 bytes
92
93 here is an example of lpq output under osf/1
94
95 Warning: no daemon present
96 Rank   Pri Owner      Job  Files                             Total Size
97 1st    0   tridge     148  README                            8096 bytes
98
99
100 <allan@umich.edu> June 30, 1998.
101 Modified to handle file names with spaces, like the parse_lpq_lprng code
102 further below.
103 ****************************************************************************/
104
105 static bool parse_lpq_bsd(char *line,print_queue_struct *buf,bool first)
106 {
107 #ifdef  OSF1
108 #define RANKTOK 0
109 #define PRIOTOK 1
110 #define USERTOK 2
111 #define JOBTOK  3
112 #define FILETOK 4
113 #define TOTALTOK (count - 2)
114 #define NTOK    6
115 #define MAXTOK  128
116 #else   /* OSF1 */
117 #define RANKTOK 0
118 #define USERTOK 1
119 #define JOBTOK  2
120 #define FILETOK 3
121 #define TOTALTOK (count - 2)
122 #define NTOK    5
123 #define MAXTOK  128
124 #endif  /* OSF1 */
125
126         char *tok[MAXTOK];
127         int  count = 0;
128         pstring line2;
129
130         pstrcpy(line2,line);
131
132 #ifdef  OSF1
133         {
134                 size_t length;
135                 length = strlen(line2);
136                 if (line2[length-3] == ':') {
137                         return False;
138                 }
139         }
140 #endif  /* OSF1 */
141
142         /* FIXME: Use next_token rather than strtok! */
143         tok[0] = strtok(line2," \t");
144         count++;
145
146         while ((count < MAXTOK) && ((tok[count] = strtok(NULL," \t")) != NULL)) {
147                 count++;
148         }
149
150         /* we must get at least NTOK tokens */
151         if (count < NTOK) {
152                 return False;
153         }
154
155         /* the Job and Total columns must be integer */
156         if (!isdigit((int)*tok[JOBTOK]) || !isdigit((int)*tok[TOTALTOK])) {
157                 return False;
158         }
159
160         buf->job = atoi(tok[JOBTOK]);
161         buf->size = atoi(tok[TOTALTOK]);
162         buf->status = strequal(tok[RANKTOK],"active")?LPQ_PRINTING:LPQ_QUEUED;
163         buf->time = time(NULL);
164         fstrcpy(buf->fs_user,tok[USERTOK]);
165         fstrcpy(buf->fs_file,tok[FILETOK]);
166
167         if ((FILETOK + 1) != TOTALTOK) {
168                 int i;
169
170                 for (i = (FILETOK + 1); i < TOTALTOK; i++) {
171                         /* FIXME: Using fstrcat rather than other means is a bit
172                          * inefficient; this might be a problem for enormous queues with
173                          * many fields. */
174                         fstrcat(buf->fs_file, " ");
175                         fstrcat(buf->fs_file, tok[i]);
176                 }
177                 /* Ensure null termination. */
178                 fstrterminate(buf->fs_file);
179         }
180
181 #ifdef PRIOTOK
182         buf->priority = atoi(tok[PRIOTOK]);
183 #else
184         buf->priority = 1;
185 #endif
186         return True;
187 }
188
189 /*
190 <magnus@hum.auc.dk>
191 LPRng_time modifies the current date by inserting the hour and minute from
192 the lpq output.  The lpq time looks like "23:15:07"
193
194 <allan@umich.edu> June 30, 1998.
195 Modified to work with the re-written parse_lpq_lprng routine.
196
197 <J.P.M.v.Itegem@tue.nl> Dec 17,1999
198 Modified to work with lprng 3.16
199 With lprng 3.16 The lpq time looks like
200                        "23:15:07"
201                        "23:15:07.100"
202                        "1999-12-16-23:15:07"
203                        "1999-12-16-23:15:07.100"
204
205 */
206 static time_t LPRng_time(char *time_string)
207 {
208         time_t jobtime;
209         struct tm *t;
210
211         jobtime = time(NULL);         /* default case: take current time */
212         t = localtime(&jobtime);
213         if (!t) {
214                 return (time_t)-1;
215         }
216
217         if ( atoi(time_string) < 24 ){
218                 t->tm_hour = atoi(time_string);
219                 t->tm_min = atoi(time_string+3);
220                 t->tm_sec = atoi(time_string+6);
221         } else {
222                 t->tm_year = atoi(time_string)-1900;
223                 t->tm_mon = atoi(time_string+5)-1;
224                 t->tm_mday = atoi(time_string+8);
225                 t->tm_hour = atoi(time_string+11);
226                 t->tm_min = atoi(time_string+14);
227                 t->tm_sec = atoi(time_string+17);
228         }    
229         jobtime = mktime(t);
230
231         return jobtime;
232 }
233
234 /****************************************************************************
235   parse a lprng lpq line
236   <allan@umich.edu> June 30, 1998.
237   Re-wrote this to handle file names with spaces, multiple file names on one
238   lpq line, etc;
239
240 ****************************************************************************/
241
242 static bool parse_lpq_lprng(char *line,print_queue_struct *buf,bool first)
243 {
244 #define LPRNG_RANKTOK   0
245 #define LPRNG_USERTOK   1
246 #define LPRNG_PRIOTOK   2
247 #define LPRNG_JOBTOK    3
248 #define LPRNG_FILETOK   4
249 #define LPRNG_TOTALTOK  (num_tok - 2)
250 #define LPRNG_TIMETOK   (num_tok - 1)
251 #define LPRNG_NTOK      7
252 #define LPRNG_MAXTOK    128 /* PFMA just to keep us from running away. */
253
254         fstring tokarr[LPRNG_MAXTOK];
255         const char *cptr;
256         char *ptr;
257         int num_tok = 0;
258
259         cptr = line;
260         while((num_tok < LPRNG_MAXTOK) && next_token( &cptr, tokarr[num_tok], " \t", sizeof(fstring))) {
261                 num_tok++;
262         }
263
264         /* We must get at least LPRNG_NTOK tokens. */
265         if (num_tok < LPRNG_NTOK) {
266                 return False;
267         }
268
269         if (!isdigit((int)*tokarr[LPRNG_JOBTOK]) || !isdigit((int)*tokarr[LPRNG_TOTALTOK])) {
270                 return False;
271         }
272
273         buf->job  = atoi(tokarr[LPRNG_JOBTOK]);
274         buf->size = atoi(tokarr[LPRNG_TOTALTOK]);
275
276         if (strequal(tokarr[LPRNG_RANKTOK],"active")) {
277                 buf->status = LPQ_PRINTING;
278         } else if (strequal(tokarr[LPRNG_RANKTOK],"done")) {
279                 buf->status = LPQ_PRINTED;
280         } else if (isdigit((int)*tokarr[LPRNG_RANKTOK])) {
281                 buf->status = LPQ_QUEUED;
282         } else {
283                 buf->status = LPQ_PAUSED;
284         }
285
286         buf->priority = *tokarr[LPRNG_PRIOTOK] -'A';
287
288         buf->time = LPRng_time(tokarr[LPRNG_TIMETOK]);
289
290         fstrcpy(buf->fs_user,tokarr[LPRNG_USERTOK]);
291
292         /* The '@hostname' prevents windows from displaying the printing icon
293          * for the current user on the taskbar.  Plop in a null.
294          */
295
296         if ((ptr = strchr_m(buf->fs_user,'@')) != NULL) {
297                 *ptr = '\0';
298         }
299
300         fstrcpy(buf->fs_file,tokarr[LPRNG_FILETOK]);
301
302         if ((LPRNG_FILETOK + 1) != LPRNG_TOTALTOK) {
303                 int i;
304
305                 for (i = (LPRNG_FILETOK + 1); i < LPRNG_TOTALTOK; i++) {
306                         /* FIXME: Using fstrcat rather than other means is a bit
307                          * inefficient; this might be a problem for enormous queues with
308                          * many fields. */
309                         fstrcat(buf->fs_file, " ");
310                         fstrcat(buf->fs_file, tokarr[i]);
311                 }
312                 /* Ensure null termination. */
313                 fstrterminate(buf->fs_file);
314         }
315
316         return True;
317 }
318
319 /*******************************************************************
320 parse lpq on an aix system
321
322 Queue   Dev   Status    Job Files              User         PP %   Blks  Cp Rnk
323 ------- ----- --------- --- ------------------ ---------- ---- -- ----- --- ---
324 lazer   lazer READY
325 lazer   lazer RUNNING   537 6297doc.A          kvintus@IE    0 10  2445   1   1
326               QUEUED    538 C.ps               root@IEDVB           124   1   2
327               QUEUED    539 E.ps               root@IEDVB            28   1   3
328               QUEUED    540 L.ps               root@IEDVB           172   1   4
329               QUEUED    541 P.ps               root@IEDVB            22   1   5
330 ********************************************************************/
331
332 static bool parse_lpq_aix(char *line,print_queue_struct *buf,bool first)
333 {
334         fstring tok[11];
335         int count=0;
336         const char *cline = line;
337
338         /* handle the case of "(standard input)" as a filename */
339         string_sub(line,"standard input","STDIN",0);
340         all_string_sub(line,"(","\"",0);
341         all_string_sub(line,")","\"",0);
342
343         for (count=0; count<10 && next_token(&cline,tok[count],NULL, sizeof(tok[count])); count++) {
344                 ;
345         }
346
347         /* we must get 6 tokens */
348         if (count < 10) {
349                 if ((count == 7) && ((strcmp(tok[0],"QUEUED") == 0) || (strcmp(tok[0],"HELD") == 0))) {
350                         /* the 2nd and 5th columns must be integer */
351                         if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[4])) {
352                                 return False;
353                         }
354                         buf->size = atoi(tok[4]) * 1024;
355                         /* if the fname contains a space then use STDIN */
356                         if (strchr_m(tok[2],' ')) {
357                                 fstrcpy(tok[2],"STDIN");
358                         }
359
360                         /* only take the last part of the filename */
361                         {
362                                 fstring tmp;
363                                 char *p = strrchr_m(tok[2],'/');
364                                 if (p) {
365                                         fstrcpy(tmp,p+1);
366                                         fstrcpy(tok[2],tmp);
367                                 }
368                         }
369
370                         buf->job = atoi(tok[1]);
371                         buf->status = strequal(tok[0],"HELD")?LPQ_PAUSED:LPQ_QUEUED;
372                         buf->priority = 0;
373                         buf->time = time(NULL);
374                         fstrcpy(buf->fs_user,tok[3]);
375                         fstrcpy(buf->fs_file,tok[2]);
376                 } else {
377                         DEBUG(6,("parse_lpq_aix count=%d\n", count));
378                         return False;
379                 }
380         } else {
381                 /* the 4th and 9th columns must be integer */
382                 if (!isdigit((int)*tok[3]) || !isdigit((int)*tok[8])) {
383                         return False;
384                 }
385
386                 buf->size = atoi(tok[8]) * 1024;
387                 /* if the fname contains a space then use STDIN */
388                 if (strchr_m(tok[4],' ')) {
389                         fstrcpy(tok[4],"STDIN");
390                 }
391
392                 /* only take the last part of the filename */
393                 {
394                         fstring tmp;
395                         char *p = strrchr_m(tok[4],'/');
396                         if (p) {
397                                 fstrcpy(tmp,p+1);
398                                 fstrcpy(tok[4],tmp);
399                         }
400                 }
401
402                 buf->job = atoi(tok[3]);
403                 buf->status = strequal(tok[2],"RUNNING")?LPQ_PRINTING:LPQ_QUEUED;
404                 buf->priority = 0;
405                 buf->time = time(NULL);
406                 fstrcpy(buf->fs_user,tok[5]);
407                 fstrcpy(buf->fs_file,tok[4]);
408         }
409
410         return True;
411 }
412
413 /****************************************************************************
414 parse a lpq line
415 here is an example of lpq output under hpux; note there's no space after -o !
416 $> lpstat -oljplus
417 ljplus-2153         user           priority 0  Jan 19 08:14 on ljplus
418       util.c                                  125697 bytes
419       server.c                                110712 bytes
420 ljplus-2154         user           priority 0  Jan 19 08:14 from client
421       (standard input)                          7551 bytes
422 ****************************************************************************/
423
424 static bool parse_lpq_hpux(char *line, print_queue_struct *buf, bool first)
425 {
426         /* must read two lines to process, therefore keep some values static */
427         static bool header_line_ok=False, base_prio_reset=False;
428         static fstring jobuser;
429         static int jobid;
430         static int jobprio;
431         static time_t jobtime;
432         static int jobstat=LPQ_QUEUED;
433         /* to store minimum priority to print, lpstat command should be invoked
434                 with -p option first, to work */
435         static int base_prio;
436         int count;
437         char htab = '\011';  
438         const char *cline = line;
439         fstring tok[12];
440
441         /* If a line begins with a horizontal TAB, it is a subline type */
442   
443         if (line[0] == htab) { /* subline */
444                 /* check if it contains the base priority */
445                 if (!strncmp(line,"\tfence priority : ",18)) {
446                         base_prio=atoi(&line[18]);
447                         DEBUG(4, ("fence priority set at %d\n", base_prio));
448                 }
449
450                 if (!header_line_ok) {
451                         return  False; /* incorrect header line */
452                 }
453
454                 /* handle the case of "(standard input)" as a filename */
455                 string_sub(line,"standard input","STDIN",0);
456                 all_string_sub(line,"(","\"",0);
457                 all_string_sub(line,")","\"",0);
458     
459                 for (count=0; count<2 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) {
460                         ;
461                 }
462                 /* we must get 2 tokens */
463                 if (count < 2) {
464                         return False;
465                 }
466     
467                 /* the 2nd column must be integer */
468                 if (!isdigit((int)*tok[1])) {
469                         return False;
470                 }
471     
472                 /* if the fname contains a space then use STDIN */
473                 if (strchr_m(tok[0],' ')) {
474                         fstrcpy(tok[0],"STDIN");
475                 }
476     
477                 buf->size = atoi(tok[1]);
478                 fstrcpy(buf->fs_file,tok[0]);
479     
480                 /* fill things from header line */
481                 buf->time = jobtime;
482                 buf->job = jobid;
483                 buf->status = jobstat;
484                 buf->priority = jobprio;
485                 fstrcpy(buf->fs_user,jobuser);
486     
487                 return True;
488         } else { /* header line */
489                 header_line_ok=False; /* reset it */
490                 if (first) {
491                         if (!base_prio_reset) {
492                                 base_prio=0; /* reset it */
493                                 base_prio_reset=True;
494                         }
495                 } else if (base_prio) {
496                         base_prio_reset=False;
497                 }
498     
499                 /* handle the dash in the job id */
500                 string_sub(line,"-"," ",0);
501     
502                 for (count=0; count<12 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) {
503                         ;
504                 }
505       
506                 /* we must get 8 tokens */
507                 if (count < 8) {
508                         return False;
509                 }
510     
511                 /* first token must be printer name (cannot check ?) */
512                 /* the 2nd, 5th & 7th column must be integer */
513                 if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[4]) || !isdigit((int)*tok[6])) {
514                         return False;
515                 }
516                 jobid = atoi(tok[1]);
517                 fstrcpy(jobuser,tok[2]);
518                 jobprio = atoi(tok[4]);
519     
520                 /* process time */
521                 jobtime=EntryTime(tok, 5, count, 8);
522                 if (jobprio < base_prio) {
523                         jobstat = LPQ_PAUSED;
524                         DEBUG (4, ("job %d is paused: prio %d < %d; jobstat=%d\n",
525                                 jobid, jobprio, base_prio, jobstat));
526                 } else {
527                         jobstat = LPQ_QUEUED;
528                         if ((count >8) && (((strequal(tok[8],"on")) ||
529                                         ((strequal(tok[8],"from")) && 
530                                         ((count > 10)&&(strequal(tok[10],"on"))))))) {
531                                 jobstat = LPQ_PRINTING;
532                         }
533                 }
534     
535                 header_line_ok=True; /* information is correct */
536                 return False; /* need subline info to include into queuelist */
537         }
538 }
539
540 /****************************************************************************
541 parse a lpstat line
542
543 here is an example of "lpstat -o dcslw" output under sysv
544
545 dcslw-896               tridge            4712   Dec 20 10:30:30 on dcslw
546 dcslw-897               tridge            4712   Dec 20 10:30:30 being held
547
548 ****************************************************************************/
549
550 static bool parse_lpq_sysv(char *line,print_queue_struct *buf,bool first)
551 {
552         fstring tok[9];
553         int count=0;
554         char *p;
555         const char *cline = line;
556
557         /* 
558          * Handle the dash in the job id, but make sure that we skip over
559          * the printer name in case we have a dash in that.
560          * Patch from Dom.Mitchell@palmerharvey.co.uk.
561          */
562
563         /*
564          * Move to the first space.
565          */
566         for (p = line ; !isspace(*p) && *p; p++) {
567                 ;
568         }
569
570         /*
571          * Back up until the last '-' character or
572          * start of line.
573          */
574         for (; (p >= line) && (*p != '-'); p--) {
575                 ;
576         }
577
578         if((p >= line) && (*p == '-')) {
579                 *p = ' ';
580         }
581
582         for (count=0; count<9 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) {
583                 ;
584         }
585
586         /* we must get 7 tokens */
587         if (count < 7) {
588                 return False;
589         }
590
591         /* the 2nd and 4th, 6th columns must be integer */
592         if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[3])) {
593                 return False;
594         }
595         if (!isdigit((int)*tok[5])) {
596                 return False;
597         }
598
599         /* if the user contains a ! then trim the first part of it */  
600         if ((p=strchr_m(tok[2],'!'))) {
601                 fstring tmp;
602                 fstrcpy(tmp,p+1);
603                 fstrcpy(tok[2],tmp);
604         }
605
606         buf->job = atoi(tok[1]);
607         buf->size = atoi(tok[3]);
608         if (count > 7 && strequal(tok[7],"on")) {
609                 buf->status = LPQ_PRINTING;
610         } else if (count > 8 && strequal(tok[7],"being") && strequal(tok[8],"held")) {
611                 buf->status = LPQ_PAUSED;
612         } else {
613                 buf->status = LPQ_QUEUED;
614         }
615         buf->priority = 0;
616         buf->time = EntryTime(tok, 4, count, 7);
617         fstrcpy(buf->fs_user,tok[2]);
618         fstrcpy(buf->fs_file,tok[2]);
619         return True;
620 }
621
622 /****************************************************************************
623 parse a lpq line
624
625 here is an example of lpq output under qnx
626 Spooler: /qnx/spooler, on node 1
627 Printer: txt        (ready) 
628 0000:     root  [job #1    ]   active 1146 bytes        /etc/profile
629 0001:     root  [job #2    ]    ready 2378 bytes        /etc/install
630 0002:     root  [job #3    ]    ready 1146 bytes        -- standard input --
631 ****************************************************************************/
632
633 static bool parse_lpq_qnx(char *line,print_queue_struct *buf,bool first)
634 {
635         fstring tok[7];
636         int count=0;
637         const char *cline = line;
638
639         DEBUG(4,("antes [%s]\n", line));
640
641         /* handle the case of "-- standard input --" as a filename */
642         string_sub(line,"standard input","STDIN",0);
643         DEBUG(4,("despues [%s]\n", line));
644         all_string_sub(line,"-- ","\"",0);
645         all_string_sub(line," --","\"",0);
646         DEBUG(4,("despues 1 [%s]\n", line));
647
648         string_sub(line,"[job #","",0);
649         string_sub(line,"]","",0);
650         DEBUG(4,("despues 2 [%s]\n", line));
651
652         for (count=0; count<7 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) {
653                 ;
654         }
655
656         /* we must get 7 tokens */
657         if (count < 7) {
658                 return False;
659         }
660
661         /* the 3rd and 5th columns must be integer */
662         if (!isdigit((int)*tok[2]) || !isdigit((int)*tok[4])) {
663                 return False;
664         }
665
666         /* only take the last part of the filename */
667         {
668                 fstring tmp;
669                 char *p = strrchr_m(tok[6],'/');
670                 if (p) {
671                         fstrcpy(tmp,p+1);
672                         fstrcpy(tok[6],tmp);
673                 }
674         }
675         
676         buf->job = atoi(tok[2]);
677         buf->size = atoi(tok[4]);
678         buf->status = strequal(tok[3],"active")?LPQ_PRINTING:LPQ_QUEUED;
679         buf->priority = 0;
680         buf->time = time(NULL);
681         fstrcpy(buf->fs_user,tok[1]);
682         fstrcpy(buf->fs_file,tok[6]);
683         return True;
684 }
685
686 /****************************************************************************
687   parse a lpq line for the plp printing system
688   Bertrand Wallrich <Bertrand.Wallrich@loria.fr>
689
690 redone by tridge. Here is a sample queue:
691
692 Local  Printer 'lp2' (fjall):
693   Printing (started at Jun 15 13:33:58, attempt 1).
694     Rank Owner       Pr Opt  Job Host        Files           Size     Date
695   active tridge      X  -    6   fjall       /etc/hosts      739      Jun 15 13:33
696      3rd tridge      X  -    7   fjall       /etc/hosts      739      Jun 15 13:33
697
698 ****************************************************************************/
699
700 static bool parse_lpq_plp(char *line,print_queue_struct *buf,bool first)
701 {
702         fstring tok[11];
703         int count=0;
704         const char *cline = line;
705
706         /* handle the case of "(standard input)" as a filename */
707         string_sub(line,"stdin","STDIN",0);
708         all_string_sub(line,"(","\"",0);
709         all_string_sub(line,")","\"",0);
710   
711         for (count=0; count<11 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) {
712                 ;
713         }
714
715         /* we must get 11 tokens */
716         if (count < 11) {
717                 return False;
718         }
719
720         /* the first must be "active" or begin with an integer */
721         if (strcmp(tok[0],"active") && !isdigit((int)tok[0][0])) {
722                 return False;
723         }
724
725         /* the 5th and 8th must be integer */
726         if (!isdigit((int)*tok[4]) || !isdigit((int)*tok[7])) {
727                 return False;
728         }
729
730         /* if the fname contains a space then use STDIN */
731         if (strchr_m(tok[6],' ')) {
732                 fstrcpy(tok[6],"STDIN");
733         }
734
735         /* only take the last part of the filename */
736         {
737                 fstring tmp;
738                 char *p = strrchr_m(tok[6],'/');
739                 if (p) {
740                         fstrcpy(tmp,p+1);
741                         fstrcpy(tok[6],tmp);
742                 }
743         }
744
745         buf->job = atoi(tok[4]);
746
747         buf->size = atoi(tok[7]);
748         if (strchr_m(tok[7],'K')) {
749                 buf->size *= 1024;
750         }
751         if (strchr_m(tok[7],'M')) {
752                 buf->size *= 1024*1024;
753         }
754
755         buf->status = strequal(tok[0],"active")?LPQ_PRINTING:LPQ_QUEUED;
756         buf->priority = 0;
757         buf->time = time(NULL);
758         fstrcpy(buf->fs_user,tok[1]);
759         fstrcpy(buf->fs_file,tok[6]);
760         return True;
761 }
762
763 /*******************************************************************
764 parse lpq on an NT system
765
766                          Windows 2000 LPD Server
767                               Printer \\10.0.0.2\NP17PCL (Paused)
768
769 Owner       Status         Jobname          Job-Id    Size   Pages  Priority
770 ----------------------------------------------------------------------------
771 root (9.99. Printing  /usr/lib/rhs/rhs-pr      3       625      0      1
772 root (9.99. Paused    /usr/lib/rhs/rhs-pr      4       625      0      1
773 jmcd        Waiting   Re: Samba Open Sour     26     32476      1      1
774
775 ********************************************************************/
776
777 static bool parse_lpq_nt(char *line,print_queue_struct *buf,bool first)
778 {
779 #define LPRNT_OWNSIZ 11
780 #define LPRNT_STATSIZ 9
781 #define LPRNT_JOBSIZ 19
782 #define LPRNT_IDSIZ 6
783 #define LPRNT_SIZSIZ 9
784         typedef struct {
785                 char owner[LPRNT_OWNSIZ];
786                 char space1;
787                 char status[LPRNT_STATSIZ];
788                 char space2;
789                 char jobname[LPRNT_JOBSIZ];
790                 char space3;
791                 char jobid[LPRNT_IDSIZ];
792                 char space4;
793                 char size[LPRNT_SIZSIZ];
794                 char terminator;
795         } nt_lpq_line;
796
797         nt_lpq_line parse_line;
798 #define LPRNT_PRINTING "Printing"
799 #define LPRNT_WAITING "Waiting"
800 #define LPRNT_PAUSED "Paused"
801
802         memset(&parse_line, '\0', sizeof(parse_line));
803         strncpy((char *) &parse_line, line, sizeof(parse_line) -1);
804
805         if (strlen((char *) &parse_line) != sizeof(parse_line) - 1) {
806                 return False;
807         }
808
809         /* Just want the first word in the owner field - the username */
810         if (strchr_m(parse_line.owner, ' ')) {
811                 *(strchr_m(parse_line.owner, ' ')) = '\0';
812         } else {
813                 parse_line.space1 = '\0';
814         }
815
816         /* Make sure we have an owner */
817         if (!strlen(parse_line.owner)) {
818                 return False;
819         }
820
821         /* Make sure the status is valid */
822         parse_line.space2 = '\0';
823         trim_char(parse_line.status, '\0', ' ');
824         if (!strequal(parse_line.status, LPRNT_PRINTING) &&
825                         !strequal(parse_line.status, LPRNT_PAUSED) &&
826                         !strequal(parse_line.status, LPRNT_WAITING)) {
827                 return False;
828         }
829   
830         parse_line.space3 = '\0';
831         trim_char(parse_line.jobname, '\0', ' ');
832
833         buf->job = atoi(parse_line.jobid);
834         buf->priority = 0;
835         buf->size = atoi(parse_line.size);
836         buf->time = time(NULL);
837         fstrcpy(buf->fs_user, parse_line.owner);
838         fstrcpy(buf->fs_file, parse_line.jobname);
839         if (strequal(parse_line.status, LPRNT_PRINTING)) {
840                 buf->status = LPQ_PRINTING;
841         } else if (strequal(parse_line.status, LPRNT_PAUSED)) {
842                 buf->status = LPQ_PAUSED;
843         } else {
844                 buf->status = LPQ_QUEUED;
845         }
846
847         return True;
848 }
849
850 /*******************************************************************
851 parse lpq on an OS2 system
852
853 JobID  File Name          Rank      Size        Status          Comment       
854 -----  ---------------    ------    --------    ------------    ------------  
855     3  Control                 1          68    Queued          root@psflinu  
856     4  /etc/motd               2       11666    Queued          root@psflinu  
857
858 ********************************************************************/
859
860 static bool parse_lpq_os2(char *line,print_queue_struct *buf,bool first)
861 {
862 #define LPROS2_IDSIZ 5
863 #define LPROS2_JOBSIZ 15
864 #define LPROS2_SIZSIZ 8
865 #define LPROS2_STATSIZ 12
866 #define LPROS2_OWNSIZ 12
867         typedef struct {
868                 char jobid[LPROS2_IDSIZ];
869                 char space1[2];
870                 char jobname[LPROS2_JOBSIZ];
871                 char space2[14];
872                 char size[LPROS2_SIZSIZ];
873                 char space3[4];
874                 char status[LPROS2_STATSIZ];
875                 char space4[4];
876                 char owner[LPROS2_OWNSIZ];
877                 char terminator;
878         } os2_lpq_line;
879
880         os2_lpq_line parse_line;
881 #define LPROS2_PRINTING "Printing"
882 #define LPROS2_WAITING "Queued"
883 #define LPROS2_PAUSED "Paused"
884
885         memset(&parse_line, '\0', sizeof(parse_line));
886         strncpy((char *) &parse_line, line, sizeof(parse_line) -1);
887
888         if (strlen((char *) &parse_line) != sizeof(parse_line) - 1) {
889                 return False;
890         }
891
892         /* Get the jobid */
893         buf->job = atoi(parse_line.jobid);
894
895         /* Get the job name */
896         parse_line.space2[0] = '\0';
897         trim_char(parse_line.jobname, '\0', ' ');
898         fstrcpy(buf->fs_file, parse_line.jobname);
899
900         buf->priority = 0;
901         buf->size = atoi(parse_line.size);
902         buf->time = time(NULL);
903
904         /* Make sure we have an owner */
905         if (!strlen(parse_line.owner)) {
906                 return False;
907         }
908
909         /* Make sure we have a valid status */
910         parse_line.space4[0] = '\0';
911         trim_char(parse_line.status, '\0', ' ');
912         if (!strequal(parse_line.status, LPROS2_PRINTING) &&
913                         !strequal(parse_line.status, LPROS2_PAUSED) &&
914                         !strequal(parse_line.status, LPROS2_WAITING)) {
915                 return False;
916         }
917
918         fstrcpy(buf->fs_user, parse_line.owner);
919         if (strequal(parse_line.status, LPROS2_PRINTING)) {
920                 buf->status = LPQ_PRINTING;
921         } else if (strequal(parse_line.status, LPROS2_PAUSED)) {
922                 buf->status = LPQ_PAUSED;
923         } else {
924                 buf->status = LPQ_QUEUED;
925         }
926
927         return True;
928 }
929
930 static const char *stat0_strings[] = { "enabled", "online", "idle", "no entries", "free", "ready", NULL };
931 static const char *stat1_strings[] = { "offline", "disabled", "down", "off", "waiting", "no daemon", NULL };
932 static const char *stat2_strings[] = { "jam", "paper", "error", "responding", "not accepting", "not running", "turned off", NULL };
933
934 #ifdef DEVELOPER
935
936 /****************************************************************************
937 parse a vlp line
938 ****************************************************************************/
939
940 static bool parse_lpq_vlp(char *line,print_queue_struct *buf,bool first)
941 {
942         int toknum = 0;
943         fstring tok;
944         const char *cline = line;
945
946         /* First line is printer status */
947
948         if (!isdigit(line[0])) return False;
949
950         /* Parse a print job entry */
951
952         while(next_token(&cline, tok, NULL, sizeof(fstring))) {
953                 switch (toknum) {
954                 case 0:
955                         buf->job = atoi(tok);
956                         break;
957                 case 1:
958                         buf->size = atoi(tok);
959                         break;
960                 case 2:
961                         buf->status = atoi(tok);
962                         break;
963                 case 3:
964                         buf->time = atoi(tok);
965                         break;
966                 case 4:
967                         fstrcpy(buf->fs_user, tok);
968                         break;
969                 case 5:
970                         fstrcpy(buf->fs_file, tok);
971                         break;
972                 }
973                 toknum++;
974         }
975
976         return True;
977 }
978
979 #endif /* DEVELOPER */
980
981 /****************************************************************************
982 parse a lpq line. Choose printing style
983 ****************************************************************************/
984
985 bool parse_lpq_entry(enum printing_types printing_type,char *line,
986                      print_queue_struct *buf,
987                      print_status_struct *status,bool first)
988 {
989         bool ret;
990
991         switch (printing_type) {
992                 case PRINT_SYSV:
993                         ret = parse_lpq_sysv(line,buf,first);
994                         break;
995                 case PRINT_AIX:      
996                         ret = parse_lpq_aix(line,buf,first);
997                         break;
998                 case PRINT_HPUX:
999                         ret = parse_lpq_hpux(line,buf,first);
1000                         break;
1001                 case PRINT_QNX:
1002                         ret = parse_lpq_qnx(line,buf,first);
1003                         break;
1004                 case PRINT_LPRNG:
1005                         ret = parse_lpq_lprng(line,buf,first);
1006                         break;
1007                 case PRINT_PLP:
1008                         ret = parse_lpq_plp(line,buf,first);
1009                         break;
1010                 case PRINT_LPRNT:
1011                         ret = parse_lpq_nt(line,buf,first);
1012                         break;
1013                 case PRINT_LPROS2:
1014                         ret = parse_lpq_os2(line,buf,first);
1015                         break;
1016 #ifdef DEVELOPER
1017                 case PRINT_VLP:
1018                 case PRINT_TEST:
1019                         ret = parse_lpq_vlp(line,buf,first);
1020                         break;
1021 #endif /* DEVELOPER */
1022                 default:
1023                         ret = parse_lpq_bsd(line,buf,first);
1024                         break;
1025         }
1026
1027         /* We don't want the newline in the status message. */
1028         {
1029                 char *p = strchr_m(line,'\n');
1030                 if (p) {
1031                         *p = 0;
1032                 }
1033         }
1034
1035         /* in the LPRNG case, we skip lines starting by a space.*/
1036         if (!ret && (printing_type==PRINT_LPRNG) ) {
1037                 if (line[0]==' ') {
1038                         return ret;
1039                 }
1040         }
1041
1042         if (status && !ret) {
1043                 /* a few simple checks to see if the line might be a
1044                         printer status line: 
1045                         handle them so that most severe condition is shown */
1046                 int i;
1047                 strlower_m(line);
1048       
1049                 switch (status->status) {
1050                         case LPSTAT_OK:
1051                                 for (i=0; stat0_strings[i]; i++) {
1052                                         if (strstr_m(line,stat0_strings[i])) {
1053                                                 fstrcpy(status->message,line);
1054                                                 status->status=LPSTAT_OK;
1055                                                 return ret;
1056                                         }
1057                                 }
1058                                 /* fallthrough */
1059                         case LPSTAT_STOPPED:
1060                                 for (i=0; stat1_strings[i]; i++) {
1061                                         if (strstr_m(line,stat1_strings[i])) {
1062                                                 fstrcpy(status->message,line);
1063                                                 status->status=LPSTAT_STOPPED;
1064                                                 return ret;
1065                                         }
1066                                 }
1067                                 /* fallthrough */
1068                         case LPSTAT_ERROR:
1069                                 for (i=0; stat2_strings[i]; i++) {
1070                                         if (strstr_m(line,stat2_strings[i])) {
1071                                                 fstrcpy(status->message,line);
1072                                                 status->status=LPSTAT_ERROR;
1073                                                 return ret;
1074                                         }
1075                                 }
1076                                 break;
1077                 }
1078         }
1079
1080         return ret;
1081 }