[BACK]Return to grep.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Diff for /src/usr.bin/mg/grep.c between version 1.27 and 1.28

version 1.27, 2006/04/03 00:19:32 version 1.28, 2006/05/02 17:10:25
Line 39 
Line 39 
 static int       grep(int, int);  static int       grep(int, int);
 static int       compile(int, int);  static int       compile(int, int);
 static int       gid(int, int);  static int       gid(int, int);
 static struct buffer    *compile_mode(const char *, const char *, const char *);  static struct buffer    *compile_mode(const char *, const char *);
 static int       getbufcwd(char *, size_t);  
 static int       xlint(int, int);  static int       xlint(int, int);
   
 void grep_init(void);  void grep_init(void);
Line 88 
Line 87 
         char     cprompt[NFILEN], *bufp;          char     cprompt[NFILEN], *bufp;
         struct buffer   *bp;          struct buffer   *bp;
         struct mgwin    *wp;          struct mgwin    *wp;
         char     path[NFILEN];  
   
         /* get buffer cwd */  
         if (getbufcwd(path, sizeof(path)) == FALSE) {  
                 ewprintf("Failed. "  
                     "Can't get working directory of current buffer.");  
                 return (FALSE);  
         }  
   
         (void)strlcpy(cprompt, "grep -n ", sizeof(cprompt));          (void)strlcpy(cprompt, "grep -n ", sizeof(cprompt));
         if ((bufp = eread("Run grep: ", cprompt, NFILEN,          if ((bufp = eread("Run grep: ", cprompt, NFILEN,
             EFDEF | EFNEW | EFCR)) == NULL)              EFDEF | EFNEW | EFCR)) == NULL)
Line 105 
Line 96 
                 return (FALSE);                  return (FALSE);
         (void)snprintf(command, sizeof(command), "%s /dev/null", bufp);          (void)snprintf(command, sizeof(command), "%s /dev/null", bufp);
   
         if ((bp = compile_mode("*grep*", command, path)) == NULL)          if ((bp = compile_mode("*grep*", command)) == NULL)
                 return (FALSE);                  return (FALSE);
         if ((wp = popbuf(bp)) == NULL)          if ((wp = popbuf(bp)) == NULL)
                 return (FALSE);                  return (FALSE);
Line 122 
Line 113 
         char     cprompt[NFILEN], *bufp;          char     cprompt[NFILEN], *bufp;
         struct buffer   *bp;          struct buffer   *bp;
         struct mgwin    *wp;          struct mgwin    *wp;
         char     path[NFILEN];  
   
         /* get buffer cwd */  
         if (getbufcwd(path, sizeof(path)) == FALSE) {  
                 ewprintf("Failed. "  
                     "Can't get working directory of current buffer.");  
                 return (FALSE);  
         }  
   
         (void)strlcpy(cprompt, "make lint ", sizeof(cprompt));          (void)strlcpy(cprompt, "make lint ", sizeof(cprompt));
         if ((bufp = eread("Run lint: ", cprompt, NFILEN,          if ((bufp = eread("Run lint: ", cprompt, NFILEN,
             EFDEF | EFNEW | EFCR)) == NULL)              EFDEF | EFNEW | EFCR)) == NULL)
Line 139 
Line 122 
                 return (FALSE);                  return (FALSE);
         (void)snprintf(command, sizeof(command), "%s 2>&1", bufp);          (void)snprintf(command, sizeof(command), "%s 2>&1", bufp);
   
         if ((bp = compile_mode("*lint*", command, path)) == NULL)          if ((bp = compile_mode("*lint*", command)) == NULL)
                 return (FALSE);                  return (FALSE);
         if ((wp = popbuf(bp)) == NULL)          if ((wp = popbuf(bp)) == NULL)
                 return (FALSE);                  return (FALSE);
Line 156 
Line 139 
         char     cprompt[NFILEN], *bufp;          char     cprompt[NFILEN], *bufp;
         struct buffer   *bp;          struct buffer   *bp;
         struct mgwin    *wp;          struct mgwin    *wp;
         char     path[NFILEN];  
   
         /* get buffer cwd */  
         if (getbufcwd(path, sizeof(path)) == FALSE) {  
                 ewprintf("Failed. "  
                     "Can't get working directory of current buffer.");  
                 return (FALSE);  
         }  
   
         (void)strlcpy(cprompt, compile_last_command, sizeof(cprompt));          (void)strlcpy(cprompt, compile_last_command, sizeof(cprompt));
         if ((bufp = eread("Compile command: ", cprompt, NFILEN,          if ((bufp = eread("Compile command: ", cprompt, NFILEN,
             EFDEF | EFNEW | EFCR)) == NULL)              EFDEF | EFNEW | EFCR)) == NULL)
Line 177 
Line 152 
   
         (void)snprintf(command, sizeof(command), "%s 2>&1", bufp);          (void)snprintf(command, sizeof(command), "%s 2>&1", bufp);
   
         if ((bp = compile_mode("*compile*", command, path)) == NULL)          if ((bp = compile_mode("*compile*", command)) == NULL)
                 return (FALSE);                  return (FALSE);
         if ((wp = popbuf(bp)) == NULL)          if ((wp = popbuf(bp)) == NULL)
                 return (FALSE);                  return (FALSE);
