+ for (i = 0; lines[i]; i++) {
+ /* Skip leading whitespace. */
+ const char *line = lines[i] + strspn(lines[i], " \t");
+ unsigned len = strspn(line, IDENT_CHARS);
+
+ if (!line[0] || cisspace(line[0]) || strstarts(line, "//")
+ || strstarts(line, "#line"))
+ continue;
+
+ assert(line[strlen(line)-1] != '\n');
+
+ /* The winners. */
+ if (strstarts(line, "if") && len == 2) {
+ *why = cast_const(char *, "starts with if");
+ return true;
+ }
+ if (strstarts(line, "for") && len == 3) {
+ *why = cast_const(char *, "starts with for");
+ return true;
+ }
+ if (strstarts(line, "while") && len == 5) {
+ *why = cast_const(char *, "starts with while");
+ return true;
+ }
+ if (strstarts(line, "do") && len == 2) {
+ *why = cast_const(char *, "starts with do");
+ return true;
+ }
+
+ /* The losers. */
+ if (strstarts(line, "#include")) {
+ *why = cast_const(char *, "starts with #include");
+ return false;
+ }
+
+ if (last_ended && strchr(line, '(')) {
+ if (strstarts(line, "static")) {
+ *why = cast_const(char *,
+ "starts with static"
+ " and contains (");
+ return false;
+ }
+ if (strends(line, ")")) {
+ *why = cast_const(char *,
+ "contains ( and ends with )");
+ return false;
+ }
+ }
+
+ /* Previously prepended. */
+ if (strstr(line, "didn't seem to belong inside a function")) {
+ *why = cast_const(char *, "Comment said so");
+ return false;
+ }
+
+ /* Single identifier then operator == inside function. */
+ if (last_ended && len
+ && cispunct(line[len+strspn(line+len, " ")])) {
+ *why = cast_const(char *, "starts with identifier"
+ " then punctuation");
+ return true;
+ }
+
+ last_ended = (strends(line, "}")
+ || strends(line, ";")
+ || strends(line, "*/")
+ || streq(line, "..."));
+ }
+
+ /* No idea... Say yes? */
+ *why = cast_const(char *, "gave no clues");
+ return true;
+}
+
+/* Examples will often build on prior ones. Try combining them. */
+static char **combine(const void *ctx, char **lines, char **prev)
+{
+ unsigned int i, lines_total, prev_total, count;
+ char **ret;
+ const char *reasoning;
+ char *why = NULL;
+
+ if (!prev)
+ return NULL;
+
+ /* If it looks internal, put prev at start. */
+ if (looks_internal(lines, &why)) {
+ count = 0;
+ reasoning = "seemed to belong inside a function";
+ } else {
+ /* Try inserting in first elided position */
+ for (count = 0; lines[count]; count++) {
+ if (strcmp(lines[count], "...") == 0)
+ break;
+ }
+ if (!lines[count]) {
+ /* Try at start anyway? */
+ count = 0;
+ reasoning = "didn't seem to belong inside"
+ " a function, so we prepended the previous"
+ " example";
+ } else {
+ reasoning = "didn't seem to belong inside"
+ " a function, so we put the previous example"
+ " at the first ...";
+
+ count++;
+ }
+ }
+
+ for (i = 0; lines[i]; i++);
+ lines_total = i;
+
+ for (i = 0; prev[i]; i++);
+ prev_total = i;
+
+ ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
+ ret[0] = talloc_asprintf(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 = talloc_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;
+ unsigned int i;
+
+ ret = talloc_asprintf(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 = talloc_asprintf_append(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(). */
+ ret = start_main(ret, why);