Debian Codegraph @

trip/files.rexx

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992 Anders Christensen <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* ==== config ==== */
21
22 conf.truncate = 1 /* truncates when writing line to middle of file */
23
24 trace o
25 parse source os .
26 call time 'R'
27 written = 0
28 signal on notready
29
30 simple = 'simple'
31 if os = 'UNIX' | os = 'BEOS' | os = 'QNX' | os = 'AMIGA' | os = 'LINUX' then
32 do
33 eol = 'LF'
34 oscopy = 'cp'
35 osdel1 = 'rm -f'
36 oslsl_root = 'ls -l /'
37 tonul = ' > /dev/null 2>&1'
38 file = '/tmp/rexx.test'
39 'echo "First line" > simple'
40 'echo "Second line" >> simple'
41 'echo "Third line" >> simple'
42 num_eol_chars = 1
43 eol_chars = '0a'x
44 end
45 else
46 do
47 eol = 'CRLF'
48 oscopy = 'copy'
49 osdel1 = 'del'
50 oslsl_root = 'dir \'
51 tonul = '> nul 2>&1'
52 file = 'c:\temp\rexx.tst'
53 'echo First line> simple'
54 'echo Second line>> simple'
55 'echo Third line>> simple'
56 num_eol_chars = 2
57 eol_chars = '0d'x || '0a'x
58 end
59 osdel1 file tonul
60
61
62 Parse Version Ver .
63 If Left( ver, 11 ) = 'REXX-Regina' Then regina = 1
64 Else regina = 0
65 /*signal test022*/
66
67 test001:
68 /* === 001 : simple write and read ============================ */
69 call notify 'simple'
70 file001 = file || 001
71 if (0\==lineout(file,'This is the first line')) then
72 call complain 'Could not write first line'
73 if (0\==lineout(file,'of a very little file')) then
74 call complain 'Could not write second line'
75 if (0\==lineout(file,'that will be used')) then
76 call complain 'Could not write third line'
77 if (0\==lineout(file,'in this trip test')) then
78 call complain 'Could not write fourth line'
79
80 call lineout file
81
82 signal test002
83
84
85 test002:
86 /* === 002 : simple reading ======================================== */
87 call notify 'read'
88 call stream file, 'c', 'close'
89 res = linein( file )
90 if (res\=='This is the first line') then
91 call complain 'didnt position at start after reopening after close'
92
93 res = linein( file )
94 res = linein( file )
95 if (res \== "that will be used") then
96 call complain 'Didnt position properly after several reads'
97
98 call lineout file
99 signal test003
100
101
102 test003:
103 /* === 003 : Test behavior at EOF ================================== */
104 call notify 'eof'
105
106 res = linein( file, 3 )
107 res = linein( file )
108 if (res\=='in this trip test') then
109 call complain 'did not manage to read last line'
110
111 signal off notready
112 res = linein( file )
113 if res\=='' then
114 call complain 'nullstring not returned at EOF'
115
116 call stream file, 'c', 'close'
117 signal on notready
118
119 signal test004
120
121 test004:
122 /* === 004 : are we able to read zero lines ======================== */
123 call notify 'zero_line'
124
125 res = linein( file, 3, 0 )
126 if (res\=='') then
127 call complain 'Could not position without reading anything'
128
129 res = linein( file )
130 if (res\=='that will be used') then
131 call complain 'Did not position correctly in the file'
132
133 res = linein( file, 2 )
134 if (res\=='of a very little file') then
135 call complain 'Could not position and read simultaneously'
136
137 call lineout file
138 signal test005
139
140
141 test005:
142 /* === 005 : Will it truncate when writing new lines ===================== */
143 call notify 'truncate'
144
145 oscopy file file'.tmp' tonul
146 res = lineout( file'.tmp', '<<<foobar>>>', 2 )
147 if (res\=='0') then
148 call complain 'non-zero result from when just positioning'
149
150 res = linein( file'.tmp', 1 )
151 if (res\=='This is the first line') then
152 call complain 'first line was corrupted'
153
154 res = linein( file'.tmp' )
155 if (res\=='<<<foobar>>>') then
156 call complain 'second line not properly written'
157
158 signal off notready
159 res = linein( file'.tmp' )
160 if (res\=="") then
161 call complain 'file was not truncated'
162 signal on notready
163
164 call lineout file'.tmp'
165 signal test006
166
167
168
169 test006:
170 /* === 006 : Can we read characters =================================== */
171 call notify 'charin'
172
173 if eol = "LF" then
174 do
175 numcol1 = 15
176 numcol2 = 17
177 resstr1 = 'rst line'||'0a'x||'of a v'
178 resstr2 = 'ery little file'||'0a'x||'t'
179 end
180 else
181 do
182 resstr1 = 'rst line'||'0d'x||'0a'x||'of a v'
183 resstr2 = 'ery little file'||'0d'x||'0a'x||'t'
184 numcol1 = 16
185 numcol2 = 18
186 end
187 res = charin( file, 15, numcol1 )
188 if (res \== resstr1) then
189 call complain 'could not read characterbased from file'
190
191 res = charin( file,, numcol2 )
192 if (res \== resstr2) then
193 call complain 'charin cant position right'
194
195 res = linein( file )
196 if (res\=='hat will be used') then
197 call complain 'character and line based io does not share read pointer'
198
199 call lineout file
200 signal test007
201
202
203
204 test007:
205 /* === 007 : Is I/O faked when a file is in error? ==================== */
206
207 if regina = 0 then signal test009 /* following tests only valid for Regina */
208
209 call notify 'faking'
210
211 exist = 'existent'
212 nonexist = 'nonexistent'
213 oslsl_root '>' exist
214 foo = 0
215
216 osdel1 nonexist tonul
217
218 call on notready name trap007
219 res = length(charin(nonexist,,10)) + length('cp'(exist,nonexist)) + foo +,
220 length(charin(nonexist,,10)) + foo + length(linein(nonexist)) + foo +,
221 length(charin(nonexist,,10)) + foo + length(linein(nonexist)) + foo
222 if res\==0 then
223 call complain "Condition NOTREADY don't fake further I/O properly"
224
225 if foo\==1 then
226 call complain "Help, I don't think condition handler was called"
227
228 call lineout nonexist
229 call lineout exist
230
231 signal on notready
232 signal test008
233
234
235
236 trap007:
237 foo = 1
238 address system
239 'touch' nonexist
240 'echo "Here is the contents of nonexistent" >>' nonexist
241 'echo "Here is the second line of nonexistent" >>' nonexist
242 'echo "Here is the third line of nonexistent" >>' nonexist
243 return
244
245
246
247
248 test008:
249 /* === 008 : But I/O to other files is _not_ to be disturbed =========== */
250 call notify 'nofake'
251
252 call on notready name trap008
253 exist = 'existent'
254 nonexist = 'nonexistent'
255 oslsl_root '>' exist
256 foo = 0
257
258 sum = 20 + length(linein(exist)) + length(linein(exist))
259 call stream exist, 'c', 'close'
260 call stream nonexist, 'c', 'close'
261
262 'rm -f' nonexist
263 res = length(charin(nonexist,,10)) + length('cp'(exist,nonexist)) + foo +,
264 length(charin(exist,,10)) + foo + length(linein(exist,1)) + foo +,
265 length(charin(nonexist,,10)) + foo + length(linein(nonexist)) + foo +,
266 length(charin(exist,,10)) + foo + length(linein(exist,2)) + foo +,
267 length(charin(nonexist,,10)) + foo + length(linein(nonexist)) + foo
268
269 if res\==sum then
270 call complain "Condition NOTREADY fake other files too"
271
272 if foo\==1 then
273 call complain "Help, I don't think condition handler was called"
274
275 call lineout exist
276 call lineout nonexist
277
278 signal on notready
279 signal test009
280
281 trap008:
282 foo = 1
283 return
284
285
286
287 test009:
288 /* === 009 : If rest of line is a EOL, what does linein return? ====== */
289 call notify 'halfline'
290
291 osdel1 file tonul
292 if eol = 'CRLF' then do
293 'echo First line>'file
294 'echo Second line>>'file
295 'echo Third line>>'file
296 end
297 else do
298 'echo "First line" >'file
299 'echo "Second line" >>'file
300 'echo "Third line" >>'file
301 end
302
303 call stream file, 'c', 'close'
304
305 res = charin( file,, 10 ) /* now at EOL */
306 if res\=='First line' then
307 call complain "Simple use of charin() failed: got:["res"] expected [First line]"
308
309 res = linein( file ) /* must be rest of line, i.e. nullstring */
310 if res\=='' then
311 call complain "Linein() didn't return '' when next char was EOL; got["res"]"
312
313 res = charin( file, 1, 10 )
314 if res\=='First line' then
315 call complain "Simple use of charin() does not work; got["res"] expected [First line]"
316
317 res = linein( file, 1 )
318 if res\=='First line' then
319 call complain "Linein() didn't read whole line after positioning; got["res"] expected [First line]"
320
321 call lineout file
322 signal test010
323
324
325 test010:
326 /* === 010 : Are read and write positions independent? ================ */
327 call notify 'crp/cwp'
328
329 file = 'testfile'
330 oscopy simple file tonul
331
332 res = charin(file,,5)
333 if res\=='First' then
334 call complain "Simple use of charin() failed"
335
336 res = charout(file,"IRST FIL",2)
337 if res\==0 then
338 call complain "Simple use of charout() failed"
339
340 res = charin(file,,5)
341 if res\==' FILe' then
342 call complain "Charout() didn't overwrite data in file"
343
344 res = charin(file,1,10)
345 if res\=='FIRST FILe' then
346 call complain "Charout() write incorrect data to file"
347
348 res = charout(file, 'xyzzy' )
349 if res\==0 then
350 call complain "Continuing charout() didn't work"
351
352 res = charin(file,, 10)
353 if eol = 'CRLF' then str = "yzzycond l"
354 else str = "yzzyond li"
355 if res\==str then
356 call complain "Charin() or charout() doesn't work; got["res"] expecting ["str"]"
357
358 call lineout file
359 signal test011
360
361
362
363
364 test011:
365 /* === : 011 I/O should only be faked when NOTREADY is trapped ======== */
366 call notify 'notready'
367
368 if eol = 'CRLF' then none = '\no\such\file'
369 else none = '/no/such/file'
370 foo = 0
371
372 call stream none, 'c', 'close'
373 osdel1 none tonul
374
375 call on notready name trap011
376 res = lineout(none,"Foobar") + lineout(none,"HeppHepp")
377 if res\==0 then
378 call complain "Don't fake line output after error output"
379
380 res = lineout(none,"Foobar") + lineout(none,"HeppHepp")
381 if res\==0 then
382 call complain "Doesn't fakes line output in next line after error output"
383
384 if foo\==2 then
385 call complain "Condition trap was not called"
386
387 signal off notready
388 res = lineout(none,"Foobar") + lineout(none,"HeppHepp")
389 if res\==2 then
390 call complain "Fakes line output in next line after error output"
391
392 if foo\==2 then
393 call complain "Condition trap called too many times"
394
395
396 call on notready name trap011
397 call stream none, 'c', 'close'
398 foo = 0
399 res = charout(none,"Foobar") + charout(none,"HeppHepp")
400 if res\==0 then
401 call complain "Don't fake char output after error output"
402
403 res = charout(none,"Foobar") + charout(none,"HeppHepp")
404 if res\==0 then
405 call complain "Doesn't fake char output in next line after error output"
406
407 if foo\==2 then
408 call complain "Condition trap was not called"
409
410 signal off notready
411 res = charout(none,"Foobar") + charout(none,"HeppHepp")
412 if res\==14 then
413 call complain "Fakes char output in next line after error output"
414
415
416
417 call stream none, 'c', 'close'
418 call off notready
419
420 res = lineout(none,"Foobar") + lineout(none,"HeppHepp")
421 if res\==2 then
422 call complain "Did fake line output when not trapping NOTREADY"
423
424 res = charout(none,"Foobar") + charout(none,"HeppHepp")
425 if res\==14 then
426 call complain "Did fake char output when not trapping NOTREADY"
427
428 if foo\==2 then
429 call complain "Did call trap, when not enabled"
430
431 call lineout none
432 signal on notready
433 signal test012
434
435
436 trap011:
437 foo = foo + 1
438 return
439
440
441 test012:
442 /* === 012 : Illegal to position more than one after EOF ============ */
443 /* in writeable files, and after EOF in readable files */
444
445 if regina = 0 then signal test013 /* test only valid for Regina */
446
447 call notify 'holes'
448
449 file = 'testfile'
450 oscopy simple file tonul
451
452 call stream file, 'c', 'close'
453 lines = lines( file )
454 chars = 0
455 if (lines>1) then
456 do lines
457 chars = chars + length(linein(file)) + num_eol_chars
458 end
459 else
460 do while lines( file )>0
461 chars = chars + length(linein(file)) + num_eol_chars
462 end
463
464 call stream file, 'c', 'close'
465
466
467 signal on notready name trap012a
468 signal on syntax name trap012a
469 foo = 0
470 call charin file, chars, 0 /* at last char */
471 foo = 1
472 trap012a:
473 call stream file, 'c', 'reset'
474 if foo\==1 then
475 call complain "Trapped when positioning read at last char in file"
476
477
478 signal on notready name trap012b
479 signal on syntax name trap012b
480 foo = 0
481 call charin file, chars+1, 0 /* char 'after' EOF */
482 foo = 1
483 trap012b:
484 call stream file, 'c', 'reset'
485 if foo\==0 then
486 call complain "Didn't trap positioning read after EOF"
487
488 signal on notready name trap012c
489 signal on syntax name trap012c
490 foo = 0
491 call charout file,, chars+1 /* legal, don't truncate */
492 foo = 1
493 trap012c:
494 call stream file, 'c', 'reset'
495 if foo\==1 then
496 call complain "Trapped when positioning write at EOF+1"
497
498 'cmp' simple file ">/dev/null"
499 if rc\==0 then
500 call complain "File changed when positioning at EOF+1"
501
502 signal on notready name trap012d
503 signal on syntax name trap012d
504 foo = 0
505 call charout file,, chars+2 /* not legal */
506 foo = 1
507 trap012d:
508 call stream file, 'c', 'reset'
509 if foo\==0 then
510 call complain "Not trapped when positioning write beyond EOF+1"
511
512 'cmp' simple file tonul
513 if rc\==0 then
514 call complain "File changed when positioning beyond EOF+1"
515
516
517 signal on notready name trap012e
518 signal on syntax name trap012e
519 foo = 0
520 call charout file,, chars /* legal, may not truncate */
521 foo = 1
522 trap012e:
523 call stream file, 'c', 'reset'
524 if foo\==1 then
525 call complain "Trapped when positioning at EOF"
526
527 'cmp' simple file tonul
528 if rc\==0 then
529 call complain "File was changed when char positioning in file"
530
531 call lineout file
532 signal off syntax
533 signal on notready
534 signal test013
535
536
537
538
539 test013:
540 /* === 013 : A line written to the middle of a file gets a ============ */
541 /* trailing, but not leading EOF ============================ */
542 call notify 'linecnt'
543
544 file = 'testfile'
545 oscopy simple file tonul
546
547 signal off notready
548 call charout file,, 14+num_eol_chars
549 call lineout file, "<<<..--..>>>"
550 res = charin( file, 1, 30+(2*num_eol_chars) )
551
552 if left(res,25+(2*num_eol_chars))\=="First line"eol_chars"Sec<<<..--..>>>"eol_chars then
553 call complain "Problems when inserting a line into a file"
554
555 if conf.truncate then do
556 last_line_len = 25+(2*num_eol_chars)
557 if length(res)\==last_line_len then
558 call complain "File was not truncated. End of file contents length should be" last_line_len"; got" length(res)
559 end
560 else
561 if length(res)\==30 & right(res,3)\=='d l' then
562 call complain "Rest of line was mangled, after inserting line in file"
563
564 call lineout file
565 signal on notready
566 signal test014
567
568
569
570
571 test014:
572 /* === 014 : When doing system() after fork() all files are closed, === */
573 /* Maybe we can trigger flushing twice? ===================== */
574 call notify 'flush'
575
576 file = 'testfile'
577 call stream file, 'c', 'close'
578 call stream file, 'c', 'open write replace'
579 call stream file, 'c', 'close'
580
581 junk = charout(file, "<<<--->>>"'0a'x)
582
583 address system 'rc 0'
584 call stream file, 'c', 'close'
585
586 if lines(file)\==1 | chars(file)\==10 then
587 call complain "Possibly double flushing during file output"
588
589 call lineout file
590 signal test015
591
592
593
594 test015:
595 /* === 015 : Illegal to position on transient streams ================= */
596 /* Default input and output are transient =================== */
597 call notify 'repos'
598
599 signal on syntax name trap015a
600 signal on notready name trap015a
601 res = charin(,42,0)
602 call complain "Didn't trap character repositioning on default input"
603
604 trap015a:
605 signal on syntax name trap015b
606 signal on notready name trap015b
607 res = charout(,,42)
608 call complain "Didn't trap character repositioning on default output"
609
610 trap015b:
611 signal on syntax name trap015c
612 signal on notready name trap015c
613 res = linein(,42,0)
614 call complain "Didn't trap line repositioning on default input"
615
616 trap015c:
617 signal on syntax name trap015d
618 signal on notready name trap015d
619 res = lineout(,,42)
620 call complain "Didn't trap line repositioning on default output"
621
622 trap015d:
623
624 signal on notready
625 signal off syntax
626 signal test016
627
628
629
630 test016:
631 /* === 016 : Reading EOF for persistent file raises NOTREADY ========== */
632 call notify 'persist/eof'
633
634 file = 'testfile'
635 oscopy simple file tonul
636
637 call stream file, 'c', 'close'
638 call on notready name trap016
639 foo = 0
640 res = charin(file,28+(num_eol_chars*2),10)
641 if (foo\==1) then
642 call complain "Didn't raise NOTREADY when reading beyond EOF"
643
644 call lineout file
645 signal on notready
646 signal test017
647
648 trap016:
649 foo = 1
650 if res\=='line'eol_chars then
651 call complain "Data not correct when reading beyond EOF"
652 return
653
654
655
656
657
658
659 test017:
660 /* === 017 : Let's try to write to <stdin> and vice versa ... ========== */
661 call notify 'stdio'
662
663 signal on notready name trap017a
664 call lineout 'stdin', 'foobar'
665 call complain "Didn't trap writing to default input stream"
666
667 trap017a:
668
669 signal on notready name trap017b
670 call linein 'stdout'
671 call complain "Didn't trap reading from default output stream"
672
673 trap017b:
674
675 signal on notready
676 signal test018
677
678
679 test018:
680 /* === 018 : Let's read some really long lines ======================== */
681 call notify 'longlines'
682
683 maxlen = 1024 * 256
684 len = 256
685
686 do while len<maxlen
687 call stream file, 'c', 'open write replace'
688 hipp = copies('x',len)
689 written = lineout( file, copies('x', len))
690 if written\=0 then
691 call complain "Didn't manage to write out all the data"
692 hopp = copies('x',len)
693 line = linein( file, 1 )
694 if line\==copies('x',len) | len\=length(line) then
695 call complain "Data read don't equal data written"
696 len = trunc(len * 1.5, 0) - 10
697 end
698
699 call lineout file
700 osdel1 file tonul
701 drop hopp hipp line
702 signal test019
703
704
705 test019:
706 /* === 019 : Are we able to open 200 files? =========================== */
707 call notify 'many'
708
709 osdel1 'a*.tmp' tonul
710
711 mf = 130
712 do i=1 to mf
713 res1 = lineout( "a" || i || ".tmp", "line 1 in file" i "of test")
714 res2 = lineout( "a" || i || ".tmp", "line 2 in file" i "of test")
715 if (res1 + res2 > 0) then do
716 call complain "Didn't manage to write to all" mf "files (only "i")"
717 signal trip019
718 end
719 end
720
721 do i=1 to 30
722 fileno = 1 + (i*5) // (mf-1)
723 lineno = random( 1, 2 )
724
725 res = linein( "a" || fileno || ".tmp", lineno ) ;
726 if (res\=="line" lineno "in file" fileno "of test") then do
727 call complain "Didn't manage to read back the data written"
728 leave ;
729 end
730 end
731
732 trip019:
733 do i=1 to mf
734 call stream "a" || i || ".tmp", 'c', 'close' ;
735 end
736
737 osdel1 'a*.tmp' tonul
738 signal test020
739
740
741
742 test020:
743 /* === 020 : Does chars() or lines() make an unopened file stick ======== */
744 /* in the rexx file table? ==================================== */
745 call notify 'stick'
746
747 time = '1'
748
749 first020:
750 signal on notready name notr020a
751 call stream 'hepp', 'c', 'close'
752 oscopy 'simple hepp' tonul
753
754 if time==2 then
755 call linein 'hepp', 1, 0
756
757 signal on syntax name trap020
758 if lines('hepp','C')\==3 then
759 call complain "Simple use of lines() didn't work"
760 signal off syntax
761
762 call stream 'hepp', 'c', 'close' /* must close before delete */
763 osdel1 'hepp' tonul
764 line = linein('hepp',1)
765 if time == 1 then do
766 if line='' then
767 call complain "NOTREADY not triggered when reading nonex. file"
768 else if line=='First line' then
769 call complain "File stuck in memory when counting lines()"
770 else
771 call complain "Something is wrong with lines(), I think"
772 end
773 else
774 if line \== "First line" then
775 call complain "File wasn't found in memory"
776
777 notr020a:
778 if time=1 then do
779 time = 2
780 signal first020
781 end
782
783
784
785 /* Then, repeat for charin() */
786
787 time = '1'
788 second020:
789 signal on notready name notr020b
790 call stream 'hepp', 'c', 'close'
791 oscopy 'simple hepp' tonul
792
793 if time==2 then
794 call lineout 'hepp', "hepp" /* opens for read and write */
795
796 hepp_size = 31 + (num_eol_chars * 3)
797 line_size = 4 + num_eol_chars
798 if chars('hepp')\==hepp_size + (time==2)*line_size then
799 call complain "Simple use of chars() didn't work"
800
801 call stream 'hepp', 'c', 'close' /* must close before delete */
802 osdel1 'hepp' tonul
803 line = linein('hepp',1)
804 if time == 1 then do
805 if line='' then
806 call complain "NOTREADY not triggered when reading nonex. file"
807 else if line=='First line' then
808 call complain "File stuck in memory when counting chars()"
809 else
810 call complain "Something is wrong with chars(), I think"
811 end
812 else
813 if line \== "First line" then
814 call complain "File wasn't found in memory"
815
816 notr020b:
817 if time=1 then do
818 time = 2
819 signal second020
820 end
821
822 call lineout hepp
823 signal on notready
824 signal test021
825
826 trap020:
827 call complain "Interpreter does not support ANSI. LINES( fn, 'C' ) is valid ANSI."
828
829
830 test021:
831 /* === 021 : Test that close *always* close a file, even if ============= */
832 /* the file in error is in error state ======================== */
833 call notify 'close'
834 call on notready name notr021
835
836 file = simple
837 res = charin(file,,5) charin(file,500),
838 stream(file,'c','close') charin(file,,5)
839 if res\=='foo' then
840 call complain "NOTREADY condition was not triggered"
841
842 call lineout file
843 signal on notready
844 signal test022
845
846 notr021:
847 if res="First line" then
848 call complain "Something very strange happened, failure not noticed"
849 else if res='First' then
850 call complain "Statement interrupted during execution"
851 else if res\="First UNKNOWN First" then
852 call complain "Didn't manage to read correct data"
853 res = 'foo'
854
855 return
856
857
858
859 test022:
860 /* === 022 : Does empty output without repositioning close the file? ===== */
861 call notify 'empty_out(default)'
862 signal off notready
863
864 call stream simple, 'c', 'close'
865 line = linein( simple ) ;
866
867 if line\=="First line" then
868 call complain "STREAM(fn,'C','CLOSE') doesn't seem to have closed file"
869
870 call lineout simple
871 line = linein( simple )
872
873 if line\=="First line" then
874 call complain "LINEOUT(fn) should be the same as 'CLOSE'."
875
876 call stream simple, 'c', 'close'
877 signal on notready
878 signal test022a
879
880 test022a:
881 /* === 022 : Does empty output without repositioning close the file? STRICT_ANSI ===== */
882 call notify 'empty_out(STRICT_ANSI)'
883 signal off notready
884 OPTIONS STRICT_ANSI
885
886 call stream simple, 'c', 'close'
887 line = linein( simple ) ;
888
889 if line\=="First line" then
890 call complain "STREAM(fn,'C','CLOSE') doesn't seem to have closed file"
891
892 call lineout simple
893 line = linein( simple )
894
895 if line\=="Second line" then
896 call complain "LINEOUT(fn) should be different to 'CLOSE' according to STRICT_ANSI."
897
898 call stream simple, 'c', 'close'
899 OPTIONS NOSTRICT_ANSI
900 signal on notready
901 signal test023
902
903
904 test023:
905 /* === 023 : Line positioning of file using STREAM ===== */
906 call notify 'line_pos'
907 signal off notready
908
909 call stream simple, 'c', 'close'
910 call stream simple, 'c', 'open write replace'
911 do i = 1 to 100
912 call lineout simple, Left('line 'i, 10, '*')
913 end
914 if eol = 'LF' then line_length = 11
915 else line_length = 12
916 call stream simple, 'c', 'close'
917
918 cmd = 'OPEN BOTH'
919 ret = stream( simple, 'c', cmd ); correct = 'READY:'
920 if ret \= correct then
921 call complain "STREAM(fn,'C','"cmd"') didn't work: got:" ret 'instead of:' correct
922 call checkseekpos 1, 1, 1+(100*line_length), 101
923
924 /*
925 * Move read pos to line 30, write pos to line 70
926 */
927 cmd = 'SEEK =30 READ LINE'
928 ret = stream( simple, 'c', cmd ); correct = 30
929 if ret \= correct then
930 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
931 call checkseekpos 1+(29*line_length) , 30, 1+(100*line_length), 101
932
933 cmd = 'SEEK =70 WRITE LINE'
934 ret = stream( simple, 'c', cmd ); correct = 70
935 if ret \= correct then
936 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
937 call checkseekpos 1+(29*line_length) , 30, 1+(69*line_length), 70
938
939 /*
940 * Move read pos to line 60, write pos to line 50
941 */
942 cmd = 'SEEK +30 READ LINE'
943 ret = stream( simple, 'c', cmd ); correct = 60
944 if ret \= correct then
945 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
946 call checkseekpos 1+(59*line_length) , 60, 1+(69*line_length), 70
947
948 cmd = 'SEEK -20 WRITE LINE'
949 ret = stream( simple, 'c', cmd ); correct = 50
950 if ret \= correct then
951 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
952 call checkseekpos 1+(59*line_length) , 60, 1+(49*line_length), 50
953
954 /*
955 * Move read pos to line 50, write pos to line 40
956 */
957 cmd = 'SEEK -10 READ LINE'
958 ret = stream( simple, 'c', cmd ); correct = 50
959 if ret \= correct then
960 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
961 call checkseekpos 1+(49*line_length) , 50, 1+(49*line_length), 50
962
963 cmd = 'SEEK -10 WRITE LINE'
964 ret = stream( simple, 'c', cmd ); correct = 40
965 if ret \= correct then
966 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
967 call checkseekpos 1+(49*line_length) , 50, 1+(39*line_length), 40
968
969 /*
970 * Should now be able to read line 50
971 */
972 ret = linein( simple ); correct = 'line 50***'
973 if ret \= correct then
974 call complain "LINEIN(fn) returns the wrong value got:" '['ret']' 'instead of:' '['correct']'
975 /* read pos should now be at line 51 */
976 call checkseekpos 1+(50*line_length) , 51, 1+(39*line_length), 40
977
978 /*
979 * Move read pos to line 91, write pos to line 81
980 */
981 cmd = 'SEEK <10 READ LINE'
982 ret = stream( simple, 'c', cmd ); correct = 91
983 if ret \= correct then
984 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
985 call checkseekpos 1+(90*line_length) , 91, 1+(39*line_length), 40
986
987 cmd = 'SEEK <20 WRITE LINE'
988 ret = stream( simple, 'c', cmd ); correct = 81
989 if ret \= correct then
990 call complain "STREAM(fn,'C','"cmd"') didn't position correctly got:" ret 'instead of:' correct
991 call checkseekpos 1+(90*line_length) , 91, 1+(80*line_length), 81
992
993 call stream simple, 'c', 'close'
994 signal on notready
995
996
997 /* CHARIN():
998
999 * If name is omitted, I/O skjer til standard input/output
1000 * Lesing starter normalt paa current read position
1001 * Error naar start er etter EOF
1002 * Dersom lengde er null, skjer bare positionering
1003 * Dersom det er sjanse for at flere tegn blir tilgjengelig
1004 (transiente) er I/O blokket.
1005 * EOF paa persistent filer gir NOTREADY
1006
1007 CHAROUT():
1008
1009 * Returnerer remaining chars
1010 * Hvis string=nullstring, => rc=0
1011 * Default write=\= default read position
1012 * Initial read position and write position
1013 * Start for write kan spesifisere size(file)+1, mens read bare
1014 kan spesifisere size(file)
1015 * Dersom string=tom og start=tom, saa settes cur write pos til
1016 EOF, og evt filen lukkes
1017 * Utskrift er blokket, eller gir feil paa persistent files
1018
1019 CHARS()
1020
1021 * Antallet inkluderer lkinjeskift
1022 * Returnerer 1 dersom ukjent
1023
1024 LINEIN():
1025
1026 * Kan returnere delvise linjer i kombinasjon med charing
1027 * Dersom start er oppgitt, starter read alltid paa starten av en
1028 linje, selv om start er current linje, og current er paa
1029 midten av linja.
1030 * Blokkerer I/O til fullstendig linje er tilgjenglig (transient)
1031 * Retuernerer ufullstendig linje og trigger NOTREADY for EOF
1032 paa persistent files
1033
1034 LINEOUT():
1035
1036 * String='' skriver ut ebn EOL
1037 * Ved skriving til en file, terminerer lionjer *etter* nu linje,
1038 ikke foran
1039
1040 LINES():
1041
1042 * Antall hele linjher som er igjen: a) halv linje paa slutten teller
1043 ikke, mens b) halv linje paa starten telles
1044
1045 * Pull/linein og say/lineout bruker samme interface.
1046
1047 * Status for en stream lagres ikke over subroutine kall
1048
1049 Vanskelige ....
1050
1051 * Skrive til <stdin> og lese fra <stdout>
1052 * Aapne filer en masse, og se om noe skjaerer seg.
1053 * Les lange (svaert lange) linjer
1054 * Proev aa forvirre med filer der siste linjer ikke har EOL
1055 * Posisjoner til en linjer som ikke finnes
1056 * Skrive inn, eller fjern EOL fra fil, og se om det forvirrer
1057 linjetellingen. Gjoer skrivingen baade innefra og utenfra Rexx
1058 * Apne tom filer for lesing
1059 * La lesepos vaere langt ute i fila, trunker paa et tidligere punkt
1060 * Forsoek posisjonering paa standard input og output
1061 * Test at lesing/skriving avc ASCII NUL fungerer baade for
1062 character og linje basert I/O
1063 * Klarer vi aa lang kroell med /dev/null? Vi skal ikke kunne lese,
1064 og ved skriving skal det bli kroell i linjetellingen
1065 * Har leseaccess; proever aa skrive, mistes leseakksess?
1066 * Omvendt, dersom vi starter med leseaccess.
1067 * Har leseaccess, denne fjernes (Av eksternt program), forsoeker aa
1068 faa skriveaksess, mistes leseacckess
1069 * samme for skriveaccess
1070 * Aapner fil for leseakses, flytter fila, aapner for skriveakksess
1071 * Kan vi lure den til aa flush'e to ganger: ved fork() og close()
1072
1073 * Kan vi bruke cd() til aa faa den til aa operere paa en annen fil?
1074
1075 * Naa trigger syntax dersom vi proever aa positionere oss til et
1076 punkt etter eof.
1077
1078 */
1079
1080
1081
1082 test999:
1083 say ' '
1084 say time('E') "seconds used"
1085 exit 0
1086
1087 notready:
1088 say ' ...'
1089 say 'Got an unwanted NOTREADY ... Ouch!'
1090 say 'cond("d")='condition("d")
1091 length = charout(,' (')
1092 return
1093
1094
1095 ch: procedure expose sigl
1096 trace off
1097 parse arg first, second
1098 if first \== second then do
1099 say
1100 say 'first = /'first'/'
1101 say 'second = /'second'/'
1102 say "FuncTrip: error in " sigl":" sourceline(sigl) ; end
1103 return
1104
1105
1106 complain:
1107 trace off
1108 say ' ...'
1109 if arg() = 2 then lineno = arg(2)
1110 else lineno = sigl
1111 say 'Tripped in line' lineno':' arg(1)'.'
1112 length = charout(,' (')
1113 return
1114
1115
1116 notify:
1117 parse arg word .
1118 written = written + length(word) + 2
1119 if written>75 then do
1120 written = length(word)
1121 say ' '
1122 end
1123 call charout , word || ', '
1124 return
1125
1126
1127 error:
1128 say 'Error discovered in function insert()'
1129 return
1130
1131 checkseekpos: Procedure Expose simple sigl
1132 parse arg readchar, readline, writechar, writeline
1133
1134 lineno = sigl
1135 cmd = 'QUERY SEEK READ CHAR'
1136 ret = stream( simple, 'c', cmd ); correct = readchar
1137 if ret \= correct then
1138 call complain "STREAM( fn,'C','"cmd"') returns the wrong value got:" ret 'instead of:' correct, lineno
1139
1140 cmd = 'QUERY SEEK READ LINE'
1141 ret = stream( simple, 'c', cmd ); correct = readline
1142 if ret \= correct then
1143 call complain "STREAM(fn,'C','"cmd"') returns the wrong value got:" ret 'instead of:' correct, lineno
1144
1145 cmd = 'QUERY SEEK WRITE CHAR'
1146 ret = stream( simple, 'c', cmd ); correct = writechar
1147 if ret \= correct then
1148 call complain "STREAM(fn,'C','"cmd"') returns the wrong value got:" ret 'instead of:' correct, lineno
1149
1150 cmd = 'QUERY SEEK WRITE LINE'
1151 ret = stream( simple, 'c', cmd ); correct = writeline
1152 if ret \= correct then
1153 call complain "STREAM(fn,'C','"cmd"') returns the wrong value got:" ret 'instead of:' correct, lineno
1154
1155 return