pass defined global variables to AWK as CLI arguments v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 06 May 2019 21:57:16 +0200
branchv_0
changeset 12 8844ebce8fb4
parent 11 f515d14794e0
child 13 b74001992ec3
pass defined global variables to AWK as CLI arguments
src/AwkHandler.h
--- a/src/AwkHandler.h	Mon May 06 20:38:17 2019 +0200
+++ b/src/AwkHandler.h	Mon May 06 21:57:16 2019 +0200
@@ -151,6 +151,7 @@
 			redirectFD(awkInputReaderFD, STDIN_FILENO);
 			redirectFD(awkOutputWriterFD, STDOUT_FILENO);
 
+			// AWK script:
 			std::wstringstream awkScript;
 			awkScript << L"BEGIN {" << std::endl;
 			awkScript << L"FS=\"\\t\";" << std::endl;
@@ -162,8 +163,14 @@
 
 			awkScript << L"{print \"AWK says: line \" NR \" '\" $0 \"' has \" NF \" fields; first field is '\" $1 \"'\";}" << std::endl;
 
+			// CLI arguments:
 			std::vector<std::string> args;
 			args.push_back("awk");
+
+			for (auto d : configuration.definitions) {
+				args.push_back("-v");
+				args.push_back(convertor.to_bytes(a2v(d.name) + L"=" + d.value));
+			}
 			args.push_back(convertor.to_bytes(awkScript.str()));
 
 			// Runs AWK program found on $PATH → user can plug-in a custom implementation or a wrapper, but this can be also bit dangerous (however AWK itself is dangerous).