commit dfdb0682e325c4b12d2fbfba1ff1a3cf5e9be6b5
parent c29011b18795ad55017c4c9b022463044023e857
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Sun, 13 Oct 2013 17:13:47 +0200
query preprocessor
Diffstat:
| A | bin/query.awk | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 92 insertions(+), 0 deletions(-)
diff --git a/bin/query.awk b/bin/query.awk
@@ -0,0 +1,92 @@
+#!/bin/awk -f
+BEGIN {
+ in_string = 0
+}
+
+function out(str) {
+ printf "%s", str
+ last_out = str
+}
+
+# called for expanding {< >}
+# global variable expand_remaining used as input
+function expand_query( expanded) {
+ expanded = ""
+ while(match(expand_remaining, "['<>]")) {
+ if(RSTART > 1)
+ expanded = expanded "\"" substr(expand_remaining, 1, RSTART-1) "\""
+ char = substr(expand_remaining, RSTART, 1)
+ expand_remaining = substr(expand_remaining, RSTART+1)
+ if(char == ">")
+ return "get("expanded")"
+ else if(char == "<") {
+ expanded = expanded expand_query()
+ } else if(char == "'") {
+ if(match(expand_remaining, "'")) {
+ expanded = expanded substr(expand_remaining, 1, RSTART-1)
+ expand_remaining = substr(expand_remaining, RSTART+1)
+ } else {
+ expanded = expand_remaining
+ expand_remaining = ""
+ }
+ } else {
+ print "ERROR: internal error in expand_query()" >"/dev/stderr"
+ exit 1
+ }
+ }
+ if(expand_remaining) {
+ expanded = "\"" expand_remaining "\""
+ expand_remaining = ""
+ }
+ return "get("expanded")"
+}
+
+function parse_line(line) {
+ while(length(line)) {
+ if(DEBUG) printf "%d: \"%s\"\n", in_string, line >"/dev/stderr"
+ if(in_string) {
+ if(match(line, /^\(\\[^"]\|[^"\\]\)/)) {
+ out(substr(line, 1, RLENGTH))
+ line = substr(line, RLENGTH+1)
+ }
+ if(match(line, /^"/)) {
+ out("\"")
+ line = substr(line, 2)
+ in_string = 0
+ } else if(match(line, /^\\$/)) {
+ out("\\")
+ line = ""
+ } else if(!line) {
+ # is this valid?
+ } else {
+ print "string parsing error" >"/dev/stderr"
+ exit 1
+ }
+ } else if(match(line, /[<"]/)) {
+ out(substr(line, 1, RSTART-1))
+ line = substr(line, RSTART)
+ if(match(line, /^"/)) {
+ line = substr(line, 2)
+ in_string = 1
+ } else {
+ if(match(last_out, /[\n\t([ ]$/) && match(line, /^<\([a-zA-Z0-9._]\|'[^']*'\)*>/)) {
+ expand_remaining = substr(line, 2, RLENGTH-2)
+ line = substr(line, RLENGTH+1)
+ out(expand_query())
+ } else if(match(line, /^<[^<"]*/)) {
+ out(substr(line, 1, RSTART-1))
+ line = substr(line, RSTART)
+ } else {
+ out(line)
+ line = ""
+ }
+ }
+ } else {
+ out(line)
+ line = ""
+ }
+ }
+ out("\n")
+}
+
+{ parse_line($0) }