; ************************** AMUS Program Label ******************************
; Filename: DOCSIZ.M68                                      Date: 01/23/90
; Category: UTIL         Hash Code: 621-607-334-122      Version: 1.0(101)
; Initials: ULTR/AM      Name: DAVID PALLMANN
; Company: ULTRASOFT CORPORATION                   Telephone #: 5163484848
; Related Files:
; Min. Op. Sys.: AMOS/L 1.0                    Expertise Level: BEG
; Special:
; Description: Shows characters, words, lines, and pages in a document.  Can
; process AlphaWRITE, SuperVue, and text files.
; ****************************************************************************

;****************************************************************************
;*                                                                          *
;*                                 DOCSIZ                                   *
;*         Report various statistics about the size of a document           *
;*                                                                          *
;****************************************************************************
;Copyright (C) 1990 UltraSoft Corp.  All Rights Reserved.
;
;Written by: David Pallmann
;
;Edit History:
;1.0(100)  22-Jan-90  created. /DFP
;1.0(101)  22-Jan-90  ignore AlphaWRITE control sequences. /DFP

       VMAJOR  =1
       VMINOR  =0
       VSUB    =0
       VEDIT   =101.
       VWHO    =0

       SEARCH  SYS
       SEARCH  SYSSYM

;ASCII character definitions

       $LF     =12                     ; line feed
       $FF     =14                     ; form feed
       $CR     =15                     ; carriage return

;AlphaWRITE 2.0 command byte definitions

       C$CTL   =^H0F2                  ; start of control sequence
       C$CTE   =^H0F3                  ; end of control sequence
       C$PAG   =^H0F5                  ; Page break (hard, inserted by user)
       C$BRK   =^H0F6                  ; Page break (soft, inserted by WRITE)
       C$CHP   =^H0F7                  ; Page break (new chapter)

;variables

       .OFINI
       .OFDEF  FILE,   D.DDB           ; file channel
       .OFDEF  PAGES,  4               ; page count
       .OFDEF  LINES,  4               ; line count
       .OFDEF  WORDS,  4               ; word count
       .OFDEF  CHARS,  4               ; character count
       .OFDEF  INWORD, 1               ; in-a-word flag
       .OFDEF  EXTRA,  1               ; *unused*
       .OFSIZ  MEMSIZ

;start of program

START:  PHDR    -1,0,PH$REE!PH$REU      ; program header
       GETIMP  MEMSIZ,A5               ; allocate memory for variables
       INIT    FILE(A5)                ;

;command line processing

CMDLIN: BYP                             ;
       LIN                             ;
       JEQ     USAGE                   ;
       FSPEC   FILE(A5),ZZZ            ;
       CMPW    FILE+D.EXT(A5),#[ZZZ]   ;
       BNE     OPEN                    ;
       LEA     A0,PATH                 ;
10$:    MOVW    (A0)+,FILE+D.EXT(A5)    ;
       JEQ     NOT.FOUND               ;
       LOOKUP  FILE(A5)                ;
       BNE     10$                     ;

;open file for processing

OPEN:   OPENI   FILE(A5)                ;
       CALL    BYPHDR                  ; bypass header if there is one

;set up for processing

SETUP:  MOV     #1,PAGES(A5)            ; always at least one page
       CLR     LINES(A5)               ; clear line count
       CLR     WORDS(A5)               ; clear word count
       CLR     CHARS(A5)               ; clear character count

;get a character and act upon it

GET:    CTRLC   CLOSE                   ; ^C check
       FILINB  FILE(A5)                ; get a byte
       TST     FILE+D.SIZ(A5)          ; end of file?
       JEQ     CLOSE                   ;   yes

;if an AlphaWRITE document, bypass control sequences

BYPASS: CMPW    FILE+D.EXT(A5),#[WRT]   ;
       BNE     20$                     ;
       CMPB    D1,#C$CTL               ;
       BNE     20$                     ;
10$:    FILINB  FILE(A5)                ;
       TST     FILE+D.SIZ(A5)          ;
       JEQ     CLOSE                   ;
       CMPB    D1,#C$CTE               ;
       BNE     10$                     ;
       BR      GET                     ;
20$:

PROCES: INC     CHARS(A5)               ; update character count
       CALL    UPDWRD                  ; update word count
       CALL    UPDLIN                  ; update line count
       CALL    UPDPAG                  ; update page count
       BR      GET                     ;

;close file

CLOSE:  CLOSE   FILE(A5)                ;

;report our findings

REPORT: MOV     CHARS(A5),D1            ;
       DCVT    0,OT$TRM!OT$TSP         ;
       TYPE    <character>             ;
       CALL    PLURAL                  ;
       TYPE    <, >                    ;
       MOV     WORDS(A5),D1            ;
       DCVT    0,OT$TRM!OT$TSP         ;
       TYPE    <word>                  ;
       CALL    PLURAL                  ;
       TYPE    <, >                    ;
       MOV     LINES(A5),D1            ;
       DCVT    0,OT$TRM!OT$TSP         ;
       TYPE    <line>                  ;
       CALL    PLURAL                  ;
       TYPE    <, >                    ;
       MOV     PAGES(A5),D1            ;
       DCVT    0,OT$TRM!OT$TSP         ;
       TYPE    <page>                  ;
       CALL    PLURAL                  ;
       CRLF                            ;
       CRLF                            ;

