This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[tprouty/samba.git] / source / printing / lpq_parse.c
index 12f12fd33c604fb6fa2ec5db0ece9012f2d11dca..b2f45ad36627570bc52b8d969018f42b1ae4bb29 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 3.0
+   Unix SMB/CIFS implementation.
    lpq parsing routines
    Copyright (C) Andrew Tridgell 2000
    
@@ -21,7 +20,7 @@
 
 #include "includes.h"
 
-static char *Months[13] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+static const char *Months[13] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Err"};
 
 
@@ -127,6 +126,7 @@ static BOOL parse_lpq_bsd(char *line,print_queue_struct *buf,BOOL first)
   }
 #endif /* OSF1 */
 
+  /* FIXME: Use next_token rather than strtok! */
   tok[0] = strtok(line2," \t");
   count++;
 
@@ -145,25 +145,21 @@ static BOOL parse_lpq_bsd(char *line,print_queue_struct *buf,BOOL first)
   buf->size = atoi(tok[TOTALTOK]);
   buf->status = strequal(tok[RANKTOK],"active")?LPQ_PRINTING:LPQ_QUEUED;
   buf->time = time(NULL);
-  StrnCpy(buf->user,tok[USERTOK],sizeof(buf->user)-1);
-  StrnCpy(buf->file,tok[FILETOK],sizeof(buf->file)-1);
+  fstrcpy(buf->fs_user,tok[USERTOK]);
+  fstrcpy(buf->fs_file,tok[FILETOK]);
 
   if ((FILETOK + 1) != TOTALTOK) {
-    int bufsize;
     int i;
 
-    bufsize = sizeof(buf->file) - strlen(buf->file) - 1;
-
     for (i = (FILETOK + 1); i < TOTALTOK; i++) {
-      safe_strcat(buf->file," ",bufsize);
-      safe_strcat(buf->file,tok[i],bufsize - 1);
-      bufsize = sizeof(buf->file) - strlen(buf->file) - 1;
-      if (bufsize <= 0) {
-        break;
-      }
+        /* FIXME: Using fstrcat rather than other means is a bit
+         * inefficient; this might be a problem for enormous queues with
+         * many fields. */
+         fstrcat(buf->fs_file, " ");
+         fstrcat(buf->fs_file, tok[i]);
     }
     /* Ensure null termination. */
-    buf->file[sizeof(buf->file)-1] = '\0';
+    fstrterminate(buf->fs_file);
   }
 
 #ifdef PRIOTOK
@@ -236,12 +232,11 @@ static BOOL parse_lpq_lprng(char *line,print_queue_struct *buf,BOOL first)
 #define        LPRNG_MAXTOK    128 /* PFMA just to keep us from running away. */
 
   fstring tokarr[LPRNG_MAXTOK];
-  char *cptr;
+  const char *cptr;
+  char *ptr;
   int  num_tok = 0;
-  pstring line2;
 
-  pstrcpy(line2,line);
-  cptr = line2;
+  cptr = line;
   while(next_token( &cptr, tokarr[num_tok], " \t", sizeof(fstring)) && (num_tok < LPRNG_MAXTOK))
     num_tok++;
 
