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