EXIT:   EXIT                            ;

USAGE:  TYPECR  Document size utility
       TYPECR  Copyright (C) 1990 UltraSoft Corporation.  All Rights Reserved.
       CRLF                            ;
       TYPECR  Usage: .DOCSIZ docname  ;
       CRLF                            ;
       TYPECR  <Compatible with AlphaWRITE, SuperVUE, and text files.>
       CRLF                            ;
       EXIT                            ;

NOT.FOUND:
       TYPECR  Document not found - please try again and specify an extension
       CRLF                            ;
       EXIT                            ;

;************
;*  BYPHDR  *
;************
;Function:      Bypass header if document has one
;
;Inputs:        FILE+D.EXT(A5) - extension of file

BYPHDR: CMPW    FILE+D.EXT(A5),#[WRT]   ; AlphaWRITE document?
       BNE     20$                     ;   no
       MOV     #508.,D0                ; load loop count
10$:    FILINB  FILE(A5)                ; get and discard header byte
       SOB     D0,10$                  ; loop
20$:    RTN                             ; return

;************
;*  UPDWRD  *
;************
;Function:      Update word count
;
;Inputs:        D1        - character from file
;
;Outputs:       WORDS(A5) - updated

UPDWRD: CALL    WRDCHR                  ; is this a word character?
       BEQ     INAWRD                  ;   yes

;we are not in a word

ENDWRD: TSTB    INWORD(A5)              ; were we in a word?
       BEQ     UW.RTN                  ;   yes
       INC     WORDS(A5)               ;
       CLRB    INWORD(A5)              ;
       BR      UW.RTN                  ;

;we are in a word

INAWRD: MOVB    #1,INWORD(A5)           ; set the in-a-word flag

UW.RTN: RTN                             ; return

;************
;*  WRDCHR  *
;************
;Function:      Indicate whether or not we have a word character
;
;Inputs:        D1 - character
;
;Outputs:        Z - set if character is a word character

WRDCHR: CMPB    D1,#'A                  ;
       BLO     WC.RTN                  ;
       CMPB    D1,#'Z                  ;
       BLOS    WC.YES                  ;
       CMPB    D1,#'a                  ;
       BLO     WC.RTN                  ;
       CMPB    D1,#'z                  ;
       BHI     WC.RTN                  ;

WC.YES: LCC     #PS.Z                   ;
       RTN                             ;

WC.RTN: LCC     #0                      ;
       RTN                             ;

;************
;*  UPDLIN  *
;************
;Function:      Update line count if we are at the end of a line
;
;Inputs:        D1             - character read from file
;               FILE+D.EXT(A5) - extension of file
;
;Outputs:       LINES(A5)      - updated

UPDLIN: MOVW    FILE+D.EXT(A5),D7       ; get extension
       CMPW    D7,#[WRT]               ; AlphaWRITE document?
       BEQ     UL.WRT                  ;   yes
       CMPW    D7,#[T  ]               ; SuperVUE document?
       BEQ     UL.SV                   ;   yes

UL.ASC: CMPB    D1,#$LF                 ; line feed?
       BEQ     UL.INC                  ;   yes - increment line count
       RTN                             ; return

UL.WRT: TSTB    D1                      ; AlphaWRITE end of line?
       BEQ     UL.INC                  ;   yes - increment line count
       RTN                             ; return

UL.SV:  CMPB    D1,#$CR                 ; SuperVUE end of line?
       BEQ     UL.INC                  ;   yes - increment line count
       RTN                             ; return

UL.INC: INC     LINES(A5)               ; increment line count
       CLRB    INWORD(A5)              ; clear "in a word" flag
       RTN                             ; return

;************
;*  UPDPAG  *
;************
;Function:      Update page count if we are at the end of a page
;
;Inputs:        D1             - character read from file
;               FILE+D.EXT(A5) - extension of file
;
;Outputs:       PAGES(A5)      - updated

UPDPAG: MOVW    FILE+D.EXT(A5),D7       ; get extension
       CMPW    D7,#[WRT]               ; AlphaWRITE document?
       BEQ     UP.WRT                  ;   yes

UP.ASC: CMPB    D1,#$FF                 ; form feed?
       BEQ     UP.INC                  ;   yes - increment page count
       RTN                             ; return

UP.WRT: CMPB    D1,#C$BRK               ; AlphaWRITE soft page break?
       BEQ     UP.INC                  ;   yes - increment page count
       CMPB    D1,#C$PAG               ; AlphaWRITE hard page break?
       BEQ     UP.INC                  ;   yes - increment page count
       CMPB    D1,#C$CHP               ; AlphaWRITE chapter page break?
       BEQ     UP.INC                  ;   yes - increment page count
       RTN                             ; return

UP.INC: INC     PAGES(A5)               ; increment page count
       CLRB    INWORD(A5)              ; clear "in a word" flag
       RTN                             ; return

PLURAL: CMP     D1,#1                   ;
       BEQ     10$                     ;
       TYPE    s                       ;
10$:    RTN                             ;

;table of extensions to look for if user doesn't specify a full filespec

PATH:   RAD50   /WRT/                   ; AlphaWRITE document
       RAD50   /T  /                   ; SuperVUE document
       RAD50   /TXT/                   ; text file
       RAD50   /LST/                   ; listing
       WORD    0                       ; ** end of table **

       END