@@ -259,6 +254,8 @@ static BOOL parse_lpq_lprng(char *line,print_queue_struct *buf,BOOL first)
 
   if (strequal(tokarr[LPRNG_RANKTOK],"active")) {
     buf->status = LPQ_PRINTING;
+  } else if (strequal(tokarr[LPRNG_RANKTOK],"done")) {
+    buf->status = LPQ_PRINTED;
   } else if (isdigit((int)*tokarr[LPRNG_RANKTOK])) {
     buf->status = LPQ_QUEUED;
   } else {
@@ -269,34 +266,30 @@ static BOOL parse_lpq_lprng(char *line,print_queue_struct *buf,BOOL first)
 
   buf->time = LPRng_time(tokarr[LPRNG_TIMETOK]);
 
-  StrnCpy(buf->user,tokarr[LPRNG_USERTOK],sizeof(buf->user)-1);
+  fstrcpy(buf->fs_user,tokarr[LPRNG_USERTOK]);
 
   /* The '@hostname' prevents windows from displaying the printing icon
    * for the current user on the taskbar.  Plop in a null.
    */
 
-  if ((cptr = strchr_m(buf->user,'@')) != NULL) {
-    *cptr = '\0';
+  if ((ptr = strchr_m(buf->fs_user,'@')) != NULL) {
+    *ptr = '\0';
   }
 
-  StrnCpy(buf->file,tokarr[LPRNG_FILETOK],sizeof(buf->file)-1);
+  fstrcpy(buf->fs_file,tokarr[LPRNG_FILETOK]);
 
   if ((LPRNG_FILETOK + 1) != LPRNG_TOTALTOK) {
-    int bufsize;
     int i;
 
-    bufsize = sizeof(buf->file) - strlen(buf->file) - 1;
-
     for (i = (LPRNG_FILETOK + 1); i < LPRNG_TOTALTOK; i++) {
-      safe_strcat(buf->file," ",bufsize);
-      safe_strcat(buf->file,tokarr[i],bufsize - 1);
-      bufsize = sizeof(buf->file) - strlen(buf->file) - 1;
-      if (bufsize <= 0) {
-        break;
-      }
+      /* FIXME: Using fstrcat rather than other means is a bit
+       * inefficient; this might be a problem for enormous queues with
+       * many fields. */
+      fstrcat(buf->fs_file, " ");
+      fstrcat(buf->fs_file, tokarr[i]);
     }
     /* Ensure null termination. */
-    buf->file[sizeof(buf->file)-1] = '\0';
+    fstrterminate(buf->fs_file);
   }
 
   return(True);
@@ -320,15 +313,16 @@ static BOOL parse_lpq_aix(char *line,print_queue_struct *buf,BOOL first)
 {
   fstring tok[11];
   int count=0;
+  const char *cline = line;
 
   /* handle the case of "(standard input)" as a filename */
-  pstring_sub(line,"standard input","STDIN");
+  string_sub(line,"standard input","STDIN",0);
   all_string_sub(line,"(","\"",0);
   all_string_sub(line,")","\"",0);
 
   for (count=0; 
        count<10 && 
-              next_token(&line,tok[count],NULL, sizeof(tok[count])); 
+              next_token(&cline,tok[count],NULL, sizeof(tok[count])); 
        count++) ;
 
   /* we must get 6 tokens */
@@ -359,8 +353,8 @@ static BOOL parse_lpq_aix(char *line,print_queue_struct *buf,BOOL first)
           buf->status = strequal(tok[0],"HELD")?LPQ_PAUSED:LPQ_QUEUED;
          buf->priority = 0;
           buf->time = time(NULL);
-          StrnCpy(buf->user,tok[3],sizeof(buf->user)-1);
-          StrnCpy(buf->file,tok[2],sizeof(buf->file)-1);
+          fstrcpy(buf->fs_user,tok[3]);
+         fstrcpy(buf->fs_file,tok[2]);
       }
       else
       {
@@ -393,8 +387,8 @@ static BOOL parse_lpq_aix(char *line,print_queue_struct *buf,BOOL first)
       buf->status = strequal(tok[2],"RUNNING")?LPQ_PRINTING:LPQ_QUEUED;
       buf->priority = 0;
       buf->time = time(NULL);
-      StrnCpy(buf->user,tok[5],sizeof(buf->user)-1);
-      StrnCpy(buf->file,tok[4],sizeof(buf->file)-1);
+      fstrcpy(buf->fs_user,tok[5]);
+      fstrcpy(buf->fs_file,tok[4]);
   }
 
 
@@ -412,7 +406,7 @@ ljplus-2153         user           priority 0  Jan 19 08:14 on ljplus
 ljplus-2154         user           priority 0  Jan 19 08:14 from client
       (standard input)                          7551 bytes
 ****************************************************************************/
-static BOOL parse_lpq_hpux(char * line, print_queue_struct *buf, BOOL first)
+static BOOL parse_lpq_hpux(char *line, print_queue_struct *buf, BOOL first)
 {
   /* must read two lines to process, therefore keep some values static */
   static BOOL header_line_ok=False, base_prio_reset=False;
@@ -424,9 +418,9 @@ static BOOL parse_lpq_hpux(char * line, print_queue_struct *buf, BOOL first)
   /* to store minimum priority to print, lpstat command should be invoked
      with -p option first, to work */
   static int base_prio;
   int count;
   char htab = '\011';  
+  const char *cline = line;
   fstring tok[12];
 
   /* If a line begins with a horizontal TAB, it is a subline type */
@@ -439,11 +433,11 @@ static BOOL parse_lpq_hpux(char * line, print_queue_struct *buf, BOOL first)
     }
     if (!header_line_ok) return (False); /* incorrect header line */
     /* handle the case of "(standard input)" as a filename */
-    pstring_sub(line,"standard input","STDIN");
+    string_sub(line,"standard input","STDIN",0);
     all_string_sub(line,"(","\"",0);
     all_string_sub(line,")","\"",0);
     
-    for (count=0; count<2 && next_token(&line,tok[count],NULL,sizeof(tok[count])); count++) ;
+    for (count=0; count<2 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) ;
     /* we must get 2 tokens */
     if (count < 2) return(False);
     
@@ -455,14 +449,14 @@ static BOOL parse_lpq_hpux(char * line, print_queue_struct *buf, BOOL first)
       fstrcpy(tok[0],"STDIN");
     
     buf->size = atoi(tok[1]);
-    StrnCpy(buf->file,tok[0],sizeof(buf->file)-1);
+    fstrcpy(buf->fs_file,tok[0]);
     
     /* fill things from header line */
     buf->time = jobtime;
     buf->job = jobid;
     buf->status = jobstat;
     buf->priority = jobprio;
-    StrnCpy(buf->user,jobuser,sizeof(buf->user)-1);
+    fstrcpy(buf->fs_user,jobuser);
     
     return(True);
   }
@@ -477,9 +471,9 @@ static BOOL parse_lpq_hpux(char * line, print_queue_struct *buf, BOOL first)
     else if (base_prio) base_prio_reset=False;
     
     /* handle the dash in the job id */
-    pstring_sub(line,"-"," ");
+    string_sub(line,"-"," ",0);
     
-    for (count=0; count<12 && next_token(&line,tok[count],NULL,sizeof(tok[count])); count++) ;
+    for (count=0; count<12 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) ;
       
     /* we must get 8 tokens */
     if (count < 8) return(False);
@@ -488,7 +482,7 @@ static BOOL parse_lpq_hpux(char * line, print_queue_struct *buf, BOOL first)
     /* the 2nd, 5th & 7th column must be integer */
     if (!isdigit((int)*tok[1]) || !isdigit((int)*tok[4]) || !isdigit((int)*tok[6])) return(False);
     jobid = atoi(tok[1]);
-    StrnCpy(jobuser,tok[2],sizeof(buf->user)-1);
+    fstrcpy(jobuser,tok[2]);
     jobprio = atoi(tok[4]);
     
     /* process time */
@@ -525,6 +519,7 @@ static BOOL parse_lpq_sysv(char *line,print_queue_struct *buf,BOOL first)
   fstring tok[9];
   int count=0;
   char *p;
+  const char *cline = line;
 
   /* 
    * Handle the dash in the job id, but make sure that we skip over
@@ -548,7 +543,7 @@ static BOOL parse_lpq_sysv(char *line,print_queue_struct *buf,BOOL first)
   if((p >= line) && (*p == '-'))
     *p = ' ';
 
-  for (count=0; count<9 && next_token(&line,tok[count],NULL,sizeof(tok[count])); count++)
+  for (count=0; count<9 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++)
     ;
 
   /* we must get 7 tokens */
@@ -578,8 +573,8 @@ static BOOL parse_lpq_sysv(char *line,print_queue_struct *buf,BOOL first)
     buf->status = LPQ_QUEUED;
   buf->priority = 0;
   buf->time = EntryTime(tok, 4, count, 7);
-  StrnCpy(buf->user,tok[2],sizeof(buf->user)-1);
-  StrnCpy(buf->file,tok[2],sizeof(buf->file)-1);
+  fstrcpy(buf->fs_user,tok[2]);
+  fstrcpy(buf->fs_file,tok[2]);
   return(True);
 }
 
