+static void start_main(char **ret, const char *why)
+{
+ tal_append_fmt(ret,
+ "/* The example %s, so fake function wrapper inserted */\n"
+ "int main(int argc, char *argv[])\n"
+ "{\n", why);
+}
+
+/* We only handle simple function definitions here. */
+static char *add_func(const tal_t *ctx, char *others, const char *line)
+{
+ const char *p, *end = strchr(line, '(') - 1;
+ char *use;
+
+ while (cisspace(*end)) {
+ end--;
+ if (end == line)
+ return others;
+ }
+
+ for (p = end; cisalnum(*p) || *p == '_'; p--) {
+ if (p == line)
+ return others;
+ }
+
+ use = tal_fmt(ctx, "printf(\"%%p\", %.*s);\n",
+ (unsigned)(end - p), p+1);
+ if (others)
+ use = tal_strcat(ctx, take(others), take(use));
+
+ return use;
+}
+
+static void strip_leading_whitespace(char **lines)
+{
+ unsigned int i, min_span = -1U;
+
+ for (i = 0; lines[i]; i++) {
+ unsigned int span = strspn(lines[i], " \t");
+ /* All whitespace? Ignore */
+ if (!lines[i][span])
+ continue;
+ if (span < min_span)
+ min_span = span;
+ }
+
+ for (i = 0; lines[i]; i++)
+ if (strlen(lines[i]) >= min_span)
+ lines[i] += min_span;
+}
+
+static bool looks_internal(char **lines, char **why)
+{
+ unsigned int i;
+ bool last_ended = true; /* Did last line finish a statement? */
+
+ 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;
+ }