#!/usr/bin/env sh


readonly 'VERSION=w3mabouturi 1.4.6'



set -efu
umask '0022'
readonly "LC_ALL_ORG=${LC_ALL-}"
LC_ALL='C'
IFS=$(printf ' \t\n_')
IFS="${IFS%_}"
PATH="${PATH-}${PATH:+:}$(command -p getconf 'PATH')"
UNIX_STD='2003'         # HP-UX POSIX mode
XPG_SUS_ENV='ON'        # AIX POSIX mode
XPG_UNIX98='OFF'        # AIX UNIX 03 mode
POSIXLY_CORRECT='1'     # GNU Coreutils POSIX mode
COMMAND_MODE='unix2003' # macOS UNIX 03 mode
export 'LC_ALL' 'IFS' 'PATH' 'UNIX_STD' 'XPG_SUS_ENV' 'XPG_UNIX98' 'POSIXLY_CORRECT' 'COMMAND_MODE'




readonly 'EX_OK=0' # successful termination

readonly 'EX__BASE=64' # base value for error messages

readonly 'EX_USAGE=64'       # command line usage error
readonly 'EX_DATAERR=65'     # data format error
readonly 'EX_NOINPUT=66'     # cannot open input
readonly 'EX_NOUSER=67'      # addressee unknown
readonly 'EX_NOHOST=68'      # host name unknown
readonly 'EX_UNAVAILABLE=69' # service unavailable
readonly 'EX_SOFTWARE=70'    # internal software error
readonly 'EX_OSERR=71'       # system error (e.g., can't fork)
readonly 'EX_OSFILE=72'      # critical OS file missing
readonly 'EX_CANTCREAT=73'   # can't create (user) output file
readonly 'EX_IOERR=74'       # input/output error
readonly 'EX_TEMPFAIL=75'    # temp failure; user is invited to retry
readonly 'EX_PROTOCOL=76'    # remote error in protocol
readonly 'EX_NOPERM=77'      # permission denied
readonly 'EX_CONFIG=78'      # configuration error

readonly 'EX__MAX=78' # maximum listed value

trap 'case "${?}" in 0) end_call;; *) end_call "${EX_SOFTWARE}";; esac' 0 # EXIT
trap 'end_call 129' 1                                                     # SIGHUP
trap 'end_call 130' 2                                                     # SIGINT
trap 'end_call 131' 3                                                     # SIGQUIT
trap 'end_call 143' 15                                                    # SIGTERM

alias org_lc='LC_ALL="${LC_ALL_ORG}"'


end_call() {
	trap '' 0 # EXIT
	rm -fr -- ${tmpDir:+"${tmpDir}"}
	exit "${1:-0}"
}




replace_all_fast() {
	eval "$1=\${2//\"\$3\"/\"\$4\"}"
}

replace_all_posix() {
	set -- "$1" "$2" "$3" "$4" ""
	until [ _"$2" = _"${2#*"$3"}" ] && eval "$1=\$5\$2"; do
		set -- "$1" "${2#*"$3"}" "$3" "$4" "$5${2%%"$3"*}$4"
	done
}

replace_all_pattern() {
	set -- "$1" "$2" "$3" "$4" ""
	until eval "[ _\"\$2\" = _\"\${2#*$3}\" ] && $1=\$5\$2"; do
		eval "set -- \"\$1\" \"\${2#*$3}\" \"\$3\" \"\$4\" \"\$5\${2%%$3*}\$4\""
	done
}

