diff options
| author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2024-11-23 11:47:28 -0500 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-23 11:47:28 -0500 | 
| commit | bdadebfceffad22179b33948113b2bf82f02c1f7 (patch) | |
| tree | 092394ab67595b7350cbcfb5a712f09ca45f6598 /src | |
| parent | b3facf587175307ef32547d0264bf096d2fcd283 (diff) | |
| parent | daa02148a63ee9e5c43373ce697dbada454440f6 (diff) | |
| download | stasis-bdadebfceffad22179b33948113b2bf82f02c1f7.tar.gz | |
Merge pull request #72 from jhunkeler/unindent-scripts
Add basic unindent function
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/core/delivery_test.c | 4 | ||||
| -rw-r--r-- | src/lib/core/str.c | 25 | 
2 files changed, 29 insertions, 0 deletions
| diff --git a/src/lib/core/delivery_test.c b/src/lib/core/delivery_test.c index 0bcf04d..e80e0ec 100644 --- a/src/lib/core/delivery_test.c +++ b/src/lib/core/delivery_test.c @@ -118,6 +118,9 @@ void delivery_tests_run(struct Delivery *ctx) {                      SYSERROR("An error occurred while rendering the following:\n%s", cmd);                      exit(1);                  } +                // Move indents +                // HEREDOCs will not work otherwise +                unindent(cmd);                  if (test->disable) {                      msg(STASIS_MSG_L2, "Script execution disabled by configuration\n", test->name); @@ -189,6 +192,7 @@ void delivery_tests_run(struct Delivery *ctx) {                          SYSERROR("An error occurred while rendering the following:\n%s", cmd);                          exit(1);                      } +                    unindent(cmd);                      struct MultiProcessingTask *task = NULL;                      char *runner_cmd = NULL; diff --git a/src/lib/core/str.c b/src/lib/core/str.c index a7dbab1..45fb60a 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -649,3 +649,28 @@ char *to_short_version(const char *s) {      strchrdel(result, ".");      return result;  } + +void unindent(char *s) { +    char *pos = NULL; +    size_t leading_spaces; + +    // Set position to beginning of string +    pos = s; + +    while (pos != NULL) { +        const size_t len = strlen(s); +        for (leading_spaces = 0; isspace(pos[leading_spaces]); leading_spaces++) {} + +        // For each new line strip an indent +        if (leading_spaces >= 4 && len >= 4) { +            leading_spaces = 4;  // remove first level of indentation +            memmove(pos, pos + leading_spaces, len - leading_spaces); +            pos[len - leading_spaces] = '\0'; +        } + +        pos = strchr(pos, '\n'); +        if (pos && strlen(pos)) { +            pos++; +        } +    } +}
\ No newline at end of file | 
