1 #include <ccan/talloc/talloc.h>
2 #include <ccan/str/str.h>
3 #include <ccan/grab_file/grab_file.h>
4 #include <ccan/str_talloc/str_talloc.h>
11 static char ** __attribute__((format(printf, 3, 4)))
12 lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
19 cmd = talloc_vasprintf(ctx, format, ap);
24 err(1, "Executing '%s'", cmd);
26 buffer = grab_fd(ctx, fileno(p), NULL);
28 err(1, "Reading from '%s'", cmd);
31 return strsplit(ctx, buffer, "\n", num);
34 static int unlink_info(char *infofile)
40 /* Be careful about trying to compile over running programs (parallel make) */
41 static char *compile_info(const void *ctx, const char *dir, const char *name)
43 char *infofile = talloc_asprintf(ctx, "%s/info.%u", dir, getpid());
44 char *cmd = talloc_asprintf(ctx, "cc " CFLAGS
45 " -o %s -x c %s/%s/_info",
47 talloc_set_destructor(infofile, unlink_info);
54 static char **get_one_deps(const void *ctx, const char *dir,
55 const char *name, unsigned int *num)
57 char **deps, *cmd, *infofile;
59 infofile = compile_info(ctx, dir, name);
61 errx(1, "Could not compile _info for '%s'", name);
63 cmd = talloc_asprintf(ctx, "%s depends", infofile);
64 deps = lines_from_cmd(cmd, num, "%s", cmd);
66 err(1, "Could not run '%s'", cmd);
70 /* Make copy of src, replacing "from" with "to". */
71 static char *replace(const void *ctx, const char *src,
72 const char *from, const char *to)
74 char *ret = talloc_strdup(ctx, "");
75 unsigned int rlen, len, add;
79 const char *next = strstr(src+len, from);
81 add = strlen(src+len) + 1;
83 add = next - (src+len);
85 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
86 memcpy(ret+rlen, src+len, add);
97 /* This is a terrible hack. We scan for ccan/ strings. */
98 static char **get_one_safe_deps(const void *ctx,
99 const char *dir, const char *name,
102 char **deps, **lines, *raw, *fname;
103 unsigned int i, n = 0;
105 fname = talloc_asprintf(ctx, "%s/%s/_info", dir, name);
106 raw = grab_file(fname, fname, NULL);
108 errx(1, "Could not open %s", fname);
110 /* Replace \n by actual line breaks, and split it. */
111 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
113 deps = talloc_array(ctx, char *, n+1);
115 for (n = i = 0; lines[i]; i++) {
119 /* Ignore lines starting with # (e.g. #include) */
120 if (lines[i][0] == '#')
123 /* Start of line, or after ". */
124 if (strstarts(lines[i], "ccan/"))
127 str = strstr(lines[i], "\"ccan/");
133 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
136 deps[n++] = talloc_strndup(deps, str, len);
145 static bool have_dep(char **deps, unsigned int num, const char *dep)
149 for (i = 0; i < num; i++)
150 if (streq(deps[i], dep))
155 /* Gets all the dependencies, recursively. */
157 get_all_deps(const void *ctx, const char *dir, const char *name,
158 char **(*get_one)(const void *, const char *, const char *,
164 deps = get_one(ctx, dir, name, &num);
165 for (i = 0; i < num; i++) {
167 unsigned int j, newnum;
169 if (!strstarts(deps[i], "ccan/"))
172 newdeps = get_one(ctx, dir, deps[i] + strlen("ccan/"),
175 /* Should be short, so brute-force out dups. */
176 for (j = 0; j < newnum; j++) {
177 if (have_dep(deps, num, newdeps[j]))
180 deps = talloc_realloc(NULL, deps, char *, num + 2);
181 deps[num++] = newdeps[j];
188 char **get_deps(const void *ctx, const char *dir, const char *name,
193 return get_one_deps(ctx, dir, name, &num);
195 return get_all_deps(ctx, dir, name, get_one_deps);
198 char **get_safe_ccan_deps(const void *ctx, const char *dir,
199 const char *name, bool recurse)
203 return get_one_safe_deps(ctx, dir, name, &num);
205 return get_all_deps(ctx, dir, name, get_one_safe_deps);
208 char *talloc_basename(const void *ctx, const char *dir)
210 char *p = strrchr(dir, '/');
214 return talloc_strdup(ctx, p+1);
217 char *talloc_dirname(const void *ctx, const char *dir)
219 char *p = strrchr(dir, '/');
222 return talloc_strdup(ctx, ".");
223 return talloc_strndup(ctx, dir, p - dir);
226 char *talloc_getcwd(const void *ctx)
231 /* *This* is why people hate C. */
233 cwd = talloc_array(ctx, char, len);
234 while (!getcwd(cwd, len)) {
235 if (errno != ERANGE) {
239 cwd = talloc_realloc(ctx, cwd, char, len *= 2);