summaryrefslogtreecommitdiff
path: root/tests/general/io.icn
blob: 826ef836157f45e48c181c0c5de2d5897633bac2 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#SRC: JCON

#  I/O test -- writes ./tmp1 and ./tmp2 as well as stdout

procedure main()
   local L, f, n, t1, t2

   L := [&input, &output, &errout,
         n := open("/dev/null", "w") | stop("no /dev/null")]
   L := sort(L)
   every f := !L do
      write(type(f), ": ", image(f))

   write()
   write(read())
   write(read(&input))
   while write(read()) do break
   write(!&input)
   every write(!&input \ 2)
   every write(!&input) \ 2

   write()
   write("flush /dev/null: ", image(flush(n)) | "FAILED")
   write("close /dev/null: ", image(close(n)) | "FAILED")
   write("close /dev/null: ", image(close(n)) | "FAILED")
   write("flush /dev/null: ", image(flush(n)) | "FAILED")

   every remove("tmp1" | "tmp2")
   every remove("tmp1" | "tmp2")
   write(image(open("tmp1")))		# should fail

   write()
   rfile("/dev/null")
   wfile("tmp1", "w", "follow the yellow brick road")
   rfile("tmp1")
   wfile("tmp1", "w", "shorter file")
   rfile("tmp1")
   wfile("tmp1", "a", "gets extended")
   rfile("tmp1")
   wfile("tmp1", "rw", "changed")
   rfile("tmp1")
   wfile("tmp1", "b", "mode b ")
   rfile("tmp1")
   wfile("tmp1", "crw", "cleared anew")
   rfile("tmp1")
   rename("tmp1", "tmp2")
   rfile("tmp2")

   write()
   write(image(t1 := open("tmp1", "w")) | "can't open tmp1")
   write(image(t2 := open("tmp2", "w")) | "can't open tmp2")
   writes(">stdout", t1, ">1a", t2, ">2a", &output)
   writes(">stdout", t2, ">2b", t1, ">1b", &output)
   write(">stdout", t1, ">1c", t2, t2, ">2c", &output)
   write(">stdout", t2, ">2d", t1, t1, ">1d", &output)
   every write(t1 | t2)
   writes(t1, ">1e")
   writes(t2, ">2e")
   write(t1, ">1f")
   write(t2, ">2f")
   every close(t1 | t2)
   rfile("tmp1")
   rfile("tmp2")

   every remove("tmp1" | "tmp2")
   every remove("tmp1" | "tmp2")

   write()
   writes("abc")
   writes("def\nghi")
   writes("\njklmno\n")
   write("pqr\nstu")
   writes("vwxyz")
   writes()
   writes("")
   writes("\n")

   write()
   tsys("echo hello world")			# simple echo
   tsys("ls io.[ids][tca][dnt]")		# check wildcarding

   tpipe()

end



#  wfile(name, mode, s) -- break apart string and write file

procedure wfile(name, mode, s)
   local f

   write()
   writes("write ", name, ",", mode, ":\t ")
   if f := open(name, mode) then s ? {
      writes(s)
      tab(many(' '))
      while not pos(0) do {
         write(f, tab(upto(' ') | 0))
         tab(many(' '))
         }
      write(" : ", where(f))
      flush(f)
      close(f)
      }
   else {
      write("can't open")
      }
   return
end



#  rfile(name) -- read and echo file contents (several different ways)

procedure rfile(name)
   local f, i

   writes("read  ", name, ":\t")
   if not (f := open(name, "r")) then {
      write(" can't open")
      fail
      }

   # read()
   while writes(" ", read(f))
   write()

   # bang
   seek(f, 1)
   every writes(" ", "  !f:\t\t" | !f | "\n")

   # both, mixed
   seek(f, 1)
   writes("   read/!f:\t")
   while writes(" ", read(f)) do writes(" ", !f)
   write()

   # reads()
   seek(f, 1)
   writes("   reads():\t")
   while writes(" ", map(reads(f, 5), "\n", "."))
   write()

#%#%   # nonsequential  (disabled because it's inconsistently buggy...)
#%#%   writes("   nonseq:\t ")
#%#%   every i := 30 to -30 by -1 do
#%#%      if seek(f, i) then
#%#%	 writes(map(reads(f), "\n", ".") | "?")
#%#%      else
#%#%	 writes("-")
#%#%   write()

   close(f)
   return
end


#  tsys(s) -- test system call

procedure tsys(s)
   write("$ ", s)
   system(s)
   return
end


#  tpipe() -- test pipes

procedure tpipe()
   local f, p

   # very simple case
   write()
   p := open("echo hello world", "rp") | stop("can't open echo pipe")
   write(image(p))
   while write("> ", read(p))
   close(p)

   # check unclosed pipe
   write()
   p := open("sed 's/^/=()= /' io.icn", "p") | stop("can't open sed pipe")
   write(image(p))
   every 1 to 10 do write("> ", read(p))
   # p is deliberately left unclosed

   # check wildcarding, and also !pipe
   write()
   p := open("ls io.i?n io.d?t io.s?d", "p") | stop("can't open ls pipe")
   write(image(p))
   every write("> ", !p)
   close(p)

   # check output pipe
   write()
   p := open("tr aeiou oaeui", "wp") | stop("can't open tr pipe")
   write(image(p))
   write(p, "once upon a midnight dreary")
   write(p, "two roads diverged in a yellow wood")
   write(p, "and the mome raths outgrabe")
   write("--- closing output pipe")
   close(p)
   write("--- done closing output pipe")
   remove("tmp1")

   return
end