home *** CD-ROM | disk | FTP | other *** search
- Rem wc, an addon command for Shell5
- Rem Display the number of lines, words and character in a file
- Rem
- Rem Copyright (C) 1998 Nick Murray
- Rem
- Rem This program is free software; you can redistribute it and/or
- Rem modify it under the terms of the GNU General Public License
- Rem as published by the Free Software Foundation; either version 2
- Rem of the License, or (at your option) any later version.
- Rem
- Rem This program is distributed in the hope that it will be useful,
- Rem but WITHOUT ANY WARRANTY; without even the implied warranty of
- Rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- Rem See the GNU General Public License for more details.
- Rem
- Rem You should have received a copy of the GNU General Public License
- Rem along with this program; if not, write to the Free Software Foundation,
- Rem Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- Rem
- PROC wc%:(n%)
- LOCAL i%,ret%,d$(255),handle%,txt$(255),p%
- LOCAL lines&,words&,chars&,size&
- ONERR ErrTrap::
- IF n%<2 AND _in%=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 _in% Rem input is redirected, so set the file
- handle%=_in% 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
- _Err:(i%,ret%)
- ELSEIF ret% AND 16 Rem this is a directory
- _Err:(i%,3)
- ELSE
- ret%=IOOPEN(handle%,d$,$0600) Rem random access opening first
- IF ret%
- _Err:(i%,ret%)
- ENDIF
- IOSEEK(handle%,2,size&) Rem get size
- IOCLOSE(handle%)
- handle%=0
- REM open=$0000, text=$0020, share=$0400
- ret%=IOOPEN(handle%,d$,$0420)
- IF ret%
- _Err:(i%,ret%)
- ELSE
- lines&=0 Rem initialize counters
- words&=0
- chars&=0
- loop:: WHILE 1
- IOYIELD
- IF _stat%<>-46
- IF _key%(1)=27
- GOTO Quit::
- ELSE
- KEYA(_stat%,_key%())
- ENDIF
- ENDIF
- Rem read the next line of input, up to 254 chars
- ret%=IOREAD(handle%,ADDR(txt$)+1,255)
- IF ret%<0 Rem an error of some sort
- IF ret%<>-36 Rem -36 merely indicates EOF
- _Err:(i%,ret%) Rem another error, display
- ENDIF
- GOTO Endfile::
- ENDIF
- POKEB ADDR(txt$),ret% Rem set length of input
- chars&=chars&+ret%+2 Rem + 0D + 0A
- 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&,10)+" line(s), "+NUM$(words&,10)+" word(s), "
- IF handle%<>_in% Rem only close input file
- ret%=IOCLOSE(handle%) Rem if it's not SHin%
- txt$=txt$+NUM$(size&,10)+" chars "+PrPATH$:(d$)
- ELSE
- txt$=txt$+NUM$(chars&,10)+" chars"
- ENDIF
- fprint%:(txt$) Rem use fprint% so output can be
- ENDIF Rem redirected
- ENDIF
- i%=i%+1
- ENDWH
- RETURN
- ErrTrap::
- ONERR off
- PRINT err$:(ERR)
- quit:: Rem called on escape key, also on an error
- IF handle% AND handle%<>_in%
- IOCLOSE(handle%)
- ENDIF
- RETURN
- ENDP
-