]> git.draconx.ca Git - cdecl99.git/blob - m4/cstring.m4
Add a tool to generate random declarations.
[cdecl99.git] / m4 / cstring.m4
1 dnl Copyright © 2011 Nick Bowler
2 dnl
3 dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
4 dnl This is free software: you are free to do what the fuck you want to.
5 dnl There is NO WARRANTY, to the extent permitted by law.
6
7 m4_define([_DX_COMPUTE_C_STRING_PROG], [AC_LANG_PROGRAM([dnl
8 #include <stdio.h>
9 #include <stdlib.h>
10 $2
11 char thestr@<:@@:>@ = $1;
12 char padstr@<:@@:>@ = "\n!&?~" $1 "~?&!\n";
13 ], [dnl
14 size_t i;
15 FILE *f = fopen("conftest.out", "w");
16 if (!f)
17         return EXIT_FAILURE;
18 for (i = 0; i < sizeof thestr; i++) {
19         if (*(thestr+i) == '\n')
20                 *(thestr+i) = ' ';
21 }
22 fprintf(f, "%s\n", thestr);
23 return 0;
24 ])])
25
26 AC_DEFUN([_DX_COMPUTE_C_STRING_NORUN], [dnl
27 AC_REQUIRE([DX_PROG_STRINGS])
28 if test x"$STRINGS" = x""; then
29         AC_MSG_WARN([strings is recommended for cross compilation])
30         dx_c_strings=cat
31 else
32         dx_c_strings="$STRINGS -n 1"
33 fi
34 dnl else
35 AC_COMPILE_IFELSE([_DX_COMPUTE_C_STRING_PROG([$2], [$3])], [dnl
36         $dx_c_strings conftest.$OBJEXT | sed -n '/^!&?~/ {
37                 t top
38                 :top
39                 s/^!&?~\(.*\)~?&!$/\1/p
40                 t
41                 N
42                 s/\n/ /g
43                 t top
44         }' > conftest.out
45         if read $1 < conftest.out; then :; else
46                 AC_MSG_ERROR([failed to extract string due to cross compilation.])
47         fi
48 ], [dnl
49         ifelse([$4], [], [:], [$4])])
50 dnl fi
51 ])
52
53 dnl DX_COMPUTE_C_STRING(variable, string, [prologue], [action-if-failed])
54 dnl
55 dnl Assigns the value of a C string literal specified by string to the shell
56 dnl variable specified by variable.  Any newlines in the string are converted
57 dnl to spaces.  In a cross compilation environment, it works by scanning a
58 dnl compiled object file.
59 AC_DEFUN([DX_COMPUTE_C_STRING], [dnl
60 AC_LANG_PUSH([C])
61 AC_RUN_IFELSE([_DX_COMPUTE_C_STRING_PROG([$2], [$3])],
62         [read $1 < conftest.out],
63         [ifelse([$4], [], [:], [$4])],
64         [_DX_COMPUTE_C_STRING_NORUN([$1], [$2], [$3], [$4])])
65 AC_LANG_POP([C])
66 ])