diff options
author | bptato <nincsnevem662@gmail.com> | 2023-10-09 00:29:01 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-10-09 00:29:01 +0200 |
commit | 4b1fd6425ef22163748c042d5b185480b6a16d03 (patch) | |
tree | 6a74f0584dfc487fd24d4e2c91d8daa7b7ec400b /bonus | |
parent | 8b0f8da9e6d5a2a4678a3886462048f110e51764 (diff) | |
download | chawan-4b1fd6425ef22163748c042d5b185480b6a16d03.tar.gz |
improve trans.cgi
document, simplify, ...
Diffstat (limited to 'bonus')
-rw-r--r-- | bonus/trans.cgi | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/bonus/trans.cgi b/bonus/trans.cgi index f40ed893..87bb8bee 100644 --- a/bonus/trans.cgi +++ b/bonus/trans.cgi @@ -2,12 +2,30 @@ # Needs https://github.com/soimort/translate-shell to work. # Usage: cgi-bin:trans.cgi?word -TEXT="$(echo "$QUERY_STRING" | sed 's/+/ /g;s/%/\\x/g' | xargs -0 printf "%b")" +decode() { + # URL-decode the string passed as the first parameter + printf '%s\n' "$1" | \ + sed 's/+/ /g;s/%/\\x/g' | \ + xargs -0 printf "%b" +} + +# QUERY_STRING is URL-encoded. We decode it using the decode() function. +TEXT="$(decode "$QUERY_STRING")" + +# Write a Content-Type HTTP header. The `trans' command outputs plain text, +# so we use text/plain. printf 'Content-Type: text/plain\n' -type trans || { - printf "\n\nERROR: translator not found" - exit +# We must write a newline here, so Chawan knows that all headers have been +# written and incoming data from now on belongs to the body. +printf '\n' + +# Check if the `trans' program exists, and if not, die. +type trans >/dev/null || { + printf "ERROR: translator not found" + exit 1 } -printf '\n%s\n' "$(trans "$TEXT")" +# Call the `trans' program. It writes its output to standard out, which +# Chawan's local CGI will read in as the content body. +trans "$TEXT" |