-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.awk
executable file
·179 lines (134 loc) · 3.27 KB
/
template.awk
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
# Global
# Source
# Sourcename
# Sourcecount
# Tmp
# Tmpcount
function sqltoscript(file, l, r, ll, d, n)
{
++Tmpcount
filenew = Tmp[Tmpcount] \
= sprintf("/tmp/%s.%s", Tmpcount, PROCINFO["pid"]);
print "#!/bin/sh\n\n" >filenew;
print "echo 'set list on;\n" >filenew;
while ((r = getline l <file) > 0) {
gsub("'", "'\"'\"'", l);
ll = "";
while ((d = index(l, "$"))) {
assert(n = match(substr(l, d + 1), /^[0-9]+/),
"Argument addressing without number: " \
l);
ll = ll substr(l, 1, d - 1) \
"'$" substr(l, d + 1, RLENGTH) + 3 "'";
l = substr(l, d + RLENGTH + 1);
}
print ll l >filenew;
}
printf "' | %s/isqlreq \"$1\" \"$2\" \"$3\" | %s/sqltocsv",
A_utilspath, A_utilspath >filenew;
close(file);
close(filenew);
return filenew;
}
function sourcesubst(file, cmd, filenew)
{
++Tmpcount
filenew = Tmp[Tmpcount] \
= sprintf("/tmp/%s.%s", Tmpcount, PROCINFO["pid"]);
system(sprintf("LC_ALL=C cat %s | awk -f %s/error.awk \
-f %s/substitute.awk -f %s/sources.awk \
-v 'A_scriptpath=%s' -v 'A_source=%s' \
-v 'A_tablegenpath=%s' -v 'A_utilspath=%s' >%s",
file, A_scriptpath, A_scriptpath, A_scriptpath,
A_scriptpath, wrapsources(), A_tablegenpath,
A_utilspath, filenew));
close(filenew)
return filenew;
}
function source(l, name, type, s, file, i, a, c, r, ll, subst)
{
gsub(/^[ \t]+/, "", l);
gsub(/[ \t]+$/, "", l);
gsub(/[ \t]+/, " ", l);
subst = 1;
if ((s = index(l, ":")) != 0) {
if (substr(l, s + 1, 1) != ":")
subst = 0;
else {
gsub(/::/, ":", l);
}
file = substr(l, s + 1);
gsub(/^[ \t]+/, "", file);
gsub(/[ \t]+$/, "", file);
l = substr(l, 1, s - 1);
}
else {
++Tmpcount
file = Tmp[Tmpcount] = sprintf("/tmp/%s.%s",
Tmpcount, PROCINFO["pid"]);
while ((r = getline ll) > 0) {
if (ll ~ /#end[ \t]*/)
break;
print ll >file;
}
assert(r != 0, "Unexpected end of source definition " l);
assert(r > 0, "Error while reading source definition" l);
close(file);
}
c = split(l, a, " ");
for (i in a) {
assert(a[i] ~ /^[a-zA-Z][a-zA-Z0-9]*$/,
"Wrong identificator name: " a[i]);
}
if (a[1] == "document") {
assert(c == 1, a[1] ": Wrong number of arguments");
type = name = a[1];
if (subst) file = sourcesubst(file);
}
else if (a[1] == "input") {
assert(c == 3, a[1] ": Wrong number of arguments");
name = a[2];
if (a[3] == "sql") {
a[3] = "script";
file = sqltoscript(file);
}
type = a[3];
if ((a[3] == "text" || a[3] == "script") && subst) {
file = sourcesubst(file);
if (a[3] == "script") {
assert(system("chmod u+x " file) == 0,
"cannot make " file " executable.");
}
}
}
else if (a[1] == "table") {
assert(c == 2, a[1] ": Wrong number of arguments");
name = a[2];
type = "table";
}
else
error("Unknown input type: " a[1]);
Sourcename[Sourcecount] = name;
Source[name,"type"] = type;
Source[name,"path"] = file;
++Sourcecount;
}
BEGIN {
Sourcecount = 0;
unwrapsources();
while ((r = getline l) > 0) {
if (l ~ /^[ \t]*$/)
continue;
assert(substr(l, 1, 1) == "#",
"Text outside definition");
l = substr(l, 2);
source(l);
}
assert(r >= 0, "Error while reading template ");
system("cat " Source["document","path"]);
exit(0);
}
END {
for (i in Tmp)
system("rm " Tmp[i]);
}