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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
####################################################################
# REPL.awk #
####################################################################
# Welcome message.
function welcome() {
if (Option["fmt-welcome-message"])
print prettify("welcome-message", Option["fmt-welcome-message"]) > STDERR
if (Option["fmt-welcome-submessage"])
print prettify("welcome-submessage", Option["fmt-welcome-submessage"]) > STDERR
}
# Prompt for interactive session.
function prompt( i, p, temp) {
p = Option["fmt-prompt"]
# Format specifiers supported by strftime().
# Roughly following ISO 8601:1988, with the notable exception of "%S", "%t" and "%T".
# GNU libc extensions like "%l", "%s" and "%_*" are not supported.
# See: <https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html>
# <http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html>
if (p ~ /%a/) gsub(/%a/, strftime("%a"), p)
if (p ~ /%A/) gsub(/%A/, strftime("%A"), p)
if (p ~ /%b/) gsub(/%b/, strftime("%b"), p)
if (p ~ /%B/) gsub(/%B/, strftime("%B"), p)
if (p ~ /%c/) gsub(/%c/, strftime("%c"), p)
if (p ~ /%C/) gsub(/%C/, strftime("%C"), p)
if (p ~ /%d/) gsub(/%d/, strftime("%d"), p)
if (p ~ /%D/) gsub(/%D/, strftime("%D"), p)
if (p ~ /%e/) gsub(/%e/, strftime("%e"), p)
if (p ~ /%F/) gsub(/%F/, strftime("%F"), p)
if (p ~ /%g/) gsub(/%g/, strftime("%g"), p)
if (p ~ /%G/) gsub(/%G/, strftime("%G"), p)
if (p ~ /%h/) gsub(/%h/, strftime("%h"), p)
if (p ~ /%H/) gsub(/%H/, strftime("%H"), p)
if (p ~ /%I/) gsub(/%I/, strftime("%I"), p)
if (p ~ /%j/) gsub(/%j/, strftime("%j"), p)
if (p ~ /%m/) gsub(/%m/, strftime("%m"), p)
if (p ~ /%M/) gsub(/%M/, strftime("%M"), p)
if (p ~ /%n/) gsub(/%n/, strftime("%n"), p)
if (p ~ /%p/) gsub(/%p/, strftime("%p"), p)
if (p ~ /%r/) gsub(/%r/, strftime("%r"), p)
if (p ~ /%R/) gsub(/%R/, strftime("%R"), p)
if (p ~ /%u/) gsub(/%u/, strftime("%u"), p)
if (p ~ /%U/) gsub(/%U/, strftime("%U"), p)
if (p ~ /%V/) gsub(/%V/, strftime("%V"), p)
if (p ~ /%w/) gsub(/%w/, strftime("%w"), p)
if (p ~ /%W/) gsub(/%W/, strftime("%W"), p)
if (p ~ /%x/) gsub(/%x/, strftime("%x"), p)
if (p ~ /%X/) gsub(/%X/, strftime("%X"), p)
if (p ~ /%y/) gsub(/%y/, strftime("%y"), p)
if (p ~ /%Y/) gsub(/%Y/, strftime("%Y"), p)
if (p ~ /%z/) gsub(/%z/, strftime("%z"), p)
if (p ~ /%Z/) gsub(/%Z/, strftime("%Z"), p)
# %_ : prompt message
if (p ~ /%_/)
gsub(/%_/, showTranslationsOf(Option["hl"]), p)
# %l : host language
if (p ~ /%l/)
gsub(/%l/, getDisplay(Option["hl"]), p)
# %L : host language (English name)
if (p ~ /%L/)
gsub(/%L/, getName(Option["hl"]), p)
# %s : source languages, separated by "+"
# 's' is the format-control character for string
# %S : source languages (English names), separated by "+"
if (p ~ /%S/) {
temp = getName(Option["sls"][1])
for (i = 2; i <= length(Option["sls"]); i++)
temp = temp "+" getName(Option["sls"][i])
gsub(/%S/, temp, p)
}
# TODO: source languages separated by "," and "/"
# %t : target languages, separated by "+"
if (p ~ /%t/) {
temp = getDisplay(Option["tl"][1])
for (i = 2; i <= length(Option["tl"]); i++)
temp = temp "+" getDisplay(Option["tl"][i])
gsub(/%t/, temp, p)
}
# %T : target languages (English names), separated by "+"
if (p ~ /%T/) {
temp = getName(Option["tl"][1])
for (i = 2; i <= length(Option["tl"]); i++)
temp = temp "+" getName(Option["tl"][i])
gsub(/%T/, temp, p)
}
# %, : target languages, separated by ","
if (p ~ /%,/) {
temp = getDisplay(Option["tl"][1])
for (i = 2; i <= length(Option["tl"]); i++)
temp = temp "," getDisplay(Option["tl"][i])
gsub(/%,/, temp, p)
}
# %< : target languages (English names), separated by ","
if (p ~ /%</) {
temp = getName(Option["tl"][1])
for (i = 2; i <= length(Option["tl"]); i++)
temp = temp "," getName(Option["tl"][i])
gsub(/%</, temp, p)
}
# %/ : target languages, separated by "/"
if (p ~ /%\//) {
temp = getDisplay(Option["tl"][1])
for (i = 2; i <= length(Option["tl"]); i++)
temp = temp "/" getDisplay(Option["tl"][i])
gsub(/%\//, temp, p)
}
# %? : target languages (English names), separated by "/"
if (p ~ /%\?/) {
temp = getName(Option["tl"][1])
for (i = 2; i <= length(Option["tl"]); i++)
temp = temp "/" getName(Option["tl"][i])
gsub(/%\?/, temp, p)
}
# %s : source languages, separated by "+"
temp = getDisplay(Option["sls"][1])
for (i = 2; i <= length(Option["sls"]); i++)
temp = temp "+" getDisplay(Option["sls"][i])
printf(prettify("prompt", p), temp) > STDERR
}
# REPL.
function repl(line, command, group, name, i, value, words) {
split(line, words, " ")
command = words[1]
if (command ~ /^:(q|quit)$/) {
exit
} else if (command ~ /^:set$/) {
name = words[2]
value = words[3]
# :set sl and :set tl should work as intended
# TODO: support multiple language codes
if (name == "sl") {
delete Option["sls"]
Option["sls"][1] = value
} else if (name == "tl") {
delete Option["tl"]
Option["tl"][1] = value
} else {
Option[name] = value
}
} else if (command ~ /^:show$/) {
name = words[2]
print prettify("welcome-submessage", toString(Option[name], 1, 0, 1))
} else if (command ~ /^:swap$/) {
tl = Option["tl"][1]
Option["tl"][1] = Option["sls"][1]
Option["sls"][1] = tl
} else if (command ~ /^:engine$/) {
value = words[2]
Option["engine"] = value
initHttpService()
} else if (command ~ /^:reset$/) {
# TODO: reset source and target languages, etc.
} else {
match(command, /^[{(\[]?((@?[[:alpha:]][[:alpha:]][[:alpha:]]?(-[[:alpha:]][[:alpha:]][[:alpha:]]?[[:alpha:]]?)?\+)*(@?[[:alpha:]][[:alpha:]][[:alpha:]]?(-[[:alpha:]][[:alpha:]][[:alpha:]]?[[:alpha:]]?)?)?)?(:|=)((@?[[:alpha:]][[:alpha:]][[:alpha:]]?(-[[:alpha:]][[:alpha:]][[:alpha:]]?[[:alpha:]]?)?\+)*(@?[[:alpha:]][[:alpha:]][[:alpha:]]?(-[[:alpha:]][[:alpha:]][[:alpha:]]?[[:alpha:]]?)?)?)[})\]]?$/, group)
if (RSTART) {
if (group[1]) {
split(group[1], Option["sls"], "+")
Option["sl"] = Option["sls"][1]
}
if (group[7]) split(group[7], Option["tl"], "+")
line = words[2]
for (i = 3; i <= length(words); i++)
line = line " " words[i]
}
if (line) {
translates(line)
# Interactive verbose mode: newline after each translation
if (Option["verbose"]) printf RS
}
}
prompt()
}
|