diff options
Diffstat (limited to 'rf.c')
| -rw-r--r-- | rf.c | 82 |
1 files changed, 60 insertions, 22 deletions
@@ -16,7 +16,7 @@ #define RUNFILE_VER_MAJ 1 #define RUNFILE_VER_MIN 3 -#define RUNFILE_VER_FIX 0 +#define RUNFILE_VER_FIX 4 /* *> rfile version system @@ -38,7 +38,11 @@ #include <wordexp.h> #include <sys/wait.h> -#define MAX_VARS 128 +#if defined(OVERRIDE_MAX_VARS) +#define MAX_VARS OVERRIDE_MAX_VARS +#else +#define MAX_VARS 512 +#endif typedef struct { char *key; @@ -50,7 +54,8 @@ size_t var_count = 0; void usage(); void printBox(char *str); -char *expand_vars(char *input); +char *expandVars(char *input); +char *trimComment(char *s); int main(int argc, char **argv) { @@ -80,22 +85,17 @@ int main(int argc, char **argv) while (getline(&line, &len, f) != -1) { line[strcspn(line, "\n")] = '\0'; char *cmd = line; + bool empty = true; while (isspace(*cmd)) cmd++; if (*cmd == '\0') continue; - else if (cmd[0] == '#') /* TODO: strchr() up to # and trim cmd to only skip - the commented-out part to allow in-line comments */ - continue; - #if defined(RF_EXTRAS) || defined(RF_EXTRA_MORE_COMMENTS) - else if (strncmp(cmd, "// ", 3) == 0) - continue; - else if (strncmp(cmd, "-- ", 3) == 0) - continue; - #endif - else if (strcmp(cmd, "}") == 0 || strcmp(line, "}") == 0) { + + trimComment(cmd); + + if (strcmp(cmd, "}") == 0 || strcmp(line, "}") == 0) { in_section = false; free(section); @@ -111,9 +111,13 @@ int main(int argc, char **argv) char *key = cmd + 1; //skip '@' char *val = eq + 1; - vars[var_count].key = strdup(key); - vars[var_count].val = strdup(val); - var_count++; + if (var_count < MAX_VARS) { + vars[var_count].key = strdup(key); + vars[var_count].val = strdup(val); + var_count++; + } else { + printf("error: maximum variable number reached (%d)\n", MAX_VARS); + } continue; } @@ -144,7 +148,7 @@ int main(int argc, char **argv) continue; if (strncmp(cmd, "@: ", 3) == 0) { - printf("\n%s\n", cmd); + printf("%s\n", cmd); continue; } else if (strncmp(cmd, "@:! ", 4) == 0) { @@ -152,7 +156,18 @@ int main(int argc, char **argv) continue; } - char *expanded = expand_vars(cmd); + char *expanded = expandVars(cmd); + + for (size_t i = 0; expanded[i]; i++) { + if (!isspace((unsigned char)expanded[i])) { + empty = false; + break; + } + } + + if (empty) { + continue; + } #if !defined(RF_HIDDEN_CMD) printf("$ %s\n", expanded); @@ -160,6 +175,7 @@ int main(int argc, char **argv) if (wordexp(expanded, &p, 0) != 0) { fprintf(stderr, "wordexp failed\n"); + wordfree(&p); continue; } @@ -191,7 +207,7 @@ void usage() ); } -char *expand_vars(char *input) +char *expandVars(char *input) { static char buf[8192]; size_t bi = 0; @@ -248,12 +264,34 @@ void printBox(char *str) printf("┌"); for (size_t i = 0; i < strlen(str) + 2; ++i) printf("─"); - printf("┐\n"); - printf("│ %s │\n", str); + printf("┐\n│ %s │\n└", str); - printf("└"); for (size_t i = 0; i < strlen(str) + 2; ++i) printf("─"); printf("┘\n"); } + +char *trimComment(char *s) +{ + char *c1 = strchr(s, '#'); + #if defined(RF_EXTRAS) || defined(RF_EXTRA_MORE_COMMENTS) + char *c2 = strstr(s, "//"); + char *c3 = strstr(s, "--"); + #endif + + char *cut = NULL; + + if (c1) cut = c1; + + #if defined(RF_EXTRAS) || defined(RF_EXTRA_MORE_COMMENTS) + if (c2 && (!cut || c2 < cut)) cut = c2; + if (c3 && (!cut || c3 < cut)) cut = c3; + #endif + + if (cut) + *cut = '\0'; + + return s; +} + |