#include "config.h" #include #include /** * foreach - macro for simple iteration of arrays * * The foreach_int and foreach_ptr macros allow simple iteration of * static arrays. This is very efficient with good compiler support * (ie. gcc), and not too bad (ie. a few compares and mallocs) for * other compilers. * * License: LGPL (v3 or any later version) * Author: Rusty Russell * * Example: * // Figure out how to get usage: message out of a program. * #include * #include * #include * #include * #include * * // Returns true if program outputs "usage:" * static bool try_usage(const char *prog, const char *arg) * { * char *command; * FILE *in; * int status; * * command = malloc(strlen(prog) + 1 + strlen(arg) + 1 + * sizeof("&1 | grep -qiw usage:")); * sprintf(command, "%s %s &1 | grep -qiw usage:", * prog, arg); * in = popen(command, "r"); * if (!in) * err(2, "running '%s'", command); * status = pclose(in); * free(command); * return (WIFEXITED(status) && WEXITSTATUS(status) == 0); * } * * int main(int argc, char *argv[]) * { * const char *arg; * * if (argc != 2) * errx(1, "Usage: %s \n" * "Figures out how to get usage message", argv[0]); * foreach_ptr(arg, "--help", "-h", "--usage", "-?", "") { * if (try_usage(argv[1], arg)) { * printf("%s %s\n", argv[1], arg); * exit(0); * } * } * printf("%s is unhelpful\n", argv[1]); * exit(1); * } */ int main(int argc, char *argv[]) { if (argc != 2) return 1; if (strcmp(argv[1], "depends") == 0) { #if !HAVE_COMPOUND_LITERALS || !HAVE_FOR_LOOP_DECLARATION printf("ccan/list\n"); #endif return 0; } return 1; }