home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fwktl101.zip / SELFWRIT.ASM < prev    next >
Assembly Source File  |  1996-05-28  |  5KB  |  177 lines

  1.  
  2.  
  3.         TITLE   SELFWRIT
  4.         NAME    SELFWRIT
  5.         .386
  6.         .387
  7.  
  8. ; Part of the FWKTL(TM) Text_program Launcher kit, version 1.01.
  9. ; (C)Copyright Frederick W. Kantor 1996. All rights reserved.
  10.  
  11. ; this program can store up to 32 bytes from its command tail in an
  12. ; internal array, and then overwrite its program file with that new
  13. ; material, or can display what it has currently in that array
  14. ; Usage:  FWKTL SELFWRIT.COM <enter>  to display current array contents
  15. ;         FWKTL SELFWRIT.COM string to load/w <enter>  to revise contents
  16.  
  17. TEXT32  SEGMENT DWORD PUBLIC 'CODE'
  18.         ASSUME  CS:FLAT, DS:FLAT
  19.         ALIGN   4
  20. MAIN:
  21. PSETUP EQU 01     ; = 1  include address for SHOW utility
  22.  
  23. ; structure for initial data:
  24.  
  25. P_ STRUC
  26. P_LOADEDAT DD ?   ; start of this memory block
  27. P_PWHENCE DD ?    ; points to ASCIIZ string re where program was found
  28. P_PCOMTAIL DD ?   ; points to ASCIIZ command tail
  29. P_GETFN DD ?      ; address for indirect call to FWKTL GETFN function:
  30. P_GETFNLIST DD ?  ; address for indirect call to FWKTL GETFNLIST
  31. P_HDOSCALLS DD ?  ; handle for DOSCALLS module as loaded by FWKTL,
  32. IF PSETUP EQ 1
  33. P_SHOW DD ?        ; address for FWKTL SHOW function;
  34. P_USWORDCAPS DD ?  ; address for FWKTL capitalization function;
  35. ENDIF ; PSETUP EQ 1
  36.  
  37. P_WROTE DD ?       ; used with the DOS32WRITE API
  38. P_HNL   DD ?       ; file handle
  39.  
  40. P_WRITE DD ?
  41. P_CLOSE DD ?
  42. P_OPEN  DD ?
  43.  
  44. P_ ENDS
  45.  
  46. CODESTART:   ; used as a reference point
  47.  
  48. RET          ; 4_byte header identification string
  49. DB 'FWK'     ;
  50. DD PSETUP    ; style
  51. DD TYPE P_   ; amount of free memory requested after code
  52. JMP SHORT @F ; execution entry point.
  53.  
  54. STORAGE:
  55. OSTORAGE EQU $-CODESTART
  56. DB 020H DUP (0)
  57. LSTORAGE EQU $-STORAGE
  58. DB 00
  59.  
  60. FNLIST:                 ; list of procedures (DOSCALLS ordinals).
  61. OFNLIST EQU $-CODESTART ; offset used for finding FNLIST in memory.
  62. ; these decimal number WORDs are in the same order
  63. ; as their corresponding DD targets in P_ STRUC
  64.         ;                      decimal ordinals
  65. DW 282  ;   DOS32WRITE          (DOSCALLS.282)
  66. DW 257  ;   DOS32CLOSE          (DOSCALLS.257)
  67. DW 273  ;   DOS32OPEN           (DOSCALLS.273)
  68. NFNLIST EQU ($-FNLIST)/2 ; number of items in FNLIST
  69.  
  70. @@:                       ; target for JMP from entry point
  71.  
  72. PUSH EBX                  ; save EBX value
  73. MOV EBP,ESP               ; save ESP value in EBP
  74. REP MOVSD                 ; load EBX:P_ STRUC
  75.  
  76. MOV ESI,[EBX].P_LOADEDAT  ; calculate position of start of FNLIST
  77. ADD ESI,OFNLIST           ;   procedure list
  78. LEA EDI,[EBX].P_WRITE     ; point EDI at address array
  79. MOV EAX,02                ; FNLIST list contains 2_byte ordinals
  80. MOV ECX,NFNLIST           ; number of items in list
  81. MOV EDX,[EBX].P_HDOSCALLS ; use handle for DOSCALLS module,
  82. CALL [EBX].P_GETFNLIST    ; get addresses for procedures in list
  83.  
  84. MOV ESI,[EBX].P_PCOMTAIL  ; search command tail for /W
  85. MOV EDI,ESI
  86. MOV ECX,0200H
  87. XOR EAX,EAX               ; find terminal 00
  88. REPNE SCASB               ;
  89. MOV ECX,EDI
  90. DEC EDI                   ;
  91. MOV AL,'/'
  92. SUB ECX,ESI
  93. STD
  94. REPNE SCASB
  95. CLD
  96. JNE SHORT L0              ; if no '/', then jmp to SHOW
  97.  
  98. INC EDI
  99. MOV BYTE PTR[EDI],00      ; terminate ASCIIZ string
  100.  
  101. PUSH ESI
  102. MOV ESI,EDI
  103. INC ESI
  104. CALL [EBX].P_USWORDCAPS   ; capitalize
  105. CMP BYTE PTR[ESI],'W'
  106. POP ESI
  107. JNE SHORT L0              ; if not /W, then skip to use SHOW
  108.  
  109. MOV ECX,EDI
  110. SUB ECX,ESI
  111. MOV EDI,LSTORAGE
  112. CMP ECX,EDI
  113. JNA SHORT @F
  114. MOV ECX,EDI
  115. @@:
  116.  
  117. MOV EDI,[EBX].P_LOADEDAT ; point to STORAGE
  118. ADD EDI,OSTORAGE         ;
  119. PUSHAD                   ; save values
  120. MOV ECX,LSTORAGE         ; clear STORAGE
  121. XOR EAX,EAX              ;
  122. REP STOSB                ;
  123. POPAD                    ; restore values
  124.  
  125. REP MOVSB                ; copy into STORAGE
  126.  
  127. PUSHD 0                  ; for opening an existing file
  128. PUSHD 042H               ; bits 6-4  = 100 = 4h OPEN_SHARE_DENYNONE
  129.                          ; bits 2-0  = 010 = 2h OPEN_ACCESS_READWRITE
  130. PUSHD 1H                 ; OPEN_ACTION_OPEN_IF_EXISTS
  131. PUSHD 7H                 ; FILE_SYSTEM | FILE_HIDDEN | FILE_READONLY
  132. PUSHD 0                  ; for opening an existing file
  133. LEA EAX,[EBX].P_WROTE    ; pDWORD for report
  134. PUSHD EAX
  135. LEA EAX,[EBX].P_HNL      ; where to save file handle
  136. PUSHD EAX
  137. PUSHD [EBX].P_PWHENCE    ; ASCIIZ d:\path\filename.ext
  138. CALL [EBX].P_OPEN        ; open
  139. TEST EAX,EAX             ; test
  140. JNZ SHORT BADEXIT        ; leave if bad
  141.  
  142. MOV EBX,[EBP]
  143.                          ; overwrite program file
  144. LEA EAX,[EBX].P_WROTE    ; pDWORD for amount written
  145. PUSHD EAX                ;
  146. PUSHD CODE_END-CODESTART ; amount to write
  147. PUSHD [EBX].P_LOADEDAT   ; push address of beginning of program
  148. PUSHD [EBX].P_HNL        ; use file handle
  149. CALL [EBX].P_WRITE       ; indirect call to DOS32WRITE to write message
  150.  
  151. MOV EBX,[EBP]            ;
  152.  
  153. PUSHD [EBX].P_HNL        ; close file handle
  154. CALL [EBX].P_CLOSE       ;
  155.  
  156. JMP SHORT EXIT
  157.  
  158. L0:
  159. MOV ESI,[EBX].P_LOADEDAT  ; display existing ASCIIZ string
  160. ADD ESI,OSTORAGE
  161. CALL [EBX].P_SHOW
  162.  
  163. EXIT:
  164. XOR EAX,EAX               ; set exit errorlevel = 0
  165.  
  166. BADEXIT:
  167. MOV ESP,EBP               ; clear STACK
  168. POP EBX                   ;
  169. RET                       ; return to FWKTL
  170. ;---------------------------
  171. CODE_END EQU $
  172.  
  173. DB 'FWKEOF',0,1           ; EOF signature for use with FWKTRIM
  174.  
  175. TEXT32 ENDS
  176. END MAIN
  177.