commit 6f84459aac721cd8d2f2f824c29faf8731b385d5
parent 0959697c330c44b1b52cbf20734d084f6d964d8c
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Sun, 29 Jun 2014 06:36:59 +0200
printing out file dependencies
Diffstat:
| M | bin/aat.awk | | | 105 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- |
1 file changed, 95 insertions(+), 10 deletions(-)
diff --git a/bin/aat.awk b/bin/aat.awk
@@ -8,6 +8,15 @@ BEGIN {
T_AWK = 3; type_names[3] = "awk"
T_FUNC = 4; type_names[4] = "func"
tok_type = T_TEXT
+
+ # get current directory
+ "pwd" | getline PWD
+ close("pwd")
+
+ # empty the depency file
+ if ("AAT_DEP" in ENVIRON) {
+ printf "" >ENVIRON["AAT_DEP"]
+ }
}
# Append 'content' to array of tokens. Token type is taken from current value
@@ -29,6 +38,9 @@ function token(content) {
# Print file dependencies for makefile usage
function print_dep(str) {
+ if(str !~ /^\//) {
+ str = PWD "/" str
+ }
if ("AAT_DEP" in ENVIRON) {
print str >>ENVIRON["AAT_DEP"]
} else {
@@ -36,33 +48,98 @@ function print_dep(str) {
}
}
-function call_macro(name, args) {
+function die(msg) {
+ print msg >>"/dev/stderr"
+ exit 1
+}
+
+function sh_escape(str) {
+ gsub(/["$\\]/, "\\&", str)
+ return "\"" str "\""
+}
+
+# find a file relative to current filename and overwrite that variable
+function find_file(name) {
+ # TODO: include search path
+ if(name ~ /^\//) {
+ # absolute path
+ filename = name
+ } else {
+ # relative path
+ if(match(filename, "/[^/]*$")) {
+ filename = substr(filename, 1, RSTART) name
+ } else {
+ filename = name
+ }
+ }
+
+ if(system("test -f " sh_escape(filename)) != 0)
+ {
+ die("could not find requested file: " sh_escape(filename) " (" name ") PWD: " PWD)
+ }
+
+ print_dep(filename)
+}
+
+function macro_readinto(args, varname) {
+ varname = sub(/[ \t].*$/, "", args)
+ find_file(sub(/^[^ \t]+[ \t]+/, "", args))
+ tok_type=T_AWK
+ while(getline <filename) {
+ gsub(/["\\]/, "\\&", $0)
+ token(varname " = \"" $0 "\\n\"\n")
+ }
+ close(filename)
+ filename = file_old
+}
+
+function call_macro(name, args, file_old) {
# Macro to recursively parse another template
if(name == "include"){
- # TODO: resolve relative filenames
- # TODO: include search path
- print_dep(args)
- while(getline <args) {
+ file_old = filename
+ find_file(args)
+ while(getline <filename) {
parse_line($0)
}
+ close(filename)
+ filename = file_old
}
# Macro to insert another file as verbatim code
else if(name == "awk"){
- print_dep(args)
+ file_old = filename
+ find_file(args)
tok_type=T_AWK
- while(getline <args) {
+ while(getline <filename) {
token($0 "\n")
}
+ close(filename)
+ filename = file_old
}
# Macro to insert another file as text
else if(name == "text"){
- print_dep(args)
+ file_old = filename
+ find_file(args)
tok_type=T_TEXT
- while(getline <args) {
+ while(getline <filename) {
token($0 "\n")
}
+ close(filename)
+ filename = file_old
+ }
+
+ # Macro to insert source filename as a variable into produced code
+ else if(name == "filename"){
+ tok_type=T_AWK
+ token("filename = \"" gsub(/["\\]/, "\\&", args) "\n")
+ close(filename)
+ filename = file_old
+ }
+
+ # Macro to read content of a file into a variable
+ else if(name == "readinto"){
+ macro_readinto(args)
}
# Assign a variable with query expression
@@ -310,8 +387,16 @@ function aat_process(str) {
return processed
}
-{ parse_line($0) }
+# for every line in files in ARGV
+{
+ # current filename being read
+ filename = FILENAME
+
+ # parse the line
+ parse_line($0)
+}
+# print the output
END {
nl = 1 # are we on new line?
for(tok_n=1; tok_types[tok_n]; tok_n++) {