meta_escape() {
	if [ "${1#*\?}" ]; then # posh <= 0.5.4
		set -- '\\\\:\\\\\\\\' '\\\[:[[]' '\\\?:[?]' '\\\*:[*]' '\\\$:[$]'
	elif [ "${2%%\\*}" ]; then # bosh = all (>= 20181007), busybox <= 1.22.0
		set -- '\\\\:\\\\\\\\' '\[:[[]' '\?:[?]' '\*:[*]' '\$:[$]'
	else # POSIX compliant
		set -- '\\:\\\\' '\[:[[]' '\?:[?]' '\*:[*]' '\$:[$]'
	fi

	set "$@" '\(:\\(' '\):\\)' '\|:\\|' '\":\\\"' '\`:\\\`' \
		'\{:\\{' '\}:\\}' "\\':\\\\'" '\&:\\&' '\=:\\=' '\>:\\>' "end"

	echo 'meta_escape() { set -- "$1" "$2" ""'
	until [ "$1" = "end" ] && shift && printf '%s\n' "$@"; do
		set -- "${1%:*}" "${1#*:}" "$@"
		set -- "$@" 'until [ _"$2" = _"${2#*'"$1"'}" ] && set -- "$1" "$3$2" ""; do'
		set -- "$@" '  set -- "$1" "${2#*'"$1"'}" "$3${2%%'"$1"'*}'"$2"'"'
		set -- "$@" 'done'
		shift 3
	done
	echo 'eval "$1=\"\$3\$2\""; }'
}
eval "$(meta_escape "a?" '\')"

replace_all() {
	(eval 'v="*#*/" p="#*/"; [ "${v//"$p"/-}" = "*-" ]') 2>/dev/null && return 0
	[ "${1#"$2"}" = "a*b" ] && return 1 || return 2
}
eval 'replace_all "a*b" "a[*]" &&:' && :
case $? in
0) # Fast version (Not POSIX compliant)
	replace_all() { replace_all_fast "$@"; } ;;
1) # POSIX version (POSIX compliant)
	replace_all() { replace_all_posix "$@"; } ;;
2) # Pattern version
	replace_all() {
		meta_escape "$1" "$3"
		eval "replace_all_pattern \"\$1\" \"\$2\" \"\${$1}\" \"\$4\""
	} ;;
esac

replace_multiple() {
	eval "${1}=\"\${2}\""
	eval "shift 2; set -- '${1}'" '${@+"${@}"}'

	while [ 2 -le "${#}" ]; do
		eval 'replace_all "${1}"' "\"\${${1}}\"" '"${2}" "${3-}"'

		case "${#}" in
		'2') set -- ;;
		*) eval "shift 3; set -- '${1}'" '${@+"${@}"}' ;;
		esac
	done
}


html_escape() {
	replace_multiple "${1}" "${2}" '&' '&amp;' '<' '&lt;' '>' '&gt;' "'" '&#39;' '"' '&quot;'
}





abspath() {
	case "${2}" in
	'/'*) set -- "${1}" "${2}/" '' ;;
	*) set -- "${1}" "${3:-$PWD}/$2/" '' ;;
	esac

	while [ -n "${2}" ]; do
		case "${2%%/*}" in
		'' | '.') set -- "${1}" "${2#*/}" "${3}" ;;
		'..') set -- "${1}" "${2#*/}" "${3%/*}" ;;
		*) set -- "${1}" "${2#*/}" "${3}/${2%%/*}" ;;
		esac
	done

	eval "${1}=\"/\${3#/}\""
}


path_to_fileurl() {
	abspath "${1}" "${2}"

	eval "${1}=\"file://\$(printf '%s' \"\${${1}}\" | urlencode | sed -e 's/%2F/\//g')\""
}






replace_all_fast() {
	eval "$1=\${2//\"\$3\"/\"\$4\"}"
}

replace_all_posix() {
	set -- "$1" "$2" "$3" "$4" ""
	until [ _"$2" = _"${2#*"$3"}" ] && eval "$1=\$5\$2"; do
		set -- "$1" "${2#*"$3"}" "$3" "$4" "$5${2%%"$3"*}$4"
	done
}

replace_all_pattern() {
	set -- "$1" "$2" "$3" "$4" ""
	until eval "[ _\"\$2\" = _\"\${2#*$3}\" ] && $1=\$5\$2"; do
		eval "set -- \"\$1\" \"\${2#*$3}\" \"\$3\" \"\$4\" \"\$5\${2%%$3*}\$4\""
	done
}

