home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / AMISL092.ZIP / NOTE.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-02-22  |  11.3 KB  |  462 lines

  1. ;-----------------------------------------------------------------------
  2. ; NOTE.ASM    Public Domain 1992 Ralf Brown
  3. ;        You may do with this software whatever you want, but
  4. ;        common courtesy dictates that you not remove my name
  5. ;        from it.
  6. ;
  7. ; Popup to append one or more lines to a text file.  Demonstration of the
  8. ; use of DOS from within an AMIS-compliant TSR.
  9. ; Note: popup may be done from the commandline or via a hotkey; however,
  10. ;     the hotkey support requires a newer BIOS which has the INT 15/4F
  11. ;    keyboard intercept
  12. ;
  13. ; Version 0.92
  14. ; LastEdit: 2/21/93
  15. ;-----------------------------------------------------------------------
  16.  
  17. __DEFAULT_MODEL__ equ __TINY__
  18.     INCLUDE AMIS.MAC
  19.     INCLUDE AMIPOPUP.MAC
  20.  
  21.     @Startup 3,00            ; need DOS 3.00
  22.                     ; this macro also takes care of declaring
  23.                     ; all the segments in the required order
  24.  
  25. ;-----------------------------------------------------------------------
  26. ;
  27. VERSION_NUM equ 005Ch    ; v0.92
  28. VERSION_STR equ "0.92"
  29.  
  30. ; comment out the following line to use the generic hotkey dispatcher, at a
  31. ; cost of an additional 80 bytes
  32. ;CUSTOM_HOTKEY_CODE equ 1
  33.  
  34. WINDOW_TOP    equ 0        ; topmost row of TSR's popup window
  35. WINDOW_LEFT   equ 5        ; leftmost column of TSR's popup window
  36. WINDOW_HEIGHT equ 3           ; height (including frame) of popup window
  37. WINDOW_WIDTH  equ 70          ; width (including frame) of popup window
  38. HOTKEY_SCAN   equ SCAN_N    ; scan code for 'N' key
  39. HOTKEY_NAME   equ "N"
  40.  
  41. screen_buffer_size equ (WINDOW_HEIGHT*WINDOW_WIDTH*2)
  42. screen_buffer_para equ (screen_buffer_size+15)/16
  43.  
  44. LODSB_ES MACRO
  45.     DB 26h,0ACh    ; LODSB ES:
  46.     ENDM
  47.  
  48. ;-----------------------------------------------------------------------
  49. ; Put the resident code into its own segment so that all the offsets are
  50. ; proper for the new location after copying it into a UMB or down into
  51. ; the PSP.
  52. ;
  53. TSRcode@
  54.  
  55. ;-----------------------------------------------------------------------
  56. ; Since we need a PSP, but might be loaded into a UMB or at the top of
  57. ; conventional memory, we make a copy of the all-important first 64 bytes
  58. ; of the PSP here.  After relocation, this copy will start at offset 0
  59. ;
  60. TSR_PSP    db 64 dup (?)
  61.  
  62. ;-----------------------------------------------------------------------
  63. ; TSR's initialized data storage
  64. ;
  65. TSRdata@
  66. extrn $AMIS$DISK_BUSY:byte,$AMIS$WANT_POPUP:byte,$AMIS$WANT_SHUTDOWN:byte
  67. extrn $AMIS$WANT_POPUP:byte,$AMIS$WANT_SHUTDOWN:byte,$AMIS$POPUP_INT28:byte
  68.  
  69. ;;; add TSR-specific initialized data below
  70.  
  71. TSR_NAME    db "NOTE",0        ; title for popup window
  72.  
  73. CRLF_buffer    db 13,10
  74.  
  75. TSRdataEnd@
  76.  
  77. ;-----------------------------------------------------------------------
  78. ; TSR's uninitialized data storage
  79. ;
  80. TSRbss@
  81.  
  82. ;;; add TSR-specific uninitialized data below
  83.  
  84. notefile_handle dw ?
  85.  
  86. edit_buffer    db WINDOW_WIDTH-2 dup (?)
  87. TSRbssEnd@
  88.  
  89.  
  90. ;-----------------------------------------------------------------------
  91.  
  92. public TSR_MAIN
  93. TSR_MAIN proc near
  94.     ASSUME    DS:TGROUP,ES:NOTHING
  95.     xor    si,si            ; SI stores line length
  96. TSR_main_loop:
  97.     MOVE_CURSOR ,,si
  98.     GETKEY
  99.     cmp    al,0Dh            ; Enter pressed?
  100.     je    TSR_main_line_end
  101.     cmp    al,27            ; Esc pressed?
  102.     je    TSR_main_done
  103.     cmp    al,8
  104.     je    backspace
  105.     cmp    al,0            ; extended ASCII?
  106.     je    TSR_main_loop        ; if yes, ignore
  107.     cmp    al,0E0h
  108.     jne    got_char
  109.     cmp    ah,0
  110.     jne    TSR_main_loop
  111. got_char:
  112.     cmp    si,WINDOW_WIDTH-2
  113.     jb    store_char
  114. beep:
  115.     mov    ax,0E07h        ; beep
  116.     int    10h
  117.     jmp    TSR_main_loop
  118. store_char:
  119.     mov    edit_buffer[si],al
  120.     inc    si            ; remember that we got another char
  121.     PUT_CHAR
  122.     jmp    TSR_main_loop
  123.  
  124. backspace:
  125.     or    si,si
  126.     jz    beep
  127.     dec    si
  128.     MOVE_CURSOR ,,si
  129.     PUT_CHAR ' '
  130.     jmp    TSR_main_loop
  131.  
  132. TSR_main_line_end:
  133.     mov    ah,40h
  134.     mov    bx,notefile_handle
  135.     mov    cx,si
  136.     mov    dx,offset TGROUP:edit_buffer
  137.     int    21h
  138.     mov    ah,40h
  139.     mov    cx,2
  140.     mov    dx,offset TGROUP:CRLF_buffer
  141.     int    21h
  142.     CLEAR_WINDOW
  143.     jmp    TSR_main        ; restart for next line
  144.  
  145. TSR_main_done:
  146.     mov    bx,notefile_handle
  147.     mov    ah,45h            ; DUP handle
  148.     int    21h
  149.     jc    TSR_main_exit        ; quit now if unable to duplicate
  150.     mov    bx,ax
  151.     mov    ah,3Eh            ; close duplicate
  152.     int    21h
  153. TSR_main_exit:
  154.     ret
  155. TSR_MAIN endp
  156.  
  157. ;-----------------------------------------------------------------------
  158. ; Function that performs any necessary cleanup prior to the TSR being
  159. ; removed from memory.  At the time it is called, the TSR is effectively
  160. ; popped up, though it has not modified the screen.  If this routine needs
  161. ; to write on the screen, it must save and restore the screen contents
  162. ; itself
  163. ;
  164. public TSR_SHUTDOWN
  165. TSR_SHUTDOWN proc near
  166.     mov    bx,notefile_handle
  167.     mov    ah,3Eh            ; close the file
  168.     int    21h
  169.     ret
  170. TSR_SHUTDOWN endp
  171.  
  172. ;-----------------------------------------------------------------------
  173. ; For this simple case, we simply fail all INT 21h calls which encounter
  174. ; a critical error
  175. ;
  176. public TSR_INT24_HANDLER
  177. TSR_INT24_HANDLER:
  178.     mov    al,03h            ; FAIL, for now
  179. ;    iret  ; save a byte by falling through to next handler
  180.  
  181. ;-----------------------------------------------------------------------
  182. ; Simply ignore Ctrl-Break and Ctrl-C interrupts
  183. ;
  184. public TSR_INT1B_HANDLER,TSR_INT23_HANDLER
  185. TSR_INT1B_HANDLER:
  186. TSR_INT23_HANDLER:
  187.     iret
  188.  
  189. ;=======================================================================
  190. ; It should not be necessary to make any changes between here and the
  191. ; end of the resident portion (other than the TSR identifier in the ALTMPX
  192. ; macro) in order to modify this code for a different purpose.
  193. ;=======================================================================
  194.  
  195.     DEFAULT_API_POPUP
  196.     DEFAULT_API_REMOVE
  197.  
  198. ;-----------------------------------------------------------------------
  199. ; Declare the interrupt vectors hooked by the program, then set up the
  200. ; Alternate Multiplex Interrupt Spec handler and the default interrupt
  201. ; handlers for the idle popups, disk lockout, and hotkey popup
  202. ;
  203.     HOOKED_INTS 08h,13h,15h,28h
  204.  
  205.     HOTKEYS HK_INT15ENTRY
  206.     HOTKEY    HOTKEY_SCAN,HK_BOTHSHIFT,<HK_ANYCTRL OR HK_ANYALT>
  207.     HOTKEYS_DONE
  208.  
  209.     ALTMPX    'Ralf B','NOTE',VERSION_NUM,"Append notes to a file",,,API_popup,API_remove,Y,hotkey_list
  210.  
  211.     DEFAULT_INT08
  212.     DEFAULT_INT13
  213.     DEFAULT_INT15 HOTKEY_SCAN,HK_BOTHSHIFT
  214.     DEFAULT_INT28
  215.  
  216. TSRcodeEnd@
  217.  
  218. ;-----------------------------------------------------------------------
  219.  
  220. _TEXT SEGMENT PUBLIC 'CODE'
  221.     ASSUME cs:_TEXT,ds:NOTHING,es:NOTHING,ss:NOTHING
  222.  
  223. extrn TSR_SET_WINDOW:DIST,$AMIS$GET_DOS_PTRS:DIST
  224.  
  225. banner              db 'NOTE v',VERSION_STR,'  Public Domain 1993 Ralf Brown',13,10,'$'
  226. usage_msg     db "Usage:",9,"NOTE -Ifile",9,"Install using <file> as notepad",13,10
  227.          db 9,"NOTE -R",9,9,"Remove from memory",13,10
  228.          db "$"
  229. hotkey_msg     db "Press Shift-Shift-",HOTKEY_NAME," to pop up",13,10,"$"
  230. no_hotkey_msg     db "Hotkey is not available on this machine",13,10,"$"
  231. hotkey_used_msg     db "Hotkey is already in use.",13,10,"$"
  232. installed_msg     db "Installed.",13,10,"$"
  233. already_inst_msg db 13,10,"Already installed.",13,10,"$"
  234. cant_remove_msg  db "Can't remove from memory.",13,10,"$"
  235. uninstalled_msg  db "Removed.",13,10,"$"
  236. cant_access_msg     db "Unable to open or create notepad file",13,10,"$"
  237.  
  238. filename_len equ 80
  239. filename_buf    db filename_len dup (?)
  240.  
  241.  
  242.  
  243.     @Startup2    Y
  244.     push    ds
  245.     pop    es
  246.     ASSUME    ES:_INIT
  247.     push    cs
  248.     pop    ds
  249.     ASSUME    DS:_TEXT
  250.     ;
  251.     ; say hello 
  252.     ;
  253.     DISPLAY_STRING banner
  254.     mov    bx,1000h        ; set memory block to 64K
  255.     mov    ah,4Ah
  256.     int    21h
  257.     mov    si,81h            ; SI -> command line
  258.     cld                ; ensure proper direction for string ops
  259. cmdline_loop:
  260.     lodsb_es
  261.     cmp    al,' '            ; skip blanks and tabs on commandline
  262.     je    cmdline_loop
  263.     cmp    al,9
  264.     je    cmdline_loop
  265.     cmp    al,'-'
  266.     je    got_cmdline_switch
  267. bad_cmdline:
  268.     jmp    usage
  269. got_cmdline_switch:
  270.     lodsb_es            ; get next character
  271.     and    al,0DFh            ; force to uppercase
  272.     cmp    al,'R'
  273.     jne    not_removing
  274.     jmp    removing
  275. not_removing:
  276.     cmp    al,'I'
  277.     jne    bad_cmdline
  278. installing:
  279.     ;
  280.     ; place any necessary pre-initialization here
  281.     ;
  282.     SET_WINDOW WINDOW_TOP,WINDOW_LEFT,WINDOW_HEIGHT,WINDOW_WIDTH,TGROUP:$AMIS$end_TSRcode,TGROUP:TSR_NAME
  283.     mov    di,offset _TEXT:filename_buf
  284.     mov    dx,di            ; remember start of filename
  285.     mov    cx,filename_len
  286.     push    es
  287.     pop    ds
  288.     ASSUME    DS:_INIT
  289.     push    cs
  290.     pop    es
  291.     ASSUME    ES:_TEXT
  292. copy_filename_loop:
  293.     lodsb
  294.     cmp    al,' '
  295.     jb    copy_filename_done
  296.     stosb
  297.     loop    copy_filename_loop
  298. copy_filename_done:
  299.     mov    al,0
  300.     stosb
  301.     push    cs
  302.     pop    ds
  303.     ASSUME    DS:_TEXT
  304.     mov    ax,3DC1h        ; open, no-inherit/DENYNONE/write-only
  305.     int    21h
  306.     jnc    open_successful
  307.     mov    ax,3C00h        ; if unable to open, try creating file
  308.     xor    cx,cx            ; no special attributes
  309.     int    21h
  310.     jnc    create_successful
  311.     mov    dx,offset _TEXT:cant_access_msg
  312.     jmp    exit_with_error
  313. open_successful:
  314. create_successful:
  315.     push    cs
  316.     pop    ds            ; restore DS
  317.     ASSUME    DS:_TEXT
  318.     mov    bx,ax
  319.     mov    ah,3Eh            ; close the file again; we now know
  320.     int    21h            ;   we can access it
  321.     ;
  322.     ; find out whether keyboard intercept is available
  323.     ;
  324.     stc
  325.     mov    ah,0C0h
  326.     int    15h            ; get ROM BIOS configuration data
  327.     ASSUME    ES:NOTHING
  328.     mov    dx,offset _TEXT:no_hotkey_msg
  329.     jc    no_kbd_intercept
  330.     test    byte ptr es:[bx+5],10h    ; have keyboard intercept?
  331.     jz    no_kbd_intercept
  332.     mov    dx,offset _TEXT:hotkey_msg
  333. no_kbd_intercept:
  334.     mov    ah,9
  335.     int    21h
  336.     call    $AMIS$GET_DOS_PTRS
  337.     ;
  338.     ; one last check: is there a hotkey conflict?
  339.     ;
  340.     IF_HOTKEY_USED    hotkey_in_use
  341.     ;
  342.     ; now go install the TSR
  343.     ;
  344.     push    cs
  345.     pop    ds
  346.     ASSUME    DS:_TEXT
  347. ;note: add TOPMEM following BEST to make TSR load at top of memory
  348.     INSTALL_TSR screen_buffer_para,BEST,,inst_patch,already_installed
  349.  
  350. hotkey_in_use:
  351.     push    cs
  352.     pop    ds
  353.     ASSUME    DS:_TEXT
  354.     mov    dx,offset hotkey_used_msg
  355.     jmp short exit_with_error
  356.  
  357. removing:
  358.     ASSUME    DS:_TEXT
  359.     UNINSTALL cant_uninstall
  360.     push    cs
  361.     pop    ds
  362.     ASSUME    DS:_TEXT
  363.     DISPLAY_STRING uninstalled_msg
  364.         mov     ax,4C00h
  365.     int    21h
  366.  
  367. already_installed:
  368.     mov    dx,offset _TEXT:already_inst_msg
  369.     jmp short exit_with_error
  370.  
  371. usage:
  372.     ASSUME    DS:_TEXT
  373.     mov    dx,offset _TEXT:usage_msg
  374.     jmp short exit_with_error
  375.  
  376. cant_uninstall:
  377.     mov    dx,offset _TEXT:cant_remove_msg
  378. exit_with_error:
  379.     push    cs
  380.     pop    ds
  381.     ASSUME    DS:_TEXT
  382.     mov    ah,9
  383.     int    21h
  384.     mov    ax,4C01h
  385.     int    21h
  386.  
  387. inst_patch:
  388.     ASSUME    DS:NOTHING
  389.     push    ds
  390.         push    es
  391.         mov     es,ax
  392.     ASSUME    ES:TGROUP
  393.     ;
  394.     ; close all files which will not be used by the TSR
  395.     ;
  396.     mov    bx,0            ; for this TSR, don't need handles 0-4
  397. close_file_loop:
  398.     mov    ah,3Eh
  399.     int    21h
  400.     inc    bx
  401.     cmp    bx,4
  402.     jbe    close_file_loop
  403.     ;
  404.     ; now copy the PSP into the resident portion
  405.     ;
  406.     mov    ds,__psp
  407.     ASSUME    DS:_INIT
  408.     xor    si,si
  409.     xor    di,di
  410.     mov    cx,size TSR_PSP
  411.     cld
  412.     rep    movsb
  413.     mov    es:[36h],es        ; adjust JFT pointer in copied PSP
  414.     mov    bx,es
  415.     mov    ah,50h            ; set PSP segment so TSR owns file
  416.     int    21h
  417.     push    cs
  418.     pop    ds
  419.     ASSUME    DS:_TEXT
  420.     mov    dx,offset _TEXT:filename_buf
  421.         mov     ax,3DC1h                ; open, no-inherit/DENYNONE/write-only
  422.     int    21h
  423.     jnc    reopen_successful
  424.     xor    ax,ax            ; point at a closed handle
  425. reopen_successful:
  426.     mov    TGROUP:notefile_handle,ax
  427.     mov    bx,ax
  428.     xor    cx,cx
  429.     xor    dx,dx
  430.     mov    ax,4202h        ; position to end of file
  431.     int    21h
  432.     mov    ah,50h            ; restore PSP segment
  433.     mov    bx,__psp
  434.     mov    ds,bx
  435.     ASSUME    DS:_INIT
  436.     int    21h
  437.     ;
  438.     ; now, zero out the JFT in our PSP so that the exit won't close
  439.     ; the files that the TSR does need
  440.     ;
  441.     mov    cx,ds:[0032h]        ; size of JFT
  442.     les    di,ds:[0034h]        ; pointer to JFT
  443.     mov    al,0FFh            ; closed-file flag
  444.     rep    stosb
  445.     pop    es
  446.     ASSUME    ES:NOTHING
  447.     ;
  448.     ; finally, announce that we are installed
  449.     ;
  450.     push    cs
  451.     pop    ds
  452.     ASSUME    DS:_TEXT
  453.     DISPLAY_STRING installed_msg
  454.     pop    ds
  455.     ASSUME    DS:NOTHING
  456.     ret
  457.  
  458. _TEXT ENDS
  459.  
  460.      end INIT
  461.  
  462.