#!/bin/awk -f # # # Copy a "set symbol = values" line to the output with new values # # function updatesymbol () { # # # Remove the current value # # sub (/\=[^\n]*/, "= ") indent = length () printf ("%s", $0) if (arguments > 2) { printf ("%s", "(") ++indent } # # # Do the new values contain spaces? # # hasspace = 0 for (argcount = 2; argcount <= arguments && !hasspace; ++argcount) hasspace = index (argument[argcount], " ") # # # Remove unnecessary quotes # # if (!hasspace) for (argcount = 2; argcount <= arguments; ++argcount) if (match (argument[argcount], "^\"[^\"]*\"$")) argument[argcount] = substr (argument[argcount], 2, RLENGTH - 2) # # # Write the values on separate lines? # # separatelines = hasspace if (!separatelines) { linelength = indent for (argcount = 2; argcount <= arguments; ++argcount) linelength = linelength + length (argument[argcount]) + 1 if (linelength > 72) separatelines = 1 } # # # Write the new values # # for (argcount = 2; argcount <= arguments; ++argcount) { if (argcount > 2 && separatelines) for (column = 0; column < indent; ++column) printf (" ") if (hasspace) printf ("\"%s\"", argument[argcount]) else printf ("%s", argument[argcount]) if (argcount < arguments) if (separatelines) printf (" \\\n") else printf (" ") else if (arguments > 2) printf (")") } printf ("\n") return 0 } # # # Initialise # # BEGIN { arguments = ARGC - 2 for (argcount = 1; argcount <= arguments; ++argcount) { argument[argcount] = ARGV[argcount + 1] delete ARGV[argcount + 1] } matched = 0 } # # # Substitute the saved values for a symbol # # { if (match($0, "set[ \t]+" argument[1] "[ \t]*=[^\\n]*")) { matched = 1 # # # Concatenate lines terminated by "\" # # while (substr ($0, length ($0), 1) == "\\") { $0 = substr ($0, 1, length ($0) - 1) if ((getline line) <= 0) break $0 = $0 line } # # # Copy the line to the output with new values # # updatesymbol() } else print $0 } # # # Is the symbol missing from the file? # # END { if (!matched) { $0 = "set " argument[1] " = " updatesymbol() } }