]> git.draconx.ca Git - dxcommon.git/blobdiff - tests/scripts.at
Partially fix "make distcheck".
[dxcommon.git] / tests / scripts.at
index 41dfbb43760bcf95cd87792db5101e851cd1d882..c38faba07caf02cc03fa5836ec85d39e92ef2297 100644 (file)
@@ -1,4 +1,4 @@
-dnl Copyright © 2021-2022 Nick Bowler
+dnl Copyright © 2021-2023 Nick Bowler
 dnl
 dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
 dnl This is free software: you are free to do what the fuck you want to.
@@ -37,7 +37,7 @@ do stuff with ARG
 --flagval
 ]])
 
-AT_CHECK([$AWK -f "$builddir/scripts/gen-options.awk" <options.def >options.h])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-options.awk" <options.def >options.h])
 
 AT_DATA([context.h],
 [[struct option { const char *name; int has_arg; int *flag; int val; };
@@ -91,55 +91,23 @@ int main(void)
 }
 ]])
 
-# pick out interesting bits from the definitions file
-sed -n '/^-/s/^.*--\([[^\= @<:@]]*\).*$/\1/p' options.def >options
-sed -n '/^-/{
-  s/[[^=]]*\(=[[^@:>@ ]]*\).*$/\1/
-  s/^[[^=]].*//
-  s/^=//
-  p
-}' options.def >argnames
-
-AS_ECHO(["-"]) | sed -n '1s/^-.*//p
-1,/^-/d
-t clear
-:clear
-s/^-.*//p
-t
-s/^#.*//
-s/^ *//
-t next
-:next
-N
-s/\n-.*//
-t done
-s/\n#.*//
-s/\n */\n/g
-t next
-:done
-s/"/\\\\"/g
-s/[[^\n]][[^\n]]*/\\"&\\"/g
-s/^\n*//
-s/\n*$//
-s/\n\n*/ /g
-p
-' options.def - >helptext
-
-exec 3<options 4<argnames 5<helptext 6>expout
-while read opt <&3 && read arg <&4 && read help <&5; do
-  if test ${arg:+y}; then
-    AS_ECHO(["--$opt=$arg"]) >&6
-  else
-    AS_ECHO(["--$opt"]) >&6
-  fi
-  eval "set x $help"; shift
-  for arg
-  do
-    AS_ECHO(["$arg"]) >&6
-  done
-done
-exec 3<&- 4<&- 5<&- 6>&-
+AT_DATA([lopthelp.awk],
+[[/^#/ { next }
+/^-/ {
+  if ($1 !~ /^--/)
+    $1 = $2;
+  if (sub(/\@:>@$/, "", $1))
+    sub(/\@<:@/, "", $1);
+
+  print $1;
+  next;
+}
+
+{ sub(/^[ \t]*/, ""); }
+/./ { print; }
+]])
 
+$AWK -f lopthelp.awk options.def >expout
 AT_CHECK([$CC -o test1$EXEEXT test1.c && ./test1$EXEEXT],
   [0], [expout], [ignore])
 
@@ -169,42 +137,78 @@ int main(void)
     if (SOPT_STRING[i+1] != ':')
       putchar('\n');
   }
+  return 0;
 }
 ]])
 
-sed -n '/^-/{
-  s/=.*/:/
-  s/[[@<:@]]/:/
-  s/^-\([[^-]]\)[[^:]]*/\1/p
-  s/^-.*//p
-}' options.def >sopts
+AT_DATA([soptstr.awk],
+[[/^-/ {
+  if ($1 ~ /^--/)
+    next;
 
-exec 3<options 4<sopts 5>expout
-while read lopt <&3 && read sopt <&4; do
-  if test ${sopt:+y}; then
-    AS_ECHO(["--$lopt $sopt"]) >&5
-  fi
-done
-exec 3<&- 4<&- 5>&-
+  sopt = substr($1, 2, 1);
+  arg = sub(/\@:>@$/, "", $2);
+  arg += sub(/\@<:@?=.*$/, "", $2);
 
+  print $2 " " sopt substr("::", 1, arg);
+}
+]])
+
+$AWK -f soptstr.awk options.def >expout
 AT_CHECK([$CC -o test2$EXEEXT test2.c && ./test2$EXEEXT],
   [0], [expout], [ignore])
 
 # Check that all help strings are translatable
