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