Line 197 
Line 172 
         struct buffer   *bp;          struct buffer   *bp;
         struct mgwin    *wp;          struct mgwin    *wp;
         int      i, j;          int      i, j;
         char     path[NFILEN];  
   
         /* get buffer cwd */  
         if (getbufcwd(path, sizeof(path)) == FALSE) {  
                 ewprintf("Failed. "  
                     "Can't get working directory of current buffer.");  
                 return (FALSE);  
         }  
   
         /* catch ([^\s(){}]+)[\s(){}]* */          /* catch ([^\s(){}]+)[\s(){}]* */
   
         i = curwp->w_doto;          i = curwp->w_doto;
Line 241 
Line 208 
                 return (FALSE);                  return (FALSE);
         (void)snprintf(command, sizeof(command), "gid %s", cprompt);          (void)snprintf(command, sizeof(command), "gid %s", cprompt);
   
         if ((bp = compile_mode("*gid*", command, path)) == NULL)          if ((bp = compile_mode("*gid*", command)) == NULL)
                 return (FALSE);                  return (FALSE);
         if ((wp = popbuf(bp)) == NULL)          if ((wp = popbuf(bp)) == NULL)
                 return (FALSE);                  return (FALSE);
Line 251 
Line 218 
 }  }
   
 struct buffer *  struct buffer *
 compile_mode(const char *name, const char *command, const char *path)  compile_mode(const char *name, const char *command)
 {  {
         struct buffer   *bp;          struct buffer   *bp;
         FILE    *fpipe;          FILE    *fpipe;
Line 266 
Line 233 
         if (bclear(bp) != TRUE)          if (bclear(bp) != TRUE)
                 return (NULL);                  return (NULL);
   
         addlinef(bp, "cd %s", path);          if (getbufcwd(bp->b_cwd, sizeof(bp->b_cwd)) != TRUE)
                   return (NULL);
           addlinef(bp, "cd %s", bp->b_cwd);
         addline(bp, command);          addline(bp, command);
         addline(bp, "");          addline(bp, "");
   
         if (getcwd(cwd, sizeof(cwd)) == NULL)          if (getcwd(cwd, sizeof(cwd)) == NULL)
                 panic("Can't get current directory!");                  panic("Can't get current directory!");
         if (chdir(path) == -1) {          if (chdir(bp->b_cwd) == -1) {
                 ewprintf("Can't change dir to %s", path);                  ewprintf("Can't change dir to %s", bp->b_cwd);
                 return (NULL);                  return (NULL);
         }          }
         if ((fpipe = popen(command, "r")) == NULL) {          if ((fpipe = popen(command, "r")) == NULL) {
Line 321 
Line 290 
         struct mgwin    *wp;          struct mgwin    *wp;
         char    *fname, *line, *lp, *ln;          char    *fname, *line, *lp, *ln;
         int      lineno, len;          int      lineno, len;
         char    *adjf;          char    *adjf, path[NFILEN];
         const char *errstr;          const char *errstr;
         struct line     *last;          struct line     *last;
   
Line 350 
Line 319 
         lineno = (int)strtonum(ln, INT_MIN, INT_MAX, &errstr);          lineno = (int)strtonum(ln, INT_MIN, INT_MAX, &errstr);
         if (errstr)          if (errstr)
                 goto fail;                  goto fail;
   
         adjf = adjustname(fname);          if (fname && fname[0] != '/') {
                   (void)strlcpy(path, curbp->b_cwd, sizeof(path));
                   if (strlcat(path, fname, sizeof(path)) >= sizeof(path))
                           goto fail;
                   adjf = path;
           } else {
                   adjf = adjustname(fname);
           }
         free(line);          free(line);
   
         if (adjf == NULL)          if (adjf == NULL)
Line 396 
Line 372 
         curwp->w_flag |= WFMOVE;          curwp->w_flag |= WFMOVE;
   
         return (compile_goto_error(f, n));          return (compile_goto_error(f, n));
 }  
   
 /*  
  * Return the working directory for the current buffer, terminated  
  * with a '/'. First, try to extract it from the current buffer's  
  * filename. If that fails, use global cwd.  
  */  
 static int  
 getbufcwd(char *path, size_t plen)  
 {  
         char *dname, cwd[NFILEN];  
         if (plen == 0)  
                 goto error;  
   
         if (curbp->b_fname && curbp->b_fname[0] != '\0' &&  
             (dname = dirname(curbp->b_fname)) != NULL) {  
                 if (strlcpy(path, dname, plen) >= plen)  
                         goto error;  
                 if (strlcat(path, "/", plen) >= plen)  
                         goto error;  
         } else {  
                 if ((dname = getcwd(cwd, sizeof(cwd))) == NULL)  
                         goto error;  
                 if (strlcpy(path, dname, plen) >= plen)  
                         goto error;  
         }  
         return (TRUE);  
 error:  
         path = NULL;  
         return (FALSE);  
 }  }

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.28