docs: Do not encourage unix passwords, and remove reference to password level
[Samba/bjacke.git] / source3 / printing / lpq_parse.c
blob91fc2ead7120e46898c130b18515d94195985691
1 /*
2 Unix SMB/CIFS implementation.
3 lpq parsing routines
4 Copyright (C) Andrew Tridgell 2000
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.
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.
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/>.
20 #include "includes.h"
21 #include "printing.h"
23 static const char *Months[13] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
24 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Err"};
27 /*******************************************************************
28 Process time fields
29 ********************************************************************/
31 static time_t EntryTime(char *tok[], int ptr, int count, int minimum)
33 time_t jobtime,jobtime1;
35 jobtime = time(NULL); /* default case: take current time */
36 if (count >= minimum) {
37 struct tm *t;
38 int i, day, hour, min, sec;
40 for (i=0; i<13; i++) {
41 if (!strncmp(tok[ptr], Months[i],3)) {
42 break; /* Find month */
46 if (i<12) {
47 fstring c;
48 t = localtime(&jobtime);
49 if (!t) {
50 return (time_t)-1;
52 day = atoi(tok[ptr+1]);
53 fstrcpy(c,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;
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 */
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;
82 return jobtime;
85 /****************************************************************************
86 parse a lpq line
88 here is an example of lpq output under bsd
90 Warning: no daemon present
91 Rank Owner Job Files Total Size
92 1st tridge 148 README 8096 bytes
94 here is an example of lpq output under osf/1
96 Warning: no daemon present
97 Rank Pri Owner Job Files Total Size
98 1st 0 tridge 148 README 8096 bytes
101 <allan@umich.edu> June 30, 1998.
102 Modified to handle file names with spaces, like the parse_lpq_lprng code
103 further below.
104 ****************************************************************************/
106 static bool parse_lpq_bsd(char *line,print_queue_struct *buf,bool first)
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 */
127 char *tok[MAXTOK];
128 int count = 0;
129 TALLOC_CTX *ctx = talloc_tos();
130 char *line2 = NULL;
131 char *saveptr;
133 line2 = talloc_strdup(ctx, line);
134 if (!line2) {
135 return false;
138 #ifdef OSF1
140 size_t length;
141 length = strlen(line2);
142 if (line2[length-3] == ':') {
143 return False;
146 #endif /* OSF1 */
148 /* FIXME: Use next_token_talloc rather than strtok! */
149 tok[0] = strtok_r(line2," \t", &saveptr);
150 count++;
152 while ((count < MAXTOK)
153 && ((tok[count] = strtok_r(NULL, " \t", &saveptr)) != NULL)) {
154 count++;
157 /* we must get at least NTOK tokens */
158 if (count < NTOK) {
159 return False;
162 /* the Job and Total columns must be integer */
163 if (!isdigit((int)*tok[JOBTOK]) || !isdigit((int)*tok[TOTALTOK])) {
164 return False;
167 buf->sysjob = atoi(tok[JOBTOK]);
168 buf->size = atoi(tok[TOTALTOK]);
169 buf->status = strequal(tok[RANKTOK],"active")?LPQ_PRINTING:LPQ_QUEUED;
170 buf->time = time(NULL);
171 fstrcpy(buf->fs_user,tok[USERTOK]);
172 fstrcpy(buf->fs_file,tok[FILETOK]);
174 if ((FILETOK + 1) != TOTALTOK) {
175 int i;
177 for (i = (FILETOK + 1); i < TOTALTOK; i++) {
178 /* FIXME: Using fstrcat rather than other means is a bit
179 * inefficient; this might be a problem for enormous queues with
180 * many fields. */
181 fstrcat(buf->fs_file, " ");
182 fstrcat(buf->fs_file, tok[i]);
184 /* Ensure null termination. */
185 buf->fs_file[sizeof(buf->fs_file)-1] = '\0';
188 #ifdef PRIOTOK
189 buf->priority = atoi(tok[PRIOTOK]);
190 #else
191 buf->priority = 1;
192 #endif
193 return True;
197 <magnus@hum.auc.dk>
198 LPRng_time modifies the current date by inserting the hour and minute from
199 the lpq output. The lpq time looks like "23:15:07"
201 <allan@umich.edu> June 30, 1998.
202 Modified to work with the re-written parse_lpq_lprng routine.
204 <J.P.M.v.Itegem@tue.nl> Dec 17,1999
205 Modified to work with lprng 3.16
206 With lprng 3.16 The lpq time looks like
207 "23:15:07"
208 "23:15:07.100"
209 "1999-12-16-23:15:07"
210 "1999-12-16-23:15:07.100"
213 static time_t LPRng_time(char *time_string)
215 time_t jobtime;
216 struct tm *t;
218 jobtime = time(NULL); /* default case: take current time */
219 t = localtime(&jobtime);
220 if (!t) {
221 return (time_t)-1;
224 if ( atoi(time_string) < 24 ){
225 t->tm_hour = atoi(time_string);
226 t->tm_min = atoi(time_string+3);
227 t->tm_sec = atoi(time_string+6);
228 } else {
229 t->tm_year = atoi(time_string)-1900;
230 t->tm_mon = atoi(time_string+5)-1;
231 t->tm_mday = atoi(time_string+8);
232 t->tm_hour = atoi(time_string+11);
233 t->tm_min = atoi(time_string+14);
234 t->tm_sec = atoi(time_string+17);
236 jobtime = mktime(t);
238 return jobtime;
241 /****************************************************************************
242 parse a lprng lpq line
243 <allan@umich.edu> June 30, 1998.
244 Re-wrote this to handle file names with spaces, multiple file names on one
245 lpq line, etc;
247 ****************************************************************************/
249 static bool parse_lpq_lprng(char *line,print_queue_struct *buf,bool first)
251 #define LPRNG_RANKTOK 0
252 #define LPRNG_USERTOK 1
253 #define LPRNG_PRIOTOK 2
254 #define LPRNG_JOBTOK 3
255 #define LPRNG_FILETOK 4
256 #define LPRNG_TOTALTOK (num_tok - 2)
257 #define LPRNG_TIMETOK (num_tok - 1)
258 #define LPRNG_NTOK 7
259 #define LPRNG_MAXTOK 128 /* PFMA just to keep us from running away. */
261 char *tokarr[LPRNG_MAXTOK];
262 const char *cptr;
263 char *ptr;
264 int num_tok = 0;
265 TALLOC_CTX *frame = talloc_stackframe();
267 cptr = line;
268 while((num_tok < LPRNG_MAXTOK) && next_token_talloc(frame, &cptr,
269 &tokarr[num_tok], " \t")) {
270 num_tok++;
273 /* We must get at least LPRNG_NTOK tokens. */
274 if (num_tok < LPRNG_NTOK) {
275 TALLOC_FREE(frame);
276 return False;
279 if (!isdigit((int)*tokarr[LPRNG_JOBTOK]) || !isdigit((int)*tokarr[LPRNG_TOTALTOK])) {
280 TALLOC_FREE(frame);
281 return False;
284 buf->sysjob = atoi(tokarr[LPRNG_JOBTOK]);
285 buf->size = atoi(tokarr[LPRNG_TOTALTOK]);
287 if (strequal(tokarr[LPRNG_RANKTOK],"active")) {
288 buf->status = LPQ_PRINTING;
289 } else if (strequal(tokarr[LPRNG_RANKTOK],"done")) {
290 buf->status = LPQ_PRINTED;
291 } else if (isdigit((int)*tokarr[LPRNG_RANKTOK])) {
292 buf->status = LPQ_QUEUED;
293 } else {
294 buf->status = LPQ_PAUSED;
297 buf->priority = *tokarr[LPRNG_PRIOTOK] -'A';
299 buf->time = LPRng_time(tokarr[LPRNG_TIMETOK]);
301 fstrcpy(buf->fs_user,tokarr[LPRNG_USERTOK]);
303 /* The '@hostname' prevents windows from displaying the printing icon
304 * for the current user on the taskbar. Plop in a null.
307 if ((ptr = strchr_m(buf->fs_user,'@')) != NULL) {
308 *ptr = '\0';
311 fstrcpy(buf->fs_file,tokarr[LPRNG_FILETOK]);
313 if ((LPRNG_FILETOK + 1) != LPRNG_TOTALTOK) {
314 int i;
316 for (i = (LPRNG_FILETOK + 1); i < LPRNG_TOTALTOK; i++) {
317 /* FIXME: Using fstrcat rather than other means is a bit
318 * inefficient; this might be a problem for enormous queues with
319 * many fields. */
320 fstrcat(buf->fs_file, " ");
321 fstrcat(buf->fs_file, tokarr[i]);
323 /* Ensure null termination. */
324 buf->fs_file[sizeof(buf->fs_file)-1] = '\0';
327 TALLOC_FREE(frame);
328 return True;
331 /*******************************************************************
332 parse lpq on an aix system
334 Queue Dev Status Job Files User PP % Blks Cp Rnk
335 ------- ----- --------- --- ------------------ ---------- ---- -- ----- --- ---
336 lazer lazer READY
337 lazer lazer RUNNING 537 6297doc.A kvintus@IE 0 10 2445 1 1
338 QUEUED 538 C.ps root@IEDVB 124 1 2
339 QUEUED 539 E.ps root@IEDVB 28 1 3
340 QUEUED 540 L.ps root@IEDVB 172 1 4
341 QUEUED 541 P.ps root@IEDVB 22 1 5
342 ********************************************************************/
344 static bool parse_lpq_aix(char *line,print_queue_struct *buf,bool first)
346 char *tok[11];
347 int count=0;
348 const char *cline = line;
349 TALLOC_CTX *frame = talloc_stackframe();
351 /* handle the case of "(standard input)" as a filename */
352 string_sub(line,"standard input","STDIN",0);
353 all_string_sub(line,"(","\"",0);
354 all_string_sub(line,")","\"",0);
356 for (count=0; count<10 &&
357 next_token_talloc(frame,&cline,&tok[count],NULL); count++) {
361 /* we must get 6 tokens */
362 if (count < 10) {
363 if ((count == 7) && ((strcmp(tok[0],"QUEUED") == 0) || (strcmp(tok[0],"HELD") == 0))) {
364 /* the 2nd and 5th columns must be integer */
365 if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[4])) {
366 TALLOC_FREE(frame);
367 return False;
369 buf->size = atoi(tok[4]) * 1024;
370 /* if the fname contains a space then use STDIN */
371 if (strchr_m(tok[2],' ')) {
372 tok[2] = talloc_strdup(frame,"STDIN");
373 if (!tok[2]) {
374 TALLOC_FREE(frame);
375 return false;
379 /* only take the last part of the filename */
381 char *p = strrchr_m(tok[2],'/');
382 if (p) {
383 tok[2] = p+1;
387 buf->sysjob = atoi(tok[1]);
388 buf->status = strequal(tok[0],"HELD")?LPQ_PAUSED:LPQ_QUEUED;
389 buf->priority = 0;
390 buf->time = time(NULL);
391 fstrcpy(buf->fs_user,tok[3]);
392 fstrcpy(buf->fs_file,tok[2]);
393 } else {
394 DEBUG(6,("parse_lpq_aix count=%d\n", count));
395 TALLOC_FREE(frame);
396 return False;
398 } else {
399 /* the 4th and 9th columns must be integer */
400 if (!isdigit((int)*tok[3]) || !isdigit((int)*tok[8])) {
401 TALLOC_FREE(frame);
402 return False;
405 buf->size = atoi(tok[8]) * 1024;
406 /* if the fname contains a space then use STDIN */
407 if (strchr_m(tok[4],' ')) {
408 tok[4] = talloc_strdup(frame,"STDIN");
409 if (!tok[4]) {
410 TALLOC_FREE(frame);
411 return false;
415 /* only take the last part of the filename */
417 char *p = strrchr_m(tok[4],'/');
418 if (p) {
419 tok[4] = p+1;
423 buf->sysjob = atoi(tok[3]);
424 buf->status = strequal(tok[2],"RUNNING")?LPQ_PRINTING:LPQ_QUEUED;
425 buf->priority = 0;
426 buf->time = time(NULL);
427 fstrcpy(buf->fs_user,tok[5]);
428 fstrcpy(buf->fs_file,tok[4]);
431 TALLOC_FREE(frame);
432 return True;
435 /****************************************************************************
436 parse a lpq line
437 here is an example of lpq output under hpux; note there's no space after -o !
438 $> lpstat -oljplus
439 ljplus-2153 user priority 0 Jan 19 08:14 on ljplus
440 util.c 125697 bytes
441 server.c 110712 bytes
442 ljplus-2154 user priority 0 Jan 19 08:14 from client
443 (standard input) 7551 bytes
444 ****************************************************************************/
446 static bool parse_lpq_hpux(char *line, print_queue_struct *buf, bool first)
448 /* must read two lines to process, therefore keep some values static */
449 static bool header_line_ok=False, base_prio_reset=False;
450 static char *jobuser;
451 static int jobid;
452 static int jobprio;
453 static time_t jobtime;
454 static int jobstat=LPQ_QUEUED;
455 /* to store minimum priority to print, lpstat command should be invoked
456 with -p option first, to work */
457 static int base_prio;
458 int count;
459 char htab = '\011';
460 const char *cline = line;
461 char *tok[12];
462 TALLOC_CTX *frame = talloc_stackframe();
464 /* If a line begins with a horizontal TAB, it is a subline type */
466 if (line[0] == htab) { /* subline */
467 /* check if it contains the base priority */
468 if (!strncmp(line,"\tfence priority : ",18)) {
469 base_prio=atoi(&line[18]);
470 DEBUG(4, ("fence priority set at %d\n", base_prio));
473 if (!header_line_ok) {
474 TALLOC_FREE(frame);
475 return False; /* incorrect header line */
478 /* handle the case of "(standard input)" as a filename */
479 string_sub(line,"standard input","STDIN",0);
480 all_string_sub(line,"(","\"",0);
481 all_string_sub(line,")","\"",0);
483 for (count=0; count<2 &&
484 next_token_talloc(frame, &cline, &tok[count],NULL);
485 count++) {
488 /* we must get 2 tokens */
489 if (count < 2) {
490 TALLOC_FREE(frame);
491 return False;
494 /* the 2nd column must be integer */
495 if (!isdigit((int)*tok[1])) {
496 TALLOC_FREE(frame);
497 return False;
500 /* if the fname contains a space then use STDIN */
501 if (strchr_m(tok[0],' ')) {
502 tok[0] = talloc_strdup(frame, "STDIN");
503 if (!tok[0]) {
504 TALLOC_FREE(frame);
505 return false;
509 buf->size = atoi(tok[1]);
510 fstrcpy(buf->fs_file,tok[0]);
512 /* fill things from header line */
513 buf->time = jobtime;
514 buf->sysjob = jobid;
515 buf->status = jobstat;
516 buf->priority = jobprio;
517 if (jobuser) {
518 fstrcpy(buf->fs_user,jobuser);
519 } else {
520 buf->fs_user[0] = '\0';
523 TALLOC_FREE(frame);
524 return True;
525 } else { /* header line */
526 header_line_ok=False; /* reset it */
527 if (first) {
528 if (!base_prio_reset) {
529 base_prio=0; /* reset it */
530 base_prio_reset=True;
532 } else if (base_prio) {
533 base_prio_reset=False;
536 /* handle the dash in the job id */
537 string_sub(line,"-"," ",0);
539 for (count=0; count<12 &&
540 next_token_talloc(frame, &cline, &tok[count],NULL);
541 count++) {
545 /* we must get 8 tokens */
546 if (count < 8) {
547 TALLOC_FREE(frame);
548 return False;
551 /* first token must be printer name (cannot check ?) */
552 /* the 2nd, 5th & 7th column must be integer */
553 if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[4]) || !isdigit((int)*tok[6])) {
554 TALLOC_FREE(frame);
555 return False;
557 jobid = atoi(tok[1]);
558 SAFE_FREE(jobuser);
559 jobuser = SMB_STRDUP(tok[2]);
560 jobprio = atoi(tok[4]);
562 /* process time */
563 jobtime=EntryTime(tok, 5, count, 8);
564 if (jobprio < base_prio) {
565 jobstat = LPQ_PAUSED;
566 DEBUG (4, ("job %d is paused: prio %d < %d; jobstat=%d\n",
567 jobid, jobprio, base_prio, jobstat));
568 } else {
569 jobstat = LPQ_QUEUED;
570 if ((count >8) && (((strequal(tok[8],"on")) ||
571 ((strequal(tok[8],"from")) &&
572 ((count > 10)&&(strequal(tok[10],"on"))))))) {
573 jobstat = LPQ_PRINTING;
577 header_line_ok=True; /* information is correct */
578 TALLOC_FREE(frame);
579 return False; /* need subline info to include into queuelist */
583 /****************************************************************************
584 parse a lpstat line
586 here is an example of "lpstat -o dcslw" output under sysv
588 dcslw-896 tridge 4712 Dec 20 10:30:30 on dcslw
589 dcslw-897 tridge 4712 Dec 20 10:30:30 being held
591 ****************************************************************************/
593 static bool parse_lpq_sysv(char *line,print_queue_struct *buf,bool first)
595 char *tok[9];
596 int count=0;
597 char *p;
598 const char *cline = line;
599 TALLOC_CTX *frame = NULL;
602 * Handle the dash in the job id, but make sure that we skip over
603 * the printer name in case we have a dash in that.
604 * Patch from Dom.Mitchell@palmerharvey.co.uk.
608 * Move to the first space.
610 for (p = line ; !isspace(*p) && *p; p++) {
615 * Back up until the last '-' character or
616 * start of line.
618 for (; (p >= line) && (*p != '-'); p--) {
622 if((p >= line) && (*p == '-')) {
623 *p = ' ';
626 frame = talloc_stackframe();
627 for (count=0; count<9 &&
628 next_token_talloc(frame, &cline, &tok[count],NULL);
629 count++) {
633 /* we must get 7 tokens */
634 if (count < 7) {
635 TALLOC_FREE(frame);
636 return False;
639 /* the 2nd and 4th, 6th columns must be integer */
640 if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[3])) {
641 TALLOC_FREE(frame);
642 return False;
644 if (!isdigit((int)*tok[5])) {
645 TALLOC_FREE(frame);
646 return False;
649 /* if the user contains a ! then trim the first part of it */
650 if ((p=strchr_m(tok[2],'!'))) {
651 tok[2] = p+1;
654 buf->sysjob = atoi(tok[1]);
655 buf->size = atoi(tok[3]);
656 if (count > 7 && strequal(tok[7],"on")) {
657 buf->status = LPQ_PRINTING;
658 } else if (count > 8 && strequal(tok[7],"being") && strequal(tok[8],"held")) {
659 buf->status = LPQ_PAUSED;
660 } else {
661 buf->status = LPQ_QUEUED;
663 buf->priority = 0;
664 buf->time = EntryTime(tok, 4, count, 7);
665 fstrcpy(buf->fs_user,tok[2]);
666 fstrcpy(buf->fs_file,tok[2]);
667 TALLOC_FREE(frame);
668 return True;
671 /****************************************************************************
672 parse a lpq line
674 here is an example of lpq output under qnx
675 Spooler: /qnx/spooler, on node 1
676 Printer: txt (ready)
677 0000: root [job #1 ] active 1146 bytes /etc/profile
678 0001: root [job #2 ] ready 2378 bytes /etc/install
679 0002: root [job #3 ] ready 1146 bytes -- standard input --
680 ****************************************************************************/
682 static bool parse_lpq_qnx(char *line,print_queue_struct *buf,bool first)
684 char *tok[7];
685 int count=0;
686 const char *cline = line;
687 TALLOC_CTX *frame = NULL;
689 DEBUG(4,("antes [%s]\n", line));
691 /* handle the case of "-- standard input --" as a filename */
692 string_sub(line,"standard input","STDIN",0);
693 DEBUG(4,("despues [%s]\n", line));
694 all_string_sub(line,"-- ","\"",0);
695 all_string_sub(line," --","\"",0);
696 DEBUG(4,("despues 1 [%s]\n", line));
698 string_sub(line,"[job #","",0);
699 string_sub(line,"]","",0);
700 DEBUG(4,("despues 2 [%s]\n", line));
702 frame = talloc_stackframe();
703 for (count=0; count<7 &&
704 next_token_talloc(frame,&cline,&tok[count],NULL);
705 count++) {
709 /* we must get 7 tokens */
710 if (count < 7) {
711 TALLOC_FREE(frame);
712 return False;
715 /* the 3rd and 5th columns must be integer */
716 if (!isdigit((int)*tok[2]) || !isdigit((int)*tok[4])) {
717 TALLOC_FREE(frame);
718 return False;
721 /* only take the last part of the filename */
723 char *p = strrchr_m(tok[6],'/');
724 if (p) {
725 tok[6] = p+1;
729 buf->sysjob = atoi(tok[2]);
730 buf->size = atoi(tok[4]);
731 buf->status = strequal(tok[3],"active")?LPQ_PRINTING:LPQ_QUEUED;
732 buf->priority = 0;
733 buf->time = time(NULL);
734 fstrcpy(buf->fs_user,tok[1]);
735 fstrcpy(buf->fs_file,tok[6]);
736 TALLOC_FREE(frame);
737 return True;
740 /****************************************************************************
741 parse a lpq line for the plp printing system
742 Bertrand Wallrich <Bertrand.Wallrich@loria.fr>
744 redone by tridge. Here is a sample queue:
746 Local Printer 'lp2' (fjall):
747 Printing (started at Jun 15 13:33:58, attempt 1).
748 Rank Owner Pr Opt Job Host Files Size Date
749 active tridge X - 6 fjall /etc/hosts 739 Jun 15 13:33
750 3rd tridge X - 7 fjall /etc/hosts 739 Jun 15 13:33
752 ****************************************************************************/
754 static bool parse_lpq_plp(char *line,print_queue_struct *buf,bool first)
756 char *tok[11];
757 int count=0;
758 const char *cline = line;
759 TALLOC_CTX *frame = talloc_stackframe();
761 /* handle the case of "(standard input)" as a filename */
762 string_sub(line,"stdin","STDIN",0);
763 all_string_sub(line,"(","\"",0);
764 all_string_sub(line,")","\"",0);
766 for (count=0; count<11 &&
767 next_token_talloc(frame,&cline,&tok[count],NULL);
768 count++) {
772 /* we must get 11 tokens */
773 if (count < 11) {
774 TALLOC_FREE(frame);
775 return False;
778 /* the first must be "active" or begin with an integer */
779 if (strcmp(tok[0],"active") && !isdigit((int)tok[0][0])) {
780 TALLOC_FREE(frame);
781 return False;
784 /* the 5th and 8th must be integer */
785 if (!isdigit((int)*tok[4]) || !isdigit((int)*tok[7])) {
786 TALLOC_FREE(frame);
787 return False;
790 /* if the fname contains a space then use STDIN */
791 if (strchr_m(tok[6],' ')) {
792 tok[6] = talloc_strdup(frame, "STDIN");
793 if (!tok[6]) {
794 TALLOC_FREE(frame);
795 return false;
799 /* only take the last part of the filename */
801 fstring tmp;
802 char *p = strrchr_m(tok[6],'/');
803 if (p) {
804 size_t len = strlen(tok[6])+1;
805 fstrcpy(tmp,p+1);
806 strlcpy(tok[6],tmp, len);
810 buf->sysjob = atoi(tok[4]);
812 buf->size = atoi(tok[7]);
813 if (strchr_m(tok[7],'K')) {
814 buf->size *= 1024;
816 if (strchr_m(tok[7],'M')) {
817 buf->size *= 1024*1024;
820 buf->status = strequal(tok[0],"active")?LPQ_PRINTING:LPQ_QUEUED;
821 buf->priority = 0;
822 buf->time = time(NULL);
823 fstrcpy(buf->fs_user,tok[1]);
824 fstrcpy(buf->fs_file,tok[6]);
825 TALLOC_FREE(frame);
826 return True;
829 /*******************************************************************
830 parse lpq on an NT system
832 Windows 2000 LPD Server
833 Printer \\10.0.0.2\NP17PCL (Paused)
835 Owner Status Jobname Job-Id Size Pages Priority
836 ----------------------------------------------------------------------------
837 root (9.99. Printing /usr/lib/rhs/rhs-pr 3 625 0 1
838 root (9.99. Paused /usr/lib/rhs/rhs-pr 4 625 0 1
839 jmcd Waiting Re: Samba Open Sour 26 32476 1 1
841 ********************************************************************/
843 static bool parse_lpq_nt(char *line,print_queue_struct *buf,bool first)
845 #define LPRNT_OWNSIZ 11
846 #define LPRNT_STATSIZ 9
847 #define LPRNT_JOBSIZ 19
848 #define LPRNT_IDSIZ 6
849 #define LPRNT_SIZSIZ 9
850 typedef struct {
851 char owner[LPRNT_OWNSIZ];
852 char space1;
853 char status[LPRNT_STATSIZ];
854 char space2;
855 char jobname[LPRNT_JOBSIZ];
856 char space3;
857 char jobid[LPRNT_IDSIZ];
858 char space4;
859 char size[LPRNT_SIZSIZ];
860 char terminator;
861 } nt_lpq_line;
863 char parse_line_char[sizeof(nt_lpq_line)];
864 nt_lpq_line *parse_line = (nt_lpq_line *)parse_line_char;
865 #define LPRNT_PRINTING "Printing"
866 #define LPRNT_WAITING "Waiting"
867 #define LPRNT_PAUSED "Paused"
869 memset(parse_line_char, '\0', sizeof(parse_line_char));
870 strncpy(parse_line_char, line, sizeof(parse_line_char) -1);
872 if (strlen(parse_line_char) != sizeof(parse_line_char) - 1) {
873 return False;
876 /* Just want the first word in the owner field - the username */
877 if (strchr_m(parse_line->owner, ' ')) {
878 *(strchr_m(parse_line->owner, ' ')) = '\0';
879 } else {
880 parse_line->space1 = '\0';
883 /* Make sure we have an owner */
884 if (!strlen(parse_line->owner)) {
885 return False;
888 /* Make sure the status is valid */
889 parse_line->space2 = '\0';
890 trim_char(parse_line->status, '\0', ' ');
891 if (!strequal(parse_line->status, LPRNT_PRINTING) &&
892 !strequal(parse_line->status, LPRNT_PAUSED) &&
893 !strequal(parse_line->status, LPRNT_WAITING)) {
894 return False;
897 parse_line->space3 = '\0';
898 trim_char(parse_line->jobname, '\0', ' ');
900 buf->sysjob = atoi(parse_line->jobid);
901 buf->priority = 0;
902 buf->size = atoi(parse_line->size);
903 buf->time = time(NULL);
904 fstrcpy(buf->fs_user, parse_line->owner);
905 fstrcpy(buf->fs_file, parse_line->jobname);
906 if (strequal(parse_line->status, LPRNT_PRINTING)) {
907 buf->status = LPQ_PRINTING;
908 } else if (strequal(parse_line->status, LPRNT_PAUSED)) {
909 buf->status = LPQ_PAUSED;
910 } else {
911 buf->status = LPQ_QUEUED;
914 return True;
917 /*******************************************************************
918 parse lpq on an OS2 system
920 JobID File Name Rank Size Status Comment
921 ----- --------------- ------ -------- ------------ ------------
922 3 Control 1 68 Queued root@psflinu
923 4 /etc/motd 2 11666 Queued root@psflinu
925 ********************************************************************/
927 static bool parse_lpq_os2(char *line,print_queue_struct *buf,bool first)
929 #define LPROS2_IDSIZ 5
930 #define LPROS2_JOBSIZ 15
931 #define LPROS2_SIZSIZ 8
932 #define LPROS2_STATSIZ 12
933 #define LPROS2_OWNSIZ 12
934 typedef struct {
935 char jobid[LPROS2_IDSIZ];
936 char space1[2];
937 char jobname[LPROS2_JOBSIZ];
938 char space2[14];
939 char size[LPROS2_SIZSIZ];
940 char space3[4];
941 char status[LPROS2_STATSIZ];
942 char space4[4];
943 char owner[LPROS2_OWNSIZ];
944 char terminator;
945 } os2_lpq_line;
947 char parse_line_char[sizeof(os2_lpq_line)];
948 os2_lpq_line *parse_line = (os2_lpq_line *)parse_line_char;
949 #define LPROS2_PRINTING "Printing"
950 #define LPROS2_WAITING "Queued"
951 #define LPROS2_PAUSED "Paused"
953 memset(parse_line_char, '\0', sizeof(parse_line_char));
954 strncpy(parse_line_char, line, sizeof(parse_line_char) -1);
956 if (strlen(parse_line_char) != sizeof(parse_line_char) - 1) {
957 return False;
960 /* Get the jobid */
961 buf->sysjob = atoi(parse_line->jobid);
963 /* Get the job name */
964 parse_line->space2[0] = '\0';
965 trim_char(parse_line->jobname, '\0', ' ');
966 fstrcpy(buf->fs_file, parse_line->jobname);
968 buf->priority = 0;
969 buf->size = atoi(parse_line->size);
970 buf->time = time(NULL);
972 /* Make sure we have an owner */
973 if (!strlen(parse_line->owner)) {
974 return False;
977 /* Make sure we have a valid status */
978 parse_line->space4[0] = '\0';
979 trim_char(parse_line->status, '\0', ' ');
980 if (!strequal(parse_line->status, LPROS2_PRINTING) &&
981 !strequal(parse_line->status, LPROS2_PAUSED) &&
982 !strequal(parse_line->status, LPROS2_WAITING)) {
983 return False;
986 fstrcpy(buf->fs_user, parse_line->owner);
987 if (strequal(parse_line->status, LPROS2_PRINTING)) {
988 buf->status = LPQ_PRINTING;
989 } else if (strequal(parse_line->status, LPROS2_PAUSED)) {
990 buf->status = LPQ_PAUSED;
991 } else {
992 buf->status = LPQ_QUEUED;
995 return True;
998 static const char *stat0_strings[] = { "enabled", "online", "idle", "no entries", "free", "ready", NULL };
999 static const char *stat1_strings[] = { "offline", "disabled", "down", "off", "waiting", "no daemon", NULL };
1000 static const char *stat2_strings[] = { "jam", "paper", "error", "responding", "not accepting", "not running", "turned off", NULL };
1002 #ifdef DEVELOPER
1004 /****************************************************************************
1005 parse a vlp line
1006 ****************************************************************************/
1008 static bool parse_lpq_vlp(char *line,print_queue_struct *buf,bool first)
1010 int toknum = 0;
1011 char *tok;
1012 TALLOC_CTX *frame = talloc_stackframe();
1013 const char *cline = line;
1015 /* First line is printer status */
1017 if (!isdigit(line[0])) {
1018 TALLOC_FREE(frame);
1019 return False;
1022 /* Parse a print job entry */
1024 while(next_token_talloc(frame, &cline, &tok, NULL)) {
1025 switch (toknum) {
1026 case 0:
1027 buf->sysjob = atoi(tok);
1028 break;
1029 case 1:
1030 buf->size = atoi(tok);
1031 break;
1032 case 2:
1033 buf->status = atoi(tok);
1034 break;
1035 case 3:
1036 buf->time = atoi(tok);
1037 break;
1038 case 4:
1039 fstrcpy(buf->fs_user, tok);
1040 break;
1041 case 5:
1042 fstrcpy(buf->fs_file, tok);
1043 break;
1045 toknum++;
1048 TALLOC_FREE(frame);
1049 return True;
1052 #endif /* DEVELOPER */
1054 /****************************************************************************
1055 parse a lpq line. Choose printing style
1056 ****************************************************************************/
1058 bool parse_lpq_entry(enum printing_types printing_type,char *line,
1059 print_queue_struct *buf,
1060 print_status_struct *status,bool first)
1062 bool ret;
1064 switch (printing_type) {
1065 case PRINT_SYSV:
1066 ret = parse_lpq_sysv(line,buf,first);
1067 break;
1068 case PRINT_AIX:
1069 ret = parse_lpq_aix(line,buf,first);
1070 break;
1071 case PRINT_HPUX:
1072 ret = parse_lpq_hpux(line,buf,first);
1073 break;
1074 case PRINT_QNX:
1075 ret = parse_lpq_qnx(line,buf,first);
1076 break;
1077 case PRINT_LPRNG:
1078 ret = parse_lpq_lprng(line,buf,first);
1079 break;
1080 case PRINT_PLP:
1081 ret = parse_lpq_plp(line,buf,first);
1082 break;
1083 case PRINT_LPRNT:
1084 ret = parse_lpq_nt(line,buf,first);
1085 break;
1086 case PRINT_LPROS2:
1087 ret = parse_lpq_os2(line,buf,first);
1088 break;
1089 #ifdef DEVELOPER
1090 case PRINT_VLP:
1091 case PRINT_TEST:
1092 ret = parse_lpq_vlp(line,buf,first);
1093 break;
1094 #endif /* DEVELOPER */
1095 default:
1096 ret = parse_lpq_bsd(line,buf,first);
1097 break;
1100 /* We don't want the newline in the status message. */
1102 char *p = strchr_m(line,'\n');
1103 if (p) {
1104 *p = 0;
1108 /* in the LPRNG case, we skip lines starting by a space.*/
1109 if (!ret && (printing_type==PRINT_LPRNG) ) {
1110 if (line[0]==' ') {
1111 return ret;
1115 if (status && !ret) {
1116 /* a few simple checks to see if the line might be a
1117 printer status line:
1118 handle them so that most severe condition is shown */
1119 int i;
1120 if (!strlower_m(line)) {
1121 return false;
1124 switch (status->status) {
1125 case LPSTAT_OK:
1126 for (i=0; stat0_strings[i]; i++) {
1127 if (strstr_m(line,stat0_strings[i])) {
1128 fstrcpy(status->message,line);
1129 status->status=LPSTAT_OK;
1130 return ret;
1133 /* fallthrough */
1134 case LPSTAT_STOPPED:
1135 for (i=0; stat1_strings[i]; i++) {
1136 if (strstr_m(line,stat1_strings[i])) {
1137 fstrcpy(status->message,line);
1138 status->status=LPSTAT_STOPPED;
1139 return ret;
1142 /* fallthrough */
1143 case LPSTAT_ERROR:
1144 for (i=0; stat2_strings[i]; i++) {
1145 if (strstr_m(line,stat2_strings[i])) {
1146 fstrcpy(status->message,line);
1147 status->status=LPSTAT_ERROR;
1148 return ret;
1151 break;
1155 return ret;