lnsocket

A minimal C library for connecting to the lightning network
git clone git://jb55.com/lnsocket
Log | Files | Refs | Submodules | README | LICENSE

configurator.c (33628B)


      1 /* Simple tool to create config.h.
      2  * Would be much easier with ccan modules, but deliberately standalone.
      3  *
      4  * Copyright 2011 Rusty Russell <rusty@rustcorp.com.au>.  MIT license.
      5  *
      6  * c12r_err, c12r_errx functions copied from ccan/err/err.c
      7  * Copyright Rusty Russell <rusty@rustcorp.com.au>. CC0 (Public domain) License.
      8  *
      9  * Permission is hereby granted, free of charge, to any person obtaining a copy
     10  * of this software and associated documentation files (the "Software"), to deal
     11  * in the Software without restriction, including without limitation the rights
     12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     13  * copies of the Software, and to permit persons to whom the Software is
     14  * furnished to do so, subject to the following conditions:
     15  *
     16  * The above copyright notice and this permission notice shall be included in
     17  * all copies or substantial portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     25  * THE SOFTWARE.
     26  */
     27 #define _POSIX_C_SOURCE 200809L                /* For pclose, popen, strdup */
     28 
     29 #define EXIT_BAD_USAGE		  1
     30 #define EXIT_TROUBLE_RUNNING	  2
     31 #define EXIT_BAD_TEST		  3
     32 #define EXIT_BAD_INPUT		  4
     33 
     34 #include <errno.h>
     35 #include <stdio.h>
     36 #include <stdarg.h>
     37 #include <stdbool.h>
     38 #include <stdlib.h>
     39 #include <string.h>
     40 #include <unistd.h>
     41 
     42 #ifdef _MSC_VER
     43 #define popen _popen
     44 #define pclose _pclose
     45 #endif
     46 
     47 #ifdef _MSC_VER
     48 #define DEFAULT_COMPILER "cl"
     49 /* Note:  Dash options avoid POSIX path conversion when used under msys bash
     50  *        and are therefore preferred to slash (e.g. -nologo over /nologo)
     51  * Note:  Disable Warning 4200 "nonstandard extension used : zero-sized array
     52  *        in struct/union" for flexible array members.
     53  */
     54 #define DEFAULT_FLAGS "-nologo -Zi -W4 -wd4200 " \
     55 	"-D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS"
     56 #define DEFAULT_OUTPUT_EXE_FLAG "-Fe:"
     57 #else
     58 #define DEFAULT_COMPILER "cc"
     59 #define DEFAULT_FLAGS "-g3 -ggdb -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
     60 #define DEFAULT_OUTPUT_EXE_FLAG "-o"
     61 #endif
     62 
     63 #define OUTPUT_FILE "configurator.out"
     64 #define INPUT_FILE "configuratortest.c"
     65 
     66 #ifdef _WIN32
     67 #define DIR_SEP   "\\"
     68 #else
     69 #define DIR_SEP   "/"
     70 #endif
     71 
     72 static const char *progname = "";
     73 static int verbose;
     74 static bool like_a_libtool = false;
     75 
     76 struct test {
     77 	const char *name;
     78 	const char *desc;
     79 	/*
     80 	 * Template style flags (pick one):
     81 	 * OUTSIDE_MAIN:
     82 	 * - put a simple boilerplate main below it.
     83 	 * DEFINES_FUNC:
     84 	 * - defines a static function called func; adds ref to avoid warnings
     85 	 * INSIDE_MAIN:
     86 	 * - put this inside main().
     87 	 * DEFINES_EVERYTHING:
     88 	 * - don't add any boilerplate at all.
     89 	 *
     90 	 * Execution flags:
     91 	 * EXECUTE:
     92 	 * - a runtime test; must compile, exit 0 means flag is set.
     93 	 * MAY_NOT_COMPILE:
     94 	 * - Only useful with EXECUTE: don't get upset if it doesn't compile.
     95 	 * <nothing>:
     96 	 * - a compile test, if it compiles must run and exit 0.
     97 	 */
     98 	const char *style;
     99 	const char *depends;
    100 	const char *link;
    101 	const char *fragment;
    102 	const char *flags;
    103 	const char *overrides; /* On success, force this to '1' */
    104 	bool done;
    105 	bool answer;
    106 };
    107 
    108 /* Terminated by a NULL name */
    109 static struct test *tests;
    110 
    111 static const struct test base_tests[] = {
    112 	{ "HAVE_UNALIGNED_ACCESS", "unaligned access to int",
    113 	  "DEFINES_EVERYTHING|EXECUTE", NULL, NULL,
    114 	  "#include <string.h>\n"
    115 	  "int main(int argc, char *argv[]) {\n"
    116 	  "	(void)argc;\n"
    117 	  "     char pad[sizeof(int *) * 1];\n"
    118 	  "	memcpy(pad, argv[0], sizeof(pad));\n"
    119 	  "	int *x = (int *)pad, *y = (int *)(pad + 1);\n"
    120 	  "	return *x == *y;\n"
    121 	  "}\n" },
    122 	{ "HAVE_TYPEOF", "__typeof__ support",
    123 	  "INSIDE_MAIN", NULL, NULL,
    124 	  "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
    125 	{ "HAVE_BIG_ENDIAN", "big endian",
    126 	  "INSIDE_MAIN|EXECUTE", NULL, NULL,
    127 	  "union { int i; char c[sizeof(int)]; } u;\n"
    128 	  "u.i = 0x01020304;\n"
    129 	  "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
    130 	{ "HAVE_BYTESWAP_H", "<byteswap.h>",
    131 	  "OUTSIDE_MAIN", NULL, NULL,
    132 	  "#include <byteswap.h>\n" },
    133 	{ "HAVE_BSWAP_64", "bswap64 in byteswap.h",
    134 	  "DEFINES_FUNC", "HAVE_BYTESWAP_H", NULL,
    135 	  "#include <byteswap.h>\n"
    136 	  "static int func(int x) { return bswap_64(x); }" },
    137 	{ "HAVE_LITTLE_ENDIAN", "little endian",
    138 	  "INSIDE_MAIN|EXECUTE", NULL, NULL,
    139 	  "union { int i; char c[sizeof(int)]; } u;\n"
    140 	  "u.i = 0x01020304;\n"
    141 	  "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
    142 	/*
    143 	{ "HAVE_32BIT_OFF_T", "off_t is 32 bits",
    144 	  "DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE", NULL, NULL,
    145 	  "#include <sys/types.h>\n"
    146 	  "int main(void) {\n"
    147 	  "	return sizeof(off_t) == 4 ? 0 : 1;\n"
    148 	  "}\n" },
    149 	{ "HAVE_ALIGNOF", "__alignof__ support",
    150 	  "INSIDE_MAIN", NULL, NULL,
    151 	  "return __alignof__(double) > 0 ? 0 : 1;" },
    152 	{ "HAVE_ASPRINTF", "asprintf() declaration",
    153 	  "DEFINES_FUNC", NULL, NULL,
    154 	  "#ifndef _GNU_SOURCE\n"
    155 	  "#define _GNU_SOURCE\n"
    156 	  "#endif\n"
    157 	  "#include <stdio.h>\n"
    158 	  "static char *func(int x) {"
    159 	  "	char *p;\n"
    160 	  "	if (asprintf(&p, \"%u\", x) == -1) \n"
    161 	  "		p = NULL;\n"
    162 	  "	return p;\n"
    163 	  "}" },
    164 	{ "HAVE_ATTRIBUTE_COLD", "__attribute__((cold)) support",
    165 	  "DEFINES_FUNC", NULL, NULL,
    166 	  "static int __attribute__((cold)) func(int x) { return x; }" },
    167 	{ "HAVE_ATTRIBUTE_CONST", "__attribute__((const)) support",
    168 	  "DEFINES_FUNC", NULL, NULL,
    169 	  "static int __attribute__((const)) func(int x) { return x; }" },
    170 	{ "HAVE_ATTRIBUTE_DEPRECATED", "__attribute__((deprecated)) support",
    171 	  "DEFINES_FUNC", NULL, NULL,
    172 	  "static int __attribute__((deprecated)) func(int x) { return x; }" },
    173 	{ "HAVE_ATTRIBUTE_NONNULL", "__attribute__((nonnull)) support",
    174 	  "DEFINES_FUNC", NULL, NULL,
    175 	  "static char *__attribute__((nonnull)) func(char *p) { return p; }" },
    176 	{ "HAVE_ATTRIBUTE_RETURNS_NONNULL", "__attribute__((returns_nonnull)) support",
    177 	  "DEFINES_FUNC", NULL, NULL,
    178 	  "static const char *__attribute__((returns_nonnull)) func(void) { return \"hi\"; }" },
    179 	{ "HAVE_ATTRIBUTE_SENTINEL", "__attribute__((sentinel)) support",
    180 	  "DEFINES_FUNC", NULL, NULL,
    181 	  "static int __attribute__((sentinel)) func(int i, ...) { return i; }" },
    182 	{ "HAVE_ATTRIBUTE_PURE", "__attribute__((pure)) support",
    183 	  "DEFINES_FUNC", NULL, NULL,
    184 	  "static int __attribute__((pure)) func(int x) { return x; }" },
    185 	{ "HAVE_ATTRIBUTE_MAY_ALIAS", "__attribute__((may_alias)) support",
    186 	  "OUTSIDE_MAIN", NULL, NULL,
    187 	  "typedef short __attribute__((__may_alias__)) short_a;" },
    188 	{ "HAVE_ATTRIBUTE_NORETURN", "__attribute__((noreturn)) support",
    189 	  "DEFINES_FUNC", NULL, NULL,
    190 	  "#include <stdlib.h>\n"
    191 	  "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
    192 	{ "HAVE_ATTRIBUTE_PRINTF", "__attribute__ format printf support",
    193 	  "DEFINES_FUNC", NULL, NULL,
    194 	  "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { (void)fmt; }" },
    195 	{ "HAVE_ATTRIBUTE_UNUSED", "__attribute__((unused)) support",
    196 	  "OUTSIDE_MAIN", NULL, NULL,
    197 	  "static int __attribute__((unused)) func(int x) { return x; }" },
    198 	{ "HAVE_ATTRIBUTE_USED", "__attribute__((used)) support",
    199 	  "OUTSIDE_MAIN", NULL, NULL,
    200 	  "static int __attribute__((used)) func(int x) { return x; }" },
    201 	{ "HAVE_BACKTRACE", "backtrace() in <execinfo.h>",
    202 	  "DEFINES_FUNC", NULL, NULL,
    203 	  "#include <execinfo.h>\n"
    204 	  "static int func(int x) {"
    205 	  "	void *bt[10];\n"
    206 	  "	return backtrace(bt, 10) < x;\n"
    207 	  "}" },
    208 	{ "HAVE_BUILTIN_CHOOSE_EXPR", "__builtin_choose_expr support",
    209 	  "INSIDE_MAIN", NULL, NULL,
    210 	  "return __builtin_choose_expr(1, 0, \"garbage\");" },
    211 	{ "HAVE_BUILTIN_CLZ", "__builtin_clz support",
    212 	  "INSIDE_MAIN", NULL, NULL,
    213 	  "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
    214 	{ "HAVE_BUILTIN_CLZL", "__builtin_clzl support",
    215 	  "INSIDE_MAIN", NULL, NULL,
    216 	  "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
    217 	{ "HAVE_BUILTIN_CLZLL", "__builtin_clzll support",
    218 	  "INSIDE_MAIN", NULL, NULL,
    219 	  "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
    220 	{ "HAVE_BUILTIN_CTZ", "__builtin_ctz support",
    221 	  "INSIDE_MAIN", NULL, NULL,
    222 	  "return __builtin_ctz(1 << (sizeof(int)*8 - 1)) == (sizeof(int)*8 - 1) ? 0 : 1;" },
    223 	{ "HAVE_BUILTIN_CTZL", "__builtin_ctzl support",
    224 	  "INSIDE_MAIN", NULL, NULL,
    225 	  "return __builtin_ctzl(1UL << (sizeof(long)*8 - 1)) == (sizeof(long)*8 - 1) ? 0 : 1;" },
    226 	{ "HAVE_BUILTIN_CTZLL", "__builtin_ctzll support",
    227 	  "INSIDE_MAIN", NULL, NULL,
    228 	  "return __builtin_ctzll(1ULL << (sizeof(long long)*8 - 1)) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
    229 	{ "HAVE_BUILTIN_CONSTANT_P", "__builtin_constant_p support",
    230 	  "INSIDE_MAIN", NULL, NULL,
    231 	  "return __builtin_constant_p(1) ? 0 : 1;" },
    232 	{ "HAVE_BUILTIN_EXPECT", "__builtin_expect support",
    233 	  "INSIDE_MAIN", NULL, NULL,
    234 	  "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
    235 	{ "HAVE_BUILTIN_FFS", "__builtin_ffs support",
    236 	  "INSIDE_MAIN", NULL, NULL,
    237 	  "return __builtin_ffs(0) == 0 ? 0 : 1;" },
    238 	{ "HAVE_BUILTIN_FFSL", "__builtin_ffsl support",
    239 	  "INSIDE_MAIN", NULL, NULL,
    240 	  "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
    241 	{ "HAVE_BUILTIN_FFSLL", "__builtin_ffsll support",
    242 	  "INSIDE_MAIN", NULL, NULL,
    243 	  "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
    244 	{ "HAVE_BUILTIN_POPCOUNT", "__builtin_popcount support",
    245 	  "INSIDE_MAIN", NULL, NULL,
    246 	  "return __builtin_popcount(255) == 8 ? 0 : 1;" },
    247 	{ "HAVE_BUILTIN_POPCOUNTL",  "__builtin_popcountl support",
    248 	  "INSIDE_MAIN", NULL, NULL,
    249 	  "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
    250 	{ "HAVE_BUILTIN_POPCOUNTLL", "__builtin_popcountll support",
    251 	  "INSIDE_MAIN", NULL, NULL,
    252 	  "return __builtin_popcountll(255LL) == 8 ? 0 : 1;" },
    253 	{ "HAVE_BUILTIN_TYPES_COMPATIBLE_P", "__builtin_types_compatible_p support",
    254 	  "INSIDE_MAIN", NULL, NULL,
    255 	  "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
    256 	{ "HAVE_ICCARM_INTRINSICS", "<intrinsics.h>",
    257 	  "DEFINES_FUNC", NULL, NULL,
    258 	  "#include <intrinsics.h>\n"
    259 	  "int func(int v) {\n"
    260 	  "	return __CLZ(__RBIT(v));\n"
    261 	  "}" },
    262 	{ "HAVE_CLOCK_GETTIME", "clock_gettime() declaration",
    263 	  "DEFINES_FUNC", "HAVE_STRUCT_TIMESPEC", NULL,
    264 	  "#include <time.h>\n"
    265 	  "static struct timespec func(void) {\n"
    266 	  "	struct timespec ts;\n"
    267 	  "	clock_gettime(CLOCK_REALTIME, &ts);\n"
    268 	  "	return ts;\n"
    269 	  "}\n" },
    270 	{ "HAVE_CLOCK_GETTIME_IN_LIBRT", "clock_gettime() in librt",
    271 	  "DEFINES_FUNC",
    272 	  "HAVE_STRUCT_TIMESPEC !HAVE_CLOCK_GETTIME",
    273 	  "-lrt",
    274 	  "#include <time.h>\n"
    275 	  "static struct timespec func(void) {\n"
    276 	  "	struct timespec ts;\n"
    277 	  "	clock_gettime(CLOCK_REALTIME, &ts);\n"
    278 	  "	return ts;\n"
    279 	  "}\n",
    280 	  "HAVE_CLOCK_GETTIME" },
    281 	{ "HAVE_COMPOUND_LITERALS", "compound literal support",
    282 	  "INSIDE_MAIN", NULL, NULL,
    283 	  "int *foo = (int[]) { 1, 2, 3, 4 };\n"
    284 	  "return foo[0] ? 0 : 1;" },
    285 	{ "HAVE_FCHDIR", "fchdir support",
    286 	  "DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE", NULL, NULL,
    287 	  "#include <sys/types.h>\n"
    288 	  "#include <sys/stat.h>\n"
    289 	  "#include <fcntl.h>\n"
    290 	  "#include <unistd.h>\n"
    291 	  "int main(void) {\n"
    292 	  "	int fd = open(\"..\", O_RDONLY);\n"
    293 	  "	return fchdir(fd) == 0 ? 0 : 1;\n"
    294 	  "}\n" },
    295 	{ "HAVE_ERR_H", "<err.h>",
    296 	  "DEFINES_FUNC", NULL, NULL,
    297 	  "#include <err.h>\n"
    298 	  "static void func(int arg) {\n"
    299 	  "	if (arg == 0)\n"
    300 	  "		err(1, \"err %u\", arg);\n"
    301 	  "	if (arg == 1)\n"
    302 	  "		errx(1, \"err %u\", arg);\n"
    303 	  "	if (arg == 3)\n"
    304 	  "		warn(\"warn %u\", arg);\n"
    305 	  "	if (arg == 4)\n"
    306 	  "		warnx(\"warn %u\", arg);\n"
    307 	  "}\n" },
    308 	{ "HAVE_FILE_OFFSET_BITS", "_FILE_OFFSET_BITS to get 64-bit offsets",
    309 	  "DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE",
    310 	  "HAVE_32BIT_OFF_T", NULL,
    311 	  "#define _FILE_OFFSET_BITS 64\n"
    312 	  "#include <sys/types.h>\n"
    313 	  "int main(void) {\n"
    314 	  "	return sizeof(off_t) == 8 ? 0 : 1;\n"
    315 	  "}\n" },
    316 	{ "HAVE_FOR_LOOP_DECLARATION", "for loop declaration support",
    317 	  "INSIDE_MAIN", NULL, NULL,
    318 	  "int ret = 1;\n"
    319 	  "for (int i = 0; i < argc; i++) { ret = 0; };\n"
    320 	  "return ret;" },
    321 	{ "HAVE_FLEXIBLE_ARRAY_MEMBER", "flexible array member support",
    322 	  "OUTSIDE_MAIN", NULL, NULL,
    323 	  "struct foo { unsigned int x; int arr[]; };" },
    324 	{ "HAVE_GETPAGESIZE", "getpagesize() in <unistd.h>",
    325 	  "DEFINES_FUNC", NULL, NULL,
    326 	  "#include <unistd.h>\n"
    327 	  "static int func(void) { return getpagesize(); }" },
    328 	{ "HAVE_ISBLANK", "isblank() in <ctype.h>",
    329 	  "DEFINES_FUNC", NULL, NULL,
    330 	  "#ifndef _GNU_SOURCE\n"
    331 	  "#define _GNU_SOURCE\n"
    332 	  "#endif\n"
    333 	  "#include <ctype.h>\n"
    334 	  "static int func(void) { return isblank(' '); }" },
    335 	{ "HAVE_MEMMEM", "memmem in <string.h>",
    336 	  "DEFINES_FUNC", NULL, NULL,
    337 	  "#ifndef _GNU_SOURCE\n"
    338 	  "#define _GNU_SOURCE\n"
    339 	  "#endif\n"
    340 	  "#include <string.h>\n"
    341 	  "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
    342 	  "return memmem(h, hl, n, nl);"
    343 	  "}\n", },
    344 	{ "HAVE_MEMRCHR", "memrchr in <string.h>",
    345 	  "DEFINES_FUNC", NULL, NULL,
    346 	  "#ifndef _GNU_SOURCE\n"
    347 	  "#define _GNU_SOURCE\n"
    348 	  "#endif\n"
    349 	  "#include <string.h>\n"
    350 	  "static void *func(void *s, int c, size_t n) {\n"
    351 	  "return memrchr(s, c, n);"
    352 	  "}\n", },
    353 	{ "HAVE_MMAP", "mmap() declaration",
    354 	  "DEFINES_FUNC", NULL, NULL,
    355 	  "#include <sys/mman.h>\n"
    356 	  "static void *func(int fd) {\n"
    357 	  "	return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
    358 	  "}" },
    359 	{ "HAVE_PROC_SELF_MAPS", "/proc/self/maps exists",
    360 	  "DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE", NULL, NULL,
    361 	  "#include <sys/types.h>\n"
    362 	  "#include <sys/stat.h>\n"
    363 	  "#include <fcntl.h>\n"
    364 	  "int main(void) {\n"
    365 	  "	return open(\"/proc/self/maps\", O_RDONLY) != -1 ? 0 : 1;\n"
    366 	  "}\n" },
    367 	{ "HAVE_QSORT_R_PRIVATE_LAST", "qsort_r cmp takes trailing arg",
    368 	  "DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE", NULL, NULL,
    369 	  "#ifndef _GNU_SOURCE\n"
    370 	  "#define _GNU_SOURCE\n"
    371 	  "#endif\n"
    372 	  "#include <stdlib.h>\n"
    373 	  "static int cmp(const void *lp, const void *rp, void *priv) {\n"
    374 	  " *(unsigned int *)priv = 1;\n"
    375 	  " return *(const int *)lp - *(const int *)rp; }\n"
    376 	  "int main(void) {\n"
    377 	  " int array[] = { 9, 2, 5 };\n"
    378 	  " unsigned int called = 0;\n"
    379 	  " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
    380 	  " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
    381 	  "}\n" },
    382 	{ "HAVE_STRUCT_TIMESPEC", "struct timespec declaration",
    383 	  "DEFINES_FUNC", NULL, NULL,
    384 	  "#include <time.h>\n"
    385 	  "static void func(void) {\n"
    386 	  "	struct timespec ts;\n"
    387 	  "	ts.tv_sec = ts.tv_nsec = 1;\n"
    388 	  "}\n" },
    389 	{ "HAVE_SECTION_START_STOP", "__attribute__((section)) and __start/__stop",
    390 	  "DEFINES_FUNC", NULL, NULL,
    391 	  "static void *__attribute__((__section__(\"mysec\"))) p = &p;\n"
    392 	  "static int func(void) {\n"
    393 	  "	extern void *__start_mysec[], *__stop_mysec[];\n"
    394 	  "	return __stop_mysec - __start_mysec;\n"
    395 	  "}\n" },
    396 	{ "HAVE_STACK_GROWS_UPWARDS", "stack grows upwards",
    397 	  "DEFINES_EVERYTHING|EXECUTE", NULL, NULL,
    398 	  "#include <stddef.h>\n"
    399 	  "static ptrdiff_t nest(const void *base, unsigned int i)\n"
    400 	  "{\n"
    401 	  "	if (i == 0)\n"
    402 	  "		return (const char *)&i - (const char *)base;\n"
    403 	  "	return nest(base, i-1);\n"
    404 	  "}\n"
    405 	  "int main(int argc, char *argv[]) {\n"
    406 	  "	(void)argv;\n"
    407 	  "	return (nest(&argc, argc) > 0) ? 0 : 1;\n"
    408 	  "}\n" },
    409 	{ "HAVE_STATEMENT_EXPR", "statement expression support",
    410 	  "INSIDE_MAIN", NULL, NULL,
    411 	  "return ({ int x = argc; x == argc ? 0 : 1; });" },
    412 	{ "HAVE_SYS_FILIO_H", "<sys/filio.h>",
    413 	  "OUTSIDE_MAIN", NULL, NULL,
    414 	  "#include <sys/filio.h>\n" },
    415 	{ "HAVE_SYS_TERMIOS_H", "<sys/termios.h>",
    416 	  "OUTSIDE_MAIN", NULL, NULL,
    417 	  "#include <sys/termios.h>\n" },
    418 	{ "HAVE_SYS_UNISTD_H", "<sys/unistd.h>",
    419 	  "OUTSIDE_MAIN", NULL, NULL,
    420 	  "#include <sys/unistd.h>\n" },
    421 	{ "HAVE_UTIME", "utime() declaration",
    422 	  "DEFINES_FUNC", NULL, NULL,
    423 	  "#include <sys/types.h>\n"
    424 	  "#include <utime.h>\n"
    425 	  "static int func(const char *filename) {\n"
    426 	  "	struct utimbuf times = { 0 };\n"
    427 	  "	return utime(filename, &times);\n"
    428 	  "}" },
    429 	{ "HAVE_WARN_UNUSED_RESULT", "__attribute__((warn_unused_result))",
    430 	  "DEFINES_FUNC", NULL, NULL,
    431 	  "#include <sys/types.h>\n"
    432 	  "#include <utime.h>\n"
    433 	  "static __attribute__((warn_unused_result)) int func(int i) {\n"
    434 	  "	return i + 1;\n"
    435 	  "}" },
    436 	{ "HAVE_OPENMP", "#pragma omp and -fopenmp support",
    437 	  "INSIDE_MAIN|EXECUTE|MAY_NOT_COMPILE", NULL, NULL,
    438 	  "int i;\n"
    439 	  "#pragma omp parallel for\n"
    440 	  "for(i = 0; i < 0; i++) {};\n"
    441 	  "return 0;\n",
    442 	  "-Werror -fopenmp" },
    443 	{ "HAVE_VALGRIND_MEMCHECK_H", "<valgrind/memcheck.h>",
    444 	  "OUTSIDE_MAIN", NULL, NULL,
    445 	  "#include <valgrind/memcheck.h>\n" },
    446 	{ "HAVE_UCONTEXT", "working <ucontext.h",
    447 	  "DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE",
    448 	  NULL, NULL,
    449 	  "#include <ucontext.h>\n"
    450 	  "static int x = 0;\n"
    451 	  "static char stack[2048];\n"
    452 	  "static ucontext_t a, b;\n"
    453 	  "static void fn(void) {\n"
    454 	  "	x |= 2;\n"
    455 	  "	setcontext(&b);\n"
    456 	  "	x |= 4;\n"
    457 	  "}\n"
    458 	  "int main(void) {\n"
    459 	  "	x |= 1;\n"
    460 	  "	getcontext(&a);\n"
    461 	  "	a.uc_stack.ss_sp = stack;\n"
    462 	  "	a.uc_stack.ss_size = sizeof(stack);\n"
    463 	  "	makecontext(&a, fn, 0);\n"
    464 	  "	swapcontext(&b, &a);\n"
    465 	  "	return (x == 3) ? 0 : 1;\n"
    466 	  "}\n"
    467 	},
    468 	{ "HAVE_POINTER_SAFE_MAKECONTEXT", "passing pointers via makecontext()",
    469 	  "DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE",
    470 	  "HAVE_UCONTEXT", NULL,
    471 	  "#include <stddef.h>\n"
    472 	  "#include <ucontext.h>\n"
    473 	  "static int worked = 0;\n"
    474 	  "static char stack[1024];\n"
    475 	  "static ucontext_t a, b;\n"
    476 	  "static void fn(void *p, void *q) {\n"
    477 	  "	void *cp = &worked;\n"
    478 	  "	void *cq = (void *)(~((ptrdiff_t)cp));\n"
    479 	  "	if ((p == cp) && (q == cq))\n"
    480 	  "		worked = 1;\n"
    481 	  "	setcontext(&b);\n"
    482 	  "}\n"
    483 	  "int main(void) {\n"
    484 	  "	void *ap = &worked;\n"
    485 	  "	void *aq = (void *)(~((ptrdiff_t)ap));\n"
    486 	  "	getcontext(&a);\n"
    487 	  "	a.uc_stack.ss_sp = stack;\n"
    488 	  "	a.uc_stack.ss_size = sizeof(stack);\n"
    489 	  "	makecontext(&a, (void (*)(void))fn, 2, ap, aq);\n"
    490 	  "	swapcontext(&b, &a);\n"
    491 	  "	return worked ? 0 : 1;\n"
    492 	  "}\n"
    493 	},
    494 	{ "HAVE_BUILTIN_CPU_SUPPORTS", "__builtin_cpu_supports()",
    495 	  "DEFINES_FUNC", NULL, NULL,
    496 	  "#include <stdbool.h>\n"
    497 	  "static bool func(void) {\n"
    498 	  "	return __builtin_cpu_supports(\"mmx\");\n"
    499 	  "}"
    500 	},
    501 	{ "HAVE_CLOSEFROM", "closefrom() offered by system",
    502 	  "DEFINES_EVERYTHING", NULL, NULL,
    503 	  "#include <stdlib.h>\n"
    504 	  "#include <unistd.h>\n"
    505 	  "int main(void) {\n"
    506 	  "    closefrom(STDERR_FILENO + 1);\n"
    507 	  "    return 0;\n"
    508 	  "}\n"
    509 	},
    510 	{ "HAVE_F_CLOSEM", "F_CLOSEM defined for fctnl.",
    511 	  "DEFINES_EVERYTHING", NULL, NULL,
    512 	  "#include <fcntl.h>\n"
    513 	  "#include <unistd.h>\n"
    514 	  "int main(void) {\n"
    515 	  "    int res = fcntl(STDERR_FILENO + 1, F_CLOSEM, 0);\n"
    516 	  "    return res < 0;\n"
    517 	  "}\n"
    518 	},
    519 	{ "HAVE_NR_CLOSE_RANGE", "close_range syscall available as __NR_close_range.",
    520 	  "DEFINES_EVERYTHING", NULL, NULL,
    521 	  "#include <limits.h>\n"
    522 	  "#include <sys/syscall.h>\n"
    523 	  "#include <unistd.h>\n"
    524 	  "int main(void) {\n"
    525 	  "    int res = syscall(__NR_close_range, STDERR_FILENO + 1, INT_MAX, 0);\n"
    526 	  "    return res < 0;\n"
    527 	  "}\n"
    528 	},
    529 	{ "HAVE_F_MAXFD", "F_MAXFD defined for fcntl.",
    530 	  "DEFINES_EVERYTHING", NULL, NULL,
    531 	  "#include <fcntl.h>\n"
    532 	  "#include <unistd.h>\n"
    533 	  "int main(void) {\n"
    534 	  "    int res = fcntl(0, F_MAXFD);\n"
    535 	  "    return res < 0;\n"
    536 	  "}\n"
    537 	},
    538 	*/
    539 };
    540 
    541 static void c12r_err(int eval, const char *fmt, ...)
    542 {
    543 	int err_errno = errno;
    544 	va_list ap;
    545 
    546 	fprintf(stderr, "%s: ", progname);
    547 	va_start(ap, fmt);
    548 	vfprintf(stderr, fmt, ap);
    549 	va_end(ap);
    550 	fprintf(stderr, ": %s\n", strerror(err_errno));
    551 	exit(eval);
    552 }
    553 
    554 static void c12r_errx(int eval, const char *fmt, ...)
    555 {
    556 	va_list ap;
    557 
    558 	fprintf(stderr, "%s: ", progname);
    559 	va_start(ap, fmt);
    560 	vfprintf(stderr, fmt, ap);
    561 	va_end(ap);
    562 	fprintf(stderr, "\n");
    563 	exit(eval);
    564 }
    565 
    566 static void start_test(const char *what, const char *why)
    567 {
    568 	if (like_a_libtool) {
    569 		printf("%s%s... ", what, why);
    570 		fflush(stdout);
    571 	}
    572 }
    573 
    574 static void end_test(bool result)
    575 {
    576 	if (like_a_libtool)
    577 		printf("%s\n", result ? "yes" : "no");
    578 }
    579 
    580 static size_t fcopy(FILE *fsrc, FILE *fdst)
    581 {
    582 	char buffer[BUFSIZ];
    583 	size_t rsize, wsize;
    584 	size_t copied = 0;
    585 
    586 	while ((rsize = fread(buffer, 1, BUFSIZ, fsrc)) > 0) {
    587 		wsize = fwrite(buffer, 1, rsize, fdst);
    588 		copied += wsize;
    589 		if (wsize != rsize)
    590 			break;
    591 	}
    592 
    593 	return copied;
    594 }
    595 
    596 static char *grab_stream(FILE *file)
    597 {
    598 	size_t max, ret, size = 0;
    599 	char *buffer;
    600 
    601 	max = BUFSIZ;
    602 	buffer = malloc(max);
    603 	while ((ret = fread(buffer+size, 1, max - size, file)) == max - size) {
    604 		size += ret;
    605 		buffer = realloc(buffer, max *= 2);
    606 	}
    607 	size += ret;
    608 	if (ferror(file))
    609 		c12r_err(EXIT_TROUBLE_RUNNING, "reading from command");
    610 	buffer[size] = '\0';
    611 	return buffer;
    612 }
    613 
    614 static char *run(const char *cmd, int *exitstatus)
    615 {
    616 	static const char redir[] = " 2>&1";
    617 	size_t cmdlen;
    618 	char *cmdredir;
    619 	FILE *cmdout;
    620 	char *ret;
    621 
    622 	cmdlen = strlen(cmd);
    623 	cmdredir = malloc(cmdlen + sizeof(redir));
    624 	memcpy(cmdredir, cmd, cmdlen);
    625 	memcpy(cmdredir + cmdlen, redir, sizeof(redir));
    626 
    627 	cmdout = popen(cmdredir, "r");
    628 	if (!cmdout)
    629 		c12r_err(EXIT_TROUBLE_RUNNING, "popen \"%s\"", cmdredir);
    630 
    631 	free(cmdredir);
    632 
    633 	ret = grab_stream(cmdout);
    634 	*exitstatus = pclose(cmdout);
    635 	return ret;
    636 }
    637 
    638 static char *connect_args(const char *argv[], const char *outflag,
    639 		const char *files)
    640 {
    641 	unsigned int i;
    642 	char *ret;
    643 	size_t len = strlen(outflag) + strlen(files) + 1;
    644 
    645 	for (i = 1; argv[i]; i++)
    646 		len += 1 + strlen(argv[i]);
    647 
    648 	ret = malloc(len);
    649 	len = 0;
    650 	for (i = 1; argv[i]; i++) {
    651 		strcpy(ret + len, argv[i]);
    652 		len += strlen(argv[i]);
    653 		if (argv[i+1] || *outflag)
    654 			ret[len++] = ' ';
    655 	}
    656 	strcpy(ret + len, outflag);
    657 	len += strlen(outflag);
    658 	strcpy(ret + len, files);
    659 	return ret;
    660 }
    661 
    662 static struct test *find_test(const char *name)
    663 {
    664 	unsigned int i;
    665 
    666 	for (i = 0; tests[i].name; i++) {
    667 		if (strcmp(tests[i].name, name) == 0)
    668 			return &tests[i];
    669 	}
    670 	c12r_errx(EXIT_BAD_TEST, "Unknown test %s", name);
    671 	abort();
    672 }
    673 
    674 #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
    675 #define MAIN_START_BOILERPLATE \
    676 	"int main(int argc, char *argv[]) {\n" \
    677 	"	(void)argc;\n" \
    678 	"	(void)argv;\n"
    679 #define USE_FUNC_BOILERPLATE "(void)func;\n"
    680 #define MAIN_BODY_BOILERPLATE "return 0;\n"
    681 #define MAIN_END_BOILERPLATE "}\n"
    682 
    683 static bool run_test(const char *cmd, const char *wrapper, struct test *test)
    684 {
    685 	char *output, *newcmd;
    686 	FILE *outf;
    687 	int status;
    688 
    689 	if (test->done)
    690 		return test->answer;
    691 
    692 	if (test->depends) {
    693 		size_t len;
    694 		const char *deps = test->depends;
    695 		char *dep;
    696 
    697 		/* Space-separated dependencies, could be ! for inverse. */
    698 		while ((len = strcspn(deps, " ")) != 0) {
    699 			bool positive = true;
    700 			if (deps[len]) {
    701 				dep = strdup(deps);
    702 				dep[len] = '\0';
    703 			} else {
    704 				dep = (char *)deps;
    705 			}
    706 
    707 			if (dep[0] == '!') {
    708 				dep++;
    709 				positive = false;
    710 			}
    711 			if (run_test(cmd, wrapper, find_test(dep)) != positive) {
    712 				test->answer = false;
    713 				test->done = true;
    714 				return test->answer;
    715 			}
    716 			if (deps[len])
    717 				free(dep);
    718 
    719 			deps += len;
    720 			deps += strspn(deps, " ");
    721 		}
    722 	}
    723 
    724 	outf = fopen(INPUT_FILE, verbose > 1 ? "w+" : "w");
    725 	if (!outf)
    726 		c12r_err(EXIT_TROUBLE_RUNNING, "creating %s", INPUT_FILE);
    727 
    728 	fprintf(outf, "%s", PRE_BOILERPLATE);
    729 
    730 	if (strstr(test->style, "INSIDE_MAIN")) {
    731 		fprintf(outf, "%s", MAIN_START_BOILERPLATE);
    732 		fprintf(outf, "%s", test->fragment);
    733 		fprintf(outf, "%s", MAIN_END_BOILERPLATE);
    734 	} else if (strstr(test->style, "OUTSIDE_MAIN")) {
    735 		fprintf(outf, "%s", test->fragment);
    736 		fprintf(outf, "%s", MAIN_START_BOILERPLATE);
    737 		fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
    738 		fprintf(outf, "%s", MAIN_END_BOILERPLATE);
    739 	} else if (strstr(test->style, "DEFINES_FUNC")) {
    740 		fprintf(outf, "%s", test->fragment);
    741 		fprintf(outf, "%s", MAIN_START_BOILERPLATE);
    742 		fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
    743 		fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
    744 		fprintf(outf, "%s", MAIN_END_BOILERPLATE);
    745 	} else if (strstr(test->style, "DEFINES_EVERYTHING")) {
    746 		fprintf(outf, "%s", test->fragment);
    747 	} else
    748 		c12r_errx(EXIT_BAD_TEST, "Unknown style for test %s: %s",
    749 			  test->name, test->style);
    750 
    751 	if (verbose > 1) {
    752 		fseek(outf, 0, SEEK_SET);
    753 		fcopy(outf, stdout);
    754 	}
    755 
    756 	fclose(outf);
    757 
    758 	newcmd = strdup(cmd);
    759 
    760 	if (test->flags) {
    761 		newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
    762 				+ strlen(test->flags) + 1);
    763 		strcat(newcmd, " ");
    764 		strcat(newcmd, test->flags);
    765 		if (verbose > 1)
    766 			printf("Extra flags line: %s", newcmd);
    767 	}
    768 
    769 	if (test->link) {
    770 		newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
    771 				+ strlen(test->link) + 1);
    772 		strcat(newcmd, " ");
    773 		strcat(newcmd, test->link);
    774 		if (verbose > 1)
    775 			printf("Extra link line: %s", newcmd);
    776 	}
    777 
    778 	start_test("checking for ", test->desc);
    779 	output = run(newcmd, &status);
    780 
    781 	free(newcmd);
    782 
    783 	if (status != 0 || strstr(output, "warning")) {
    784 		if (verbose)
    785 			printf("Compile %s for %s, status %i: %s\n",
    786 			       status ? "fail" : "warning",
    787 			       test->name, status, output);
    788 		if (strstr(test->style, "EXECUTE")
    789 		    && !strstr(test->style, "MAY_NOT_COMPILE"))
    790 			c12r_errx(EXIT_BAD_TEST,
    791 				  "Test for %s did not compile:\n%s",
    792 				  test->name, output);
    793 		test->answer = false;
    794 		free(output);
    795 	} else {
    796 		/* Compile succeeded. */
    797 		free(output);
    798 		/* We run INSIDE_MAIN tests for sanity checking. */
    799 		if (strstr(test->style, "EXECUTE")
    800 		    || strstr(test->style, "INSIDE_MAIN")) {
    801 			char *cmd = malloc(strlen(wrapper) + strlen(" ." DIR_SEP OUTPUT_FILE) + 1);
    802 
    803 			strcpy(cmd, wrapper);
    804 			strcat(cmd, " ." DIR_SEP OUTPUT_FILE);
    805 			output = run(cmd, &status);
    806 			free(cmd);
    807 			if (!strstr(test->style, "EXECUTE") && status != 0)
    808 				c12r_errx(EXIT_BAD_TEST,
    809 					  "Test for %s failed with %i:\n%s",
    810 					  test->name, status, output);
    811 			if (verbose && status)
    812 				printf("%s exited %i\n", test->name, status);
    813 			free(output);
    814 		}
    815 		test->answer = (status == 0);
    816 	}
    817 	test->done = true;
    818 	end_test(test->answer);
    819 
    820 	if (test->answer && test->overrides) {
    821 		struct test *override = find_test(test->overrides);
    822 		override->done = true;
    823 		override->answer = true;
    824 	}
    825 	return test->answer;
    826 }
    827 
    828 static char *any_field(char **fieldname)
    829 {
    830 	char buf[1000];
    831 	for (;;) {
    832 		char *p, *eq;
    833 
    834 		if (!fgets(buf, sizeof(buf), stdin))
    835 			return NULL;
    836 
    837 		p = buf;
    838 		/* Ignore whitespace, lines starting with # */
    839 		while (*p == ' ' || *p == '\t')
    840 			p++;
    841 		if (*p == '#' || *p == '\n')
    842 			continue;
    843 
    844 		eq = strchr(p, '=');
    845 		if (!eq)
    846 			c12r_errx(EXIT_BAD_INPUT, "no = in line: %s", p);
    847 		*eq = '\0';
    848 		*fieldname = strdup(p);
    849 		p = eq + 1;
    850 		if (strlen(p) && p[strlen(p)-1] == '\n')
    851 			p[strlen(p)-1] = '\0';
    852 		return strdup(p);
    853 	}
    854 }
    855 
    856 static char *read_field(const char *name, bool compulsory)
    857 {
    858 	char *fieldname, *value;
    859 
    860 	value = any_field(&fieldname);
    861 	if (!value) {
    862 		if (!compulsory)
    863 			return NULL;
    864 		c12r_errx(EXIT_BAD_INPUT, "Could not read field %s", name);
    865 	}
    866 	if (strcmp(fieldname, name) != 0)
    867 		c12r_errx(EXIT_BAD_INPUT,
    868 			  "Expected field %s not %s", name, fieldname);
    869 	return value;
    870 }
    871 
    872 /* Test descriptions from stdin:
    873  * Lines starting with # or whitespace-only are ignored.
    874  *
    875  * First three non-ignored lines must be:
    876  *  var=<varname>
    877  *  desc=<description-for-autotools-style>
    878  *  style=OUTSIDE_MAIN DEFINES_FUNC INSIDE_MAIN DEFINES_EVERYTHING EXECUTE MAY_NOT_COMPILE
    879  *
    880  * Followed by optional lines:
    881  *  depends=<space-separated-testnames, ! to invert>
    882  *  link=<extra args for link line>
    883  *  flags=<extra args for compile line>
    884  *  overrides=<testname-to-force>
    885  *
    886  * Finally a code line, either:
    887  *  code=<oneline> OR
    888  *  code=
    889  *  <lines of code>
    890  *  <end-comment>
    891  *
    892  * And <end-comment> looks like this next comment: */
    893 /*END*/
    894 static bool read_test(struct test *test)
    895 {
    896 	char *field, *value;
    897 	char buf[1000];
    898 
    899 	memset(test, 0, sizeof(*test));
    900 	test->name = read_field("var", false);
    901 	if (!test->name)
    902 		return false;
    903 	test->desc = read_field("desc", true);
    904 	test->style = read_field("style", true);
    905 	/* Read any optional fields. */
    906 	while ((value = any_field(&field)) != NULL) {
    907 		if (strcmp(field, "depends") == 0)
    908 			test->depends = value;
    909 		else if (strcmp(field, "link") == 0)
    910 			test->link = value;
    911 		else if (strcmp(field, "flags") == 0)
    912 			test->flags = value;
    913 		else if (strcmp(field, "overrides") == 0)
    914 			test->overrides = value;
    915 		else if (strcmp(field, "code") == 0)
    916 			break;
    917 		else
    918 			c12r_errx(EXIT_BAD_INPUT, "Unknown field %s in %s",
    919 				  field, test->name);
    920 	}
    921 	if (!value)
    922 		c12r_errx(EXIT_BAD_INPUT, "Missing code in %s", test->name);
    923 
    924 	if (strlen(value) == 0) {
    925 		/* Multiline program, read to END comment */
    926 		while (fgets(buf, sizeof(buf), stdin) != 0) {
    927 			size_t n;
    928 			if (strncmp(buf, "/*END*/", 7) == 0)
    929 				break;
    930 			n = strlen(value);
    931 			value = realloc(value, n + strlen(buf) + 1);
    932 			strcpy(value + n, buf);
    933 			n += strlen(buf);
    934 		}
    935 	}
    936 	test->fragment = value;
    937 	return true;
    938 }
    939 
    940 static void read_tests(size_t num_tests)
    941 {
    942 	while (read_test(tests + num_tests)) {
    943 		num_tests++;
    944 		tests = realloc(tests, (num_tests + 1) * sizeof(tests[0]));
    945 		tests[num_tests].name = NULL;
    946 	}
    947 }
    948 
    949 int main(int argc, const char *argv[])
    950 {
    951 	char *cmd;
    952 	unsigned int i;
    953 	const char *default_args[]
    954 		= { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
    955 	const char *outflag = DEFAULT_OUTPUT_EXE_FLAG;
    956 	const char *configurator_cc = NULL;
    957 	const char *wrapper = "";
    958 	const char *orig_cc;
    959 	const char *varfile = NULL;
    960 	const char *headerfile = NULL;
    961 	bool extra_tests = false;
    962 	FILE *outf;
    963 
    964 	if (argc > 0)
    965 		progname = argv[0];
    966 
    967 	while (argc > 1) {
    968 		if (strcmp(argv[1], "--help") == 0) {
    969 			printf("Usage: configurator [-v] [--var-file=<filename>] [-O<outflag>] [--configurator-cc=<compiler-for-tests>] [--wrapper=<wrapper-for-tests>] [--autotools-style] [--extra-tests] [<compiler> <flags>...]\n"
    970 			       "  <compiler> <flags> will have \"<outflag> <outfile> <infile.c>\" appended\n"
    971 			       "Default: %s %s %s\n",
    972 			       DEFAULT_COMPILER, DEFAULT_FLAGS,
    973 			       DEFAULT_OUTPUT_EXE_FLAG);
    974 			exit(0);
    975 		}
    976 		if (strncmp(argv[1], "-O", 2) == 0) {
    977 			argc--;
    978 			argv++;
    979 			outflag = argv[1] + 2;
    980 			if (!*outflag) {
    981 				fprintf(stderr,
    982 					"%s: option requires an argument -- O\n",
    983 					argv[0]);
    984 				exit(EXIT_BAD_USAGE);
    985 			}
    986 		} else if (strcmp(argv[1], "-v") == 0) {
    987 			argc--;
    988 			argv++;
    989 			verbose++;
    990 		} else if (strcmp(argv[1], "-vv") == 0) {
    991 			argc--;
    992 			argv++;
    993 			verbose += 2;
    994 		} else if (strncmp(argv[1], "--configurator-cc=", 18) == 0) {
    995 			configurator_cc = argv[1] + 18;
    996 			argc--;
    997 			argv++;
    998 		} else if (strncmp(argv[1], "--wrapper=", 10) == 0) {
    999 			wrapper = argv[1] + 10;
   1000 			argc--;
   1001 			argv++;
   1002 		} else if (strncmp(argv[1], "--var-file=", 11) == 0) {
   1003 			varfile = argv[1] + 11;
   1004 			argc--;
   1005 			argv++;
   1006 		} else if (strcmp(argv[1], "--autotools-style") == 0) {
   1007 			like_a_libtool = true;
   1008 			argc--;
   1009 			argv++;
   1010 		} else if (strncmp(argv[1], "--header-file=", 14) == 0) {
   1011 			headerfile = argv[1] + 14;
   1012 			argc--;
   1013 			argv++;
   1014 		} else if (strcmp(argv[1], "--extra-tests") == 0) {
   1015 			extra_tests = true;
   1016 			argc--;
   1017 			argv++;
   1018 		} else if (strcmp(argv[1], "--") == 0) {
   1019 			break;
   1020 		} else if (argv[1][0] == '-') {
   1021 			c12r_errx(EXIT_BAD_USAGE, "Unknown option %s", argv[1]);
   1022 		} else {
   1023 			break;
   1024 		}
   1025 	}
   1026 
   1027 	if (argc == 1)
   1028 		argv = default_args;
   1029 
   1030 	/* Copy with NULL entry at end */
   1031 	tests = calloc(sizeof(base_tests)/sizeof(base_tests[0]) + 1,
   1032 		       sizeof(base_tests[0]));
   1033 	memcpy(tests, base_tests, sizeof(base_tests));
   1034 
   1035 	if (extra_tests)
   1036 		read_tests(sizeof(base_tests)/sizeof(base_tests[0]));
   1037 
   1038 	orig_cc = argv[1];
   1039 	if (configurator_cc)
   1040 		argv[1] = configurator_cc;
   1041 
   1042 	cmd = connect_args(argv, outflag, OUTPUT_FILE " " INPUT_FILE);
   1043 	if (like_a_libtool) {
   1044 		start_test("Making autoconf users comfortable", "");
   1045 		sleep(1);
   1046 		end_test(1);
   1047 	}
   1048 	for (i = 0; tests[i].name; i++)
   1049 		run_test(cmd, wrapper, &tests[i]);
   1050 	free(cmd);
   1051 
   1052 	remove(OUTPUT_FILE);
   1053 	remove(INPUT_FILE);
   1054 
   1055 	if (varfile) {
   1056 		FILE *vars;
   1057 
   1058 		if (strcmp(varfile, "-") == 0)
   1059 			vars = stdout;
   1060 		else {
   1061 			start_test("Writing variables to ", varfile);
   1062 			vars = fopen(varfile, "a");
   1063 			if (!vars)
   1064 				c12r_err(EXIT_TROUBLE_RUNNING,
   1065 					 "Could not open %s", varfile);
   1066 		}
   1067 		for (i = 0; tests[i].name; i++)
   1068 			fprintf(vars, "%s=%u\n", tests[i].name, tests[i].answer);
   1069 		if (vars != stdout) {
   1070 			if (fclose(vars) != 0)
   1071 				c12r_err(EXIT_TROUBLE_RUNNING,
   1072 					 "Closing %s", varfile);
   1073 			end_test(1);
   1074 		}
   1075 	}
   1076 
   1077 	if (headerfile) {
   1078 		start_test("Writing header to ", headerfile);
   1079 		outf = fopen(headerfile, "w");
   1080 		if (!outf)
   1081 			c12r_err(EXIT_TROUBLE_RUNNING,
   1082 				 "Could not open %s", headerfile);
   1083 	} else
   1084 		outf = stdout;
   1085 
   1086 	fprintf(outf, "/* Generated by CCAN configurator */\n"
   1087 	       "#ifndef CCAN_CONFIG_H\n"
   1088 	       "#define CCAN_CONFIG_H\n");
   1089 	fprintf(outf, "#ifndef _GNU_SOURCE\n");
   1090 	fprintf(outf, "#define _GNU_SOURCE /* Always use GNU extensions. */\n");
   1091 	fprintf(outf, "#endif\n");
   1092 	fprintf(outf, "#define CCAN_COMPILER \"%s\"\n", orig_cc);
   1093 	cmd = connect_args(argv + 1, "", "");
   1094 	fprintf(outf, "#define CCAN_CFLAGS \"%s\"\n", cmd);
   1095 	free(cmd);
   1096 	fprintf(outf, "#define CCAN_OUTPUT_EXE_CFLAG \"%s\"\n\n", outflag);
   1097 	/* This one implies "#include <ccan/..." works, eg. for tdb2.h */
   1098 	fprintf(outf, "#define HAVE_CCAN 1\n");
   1099 	for (i = 0; tests[i].name; i++)
   1100 		fprintf(outf, "#define %s %u\n", tests[i].name, tests[i].answer);
   1101 	fprintf(outf, "#endif /* CCAN_CONFIG_H */\n");
   1102 
   1103 	if (headerfile) {
   1104 		if (fclose(outf) != 0)
   1105 			c12r_err(EXIT_TROUBLE_RUNNING, "Closing %s", headerfile);
   1106 		end_test(1);
   1107 	}
   1108 
   1109 	return 0;
   1110 }