]> git.draconx.ca Git - dxcommon.git/blobdiff - scripts/gen-options.awk
Rework backslash substitutions in awk scripts.
[dxcommon.git] / scripts / gen-options.awk
index 80adcb44177a3fb91f79b0868e4e2b07dd4ca812..c6d361cd89159956c1ef71f948abcd7a845c4bbc 100755 (executable)
@@ -123,6 +123,10 @@ END {
 }
 
 BEGIN {
+  # Check if "\\\\" in substitutions gives just one backslash.
+  bs = "x"; sub(/x/, "\\\\", bs);
+  bs = (length(bs) == 1 ? "\\\\" : "\\");
+
   has_actions = 0
   sopt_string = ""
   num_options = 0
@@ -238,7 +242,7 @@ END {
     lopt_strings = add_to_strtab(lopt_strings, sorted_options[i], offsets)
   }
   gsub(/[^ ]+/, "\"&", lopt_strings)
-  gsub(/ /, "\\0\"\n\t", lopt_strings)
+  gsub(/ /, bs"0\"\n\t", lopt_strings)
 
   print "static const char lopt_strings[] ="
   print "\t" lopt_strings "\";\n"
@@ -318,8 +322,8 @@ END {
       help_offsets[opt] = help_pos
       help_pos += length(help) + 1
 
-      gsub(/"/, "\\\"", help)
-      gsub(/\n/, "\\n\"\n\t    \"", help)
+      gsub(/"/, bs"\"", help)
+      gsub(/\n/, bs"n\"\n\t    \"", help)
       help = "\tPN_(\"" opt "\",\n\t    \"" help "\")"
     }
   }