@@ -597,23 +592,22 @@ static BOOL parse_lpq_qnx(char *line,print_queue_struct *buf,BOOL first)
 {
   fstring tok[7];
   int count=0;
+  const char *cline = line;
 
   DEBUG(4,("antes [%s]\n", line));
 
   /* handle the case of "-- standard input --" as a filename */
-  pstring_sub(line,"standard input","STDIN");
+  string_sub(line,"standard input","STDIN",0);
   DEBUG(4,("despues [%s]\n", line));
   all_string_sub(line,"-- ","\"",0);
   all_string_sub(line," --","\"",0);
   DEBUG(4,("despues 1 [%s]\n", line));
 
-  pstring_sub(line,"[job #","");
-  pstring_sub(line,"]","");
+  string_sub(line,"[job #","",0);
+  string_sub(line,"]","",0);
   DEBUG(4,("despues 2 [%s]\n", line));
 
-  
-  
-  for (count=0; count<7 && next_token(&line,tok[count],NULL,sizeof(tok[count])); count++) ;
+  for (count=0; count<7 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) ;
 
   /* we must get 7 tokens */
   if (count < 7)
@@ -639,8 +633,8 @@ static BOOL parse_lpq_qnx(char *line,print_queue_struct *buf,BOOL first)
   buf->status = strequal(tok[3],"active")?LPQ_PRINTING:LPQ_QUEUED;
   buf->priority = 0;
   buf->time = time(NULL);
-  StrnCpy(buf->user,tok[1],sizeof(buf->user)-1);
-  StrnCpy(buf->file,tok[6],sizeof(buf->file)-1);
+  fstrcpy(buf->fs_user,tok[1]);
+  fstrcpy(buf->fs_file,tok[6]);
   return(True);
 }
 