-sed 's/\([[^\\]]\\\)" /\1\\n\\" /g' helptext >help-po
-exec 3<options 4<argnames 5<help-po 6>expected.po
-while read opt <&3 && read arg <&4 && read help <&5; do
-  if test ${arg:+y}; then
-    AS_ECHO(["msgctxt \"$opt\" msgid \"$arg\""]) >&6
-  fi
-  AS_ECHO(["msgctxt \"$opt\" msgid${help:+ }$help"]) >&6
-done
-exec 3<&- 4<&- 5<&- 6>&-
+AT_DATA([messages.awk],
+[[BEGIN { lines = -1; }
+END { output(); }
+
+/^#/ { next }
+/^-/ {
+  output();
+  if ($1 !~ /^--/)
+    $1 = $2;
+
+  tmp=$1;
+  arg="";
+  if (sub(/\@<:@?=.*/, "", $1)) {
+    arg = substr(tmp, index(tmp, "=")+1);
+    sub(/\@:>@$/, "", arg);
+  }
+
+  sub(/^--/, "", $1);
+  ctxt=("msgctxt \"" $1 "\" msgid");
+
+  if (arg)
+    print ctxt, ("\"" arg "\"");
+  next;
+}
+
+{ sub(/^[ \t]*/, ""); }
+/./ {
+  gsub(/"/, "\\\"", $0);
+  help[lines++] = $0;
+}
+
+function output(i)
+{
+  if (lines >= 0) {
+    printf "%s", ctxt;
+    for (i = 0; i < lines; i++) {
+      nl = (i+1 < lines ? "\\n" : "");
+      printf(" \"%s%s\"", help[i], nl);
+    }
+    print "";
+  }
+
+  lines = 0;
+}
+]])
 
 AT_CHECK([xgettext --keyword=PN_:1c,2 options.h
   test -f messages.po || exit 77])
 
-LC_ALL=C sort expected.po >expout
+$AWK -f messages.awk options.def | LC_ALL=C sort >expout
 AT_CHECK([sed -n '/^msgctxt/{
 t next
 :next
@@ -220,6 +224,73 @@ p
 
 AT_CLEANUP
 
+AT_SETUP([gen-options.awk packed format])
+AT_KEYWORDS([gen-options awk script scripts])
+
+AT_DATA([test.c], [[#include <stdio.h>
+struct option { const char *name; int has_arg; int *flag; int val; };
+
+#include "options.h"
+
+static unsigned opts[] = { LOPTS_PACKED_INITIALIZER };
+
+int main(void)
+{
+  unsigned i;
+  int x =
+#if !LOPT_PACK_BITS
+  0
+#elif LOPT_PACK_BITS <= 8
+  1
+#elif LOPT_PACK_BITS <= 16
+  2
+#elif LOPT_PACK_BITS <= 32
+  3
+#else
+#  error too big
+#endif
+  ;
+  printf("%d\n", x);
+  for (i = 0; i < sizeof opts / sizeof opts[0]; i++) {
+    struct option o;
+
+    LOPT_UNPACK(o, opts[i]);
+    printf("--%s, %d, ", o.name, o.has_arg);
+    if (o.val > UCHAR_MAX)
+      printf("%d\n", o.val - UCHAR_MAX - 1);
+    else
+      printf("'%c'\n", o.val);
+  }
+  return 0;
+}
+]])
+
+AT_DATA([single.dat],
+[[--single-option
+]])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-options.awk" <single.dat >options.h])
+AT_CHECK([$CC -o single$EXEEXT test.c && ./single$EXEEXT], [0],
+[[0
+--single-option, 0, 0
+]])
+
+AT_DATA([16bit.dat],
+[[-a, --the-first-option
+-b, --the-second-option=ARG
+-c, --the-third-option[=ARG]
+-d, --the-fourth-option
+]])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-options.awk" <16bit.dat >options.h])
+AT_CHECK([$CC -o 16bit$EXEEXT test.c && ./16bit$EXEEXT], [0],
+[[2
+--the-first-option, 0, 'a'
+--the-second-option, 1, 'b'
+--the-third-option, 2, 'c'
+--the-fourth-option, 0, 'd'
+]])
+
+AT_CLEANUP
+
 AT_SETUP([gen-strtab.awk])
 AT_KEYWORDS([gen-strtab awk script scripts])
 
@@ -246,7 +317,7 @@ newline\
 # with a comment
 ]])
 
