home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 283 / Shell5SourceCode.sis / wc.txt < prev    next >
Encoding:
Text File  |  1998-08-30  |  3.3 KB  |  109 lines

  1. Rem   wc, an addon command for Shell5
  2. Rem   Display the number of lines, words and character in a file
  3. Rem
  4. Rem   Copyright (C) 1998  Nick Murray
  5. Rem
  6. Rem   This program is free software; you can redistribute it and/or
  7. Rem   modify it under the terms of the GNU General Public License
  8. Rem   as published by the Free Software Foundation; either version 2
  9. Rem   of the License, or (at your option) any later version.
  10. Rem
  11. Rem   This program is distributed in the hope that it will be useful,
  12. Rem   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. Rem   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. Rem   See the GNU General Public License for more details.
  15. Rem
  16. Rem   You should have received a copy of the GNU General Public License
  17. Rem   along with this program; if not, write to the Free Software Foundation,
  18. Rem   Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. Rem
  20. PROC wc%:(n%)
  21. LOCAL i%,ret%,d$(255),handle%,txt$(255),p%
  22. LOCAL lines&,words&,chars&,size&
  23.     ONERR ErrTrap::
  24.     IF n%<2 AND _in%=0    Rem no args and no redirected input
  25.         PRINT "Usage: wc <filename>"
  26.         RETURN
  27.     ENDIF
  28.     i%=2        Rem argv&(2) is the FIRST argument after the comman d
  29.     IF _in%                Rem input is redirected, so set the file
  30.         handle%=_in%    Rem handle to SHin% and jump over the
  31.         GOTO loop::        Rem file handling routines.
  32.     ENDIF
  33.     WHILE i%<=n%        Rem whilst we have more arguments
  34.         Rem parse the i% th argument to check if it's a valid file
  35.         ret%=Fparse%:(ADDR(d$),PEEK$(argv&(i%)))
  36.         IF ret%<0                    Rem error opening this file
  37.             _Err:(i%,ret%)
  38.         ELSEIF ret% AND 16    Rem this is a directory
  39.             _Err:(i%,3)
  40.         ELSE
  41.             ret%=IOOPEN(handle%,d$,$0600)        Rem random access opening first
  42.             IF ret%
  43.                 _Err:(i%,ret%)
  44.             ENDIF
  45.             IOSEEK(handle%,2,size&)                                Rem get size
  46.             IOCLOSE(handle%)
  47.             handle%=0
  48.             REM open=$0000, text=$0020, share=$0400
  49.             ret%=IOOPEN(handle%,d$,$0420)
  50.             IF ret%
  51.                 _Err:(i%,ret%)
  52.             ELSE
  53.                 lines&=0            Rem initialize counters
  54.                 words&=0
  55.                 chars&=0
  56. loop::        WHILE 1
  57.                     IOYIELD
  58.                     IF _stat%<>-46
  59.                         IF _key%(1)=27
  60.                             GOTO Quit::
  61.                         ELSE
  62.                             KEYA(_stat%,_key%())
  63.                         ENDIF
  64.                     ENDIF
  65.                     Rem read the next line of input, up to 254 chars
  66.                     ret%=IOREAD(handle%,ADDR(txt$)+1,255)
  67.                     IF ret%<0                    Rem an error of some sort
  68.                         IF ret%<>-36        Rem -36 merely indicates EOF
  69.                             _Err:(i%,ret%)    Rem another error, display
  70.                         ENDIF
  71.                         GOTO Endfile::
  72.                     ENDIF
  73.                     POKEB ADDR(txt$),ret%        Rem set length of input
  74.                     chars&=chars&+ret%+2        Rem + 0D + 0A
  75.                     lines&=lines&+1
  76.                     WHILE LEN(txt$)
  77.                         p%=LOC(txt$," ")
  78.                         IF p%>1    Rem not blank first
  79.                             words&=words&+1
  80.                         ELSEIF p%=0 AND LEN(txt$)
  81.                             words&=words&+1
  82.                             BREAK
  83.                         ENDIF
  84.                         txt$=RIGHT$(txt$,LEN(txt$)-p%)
  85.                     ENDWH
  86.                 ENDWH
  87. EndFile::    txt$=num$(lines&,10)+" line(s), "+NUM$(words&,10)+" word(s), "
  88.                 IF handle%<>_in%                Rem only close input file
  89.                     ret%=IOCLOSE(handle%)        Rem if it's not SHin%
  90.                     txt$=txt$+NUM$(size&,10)+" chars  "+PrPATH$:(d$)
  91.                 ELSE
  92.                     txt$=txt$+NUM$(chars&,10)+" chars"
  93.                 ENDIF
  94.                 fprint%:(txt$)        Rem use fprint% so output can be
  95.             ENDIF                        Rem redirected
  96.         ENDIF
  97.         i%=i%+1
  98.     ENDWH
  99.     RETURN
  100. ErrTrap::
  101.     ONERR off
  102.     PRINT err$:(ERR)
  103. quit::    Rem called on escape key, also on an error
  104.     IF handle% AND handle%<>_in%
  105.         IOCLOSE(handle%)
  106.     ENDIF
  107.     RETURN
  108. ENDP
  109.