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