#  Copyright 2000 (c): Albert van der Horst, Dutch Forth Worksshop by GPL
lina <<%
\ ( Environmental dependancies : )
\ ( This is a ISO Forth Program using only standard ISO Words)
\ ( plus LINOS (par1, par2, par3, function# -- result/error )
\ ( The following words are not ANSI : none (any more))
\ ( An environmental dependancy ?BLANK is used. )

9 CONSTANT TAB
10 CONSTANT LF
 ( char -- flag)
\ : IS-BLANK    BL 1+ < ;   Compiles under ISO.
: IS-BLANK ?BLANK ;

VARIABLE LAST-WAS-BLANK
VARIABLE L
VARIABLE W
VARIABLE C
: ZERO-COUNT 0 L !   0 W !   0 C ! -1 LAST-WAS-BLANK ! ;
: SHOW-COUNT SPACE L @ . W @ . C @ . CR ;

100000 CONSTANT SIZE
CREATE Buffer SIZE ALLOT
: AT-WORD-START IS-BLANK  LAST-WAS-BLANK @ 0= AND ; ( char -- flag)
( All four  : ( char -- char )
: COUNT-L LF OVER = IF 1 L +! THEN ;
: COUNT-W DUP AT-WORD-START IF 1 W +! THEN DUP IS-BLANK LAST-WAS-BLANK ! ;
: COUNT-C 1 C +! ;
: COUNT-CHAR COUNT-C COUNT-W COUNT-L ;
: COUNT-BUFFER Buffer SWAP OVER + SWAP DO I C@ COUNT-CHAR DROP LOOP ; ( len-)

5 CONSTANT OPEN
3 CONSTANT READ
6 CONSTANT CLOSE
0 CONSTANT O_RDONLY

VARIABLE HANDLE
: COUNT-FILE
    BEGIN
        HANDLE @ Buffer SIZE READ LINOS DUP ?ERRUR
    DUP 0 > WHILE
        COUNT-BUFFER
    REPEAT DROP ;

: ZERO-ENDED COUNT OVER + 0 SWAP C! ; ( filename -- c-zero ended filename )
: STILL-FILES C@  ; ( string variable -- flag )( Length <>0 means more files)
( c-name -- ) ( Open file with the specified name, c-name must be modifiable)
: MY-OPEN-FILE ZERO-ENDED O_RDONLY 0 OPEN LINOS DUP ?ERRUR HANDLE ! ;
( [filename-at-here] -- handle/-1 )
: TREAT-FILE ZERO-COUNT COUNT-FILE COUNT TYPE SHOW-COUNT ;
: MY-CLOSE-FILE HANDLE @ 0 0 CLOSE LINOS ?ERRUR ;

: DO-FILE
    BEGIN
        BL WORD
    DUP STILL-FILES WHILE
        DUP MY-OPEN-FILE TREAT-FILE MY-CLOSE-FILE
    REPEAT ;

0 WARNING !

DO-FILE $*

BYE
%

# The remainder is interpreted by the shell as a here document,
# but forth has long gone bye.
#
# Note that because the input is not from a terminal messages are suppressed.
# In principle BYE is not needed, because Forth ends on the end of input.
