#!./eui -- purpose: search engine for website, using "cgi-bin" directory and "GET" method -- language: RapidEuphoria 3.1.1 -- author: shian -- version: 1.03 -- note: using OpenEuphoria 64-bit interpreter eui version 4.1 (64-bit for the server), -- compiled by James Cook. without warning include get.e -- with trace -- trace(3) -- trace(1) function search_in_docs(sequence path, sequence ext, sequence docs, sequence text) -- search "text" in all files; -- return list sequence of {{"file-pathname", "line-match-text"}, ...} sequence filename, match_files integer fn object line match_files = {} for i = 1 to length(docs) do filename = path & docs[i] & ext fn = open(filename, "r") if fn != -1 then while 1 do line = gets(fn) if atom(line) then close(fn) exit elsif match(text, line) then match_files = append(match_files, {filename, line}) end if end while end if end for return match_files end function procedure print_HTMLheader(sequence back_url) -- Start of HTML output to user puts(1, "Content-type: text/html\n\n") puts(1, "\n") puts(1, "\n") puts(1, "\nSearch Results\n") puts(1, "\n") puts(1, "\n") puts(1, "\n") puts(1, "\n\n") puts(1, "\n\n") -- nav puts(1, "
\n") puts(1, "
\"logo\"Rapid Euphoria 3.1.1
\n") puts(1, "\n
\n\n") -- section puts(1, "
\n") puts(1, "

⮨ Back


\n\n") end procedure procedure print_HTMLfooter(sequence back_url) -- End of HTML output to user puts(1, "

⮨ Back


\n\n") puts(1, "
\n\n") -- footer puts(1, "\n\n") -- puts(1, "\n") end procedure procedure print_HTMLerror(sequence msg) -- Report fatal error puts(1, "
An error occurred. Please report a bug in Rapid Euphoria 3.1.1 Forum.

\n") puts(1, "" & msg & "

\n") end procedure function no_html(sequence s) -- remove '<' and '>' from html string s integer p, st p = 1 st = 1 while p do p = find_from('<', s, st) if p then s = s[1..p-1] & "<" & s[p+1..$] st += 4 end if p = find_from('>', s, st) if p then s = s[1..p-1] & ">" & s[p+1..$] st += 4 end if end while return s end function procedure print_HTMLno_result(sequence text) -- Report match not found puts(1, "
\"" & no_html(text) & "\" not found! Please try again.

\n") puts(1, "This search engine is case sensitive! (i.e. 'a' is not 'A').
\n") puts(1, "You may also search for UTF-8 characters and HTML tags (e.g. <br>).
\n") puts(1, "Only exact word, part of word, or phrase will be found.

\n") end procedure procedure print_HTMLresult(sequence list, sequence text) -- print list of results: link to page & match line printf(1, "

%d Search results for \"" & no_html(text) & "\":

\n", length(list)) puts(1, "
    \n") for i = 1 to length(list) do puts(1, "
  1. " & list[i][1] & "
    \n" & no_html(list[i][2]) & "

  2. \n") end for puts(1, "
\n\n") end procedure function upper(object x) -- convert atom or sequence to upper case return x - (x >= 'a' and x <= 'z') * 32 end function function parse_input(sequence query) -- convert '+' and hexa characters of query to the real char integer p, st sequence v -- convert all '+' to space p = 1 while p do p = find('+', query) if p then query[p] = ' ' end if end while -- convert all hexa (%2B) to value p = 1 st = 1 while p do p = find_from('%', query, st) if p and p + 2 <= length(query) then v = value('#' & upper(query[p+1..p+2])) if v[1] = GET_SUCCESS then query = query[1..p-1] & v[2] & query[p+3..$] st = p end if end if st += 1 end while return query end function function read_input() -- read the data sent from the Web browser using the "GET" method -- return "text" to search for, or "" if error object query integer p query = getenv("QUERY_STRING") -- query = "Search=
&%D0" -- debug -- parse text if not atom(query) then p = find('=', query) if p then query = query[p + 1..$] -- "text" to search for query = parse_input(query) return query end if end if return "" -- error? end function global procedure main(sequence path, sequence ext, sequence docs, sequence back_url) -- main() runs the search engine sequence text, list text = read_input() -- text to search for print_HTMLheader(back_url) if length(text) then list = search_in_docs(path, ext, docs, text) if length(list) then print_HTMLresult(list, text) else print_HTMLno_result(text) end if else print_HTMLerror("Search string is empty (\"\")!") end if print_HTMLfooter(back_url) end procedure