| 82 | } |
| 83 | |
| 84 | static void parse_include(const char *filename, bool must_exist, bool early, |
| 85 | size_t depth) |
| 86 | { |
| 87 | char *contents, **lines; |
| 88 | char **all_args; /*For each line: either `--`argument, include file, or NULL*/ |
| 89 | char *argv[3]; |
| 90 | int i, argc; |
| 91 | |
| 92 | contents = grab_file(NULL, filename); |
| 93 | |
| 94 | /* The default config doesn't have to exist, but if the config was |
| 95 | * specified on the command line it has to exist. */ |
| 96 | if (!contents) { |
| 97 | if (must_exist) |
| 98 | err(1, "Opening and reading %s", filename); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | lines = tal_strsplit(contents, contents, "\r\n", STR_EMPTY_OK); |
| 103 | |
| 104 | /* We have to keep all_args around, since opt will point into it: use |
| 105 | * magic tal name to tell memleak this isn't one. */ |
| 106 | all_args = tal_arr_label(options_ctx, char *, tal_count(lines) - 1, |
| 107 | TAL_LABEL(options_array_notleak, "")); |
| 108 | |
| 109 | for (i = 0; i < tal_count(lines) - 1; i++) { |
| 110 | if (strstarts(lines[i], "#")) { |
| 111 | all_args[i] = NULL; |
| 112 | } else if (strstarts(lines[i], "include ")) { |
| 113 | /* If relative, it's relative to current config file */ |
| 114 | all_args[i] = path_join(all_args, |
| 115 | take(path_dirname(NULL, |
| 116 | filename)), |
| 117 | lines[i] + strlen("include ")); |
| 118 | } else { |
| 119 | /* Only valid forms are "foo" and "foo=bar" */ |
| 120 | all_args[i] = tal_fmt(all_args, "--%s", lines[i]); |
| 121 | } |
| 122 | /* This isn't a leak either */ |
| 123 | if (all_args[i]) |
| 124 | tal_set_name(all_args[i], TAL_LABEL(config_notleak, "")); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | For each line we construct a fake argc,argv commandline. |
| 129 | argv[1] is the only element that changes between iterations. |
| 130 | */ |
| 131 | argc = 2; |
| 132 | argv[0] = cast_const(char *, filename); |
| 133 | argv[argc] = NULL; |
| 134 | |
| 135 | for (i = 0; i < tal_count(all_args); i++) { |
| 136 | if (all_args[i] == NULL) |
| 137 | continue; |
| 138 | |
| 139 | if (!strstarts(all_args[i], "--")) { |
| 140 | /* There could be more, but this gives a hint. */ |
| 141 | if (depth > 100) |
no test coverage detected