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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
####################################################################
# Main.awk #
####################################################################
# Detect gawk version.
function initGawk( group) {
Gawk = "gawk"
GawkVersion = PROCINFO["version"]
split(PROCINFO["version"], group, ".")
if (group[1] < 4) {
e("[ERROR] Oops! Your gawk (version " GawkVersion ") appears to be too old.\nYou need at least gawk 4.0.0 to run this program.")
exit 1
}
}
# Pre-initialization (before option parsing).
function preInit() {
initGawk() #<< AnsiCode
# Languages
initBiDi()
initLocale()
initLocaleDisplay() #<< Locale, BiDi
initUserLang() #<< Locale
RS = "\n"
ExitCode = 0
Option["debug"] = 0
Option["verbose"] = 1
if (ENVIRON["COLUMNS"])
Option["width"] = ENVIRON["COLUMNS"]
else {
"tput cols 2>/dev/null" |& getline Option["width"]
if (!Option["width"]) Option["width"] = 64
}
Option["browser"] = ENVIRON["BROWSER"]
Option["play"] = 0
Option["player"] = ENVIRON["PLAYER"]
Option["proxy"] = ENVIRON["HTTP_PROXY"] ? ENVIRON["HTTP_PROXY"] : ENVIRON["http_proxy"]
Option["interactive"] = 0
Option["no-rlwrap"] = 0
Option["emacs"] = 0
Option["prompt"] = ENVIRON["TRANS_PS"] ? ENVIRON["TRANS_PS"] : "%s>"
Option["prompt-color"] = ENVIRON["TRANS_PS_COLOR"] ? ENVIRON["TRANS_PS_COLOR"] : "blue"
Option["input"] = ""
Option["output"] = "/dev/stdout"
Option["hl"] = ENVIRON["HOME_LANG"] ? ENVIRON["HOME_LANG"] : UserLang
Option["sl"] = ENVIRON["SOURCE_LANG"] ? ENVIRON["SOURCE_LANG"] : "auto"
Option["tl"][1] = ENVIRON["TARGET_LANG"] ? ENVIRON["TARGET_LANG"] : UserLang
}
# Post-initialization (after option parsing).
function postInit() {
# Translate
initHttpService()
}
# Main entry point.
BEGIN {
preInit()
pos = 0
while (ARGV[++pos]) {
# -, -no-op
match(ARGV[pos], /^-(-?no-op)?$/)
if (RSTART) continue
# -V, -version
match(ARGV[pos], /^--?(vers(i(on?)?)?|V)$/)
if (RSTART) {
print getVersion()
print
printf("%-22s%s\n", "gawk (GNU Awk)", PROCINFO["version"])
printf("%s\n", FriBidi ? FriBidi : "fribidi (GNU FriBidi) [NOT INSTALLED]")
printf("%-22s%s\n", "User Language", Locale[getCode(UserLang)]["name"] " (" show(Locale[getCode(UserLang)]["endonym"]) ")")
exit
}
# -H, -h, -help
match(ARGV[pos], /^--?(h(e(lp?)?)?|H)$/)
if (RSTART) {
if (ENVIRON["TRANS_MANPAGE"])
system("echo -E \"${TRANS_MANPAGE}\" | " \
"groff -Wall -mtty-char -mandoc -Tutf8 -Dutf8 -rLL=${COLUMNS}n -rLT=${COLUMNS}n | " \
(system("most 2>/dev/null") ?
"less -s -P\"\\ \\Manual page " Command "(1) line %lt (press h for help or q to quit)\"" :
"most -Cs"))
else
print getHelp()
exit
}
# -r, -reference
match(ARGV[pos], /^--?r(e(f(e(r(e(n(ce?)?)?)?)?)?)?)?$/)
if (RSTART) {
print getReference("endonym")
exit
}
# -R, -reference-english
match(ARGV[pos], /^--?(reference-(e(n(g(l(i(sh?)?)?)?)?)?)?|R)$/)
if (RSTART) {
print getReference("name")
exit
}
# -d, -debug
match(ARGV[pos], /^--?d(e(b(ug?)?)?)?$/)
if (RSTART) {
Option["debug"] = 1
continue
}
# -v, -verbose
match(ARGV[pos], /^--?v(e(r(b(o(se?)?)?)?)?)?$/)
if (RSTART) {
Option["verbose"] = 1 # default value
continue
}
# -b, -brief
match(ARGV[pos], /^--?b(r(i(ef?)?)?)?$/)
if (RSTART) {
Option["verbose"] = 0
continue
}
# -w [num], -width [num]
match(ARGV[pos], /^--?w(i(d(th?)?)?)?(=(.*)?)?$/, group)
if (RSTART) {
Option["width"] = group[4] ?
(group[5] ? group[5] : Option["width"]) :
ARGV[++pos]
continue
}
# -browser [program]
match(ARGV[pos], /^--?browser(=(.*)?)?$/, group)
if (RSTART) {
Option["browser"] = group[1] ?
(group[2] ? group[2] : Option["browser"]) :
ARGV[++pos]
continue
}
# -p, -play
match(ARGV[pos], /^--?p(l(ay?)?)?$/)
if (RSTART) {
Option["play"] = 1
continue
}
# -player [program]
match(ARGV[pos], /^--?player(=(.*)?)?$/, group)
if (RSTART) {
Option["play"] = 1
Option["player"] = group[1] ?
(group[2] ? group[2] : Option["player"]) :
ARGV[++pos]
continue
}
# -x [proxy], -proxy [proxy]
match(ARGV[pos], /^--?(proxy|x)(=(.*)?)?$/, group)
if (RSTART) {
Option["proxy"] = group[2] ?
(group[3] ? group[3] : Option["proxy"]) :
ARGV[++pos]
continue
}
# -I, -interactive
match(ARGV[pos], /^--?(int(e(r(a(c(t(i(ve?)?)?)?)?)?)?)?|I)$/)
if (RSTART) {
Option["interactive"] = 1
continue
}
# -no-rlwrap
match(ARGV[pos], /^--?no-rlwrap/)
if (RSTART) {
Option["no-rlwrap"] = 1
continue
}
# -E, -emacs
match(ARGV[pos], /^--?(emacs|E)$/)
if (RSTART) {
Option["emacs"] = 1
continue
}
# -prompt [prompt_string]
match(ARGV[pos], /^--?prompt(=(.*)?)?$/, group)
if (RSTART) {
Option["prompt"] = group[1] ?
(group[2] ? group[2] : Option["prompt"]) :
ARGV[++pos]
continue
}
# -prompt-color [color_code]
match(ARGV[pos], /^--?prompt-color(=(.*)?)?$/, group)
if (RSTART) {
Option["prompt-color"] = group[1] ?
(group[2] ? group[2] : Option["prompt-color"]) :
ARGV[++pos]
continue
}
# -i [file], -input [file]
match(ARGV[pos], /^--?i(n(p(ut?)?)?)?(=(.*)?)?$/, group)
if (RSTART) {
Option["input"] = group[4] ?
(group[5] ? group[5] : Option["input"]) :
ARGV[++pos]
continue
}
# -o [file], -output [file]
match(ARGV[pos], /^--?o(u(t(p(ut?)?)?)?)?(=(.*)?)?$/, group)
if (RSTART) {
Option["output"] = group[5] ?
(group[6] ? group[6] : Option["output"]) :
ARGV[++pos]
continue
}
# -l [code], -lang [code]
match(ARGV[pos], /^--?l(a(ng?)?)?(=(.*)?)?$/, group)
if (RSTART) {
Option["hl"] = group[3] ?
(group[4] ? group[4] : Option["hl"]) :
ARGV[++pos]
continue
}
# -s [code], -source [code]
match(ARGV[pos], /^--?s(o(u(r(ce?)?)?)?)?(=(.*)?)?$/, group)
if (RSTART) {
Option["sl"] = group[5] ?
(group[6] ? group[6] : Option["sl"]) :
ARGV[++pos]
continue
}
# -t [codes], -target [codes]
match(ARGV[pos], /^--?t(a(r(g(et?)?)?)?)?(=(.*)?)?$/, group)
if (RSTART) {
if (group[5]) {
if (group[6]) split(group[6], Option["tl"], "+")
} else
split(ARGV[++pos], Option["tl"], "+")
continue
}
# Shortcut format
# '[code]:[code]+...' or '[code]=[code]+...'
match(ARGV[pos], /^[{([]?([[:alpha:]][[:alpha:]][[:alpha:]]?(-[[:alpha:]][[:alpha:]])?)?(:|=)((@?[[:alpha:]][[:alpha:]][[:alpha:]]?(-[[:alpha:]][[:alpha:]])?\+)*(@?[[:alpha:]][[:alpha:]][[:alpha:]]?(-[[:alpha:]][[:alpha:]])?)?)[})\]]?$/, group)
if (RSTART) {
if (group[1]) Option["sl"] = group[1]
if (group[4]) split(group[4], Option["tl"], "+")
continue
}
# --
match(ARGV[pos], /^--$/)
if (RSTART) {
++pos # skip the end-of-options option
break # no more option from here
}
break # no more option from here
}
# Option parsing finished
postInit()
if (Option["interactive"] && !Option["no-rlwrap"]) {
# Interactive mode
initRlwrap() # initialize Rlwrap
if (Rlwrap && (ENVIRON["TRANS_PROGRAM"] || fileExists(EntryPoint))) {
command = Rlwrap " " Gawk " " (ENVIRON["TRANS_PROGRAM"] ?
"\"${TRANS_PROGRAM}\"" :
"-f " EntryPoint) " -" \
" -no-rlwrap" # be careful - never fork Rlwrap recursively!
for (i = 1; i < length(ARGV); i++)
if (ARGV[i])
command = command " " parameterize(ARGV[i])
if (!system(command))
exit # child process finished, exit
else
; # skip
} else
; # skip
} else if (!Option["interactive"] && !Option["no-rlwrap"] && Option["emacs"]) {
# Emacs interface
Emacs = "emacs"
if (ENVIRON["TRANS_PROGRAM"] || fileExists(EntryPoint)) {
params = ""
for (i = 1; i < length(ARGV); i++)
if (ARGV[i])
params = params " " (parameterize(ARGV[i], "\""))
if (ENVIRON["TRANS_PROGRAM"]) {
el = "(progn (setq trans-program (getenv \"TRANS_PROGRAM\")) " \
"(setq explicit-shell-file-name \"" Gawk "\") " \
"(setq explicit-" Gawk "-args (cons trans-program '(\"-\" \"-I\" \"-no-rlwrap\"" params "))) " \
"(command-execute 'shell) (rename-buffer \"" Name "\"))"
} else {
el = "(progn (setq explicit-shell-file-name \"" Gawk "\") " \
"(setq explicit-" Gawk "-args '(\"-f\" \"" EntryPoint "\" \"--\" \"-I\" \"-no-rlwrap\"" params ")) " \
"(command-execute 'shell) (rename-buffer \"" Name "\"))"
}
command = Emacs " --eval " parameterize(el)
if (!system(command))
exit # child process finished, exit
else
Option["interactive"] = 1 # skip
} else
Option["interactive"] = 1 # skip
}
if (Option["play"]) {
# Initialize audio player or speech synthesizer
if (!Option["player"]) {
initAudioPlayer()
Option["player"] = AudioPlayer ? AudioPlayer : Option["player"]
if (!Option["player"])
initSpeechSynthesizer()
}
if (!Option["player"] && !SpeechSynthesizer) {
w("[WARNING] No available audio player or speech synthesizer is found.")
Option["play"] = 0
}
}
if (Option["interactive"]) {
print AnsiCode["bold"] AnsiCode[tolower(Option["prompt-color"])] getVersion() AnsiCode[0] > "/dev/stderr"
print AnsiCode[tolower(Option["prompt-color"])] "(:q to quit)" AnsiCode[0] > "/dev/stderr"
}
# Initialize browser
if (!Option["browser"]) {
"xdg-mime query default text/html 2>/dev/null" |& getline Option["browser"]
match(Option["browser"], "(.*).desktop$", group)
Option["browser"] = group[1]
}
if (pos < ARGC) {
# More parameters
# Translate the remaining parameters
for (i = pos; i < ARGC; i++) {
# Verbose mode: separator between sources
if (Option["verbose"] && i > pos)
print replicate("═", Option["width"])
translate(ARGV[i], 1) # inline mode
}
# If input not specified, we're done
} else {
# No more parameter besides options
# If input not specified, use stdin
if (!Option["input"]) Option["input"] = "/dev/stdin"
}
# If input specified, start translating
if (Option["input"])
translateMain()
exit ExitCode
}
|