@@ -662,13 +656,14 @@ static BOOL parse_lpq_plp(char *line,print_queue_struct *buf,BOOL first)
 {
   fstring tok[11];
   int count=0;
+  const char *cline = line;
 
   /* handle the case of "(standard input)" as a filename */
-  pstring_sub(line,"stdin","STDIN");
+  string_sub(line,"stdin","STDIN",0);
   all_string_sub(line,"(","\"",0);
   all_string_sub(line,")","\"",0);
   
-  for (count=0; count<11 && next_token(&line,tok[count],NULL,sizeof(tok[count])); count++) ;
+  for (count=0; count<11 && next_token(&cline,tok[count],NULL,sizeof(tok[count])); count++) ;
 
   /* we must get 11 tokens */
   if (count < 11)
@@ -709,87 +704,8 @@ static BOOL parse_lpq_plp(char *line,print_queue_struct *buf,BOOL first)
   buf->status = strequal(tok[0],"active")?LPQ_PRINTING:LPQ_QUEUED;
   buf->priority = 0;
   buf->time = time(NULL);
-  StrnCpy(buf->user,tok[1],sizeof(buf->user)-1);
-  StrnCpy(buf->file,tok[6],sizeof(buf->file)-1);
-  return(True);
-}
-
-/****************************************************************************
-parse a qstat line
-
-here is an example of "qstat -l -d qms" output under softq
-
-Queue qms: 2 jobs; daemon active (313); enabled; accepting;
- job-ID   submission-time     pri     size owner      title 
-205980: H 98/03/09 13:04:05     0    15733 stephenf   chap1.ps
-206086:>  98/03/12 17:24:40     0      659 chris      -
-206087:   98/03/12 17:24:45     0     4876 chris      -
-Total:      21268 bytes in queue
-
-
-****************************************************************************/
-static BOOL parse_lpq_softq(char *line,print_queue_struct *buf,BOOL first)
-{
-  fstring tok[10];
-  int count=0;
-
-  /* mung all the ":"s to spaces*/
-  pstring_sub(line,":"," ");
-  
-  for (count=0; count<10 && next_token(&line,tok[count],NULL,sizeof(tok[count])); count++) ;
-
-  /* we must get 9 tokens */
-  if (count < 9)
-    return(False);
-
-  /* the 1st and 7th columns must be integer */
-  if (!isdigit((int)*tok[0]) || !isdigit((int)*tok[6]))  return(False);
-  /* if the 2nd column is either '>' or 'H' then the 7th and 8th must be
-   * integer, else it's the 6th and 7th that must be
-   */
-  if (*tok[1] == 'H' || *tok[1] == '>')
-    {
-      if (!isdigit((int)*tok[7]))
-        return(False);
-      buf->status = *tok[1] == '>' ? LPQ_PRINTING : LPQ_PAUSED;
-      count = 1;
-    }
-  else
-    {
-      if (!isdigit((int)*tok[5]))
-        return(False);
-      buf->status = LPQ_QUEUED;
-      count = 0;
-    }
-       
-
-  buf->job = atoi(tok[0]);
-  buf->size = atoi(tok[count+6]);
-  buf->priority = atoi(tok[count+5]);
-  StrnCpy(buf->user,tok[count+7],sizeof(buf->user)-1);
-  StrnCpy(buf->file,tok[count+8],sizeof(buf->file)-1);
-  buf->time = time(NULL);              /* default case: take current time */
-  {
-    time_t jobtime;
-    struct tm *t;
-
-    t = localtime(&buf->time);
-    t->tm_mday = atoi(tok[count+2]+6);
-    t->tm_mon  = atoi(tok[count+2]+3);
-    switch (*tok[count+2])
-    {
-    case 7: case 8: case 9: t->tm_year = atoi(tok[count+2]); break;
-    default:                t->tm_year = atoi(tok[count+2]); break;
-    }
-
-    t->tm_hour = atoi(tok[count+3]);
-    t->tm_min = atoi(tok[count+4]);
-    t->tm_sec = atoi(tok[count+5]);
-    jobtime = mktime(t);
-    if (jobtime != (time_t)-1)
-      buf->time = jobtime; 
-  }
-
+  fstrcpy(buf->fs_user,tok[1]);
+  fstrcpy(buf->fs_file,tok[6]);
   return(True);
 }
 
