4.3BSD/usr/contrib/icon/src/cmd/i-trfil.icn

#	I-TRFIL(1)
#
#	Icon trace filter
#
#	Allan J. Anderson
#
#	Last modified 7/10/83
#

global buffer, ylist, nlist, yylist, priorlines, followlines

procedure main(a)
   local s, n, i
   initial {
      ylist := []
      nlist := []
      yylist := []
      buffer := []
      priorlines := followlines := 0
      }
   every s := !a do
      if s[1+:2] == ("-c" | "-C") then {
	 if n := integer(s[3:0] | a[i + 1]) then {
	    if n = 0 then priorlines := followlines := 0
	    else if n < 0 then priorlines := abs(n)
	    else if n > 0 then followlines := n
	    }
	 }
      else if s[1] == "+" then
	 if *s <= 1 then next
	 else put(ylist,s[2:0])
      else if s[1] == "^" then
	 if *s <= 1 then next
	 else put(nlist,s[2:0])
      else if s[1] == "!" then
	 if *s <= 1 then next
	 else put(yylist,s[2:0])
      else if not integer(s) then stop(
	 "usage: trfil +includestring ^excludestring !keepstring -cn <infile")
   while trfil(read())
end	# main

procedure trfil(line)
   if *buffer > priorlines then get(buffer)
   put(buffer,line)
   if (*ylist = 0) | find(!ylist,line) then delcheck(line)
   return
end

procedure delcheck(line)
   local n
   if find(!nlist,line) then 
      if not find(!yylist,line) then return
   every write(get(buffer))
   n := followlines
   while n > 0 do {
      if find(!ylist,line) & (not find(!nlist,line)) then
	 n := followlines
      else n -:= 1
      if *buffer < priorlines then put(buffer,line)
      write(line)
      if *buffer >= priorlines then get(buffer,line)
      line := read()
      }
   if (priorlines > 0 | followlines > 0) then write("\t")
end