home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 277.lha / TestRH / src / breakout.asm next >
Encoding:
Assembly Source File  |  1989-08-08  |  3.3 KB  |  129 lines

  1. ;*
  2. ;* BREAKOUT.ASM
  3. ;*    (C) Copyright 1989 Vidhyanath Rao. All rights reserved.
  4. ;*
  5. ;*    Based on the C version of Matt Dillon and used in his DME.
  6. ;*
  7. ;*
  8.  
  9.  
  10. ; USAGE:  char *breakout(ptr, pqut)
  11. ;    ptr: pointer to the string to break an argument out of
  12. ;    pqut: pointer to a char. 1 if the arguement was quoted;
  13. ;        2 if nesting error;
  14. ;        3 if malloc failed.
  15. ;   returned string MUST be free()'ed.
  16.  
  17. CSEG
  18. _breakout:
  19.     link    a5,#-256
  20. ; local variable:
  21. ;    -256(a5) :: char bfr[256];
  22.     movem.l    d4-d7/a2-a3,-(sp)
  23. ; register variable:
  24. ;    d4.w     :: UWORD ix    = 254 - (index into buffer)
  25. ;    d3.l     :: UWORD count = count of unclosed quotes
  26. ;    d5.b     :: char opc    = char closing quotes
  27. ;    d6.b     :: char clc    = char opening quotes
  28. ;    d7.b     :: char isaux    = char opening quotes
  29. ;    a2.l     :: char *str    = pointer into the passed string
  30. ;    a3.l     :: char *bp    = pointer into the local buffer
  31.     move.l    8(a5),a0;str = *ptr
  32.     move.l    (a0),a2;points to the beginning of the string
  33. ; go past the initial blanks and move the first non-blank char to d0.b
  34.     move.l    #0,d7;clear error;
  35. lp1:    cmp.b    #32,(a2)+
  36.     beq    lp1
  37.     move.b    -(a2),d0
  38.     beq    out1; if empty, exit
  39. ; set opc, clc and isaux based on the first non-blank
  40.     cmp.b    #'`',d0
  41.     beq    la1
  42.     cmp.b    #'(',d0
  43.     bne    la2
  44.     move.l    #'(',d5;opc := '('
  45.     move.l    #')',d6;clc := ')'
  46.     move.l    #1,d7;isaux := TRUE
  47.     addq.l    #1,a2;correct the str pointer
  48.     bra    la3
  49. la1:    move.l    #'`',d5;opc := '`'
  50.     move.l    #$27,d6;clc := '\''
  51.     move.l    #1,d7;isaux := TRUE
  52.     addq.l    #1,a2;correct the str pointer
  53.     bra    la3
  54. la2:    move.l    #0,d5;opc not applicable
  55.     move.l    #' ',d6;clc := space
  56. la3:    move.w    #254,d4;ix = 254
  57.     move.l    #1,d3;count = 1 here becuase it doen't get bumped latter
  58.     lea    -256(a5),a3;bp = bfr
  59. lp4:    move.b    (a2)+,d0; test the next byte
  60.     beq    out3; if null, exit, but check count first
  61.     cmp.b    d5,d0; if opc
  62.     beq    lb1; increase count
  63.     cmp.b    d6,d0; if clc
  64.     beq    lb2
  65.     cmp.b    #'^',d0
  66.     beq    lb3
  67.     cmp.b    #'\\',d0
  68.     beq    lb4
  69.     cmp.b    #'$',d0
  70.     beq    lb5
  71. lf1:    move.b    d0,(a3)+;copy the char
  72.     dbf    d4,lp4;loop if space left
  73. outerr:    moveq.l    #3,d7;error return. no memory.
  74. out1:    moveq.l    #0,d0;null pointer
  75.     sub.l    #1,a2; make a2 at the last character read.
  76.     bra    le1
  77. lb3:    move.b    (a2)+,d0;convert the next char to a control char
  78.     and.b    #$1f,d0
  79.     bra    lf1
  80. lb4:    move.b    (a2)+,d0;next char is literal
  81. lf2:    bra    lf1
  82. lb5:    move.l    _String,a0; strcpy String to bp
  83. lp2:    move.b    (a0)+,(a3)+
  84.     dbeq    d4,lp2; if no more space, quit
  85.     tst.w    d4
  86.     blt    out1
  87.     subq.l    #1,a3
  88.     bra    lp4;and loop
  89. lb1:    addq.l    #1,d3; increment count
  90.     bra    lf1; and continue
  91. lb2:    subq.l    #1,d3
  92.     bne    lf1;continue if nesting incomplete
  93. ; at exit, d0 = return string, isaux to *pqut
  94. ;    and a3 = ptr
  95. out2:    clr.b    (a3);quoted exit
  96.     move.w    #255,d0
  97.     sub.w    d4,d0
  98.     ext.l    d0
  99.     move.l    d0,-(sp)
  100.     jsr    _malloc;allocate space for the breakout string
  101.     addq.l    #4,sp
  102.     tst.l    d0
  103.     beq    outerr;if no memory, quit
  104.     move.l    d0,a0
  105.     lea    -256(a5),a1
  106. lp3:    move.b    (a1)+,(a0)+;strcpy bfr to return string
  107.     bne    lp3
  108.     tst.b    -1(a2);if the last char read was not the clc
  109.     bne    le1;but a null,
  110.     subq.l    #1,a2;correct a2
  111. le1:    move.l    12(a5),a1
  112.     move.b    d7,(a1);*pqut filled in
  113.     move.l    8(a5),a3;correct str to point at the
  114.     move.l    a2,(a3);new start
  115.     movem.l    (sp)+,d4-d7/a2-a3
  116.     unlk    a5
  117.     rts
  118. out3:    tst.b    d5;if the beginning was bracketed,
  119.     beq    out2
  120.     tst.l    d3;but the count is nonzero, something is amiss
  121.     beq    out2
  122.     move.l    #2,d7; bad command
  123.     bra    out1
  124.  
  125.     public _breakout
  126.     public _malloc
  127.     public _String
  128. END
  129.