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