+ for (i = 0; lines[i]; i++);
+ lines_total = i;
+
+ for (i = 0; prev[i]; i++);
+ prev_total = i;
+
+ ret = tal_arr(ctx, char *, 1 + lines_total + prev_total + 1);
+ ret[0] = tal_fmt(ret, "/* The example %s, thus %s */",
+ why, reasoning);
+ memcpy(ret+1, lines, count * sizeof(ret[0]));
+ memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
+ memcpy(ret+1 + count + prev_total, lines + count,
+ (lines_total - count + 1) * sizeof(ret[0]));
+ return ret;
+}
+
+/* Only handles very simple comments. */
+static char *strip_comment(const void *ctx, const char *orig_line)
+{
+ char *p, *ret = tal_strdup(ctx, orig_line);
+
+ p = strstr(ret, "/*");
+ if (!p)
+ p = strstr(ret, "//");
+ if (p)
+ *p = '\0';
+ return ret;
+}
+
+static char *mangle(struct manifest *m, char **lines)
+{
+ char *ret, *use_funcs = NULL, *why;
+ bool in_function = false, fake_function = false, has_main = false,
+ fake_main = false;
+ unsigned int i;
+
+ ret = tal_fmt(m,
+ "/* Include header from module. */\n"
+ "#include <ccan/%s/%s.h>\n"
+ "/* Prepend a heap of headers. */\n"
+ "#include <assert.h>\n"
+ "#include <err.h>\n"
+ "#include <errno.h>\n"
+ "#include <fcntl.h>\n"
+ "#include <limits.h>\n"
+ "#include <stdbool.h>\n"
+ "#include <stdint.h>\n"
+ "#include <stdio.h>\n"
+ "#include <stdlib.h>\n"
+ "#include <string.h>\n"
+ "#include <sys/stat.h>\n"
+ "#include <sys/types.h>\n"
+ "#include <unistd.h>\n",
+ m->modname, m->basename);
+
+ ret = tal_strcat(m, take(ret), "/* Useful dummy functions. */\n"
+ "extern int somefunc(void);\n"
+ "int somefunc(void) { return 0; }\n"
+ "extern char somestring[];\n"
+ "char somestring[] = \"hello world\";\n");
+
+ if (looks_internal(lines, &why)) {
+ /* Wrap it all in main(). */
+ start_main(&ret, why);
+ fake_function = true;
+ in_function = true;
+ has_main = true;
+ fake_main = true;
+ } else
+ tal_append_fmt(&ret,
+ "/* The example %s, so didn't wrap in main() */\n",
+ why);
+