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