Pastey A portable dsearch.exw

--****
-- === win32/dsearch.exw 
--  
-- search for a .DLL that contains a given routine 
-- ==== Usage 
-- {{{ 
-- eui dsearch [routine]  
-- }}} 
-- 
-- If you don't supply a string on the command line you will be prompted  
-- for it.  
-- 
 
-- A list of files and directories located where we expect to find  
-- dynamic loading libraries 
sequence dll_list 
dll_list = {} 
 
include std/filesys.e 
include std/dll.e 
include std/machine.e 
 
-- because various operating systems use distinct extensions for dlls we cannot 
-- use *.so in the library listing.  So, we must later ignore if it cannot open 
-- files (as we don't know which ones are valid) 
sequence file_list = dir(`c:\windows\system32\*.dll`) & dir(`/usr/lib/*`)  
	& dir(`/usr/local/lib/*`) 
for i = 1 to length(file_list) do 
	if atom(file_list[i]) then 
		continue 
	end if 
	dll_list = append( dll_list, file_list[i][D_NAME] ) 
end for	 
 
constant KEYBOARD = 0, SCREEN = 1 
 
type enum boolean 
	TRUE = 1, FALSE = 0 
end type 
 
sequence cmd, orig_string 
 
integer scanned, no_open 
scanned = 0 
no_open = 0 
 
 
atom string_pointer 
sequence routine_name 
 
function scan(sequence file_name) -- as boolean 
-- process an eligible file 
    atom lib 
    lib = open_dll(file_name) 
    if lib = 0 then 
	no_open += 1 
	ifdef WINDOWS then  
	    puts(SCREEN, file_name & ": Couldn't open.\n") 
        end ifdef 
	return FALSE 
    end if 
    scanned += 1 
    if define_c_var(lib, routine_name) != -1 then 
	printf(SCREEN, "%s: ", {file_name}) 
	printf(SCREEN, "\n\n%s was FOUND in %s\n", {routine_name, file_name}) 
	return TRUE 
    end if 
    return FALSE 
end function 
 
function delete_trailing_white(sequence name) -- as sequence 
-- get rid of blanks, tabs, newlines at end of string 
    while length(name) > 0 do 
	if find(name[length(name)], "\n\r\t ") then 
	    name = name[1..length(name)-1] 
	else 
	    exit 
	end if 
    end while 
    return name 
end function 
 
cmd = command_line()   -- eui dsearch [string] 
 
if length(cmd) >= 3 then 
    orig_string = cmd[3] 
else 
    puts(SCREEN, "C function name:") 
    orig_string = delete_trailing_white(gets(KEYBOARD)) 
    puts(SCREEN, '\n') 
end if 
 
routine_name = orig_string 
 
procedure locate(sequence name) 
    routine_name = name 
    puts(1, "Looking for " & routine_name & "\n ") 
    for i = 1 to length(dll_list) do 
	if scan(dll_list[i]) then 
	    if getc(KEYBOARD) then 
	    end if 
	    abort(1) 
	end if 
    end for 
    puts(1, '\n') 
end procedure 
 
if length(routine_name) = 0 then 
    abort(0) 
end if 
 
locate(orig_string) 
ifdef WINDOWS then 
	locate(orig_string & "A") 
	locate(orig_string & "Ex") 
	locate(orig_string & "ExA") 
end ifdef 
 
puts(1, "\nCouldn't find " & orig_string & '\n') 
puts(1, "Press Enter\n") 
 
if getc(KEYBOARD) then 
end if