diff options
Diffstat (limited to 'search_cgi')
-rwxr-xr-x | search_cgi | 127 |
1 files changed, 103 insertions, 24 deletions
@@ -1,35 +1,114 @@ #!/bin/sh -echo "Content-type: text/html\n\n" +##func +printlines (){ + echo "<ul>\n" + l=$(echo "$@" | tr ' ' '\n') + for r in $l; do + echo "<li><div><a href=\"$r\" target='new'>$r</a></div></li>\n" + done + echo "</ul>\n" +} -query="${QUERY_STRING}" -echo "<html><head><style type='text/css'>" -echo "body{padding: 20px; margin: 40px auto;line-height: 1.6;font-size: 18px; color:#444;}" -echo "</style></head>" -echo "<body><div>Search for: </div><form method='GET' action='/cgi-bin/search_cgi'>" -echo "<input type='text' name='query'></input>" -echo "<input type='submit' value='Search!'></input>" -echo "</form>" +printf "Content-type: text/html\n\n" +query=$(echo "${QUERY_STRING}" | sed -E 's/[\$^?<>@;\*`\|]//g') -terms=$(echo "${query}" | tr '&' '\n' | grep -E "^query" | sed -E 's/^query=//') +##func +print_head(){ + echo "<html><head><style type='text/css'>" + echo "body{padding: 20px; margin: 40px auto;line-height: 1.6;font-size: 18px; color:#444;}" + echo "</style></head>" + echo "<body><div>Search for: </div><form method='GET' action='/cgi-bin/search_cgi'>" + echo "<div><input type='text' name='query'></input></div>" + echo "<div>in: <input type='radio' name='type' value='url' checked>url</input>" + echo "<input type='radio' name='type' value='descr'>description</input>" + echo "<input type='radio' name='type' value='read'>readme</input>" + echo "<input type='radio' name='type' value='all'>all</input>" + echo "</div>" + echo "<input type='submit' value='Search!'></input>" + echo "</form>" -if [ -z "$terms" ]; then - exit 0 -else +} + +##func +print_foot(){ + echo "</body></html>" +} + +terms=$(echo "${query}" | tr '&' '\n' | grep -E "^query=" | sed -E 's/^query=//') +qtype=$(echo "${query}" | tr '&' '\n' | grep -E "^type=" | sed -E 's/^type=//') + +print_head + +if [ -n "$terms" ]; then search=$(echo "$terms" | sed -E 's/\+/ /g') - res=$(./search_repo ./ $search ) - if [ -z "$res" ]; then - echo "<p>No results for \"$search\":" - exit 0; - else - echo "<p>Results for \"$search\"" - echo "<ul>\n" - for r in $res; do - echo "<li><div><a href=\"$r\" target='new'>$r</a></div></li>\n" - done - echo "</ul>" + numres=0 + lines=$(./search_repo ./ "$search" 2>&1 ) + for line in $lines; do + case "$line" in + "--URL") + #echo "$line<br>" + if [ "$qtype" = "url" -o "$qtype" = "all" ]; then + curstr="url" + else + curstr="" + fi + ;; + + "--DESCR") + if [ -n "$curstr" ]; then + if [ -n "$results" ]; then + printf "<div>%s results in repo %s</div>\n" $numres $curstr + printlines "$results" + else + printf "<div>No results in repo %s</div>\n" $curstr + fi + fi + results="" + numres=0 + if [ "$qtype" = "descr" -o "$qtype" = "all" ]; then + curstr="description" + else + curstr="" + fi + ;; + + "--README") + #echo "$line<br>" + if [ -n "$curstr" ]; then + if [ -n "$results" ]; then + printf "<div>%s results in repo %s</div>\n" $numres $curstr + printlines "$results" + else + printf "<div>No results in repo %s</div>\n" $curstr + fi + fi + results="" + numres=0 + if [ "$qtype" = "read" -o "$qtype" = "all" ]; then + curstr="readme" + else + curstr="" + fi + ;; + + *) + numres=$(($((numres)) + 1)) + results="$results $line" + ;; + esac + done + if [ -n "$curstr" ]; then + if [ -n "$results" ]; then + printf "<div>%s results in readme files</div>\n" $numres + printlines $results + else + printf "<div>No results in readme files </div>\n" $curstr + fi fi fi + +print_foot |