home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / COLOR.ZIP / COLOR.ASM next >
Assembly Source File  |  1989-03-14  |  4KB  |  155 lines

  1. ;; COLOR uses the OS/2 ANSI driver to set screen foreground and background
  2. ;; colors in protected mode.  Syntax is:
  3. ;;
  4. ;;    COLOR fb
  5. ;;
  6. ;; where f = foreground color (X,x,B,b,G,g,C,c,R,r,M,m,Y,y,W,w), and
  7. ;;       b = background color (x,b,g,c,r,m,y,w)
  8. ;;
  9. ;; Assemble with:  MASM COLOR;
  10. ;; Link with:       LINK COLOR,,NUL,DOSCALLS,COLOR;
  11. ;;
  12. ;; Module Definition File (COLOR.DEF):
  13. ;;    NAME COLOR
  14. ;;    STACKSIZE 4096
  15. ;;    PROTMODE
  16. ;;
  17. ;; Copyright (c) 1989 Ziff Communications Co.
  18. ;; Written March 1989 for PC Magazine by Jeff Prosise
  19.  
  20.         .286
  21.  
  22.         extrn    DosExit:far
  23.         extrn    VioSetAnsi:far
  24.         extrn    VioWrtTTY:far
  25.  
  26. DGROUP        group    _DATA
  27.  
  28. _DATA        segment    word public 'DATA'
  29.  
  30. ErrTxt1        db    "Usage: COLOR fb",13,10
  31. TxtLen1        equ    $ - ErrTxt1
  32.  
  33. ErrTxt2        db    "Invalid color ID",13,10
  34. TxtLen2        equ    $ - ErrTxt2
  35.  
  36. EscSeq        db    27,"[0;",8 dup (?)
  37. EscLen        dw    4
  38.  
  39. FCodes        db    "XBGCRMYW"
  40. BCodes        db    "xbgcrmyw"
  41.  
  42. FAnsiCodes    db    "37; 33; 35; 31; 36; 32; 34; 30;"
  43. BAnsiCodes    db    "47m 43m 45m 41m 46m 42m 44m 40m"
  44.  
  45. _DATA        ends
  46.  
  47. _TEXT        segment    word public 'CODE'
  48.         assume    cs:_TEXT, ds:DGROUP
  49. ;;
  50. ;; MAIN is the main body of the program.
  51. ;;
  52. main        proc    far
  53.  
  54.         mov    es,ax            ;move env selector into ES
  55.         mov    di,bx            ;point DI to command line
  56.         cld                ;clear direction flag
  57.         xor    al,al            ;find first null byte
  58.         repne    scasb
  59.  
  60. next_char:    cmp    byte ptr es:[di],20h    ;skip leading spaces
  61.         jne    get_entry
  62.         inc    di
  63.         jmp    next_char
  64. ;
  65. ;Build the foreground portion of the ANSI escape string.
  66. ;
  67. get_entry:    mov    al,es:[di]        ;get foreground ID code
  68.         or    al,al            ;see if a foreground code
  69.         jnz    check_next        ;  was entered
  70.  
  71. error1:        push    ds            ;display error message
  72.         push    offset DGROUP:ErrTxt1    ;  and exit if not
  73.         push    TxtLen1
  74. error2:        push    0
  75.         call    VioWrtTTY
  76.  
  77.         push    1            ;exit with return code of 1
  78.         push    1
  79.         call    DosExit
  80.  
  81. check_next:    mov    ah,es:[di+1]        ;get background ID code
  82.         or    ah,ah            ;exit if no background code
  83.         jz    error1            ;  was entered
  84.  
  85.         mov    bx,DGROUP        ;point ES to DGROUP
  86.         mov    es,bx
  87.         mov    di,offset DGROUP:FCodes    ;point DI to list of valid
  88.         mov    cx,16            ;  codes and see if the
  89.         repne    scasb            ;  foreground entry is
  90.         je    char_found        ;  valid
  91.  
  92. error3:        push    ds            ;error if foreground entry
  93.         push    offset DGROUP:ErrTxT2    ;  isn't in the list
  94.         push    TxtLen2
  95.         jmp    error2
  96.  
  97. char_found:    cmp    cx,8            ;check for highlighted
  98.         jb    normal            ;  foreground code
  99.  
  100.         sub    cx,8            ;add "1;" to ANSI string
  101.         mov    EscSeq[4],"1"        ;  if foreground color
  102.         mov    EscSeq[5],";"        ;  is highlighted
  103.         add    EscLen,2
  104.  
  105. normal:        mov    si,offset DGROUP:FAnsiCodes    ;append foreground
  106.         call    append                ;  code to string
  107. ;
  108. ;Build the background portion of the ANSI escape string.
  109. ;
  110.         xchg    ah,al                ;transfer code to AL
  111.         mov    di,offset DGROUP:BCodes        ;check background entry
  112.         mov    cx,8                ;  for validity
  113.         repne    scasb
  114.         jne    error3
  115.  
  116.         mov    si,offset DGROUP:BAnsiCodes    ;append background
  117.         call    append                ;  code to string
  118. ;
  119. ;Activate the ANSI driver and send escape string to set colors.
  120. ;
  121.         push    1            ;enable the ANSI driver
  122.         push    0
  123.         call    VioSetAnsi
  124.  
  125.         push    ds            ;transmit the ANSI escape
  126.         push    offset DGROUP:EscSeq    ;  sequence thru VioWrtTTY
  127.         push    EscLen
  128.         push    0
  129.         call    VioWrtTTY
  130.  
  131.         push    1            ;exit program with return
  132.         push    0            ;  code of 0
  133.         call    DosExit
  134.  
  135. main        endp
  136. ;;
  137. ;; APPEND appends a 3-character string to the ANSI escape string.
  138. ;;
  139. append        proc    near
  140.  
  141.         shl    cx,2            ;multiply index by 4
  142.         add    si,cx            ;add to absolute offset
  143.         mov    di,offset DGROUP:EscSeq    ;point DI to end of
  144.         add    di,EscLen        ;  ANSI string
  145.         mov    cx,3            ;append 3 bytes
  146.         rep    movsb
  147.         add    EscLen,3        ;update pointer
  148.         ret
  149.  
  150. append        endp
  151.  
  152. _TEXT        ends
  153.  
  154.         end    main
  155.