-AT_CHECK([$AWK -f "$builddir/scripts/gen-strtab.awk" <test.def >test.h])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test.def >test.h])
 
 sed -n 's/^[[&]]\([[^ ]]*\).*/\1/p' test.def >identifiers
 
@@ -300,6 +371,117 @@ oneline
 ---
 ], [ignore])
 
+AT_CLEANUP
+
+AT_SETUP([gen-strtab.awk @nozero option])
+AT_KEYWORDS([gen-strtab awk script scripts])
+
+AT_DATA([test0.def],
+[[&hello hello
+]])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test0.def >test0.h])
+
+AT_DATA([test1.def],
+[[@nozero
+&hello hello
+]])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test1.def >test1.h])
+
+AT_DATA([test.c],
+[[#include <stdio.h>
+#include HEADER
+int main(void) { printf("%d %s\n", hello, strtab+hello); return 0; }
+]])
+AT_CHECK([$CC -DHEADER='"test0.h"' -o test0$EXEEXT test.c && ./test0$EXEEXT],
+  [0], [[0 hello
+]])
+AT_CHECK([$CC -DHEADER='"test1.h"' -o test1$EXEEXT test.c && ./test1$EXEEXT],
+  [0], [[1 hello
+]])
+
+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 "$srcdir/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])
+
+AT_DATA([l10n.sed], dnl (
+[[/^#/b
+s/.*N_(\([^)]*\)).*/\1/p
+]])
+
+AT_DATA([test0.def],
+[[&a hello world
+&b world
+&c goodbye
+]])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test0.def >test0.h])
+AT_CHECK([sed -n -f l10n.sed test0.h | LC_ALL=C sort], [0],
+[["goodbye"
+"hello world"
+"world"
+]])
+
+AT_DATA([test1.def],
+[[&a hello world
+&&b world
+&&c goodbye
+]])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test1.def >test1.h])
+AT_CHECK([sed -n -f l10n.sed test1.h], [0],
+[["hello world"
+]])
+
+AT_DATA([test.c],
+[[#include <stdio.h>
+#include HEADER
+
+int main(void)
+{
+  printf("%s %s %s\n", strtab+a, strtab+b, strtab+c);
+  return 0;
+}
+]])
+
+AT_CHECK([$CC -DHEADER='"test0.h"' -o test0$EXEEXT test.c && ./test0$EXEEXT],
+  [0], [[hello world world goodbye
+]])
+
+AT_CHECK([$CC -DHEADER='"test1.h"' -o test1$EXEEXT test.c && ./test1$EXEEXT],
+  [0], [[hello world world goodbye
+]])
+
+
 AT_CLEANUP
 
 AT_SETUP([gen-tree.awk])
@@ -330,7 +512,7 @@ ROOT1
 # comment
 ]])
 
-AT_CHECK([$AWK -f "$builddir/scripts/gen-tree.awk" <tree.def >tree.h])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-tree.awk" <tree.def >tree.h])
 
 AT_DATA([test0.c],
 [[#include "tree.h"
@@ -388,7 +570,7 @@ ROOT
   e 1
   f 2
 ]])
-AT_CHECK([$AWK -f "$builddir/scripts/gen-tree.awk" <tree.def >tree.h])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-tree.awk" <tree.def >tree.h])
 
 AT_DATA([test0.c],
 [[float tree_strtab = 0;
@@ -408,6 +590,7 @@ int main(void)
   for (i = 0; i < sizeof root / sizeof root[0]; i++) {
     printf("%d, %d\n", root[i].num, root[i].offset);
   }
+  return 0;
 }
 ]])
 
@@ -430,7 +613,7 @@ AT_DATA([flat.def],
  c 3
 @nostrtab
 ]])
-AT_CHECK([$AWK -f "$builddir/scripts/gen-tree.awk" <flat.def >flat.h])
+AT_CHECK([$AWK -f "$srcdir/scripts/gen-tree.awk" <flat.def >flat.h])
 
 sed -e 's/tree\.h/flat.h/' -e 's/ROOT/FLAT/' test0.c >test1.c
 AT_CHECK([$CC -o test1$EXEEXT test1.c && ./test1$EXEEXT], [0],
@@ -445,7 +628,7 @@ AT_CLEANUP
 AT_SETUP([join.awk])
 AT_KEYWORDS([join awk script scripts])
 
-JOIN="$AWK -f $builddir/scripts/join.awk --"
+JOIN="$AWK -f $srcdir/scripts/join.awk --"
 
 AT_DATA([a],
 [[1 a