6 * foreach - macro for simple iteration of arrays
8 * The foreach_int and foreach_ptr macros allow simple iteration of
9 * static arrays. This is very efficient with good compiler support
10 * (ie. gcc), and not too bad (ie. a few compares and mallocs) for
13 * License: LGPL (3 or any later version)
14 * Author: Rusty Russell <rusty@rustcorp.com.au>
17 * // Figure out how to get usage: message out of a program.
18 * #include <ccan/foreach/foreach.h>
24 * // Returns true if program outputs "usage:"
25 * static bool try_usage(const char *prog, const char *arg)
31 * command = malloc(strlen(prog) + 1 + strlen(arg) + 1 +
32 * sizeof("</dev/null 2>&1 | grep -qiw usage:"));
33 * sprintf(command, "%s %s </dev/null 2>&1 | grep -qiw usage:",
35 * in = popen(command, "r");
37 * err(2, "running '%s'", command);
38 * status = pclose(in);
40 * return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
43 * int main(int argc, char *argv[])
48 * errx(1, "Usage: %s <progname>\n"
49 * "Figures out how to get usage message", argv[0]);
50 * foreach_ptr(arg, "--help", "-h", "--usage", "-?", "") {
51 * if (try_usage(argv[1], arg)) {
52 * printf("%s %s\n", argv[1], arg);
56 * printf("%s is unhelpful\n", argv[1]);
60 int main(int argc, char *argv[])
65 if (strcmp(argv[1], "depends") == 0) {
66 #if !HAVE_COMPOUND_LITERALS || !HAVE_FOR_LOOP_DECLARATION
67 printf("ccan/list\n");