@@ -863,8 +779,8 @@ static BOOL parse_lpq_nt(char *line,print_queue_struct *buf,BOOL first)
   buf->priority = 0;
   buf->size = atoi(parse_line.size);
   buf->time = time(NULL);
-  StrnCpy(buf->user, parse_line.owner, sizeof(buf->user)-1);
-  StrnCpy(buf->file, parse_line.jobname, sizeof(buf->file)-1);
+  fstrcpy(buf->fs_user, parse_line.owner);
+  fstrcpy(buf->fs_file, parse_line.jobname);
   if (strequal(parse_line.status, LPRNT_PRINTING))
     buf->status = LPQ_PRINTING;
   else if (strequal(parse_line.status, LPRNT_PAUSED))
@@ -922,7 +838,7 @@ static BOOL parse_lpq_os2(char *line,print_queue_struct *buf,BOOL first)
   /* Get the job name */
   parse_line.space2[0] = '\0';
   trim_string(parse_line.jobname, NULL, " ");
-  StrnCpy(buf->file, parse_line.jobname, sizeof(buf->file)-1);
+  fstrcpy(buf->fs_file, parse_line.jobname);
 
   buf->priority = 0;
   buf->size = atoi(parse_line.size);
@@ -940,7 +856,7 @@ static BOOL parse_lpq_os2(char *line,print_queue_struct *buf,BOOL first)
       !strequal(parse_line.status, LPROS2_WAITING))
     return(False);
 
-  StrnCpy(buf->user, parse_line.owner, sizeof(buf->user)-1);
+  fstrcpy(buf->fs_user, parse_line.owner);
   if (strequal(parse_line.status, LPROS2_PRINTING))
     buf->status = LPQ_PRINTING;
   else if (strequal(parse_line.status, LPROS2_PAUSED))