meta_escape() {
	if [ "${1#*\?}" ]; then # posh <= 0.5.4
		set -- '\\\\:\\\\\\\\' '\\\[:[[]' '\\\?:[?]' '\\\*:[*]' '\\\$:[$]'
	elif [ "${2%%\\*}" ]; then # bosh = all (>= 20181007), busybox <= 1.22.0
		set -- '\\\\:\\\\\\\\' '\[:[[]' '\?:[?]' '\*:[*]' '\$:[$]'
	else # POSIX compliant
		set -- '\\:\\\\' '\[:[[]' '\?:[?]' '\*:[*]' '\$:[$]'
	fi

	set "$@" '\(:\\(' '\):\\)' '\|:\\|' '\":\\\"' '\`:\\\`' \
		'\{:\\{' '\}:\\}' "\\':\\\\'" '\&:\\&' '\=:\\=' '\>:\\>' "end"

	echo 'meta_escape() { set -- "$1" "$2" ""'
	until [ "$1" = "end" ] && shift && printf '%s\n' "$@"; do
		set -- "${1%:*}" "${1#*:}" "$@"
		set -- "$@" 'until [ _"$2" = _"${2#*'"$1"'}" ] && set -- "$1" "$3$2" ""; do'
		set -- "$@" '  set -- "$1" "${2#*'"$1"'}" "$3${2%%'"$1"'*}'"$2"'"'
		set -- "$@" 'done'
		shift 3
	done
	echo 'eval "$1=\"\$3\$2\""; }'
}
eval "$(meta_escape "a?" '\')"

replace_all() {
	(eval 'v="*#*/" p="#*/"; [ "${v//"$p"/-}" = "*-" ]') 2>/dev/null && return 0
	[ "${1#"$2"}" = "a*b" ] && return 1 || return 2
}
eval 'replace_all "a*b" "a[*]" &&:' && :
case $? in
0) # Fast version (Not POSIX compliant)
	replace_all() { replace_all_fast "$@"; } ;;
1) # POSIX version (POSIX compliant)
	replace_all() { replace_all_posix "$@"; } ;;
2) # Pattern version
	replace_all() {
		meta_escape "$1" "$3"
		eval "replace_all_pattern \"\$1\" \"\$2\" \"\${$1}\" \"\$4\""
	} ;;
esac

replace_multiple() {
	eval "${1}=\"\${2}\""
	eval "shift 2; set -- '${1}'" '${@+"${@}"}'

	while [ 2 -le "${#}" ]; do
		eval 'replace_all "${1}"' "\"\${${1}}\"" '"${2}" "${3-}"'

		case "${#}" in
		'2') set -- ;;
		*) eval "shift 3; set -- '${1}'" '${@+"${@}"}' ;;
		esac
	done
}


remove_control_character() {
	set -- "${1}" "${2}" "${3}" "$(printf '\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\177\200\201\202\203\204\205\206\207\210\211\212\213\214')"

	while [ -n "${3}" ]; do
		replace_all "${1}" "${4}" "${3%%${3#?}}" ''

		eval 'set -- "${1}" "${2}" "${3#?}"' "\"\${${1}}\""
	done

	set -- "${1}" "${2}" "${4}" ''

	while [ -n "${3}" ]; do
		set -- "${1}" "${2}" "${3#?}" "${4} '${3%%${3#?}}' ''"
	done

	eval 'replace_multiple "${1}" "${2}"' "${4}"
}


safe_string() {
	remove_control_character "${1}" "${2}" "$(printf '\t\n\r')"
}

: "${W3MPLUS_W3M_HOME:=${HOME}/.w3m}"
: "${W3MPLUS_W3M_CONFIG:=${W3MPLUS_W3M_HOME}/config}"
: "${W3MPLUS_W3M_SITECONF:=${W3MPLUS_W3M_HOME}/siteconf}"

