#!/usr/local/bin/kermit +
#
# In Unix change the first line to show actual full pathname of C-Kermit,
# and give this file execute permission.
#
# Synopsis:      Recursive "grep" (search for pattern in directory tree).
# Requires:      C-Kermit 7.0 later or K95 1.1.19 or later.
# Illustrates:   Recursive directory traversal, pattern matching.
# Author:        F. da Cruz
#                The Kermit Project, Columbia University
#                July 1999; Updated Feb 2000 for K95.
#
# If you execute this file as a Kerbang script, it does the search and
# then exits.  If you TAKE this file from the Kermit prompt, it defines
# the RGREP macro, which you can use subsequently as many times as you want.
#
# Both pattern and filespec may contain metacharacters.  If no filespec is
# given, all files in the current directory tree are searched.
#
# When running as a Kerbang script, and the filespec contains metacharacters,
# you must enclose it in doublequotes to prevent the UNIX shell from expanding
# it so Kermit itself can expand it (recursively).
#
# The pattern must be quoted if it contains metacharacters or spaces, no
# matter how you are invoking RGREP; otherwise Kermit wouldn't know where
# the pattern ended and where the filenames start.
#
# Alphabet case is significant.  Uncomment the SET CASE OFF command if you
# prefer case independence.  Or (exercise left for the reader): modify this
# script to accept optional "switches" for case sensitivity and any other
# operating parameters you'd like to specify for each RGREP invocation.
#
# Kerbang examples (UNIX, assuming this file is called "rgrep"):
#  rgrep Version			; Looks for "Version" in all files
#  rgrep "Version Number"		; ... "Version Number" in all files
#  rgrep Version *.c                    ; Illegal because shell expands *.c
#  rgrep Version "*.c"                  ; Looks for "Version" in all .c files
#
# Examples from Kermit prompt:
#  take rgrep.ksc                       ; You must TAKE this file first.
#  rgrep Version			; Looks for "Version" in all files
#  rgrep {Version Number}		; ... "Version Number" in all files
#  rgrep Version *.c                    ; Looks for "Version" in all .c files

local kerbang myquit f \%n \%i \&a[]	; Invoked as Kerbang script?
.kerbang = 0				; Assume no.
if eq "\%0" "\v(cmdfil)" .kerbang = 1	; This means we were.

define MYQUIT {				; Macro to exit appropriately.
   if not def \%1 def \%1 0		; Optional exit status code.
   if def \%2 echo \%2			; Optional message.
   if \m(kerbang) exit \%1		; If Kerbang script, exit all the way.
   stop \%1				; Otherwise return to Kermit prompt.
}
if not \m(kerbang) {			; Define the RGREP macro
    if not def RGREP {
	assign RGREP take \fpathname(\v(cmdfil)),
	echo RGREP macro defined.
	echo {To use it, type "rgrep <pattern> <filespec>".}
	myquit
    }
}
;;; SET CASE OFF			; Uncomment for case independence.

.\%n ::= \v(argc) - 1			; How many arguments on command line.
switch \%n {
  :0, myquit 1 {Usage: \%0 pattern [ filespec ]}
  :1, .\%2 = *
  :2, .\%n := \frfiles(\fcont(\%2),&a), break ; 1 arg, expand it recursively.
  :default, myquit 1 {Too many args - please quote filespec}
}
; The following FOR loop does all the real work.  See the TYPE /MATCH
; description for an explanation.

for \%i 1 \%n 1 {			; For each file
    .f := \fcont(\&a[\%i])		; List matching lines
    type /nopage /match:{*\%1*} /prefix:{\m(f): } \m(f)
}
if ( eq \v(system) WIN32 ) {
    echo
    echo (Use Page Up (\\Kupscn) to view material that scrolled off screen...)
}
myquit
