Remove next_token - all uses must now be next_token_talloc.
[metze/samba/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
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 fstring 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                 fstrcpy(buf->fs_user,jobuser);
515
516                 TALLOC_FREE(frame);
517                 return True;
518         } else { /* header line */
519                 header_line_ok=False; /* reset it */
520                 if (first) {
521                         if (!base_prio_reset) {
522                                 base_prio=0; /* reset it */
523                                 base_prio_reset=True;
524                         }
525                 } else if (base_prio) {
526                         base_prio_reset=False;
527                 }
528
529                 /* handle the dash in the job id */
530                 string_sub(line,"-"," ",0);
531
532                 for (count=0; count<12 &&
533                                 next_token_talloc(frame, &cline, &tok[count],NULL);
534                                 count++) {
535                         ;
536                 }
537
538                 /* we must get 8 tokens */
539                 if (count < 8) {
540                         TALLOC_FREE(frame);
541                         return False;
542                 }
543
544                 /* first token must be printer name (cannot check ?) */
545                 /* the 2nd, 5th & 7th column must be integer */
546                 if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[4]) || !isdigit((int)*tok[6])) {
547                         TALLOC_FREE(frame);
548                         return False;
549                 }
550                 jobid = atoi(tok[1]);
551                 fstrcpy(jobuser,tok[2]);
552                 jobprio = atoi(tok[4]);
553
554                 /* process time */
555                 jobtime=EntryTime(tok, 5, count, 8);
556                 if (jobprio < base_prio) {
557                         jobstat = LPQ_PAUSED;
558                         DEBUG (4, ("job %d is paused: prio %d < %d; jobstat=%d\n",
559                                 jobid, jobprio, base_prio, jobstat));
560                 } else {
561                         jobstat = LPQ_QUEUED;
562                         if ((count >8) && (((strequal(tok[8],"on")) ||
563                                         ((strequal(tok[8],"from")) && 
564                                         ((count > 10)&&(strequal(tok[10],"on"))))))) {
565                                 jobstat = LPQ_PRINTING;
566                         }
567                 }
568
569                 header_line_ok=True; /* information is correct */
570                 TALLOC_FREE(frame);
571                 return False; /* need subline info to include into queuelist */
572         }
573 }
574
575 /****************************************************************************
576 parse a lpstat line
577
578 here is an example of "lpstat -o dcslw" output under sysv
579
580 dcslw-896               tridge            4712   Dec 20 10:30:30 on dcslw
581 dcslw-897               tridge            4712   Dec 20 10:30:30 being held
582
583 ****************************************************************************/
584
585 static bool parse_lpq_sysv(char *line,print_queue_struct *buf,bool first)
586 {
587         char *tok[9];
588         int count=0;
589         char *p;
590         const char *cline = line;
591         TALLOC_CTX *frame = NULL;
592
593         /*
594          * Handle the dash in the job id, but make sure that we skip over
595          * the printer name in case we have a dash in that.
596          * Patch from Dom.Mitchell@palmerharvey.co.uk.
597          */
598
599         /*
600          * Move to the first space.
601          */
602         for (p = line ; !isspace(*p) && *p; p++) {
603                 ;
604         }
605
606         /*
607          * Back up until the last '-' character or
608          * start of line.
609          */
610         for (; (p >= line) && (*p != '-'); p--) {
611                 ;
612         }
613
614         if((p >= line) && (*p == '-')) {
615                 *p = ' ';
616         }
617
618         frame = talloc_stackframe();
619         for (count=0; count<9 &&
620                         next_token_talloc(frame, &cline, &tok[count],NULL);
621                         count++) {
622                 ;
623         }
624
625         /* we must get 7 tokens */
626         if (count < 7) {
627                 TALLOC_FREE(frame);
628                 return False;
629         }
630
631         /* the 2nd and 4th, 6th columns must be integer */
632         if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[3])) {
633                 TALLOC_FREE(frame);
634                 return False;
635         }
636         if (!isdigit((int)*tok[5])) {
637                 TALLOC_FREE(frame);
638                 return False;
639         }
640
641         /* if the user contains a ! then trim the first part of it */
642         if ((p=strchr_m(tok[2],'!'))) {
643                 tok[2] = p+1;
644         }
645
646         buf->job = atoi(tok[1]);
647         buf->size = atoi(tok[3]);
648         if (count > 7 && strequal(tok[7],"on")) {
649                 buf->status = LPQ_PRINTING;
650         } else if (count > 8 && strequal(tok[7],"being") && strequal(tok[8],"held")) {
651                 buf->status = LPQ_PAUSED;
652         } else {
653                 buf->status = LPQ_QUEUED;
654         }
655         buf->priority = 0;
656         buf->time = EntryTime(tok, 4, count, 7);
657         fstrcpy(buf->fs_user,tok[2]);
658         fstrcpy(buf->fs_file,tok[2]);
659         TALLOC_FREE(frame);
660         return True;
661 }
662
663 /****************************************************************************
664 parse a lpq line
665
666 here is an example of lpq output under qnx
667 Spooler: /qnx/spooler, on node 1
668 Printer: txt        (ready)
669 0000:     root  [job #1    ]   active 1146 bytes        /etc/profile
670 0001:     root  [job #2    ]    ready 2378 bytes        /etc/install
671 0002:     root  [job #3    ]    ready 1146 bytes        -- standard input --
672 ****************************************************************************/
673
674 static bool parse_lpq_qnx(char *line,print_queue_struct *buf,bool first)
675 {
676         char *tok[7];
677         int count=0;
678         const char *cline = line;
679         TALLOC_CTX *frame = NULL;
680
681         DEBUG(4,("antes [%s]\n", line));
682
683         /* handle the case of "-- standard input --" as a filename */
684         string_sub(line,"standard input","STDIN",0);
685         DEBUG(4,("despues [%s]\n", line));
686         all_string_sub(line,"-- ","\"",0);
687         all_string_sub(line," --","\"",0);
688         DEBUG(4,("despues 1 [%s]\n", line));
689
690         string_sub(line,"[job #","",0);
691         string_sub(line,"]","",0);
692         DEBUG(4,("despues 2 [%s]\n", line));
693
694         frame = talloc_stackframe();
695         for (count=0; count<7 &&
696                         next_token_talloc(frame,&cline,&tok[count],NULL);
697                         count++) {
698                 ;
699         }
700
701         /* we must get 7 tokens */
702         if (count < 7) {
703                 TALLOC_FREE(frame);
704                 return False;
705         }
706
707         /* the 3rd and 5th columns must be integer */
708         if (!isdigit((int)*tok[2]) || !isdigit((int)*tok[4])) {
709                 TALLOC_FREE(frame);
710                 return False;
711         }
712
713         /* only take the last part of the filename */
714         {
715                 char *p = strrchr_m(tok[6],'/');
716                 if (p) {
717                         tok[6] = p+1;
718                 }
719         }
720
721         buf->job = atoi(tok[2]);
722         buf->size = atoi(tok[4]);
723         buf->status = strequal(tok[3],"active")?LPQ_PRINTING:LPQ_QUEUED;
724         buf->priority = 0;
725         buf->time = time(NULL);
726         fstrcpy(buf->fs_user,tok[1]);
727         fstrcpy(buf->fs_file,tok[6]);
728         TALLOC_FREE(frame);
729         return True;
730 }
731
732 /****************************************************************************
733   parse a lpq line for the plp printing system
734   Bertrand Wallrich <Bertrand.Wallrich@loria.fr>
735
736 redone by tridge. Here is a sample queue:
737
738 Local  Printer 'lp2' (fjall):
739   Printing (started at Jun 15 13:33:58, attempt 1).
740     Rank Owner       Pr Opt  Job Host        Files           Size     Date
741   active tridge      X  -    6   fjall       /etc/hosts      739      Jun 15 13:33
742      3rd tridge      X  -    7   fjall       /etc/hosts      739      Jun 15 13:33
743
744 ****************************************************************************/
745
746 static bool parse_lpq_plp(char *line,print_queue_struct *buf,bool first)
747 {
748         char *tok[11];
749         int count=0;
750         const char *cline = line;
751         TALLOC_CTX *frame = talloc_stackframe();
752
753         /* handle the case of "(standard input)" as a filename */
754         string_sub(line,"stdin","STDIN",0);
755         all_string_sub(line,"(","\"",0);
756         all_string_sub(line,")","\"",0);
757
758         for (count=0; count<11 &&
759                         next_token_talloc(frame,&cline,&tok[count],NULL);
760                         count++) {
761                 ;
762         }
763
764         /* we must get 11 tokens */
765         if (count < 11) {
766                 TALLOC_FREE(frame);
767                 return False;
768         }
769
770         /* the first must be "active" or begin with an integer */
771         if (strcmp(tok[0],"active") && !isdigit((int)tok[0][0])) {
772                 TALLOC_FREE(frame);
773                 return False;
774         }
775
776         /* the 5th and 8th must be integer */
777         if (!isdigit((int)*tok[4]) || !isdigit((int)*tok[7])) {
778                 TALLOC_FREE(frame);
779                 return False;
780         }
781
782         /* if the fname contains a space then use STDIN */
783         if (strchr_m(tok[6],' ')) {
784                 tok[6] = talloc_strdup(frame, "STDIN");
785                 if (!tok[6]) {
786                         TALLOC_FREE(frame);
787                         return false;
788                 }
789         }
790
791         /* only take the last part of the filename */
792         {
793                 fstring tmp;
794                 char *p = strrchr_m(tok[6],'/');
795                 if (p) {
796                         fstrcpy(tmp,p+1);
797                         fstrcpy(tok[6],tmp);
798                 }
799         }
800
801         buf->job = atoi(tok[4]);
802
803         buf->size = atoi(tok[7]);
804         if (strchr_m(tok[7],'K')) {
805                 buf->size *= 1024;
806         }
807         if (strchr_m(tok[7],'M')) {
808                 buf->size *= 1024*1024;
809         }
810
811         buf->status = strequal(tok[0],"active")?LPQ_PRINTING:LPQ_QUEUED;
812         buf->priority = 0;
813         buf->time = time(NULL);
814         fstrcpy(buf->fs_user,tok[1]);
815         fstrcpy(buf->fs_file,tok[6]);
816         TALLOC_FREE(frame);
817         return True;
818 }
819
820 /*******************************************************************
821 parse lpq on an NT system
822
823                          Windows 2000 LPD Server
824                               Printer \\10.0.0.2\NP17PCL (Paused)
825
826 Owner       Status         Jobname          Job-Id    Size   Pages  Priority
827 ----------------------------------------------------------------------------
828 root (9.99. Printing  /usr/lib/rhs/rhs-pr      3       625      0      1
829 root (9.99. Paused    /usr/lib/rhs/rhs-pr      4       625      0      1
830 jmcd        Waiting   Re: Samba Open Sour     26     32476      1      1
831
832 ********************************************************************/
833
834 static bool parse_lpq_nt(char *line,print_queue_struct *buf,bool first)
835 {
836 #define LPRNT_OWNSIZ 11
837 #define LPRNT_STATSIZ 9
838 #define LPRNT_JOBSIZ 19
839 #define LPRNT_IDSIZ 6
840 #define LPRNT_SIZSIZ 9
841         typedef struct {
842                 char owner[LPRNT_OWNSIZ];
843                 char space1;
844                 char status[LPRNT_STATSIZ];
845                 char space2;
846                 char jobname[LPRNT_JOBSIZ];
847                 char space3;
848                 char jobid[LPRNT_IDSIZ];
849                 char space4;
850                 char size[LPRNT_SIZSIZ];
851                 char terminator;
852         } nt_lpq_line;
853
854         nt_lpq_line parse_line;
855 #define LPRNT_PRINTING "Printing"
856 #define LPRNT_WAITING "Waiting"
857 #define LPRNT_PAUSED "Paused"
858
859         memset(&parse_line, '\0', sizeof(parse_line));
860         strncpy((char *) &parse_line, line, sizeof(parse_line) -1);
861
862         if (strlen((char *) &parse_line) != sizeof(parse_line) - 1) {
863                 return False;
864         }
865
866         /* Just want the first word in the owner field - the username */
867         if (strchr_m(parse_line.owner, ' ')) {
868                 *(strchr_m(parse_line.owner, ' ')) = '\0';
869         } else {
870                 parse_line.space1 = '\0';
871         }
872
873         /* Make sure we have an owner */
874         if (!strlen(parse_line.owner)) {
875                 return False;
876         }
877
878         /* Make sure the status is valid */
879         parse_line.space2 = '\0';
880         trim_char(parse_line.status, '\0', ' ');
881         if (!strequal(parse_line.status, LPRNT_PRINTING) &&
882                         !strequal(parse_line.status, LPRNT_PAUSED) &&
883                         !strequal(parse_line.status, LPRNT_WAITING)) {
884                 return False;
885         }
886   
887         parse_line.space3 = '\0';
888         trim_char(parse_line.jobname, '\0', ' ');
889
890         buf->job = atoi(parse_line.jobid);
891         buf->priority = 0;
892         buf->size = atoi(parse_line.size);
893         buf->time = time(NULL);
894         fstrcpy(buf->fs_user, parse_line.owner);
895         fstrcpy(buf->fs_file, parse_line.jobname);
896         if (strequal(parse_line.status, LPRNT_PRINTING)) {
897                 buf->status = LPQ_PRINTING;
898         } else if (strequal(parse_line.status, LPRNT_PAUSED)) {
899                 buf->status = LPQ_PAUSED;
900         } else {
901                 buf->status = LPQ_QUEUED;
902         }
903
904         return True;
905 }
906
907 /*******************************************************************
908 parse lpq on an OS2 system
909
910 JobID  File Name          Rank      Size        Status          Comment       
911 -----  ---------------    ------    --------    ------------    ------------  
912     3  Control                 1          68    Queued          root@psflinu  
913     4  /etc/motd               2       11666    Queued          root@psflinu  
914
915 ********************************************************************/
916
917 static bool parse_lpq_os2(char *line,print_queue_struct *buf,bool first)
918 {
919 #define LPROS2_IDSIZ 5
920 #define LPROS2_JOBSIZ 15
921 #define LPROS2_SIZSIZ 8
922 #define LPROS2_STATSIZ 12
923 #define LPROS2_OWNSIZ 12
924         typedef struct {
925                 char jobid[LPROS2_IDSIZ];
926                 char space1[2];
927                 char jobname[LPROS2_JOBSIZ];
928                 char space2[14];
929                 char size[LPROS2_SIZSIZ];
930                 char space3[4];
931                 char status[LPROS2_STATSIZ];
932                 char space4[4];
933                 char owner[LPROS2_OWNSIZ];
934                 char terminator;
935         } os2_lpq_line;
936
937         os2_lpq_line parse_line;
938 #define LPROS2_PRINTING "Printing"
939 #define LPROS2_WAITING "Queued"
940 #define LPROS2_PAUSED "Paused"
941
942         memset(&parse_line, '\0', sizeof(parse_line));
943         strncpy((char *) &parse_line, line, sizeof(parse_line) -1);
944
945         if (strlen((char *) &parse_line) != sizeof(parse_line) - 1) {
946                 return False;
947         }
948
949         /* Get the jobid */
950         buf->job = atoi(parse_line.jobid);
951
952         /* Get the job name */
953         parse_line.space2[0] = '\0';
954         trim_char(parse_line.jobname, '\0', ' ');
955         fstrcpy(buf->fs_file, parse_line.jobname);
956
957         buf->priority = 0;
958         buf->size = atoi(parse_line.size);
959         buf->time = time(NULL);
960
961         /* Make sure we have an owner */
962         if (!strlen(parse_line.owner)) {
963                 return False;
964         }
965
966         /* Make sure we have a valid status */
967         parse_line.space4[0] = '\0';
968         trim_char(parse_line.status, '\0', ' ');
969         if (!strequal(parse_line.status, LPROS2_PRINTING) &&
970                         !strequal(parse_line.status, LPROS2_PAUSED) &&
971                         !strequal(parse_line.status, LPROS2_WAITING)) {
972                 return False;
973         }
974
975         fstrcpy(buf->fs_user, parse_line.owner);
976         if (strequal(parse_line.status, LPROS2_PRINTING)) {
977                 buf->status = LPQ_PRINTING;
978         } else if (strequal(parse_line.status, LPROS2_PAUSED)) {
979                 buf->status = LPQ_PAUSED;
980         } else {
981                 buf->status = LPQ_QUEUED;
982         }
983
984         return True;
985 }
986
987 static const char *stat0_strings[] = { "enabled", "online", "idle", "no entries", "free", "ready", NULL };
988 static const char *stat1_strings[] = { "offline", "disabled", "down", "off", "waiting", "no daemon", NULL };
989 static const char *stat2_strings[] = { "jam", "paper", "error", "responding", "not accepting", "not running", "turned off", NULL };
990
991 #ifdef DEVELOPER
992
993 /****************************************************************************
994 parse a vlp line
995 ****************************************************************************/
996
997 static bool parse_lpq_vlp(char *line,print_queue_struct *buf,bool first)
998 {
999         int toknum = 0;
1000         char *tok;
1001         TALLOC_CTX *frame = talloc_stackframe();
1002         const char *cline = line;
1003
1004         /* First line is printer status */
1005
1006         if (!isdigit(line[0])) {
1007                 TALLOC_FREE(frame);
1008                 return False;
1009         }
1010
1011         /* Parse a print job entry */
1012
1013         while(next_token_talloc(frame, &cline, &tok, NULL)) {
1014                 switch (toknum) {
1015                 case 0:
1016                         buf->job = atoi(tok);
1017                         break;
1018                 case 1:
1019                         buf->size = atoi(tok);
1020                         break;
1021                 case 2:
1022                         buf->status = atoi(tok);
1023                         break;
1024                 case 3:
1025                         buf->time = atoi(tok);
1026                         break;
1027                 case 4:
1028                         fstrcpy(buf->fs_user, tok);
1029                         break;
1030                 case 5:
1031                         fstrcpy(buf->fs_file, tok);
1032                         break;
1033                 }
1034                 toknum++;
1035         }
1036
1037         TALLOC_FREE(frame);
1038         return True;
1039 }
1040
1041 #endif /* DEVELOPER */
1042
1043 /****************************************************************************
1044 parse a lpq line. Choose printing style
1045 ****************************************************************************/
1046
1047 bool parse_lpq_entry(enum printing_types printing_type,char *line,
1048                      print_queue_struct *buf,
1049                      print_status_struct *status,bool first)
1050 {
1051         bool ret;
1052
1053         switch (printing_type) {
1054                 case PRINT_SYSV:
1055                         ret = parse_lpq_sysv(line,buf,first);
1056                         break;
1057                 case PRINT_AIX:      
1058                         ret = parse_lpq_aix(line,buf,first);
1059                         break;
1060                 case PRINT_HPUX:
1061                         ret = parse_lpq_hpux(line,buf,first);
1062                         break;
1063                 case PRINT_QNX:
1064                         ret = parse_lpq_qnx(line,buf,first);
1065                         break;
1066                 case PRINT_LPRNG:
1067                         ret = parse_lpq_lprng(line,buf,first);
1068                         break;
1069                 case PRINT_PLP:
1070                         ret = parse_lpq_plp(line,buf,first);
1071                         break;
1072                 case PRINT_LPRNT:
1073                         ret = parse_lpq_nt(line,buf,first);
1074                         break;
1075                 case PRINT_LPROS2:
1076                         ret = parse_lpq_os2(line,buf,first);
1077                         break;
1078 #ifdef DEVELOPER
1079                 case PRINT_VLP:
1080                 case PRINT_TEST:
1081                         ret = parse_lpq_vlp(line,buf,first);
1082                         break;
1083 #endif /* DEVELOPER */
1084                 default:
1085                         ret = parse_lpq_bsd(line,buf,first);
1086                         break;
1087         }
1088
1089         /* We don't want the newline in the status message. */
1090         {
1091                 char *p = strchr_m(line,'\n');
1092                 if (p) {
1093                         *p = 0;
1094                 }
1095         }
1096
1097         /* in the LPRNG case, we skip lines starting by a space.*/
1098         if (!ret && (printing_type==PRINT_LPRNG) ) {
1099                 if (line[0]==' ') {
1100                         return ret;
1101                 }
1102         }
1103
1104         if (status && !ret) {
1105                 /* a few simple checks to see if the line might be a
1106                         printer status line: 
1107                         handle them so that most severe condition is shown */
1108                 int i;
1109                 strlower_m(line);
1110       
1111                 switch (status->status) {
1112                         case LPSTAT_OK:
1113                                 for (i=0; stat0_strings[i]; i++) {
1114                                         if (strstr_m(line,stat0_strings[i])) {
1115                                                 fstrcpy(status->message,line);
1116                                                 status->status=LPSTAT_OK;
1117                                                 return ret;
1118                                         }
1119                                 }
1120                                 /* fallthrough */
1121                         case LPSTAT_STOPPED:
1122                                 for (i=0; stat1_strings[i]; i++) {
1123                                         if (strstr_m(line,stat1_strings[i])) {
1124                                                 fstrcpy(status->message,line);
1125                                                 status->status=LPSTAT_STOPPED;
1126                                                 return ret;
1127                                         }
1128                                 }
1129                                 /* fallthrough */
1130                         case LPSTAT_ERROR:
1131                                 for (i=0; stat2_strings[i]; i++) {
1132                                         if (strstr_m(line,stat2_strings[i])) {
1133                                                 fstrcpy(status->message,line);
1134                                                 status->status=LPSTAT_ERROR;
1135                                                 return ret;
1136                                         }
1137                                 }
1138                                 break;
1139                 }
1140         }
1141
1142         return ret;
1143 }