parser_definition() {
	setup REST plus:true abbr:true error:option_error no:0 help:usage \
		-- 'Usage:' "  ${2##*/} [ABOUT_URI]" \
		'' 'Options:'

	disp :usage -h --help -- 'このヘルプを表示して終了する'
	disp VERSION -v --version -- 'バージョン情報を表示して終了する'

	msg -- '' 'Exit Status:' \
		'    0 - successful termination' \
		'   64 - command line usage error' \
		'   65 - data format error' \
		'   66 - cannot open input' \
		'   67 - addressee unknown' \
		'   68 - host name unknown' \
		'   69 - service unavailable' \
		'   70 - internal software error' \
		"   71 - system error (e.g., can't fork)" \
		'   72 - critical OS file missing' \
		"   73 - can't create (user) output file" \
		'   74 - input/output error' \
		'   75 - temp failure; user is invited to retry' \
		'   76 - remote error in protocol' \
		'   77 - permission denied' \
		'   78 - configuration error' \
		'  129 - received SIGHUP' \
		'  130 - received SIGINT' \
		'  131 - received SIGQUIT' \
		'  143 - received SIGTERM'
}

REST=''
parse() {
	OPTIND=$(($# + 1))
	while OPTARG= && [ $# -gt 0 ]; do
		set -- "${1%%\=*}" "${1#*\=}" "$@"
		while [ ${#1} -gt 2 ]; do
			case $1 in *[!a-zA-Z0-9_-]*) break ;; esac
			case '--help' in
			"$1")
				OPTARG=
				break
				;;
			$1*) OPTARG="$OPTARG --help" ;;
			esac
			case '--version' in
			"$1")
				OPTARG=
				break
				;;
			$1*) OPTARG="$OPTARG --version" ;;
			esac
			break
		done
		case ${OPTARG# } in
		*\ *)
			eval "set -- $OPTARG $1 $OPTARG"
			OPTIND=$((($# + 1) / 2)) OPTARG=$1
			shift
			while [ $# -gt "$OPTIND" ]; do
				OPTARG="$OPTARG, $1"
				shift
			done
			set "Ambiguous option: $1 (could be $OPTARG)" ambiguous "$@"
			option_error "$@" >&2 || exit $?
			echo "$1" >&2
			exit 1
			;;
		?*)
			[ "$2" = "$3" ] || OPTARG="$OPTARG=$2"
			shift 3
			eval 'set -- "${OPTARG# }"' ${1+'"$@"'}
			OPTARG=
			;;
		*) shift 2 ;;
		esac
		case $1 in
		--?*=*)
			OPTARG=$1
			shift
			eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
			;;
		--no-* | --without-*) unset OPTARG ;;
		-[hv]?*)
			OPTARG=$1
			shift
			eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'}
			OPTARG=
			;;
		+*) unset OPTARG ;;
		esac
		case $1 in
		'-h' | '--help')
			usage
			exit 0
			;;
		'-v' | '--version')
			echo "${VERSION}"
			exit 0
			;;
		--)
			shift
			while [ $# -gt 0 ]; do
				REST="${REST} \"\${$((OPTIND - $#))}\""
				shift
			done
			break
			;;
		[-+]?*)
			set "unknown" "$1"
			break
			;;
		*)
			REST="${REST} \"\${$((OPTIND - $#))}\""
			;;
		esac
		shift
	done
	[ $# -eq 0 ] && {
		OPTIND=1
		unset OPTARG
		return 0
	}
	case $1 in
	unknown) set "Unrecognized option: $2" "$@" ;;
	noarg) set "Does not allow an argument: $2" "$@" ;;
	required) set "Requires an argument: $2" "$@" ;;
	pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;
	notcmd) set "Not a command: $2" "$@" ;;
	*) set "Validation error ($1): $2" "$@" ;;
	esac
	option_error "$@" >&2 || exit $?
	echo "$1" >&2
	exit 1
}
usage() {
	cat <<'GETOPTIONSHERE'
Usage:
  w3mabouturi [ABOUT_URI]

Options:
  -h,     --help              このヘルプを表示して終了する
  -v,     --version           バージョン情報を表示して終了する

Exit Status:
    0 - successful termination
   64 - command line usage error
   65 - data format error
   66 - cannot open input
   67 - addressee unknown
   68 - host name unknown
   69 - service unavailable
   70 - internal software error
   71 - system error (e.g., can't fork)
   72 - critical OS file missing
   73 - can't create (user) output file
   74 - input/output error
   75 - temp failure; user is invited to retry
   76 - remote error in protocol
   77 - permission denied
   78 - configuration error
  129 - received SIGHUP
  130 - received SIGINT
  131 - received SIGQUIT
  143 - received SIGTERM
GETOPTIONSHERE
}

parse ${@+"${@}"}
eval "set -- ${REST}"

case "${1-about:about}" in
'about:')
	safe_string 'version' "$(w3m -version)"
	html_escape 'version' "${version}"

	printf '<pre title="w3m Version Information"><samp>%s</samp></pre>' "${version}" | org_lc printhtml --title 'About:' -- - | org_lc httpresponse -H 'Content-Type: text/html; charset=UTF-8' -- -
	;;
