home *** CD-ROM | disk | FTP | other *** search
- Rem ****************************************
- Rem * Shell3a external command - wc.opl
- Rem * ╕ Nick Murray August 1996
- Rem *
- Rem * wc - Display the number of lines,
- Rem * words and character in a file
- Rem ****************************************
- PROC wc%:(n%)
- LOCAL i%,ret%,d$(128),handle%,txt$(255),p%
- LOCAL lines%,words%,chars%
- ONERR ErrTrap::
- IF n%<2 AND SHin%=0 Rem no args and no redirected input
- PRINT "Usage: wc <filename>"
- RETURN
- ENDIF
- i%=2 Rem argv%(2) is the FIRST argument after the comman d
- IF SHin% Rem input is redirected, so set the file
- handle%=SHin% Rem handle to SHin% and jump over the
- GOTO loop:: Rem file handling routines.
- ENDIF
- WHILE i%<=n% Rem whilst we have more arguments
- Rem parse the i% th argument to check if it's a valid file
- ret%=Fparse%:(ADDR(d$),PEEK$(argv%(i%)))
- IF ret%<0 Rem error opening this file
- ShErr:(i%,ret%)
- ELSEIF ret% AND 16 Rem this is a directory
- ShErr:(i%,3)
- ELSE
- REM open=$0000, text=$0020, share=$0400
- ret%=IOOPEN(handle%,d$,$0420)
- IF ret%<0
- ShErr:(i%,ret%)
- ELSE
- lines%=0 Rem initialize counters
- words%=0
- chars%=0
- loop:: WHILE 1
- IF KEY=27 Rem check for escape key
- GIPRINT ERR$(-114)
- SHesc%=1
- GOTO quit::
- ENDIF
- Rem read the next line of input, up to 254 chars
- ret%=IOREAD(handle%,UADD(ADDR(txt$),1),255)
- IF ret%<0 Rem an error of some sort
- IF ret%<>-36 Rem -36 merely indicates EOF
- ShErr:(i%,ret%) Rem another error, display
- ENDIF
- GOTO Endfile::
- ENDIF
- POKEB ADDR(txt$),ret% Rem set length of input
- chars%=chars%+ret%+2 Rem EOL 10 and 13
- lines%=lines%+1
- WHILE LEN(txt$)
- p%=LOC(txt$," ")
- IF p%>1 Rem not blank first
- words%=words%+1
- ELSEIF p%=0 AND LEN(txt$)
- words%=words%+1
- BREAK
- ENDIF
- txt$=RIGHT$(txt$,LEN(txt$)-p%)
- ENDWH
- ENDWH
- EndFile:: txt$=num$(lines%,-6)+NUM$(words%,-6)+NUM$(chars%,-6)
- IF handle%<>SHin% Rem only close input file
- ret%=IOCLOSE(handle%) Rem if it's not SHin%
- IF ret%<0
- ShErr:(i%,ret%)
- ENDIF
- txt$=txt$+" "+PrPATH$:(d$)
- ENDIF
- fprint%:(txt$) Rem use fprint% so output can be
- ENDIF Rem redirected
- ENDIF
- i%=i%+1
- ENDWH
- RETURN
- quit:: Rem called on escape key
- IF handle%<>SHin%
- IOCLOSE(handle%)
- ENDIF
- RETURN
- ErrTrap::
- ONERR off
- PRINT err$:(ERR)
- ENDP
-