]> git.draconx.ca Git - dxcommon.git/commitdiff
gen-strtab.awk: Add an option to suppress object definitions.
authorNick Bowler <nbowler@draconx.ca>
Thu, 26 Jan 2023 02:06:21 +0000 (21:06 -0500)
committerNick Bowler <nbowler@draconx.ca>
Thu, 26 Jan 2023 02:06:21 +0000 (21:06 -0500)
Defining objects in the generated header can make some usage more
difficult.  Add an option which defines a macro instead, allowing
the user to control definitions.

scripts/gen-strtab.awk
tests/scripts.at

index f14bda959db040f4428ee2ad6f08f4f17643d814..2265bb5d42d654aca2ea66aa4ad6d7a8987d139d 100755 (executable)
 #   @nozero
 #     All strings will have a non-zero offset in the strtab.
 #
+#   @macro
+#     Instead of a variable declaration, the generated header will define an
+#     object-like macro that can be used as the initializer for a char array.
+#
 # A string is defined by beginning a line with one or two & characters, which
 # must be immediately followed by a C identifier.  Two & characters indicates
 # a string that should not be translated, as described below.  A nonempty
 #   - All other backslashes are deleted.  This can be used to prevent special
 #     handling of whitespace, # or & characters at the beginning of a line.
 #
-# The output defines a variable, strtab, which contains all of the strings,
-# and each identifier in the input is declared as an emumeration constant
-# whose value is the offset of the associated string within strtab.
+# Unless the @macro option is specified, the output defines a variable,
+# strtab, which contains all of the strings, and each identifier in the input
+# is declared as an emumeration constant whose value is the offset of the
+# associated string within strtab.  Otherwise, if the @macro option is
+# specified, no variables are defined and STRTAB_INITIALIZER object-like macro
+# may be used to initialize a char array with static storage duration.
 #
 # Normally, the generated source code wraps strings using the identity macro
 # N_(x), which has no effect on the resulting data structures but enables tools
@@ -74,6 +81,7 @@ END {
 
 BEGIN {
   opts["zero"] = 1
+  opts["macro"] = 0
   collected = ident = ""
   startline = endline = 0
   num_vars = 0
@@ -131,7 +139,7 @@ END {
 }
 
 END {
-  strtab = ""
+  strtab = cont = ""
   strtab_len = 0
   count = bucketsort(sorted_strings, strings)
   max = 0
@@ -140,7 +148,12 @@ END {
   print "#ifndef N_"
   print "#  define N_(x) x"
   print "#endif"
-  print "\nstatic const char strtab[] ="
+  if (opts["macro"]) {
+    cont = " \\";
+    print "\n#define STRTAB_INITIALIZER" cont;
+  } else {
+    print "\nstatic const char strtab[] =";
+  }
 
   for (i = 0; i < count; i++) {
     s = sorted_strings[i]
@@ -148,7 +161,7 @@ END {
     if ((n = index(strtab "\1", s "\1")) > 0) {
       offsets[sorted_strings[i]] = real_length(substr(strtab, 1, n-1));
       if (!(sorted_strings[i] in nol10n))
-        print "\tSTR_L10N_(N_(\"" sorted_strings[i] "\"))";
+        print "\tSTR_L10N_(N_(\"" sorted_strings[i] "\"))" cont;
     } else if (strtab) {
       strtab = strtab "\1" s
       offsets[sorted_strings[i]] = strtab_len + 1
@@ -166,12 +179,12 @@ END {
     printf("\t%4s ", i > !!opts["zero"] ? "\"\\0\"" : "");
 
     if (split_strtab[i] in nol10n) {
-      print "\"" split_strtab[i] "\"";
+      print "\"" split_strtab[i] "\"" cont;
     } else {
-      print "N_(\"" split_strtab[i] "\")";
+      print "N_(\"" split_strtab[i] "\")" cont;
     }
   }
-  print "\t\"\";";
+  print "\t\"\"" substr(";", 1, !opts["macro"]);
 
   print "enum {"
   for (i = 0; i < num_vars; i++) {
index c03a83f2d2baa080b37e48fb1472cad0abdd0ef2..932a0621915acefd39c39ce5510cc0d88605bb64 100644 (file)
@@ -401,6 +401,37 @@ AT_CHECK([$CC -DHEADER='"test1.h"' -o test1$EXEEXT test.c && ./test1$EXEEXT],
 
 AT_CLEANUP
 
+AT_SETUP([gen-strtab.awk @macro option])
+AT_KEYWORDS([gen-strtab awk script scripts])
+
+AT_DATA([test0.def],
+[[@macro
+&foo foobar
+&bar bar
+&baz baz
+]])
+AT_CHECK([$AWK -f "$builddir/scripts/gen-strtab.awk" <test0.def >test0.h])
+
+AT_DATA([test0.c],
+[[#include <stdio.h>
+extern const char strtab[];
+#include "test0.h"
+
+int main(void)
+{
+  static const char mystrtab[] = STRTAB_INITIALIZER;
+  printf("%s\n%s\n%s\n", mystrtab+foo, mystrtab+bar, mystrtab+baz);
+  return 0;
+}
+]])
+AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0],
+[[foobar
+bar
+baz
+]])
+
+AT_CLEANUP
+
 AT_SETUP([gen-strtab.awk l10n options])
 AT_KEYWORDS([gen-strtab awk script scripts])