'about:about')
	org_lc printhtml --title 'About About' -- - <<-'__EOF__' | org_lc httpresponse -H 'Content-Type: text/html; charset=UTF-8' -- -
		<section>
			<h1>About About</h1>

			<p>This is a list of “about” pages for your convenience.</p>

			<ul>
				<li><a href="about:">about:</a></li>
				<li><a href="about:about">about:about</a></li>
				<li><a href="about:blank">about:blank</a></li>
				<li><a href="about:bookmark">about:bookmark</a></li>
				<li><a href="about:cache">about:cache</a></li>
				<li><a href="about:config">about:config</a></li>
				<li><a href="about:cookie">about:cookie</a></li>
				<li><a href="about:downloads">about:downloads</a></li>
				<li><a href="about:help">about:help</a></li>
				<li><a href="about:history">about:history</a></li>
				<li><a href="about:home">about:home</a></li>
				<li><a href="about:memory">about:memory</a></li>
				<li><a href="about:message">about:message</a></li>
				<li><a href="about:newtab">about:newtab</a></li>
				<li><a href="about:permissions">about:permissions</a></li>
				<li><a href="about:private">about:private</a></li>
				<li><a href="about:preferences">about:preferences</a></li>
			</ul>
		</section>
	__EOF__
	;;
'about:blank')
	org_lc printhtml --title 'about:blank' -- - | org_lc httpresponse -H 'Content-Type: text/html; charset=UTF-8' -- -
	;;
'about:bookmark')
	org_lc httpresponse -H 'W3m-control: BACK' -H 'W3m-control: VIEW_BOOKMARK'
	;;
'about:cache')
	path_to_fileurl 'baseURL' "${W3MPLUS_W3M_HOME}"

	awkScript=$(
		cat <<-'__EOF__'



			function lsdate_to_isodate(month, day, year, currentYear, currentMonth,  months) {
				split("", months)

				months["Jan"] = 1; months["Feb"] = 2; months["Mar"] = 3;
				months["Apr"] = 4; months["May"] = 5; months["Jun"] = 6;
				months["Jul"] = 7; months["Aug"] = 8; months["Sep"] = 9;
				months["Oct"] = 10; months["Nov"] = 11; months["Dec"] = 12;

				if(currentYear == "") {
					"date -- '+%Y'" | getline currentYear
					close("date -- '+%Y'")
				}

				if(currentMonth == "") {
					"date -- '+%m'" | getline currentMonth
					close("date -- '+%m'")
				}

				if(index(year, ":")) {
					return sprintf("%04d-%02d-%02dT%s", currentYear - (currentMonth < months[month]), months[month], day, year)
				}

				return sprintf("%04d-%02d-%02d", year, months[month], day)
			}

						BEGIN {
							command = "date -- '+%Y %m'"
							command | getline
							close(command)

							currentYear = $1
							currentMonth = $2

							itemCount = totalSize  = 0

							printf("<tbody>")
						}

						$0 ~ /^-/ && $7 ~ /^w3m(cache|cookie|frame|src|tmp)[.0-9A-Z_a-z-]*$/ {
							size = $3
							month = $4
							day = $5
							year = $6
							name = $7

							itemCount++
							totalSize += size

							date = lsdate_to_isodate(month, day, year, currentYear, currentMonth)
							gsub(/T/, " ", date)

							printf("<tr><td><a href=\"%s\">%s</a></td><td align=\"right\">%s</td><td align=\"right\"><time datetime=\"%s\">%s</time></td></tr>", baseURL name, name, size, substr(date, 1, 10), date)
						}

						END {
							printf("</tbody>")
							printf("<tfoot><tr><th scope=\"row\">Total Item / Total Size</th><td colspan=\"2\" align=\"right\">%d / %d</td></tr></tfoot>", itemCount, totalSize)
						}
		__EOF__
	)

	org_lc printhtml --title 'Network Cache Storage Information' -- - <<-__EOF__ | org_lc httpresponse -H 'Content-Type: text/html; charset=UTF-8' -- -
		<table>
			<caption>w3m Cache Files in '<a href="${baseURL}">${W3MPLUS_W3M_HOME}</a>'</caption>
			<thead>
				<tr>
					<th scope="col">File Name</th>
					<th scope="col">Size (Byte)</th>
					<th scope="col">Modified</th>
				</tr>
			</thead>
			$(
				ls -Lgko -- "${W3MPLUS_W3M_HOME}" 2>'/dev/null' |
					awk -v "baseURL=${baseURL}/" -- "${awkScript}"
			)
		</table>
	__EOF__
	;;