@@ -951,9 +867,9 @@ static BOOL parse_lpq_os2(char *line,print_queue_struct *buf,BOOL first)
   return(True);
 }
 
-static char *stat0_strings[] = { "enabled", "online", "idle", "no entries", "free", "ready", NULL };
-static char *stat1_strings[] = { "offline", "disabled", "down", "off", "waiting", "no daemon", NULL };
-static char *stat2_strings[] = { "jam", "paper", "error", "responding", "not accepting", "not running", "turned off", NULL };
+static const char *stat0_strings[] = { "enabled", "online", "idle", "no entries", "free", "ready", NULL };
+static const char *stat1_strings[] = { "offline", "disabled", "down", "off", "waiting", "no daemon", NULL };
+static const char *stat2_strings[] = { "jam", "paper", "error", "responding", "not accepting", "not running", "turned off", NULL };
 
 #ifdef DEVELOPER
 
@@ -964,6 +880,7 @@ static BOOL parse_lpq_vlp(char *line,print_queue_struct *buf,BOOL first)
 {
        int toknum = 0;
        fstring tok;
+       const char *cline = line;
 
        /* First line is printer status */
 
@@ -971,7 +888,7 @@ static BOOL parse_lpq_vlp(char *line,print_queue_struct *buf,BOOL first)
 
        /* Parse a print job entry */
 
-       while(next_token(&line, tok, NULL, sizeof(fstring))) {
+       while(next_token(&cline, tok, NULL, sizeof(fstring))) {
                switch (toknum) {
                case 0:
                        buf->job = atoi(tok);
@@ -986,10 +903,10 @@ static BOOL parse_lpq_vlp(char *line,print_queue_struct *buf,BOOL first)
                        buf->time = atoi(tok);
                        break;
                case 4:
-                       fstrcpy(buf->user, tok);
+                       fstrcpy(buf->fs_user, tok);
                        break;
                case 5:
-                       fstrcpy(buf->file, tok);
+                       fstrcpy(buf->fs_file, tok);
                        break;
                }
                toknum++;
@@ -1003,6 +920,7 @@ static BOOL parse_lpq_vlp(char *line,print_queue_struct *buf,BOOL first)
 /****************************************************************************
 parse a lpq line. Choose printing style
 ****************************************************************************/
+
 BOOL parse_lpq_entry(int snum,char *line,
                     print_queue_struct *buf,
                     print_status_struct *status,BOOL first)
@@ -1029,9 +947,6 @@ BOOL parse_lpq_entry(int snum,char *line,
     case PRINT_PLP:
       ret = parse_lpq_plp(line,buf,first);
       break;
-    case PRINT_SOFTQ:
-      ret = parse_lpq_softq(line,buf,first);
-      break;
     case PRINT_LPRNT:
       ret = parse_lpq_nt(line,buf,first);
       break;
@@ -1075,23 +990,23 @@ BOOL parse_lpq_entry(int snum,char *line,
       case LPSTAT_OK:
        for (i=0; stat0_strings[i]; i++)
          if (strstr(line,stat0_strings[i])) {
-           StrnCpy(status->message,line,sizeof(status->message)-1);
-           status->status=LPSTAT_OK;
-           return ret;
+                 fstrcpy(status->message,line);
+                 status->status=LPSTAT_OK;
+                 return ret;
          }
       case LPSTAT_STOPPED:
        for (i=0; stat1_strings[i]; i++)
          if (strstr(line,stat1_strings[i])) {
-           StrnCpy(status->message,line,sizeof(status->message)-1);
-           status->status=LPSTAT_STOPPED;
-           return ret;
+                 fstrcpy(status->message,line);
+                 status->status=LPSTAT_STOPPED;
+                 return ret;
          }
       case LPSTAT_ERROR:
        for (i=0; stat2_strings[i]; i++)
          if (strstr(line,stat2_strings[i])) {
-           StrnCpy(status->message,line,sizeof(status->message)-1);
-           status->status=LPSTAT_ERROR;
-           return ret;
+                 fstrcpy(status->message,line);
+                 status->status=LPSTAT_ERROR;
+                 return ret;
          }
        break;
       }