home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol165 / fynde.asm < prev    next >
Encoding:
Assembly Source File  |  1984-07-09  |  10.8 KB  |  726 lines

  1.  
  2.  
  3. ;    ----------------------------------------------------------
  4. ;    Reassembly of FIND.COM, which was originally extracted
  5. ;    from the CBBS(R) package available from Ward Christensen
  6. ;    and Randy Suess. However, considerable rearrangement has
  7. ;    taken place, most notably the following:
  8. ;        <no file name> results in a screen of information
  9. ;        the search pattern may be a regular expression
  10. ;        label+line number as an alternative to line number
  11. ;        instance count reported, both per file and globally
  12. ;    To achieve compatibility with MicroShell the vertical bar
  13. ;    was replaced by exclamation point; all syntactic elements
  14. ;    are defined by EQU's and may be redefined. LABEL!PATTERN
  15. ;    is checked for balanced parentheses and non-null arguments
  16. ;    to forestall the most common failure modes. Had MicroShell
  17. ;    not been available, an option to direct the output to some
  18. ;    disk file would probably have been included.
  19. ;
  20. ;            FYNDE.ASM   Copyright (C) 1982
  21. ;            Universidad Autonoma de Puebla
  22. ;
  23. ;    [Harold V. McIntosh, 20 December 1982]
  24. ;    ----------------------------------------------------------
  25.  
  26. HT    equ    09H    ;horizontal tab
  27. LF    equ    0AH    ;line feed
  28. CR    equ    0DH    ;carriage return
  29. KZ    equ    1AH    ;^Z
  30.  
  31. ;    Delimiters for the command line
  32.  
  33. LSQ    equ    '['    ;begin alternative list
  34. RSQ    equ    ']'    ;end alternative list
  35. LBR    equ    '{'    ;begin iterated expression
  36. RBR    equ    '}'    ;end iterated expression
  37. ORR    equ    '!'    ;separate alternatives
  38.  
  39. ;    Representatives of characters or classes.
  40.  
  41. TAB    equ    '_'    ;substitute for tab
  42. QUE    equ    '?'    ;represent any byte
  43. ALF    equ    '@'    ;represent any alphanumeric
  44.  
  45. ;    CPM locations and parameters
  46.  
  47. cfcb    equ    005CH    ;CP/M's file control block
  48. cblk    equ    007CH    ;CP/M's block counter
  49. csiz    equ    0080H    ;CP/M's record size
  50. cbuf    equ    0080H    ;CP/M's record buffer
  51. ksiz    equ    26    ;sector capacity of IN buffer
  52. isiz    equ    ksiz*128
  53.  
  54.     org    100H
  55.  
  56. X0100:    lxi    sp,stak
  57.     lda    cfcb+1    ;file name
  58.     cpi    ' '
  59.     jnz    X0143
  60.     lxi    h,M1
  61.     call    mssg
  62.     jmp    0000
  63.  
  64. X0143:    lxi    h,M2
  65.     call    mssg
  66.     mvi    c,12
  67.     lxi    d,cfcb
  68.     lxi    h,file
  69.     call    miuc
  70.     xra    a
  71.     sta    enth
  72.     lxi    h,cbuf    ;CP/M's record buffer
  73.     mov    e,m
  74.     mov    c,e
  75.     mov    d,a
  76.     xchg
  77.     inx    d
  78.     dad    d
  79.     mov    m,a
  80.     xchg
  81. X0152:    inx    h
  82.     dcr    c
  83.     mov    a,m
  84.     ora    a
  85.     jz    X029C
  86.     cpi    ' '
  87.     jnz    X0152
  88.     inx    h
  89.     call    bala    ;check balance of [], {}.
  90.     call    nula    ;check for null alternatives
  91.     lxi    d,patt    ;command line pattern
  92.     call    muve
  93.     mvi    c,4
  94.     lxi    d,lzer
  95.     lxi    h,dtot
  96.     call    miuc
  97.     lxi    h,0000
  98.     shld    lapo
  99.     lxi    h,patt    ;command line pattern
  100.     shld    papo
  101.     call    next
  102.     ora    a
  103.     jz    scan
  104.     shld    papo
  105.     lxi    h,patt
  106.     shld    lapo
  107.  
  108. ;    Scan the directory for file names.
  109.  
  110. scan:    mvi    c,26    ;(1A) set DMA address
  111.     lxi    d,cbuf    ;CP/M's record buffer
  112.     call    0005    ; - B D O S -
  113.     mvi    c,17    ;(11) search once
  114.     lxi    d,file
  115.     call    0005    ; - B D O S -
  116.     lxi    h,enth
  117.     inr    m
  118.     mov    c,m
  119. fnth:    inr    a
  120.     jz    done    ;we're all done
  121.     dcr    c
  122.     jz    this
  123.     push    b
  124.     mvi    c,18    ;(12) search again
  125.     lxi    d,file
  126.     call    0005    ; - B D O S -
  127.     pop    b
  128.     jmp    fnth
  129.  
  130. ;    We're all done.
  131.  
  132. done:    lxi    h,dtot
  133.     call    mssg
  134.     jmp    0000
  135.  
  136. ;    A prospective file has been located
  137.  
  138. this:    dcr    a
  139.     ani    03
  140.     add    a
  141.     add    a
  142.     add    a
  143.     add    a
  144.     add    a
  145.     adi    81H
  146.     mov    e,a
  147.     mvi    d,00
  148.     mvi    c,12
  149.     lxi    h,cfcb+1
  150.     call    miuc
  151.     mvi    c,15    ;(0F) open file
  152.     lxi    d,cfcb    ;CP/M's FCB
  153.     call    0005    ; - B I O S -
  154.     inr    a
  155.     jz    0000    ;quit [without message]
  156.     xra    a
  157.     sta    cblk    ;block pointer
  158.  
  159.     mvi    c,4
  160.     lxi    d,lzer
  161.     lxi    h,lnum    ;'line number'
  162.     call    miuc
  163.     mvi    c,4
  164.     lxi    d,lzer
  165.     lxi    h,ftot    ;'file total'
  166.     call    miuc
  167.     mvi    c,8
  168.     lxi    d,cfcb+1    ;file name
  169.     lxi    h,fnam    ;'file name'
  170.     call    miuc
  171.     mvi    c,3
  172.     lxi    d,0065H    ;extension
  173.     lxi    h,fext    ;'file extension'
  174.     call    miuc
  175.     lxi    h,fhed
  176.     call    mssg    ;message to console
  177.     lxi    b,2006H    ;six spaces
  178.     lxi    h,llbl
  179.     call    fiuc
  180.  
  181. X01C2:    lxi    h,0000
  182.     shld    ictr
  183. X01C8:    lxi    h,lnum+3    ;increment l.c.
  184.     call    inco    ;increment line counter
  185.     lxi    h,lbuf    ;line buffer
  186.     mvi    b,0FFH
  187. X01E0:    inr    b
  188.     jm    X01FD
  189.     push    b
  190.     push    h
  191.     call    inch    ;char from big bffr to line bffr
  192.     pop    h
  193.     pop    b
  194.     mov    m,a
  195.     inx    h
  196.     cpi    KZ
  197.     jnz    X01E8
  198.     lxi    h,ftot
  199.     call    mssg
  200.     jmp    scan
  201. X01E8:    cpi    LF
  202.     jnz    X01E0
  203.     jmp    X0202
  204.  
  205. X01FD:    mvi    m,CR
  206.     inx    h
  207.     mvi    m,LF
  208.     inx    h
  209.  
  210. ;    Check console for termination request. If one
  211. ;    is present, clear it out before leaving.
  212.  
  213.  
  214. X0202:    mvi    m,00    ;guarantee right hand fence
  215.     mvi    c,11    ;(0B) console status
  216.     call    0005    ; - B I O S -
  217.     ora    a
  218.     jz    culi
  219.     mvi    c,1    ;(01) read console
  220.     call    0005    ; - B D O S -
  221.     lxi    h,M4    ;"search terminated"
  222.     call    mssg
  223.     jmp    0000
  224.  
  225. ;    Scan the current line.
  226. ;    First see if it is labelled.
  227.  
  228. culi:    lhld    lapo
  229.     mov    a,h
  230.     ora    l
  231.     jz    X0217    ;no label requested
  232.     xchg
  233.     lxi    h,lbuf
  234.     call    chek
  235.     jnz    X0217    ;label not found
  236.     push    h
  237.     lxi    b,2006H    ;six spaces
  238.     lxi    h,llbl
  239.     call    fiuc
  240.     pop    h
  241.     lxi    d,llbl+5
  242.     mvi    c,6
  243. didl:    dcx    h
  244.     mov    a,m
  245.     cpi    HT    ;ignore tabs in text
  246.     jz    didl
  247.     cpi    ' '    ;quit at head of line
  248.     jc    dido
  249.     stax    d
  250.     dcx    d
  251.     dcr    c
  252.     jnz    didl
  253. dido:    mvi    c,4
  254.     lxi    d,lzer
  255.     lxi    h,lnum
  256.     call    miuc
  257.  
  258. ;    Now look for the pattern
  259.  
  260. X0217:    lxi    h,lbuf    ;line buffer
  261. X021A:    xchg
  262.     lhld    papo    ;pattern pointer
  263.     xchg
  264.     push    h
  265.     call    chek
  266.     pop    h
  267.     jz    X0263
  268.     inx    h
  269.     mov    a,m
  270.     cpi    CR
  271.     jnz    X021A
  272.     jmp    X01C8    ;increment l.c. at X026A
  273.  
  274. ;    Pattern matches, so type label & line containing it
  275.  
  276. X0263:    lxi    h,llbl    ;line label
  277.     call    mssg    ;message to console
  278.     lxi    h,lbuf    ;line buffer
  279.     call    mssg    ;message to console
  280.     lxi    h,ftot+3
  281.     call    inco
  282.     lxi    h,dtot+3
  283.     call    inco
  284.     jmp    X01C8    ;increment l.c. at X026A
  285.  
  286. ;    Increment ASCII counter at (HL-3).
  287.  
  288. inco:    mov    a,m
  289.     ori    30H
  290.     inr    a
  291.     mov    m,a
  292.     cpi    ':'
  293.     rnz
  294.     mvi    m,'0'
  295.     dcx    h
  296.     jmp    inco
  297.  
  298. ;    Memory to console
  299.  
  300. mssg:    mov    e,m
  301.     inx    h
  302.     push    h
  303.     mvi    c,2    ;(02) write console
  304.     call    0005    ; - B I O S -
  305.     pop    h
  306.     mov    a,m
  307.     ora    a
  308.     jnz    mssg
  309.     ret
  310.  
  311. X029C:    lxi    h,M3    ;"bad pattern"
  312.     call    mssg
  313.     jmp    0000
  314.  
  315. inch:    lhld    ictr
  316.     mov    a,h
  317.     ora    l
  318.     cz    indi    ;disk to IN area
  319.     lhld    ictr
  320.     dcx    h
  321.     shld    ictr
  322.     lhld    iptr
  323.     mov    a,m
  324.     cpi    KZ
  325.     rz
  326.     inx    h
  327.     shld    iptr
  328.     ret
  329.  
  330. indi:    mvi    b,ksiz
  331.     lxi    h,isiz
  332.     shld    ictr
  333.     lxi    h,ibuf
  334.     shld    iptr
  335. indd:    mvi    m,KZ
  336.     push    h
  337.     push    b
  338.     xchg
  339.     mvi    c,26    ;(1A) set DMA address
  340.     call    0005    ; - B D O S -
  341.     lxi    d,cfcb    ;CP/M's file control block
  342.     mvi    c,20    ;(14) read one record
  343.     call    0005    ; - B D O S -
  344.     pop    b
  345.     pop    h
  346.     ora    a
  347.     rnz
  348.     dcr    b
  349.     rz
  350.     lxi    d,csiz    ;CP/M's record size
  351.     dad    d
  352.     jmp    indd
  353.  
  354. ;    Advance to next alternative
  355.  
  356. nexx:    mov    e,m
  357.     inx    h
  358.     mov    d,m
  359.     xchg
  360. next:    mov    a,m
  361.     ora    a
  362.     rz
  363.     inx    h
  364.     call    enda
  365.     rz
  366.     call    begb
  367.     jz    nexx
  368.     jmp    next
  369.  
  370. fiuc:    mov    m,b
  371.     inx    h
  372.     dcr    c
  373.     jnz    fiuc
  374.     ret
  375.  
  376. miuc:    ldax    d
  377.     mov    m,a
  378.     inx    d
  379.     inx    h
  380.     dcr    c
  381.     jnz    miuc
  382.     ret
  383.  
  384. ;    Move and semi-compile the command line.
  385.  
  386. muve:    mov    a,m
  387.     cpi    TAB
  388.     jnz    munt
  389.     mvi    a,HT
  390. munt:    stax    d
  391.     inx    h
  392.     inx    d
  393.     cpi    RBR
  394.     jz    murb
  395.     cpi    RSQ
  396.     jz    murb
  397.     cpi    LBR
  398.     jz    mulb
  399.     cpi    LSQ
  400.     jz    mulb
  401. must:    dcr    c
  402.     jnz    muve
  403.     ret
  404.  
  405. murb:    xthl
  406.     mov    m,e
  407.     inx    h
  408.     mov    m,d
  409.     pop    h
  410.     jmp    must
  411.  
  412. mulb:    push    d
  413.     inx    d
  414.     inx    d
  415.     jmp    must
  416.  
  417. ;    Check balance of []'s and {}'s.
  418.  
  419. bala:    push    h
  420.     push    b
  421.     lxi    b,0101H
  422. balb:    mov    a,m
  423.     inx    h
  424.     cpi    LSQ
  425.     jnz    balc
  426.     inr    b
  427.     jmp    balb
  428. balc:    cpi    RSQ
  429.     jnz    bald
  430.     dcr    b
  431.     jz    balx
  432.     jmp    balb
  433. bald:    cpi    LBR
  434.     jnz    bale
  435.     inr    c
  436.     jmp    balb
  437. bale:    cpi    RBR
  438.     jnz    balf
  439.     dcr    c
  440.     jz    balx
  441.     jmp    balb
  442. balf:    ora    a
  443.     jnz    balb
  444.     mov    a,c
  445.     cpi    01
  446.     jnz    balx
  447.     mov    a,b
  448.     cpi    01
  449.     pop    b
  450.     pop    h
  451.     rz
  452. balx:    lxi    h,M3    ;"bad pattern"
  453.     call    mssg
  454.     jmp    0000    ;PDL unbalanced but doesn't matter
  455.  
  456. ;    Check for termination of alternative.
  457.  
  458. enda:    cpi    ORR
  459.     rz
  460. endb:    cpi    RSQ
  461.     rz
  462.     cpi    RBR
  463.     rz
  464.     ora    a
  465.     ret
  466.  
  467. ;    Check for beginning of alternative.
  468.  
  469. bega:    cpi    ORR
  470.     rz
  471. begb:    cpi    LSQ
  472.     rz
  473.     cpi    LBR
  474.     ret
  475.  
  476. ;    Check for null alternative.
  477.  
  478. nula:    push    h
  479.     call    nulb
  480.     pop    h
  481.     ret
  482. nulb:    mov    a,m
  483.     inx    h
  484.     ora    a
  485.     rz
  486.     call    bega
  487.     jnz    nulb
  488.     mov    a,m
  489.     call    enda
  490.     jnz    nulb
  491.     jmp    balx
  492.  
  493. ;    Check for given expression.
  494.  
  495. chek:    ldax    d
  496.     inx    d
  497.     call    enda
  498.     rz
  499.     mov    b,a
  500.     mov    a,m
  501.     cpi    CR
  502.     jz    chno
  503.     mov    a,b
  504.     cpi    LBR
  505.     jz    chlb
  506.     cpi    LSQ
  507.     jz    chsq
  508.     mov    c,m
  509.     inx    h
  510.     cpi    QUE
  511.     jz    chek
  512.     cpi    ALF
  513.     jz    chal
  514.     cmp    c
  515.     jz    chek
  516.     mov    b,a
  517.     mov    a,c
  518.     cpi    'a'
  519.     jc    chno
  520.     cpi    '{'
  521.     jnc    chno
  522.     ani    05FH
  523.     cmp    b
  524.     jz    chek
  525. chno:    ori    0FFH
  526.     ret
  527.  
  528. ;    Check alphanumeric.
  529.  
  530. chal:    mov    a,c
  531.     cpi    '0'
  532.     jc    chno
  533.     cpi    ':'
  534.     jc    chek
  535.     cpi    'A'
  536.     jc    chno
  537.     cpi    '['
  538.     jc    chek
  539.     cpi    'a'
  540.     jc    chno
  541.     cpi    '{'
  542.     jc    chek
  543.     jmp    chno
  544.  
  545. ;    Check list of alternatives.
  546.  
  547. chsq:    mov    c,l
  548.     mov    b,h
  549.     lhld    sqxx
  550.     push    h
  551.     lhld    sqaa
  552.     push    h
  553.     lhld    sqzz
  554.     push    h
  555.     mov    l,c
  556.     mov    h,b
  557.     shld    sqxx
  558.     xchg
  559.     mov    e,m
  560.     inx    h
  561.     mov    d,m
  562.     inx    h
  563.     shld    sqaa
  564.     xchg
  565.     shld    sqzz
  566. chaa:    lhld    sqxx
  567.     call    chek
  568.     jz    chff
  569. chbb:    lhld    sqaa    ;fail so find next alternative
  570. chcc:    call    next
  571.     cpi    RSQ
  572.     jz    chdd    ;no more alternatives, so fail
  573.     cpi    ORR
  574.     jnz    chcc
  575.     shld    sqaa
  576.     xchg
  577.     jmp    chaa    ;try next alternative
  578. chdd:    lhld    sqxx
  579.     ori    0FFH
  580. chee:    mov    c,l
  581.     mov    b,h
  582.     pop    h
  583.     shld    sqzz
  584.     pop    h
  585.     shld    sqaa
  586.     pop    h
  587.     shld    sqxx
  588.     mov    l,c
  589.     mov    h,b
  590.     ret
  591. chff:    xchg        ;good alternative, try rest
  592.     lhld    sqzz
  593.     xchg
  594.     call    chek
  595.     jz    chee
  596.     jmp    chbb
  597.  
  598. ;    Check iterative pattern.
  599.  
  600. chlb:    mov    c,l
  601.     mov    b,h
  602.     lhld    text
  603.     push    h
  604.     lhld    texx
  605.     push    h
  606.     lhld    rest
  607.     push    h
  608.     lhld    rept
  609.     push    h
  610.     lhld    repp
  611.     push    h
  612.     mov    l,c
  613.     mov    h,b
  614.     shld    text
  615.     shld    texx
  616.     xchg
  617.     mov    e,m
  618.     inx    h
  619.     mov    d,m
  620.     inx    h
  621.     shld    rept
  622.     shld    repp
  623.     xchg
  624.     shld    rest
  625. chlc:    lhld    rest
  626.     xchg
  627.     lhld    text
  628.     call    chek    ;check rest
  629.     jz    chzz
  630. chii:    lhld    rept    ;rest failed
  631.     xchg
  632.     lhld    text    ;keep same text
  633.     call    chek    ;try out the repeater
  634.     jnz    choo
  635.     shld    text    ;repeater worked, record progress
  636.     lhld    repp    ;start alternatives over again
  637.     shld    rept
  638.     jmp    chlc
  639. choo:    lhld    rept    ;repeater failed, try next
  640. chxx:    call    next
  641.     cpi    RBR
  642.     jz    chyy    ;this was the last, quit
  643.     cpi    ORR
  644.     jnz    chxx
  645.     shld    rept
  646.     jmp    chii
  647. chyy:    lhld    texx
  648.     ori    00    ;emphasize the RBR
  649. chzz:    mov    c,l
  650.     mov    b,h
  651.     pop    h
  652.     shld    repp
  653.     pop    h
  654.     shld    rept
  655.     pop    h
  656.     shld    rest
  657.     pop    h
  658.     shld    texx
  659.     pop    h
  660.     shld    text
  661.     mov    l,c
  662.     mov    h,b
  663.     ret
  664.  
  665. M1:    db    'The command line',CR,LF
  666.     db    '     FYNDE D:FILE.EXT EXPRESSION',CR,LF
  667.     db    'will search through all instances of FILE.EXT',CR,LF
  668.     db    '(which may be an ambiguous reference) on disk D',CR,LF
  669.     db    'to find lines containing EXPRESSION. Such lines',CR,LF
  670.     db    'will be presented on the console preceded by a',CR,LF
  671.     db    'line number, and classified by file. EXPRESSION',CR,LF
  672.     db    'may have the form LABEL!PATTERN or simply the',CR,LF
  673.     db    'form PATTERN. Both may contain:',CR,LF
  674.     db    '     [p1!p2!...!pn]  alternative strings',CR,LF
  675.     db    '     {p1!p2!...!pn}  repeated alternatives',CR,LF
  676.     db    '     ? any single character',CR,LF
  677.     db    '     @ for any alphanumeric: a-z, A-Z, 0-9',CR,LF
  678.     db    '     _ in place of horizontal tab',CR,LF
  679.     db    'When a label is present, lines will be numbered',CR,LF
  680.     db    'relative to the label.  Example: X{?}:![call!ret]',CR,LF
  681.     db    'will list calls and returns relative to labels',CR,LF
  682.     db    'like X0100: or X33:. LABEL begins in column 1,',CR,LF
  683.     db    'PATTERN can begin in any column.',CR,LF,00
  684.  
  685. M2:    db    'FYNDE.COM 12/20/82 ICUAP',CR,LF,00
  686.  
  687. M3:    db    '-- Bad Pattern --',00
  688.  
  689. M4:    db    CR,LF,'-- Search Terminated --',00
  690.  
  691. enth:    ds    1
  692. file:    db    'DFilenameEXT',00
  693.     ds    19
  694.  
  695. sqxx:    ds    2
  696. sqaa:    ds    2
  697. sqzz:    ds    2
  698. text:    ds    2
  699. texx:    ds    2
  700. rest:    ds    2
  701. rept:    ds    2
  702. repp:    ds    2
  703.  
  704. patt:    ds    256    ;command line pattern
  705.     ds    100    ;stack area
  706. stak:    ds    0    ;initialize stack pointer
  707. lapo:    ds    2    ;label pointer
  708. papo:    ds    2    ;pattern pointer
  709. fhed:    db    '----> File '
  710. fnam:    db    'xxxxxxxx.'    ;filename
  711. fext:    db    'xxx',CR,LF,00    ;file extension
  712. llbl:    db    '      +'
  713. lnum:    db    '         ',00
  714. lzer:    db    '   0'
  715. ftot:    db    '      lines found',CR,LF,00
  716. dtot:    db    '      instances in the entire disk',CR,LF,00
  717.     db    00    ;fence for line buffer
  718. lbuf:    ds    85H    ;line buffer
  719. ictr:    ds    2
  720. iptr:    ds    2
  721. ibuf:    ds    isiz
  722. fini:    ds    0
  723.  
  724.     end
  725.  
  726.