home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / rgrep < prev    next >
Text File  |  2020-01-01  |  4KB  |  84 lines

  1. #!/usr/local/bin/kermit +
  2. #
  3. # In Unix change the first line to show actual full pathname of C-Kermit,
  4. # and give this file execute permission.
  5. #
  6. # Synopsis:      Recursive "grep" (search for pattern in directory tree).
  7. # Requires:      C-Kermit 7.0 later or K95 1.1.19 or later.
  8. # Illustrates:   Recursive directory traversal, pattern matching.
  9. # Author:        F. da Cruz
  10. #                The Kermit Project, Columbia University
  11. #                July 1999; Updated Feb 2000 for K95.
  12. #
  13. # If you execute this file as a Kerbang script, it does the search and
  14. # then exits.  If you TAKE this file from the Kermit prompt, it defines
  15. # the RGREP macro, which you can use subsequently as many times as you want.
  16. #
  17. # Both pattern and filespec may contain metacharacters.  If no filespec is
  18. # given, all files in the current directory tree are searched.
  19. #
  20. # When running as a Kerbang script, and the filespec contains metacharacters,
  21. # you must enclose it in doublequotes to prevent the UNIX shell from expanding
  22. # it so Kermit itself can expand it (recursively).
  23. #
  24. # The pattern must be quoted if it contains metacharacters or spaces, no
  25. # matter how you are invoking RGREP; otherwise Kermit wouldn't know where
  26. # the pattern ended and where the filenames start.
  27. #
  28. # Alphabet case is significant.  Uncomment the SET CASE OFF command if you
  29. # prefer case independence.  Or (exercise left for the reader): modify this
  30. # script to accept optional "switches" for case sensitivity and any other
  31. # operating parameters you'd like to specify for each RGREP invocation.
  32. #
  33. # Kerbang examples (UNIX, assuming this file is called "rgrep"):
  34. #  rgrep Version            ; Looks for "Version" in all files
  35. #  rgrep "Version Number"        ; ... "Version Number" in all files
  36. #  rgrep Version *.c                    ; Illegal because shell expands *.c
  37. #  rgrep Version "*.c"                  ; Looks for "Version" in all .c files
  38. #
  39. # Examples from Kermit prompt:
  40. #  take rgrep.ksc                       ; You must TAKE this file first.
  41. #  rgrep Version            ; Looks for "Version" in all files
  42. #  rgrep {Version Number}        ; ... "Version Number" in all files
  43. #  rgrep Version *.c                    ; Looks for "Version" in all .c files
  44.  
  45. local kerbang myquit f \%n \%i \&a[]    ; Invoked as Kerbang script?
  46. .kerbang = 0                ; Assume no.
  47. if eq "\%0" "\v(cmdfil)" .kerbang = 1    ; This means we were.
  48.  
  49. define MYQUIT {                ; Macro to exit appropriately.
  50.    if not def \%1 def \%1 0        ; Optional exit status code.
  51.    if def \%2 echo \%2            ; Optional message.
  52.    if \m(kerbang) exit \%1        ; If Kerbang script, exit all the way.
  53.    stop \%1                ; Otherwise return to Kermit prompt.
  54. }
  55. if not \m(kerbang) {            ; Define the RGREP macro
  56.     if not def RGREP {
  57.     assign RGREP take \fpathname(\v(cmdfil)),
  58.     echo RGREP macro defined.
  59.     echo {To use it, type "rgrep <pattern> <filespec>".}
  60.     myquit
  61.     }
  62. }
  63. ;;; SET CASE OFF            ; Uncomment for case independence.
  64.  
  65. .\%n ::= \v(argc) - 1            ; How many arguments on command line.
  66. switch \%n {
  67.   :0, myquit 1 {Usage: \%0 pattern [ filespec ]}
  68.   :1, .\%2 = *
  69.   :2, .\%n := \frfiles(\fcont(\%2),&a), break ; 1 arg, expand it recursively.
  70.   :default, myquit 1 {Too many args - please quote filespec}
  71. }
  72. ; The following FOR loop does all the real work.  See the TYPE /MATCH
  73. ; description for an explanation.
  74.  
  75. for \%i 1 \%n 1 {            ; For each file
  76.     .f := \fcont(\&a[\%i])        ; List matching lines
  77.     type /nopage /match:{*\%1*} /prefix:{\m(f): } \m(f)
  78. }
  79. if ( eq \v(system) WIN32 ) {
  80.     echo
  81.     echo (Use Page Up (\\Kupscn) to view material that scrolled off screen...)
  82. }
  83. myquit
  84.