about summary refs log tree commit diff stats
path: root/bonus/trans.cgi
blob: bfaca71312c7d49aec23dd614af5965d3f3b2384 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/sh
# Needs https://github.com/soimort/translate-shell to work.
# Usage: cgi-bin:trans.cgi?word

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'

# 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
}

# 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"