home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / grep.ex < prev    next >
Text File  |  1994-03-10  |  2KB  |  88 lines

  1. -- grep.e
  2. -- usage: grep string [files...]
  3. -- search through a bunch of files for a given string
  4. -- example:  grep constant *.e
  5. -- String may be given between quotes "...." with \ as an escape
  6.  
  7. constant SCREEN = 1
  8. constant TRUE = 1
  9. constant LINES_PER_CR = 21
  10.  
  11. integer line_count
  12. line_count = LINES_PER_CR
  13.  
  14. integer console
  15. integer match_count
  16.  
  17. procedure scan(integer fileNum, 
  18.           sequence string, 
  19.           sequence file_name,
  20.           integer show_name)
  21. -- print all lines in current file containing the string
  22.     object line
  23.  
  24.     while TRUE do
  25.     line = gets(fileNum)
  26.     if atom(line) then
  27.         return -- end of file
  28.     else
  29.         if match(string, line) then
  30.         match_count = match_count + 1
  31.         if show_name then
  32.             puts(SCREEN, file_name & ": ")
  33.         end if
  34.         puts(SCREEN, line)
  35.         line_count = line_count - 1
  36.         if line_count = 0 then
  37.             puts(SCREEN, "\n\t\t=== Enter for more ===\n")
  38.             line = gets(console) -- pause
  39.             line_count = LINES_PER_CR
  40.         end if
  41.         end if
  42.     end if
  43.     end while
  44. end procedure
  45.  
  46. include getnames.e
  47.  
  48. procedure grep()
  49. -- process all files 
  50. -- usage: grep.bat string files...
  51.     sequence string, line
  52.     integer fileNum
  53.     sequence file_names
  54.  
  55.     file_names = get_names()
  56.  
  57.     -- process all the files    
  58.     line = command_line()
  59.     if length(line) < 3 then
  60.     puts(SCREEN, "usage: grep string [files]\n")
  61.     return
  62.     end if
  63.     string = line[3]
  64.     if length(string) = 0 then
  65.     puts(SCREEN, "search string is empty\n")
  66.     return
  67.     end if
  68.     match_count = 0
  69.     for i = 1 to length(file_names) do
  70.     fileNum = open(file_names[i], "r")   
  71.     if fileNum = -1 then
  72.         printf(SCREEN, "cannot open %s\n", {file_names[i]})
  73.     else
  74.         scan(fileNum, string, file_names[i], length(file_names) > 1)            
  75.         close(fileNum)
  76.     end if
  77.     end for
  78.     if match_count > 1 then
  79.         printf(SCREEN, "\n\t\t%d lines contain \"%s\"\n", {match_count, string})
  80.     end if
  81. end procedure
  82.  
  83. console = open("CON", "r")
  84. if console != -1 then
  85.     grep()
  86. end if
  87.  
  88.