#!/usr/local/bin/gawk -f # # Meta interpreter to parse SOUP scripts # # (c) 2003 ND, Peter van Eerten # ########################################### Generate SH script function parse_sh(which, data, l_front, l_clean, l_back, l_i) { # Create intro for (l_i in g_globalvars) l_front = l_front "\n" g_globalvars[l_i] "=`cat .meta | grep " g_globalvars[l_i] " | awk '{sub($1\" \", \"\", $0); print $0}'`\n" # Create cleanup action for metafile l_clean = "\necho \"\" > .meta\n" # Create outro for (l_i in g_globalvars) l_back = l_back "\necho " g_globalvars[l_i] " $" g_globalvars[l_i] " >> .meta\n" # Create the final script print l_front data l_clean l_back > ".tmp.sh" # Handle AWK buffers and I/O fflush(".tmp.sh") close(".tmp.sh") # Execute temporary script system(which " .tmp.sh") } ########################################### Generate AWK script function parse_awk(which, data, l_front, l_clean, l_back, l_i) { # Create intro l_front = "\nBEGIN{\nwhile ((getline < \".meta\") > 0){\n" for (l_i in g_globalvars) l_front = l_front "if ($0 ~ /" g_globalvars[l_i] "/){\nsub($1\" \", \"\", $0)\n" g_globalvars[l_i] " = $0\nif (" g_globalvars[l_i] " !~ /[[:alpha:][:space:]]/) " g_globalvars[l_i] " += 0\n}\n" l_front = l_front "}\nclose(\".meta\")\n$0=\"\"\n" # Create cleanup action for metafile l_clean = "\nprint \"\" > \".meta\"\n" # Create outro for (l_i in g_globalvars) l_back = l_back "print \"" g_globalvars[l_i] "\", " g_globalvars[l_i] " >> \".meta\"\n" l_back = l_back "\nfflush(\"\")\nclose(\".meta\")\n}" # Create the final script print l_front data l_clean l_back > ".tmp.awk" # Handle AWK buffers and I/O fflush(".tmp.awk") close(".tmp.awk") # Execute temporary script system(which " -f .tmp.awk") } ########################################### Main BEGIN { print "\nThis is the S.O.U.P. meta-interpreter version 0.1 alpha.\n" # Keep track of the amount of used global variables g_varcounter = 0 # Check availability of argument if (ARGC == 1){ print "Usage: ./soup.awk \n" exit 1 } # Argument is available, goto work else { eof = (getline < ARGV[1]) while(eof > 0){ # Process DEFINE keyword if ($0 ~ /#define/){ # Check on SH definition if ($2 ~ /sh/){ sh_binary = substr($0, index($0, "=") + 1) gsub (" ", "", sh_binary) } # Check on AWK definition if ($2 ~ /awk/){ awk_binary = substr($0, index($0, "=") + 1) gsub (" ", "", awk_binary) } } # Process GLOBAL keyword if ($0 ~ /#global/){ g_globalvars[g_varcounter] = $2 g_varcounter ++ } # Process tag if ($0 ~ //){ sub(//, "", $0) data = "" # Assemble Bourne script from source file while ($0 !~ /<\/sh>/){ data = data substr($0, index($0, $1)) "\n" eof = (getline < ARGV[1]) } data = data substr($0, 1, index($0, "") - 1) $0 = substr($0, index($0, "")) # Execute the script parse_sh(sh_binary, data) } # Process tag if ($0 ~ //){ sub(//, "", $0) data = "" # Assemble AWK script from source file while ($0 !~ /<\/awk>/){ data = data substr($0, index($0, $1)) "\n" eof = (getline < ARGV[1]) } data = data substr($0, 1, index($0, "") - 1) $0 = substr($0, index($0, "")) # Execute the script parse_awk(awk_binary, data) } else{ eof = (getline < ARGV[1]) } } close(ARGV[1]) } }