home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / filutl / gf10.lbr / GF.ZZ0 / GF.Z80
Text File  |  1989-02-28  |  7KB  |  245 lines

  1. ;
  2. ;**********************************************************************
  3. ; GF is a generalized file filter program for ZCPR systems.
  4. ;
  5. ; Purpose: GF allows the user to change any single character from an
  6. ;           input file to any other character in the resulting an output 
  7. ;           file.
  8. ;
  9. ; Author: Dave Ramsey                     Date: December 12, 1988
  10. ;
  11. ; Assembler Used: SLR's Z80ASM
  12. ;
  13. ;**********************************************************************
  14. ;
  15. ;PDL for Filter follows:
  16. ;
  17. ;Begin FILTER
  18. ;    Get ptr to cmd line
  19. ;    Parse cmd line
  20. ;    If number args = 4 then
  21. ;        open input file (arg[1])
  22. ;        if open error then
  23. ;            abort with message "can't open input file" arg[1]
  24. ;        endif
  25. ;        open output file (arg[2])
  26. ;        target = control(arg[3])
  27. ;        replacement = control(arg[4])
  28. ;        while not eof(input file) do
  29. ;            inchar = getnextchar(input file)
  30. ;            if inchar = target then
  31. ;                inchar = replacement
  32. ;            endif
  33. ;            putnextchar(output file)
  34. ;        end while
  35. ;    else
  36. ;        show help
  37. ;    endif
  38. ;end FILTER
  39. ;
  40. ;
  41. ;**********************************************************************
  42. ;
  43. VERSION    equ    10        ;defines current version of GF
  44. FCB0    equ    5Ch
  45. FCB1    equ    6Ch
  46. BDOSEV    equ    0005h        ;BDOS entry vector
  47. BOOTEV    equ    0000h        ;system warm start entry vector
  48. CR    equ    0Dh        ;carriage return = ASCII char 13
  49. LF    equ    0Ah        ;line feed = ASCII char 10
  50. TBUFF    equ    080h
  51. ;
  52. ;
  53.     ext    print,fi0$open,f0$get,fo1$open,f1$put,fi0$close,fo1$close
  54.     ext    caps,z3init,argv,pstr,cout,crlf
  55. ;
  56. ;
  57. main:
  58.     jp    start
  59.     db    'Z3ENV'
  60. z3eadr    ds    2
  61. ;
  62. start:
  63.     ld    hl,z3eadr
  64.     call    z3init        ;initialize Z3 environment
  65. ;
  66. ; setup program stack here...
  67. ;
  68.     ld    hl,0
  69.     add    hl,sp        ;save current sp
  70.     ld    (oldsp),hl
  71.     ld    hl,BDOSEV+1    ;get BDOS address...
  72.     ld    a,h
  73.     sub    a,8        ;our stack = BDOS - 2K (to save CCP)
  74.     ld    h,a        ;set page address of stack
  75.     xor    a
  76.     ld    l,a        ;put on a page boundary
  77. ;
  78. ; check for help request
  79. ;
  80.     ld    a,(FCB0+1)
  81.     cp    a,'/'
  82.     jr    z,help
  83.     cp    a,' '
  84.     jr    nz,start2
  85. help:
  86.     ld    hl,usage
  87.     call    pstr
  88.     jp    fini2
  89. ;
  90. ; parse the command tail...
  91. ;
  92. start2:
  93.     ld    de,argtbl
  94.     ld    hl,TBUFF+1    ;cmd tail is in TBUFF
  95.     xor    a        ;do not NULL terminate args
  96.     call    argv
  97.     jr    z,start3    ;if parsing error occurred, show help
  98.     ld    hl,parserr
  99.     call    pstr
  100.     jp    fini2
  101. start3:
  102.     ld    a,(argtbl+1)
  103.     cp    a,4
  104.     jr    z,openin    ;if wrong # of args, show help
  105.     ld    hl,argcount
  106.     call    pstr
  107.     jp    fini2
  108. ;
  109. ; open input file specified in arg #1
  110. ;
  111. openin:
  112.     ld    de,FCB0
  113.     call    fi0$open    ;open file in infcb for input
  114.     jp    z,openout    ;if ok, goto start
  115.     call    print        ;else print error msg and abort
  116.     db    'Input file not found.',0
  117.     jp    fini2
  118. ;
  119. ; open output file specified in arg #2
  120. ;
  121. openout:
  122.     ld    de,FCB1
  123.     call    fo1$open    ;open file in outfcb for output
  124.     jp    z,getargs
  125.     call    print
  126.     db    'Unable to open specified output file.',0
  127.     jp    fini1
  128. getargs:
  129. ;
  130. ; here we would set values from cmd line for target and replacement
  131. ; chars...
  132. ;
  133.     
  134.     ld    hl,(arg3)    ;point at 3rd cmd line arg
  135.     ld    a,(hl)
  136.     call    control        ;convert search arg
  137.     ld    (oldchar),a    ;save converted char
  138. ;
  139.     ld    hl,(arg4)    ;point at 4th cmd line arg
  140.     ld    a,(hl)
  141.     call    control        ;convert replacement arg
  142.     ld    (newchar),a    ;save converted char
  143. loop:                ;while not eof do
  144.     call    f0$get        ; get next input byte
  145.     jp    nz,finish    ; if not zero, error - process error
  146.     ld    hl,oldchar
  147.     cp    a,(hl)        ; input byte = search char?
  148.     jr    nz,loop2    ; if no, continue processing
  149.     ld    a,(newchar)    ; else replace with new char
  150. ;
  151. loop2:    call    f1$put        ; put next byte in output file
  152.     jr    loop        ;endwhile
  153. ;
  154. finish:
  155.     call    fo1$close    ;close output file
  156. fini1:
  157.     call    fi0$close    ;close input file
  158. fini2:    ld    hl,(oldsp)    ;get old stack pointer
  159.     ld    sp,hl        ;restore BDOS stack pointer
  160.     ret            ;return to CCP
  161. ;
  162. ;****************************************************************
  163. ;
  164. ; CONTROL routine: returns the cmd line arg pointed to by HL
  165. ;    as a character in A. Converts cmd line string ^c to the
  166. ;    equivalent control character.
  167. ;
  168. ;Begin CONTROL
  169. ;    temp = charptr passed in from caller
  170. ;    if *charptr == '^' then
  171. ;        ++charptr
  172. ;        convert to uppercase(*charptr)
  173. ;     if *charptr == `^` then
  174. ;            temp = *charptr
  175. ;        else
  176. ;            temp = *charptr - 64  /* convert to control */
  177. ;        endif
  178. ;    else if *charptr == '~' then
  179. ;        ++charptr
  180. ;        if *charptr == '~' then
  181. ;            temp = *charptr
  182. ;        else
  183. ;            temp = *charptr + 32  /* convert to lowercase */
  184. ;    endif
  185. ;    return temp
  186. ;end CONTROL
  187. ;
  188. ;****************************************************************
  189. ;
  190. control:
  191.     ld    a,(hl)        ;get initial arg
  192.     cp    a,'^'        ;is it control trigger?
  193.     jr    nz,contr2    ;if not, check for lowercase flag
  194.     inc    hl
  195.     ld    a,(hl)        ;else get next byte in arg
  196.     cp    a,'^'        ;trap '^^' sequence...
  197.     jr    z,contr3    ;let caller use '^' char...
  198.     call    caps        ;else capitalize it, and then
  199.     sub    a,040h        ;subtract 64 to get ASCII ctrl code
  200.     ret
  201. ;
  202. ; check here for lowercase trigger...
  203. contr2:
  204.     cp    a,'~'        ;`~` is our lowercase flag
  205.     jr    nz,contr3    ;if it's not `~` it's just a plain char
  206.     inc    hl
  207.     ld    a,(hl)
  208.     cp    a,'~'        ;trap `~~` sequence and let caller use 
  209.     jr    z,contr3    ;the `~` char...
  210.     add    a,020h        ;add 32 to make it lowercase
  211. contr3:
  212.     ret            ;return from control char conversion
  213. ;
  214. ; data areas:
  215. ;
  216. usage:    db    'GF, Version ',VERSION/10+'0','.',VERSION MOD 10+'0'
  217.     db    ' - A Generalized Filter.',CR,LF
  218.     db    'Syntax:',CR,LF
  219.     db    '        GF du:fn1 du:fn2 oldch newch',CR,LF
  220.     db    '   Where:',CR,LF
  221.     db    '       du:fn1     is the input file spec;',CR,LF
  222.     db    '       du:fn2     is the output file spec;',CR,LF
  223.     db    '       oldchar    is the search character; and',CR,LF
  224.     db    '       newchar    is the replacement character.',CR,LF,
  225.     db    '   A ^ character before either oldchar or newchar',CR,LF
  226.     db    '   tells GF to convert it to a control character.',CR,LF
  227.     db    '   A ~ character before either oldchar or newchar',CR,LF
  228.     db    '   tells GF to convert it to lowercase.',CR,LF
  229.     db    '   i.e. ^M is a carriage return and ~M is an "m"',CR,LF,LF,0
  230. ;
  231. parserr    db    'Parsing error!',CR,LF,0
  232. argcount db    'Incorrect number of arguments!',CR,LF,0
  233. ;
  234. oldsp    ds    2
  235. oldchar    ds    1
  236. newchar    ds    1
  237. argtbl    db    4    ;max number of args expected
  238.     ds    1    ;number of args actually found
  239.     ds    2    ;pointer to arg1
  240.     ds    2    ;pointer to arg2
  241. arg3    ds    2    ;pointer to arg3
  242. arg4    ds    2    ;pointer to arg4
  243. ;
  244.     end
  245.