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

  1. ;****************************************************************************
  2. ; FKEYS programs the function keys to produce DOS commands.  Its syntax is
  3. ;
  4. ;       FKEYS [shift-]key ["command"[*]]
  5. ;
  6. ; where "shift" designates a shift state (CTRL, ALT, or SHIFT), "key"
  7. ; designates the function key to be programmed (F1 through F12), "command"
  8. ; is the command to assign to the function key, and "*" indicates that the
  9. ; command should be executed when the function key is pressed.  Omitting
  10. ; "command" cancels a function key assignment.  The following metastrings
  11. ; may be used in the "command" parameter to specify special symbols:
  12. ;
  13. ;       $B      Piping operator (|)
  14. ;       $G      Output redirection operator (>)
  15. ;       $L      Input redirection operator (<)
  16. ;       $T      Ctrl-T (command separator)
  17. ;       $$      Dollar sign
  18. ;
  19. ; FKEYS requires DOS 4.0 or higher and requires that ANSI.SYS be installed.
  20. ;****************************************************************************
  21.  
  22. code            segment
  23.                 assume  cs:code,ds:code
  24.                 org     100h
  25. begin:          jmp     main
  26.  
  27. helpmsg         db      "Programs the function keys to produce DOS commands."
  28.                 db      13,10,13,10
  29.                 db      "FKEYS [shift-]key [",22h,"command",22h,"[*]]"
  30.                 db      13,10,13,10
  31.                 db      "  shift    Identifies a shift key (Ctrl, Alt, or "
  32.                 db      "Shift).",13,10
  33.                 db      "  key      Identifies a function key (F1 through "
  34.                 db      "F12).",13,10
  35.                 db      "  command  Command to be assigned to the function "
  36.                 db      "key.",13,10
  37.                 db      "  *        Indicates the command should be auto-"
  38.                 db      "executed.",13,10,13,10
  39.                 db      "Running FKEYS without a ",22h,"command",22h
  40.                 db      " parameter cancels a function key assignment."
  41.                 db      13,10,"$"
  42.  
  43. errmsg1         db      "Syntax: FKEYS [shift-]key [",22h,"command",22h
  44.                 db      "[*]]",13,10,"$"
  45. errmsg2         db      "DOS 4.0 or higher required",13,10,"$"
  46. errmsg3         db      "ANSI.SYS not installed",13,10,"$"
  47.  
  48. altkey          db      "ALT"
  49. ctrlkey         db      "CTRL"
  50. shiftkey        db      "SHIFT"
  51. adder1          db      58
  52. adder2          db      122
  53.  
  54. command         db      27,"[0;",128 dup (?)
  55.  
  56. ;****************************************************************************
  57. ; Procedure MAIN
  58. ;****************************************************************************
  59.  
  60. main            proc    near
  61.                 cld                             ;Clear direction flag
  62.                 mov     si,81h                  ;Point SI to command line
  63.                 call    scanhelp                ;Scan for "/?" switch
  64.                 jnc     checkver                ;Branch if not found
  65.                 mov     ah,09h                  ;Display help text and exit
  66.                 mov     dx,offset helpmsg       ;  with ERRORLEVEL=0
  67.                 int     21h
  68.                 mov     ax,4C01h
  69.                 int     21h
  70. ;
  71. ; Make sure this is DOS 4.0 or higher and that ANSI.SYS is installed
  72. ; before proceeding.
  73. ;
  74. checkver:       mov     dx,offset errmsg2       ;Exit if DOS version
  75.                 mov     ah,30h                  ;  is less than 4.0
  76.                 int     21h
  77.                 cmp     al,4
  78.                 jae     checkansi
  79.  
  80. error:          mov     ah,09h                  ;Display error message and
  81.                 int     21h                     ;  exit with ERRORLEVEL=1
  82.                 mov     ax,4C01h
  83.                 int     21h
  84.  
  85. checkansi:      mov     ax,1A00h                ;Check for ANSI.SYS using
  86.                 int     2Fh                     ;  the DOS multiplex
  87.                 mov     dx,offset errmsg3       ;  interrupt
  88.                 cmp     al,0FFh
  89.                 jne     error                   ;Error if not installed
  90. ;
  91. ; Capitalize everything that's not between quotation marks.
  92. ;
  93.                 mov     si,81h                  ;Point SI to command line
  94. tocaps1:        lodsb                           ;Get a character
  95.                 cmp     al,0Dh                  ;Branch if end of line
  96.                 je      parse
  97.                 cmp     al,22h                  ;Branch if not quotation
  98.                 jne     tocaps3                 ;  mark
  99.  
  100. tocaps2:        lodsb                           ;Get a character
  101.                 cmp     al,0Dh                  ;Branch if end of line
  102.                 je      parse
  103.                 cmp     al,22h                  ;Is it a quotation mark?
  104.                 je      tocaps1                 ;Branch if it is
  105.                 jmp     tocaps2                 ;Loop if it's not
  106.  
  107. tocaps3:        cmp     al,"a"                  ;Leave it if less than "a"
  108.                 jb      tocaps1
  109.                 cmp     al,"z"                  ;Leave it if greater than "z"
  110.                 ja      tocaps1
  111.                 and     byte ptr [si-1],0DFh    ;Capitalize it
  112.                 jmp     tocaps1                 ;Loop until done
  113. ;
  114. ; Parse the command line for a function key identifier.
  115. ;
  116. parse:          mov     dx,offset errmsg1       ;Initialize error pointer
  117.                 mov     si,81h                  ;Reset SI
  118.                 call    findchar                ;Find first parameter
  119.                 jc      error                   ;Error if nothing found
  120.  
  121.                 cmp     byte ptr [si],"A"       ;Is the character an "A?"
  122.                 jne     not_alt                 ;No, then branch
  123.                 mov     cx,3                    ;Set CX to string length
  124.                 mov     di,offset altkey        ;Point DI to "ALT"
  125.                 add     adder1,45               ;Update adders
  126.                 add     adder2,6
  127.                 repe    cmpsb                   ;Compare the strings
  128.                 jne     error                   ;Error if they don't match
  129.                 jmp     short checkdash
  130.  
  131. not_alt:        cmp     byte ptr [si],"C"       ;Is the character a "C?"
  132.                 jne     not_ctrl                ;No, then branch
  133.                 mov     cx,4                    ;Set CX to string length
  134.                 mov     di,offset ctrlkey       ;Point DI to "CTRL"
  135.                 add     adder1,35               ;Update adders
  136.                 add     adder2,4
  137.                 repe    cmpsb                   ;Compare the strings
  138.                 jne     error                   ;Error if they don't match
  139.                 jmp     short checkdash
  140.  
  141. not_ctrl:       cmp     byte ptr [si],"S"       ;Is the character an "S?"
  142.                 jne     fkey                    ;No, then branch
  143.                 mov     cx,5                    ;Set CX to string lemgth
  144.                 mov     di,offset shiftkey      ;Point DI to "SHIFT"
  145.                 add     adder1,25               ;Update adders
  146.                 add     adder2,2
  147.                 repe    cmpsb                   ;Compare the strings
  148.                 jne     error1                  ;Error if they don't match
  149.  
  150. checkdash:      lodsb                           ;Get the next character
  151.                 cmp     al,"-"                  ;Should be a hyphen
  152.                 jne     error1                  ;Error if it's not
  153.  
  154. fkey:           lodsb                           ;Get next character
  155.                 cmp     al,"F"                  ;Is it "F?"
  156.                 je      fkey1                   ;Branch if it is
  157. error1:         jmp     error                   ;Error if it's not
  158. fkey1:          call    asc2bin                 ;Convert to binary
  159.                 mov     dx,offset errmsg1       ;Initialize error pointer
  160.                 jc      error1                  ;Exit on error
  161.                 cmp     al,1                    ;Error if number is less
  162.                 jb      error1                  ;  than 1
  163.                 cmp     al,12                   ;Error if number is greater
  164.                 ja      error1                  ;  than 12
  165.                 cmp     al,10                   ;Is it F1 through F10?
  166.                 jbe     fkey2                   ;Branch if it is
  167.                 add     al,adder2               ;Convert to ANSI.SYS code
  168.                 jmp     short fkey3             ;Branch around the next line
  169. fkey2:          add     al,adder1               ;Convert to ANSI.SYS code
  170. fkey3:          mov     di,offset command+4     ;Point DI to end of command
  171.                 call    bin2asc                 ;Convert binary to ASCII
  172.                 mov     al,";"                  ;Append a semicolon
  173.                 stosb
  174. ;
  175. ; Parse the command line for a command string.
  176. ;
  177.                 call    findchar                ;Find next parameter
  178.                 jnc     fkey4                   ;Branch if one found
  179.  
  180.                 mov     si,offset command+2     ;Point SI to key code
  181.                 mov     cx,di                   ;Compute length of string
  182.                 sub     cx,offset command+3     ;  entered thus far
  183.                 rep     movsb                   ;Copy the key code
  184.                 jmp     short exit              ;Output and exit
  185.  
  186. fkey4:          movsb                           ;Copy the next character
  187.                 mov     dx,offset errmsg1       ;Initialize error pointer
  188.                 cmp     byte ptr [si-1],22h     ;Error if it was not a
  189.                 jne     error1                  ;  quotation mark
  190.  
  191. fkey5:          lodsb                           ;Read another character
  192.                 cmp     al,0Dh                  ;Error if it's a carriage
  193.                 je      error1                  ;  return
  194.                 cmp     al,22h                  ;Branch if it's a quotation
  195.                 je      fkey7                   ;  mark
  196.  
  197.                 cmp     al,"$"                  ;Branch if character is not
  198.                 jne     fkey6                   ;  a dollar sign
  199.                 call    convert_mstring         ;Convert metastring if it is
  200. fkey6:          stosb                           ;Write it to the string
  201.                 jmp     fkey5                   ;Loop back for more
  202.  
  203. fkey7:          cmp     byte ptr [si],"*"       ;Is next character a "*"?
  204.                 jne     fkey8                   ;Branch if it's not
  205.                 mov     byte ptr [di],0Dh       ;Append a carriage return
  206.                 inc     di
  207. fkey8:          stosb                           ;Add closing quotation mark
  208. ;
  209. ; Output the command string to ANSI.SYS and terminate.
  210. ;
  211. exit:           mov     ax,0070h                ;Append a "p" and binary
  212.                 stosw                           ;  zero to the command
  213.                 mov     si,offset command       ;Transmit the string to
  214.                 call    dos_out                 ;  ANSI.SYS
  215.                 mov     ax,4C00h                ;Exit with ERRORLEVEL=0
  216.                 int     21h
  217. main            endp
  218.  
  219. ;****************************************************************************
  220. ; FINDCHAR advances SI to the next non-space or non-comma character.
  221. ; On return, carry set indicates EOL was encountered.
  222. ;****************************************************************************
  223.  
  224. findchar        proc    near
  225.                 lodsb                           ;Get the next character
  226.                 cmp     al,20h                  ;Loop if space
  227.                 je      findchar
  228.                 cmp     al,2Ch                  ;Loop if comma
  229.                 je      findchar
  230.                 dec     si                      ;Point SI to the character
  231.                 cmp     al,0Dh                  ;Exit with carry set if end
  232.                 je      eol                     ;  of line is reached
  233.  
  234.                 clc                             ;Clear carry and exit
  235.                 ret
  236.  
  237. eol:            stc                             ;Set carry and exit
  238.                 ret
  239. findchar        endp
  240.  
  241. ;****************************************************************************
  242. ; FINDDELIM advances SI to the next space or comma character.  On return,
  243. ; carry set indicates EOL was encountered.
  244. ;****************************************************************************
  245.  
  246. finddelim       proc    near
  247.                 lodsb                           ;Get the next character
  248.                 cmp     al,20h                  ;Exit if space
  249.                 je      fd_exit
  250.                 cmp     al,2Ch                  ;Exit if comma
  251.                 je      fd_exit
  252.                 cmp     al,0Dh                  ;Loop back for more if end
  253.                 jne     finddelim               ;  of line isn't reached
  254.  
  255.                 dec     si                      ;Set carry and exit
  256.                 stc
  257.                 ret
  258.  
  259. fd_exit:        dec     si                      ;Clear carry and exit
  260.                 clc
  261.                 ret
  262. finddelim       endp
  263.  
  264. ;****************************************************************************
  265. ; SCANHELP scans the command line for a /? switch.  If found, carry returns
  266. ; set and SI contains its offset.  If not found, carry returns clear.
  267. ;****************************************************************************
  268.  
  269. scanhelp        proc    near
  270.                 push    si                      ;Save SI
  271. scanloop:       lodsb                           ;Get a character
  272.                 cmp     al,0Dh                  ;Exit if end of line
  273.                 je      scan_exit
  274.                 cmp     al,22h                  ;Branch if it's a quotation
  275.                 je      skip                    ;  mark
  276.                 cmp     al,"?"                  ;Loop if not "?"
  277.                 jne     scanloop
  278.                 cmp     byte ptr [si-2],"/"     ;Loop if not "/"
  279.                 jne     scanloop
  280.  
  281.                 add     sp,2                    ;Clear the stack
  282.                 sub     si,2                    ;Adjust SI
  283.                 stc                             ;Set carry and exit
  284.                 ret
  285.  
  286. scan_exit:      pop     si                      ;Restore SI
  287.                 clc                             ;Clear carry and exit
  288.                 ret
  289.  
  290. skip:           lodsb                           ;Get a character
  291.                 cmp     al,0Dh                  ;Exit if end of line
  292.                 je      scan_exit
  293.                 cmp     al,22h                  ;Reenter the loop if it's
  294.                 je      scanloop                ;  a quotation mark
  295.                 jmp     skip                    ;Continue scanning
  296. scanhelp        endp
  297.  
  298. ;****************************************************************************
  299. ; CONVERT_MSTRING converts a $B, $G, $L, $T, or $$ into a single character.
  300. ; On entry, AL holds the ASCII code for the dollar sign that prompted the
  301. ; call.  On exit, AL holds the character to be used in the command.
  302. ;****************************************************************************
  303.  
  304. convert_mstring proc    near
  305.                 mov     ah,7Ch                  ;Substitute "|" for $B
  306.                 cmp     byte ptr [si],"b"
  307.                 je      cm_convert
  308.                 cmp     byte ptr [si],"B"
  309.                 je      cm_convert
  310.  
  311.                 mov     ah,3Eh                  ;Substitute ">" for $G
  312.                 cmp     byte ptr [si],"g"
  313.                 je      cm_convert
  314.                 cmp     byte ptr [si],"G"
  315.                 je      cm_convert
  316.  
  317.                 mov     ah,3Ch                  ;Substitute "<" for $L
  318.                 cmp     byte ptr [si],"l"
  319.                 je      cm_convert
  320.                 cmp     byte ptr [si],"L"
  321.                 je      cm_convert
  322.  
  323.                 mov     ah,14h                  ;Substitute Ctrl-T for $T
  324.                 cmp     byte ptr [si],"t"
  325.                 je      cm_convert
  326.                 cmp     byte ptr [si],"T"
  327.                 je      cm_convert
  328.  
  329.                 mov     ah,24h                  ;Substitute "$" for $$
  330.                 cmp     byte ptr [si],"$"
  331.                 je      cm_convert
  332.                 cmp     byte ptr [si],"$"
  333.                 jne     cm_exit
  334.  
  335. cm_convert:     mov     al,ah                   ;Store code in AL
  336.                 inc     si                      ;Skip next character
  337.  
  338. cm_exit:        ret                             ;Exit to caller
  339. convert_mstring endp
  340.  
  341. ;****************************************************************************
  342. ; ASC2BIN converts a decimal number entered in ASCII form into a binary
  343. ; value in AL.  Carry set on return indicates that an error occurred in
  344. ; the conversion.
  345. ;****************************************************************************
  346.  
  347. asc2bin         proc    near
  348.                 sub     ax,ax                   ;Initialize registers
  349.                 sub     bh,bh
  350.                 mov     dl,10
  351.  
  352. a2b_loop:       mov     bl,[si]                 ;Get a character
  353.                 inc     si
  354.                 cmp     bl,20h                  ;Exit if space
  355.                 je      a2b_exit
  356.                 cmp     bl,2Ch                  ;Exit if comma
  357.                 je      a2b_exit
  358.                 cmp     bl,0Dh                  ;Exit if carriage return
  359.                 je      a2b_exit
  360.  
  361.                 cmp     bl,"0"                  ;Error if character is not
  362.                 jb      a2b_error               ;  a number
  363.                 cmp     bl,"9"
  364.                 ja      a2b_error
  365.  
  366.                 mul     dl                      ;Multiply the value in AL by
  367.                 jc      a2b_error               ;  10 and exit on overflow
  368.                 sub     bl,30h                  ;ASCII => binary
  369.                 add     ax,bx                   ;Add latest value to AX
  370.                 cmp     ax,255                  ;Error if sum > 255
  371.                 jna     a2b_loop                ;Loop back for more
  372.  
  373. a2b_error:      dec     si                      ;Set carry and exit
  374.                 stc
  375.                 ret
  376.  
  377. a2b_exit:       dec     si                      ;Clear carry and exit
  378.                 clc
  379.                 ret
  380. asc2bin         endp
  381.  
  382. ;****************************************************************************
  383. ; BIN2ASC converts a binary value in AX to ASCII form.  On entry, ES:DI
  384. ; holds the address the string will be written to.
  385. ;****************************************************************************
  386.  
  387. bin2asc         proc    near
  388.                 mov     bx,10                   ;Initialize divisor word and
  389.                 xor     cx,cx                   ;  digit counter
  390. b2a1:           inc     cx                      ;Increment digit count
  391.                 xor     dx,dx                   ;Divide by 10
  392.                 div     bx
  393.                 push    dx                      ;Save remainder on stack
  394.                 or      ax,ax                   ;Loop until quotient is zero
  395.                 jnz     b2a1
  396. b2a2:           pop     ax                      ;Retrieve a digit from stack
  397.                 add     al,30h                  ;Convert it to ASCII
  398.                 stosb                           ;Store it
  399.                 loop    b2a2                    ;Loop until done
  400.                 ret
  401. bin2asc         endp
  402.  
  403. ;****************************************************************************
  404. ; DOS_OUT outputs the ASCIIZ string pointed to by DS:SI.
  405. ;****************************************************************************
  406.  
  407. dos_out         proc    near
  408.                 lodsb                           ;Get a character
  409.                 or      al,al                   ;Exit if it's zero
  410.                 jz      dos_exit
  411.                 mov     ah,02h                  ;Output it using DOS
  412.                 mov     dl,al                   ;  function 02H
  413.                 int     21h
  414.                 jmp     dos_out                 ;Loop until done
  415. dos_exit:       ret
  416. dos_out         endp
  417.  
  418. code            ends
  419.                 end     begin
  420.