home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / ibrow.icn < prev    next >
Text File  |  2000-07-29  |  5KB  |  187 lines

  1. ############################################################################
  2. #
  3. #    File:     ibrow.icn
  4. #
  5. #    Subject:  Program to browse Icon files for declarations
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     September 7, 1990
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Usage: ibrow [<Icon source file name>...]
  18. #
  19. #  If no source file names are provided on the command line, all *.icn
  20. #  files in the current directory are browsed.
  21. #
  22. #  The program facilitates browsing of Icon programs.  It was originally
  23. #  written to browse the Icon Program Library, for which purpose it
  24. #  serves quite well.  The user interface is self-explanatory -- just
  25. #  remember to use "?" for help if you're confused.
  26. #
  27. ############################################################################
  28. #
  29. #  Links:  colmize
  30. #
  31. ############################################################################
  32. #
  33. #  Requires:  UNIX
  34. #
  35. ############################################################################
  36.  
  37. link colmize
  38.  
  39. procedure main(arg)
  40.    local p, proctab, doneNames, fn, f, foundNonEmptyLine, block, lineNbr
  41.    local line, keywd, startLine, proclist, w, i, x, proclines, cmd, b
  42.  
  43.    if not (&features == "UNIX") then stop("Runs only under UNIX")
  44.    if *arg = 0 then {
  45.       p := open("ls *.icn","rp")
  46.       while put(arg,read(p))
  47.       close(p)
  48.       }
  49.    proctab := table()
  50.    #
  51.    #  Loop to scan all of the specified source files and save their
  52.    #  procedures and records.
  53.    #
  54.    doneNames := set()    # This set is used to prevent scanning twice if
  55.             # both a source and a suffixless icode file are
  56.             # passed as arguments (e.g. mydir/*).
  57.    write("Icon Browser -- scanning files:")
  58.    every fn := !arg do {
  59.       if not (fn[-4:0] == ".icn") then fn ||:= ".icn"
  60.       if member(doneNames,fn) then next
  61.       insert(doneNames,fn)
  62.       f := if fn == "-" then &input else open(fn) | next
  63.       write("  ",fn)
  64.       #
  65.       #  Loop to process lines of file (in string scanning mode).
  66.       #
  67.       foundNonEmptyLine := &null
  68.       block := []
  69.       lineNbr := 0
  70.       while line := read(f) do line ? {
  71.      lineNbr +:= 1
  72.      if not pos(0) then {
  73.         foundNonEmptyLine := 1
  74.         if (tab(many(' \t')) | "")\1 &
  75.           (keywd := =("end" | "global" | "link")\1) |
  76.           (keywd := =("procedure" | "record")\1 &
  77.           tab(many(' \t')) & name := tab(upto(' \t('))\1) then {
  78.            if keywd == ("procedure" | "record") then startLine := lineNbr
  79.            if keywd == "record" then {
  80.           until find(")",line) do {
  81.              put(block,line)
  82.              line := read(f) | break
  83.              lineNbr +:= 1
  84.              }
  85.           }
  86.            if proctab[name || case keywd of {"end": "()"; "record": "."}] :=
  87.              [block,fn,startLine] then put(block,line)
  88.            if keywd ~== "procedure" then {
  89.           foundNonEmptyLine := &null
  90.           block := []
  91.           }
  92.            }
  93.         }
  94.      if \foundNonEmptyLine then put(block,line)
  95.      }
  96.       #
  97.       #  Close this file.
  98.       #
  99.       close(f)
  100.       }
  101.    doneNames := &null
  102.    #
  103.    #  Reorganize the data.
  104.    #
  105.    proctab := sort(proctab)
  106.    proclist := []
  107.    w := **proctab
  108.    i := 0
  109.    every x := !proctab do
  110.      put(proclist,right(i +:= 1,w) || ". " || x[1])
  111.    proclines := []
  112.    every put(proclines,colmize(proclist))
  113.    proclist := []
  114.    every put(proclist,(!proctab)[2])
  115.    proctab := &null
  116.    #
  117.    #  Interact with the user to browse.
  118.    #
  119.    repeat {
  120.       write()
  121.       every write(!proclines)
  122.       write()
  123.       repeat {
  124.      #
  125.      #  Prompt for, read, and analyze the user's command.
  126.      #
  127.      writes("\nq,nn,nn[fmev],<return> (? for help): ")
  128.      line := read() | exit()
  129.      case line of {
  130.        "q": exit()
  131.        "?": help() & next
  132.        "":  break
  133.      }
  134.      if integer(line) then line ||:= "f"
  135.      if cmd := line[-1] & any('fmev',cmd) &
  136.            block := proclist[0 < integer(line[1:-1])] then {
  137.         case cmd of {
  138.            "f": {
  139.           #
  140.           #  Write the file name containing the procedure and the
  141.           #  first line of the procedure.
  142.           #
  143.           b := block[1]
  144.           every line := b[1 to *b] do {
  145.              line ? (if (tab(many(' \t')) | "")\1 &
  146.                =("procedure" | "record") then break)
  147.              }
  148.           write(block[2],": ",line)
  149.           }
  150.            "m": {
  151.           #
  152.           #  List the procedure using "more".
  153.           #
  154.           write()
  155.           p := open("more","pw") | stop("Can't popen")
  156.           every write(p,!block[1])
  157.           close(p)
  158.               }
  159.            "e" | "v": {
  160.           #
  161.           #  Invoke ex or vi positioned at the first line
  162.           #  of procedure or record.
  163.           #
  164.           system((if cmd == "e" then "ex" else "vi") ||
  165.                 " +" || block[3] || " " || block[2])
  166.           }
  167.            }
  168.         }
  169.      }
  170.      }
  171. end
  172.  
  173. procedure help()
  174.    write(
  175. "\nEnter:_
  176. \n    q                     Quit_
  177. \n    ?                     Display help message (this message)_
  178. \n    <return>              Redisplay the list of procedure and record names_
  179. \n    <number from list>[f] Display the file name and first line of_
  180. \n                          procedure or record_
  181. \n    <number from list>m   Display the procedure or record using \"more\"_
  182. \n    <number from list>e   Invoke \"ex\" positioned to procedure or record_
  183. \n    <number from list>v   Invoke \"vi\" positioned to procedure or record"
  184.    )
  185.    return
  186. end
  187.