home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / ASM / ALIB30A / SCAN.ASM < prev    next >
Assembly Source File  |  1994-12-03  |  9KB  |  381 lines

  1. ;*****************************   SCAN.ASM  **********************************
  2. PAGE  70,132
  3. comment 
  4.                                 SCAN
  5.                              -------------
  6.  
  7.      Purpose:
  8.      --------
  9.  
  10.      Scan disk for string.
  11.  
  12.      Using newscan
  13.      ----------
  14.      SCAN <files/path> <pattern>
  15.  
  16.      SCAN starts searching from the current directory and checks each
  17.      each file which matches the <files/path> specification for the
  18.      <pattern>
  19.  
  20.      Example:  To scan all .ASM files in the current directory
  21.                for include files:
  22.  
  23.                      SCAN *.asm include
  24.  
  25.                To scan all the files in the current and its subdirectories
  26.                for include files:
  27.  
  28.                      SCAN *.* include             
  29.  
  30.      The <files/path> parameter applies to both files and subdirectories.
  31.      Thus, if you type "SCAN X*.* include" it will look in the current
  32.      directory and all subdirectories which begin with "X" for files
  33.      which begin with "X"
  34.      
  35.      Compiling
  36.      ---------
  37.  
  38.      The commands needed to build newscan.EXE using MASM are:
  39.         masm newscan;
  40.         link newscan,newscan,,alib.lib;
  41. 
  42.      
  43.     include    mac.inc
  44.     include    common.inc
  45. ;-----------------------------------------------------------------------------
  46.     extrn    library_setup:far
  47.     extrn    library_terminate:far    
  48.     extrn    dos_mem_allocate:far
  49.     extrn    dos_mem_release:far
  50.     extrn    walk_path:far
  51.     extrn    scan_block_fopen:far
  52.     extrn    scan_block_fast:far
  53.     extrn    scan_block_fclose:far
  54.     extrn    parse_first:far
  55.     extrn    parse_next:far
  56.     extrn    stdout_string:far
  57.     extrn    stdout_crlf:far
  58.     extrn    stdout_spaces:far
  59.     extrn    stdout_char:far
  60.     extrn    is_text:far
  61.     extrn    is_stdout_console:far
  62.     extrn    key_read:far
  63. ;------------------------------------------------------------------------------
  64. code        segment byte
  65.         assume    cs:code, ds:code
  66. ;-----------------------------------------------------------------------------
  67. ;
  68.  
  69. data_area    struc
  70. file_mask    db    13 dup (?)
  71. compare_string    db    40 dup (?)
  72. match_posn    dw    ?        ;split buffer match handling
  73.  
  74. fname_off    dw    ?
  75. fname_seg    dw    ?
  76. file_handle    dw    ?
  77.  
  78. ;buffer_size    equ    512 * 125
  79. buffer_size    equ    512 * 100
  80. buffer        db    buffer_size dup (?)
  81. last_read_amount dw    ?
  82.  
  83. match_line    db    76 dup (?)
  84. open_flag    db    ?            ;set to one if new file opened
  85.  
  86. stdout_flag    db    ?            ;0=redirected 2=console
  87.  
  88.         dw    300 dup (?)        ;stack
  89. stack_        dw    ?
  90. data_area    ends
  91.  
  92. pspseg        dw    0
  93. data_seg    dw    0
  94. ;-----------------------------------------------------------------------------
  95. start:
  96.     cli
  97.     mov    cs:pspseg,es    ;save PSP segment
  98.     mov    ax,cs        ;get CODE segment
  99.     mov    ss,ax
  100.     mov    ds,ax
  101.     mov    es,ax
  102.     mov    sp,offset stack_
  103.     sti
  104.     
  105. ; next, release memory beyond the end of the program
  106. ; The  definition for ZSEG marks the
  107. ; end of the program's code, data and stack area.
  108. ; When linking be sure ZSEG is at the end of the program.
  109.  
  110.     mov    ax,zseg
  111.  
  112.     mov    bx,cs:pspseg        ;
  113.     mov    es,bx
  114.     sub    bx,ax
  115.     neg    bx            ; size of program in paragraphs
  116.     mov    ah,4Ah            ; resize memory block
  117.     int    21h
  118.  
  119.     mov    ax,cs
  120.     mov    es,ax
  121. ;
  122. ; check if enough memory free to run program
  123. ;
  124.     mov    ax,pspseg        ;pass psp segment to setup
  125.     mov    bx,8            ;number of floating point variables
  126.     call    library_setup
  127.     cmp    ax,128
  128.     jae    got_enough_mem        ;jmp if 128k of memory avail
  129. ;;    mov    al,7
  130. ;;    mov    ah,fatal_return
  131. ;;    call    lib_error_handler
  132.     jmp    exitx
  133.     
  134. got_enough_mem:
  135.     mov    ax,size data_area
  136.     mov    dx,0
  137.     call    dos_mem_allocate
  138.     mov    cs:data_seg,es
  139.     push    es
  140.     pop    ds
  141. ;
  142. ; clear data area
  143. ;
  144.     cld
  145.     mov    al,0
  146.     mov    cx,size data_area
  147.     mov    di,0
  148.     rep    stosb
  149.  
  150.     mov    di,offset file_mask
  151.     call    parse_first
  152.  
  153.     mov    di,offset compare_string
  154.     call    parse_next
  155.  
  156.     call    is_stdout_console
  157.     mov    [ds:stdout_flag],al
  158.  
  159.     mov    si,offset compare_string
  160.     mov    dl,20h            ;match either case
  161.     call    scan_block_fopen
  162.                 
  163.     mov    si,offset file_mask
  164.     mov    ax,offset our_process
  165.     mov    cx,10h            ;walk directories also
  166.     call    FAR PTR walk_path
  167.  
  168.     call    scan_block_fclose
  169.     
  170. ; normal program exit
  171.  
  172. exitx:    mov    es,cs:data_seg
  173.     call    dos_mem_release
  174.     mov    ax,1
  175.     call    library_terminate
  176.     mov    ax,4C00h
  177.     int    21h
  178.  
  179. ;----------------------------------------------------------------------
  180. ; our_process - process each file found by walk_path
  181. ;  inputs:  es:di point at fully quailified filename including drive.
  182. ;                 Also, the path is set to same location as file.
  183. ;
  184. our_process    proc    far
  185.     push    es
  186.     pop    ds
  187.     mov    dx,di
  188.     add    dx,2        ;move past drive
  189.     mov    ax,3d00h    ;open file for read-only
  190.     int    21h
  191.     mov    ds,[cs:data_seg]
  192.     mov    [ds:open_flag],1 ;signal we opened a new file
  193.     mov    [ds:fname_off],di
  194.     mov    [ds:fname_seg],es
  195.     mov    es,[cs:data_seg]
  196.     mov    [ds:file_handle],ax
  197.     mov    di,offset compare_string
  198. ;
  199. ; read the file
  200. ;
  201. rd_lp:    mov    [ds:match_posn],di
  202.     mov    bx,[ds:file_handle]
  203.     mov    cx,buffer_size
  204.     mov    dx,offset buffer
  205.     mov    ah,3fh
  206.     int    21h
  207.     jc    exit_and_close
  208.     cmp    ax,0
  209.     je    exit_and_close
  210.     mov    [ds:last_read_amount],ax
  211. ;
  212. ; search the buffer for string
  213. ;
  214.     mov    cx,ax            ;length of buffer
  215.     mov    si,offset buffer
  216. mt_lp:    call    scan_block_fast
  217.     cmp    ax,0
  218.     jne    no_match
  219. ;
  220. ; we have found a match.  ds:si points at match end
  221. ;
  222.     cmp    [ds:open_flag],1
  223.     jne    show_match        ;jmp if file name already displayed
  224.     mov    [ds:open_flag],0
  225.  
  226.     apush    si,di,ds    
  227.     call    linefeed
  228.     lds    si,dword ptr [ds:fname_off]    ;display the file name
  229.     call    stdout_string
  230.     call    linefeed
  231.     apop    ds,di,si
  232. show_match:
  233.     call    display_match
  234.     jcxz    rd_lp
  235.     jmp    mt_lp    
  236.  
  237. ;
  238. ; no match was found, check for partial match,
  239. ;
  240. no_match:
  241.  
  242. ;
  243. ; check if more data in file
  244. ;
  245.     cmp    [ds:last_read_amount],buffer_size
  246.     je    rd_lp                ;jmp if more data in file
  247. ;
  248. ; close the file and exit
  249. ;
  250. exit_and_close:
  251.     mov    bx,[ds:file_handle]
  252.     mov    ah,3eh
  253.     int    21h    
  254.     retf
  255. our_process    endp    
  256. ;-----------------------------------------------------------------------------
  257. ; display match line
  258. ;   inputs:  ds:si point at match
  259. ;               cx = amount of data remaining in buffer
  260. ;   output:  match_line has line with match text, and it is sent to stdout
  261. ;
  262. display_match    proc    near
  263.     apush    cx,dx,si,di,bp
  264.     push    ds
  265.     pop    es
  266.     mov    ah,4
  267.     call    stdout_spaces
  268. ;
  269. ; register use: si=left end  di=right end  bp=amount collected
  270. ;    
  271.     mov    bp,0            ;init amount of data collected
  272.     mov    di,si            ;init right end
  273.     mov    ah,0            ;set mode for IS_TEXT
  274. ;
  275. ; go left and collect data
  276. ;
  277. left_lp:cmp    si,offset buffer
  278.     je    got_left        ;jmp if can't go left
  279.     mov    al,ds:[si-1]
  280.     call    is_text
  281.     jc    got_left        ;jmp if not text
  282.     inc    bp            ;bump amount collected
  283.     dec    si            ;move ptr to new char posn
  284.     cmp    bp,75
  285.     jb    left_lp            ;jmp if not at limit yet
  286. ;
  287. ; now  scan right
  288. ;
  289. got_left:
  290.     jcxz    got_right        ;jmp if no data in buffer
  291.     mov    al,ds:[di]        ;get next char
  292.     call    is_text
  293.     jc    got_right
  294.     inc    bp
  295.     inc    di
  296.     dec    cx
  297.     jcxz    got_right
  298.     cmp    bp,75
  299.     jb    got_left
  300. got_right:
  301.     mov    cx,bp            ;amount of data collected
  302.     mov    di,offset match_line
  303.     cld
  304. ;
  305. ; move data to local buffer
  306. ;
  307. mv_lp:    lodsb
  308.     cmp    al,9                ;check for tab
  309.     jne    moveon
  310.     mov    al,' '
  311. moveon:    stosb
  312.     loop    mv_lp
  313.     mov    byte ptr [di],0            ;put zero at end
  314.     mov    si,offset match_line
  315.     call    stdout_string
  316.     call    linefeed
  317.  
  318.     apop    bp,di,si,dx,cx
  319.     ret
  320. display_match    endp
  321. ;-------------------------------------------------------------------------
  322. ; linefeed - move to next display line, count line
  323. ;  inputs: none
  324. ;
  325. line_counter    db    0
  326. pause_msg    db    '(ESC=abort, any other key to continue)',0
  327.  
  328. linefeed:
  329.     apush    ax,si,ds
  330.     mov    ax,cs
  331.     mov    ds,ax
  332.     
  333.     call    stdout_crlf
  334.     mov    al,cs:line_counter
  335.     inc    al
  336.     cmp    al,23
  337.     jb    lf_exit
  338.     cmp    es:stdout_flag,0
  339.     je    lf_exit1        ;jmp if console redirected
  340.     mov    si,offset pause_msg
  341.     call    stdout_string
  342.     call    key_read
  343.     cmp    ax,3
  344.     je    abort
  345.     cmp    ax,1bh
  346.     je    abort
  347.     mov    al,0dh
  348.     call    stdout_char
  349.     mov    ah,40
  350.     call    stdout_spaces
  351.     mov    al,0dh
  352.     call    stdout_char
  353. lf_exit1:
  354.     mov    al,0    
  355. lf_exit:
  356.     mov    cs:line_counter,al
  357.     apop    ds,si,ax
  358.     ret
  359.  
  360. abort:    jmp    exitx            ;!! beware this exits program    
  361. code        ends
  362.  
  363. ;-------------------------------------------------------------------------
  364. ;
  365. ; This segment definition is needed so linker will put the LIBSEG here
  366. ; before the ZSEG.  We want ZSEG to be last so memory allocation will
  367. ; work correctly.
  368. ;
  369. LIBSEG           segment byte public 'LIB'
  370.     assume    cs:LIBSEG
  371. LIBSEG    ENDS
  372.  
  373. ;-------------------------------------------------------------------------
  374. ; zseg must be at the end of the program for memory allocation from
  375. ; DOS.
  376. ;
  377. zseg    segment    para public 'ZZ'
  378. zseg    ends
  379.  
  380.         end    start
  381.