From 590bdda8be9cacf125bb44938f14a013414f4f48 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 30 Mar 2025 18:32:31 +0200 Subject: [PATCH] store script before strating --- morep_valid.c | 67 +++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/morep_valid.c b/morep_valid.c index 25ae870..d10f84b 100644 --- a/morep_valid.c +++ b/morep_valid.c @@ -157,10 +157,10 @@ uint8_t *parse_line (char *line, uint8_t *msgtype, int *plen) char *delim = " =\t"; char *str = strdup (line); char *save_ptr; - char *pt = str; + char *ptr = str; while (1) { - char *var = strtok_r (pt, delim, &save_ptr); - pt = NULL; + char *var = strtok_r (ptr, delim, &save_ptr); + ptr = NULL; if (!var) break; char *val = strtok_r (NULL, delim, &save_ptr); if (!var) break; @@ -230,12 +230,12 @@ int main (int argc, char **argv) comm_t comm_list[MAXCOMMS] = {0}; /* get basename */ - char *pt = progname = argv[0]; - while (*pt) { - if ((*pt == '/') || (*pt == '\\')) { - progname = pt + 1; + char *ptr = progname = argv[0]; + while (*ptr) { + if ((*ptr == '/') || (*ptr == '\\')) { + progname = ptr + 1; } - pt++; + ptr++; } /* process argument */ @@ -334,6 +334,14 @@ int main (int argc, char **argv) return -1; } } + char *script = read_stream (fid, NULL); + if (fid != stdin) { + fclose (fid); + } + if (script == NULL) { + VERBOSE (morep, ERROR, PRINTF ("no script read\n")); + return -1; + } /* signals */ signal(SIGINT, sig_handler); @@ -341,38 +349,36 @@ int main (int argc, char **argv) /* main loop */ int rc = 0; - while (!feof (fid)) { + ptr = script; + while (*ptr != '\0') { /* read line */ - char line[BUFMAX] = {0}; - int i = 0; + char *line = ptr; do { - fread (line + i, 1, 1, fid); - if ((line[i] == '\n') || (line[i] == '\r')) { - line[i] = 0; + if ((*ptr == '\n') || (*ptr == '\r')) { + *ptr = 0; } - } while ((line[i] != 0) && (++i < (int)sizeof (line) - 1)); + } while (*ptr++ != '\0'); /* clean line */ - char *pt = line; - while ((*pt == ' ') || (*pt == '\t')) { - if (*pt == '\0') { + while ((*line == ' ') || (*line == '\t')) { + if (*line == '\0') { break; } - pt++; + line++; } - if ((*pt == '\0') || (*pt == '#')) { + if ((*line == '\0') || (*line == '#')) { continue; } /* analyse line */ mode = -1; - if (*pt == 'R') { + if (*line == 'R') { mode = 0; - } else if (*pt == 'T') { + } else if (*line == 'T') { mode = 1; - } else if (strncmp (pt, "SLEEP", 5) == 0) { - int duration = atoi (pt + 5); + } else if (strncmp (line, "SLEEP", 5) == 0) { + int duration = atoi (line + 5); VERBOSE (morep, INFO, PRINTF ("sleep %dms\n", duration)); usleep (duration * 1000); continue; @@ -383,14 +389,15 @@ int main (int argc, char **argv) } /* find ethertype */ - pt++; comm_t *comm = NULL; + int offset = 1; + int i; for (i = 0; i < nbcomms; i++) { comm_t *c = comm_list + i; VERBOSE (morep, TRACE, PRINTF ("test %c[%s]\n", c->mode ? 'T' : 'R', c->etype)); - if ((strncmp (pt, c->etype, strlen (c->etype)) == 0) && (c->mode == mode)) { + if ((strncmp (line + offset, c->etype, strlen (c->etype)) == 0) && (c->mode == mode)) { comm = c; - pt += strlen (c->etype); + offset += strlen (c->etype); break; } } @@ -403,7 +410,7 @@ int main (int argc, char **argv) /* get values */ uint8_t msgtype = 0; int len = 0; - uint8_t *payload = parse_line (pt, &msgtype, &len); + uint8_t *payload = parse_line (line + offset, &msgtype, &len); if (len < 0) { VERBOSE (morep, WARNING, PRINTF ("can't parse line '%s'\n", line)); continue; @@ -440,9 +447,7 @@ int main (int argc, char **argv) } /* cleaning */ - if (fid != stdin) { - fclose (fid); - } + free (script); while (nbcomms) { MOREP_Close (--nbcomms); } -- 2.30.2