home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / GRDBDL17.ZIP / HISTORY.ASM < prev    next >
Encoding:
Assembly Source File  |  1998-10-26  |  7.0 KB  |  383 lines

  1. ;
  2. ; GRDP
  3. ;
  4. ; Copyright(c) LADsoft
  5. ;
  6. ; David Lindauer, camille@bluegrass.net
  7. ;
  8. ;
  9. ; HISTORY.ASM
  10. ;
  11. ; Function: Handle command line history
  12. ;
  13.     ;MASM MODE
  14.     .MODEL SMALL
  15.     .386
  16. HISTSIZE EQU 1024    ; MUST be longer than longest command line!!!!
  17.             ; but shorter than 32K
  18.  
  19. include    eprints.inc 
  20. include    einput.inc 
  21. include    emtrap.inc 
  22. include    ebreaks.inc 
  23. include    eloader.inc
  24. include    edos.inc
  25. include    eoptions.inc
  26. include    eexec.inc
  27. include    ememory.inc
  28. include    elogging.inc
  29.  
  30.     PUBLIC CheckHistory, EnterHistory, LoadHistory, histoff,histon
  31.     .data
  32. oldhistory    db    0    ;to keep track of when history opt changes
  33.         db    0    ; safety net for pushing value
  34. histlen        dw    0    ;length of history segment
  35. histseg     dw    0    ;history segment itself
  36. histpos     dw    0    ;position in history segment
  37. nonext        db    0    ; flag to not do first find next after new
  38.                 ; command entered
  39.     .code
  40. histonm        db    13,10,"History enabled",0
  41. histoffm        db    13,10,"History disabled",0
  42. histerrm        db    13,10,"No mem for history",0
  43. histmsg     db    " (History access)",0
  44. ;
  45. ; turn history off temporarily
  46. ;
  47. histoff proc
  48.     mov    ax,word ptr [oldhistory]
  49.     xchg    ax,[esp]
  50.     mov    [oldhistory],0
  51.     jmp    ax
  52. histoff endp
  53. ;
  54. ; turn history back on
  55. ;
  56. histon    proc
  57.     pop    ax
  58.     pop    word ptr [oldhistory]
  59.     jmp    ax
  60. histon    endp
  61.     
  62. ; log that we are doing a history access
  63. ;
  64. CommandToLog PROC
  65.     push    di            ; save hist buffer index
  66.     sub    si,offset inputbuffer    ; get length of input
  67.     mov    cx,si             ; this many backspaces
  68.     jcxz    nobkspc
  69. clrlp:
  70.     mov    ah,2            ; we are bypassing the logger
  71.                     ; for this, in a moment we will
  72.                     ; log a message saying we did this
  73.     mov     dl,8
  74.     int    21h            ;
  75.     mov     dl,' '
  76.     int    21h            ;
  77.     mov     dl,8
  78.     int    21h            ;
  79.     loop    clrlp            ;
  80. nobkspc:
  81.     mov    bx,offset histmsg    ; now log a message saying we
  82. cllp:                    ; did this
  83.     mov    al,cs:[bx]
  84.     or    al,al
  85.     jz    clnd
  86.     inc    bx
  87.     call    LogToFile
  88.     jmp    cllp
  89. clnd:
  90.     pop    si            ; retreive hist buffer index
  91.     push    si
  92. cllp2:
  93.     lods    byte ptr es:[si]    ; play out the new string
  94.     or    al,al
  95.     jz    clnd2
  96.     mov    dl,al
  97.     call    PutChar
  98.     jmp    cllp2
  99. clnd2:
  100.     pop    di
  101.     ret
  102. CommandToLog ENDP
  103. ;
  104. ; go back one history line
  105. ;
  106. findprev proc
  107.     mov    di,[histpos]    ; start here
  108.     or    di,di        ; check if here is beginning
  109.     jnz    prevnowrap
  110.     add    di,[histlen]    ; yes,start at end
  111.  
  112. prevnowrap:
  113.     dec    di        ; point back one char
  114.     or    di,di
  115.     jle    zerprev        ; get out if past zero
  116.     test    byte ptr es:[di-1],0ffh    ; check for null terminator
  117.     jnz    prevnowrap    ; loop if not
  118.     mov    [histpos],di    ; else exit with pointer
  119.     ret
  120.  
  121. zerprev:
  122.     sub    di,di        ; zero pointer
  123.     mov    [histpos],di
  124.     ret
  125. findprev endp
  126. ;
  127. ; go forward one history line
  128. ;
  129. findnext proc
  130.     mov    di,[histpos]    ; start here
  131.     test    [nonext],1
  132.     jnz    nofindnext
  133. fnlp:
  134.     inc    di        ; inc di
  135.     cmp    di,[histlen]    ; see if past end
  136.     jae    zernext        ; yes, zero
  137.     test    byte ptr es:[di-1],0ffh    ; no, test for null term
  138.     jnz    fnlp            ; loop until found
  139.     mov    [histpos],di    ; exit with pointer
  140.     ret
  141. zernext:
  142.     sub    di,di           ;
  143.     mov    [histpos],di
  144.     ret
  145. nofindnext:
  146.     mov    [nonext],0
  147.     ret
  148. findnext endp
  149. ;
  150. ; compare two strings
  151. ;
  152. compare1 proc
  153.     jcxz    matches
  154.     push    cx
  155.     push    si
  156.     push    di
  157.     repe    cmpsb
  158.     pop    di
  159.     pop    si
  160.     pop    cx
  161.     ret
  162. matches:
  163.     sub    ax,ax
  164.     ret
  165. compare1 endp
  166. ;
  167. ; search to see if in history list
  168. ;
  169. search1    proc
  170.     push    es
  171.     mov    es,[histseg]
  172.     sub    di,di
  173.     mov    [histpos],di
  174. lp:
  175.     call    stringsize
  176.     mov    cx,ax
  177.     dec    cx
  178.     push    si
  179.     call    compare1
  180.     pop    si
  181.     jz    founds
  182.     call    findnext
  183.     or    di,di
  184.     jnz    lp
  185.     pop    es
  186.     stc
  187.     ret
  188. founds:
  189.     pop    es
  190.     clc
  191.     ret
  192. search1    endp
  193. ;
  194. ; find the size of a string, including null terminator
  195. ;
  196. stringsize proc
  197.     push    di
  198.     push    cx
  199.     mov    cx,-1
  200.     sub    al,al
  201.     repne    scasb
  202.     not    cx
  203.     mov    ax,cx
  204.     pop    cx
  205.     pop    di
  206.     ret
  207. stringsize endp
  208. ;
  209. ; string copy back to main buffer
  210. ;
  211. copystring proc
  212.     lodsb
  213.     or    al,al
  214.     jz    csend
  215.     stosb
  216.     jmp    copystring
  217. csend:
  218.     ret
  219. copystring endp
  220.  
  221.  
  222. CheckHistory PROC
  223.     push    di
  224.     push    si
  225.     push    es
  226.     test    [oldhistory],1    ;history on?
  227.     jz    okcheck
  228.     test    [histlen],-1    ; anything in history buf?
  229.     jz    okcheck
  230.     mov    cx,si        ; calculate length of input
  231.     sub    cx,di
  232.     mov    si,di
  233.     cmp    ax,3d00h    ; F3 key
  234.     jz    dof3
  235.     cmp    ax,5000h    ; down arrow
  236.     jz    up
  237.     cmp    ax,4800h    ; up arrow
  238.     clc
  239.     jnz    okcheck
  240. down:
  241.     mov    es,[histseg]    ; down arr, load stuf
  242.     mov    di,[histpos]
  243.     call    findprev    ; back one
  244.     jmp    found
  245. dof3:
  246.     mov    es,[histseg]
  247.     sub    di,di       ; F3, just go to last item
  248.     mov    [histpos],di
  249.     call    findprev
  250.     jmp    found
  251. up:
  252.     mov    es,[histseg]    ; up arr, load stuff
  253.     mov    di,[histpos]
  254.     call    findnext    ; forward one
  255.     jmp    found
  256. found:
  257.     call    CommandToLog    ; output the command
  258.     pop    es        ; found, swap seg regs
  259.     push    es
  260.     push    ds
  261.     mov    ds,[histseg]
  262.     mov    si,di               ; put string in input buffer
  263.     mov    di,offset inputbuffer
  264.     call    copystring
  265.     pop    ds
  266.     pop    es
  267.     pop    si
  268.     add    sp,2
  269.     stc
  270.     ret
  271. nokcheck:
  272.     stc
  273. okcheck:
  274.     pop    es
  275.     pop    si
  276.     pop    di
  277.     ret
  278.  
  279. CheckHistory ENDP
  280.  
  281. ;
  282. ; add a new line to the history segment
  283. ;
  284. EnterHistory PROC
  285.     test    [oldhistory],1        ; history on ?
  286.     jz    noenter
  287.     mov    cx,di            ; yes, any chars besides CR?
  288.     sub    cx,si
  289.     cmp    cx,1
  290.     jbe    noenter            ; no, get out
  291.     push    di                 ; is in hist list?
  292.     push    cx
  293.     push    [histpos]
  294.     call    search1
  295.     pop    [histpos]
  296.     pop    cx
  297.     pop    di
  298.     jnc    noenter            ; yes, don't put again
  299.     push    es
  300.     push    si
  301.     mov    es,[histseg]        ; ES = hist segment
  302. ehsl:
  303.     sub    di,di            ; di = pointer to first string
  304.     mov    ax,HISTSIZE        ; See if enough room for new string
  305.     sub    ax,[histlen]
  306.     sub    ax,cx
  307.     jnc    enoughroom        ; yes, go insert new string
  308.     push    cx            ; no, get size of first string
  309.     call    stringsize
  310.     mov    cx,HISTSIZE        ; count to move = seg size - string size
  311.     sub    cx,ax
  312.     mov    si,di
  313.     add    si,ax            ; SI = second string
  314.     push    ds
  315.     mov    ds,[histseg]
  316.     rep    movsb            ; cancel out first string
  317.     pop    ds
  318.     sub    [histlen],ax
  319.     pop    cx
  320.     jmp    ehsl            ; loop
  321. enoughroom:
  322.     mov    di,[histlen]        ; add new length to hist seg
  323.     pop    si
  324.     push    si
  325.     push    di
  326.     add    [histlen],cx
  327.     rep    movsb               ; concat new string
  328.     mov    byte ptr es:[di-1],0    ; null terminator
  329.     pop    di
  330.     pop    si
  331.     pop    es
  332.     mov    [histpos],0        ; pos = 0...
  333. noenter:
  334.     ret
  335. EnterHistory ENDP
  336. ;
  337. ; turn history on or off
  338. ;
  339. LoadHistory PROC
  340.     sub    ah,ah
  341.     mov    al,[opthist]          ; see if history flag changed
  342.     cmp    al,[oldhistory]
  343.     jz    nochange        ; get out if not
  344.     call    KillFiles        ; else clear mem state to allow
  345.     call    ReleaseMemory        ; the change
  346.     test    [opthist],0ffh        ; turning on?
  347.     jnz    doload            ; yes, turn on
  348.     push    es
  349.     mov    es,[histseg]        ; else unload history seg
  350.     mov    ah,49h
  351.     int    21h
  352.     mov    [oldhistory],0        ; flag off
  353.     pop    es
  354.     mov    bx,offset histoffm    ; history off message
  355.     jmp    reload
  356. doload:
  357.     mov    bx,HISTSIZE SHR 4    ; load history seg
  358.     mov    ax,4800h
  359.     int    21h
  360.     mov    bx, offset histerrm
  361.     mov    [opthist],0        ; assume it didn't succeed
  362.     jc    reload
  363.     mov    [oldhistory],1        ; turn on history
  364.     mov    [opthist],1
  365.     mov    [histseg],ax        ; init the vars
  366.     mov    [histlen],0
  367.     mov    [histpos],0
  368.     mov    [nonext],1
  369.     mov    bx, offset histonm    ; history on message
  370. reload:
  371.     call    olMessage
  372.     mov    si,offset grdbname    ; reload empty program
  373.     call    MakeEmptyProg
  374.     sub    ax,ax
  375.     test    [loadfile],0ffh
  376.     jz    nochange
  377.     call    LoadProgram        ; reload user program
  378.     lahf
  379. nochange:
  380.     ret
  381. LoadHistory ENDP
  382.  
  383. end