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