home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PSION / UTILS / SHELL3A / SRC.ZIP / OPL / WC.OPL < prev   
Encoding:
Text File  |  1996-09-03  |  2.4 KB  |  88 lines

  1. Rem ****************************************
  2. Rem * Shell3a external command - wc.opl
  3. Rem * ╕ Nick Murray August 1996
  4. Rem *
  5. Rem * wc - Display the number of lines,
  6. Rem *     words and character in a file
  7. Rem ****************************************
  8. PROC wc%:(n%)
  9. LOCAL i%,ret%,d$(128),handle%,txt$(255),p%
  10. LOCAL lines%,words%,chars%
  11.     ONERR ErrTrap::
  12.     IF n%<2 AND SHin%=0    Rem no args and no redirected input
  13.         PRINT "Usage: wc <filename>"
  14.         RETURN
  15.     ENDIF
  16.     i%=2        Rem argv%(2) is the FIRST argument after the comman d
  17.     IF SHin%                Rem input is redirected, so set the file
  18.         handle%=SHin%    Rem handle to SHin% and jump over the
  19.         GOTO loop::        Rem file handling routines.
  20.     ENDIF
  21.     WHILE i%<=n%        Rem whilst we have more arguments
  22.         Rem parse the i% th argument to check if it's a valid file
  23.         ret%=Fparse%:(ADDR(d$),PEEK$(argv%(i%)))
  24.         IF ret%<0                    Rem error opening this file
  25.             ShErr:(i%,ret%)
  26.         ELSEIF ret% AND 16    Rem this is a directory
  27.             ShErr:(i%,3)
  28.         ELSE
  29.             REM open=$0000, text=$0020, share=$0400
  30.             ret%=IOOPEN(handle%,d$,$0420)
  31.             IF ret%<0
  32.                 ShErr:(i%,ret%)
  33.             ELSE
  34.                 lines%=0            Rem initialize counters
  35.                 words%=0
  36.                 chars%=0
  37. loop::        WHILE 1
  38.                     IF KEY=27    Rem check for escape key
  39.                         GIPRINT ERR$(-114)
  40.                         SHesc%=1
  41.                         GOTO quit::
  42.                     ENDIF
  43.                     Rem read the next line of input, up to 254 chars
  44.                     ret%=IOREAD(handle%,UADD(ADDR(txt$),1),255)
  45.                     IF ret%<0                    Rem an error of some sort
  46.                         IF ret%<>-36        Rem -36 merely indicates EOF
  47.                             ShErr:(i%,ret%)    Rem another error, display
  48.                         ENDIF
  49.                         GOTO Endfile::
  50.                     ENDIF
  51.                     POKEB ADDR(txt$),ret%        Rem set length of input
  52.                     chars%=chars%+ret%+2    Rem EOL 10 and 13
  53.                     lines%=lines%+1
  54.                     WHILE LEN(txt$)
  55.                         p%=LOC(txt$," ")
  56.                         IF p%>1    Rem not blank first
  57.                             words%=words%+1
  58.                         ELSEIF p%=0 AND LEN(txt$)
  59.                             words%=words%+1
  60.                             BREAK
  61.                         ENDIF
  62.                         txt$=RIGHT$(txt$,LEN(txt$)-p%)
  63.                     ENDWH
  64.                 ENDWH
  65. EndFile::    txt$=num$(lines%,-6)+NUM$(words%,-6)+NUM$(chars%,-6)
  66.                 IF handle%<>SHin%                Rem only close input file
  67.                     ret%=IOCLOSE(handle%)    Rem if it's not SHin%
  68.                     IF ret%<0
  69.                         ShErr:(i%,ret%)
  70.                     ENDIF
  71.                     txt$=txt$+"  "+PrPATH$:(d$)
  72.                 ENDIF
  73.                 fprint%:(txt$)        Rem use fprint% so output can be
  74.             ENDIF                        Rem redirected
  75.         ENDIF
  76.         i%=i%+1
  77.     ENDWH
  78.     RETURN
  79. quit::    Rem called on escape key
  80.     IF handle%<>SHin%
  81.         IOCLOSE(handle%)
  82.     ENDIF
  83.     RETURN
  84. ErrTrap::
  85.     ONERR off
  86.     PRINT err$:(ERR)
  87. ENDP
  88.