home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / eel / grep04.zip / GREP.E < prev    next >
Text File  |  1991-08-12  |  6KB  |  217 lines

  1. /*
  2.  * Grep commands for Epsilon 5.0
  3.  *
  4.  * Copyright (C) 1991, K. Shane Hartman
  5.  *
  6.  * This file is free software; you can redistribute it and/or modify
  7.  * it so long as this notice is preserved and no fee is charged (other than 
  8.  * reasonable media fees) for distribution.  You are forbidden to deny these
  9.  * rights to any other user of Epsilon.  Lugaru Software Ltd. may incorporate 
  10.  * any and all of this code into Epsilon and have all rights to
  11.  * the code, since it is only useful for Epsilon, although a lower level 
  12.  * interface would work better.
  13.  *
  14.  * This file is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  17.  *
  18.  *
  19.  * This program provides an interface to Unix like Grep program for epsilon.
  20.  * Two commands are provided: M-x grep and M-x project grep.  grep prompts 
  21.  * for a grep regular expression and a path spec to search in.  project-grep 
  22.  * is similar but it will search over a list of directories (see variable 
  23.  * project_directories below).  Output is directed to the process buffer so
  24.  * that next-error command (C-X C-n) will take you to successive matches.
  25.  * You can continue to edit while the search proceeds.
  26.  *
  27.  * Unfortunately, I have not figured out a way to notify when a match is 
  28.  * found, so either watch the process buffer or keep trying C-x C-n.  This 
  29.  * program assumes the existence of Gnu style Grep program in your path.
  30.  * If you obtained this from the Lugaru bulletin board, they won't allow 
  31.  * the grep program this goes with to reside on the BBS.  You can obtain it
  32.  * from Channel 1 BBS in Cambridge, or by anonymous FTP from several places,
  33.  * for example, WSMR-SIMTEL20.ARMY.MIL, FTP.AI.MIT.EDU, PREP.AI.MIT.EDU.  Just
  34.  * look in the Gnu directories.
  35.  *
  36.  *
  37.  * Revision History: for epsilon 5.0
  38.  * Send comments, bug fixes, suggestions to shane@ai.mit.edu
  39.  *
  40.  * Version 1.2: 07/17/91 shane@ai.mit.edu Prepare for uploading to simtel.
  41.  * Version 1.3: 08/02/91 shane@ai.mit.edu Changed copyright wording.  Add
  42.  *         comments.
  43.  * Version 1.4: 08/12/91 johnk@wrq.com (John Kercheval) Don't recenter the
  44.  *         process buffer, remove superfluous call to make mode.  Pass
  45.  *         the case flag to grep correctly.
  46.  */
  47.  
  48. #include "eel.h"
  49.  
  50. /*
  51.  * Customizable variables.
  52.  */
  53. #ifdef SPR
  54.  
  55. #include "vardefs.h"
  56.  
  57. #else
  58.  
  59. /* Contains the last regex searched for by grep or project-grep. */
  60. char grep_regex[81] = "";
  61.  
  62. /*
  63.  * A list of subdirectories or absolute directories that project-grep will 
  64.  * search in.  It is semicolon separated list just like PATH environment 
  65.  * variable.  If a component is not absolute, it will be interpreted as 
  66.  * relative to project_directory variable.  For example, suppose that 
  67.  * project_directory is "c:\project" and this variable is set to
  68.  * ".;foo;d:\bar", then the following directories will be searched, in order,
  69.  *
  70.  *  the current directory
  71.  *  c:\project\foo
  72.  *  d:\bar
  73.  */
  74. char project_directories[256] = "";
  75.  
  76. /* 
  77.  * This is the root pathname for the project grep command. Directories 
  78.  * mentioned in project_directories are interpreted as relative to this.  It 
  79.  * should be absolute, for example, "c:\project".
  80.  */
  81. char project_directory[FNAMELEN] = "";
  82.  
  83. #endif   /* SPR */
  84.  
  85. /* These are the files that grep will search in by default. */
  86. char grep_files[81] = "*.h *.c";
  87.  
  88. /*
  89.  * Code.
  90.  */
  91.  
  92. do_grep (files, last, fold)
  93. char *files;
  94. short last;
  95. {
  96.   short result;
  97.   char cmd[128];
  98.   char *caseflag;
  99.   char *obufname = bufname;  
  100.   
  101.   if (fold)
  102.     caseflag = "-i";
  103.   else
  104.     caseflag = "";
  105.   sprintf (cmd, "grep %s -n \"%s\" %s", caseflag, grep_regex, files);
  106.   if (!another)
  107.   {
  108.     zap ("process");
  109.     if (concur_shell ("", ""))
  110.     {
  111.       error ("Couldn't exec: error %d", errno);
  112.     }
  113.   }
  114.   bufname = "process";
  115.   point = size ();
  116.   bprintf ("\n%s\n", cmd);
  117.   if (last)
  118.     bprintf ("\nexit\n");
  119.   bufname = obufname;
  120. }
  121.  
  122. pathname_as_directory(spec)
  123. char *spec;
  124. {
  125.   int i = strlen (spec);
  126.   if (i && !is_path_separator (spec[i - 1]))
  127.   {
  128.         spec[i] = path_sep, spec[i + 1] = 0;    /* add final / if none */
  129.   }
  130. }
  131.  
  132. /*
  133.  * Commands.
  134.  */
  135.  
  136. command grep()
  137. {
  138.   char cmd[sizeof (grep_regex)];
  139.   
  140.   get_strdef (cmd, "Search for", grep_regex);
  141.   if (!*cmd)
  142.     quick_abort ();
  143.   strcpy (grep_regex, cmd);
  144.   get_strdef (cmd, "in files", grep_files);
  145.   if (!*cmd)
  146.     quick_abort();
  147.   strcpy (grep_files, cmd);
  148.   do_grep (grep_files, 1, has_arg ? !case_fold : case_fold);
  149. }
  150.  
  151. command project_grep()
  152. {
  153.   char cmd[sizeof (grep_regex)];
  154.   char str[sizeof (project_directories)];
  155.   char spec[FNAMELEN];
  156.   char *s = str;
  157.   char *f;
  158.   char *ss = s;
  159.   char *fs;
  160.   int i;
  161.   
  162.   iter = 0;
  163.   get_strdef (cmd, "Search for", grep_regex);
  164.   if (!*cmd)
  165.     quick_abort ();
  166.   strcpy (grep_regex, cmd);
  167.   get_strdef (cmd, "in files", grep_files);
  168.   if (!*cmd)
  169.     quick_abort ();
  170.   strcpy (grep_files, cmd);  
  171.   strcpy (s, project_directories);
  172.   while (s)
  173.   {
  174.     ss = index (s, ';');
  175.     if (ss)
  176.       *ss++ = 0;
  177.     if (strcmp (s, ".") == 0)
  178.     {
  179.       strcpy (spec, project_directory);
  180.     }
  181.     else
  182.     {
  183.       strcpy (spec, s);
  184.       absolute (spec);
  185.       if (strcmp (spec, s) != 0)
  186.       {
  187.         strcpy (spec, project_directory);
  188.         pathname_as_directory (spec);
  189.         strcat (spec, s);
  190.       }
  191.     }
  192.     pathname_as_directory (spec);      
  193.     f = grep_files;
  194.     while (f != NULL)
  195.     {
  196.       strcpy (cmd, spec);
  197.       for (i = 0; f[i] != 0 && f[i] != ' '; i++)
  198.         ;
  199.       if (i > 0)
  200.       {
  201.         char save = f[i];
  202.         f[i] = 0;
  203.         strcat (cmd, f);
  204.         f[i] = save;
  205.         for (; f[i] == ' '; i++)
  206.           ;
  207.         if (f[i] == 0)
  208.           f = NULL;
  209.         else
  210.           f += i;
  211.       }
  212.       do_grep (cmd, (short) !ss && !f, has_arg ? !case_fold : case_fold);
  213.     }
  214.     s = ss;
  215.   }
  216. }
  217.