'about:config')
	path_to_fileurl 'uri' "${W3MPLUS_W3M_CONFIG}"

	org_lc w3mredirect "${uri}"
	;;
'about:cookie')
	org_lc httpresponse -H 'W3m-control: BACK' -H 'W3m-control: COOKIE'
	;;
'about:downloads')
	org_lc httpresponse -H 'W3m-control: BACK' -H 'W3m-control: DOWNLOAD_LIST'
	;;
'about:help')
	org_lc httpresponse -H 'W3m-control: BACK' -H 'W3m-control: HELP'
	;;
'about:history')
	org_lc httpresponse -H 'W3m-control: BACK' -H 'W3m-control: HISTORY'
	;;
'about:home')
	safe_string 'pass' "${W3MPLUS_PASS_VALUE-}"
	html_escape 'pass' "${pass}"

	safe_string 'server' "${SERVER_SOFTWARE:-w3m}"
	html_escape 'server' "${server}"

	path_to_fileurl 'homeurl' "${HOME}"

	org_lc printhtml --title 'w3m Start Page' -- - <<-__EOF__ | org_lc httpresponse -H 'Content-Type: text/html; charset=UTF-8' -H 'W3m-control: BEGIN' -H 'W3m-control: NEXT_LINK' -- -
		<form action="file:///cgi-bin/w3mplus" method="get" accept-charset="UTF-8">
			<p align="center">
				<input type="hidden" name="pass" value="${pass}" />
				<input type="hidden" name="action" value="search" />
				<input accesskey="s" type="text" name="query" size="30" placeholder="Search Keywords" />
			</p>
		</form>

		<p align="center">
			<a accesskey="~" href="${homeurl}">Home</a> -
			<a accesskey="b" href="about:bookmark">Bookmarks</a> -
			<a accesskey="h" href="about:history">History</a> -
			<a accesskey="d" href="about:downloads">Downloads</a>
		</p>

		<p align="center">
			<a accesskey="?" href="about:help">Help</a> -
			<a accesskey="o" href="about:preferences">Options</a> -
			<a accesskey="c" href="about:cookie">Cookies</a> -
			<a accesskey="m" href="about:message">Messages</a>
		</p>

		<p align="center"><samp>${server}</samp></p>
	__EOF__
	;;
