From 2de8ff052dd81d6eba4d8c408204ff1bf5661246 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 19 Apr 2023 11:29:51 +0200 Subject: [PATCH] src/net: factor out pipe identification and debug each piped command Signed-off-by: Joachim Wiberg --- src/net/src/main.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/net/src/main.c b/src/net/src/main.c index bae6d975..716f8dc8 100644 --- a/src/net/src/main.c +++ b/src/net/src/main.c @@ -103,6 +103,24 @@ static int save_rdeps(const char *path, char *gen) return systemf("sed '1!G;h;$!d' < %s/%s/deps >%s/%s/rdeps", path, gen, path, gen); } +static FILE *pipep(const char *action) +{ + char *ptr; + + ptr = strrchr(action, '.'); + if (!ptr) { + warnx("missing action type, assuming shell script: '%s'", action); + return NULL; + } + + if (!strcmp(ptr, ".ip")) + return ip; + if (!strcmp(ptr, ".bridge")) + return bridge; + + return NULL; +} + static int pipeit(FILE *pp, const char *action) { char line[256]; @@ -112,11 +130,10 @@ static int pipeit(FILE *pp, const char *action) if (!fp) return 0; /* nop */ - log("running %s ...", action); - while (fgets(line, sizeof(line), fp)) { - dbg("%s: read line: %s", action, line); - fputs(line, pp); + chomp(line); + dbg(">>> %s", line); + fprintf(pp, "%s\n", line); } return fclose(fp); @@ -124,22 +141,16 @@ static int pipeit(FILE *pp, const char *action) static int run(const char *action) { - char *ptr; + FILE *pp = pipep(action); int rc; - ptr = strrchr(action, '.'); - if (!ptr) { - warnx("invalid action script: '%s'", action); - return 0; + if (pp) { + log("running pipe action %s:", action); + return pipeit(pp, action); } - if (strcmp(action, ".ip")) - return pipeit(ip, action); - if (strcmp(action, ".bridge")) - return pipeit(bridge, action); - /* other actions are plain shell scripts (.sh) */ - log("running %s ...", action); + log("running shell action %s:", action); rc = systemf("%s", action); if (rc)