home *** CD-ROM | disk | FTP | other *** search
/ Multimedia & CD-ROM 3 / mmcd03-jun1995-cd.iso / utils / various / utils-1 / color.asm < prev    next >
Assembly Source File  |  1991-06-24  |  12KB  |  256 lines

  1. ;****************************************************************************
  2. ; COLOR sets the screen to the specified combination of colors.  Its
  3. ; syntax is
  4. ;
  5. ;       COLOR foreground background
  6. ;
  7. ; where "foreground" specifies the foreground color and "background"
  8. ; specifies the background color.  Valid values for these parameters are:
  9. ;
  10. ;       BLACK           RED
  11. ;       BLUE            MAGENTA
  12. ;       GREEN           YELLOW, ORANGE, or BROWN
  13. ;       CYAN            WHITE
  14. ;
  15. ; Preceding a color identifier with an underscore selects the intense
  16. ; version of that color (foreground colors only).  The ANSI.SYS driver
  17. ; must be installed.
  18. ;****************************************************************************
  19.  
  20. code            segment
  21.                 assume  cs:code,ds:code
  22.                 org     100h
  23. begin:          jmp     main
  24.  
  25. helpmsg         db      "Sets the screen to the specified combination of "
  26.                 db      "colors.",13,10,13,10
  27.                 db      "COLOR foreground background",13,10,13,10
  28.                 db      "  foreground  Foreground color.",13,10
  29.                 db      "  background  Background color.",13,10,13,10
  30.                 db      "Valid colors are BLACK, BLUE, GREEN, CYAN, RED, "
  31.                 db      "MAGENTA, YELLOW, and WHITE.",13,10
  32.                 db      "ORANGE or BROWN may be substituted for YELLOW.  "
  33.                 db      "Preceding a color identifier",13,10
  34.                 db      "with an underscore highlights the color "
  35.                 db      "(foreground colors only).",13,10,"$"
  36.  
  37. errmsg1         db      "Syntax: COLOR foreground background",13,10,"$"
  38. errmsg2         db      "DOS 4.0 or higher required",13,10,"$"
  39. errmsg3         db      "ANSI.SYS not installed",13,10,"$"
  40. errmsg4         db      "Invalid color identifier",13,10,"$"
  41.  
  42. color_ids       db      "BROWN",0               ;Color ID table
  43.                 db      "ORANGE",0
  44.                 db      "WHITE",0
  45.                 db      "CYAN",0
  46.                 db      "MAGENTA",0
  47.                 db      "BLUE",0
  48.                 db      "YELLOW",0
  49.                 db      "GREEN",0
  50.                 db      "RED",0
  51.                 db      "BLACK",0
  52.  
  53. command         db      27,"[0;3x;4xm$"         ;ANSI command string
  54.  
  55. ;****************************************************************************
  56. ; Procedure MAIN
  57. ;****************************************************************************
  58.  
  59. main            proc    near
  60.                 cld                             ;Clear direction flag
  61.                 mov     si,81h                  ;Point SI to command line
  62.                 call    scanhelp                ;Scan for "/?" switch
  63.                 jnc     checkver                ;Branch if not found
  64.                 mov     ah,09h                  ;Display help text and exit
  65.                 mov     dx,offset helpmsg       ;  with ERRORLEVEL=0
  66.                 int     21h
  67.                 mov     ax,4C01h
  68.                 int     21h
  69. ;
  70. ; Make sure this is DOS 4.0 or higher and that ANSI.SYS is installed
  71. ; before proceeding.
  72. ;
  73. checkver:       mov     dx,offset errmsg2       ;Exit if DOS version
  74.                 mov     ah,30h                  ;  is less than 4.0
  75.                 int     21h
  76.                 cmp     al,4
  77.                 jae     checkansi
  78.  
  79. error:          mov     ah,09h                  ;Display error message and
  80.                 int     21h                     ;  exit with ERRORLEVEL=1
  81.                 mov     ax,4C01h
  82.                 int     21h
  83.  
  84. checkansi:      mov     ax,1A00h                ;Check for ANSI.SYS using
  85.                 int     2Fh                     ;  the DOS multiplex
  86.                 mov     dx,offset errmsg3       ;  interrupt
  87.                 cmp     al,0FFh
  88.                 jne     error                   ;Error if not installed
  89. ;
  90. ; Capitalize everything on the command line.
  91. ;
  92.                 mov     si,81h                  ;Point SI to start
  93.                 mov     cl,[si-1]               ;Get character count in CX
  94.                 sub     ch,ch
  95.                 jcxz    parse                   ;Branch if nothing entered
  96. capsloop:       lodsb                           ;Get a character
  97.                 cmp     al,"a"                  ;Branch if lower than "a"
  98.                 jb      skipchar
  99.                 cmp     al,"z"                  ;Branch if higher than "z"
  100.                 ja      skipchar
  101.                 and     byte ptr [si-1],0DFh    ;Capitalize the character
  102. skipchar:       loop    capsloop                ;Loop until done
  103. ;
  104. ; Parse the command line for identifiers and build the command string.
  105. ;
  106. parse:          mov     si,81h                  ;Reset SI again
  107.                 call    findchar                ;Find first parameter
  108.                 mov     dx,offset errmsg1       ;Error if no parameter
  109.                 jc      error                   ;  found
  110.                 cmp     byte ptr [si],5Fh       ;Test for an underscore
  111.                 jne     not_uscore              ;Branch if found
  112.                 inc     si                      ;Advance SI past underscore
  113.                 inc     byte ptr command[2]     ;Select intense foreground
  114. not_uscore:     call    convert_color           ;Convert to a color code
  115.                 mov     dx,offset errmsg4       ;Exit if error occurred in
  116.                 jc      error                   ;  the conversion
  117.                 add     cl,30h                  ;Convert binary to ASCII
  118.                 mov     command[5],cl           ;Store in command string
  119.  
  120.                 call    finddelim               ;Find end of parameter
  121.                 mov     dx,offset errmsg1       ;Exit if parameter ended
  122.                 jc      error                   ;  with a carriage return
  123.  
  124.                 call    findchar                ;Find second parameter
  125.                 mov     dx,offset errmsg1       ;Error if no parameter
  126.                 jc      error                   ;  found
  127.                 call    convert_color           ;Convert to color code
  128.                 mov     dx,offset errmsg4       ;Exit if error occurred in
  129.                 jc      error                   ;  the conversion
  130.                 add     cl,30h                  ;Convert binary to ASCII
  131.                 mov     command[8],cl           ;Store in command string
  132. ;
  133. ; Output the command string to ANSI.SYS and terminate.
  134. ;
  135.                 mov     ah,09h                  ;Output the string with
  136.                 mov     dx,offset command       ;  DOS function 09H
  137.                 int     21h
  138.                 mov     ax,4C00h                ;Exit with ERRORLEVEL=0
  139.                 int     21h
  140. main            endp
  141.  
  142. ;****************************************************************************
  143. ; FINDCHAR advances SI to the next non-space or non-comma character.
  144. ; On return, carry set indicates EOL was encountered.
  145. ;****************************************************************************
  146.  
  147. findchar        proc    near
  148.                 lodsb                           ;Get the next character
  149.                 cmp     al,20h                  ;Loop if space
  150.                 je      findchar
  151.                 cmp     al,2Ch                  ;Loop if comma
  152.                 je      findchar
  153.                 dec     si                      ;Point SI to the character
  154.                 cmp     al,0Dh                  ;Exit with carry set if end
  155.                 je      eol                     ;  of line is reached
  156.  
  157.                 clc                             ;Clear carry and exit
  158.                 ret
  159.  
  160. eol:            stc                             ;Set carry and exit
  161.                 ret
  162. findchar        endp
  163.  
  164. ;****************************************************************************
  165. ; FINDDELIM advances SI to the next space or comma character.  On return,
  166. ; carry set indicates EOL was encountered.
  167. ;****************************************************************************
  168.  
  169. finddelim       proc    near
  170.                 lodsb                           ;Get the next character
  171.                 cmp     al,20h                  ;Exit if space
  172.                 je      fd_exit
  173.                 cmp     al,2Ch                  ;Exit if comma
  174.                 je      fd_exit
  175.                 cmp     al,0Dh                  ;Loop back for more if end
  176.                 jne     finddelim               ;  of line isn't reached
  177.  
  178.                 dec     si                      ;Set carry and exit
  179.                 stc
  180.                 ret
  181.  
  182. fd_exit:        dec     si                      ;Clear carry and exit
  183.                 clc
  184.                 ret
  185. finddelim       endp
  186.  
  187. ;****************************************************************************
  188. ; SCANHELP scans the command line for a /? switch.  If found, carry returns
  189. ; set and SI contains its offset.  If not found, carry returns clear.
  190. ;****************************************************************************
  191.  
  192. scanhelp        proc    near
  193.                 push    si                      ;Save SI
  194. scanloop:       lodsb                           ;Get a character
  195.                 cmp     al,0Dh                  ;Exit if end of line
  196.                 je      scan_exit
  197.                 cmp     al,"?"                  ;Loop if not "?"
  198.                 jne     scanloop
  199.                 cmp     byte ptr [si-2],"/"     ;Loop if not "/"
  200.                 jne     scanloop
  201.  
  202.                 add     sp,2                    ;Clear the stack
  203.                 sub     si,2                    ;Adjust SI
  204.                 stc                             ;Set carry and exit
  205.                 ret
  206.  
  207. scan_exit:      pop     si                      ;Restore SI
  208.                 clc                             ;Clear carry and exit
  209.                 ret
  210. scanhelp        endp
  211.  
  212. ;****************************************************************************
  213. ; CONVERT_COLOR compares the string pointed to by DS:SI against a list
  214. ; of ASCIIZ strings and returns carry clear if there's a match, carry set
  215. ; if there's not.  If carry is clear, CX holds the number of the string
  216. ; that was matched.
  217. ;****************************************************************************
  218.  
  219. convert_color   proc    near
  220.                 push    si                      ;Save SI on stack
  221.                 mov     bx,si                   ;Also save it in BX
  222. ;
  223. ; Compare the parameter to the list of color identifiers.
  224. ;
  225.                 mov     si,offset color_ids     ;Point SI to table
  226.                 mov     cx,9                    ;Initialize CX
  227. cc1:            mov     di,bx                   ;Point DI to parameter
  228. cc2:            lodsb                           ;Get a character
  229.                 or      al,al                   ;Is it zero?
  230.                 je      match                   ;Match if it is
  231.                 scasb                           ;Compare the characters
  232.                 je      cc2                     ;Loop if they're equal
  233.                 jcxz    nomatch                 ;Finished if CX is 0
  234.                 dec     cx                      ;Decrement count
  235. cc3:            lodsb                           ;Skip to the first character
  236.                 or      al,al                   ;  beyond the next zero and
  237.                 jnz     cc3                     ;  compare the next string
  238.                 jmp     cc1                     ;  in the list
  239. ;
  240. ; Set the carry flag to 1 or 0, then exit.
  241. ;
  242. nomatch:        pop     si                      ;Restore SI
  243.                 stc                             ;Set carry and exit
  244.                 ret
  245.  
  246. match:          cmp     cx,8                    ;Set CX to 3 if it's
  247.                 jb      match1                  ;  greater than 8
  248.                 mov     cx,3
  249. match1:         pop     si                      ;Restore SI
  250.                 clc                             ;Clear carry and exit
  251.                 ret
  252. convert_color   endp
  253.  
  254. code            ends
  255.                 end     begin
  256.