'about:memory')
	header='pid=,ppid=,pcpu=,vsz=,nice=,etime=,time=,args='
	awkScript=$(
		cat <<-'__EOF__'



			function html_escape(string) {
				gsub(/&/, "\\&amp;", string)
				gsub(/</, "\\&lt;", string)
				gsub(/</, "\\&gt;", string)
				gsub(/'/, "\\&#39;", string)
				gsub(/"/, "\\&quot;", string)

				return string
			}





			function remove_control_character(string, except,  cc,char) {
				cc = "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\177\200\201\202\203\204\205\206\207\210\211\212\213\214"

				gsub(except, "", cc)

				for(char = ""; cc != ""; cc = substr(cc, 2)) {
					gsub(substr(cc, 1, 1), "", string)
				}

				return string
			}


			function safe_string(string) {
				return remove_control_character(string, "\011\012\015")
			}

						BEGIN {
							hCount = split("", headers)

							headers[++hCount] = "Process ID"
							headers[++hCount] = "Parent process ID"
							headers[++hCount] = "Ratio of CPU time"
							headers[++hCount] = "Memory usage"
							headers[++hCount] = "Nice value"
							headers[++hCount] = "Elapsed time"
							headers[++hCount] = "Cumulative CPU time"
							headers[++hCount] = "Command"

							split("S M H DT", timeUnit)
						}

						{
							printf("<table><tbody>")

							for(i = 1; i <= hCount; i++) {
								printf("<tr><th>%s</th><td>", headers[i])

								if(i == 3) {
									printf("%s%%", $i)
								} else if(i == 4) {
									printf("%s KiB", $i)
								} else if((i == 6) || (i == 7)) {
									count = split($i, time, /[:-]/)

									datetime = (count == 4 ? "P" : "PT")
									for(j = 1; j <= count; j++) {
										datetime = datetime (time[j] + 0) timeUnit[count - j + 1]
									}

									printf("<time datetime=\"%s\">%s</time>", datetime, $i)
								} else if(i == hCount) {
									printf("%s", $i)

									for(j = hCount + 1; j <= NF; j++) {
										printf(" %s", html_escape(safe_string($j)))
									}
								} else {
									printf("%s", $i)
								}

								printf("</td></tr>")
							}

							printf("</tbody></table>")
						}
		__EOF__
	)

	w3mid=$(
		psrelative -o 'pid,comm' 'ancestor' "${$}" |
			awk -- '$2 == "w3m" { printf("%d", $1); exit; }'
	)

	org_lc printhtml --title 'w3m processes' -- - <<-__EOF__ | org_lc httpresponse -H 'Content-Type: text/html; charset=UTF-8' -- -
		<section>
			<h1>Current w3m process</h1>

			$(
				case "${w3mid}" in [0-9]*)
					ps -o "${header}" -p "${w3mid}" | awk -- "${awkScript}"
					;;
				esac
			)
		</section>

		<section>
			<h1>Other w3m processes</h1>

			$(
				ps -o "${header}" -u "${USER}" |
					awk -v "pid=${w3mid}" -- '($1 != pid) && ($8 == "w3m") { print $0 }' |
					awk -- "${awkScript}"
			)
		</section>
	__EOF__
	;;
'about:message')
	org_lc httpresponse -H 'W3m-control: BACK' -H 'W3m-control: MSGS'
	;;
'about:newtab')
	org_lc w3mredirect -t 'newtab' 'about:blank'
	;;
'about:permissions')
	path_to_fileurl 'uri' "${W3MPLUS_W3M_SITECONF}"

	org_lc w3mredirect "${url}"
	;;
'about:preferences')
	org_lc httpresponse -H 'W3m-control: BACK' -H 'W3m-control: OPTIONS'
	;;
'about:private')
	org_lc printhtml --title 'Private Browsing' -- - <<-'__EOF__' | org_lc httpresponse -H 'Content-Type: text/html; charset=UTF-8' -- -
		<h1>Private Browsing</h1>

		<p>w3m won't remember any history for this window.</p>

		<p>In a Private Browsing window, w3m won't keep any browser history, search history, download history, web form history, cookies, or temporary internet files. However, files you download and bookmarks you make will be kept.</p>

		<p>To stop Private Browsing, you can close this window.</p>

		<p>While this computer won't have a record of your browsing history, your internet service provider or employer can still track the pages you visit.</p>
	__EOF__
	;;
*)
	org_lc printhtml --title 'Problem loading page' -- - <<-'__EOF__' | org_lc httpresponse --status-line 'HTTP/1.1 400 Bad Request' -H 'Content-Type: text/html; charset=UTF-8' -- -
		<h1>The address isn't valid</h1>

		<p>The URL is not valid and cannot be loaded.</p>

		<ul>
			<li>Web addresses are usually written like <strong>http://www.example.com/</strong></li>
			<li>Make sure that you're using forward slashes (i.e. /).</li>
		</ul>
	__EOF__

	end_call 1
	;;
esac
