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 / ifilter.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  87 lines

  1. ############################################################################
  2. #
  3. #    File:     ifilter.icn
  4. #
  5. #    Subject:  Program to filter lines of file
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     January 21, 1999
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program applies the operation given as a command-line argument
  18. #  to each line of standard input, writing out the results.  For example,
  19. #
  20. #    ifilter reverse <foo
  21. #  
  22. #  writes out the lines of foo reversed end-for-end.
  23. #
  24. #  Trailing arguments can be given on the command line, as in
  25. #
  26. #    ifilter right 10 0 <foo        # right(*, "10", "0")
  27. #    ifilter "%" 11 <foo        # * % "11"
  28. #
  29. #  The modules strings and numbers are linked to provide access to the
  30. #  procedures they contain.  Except for these and operators and (built-in)
  31. #  functions, this program needs to be linked with procedures to be
  32. #  used with it.
  33. #
  34. #  The following options are supported:
  35. #
  36. #    -a i    argument position for strings read in; default 1
  37. #    -o i    resolution of ambiguous operator string names, 1 for unary, 2
  38. #          for binary; default 2
  39. #    -l i    limit on generation, with nonpositive indicating
  40. #          no limitation; default 1
  41. #
  42. ############################################################################
  43. #
  44. #  Note:  This is a renaming of an earlier program, filter.icn, to
  45. #  avoid name collisions on systems where there already is a utility
  46. #  named filter.
  47. #
  48. ############################################################################
  49. #
  50. #  Links:  lists, numbers, options, and strings
  51. #
  52. ############################################################################
  53.  
  54. invocable all
  55.  
  56. link lists
  57. link numbers
  58. link options
  59. link strings
  60.  
  61. procedure main(args)
  62.    local op, opts, i, interp, limit
  63.  
  64.    opts := options(args, "a+o+l+")
  65.    i := \opts["a"] | 1
  66.    limit := \opts["l"] | 1
  67.    if limit < 1 then limit := 2 ^ 31
  68.  
  69.    if opts["o"] === (&null | 2) then {
  70.       op := proc(pop(args), 2 | 1 | 3) |
  71.          stop("*** invalid or missing operation")
  72.       }
  73.    else if opts["o"] = 1 then {
  74.        op := proc(pop(args), 1 | 2 | 3) |
  75.          stop("*** invalid or missing operation")
  76.       }
  77.    else stop("*** invalid -o option")
  78.  
  79.    lextend(args, i - 1)                # be sure list is long enough
  80.  
  81.    args := args[1:i] ||| [&null] ||| args[i:0]    # make room for input argument
  82.  
  83.    while args[i] := read() do
  84.       every write(op ! args) \ limit
  85.  
  86. end
  87.