From 07e2feac2bb15c8f35e819d7186e5543f1b53148 Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Sat, 24 Feb 2024 06:52:54 +0000 Subject: [PATCH 1/6] waaaaaaaaaaaaay better... trust me dude --- bake.c | 168 +++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 91 insertions(+), 77 deletions(-) diff --git a/bake.c b/bake.c index 60928f0..0116f2d 100644 --- a/bake.c +++ b/bake.c @@ -24,9 +24,6 @@ #include "config.h" -#define START "@BAKE" -#define STOP "@STOP" - #define HELP \ BOLD "[option] target-file" RESET " [" GREEN "arguments" RESET " ...]\n" \ "Use the format `" BOLD "@BAKE" RESET " cmd ...' within the target-file, this will execute the\n" \ @@ -40,6 +37,14 @@ "\t" YELLOW "$*" RESET " returns target-file without suffix (^-> abc.x)\n" \ "\t" YELLOW "$+" RESET " returns " GREEN "arguments" RESET "\n" +typedef struct { + size_t len; + char * buf; +} string_t; + +const static string_t START = { 5, "@BAKE" }; +const static string_t STOP = { 5, "@STOP" }; + /*** Utility functions ***/ static void @@ -50,20 +55,18 @@ swap(char * a, char * b) { } static char * -find(char * x, char * buf, char * end) { - size_t len = strlen(x); - for (; (buf < end) && len < (size_t)(end - buf); ++buf) { - if (!strncmp(buf, x, len)) +find(string_t x, char * buf, char * end) { + for (; (buf < end) && x.len < (size_t)(end - buf); ++buf) { + if (!strncmp(buf, x.buf, x.len)) { return buf; } } return NULL; } static char * -insert(char * new, char * str, size_t offset, size_t shift) { - size_t len, max; - len = strlen(new); - max = (strlen(str) + 1 - offset - shift); +insert(char * new, size_t len, char * str, size_t slen, size_t offset, size_t shift) { + size_t max; + max = (slen + 1 - offset - shift); memmove(str + offset + len, str + offset + shift, max); memcpy(str + offset, new, len); return str; @@ -71,7 +74,24 @@ insert(char * new, char * str, size_t offset, size_t shift) { /*** g_short, g_all Functions ***/ -static char * g_filename, * g_short, * g_all; +#define GLOBALS_COUNT 3 + +static char * globals[GLOBALS_COUNT]; + +#define g_filename globals[0] +#define g_short globals[1] +#define g_all globals[2] + +/* doing a ? strlen(a) : "" really bothered me and this could be optimized out */ +static size_t * +get_globals_length(void) { + static size_t len[GLOBALS_COUNT]; + size_t i; + for (i = 0; i < GLOBALS_COUNT; ++i) { + len[i] = strlen(globals[i]); + } + return len; +} static char * shorten(char * fn) { @@ -86,7 +106,7 @@ shorten(char * fn) { last = last ? last : i; strncpy(sh, fn, last); sh[last] = '\0'; - return sh; + return sh ? sh : calloc(1,1); } static char * @@ -108,7 +128,7 @@ all_args(size_t argc, char ** argv) { if (i + 1 < argc) { all[len - 1] = ' '; } } } - return all; + return all ? all : calloc(1,1); } /*** Map ***/ @@ -139,14 +159,14 @@ map(char * fn) { /*** Important Functions ***/ static char * -find_region(map_t m) { +find_region(map_t m, size_t * retlen) { extern char * strndup(const char * s, size_t n); /* for splint */ char * buf = NULL, * start, * stop; start = find(START, m.str, m.str + m.len); if (start) { - start += strlen(START); + start += START.len; #ifdef REQUIRE_SPACE if (!isspace(*start)) { @@ -167,7 +187,10 @@ find_region(map_t m) { } if (stop) - { buf = strndup(start, (size_t) (stop - m.str) - (start - m.str)); } + { + *retlen = (size_t) (stop - m.str) - (start - m.str); + buf = strndup(start, *retlen); + } } return buf; } @@ -192,63 +215,52 @@ root(char ** rootp) { return ret; } -static size_t -expand_size(char * buf, int argc, char ** argv) { - size_t i, len, max; - - len = max = strlen(buf) + 1; - - for (i = 0; i < len; ++i) { - if (buf[i] == '\\') { - i += 2; - continue; - } else if (buf[i] == '$') { - switch (buf[++i]) { - case '@': - max += strlen(g_filename); - break; - case '*': - if (!g_short) - { g_short = shorten(g_filename); } - max += g_short ? strlen(g_short) : 0; - break; - case '+': - if (!g_all) - { g_all = all_args((size_t) argc, argv); } - max += g_all ? strlen(g_all) : 0; - break; - } - } - } - return max; -} +#define ARRLEN(x) (sizeof(x) / sizeof(x[0])) static char * -expand(char * buf) { - size_t i; - char * ptr = NULL; +expand(char * buf, size_t len) { + enum { + MACRO_FILENAME = 0, + MACRO_SHORT = 1, + MACRO_ARGS = 2, + }; - for (i = 0; buf[i]; ++i) { - if (buf[i] == '\\') { - i += 2; - continue; - } else if (buf[i] == '$') { - switch (buf[++i]) { - case '@': - ptr = g_filename; - break; - case '*': - ptr = g_short; - break; - case '+': - ptr = g_all ? g_all : ""; - break; - default: continue; + char *macro[] = { + [MACRO_FILENAME] = "$@", + [MACRO_SHORT ] = "$*", + [MACRO_ARGS ] = "$+", + }; + + size_t macro_length[] = { + [MACRO_FILENAME] = 2, + [MACRO_SHORT ] = 2, + [MACRO_ARGS ] = 2, + }; + + size_t * globals_length = get_globals_length(); + + char * ptr; + size_t i, f; + + { + size_t max = len + 1; + for (i = 0; i < len; ++i) { + for (f = 0; f < ARRLEN(macro); ++f) { + if (!strncmp(buf + i, macro[f], macro_length[f])) { + max += globals_length[f]; + } + } + } + buf = realloc(buf, max + 10); + len = max - 1; + } + for (i = 0; i < len; ++i) { + for (f = 0; f < ARRLEN(macro); ++f) { + if (!strncmp(buf + i, macro[f], macro_length[f])) { + buf = insert(globals[f], globals_length[f], buf, len, i, 2); } - buf = insert(ptr, buf, i - 1, 2); } } - free(g_short); free(g_all); return buf; } @@ -280,6 +292,7 @@ int main(int argc, char ** argv) { int ret = 0; char * buf = NULL; + size_t len; if (argc < 2 || !strcmp(argv[1], "-h") @@ -296,24 +309,25 @@ main(int argc, char ** argv) { { map_t m = map(g_filename); if (m.str) { - buf = find_region(m); + buf = find_region(m, &len); munmap(m.str, m.len); } } if (!buf) { - if (errno) { fprintf(stderr, BOLD RED "%s" RESET ": %s\n", g_filename, strerror(errno)); } - else { fprintf(stderr, BOLD RED "%s" RESET ": File unrecognized.\n", g_filename); } + if (errno) + { fprintf(stderr, BOLD RED "%s" RESET ": %s\n", g_filename, strerror(errno)); } + else + { fprintf(stderr, BOLD RED "%s" RESET ": File unrecognized.\n", g_filename); } return 1; } + g_all = all_args((size_t) argc, argv); + g_short = shorten(g_filename); root(&g_filename); - { char * buf2 = buf; - buf = realloc(buf, expand_size(buf, argc, argv)); - if (!buf) - { free(buf2); free(g_short); free(g_all); return 1; } - } - buf = expand(buf); + buf = expand(buf, len); + free(g_short); free(g_all); + if (!buf) { return 1; } fprintf(stderr, GREEN "%s" RESET ": %s\n", argv[0], buf + strip(buf)); ret = ret ? 0 : run(buf); From ad135f75004eeb5bc7e197e1df97a5434f9b3391 Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Mon, 26 Feb 2024 23:34:36 +0000 Subject: [PATCH 2/6] git requires a message --- bake.c | 276 ++++++++++++++++++++++++++++++++++----------------------------- config.h | 12 +-- 2 files changed, 157 insertions(+), 131 deletions(-) diff --git a/bake.c b/bake.c index 0116f2d..54ebd6b 100644 --- a/bake.c +++ b/bake.c @@ -3,7 +3,7 @@ * * Licensed under the GNU Public License version 3 only, see LICENSE. * - * @BAKE cc -std=c89 -O2 -I. $@ -o $* $+ # @STOP + * @BAKE cc -std=c99 -O2 $@ -o $* $+ # @STOP */ #define _POSIX_C_SOURCE 200809L @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "config.h" @@ -42,11 +43,16 @@ typedef struct { char * buf; } string_t; -const static string_t START = { 5, "@BAKE" }; -const static string_t STOP = { 5, "@STOP" }; +typedef string_t map_t; + +static string_t START = { 5, "@BAKE" }; +static string_t STOP = { 5, "@STOP" }; /*** Utility functions ***/ +#define strneq(a,b,n) (!strncmp(a,b,n)) +#define streq(a,b) (!strcmp(a,b)) + static void swap(char * a, char * b) { *a ^= *b; @@ -63,80 +69,87 @@ find(string_t x, char * buf, char * end) { return NULL; } -static char * -insert(char * new, size_t len, char * str, size_t slen, size_t offset, size_t shift) { - size_t max; - max = (slen + 1 - offset - shift); - memmove(str + offset + len, str + offset + shift, max); - memcpy(str + offset, new, len); - return str; +static void +insert(char * str, size_t slen, char * new, size_t len, size_t shift) { + memmove(str + len, str + shift, slen - len - shift); + memcpy(str, new, len); } /*** g_short, g_all Functions ***/ #define GLOBALS_COUNT 3 -static char * globals[GLOBALS_COUNT]; +static string_t globals[GLOBALS_COUNT]; #define g_filename globals[0] #define g_short globals[1] #define g_all globals[2] -/* doing a ? strlen(a) : "" really bothered me and this could be optimized out */ -static size_t * -get_globals_length(void) { - static size_t len[GLOBALS_COUNT]; - size_t i; - for (i = 0; i < GLOBALS_COUNT; ++i) { - len[i] = strlen(globals[i]); +static string_t +shorten(string_t s) { + size_t i, last = 0, len = s.len; + char * sh, * fn = s.buf; + sh = malloc(len); + if (sh) { + for (i = 0; i < len; ++i) { + if (fn[i] == '.') { last = i; } + } + last = last ? last : i; + strncpy(sh, fn, last); + sh[last] = '\0'; } - return len; + return (string_t) { last, sh ? sh : calloc(0,0) }; } -static char * -shorten(char * fn) { - size_t i, last = 0, len; - char * sh; - len = strlen(fn); - sh = malloc(len + 1); - if (!sh) { return NULL; } - for (i = 0; i < len; ++i) { - if (fn[i] == '.') { last = i; } - } - last = last ? last : i; - strncpy(sh, fn, last); - sh[last] = '\0'; - return sh ? sh : calloc(1,1); -} - -static char * -all_args(size_t argc, char ** argv) { +static string_t +all_args(int argc, char ** argv) { char * all = NULL; + size_t len = 0; if (argc > 2) { - size_t i, len = argc; + int i; + len = (size_t) argc; for (i = 2; i < argc; ++i) { len += strlen(argv[i]); } - all = malloc(len + 1); - if (!all) { return NULL; } - - all[len] = '\0'; - for (len = 0, i = 2; i < argc; ++i) { - strcpy(all + len, argv[i]); - len += strlen(argv[i]) + 1; - if (i + 1 < argc) { all[len - 1] = ' '; } + all = malloc(len); + if (all) { + all[len - 1] = '\0'; + for (len = 0, i = 2; i < argc; ++i) { + strcpy(all + len, argv[i]); + len += strlen(argv[i]) + 1; + if (i + 1 < argc) { all[len - 1] = ' '; } + } } } - return all ? all : calloc(1,1); + return (string_t) { len, all ? all : calloc(0,0) }; } -/*** Map ***/ +#if 0 +static string_t +all_args(int argc, char ** argv) { + string_t s = (string_t) { 0, NULL }; + if (argc > 2) { + size_t i; + for (i = (size_t) argc; i > 2; --i) { + s.len += strlen(argv[i]) + 1; + } + s.buf = malloc(s.len + (size_t) argc); + if (s.buf) { + size_t len; + s.buf[s.len - 1] = '\0'; + for (len = 0, i = (size_t) argc; i > 2; --i) { + strcpy(s.buf + len, argv[i]); + len += strlen(argv[i]) + 1; + s.buf[len - 1] = ' '; + } + } + } + return s; +} +#endif -typedef struct { - char * str; - size_t len; -} map_t; +/*** Map ***/ static map_t map(char * fn) { @@ -149,7 +162,7 @@ map(char * fn) { && s.st_mode & S_IFREG && s.st_size) { m.len = (size_t) s.st_size; - m.str = (char *) mmap(NULL, m.len, PROT_READ, MAP_SHARED, fd, 0); + m.buf = (char *) mmap(NULL, m.len, PROT_READ, MAP_SHARED, fd, 0); } close(fd); } @@ -158,24 +171,25 @@ map(char * fn) { /*** Important Functions ***/ -static char * -find_region(map_t m, size_t * retlen) { +static string_t +find_region(map_t m, string_t startsym, string_t stopsym) { extern char * strndup(const char * s, size_t n); /* for splint */ char * buf = NULL, * start, * stop; + size_t len; - start = find(START, m.str, m.str + m.len); + start = find(startsym, m.buf, m.buf + m.len); if (start) { - start += START.len; + start += startsym.len; #ifdef REQUIRE_SPACE if (!isspace(*start)) { - fprintf(stderr, RED "%s" RESET ": Found start without suffix spacing.\n", g_filename); - return buf; + fprintf(stderr, RED "%s" RESET ": Found start without suffix spacing.\n", g_filename.buf); + return (string_t) { 0 , buf }; } #endif /* REQUIRE_SPACE */ - stop = find(STOP, start, start + m.len - (start - m.str)); + stop = find(stopsym, start, start + m.len - (start - m.buf)); if (!stop) { stop = start; @@ -188,18 +202,29 @@ find_region(map_t m, size_t * retlen) { if (stop) { - *retlen = (size_t) (stop - m.str) - (start - m.str); - buf = strndup(start, *retlen); + len = (size_t) (stop - m.buf) - (start - m.buf); + buf = strndup(start, len); } } - return buf; + return (string_t) { len, buf }; +} + +static string_t +file_find_region(char * fn, string_t start, string_t stop) { + string_t s = { 0, NULL }; + map_t m = map(fn); + if (m.buf) { + s = find_region(m, start, stop); + munmap(m.buf, m.len); + } + return s; } static int -root(char ** rootp) { +root(string_t s) { char x[1] = {'\0'}; - char * root = *rootp; - size_t len = strlen(root); + char * root = s.buf; + size_t len = s.len; int ret; while (len && root[len] != '/') @@ -211,130 +236,131 @@ root(char ** rootp) { ret = chdir(root); swap(root + len, x); - *rootp += len + 1; +/* *rootp += len + 1; */ return ret; } #define ARRLEN(x) (sizeof(x) / sizeof(x[0])) -static char * -expand(char * buf, size_t len) { +static string_t +expand(string_t s) { enum { MACRO_FILENAME = 0, MACRO_SHORT = 1, MACRO_ARGS = 2, }; - char *macro[] = { - [MACRO_FILENAME] = "$@", - [MACRO_SHORT ] = "$*", - [MACRO_ARGS ] = "$+", + string_t macro[] = { + [MACRO_FILENAME] = { 2, "$@" }, + [MACRO_SHORT ] = { 2, "$*" }, + [MACRO_ARGS ] = { 2, "$+" }, }; - size_t macro_length[] = { - [MACRO_FILENAME] = 2, - [MACRO_SHORT ] = 2, - [MACRO_ARGS ] = 2, - }; - - size_t * globals_length = get_globals_length(); - - char * ptr; size_t i, f; { - size_t max = len + 1; - for (i = 0; i < len; ++i) { + size_t max = s.len; + for (i = 0; i < s.len; ++i) { for (f = 0; f < ARRLEN(macro); ++f) { - if (!strncmp(buf + i, macro[f], macro_length[f])) { - max += globals_length[f]; + if (!strncmp(s.buf + i, macro[f].buf, macro[f].len)) { + max += globals[f].len; } } } - buf = realloc(buf, max + 10); - len = max - 1; + s.buf = realloc(s.buf, max + 27); /* I don't know, man */ + if (!s.buf) { return (string_t) { 0, NULL}; } + memset(s.buf + s.len, 0, max - s.len); + s.len = max; } - for (i = 0; i < len; ++i) { + for (i = 0; i < s.len; ++i) { for (f = 0; f < ARRLEN(macro); ++f) { - if (!strncmp(buf + i, macro[f], macro_length[f])) { - buf = insert(globals[f], globals_length[f], buf, len, i, 2); + if (!strncmp(s.buf + i, macro[f].buf, macro[f].len)) { + insert(s.buf + i, s.len - i, globals[f].buf, globals[f].len, 2); } } } - return buf; + return s; } /* Strips all prefixing and leading whitespace. * Except if the last character beforehand is a newline. */ static size_t -strip(char * buf) { - size_t i = strlen(buf); +strip(string_t s) { + size_t i = s.len; if (!i) { return 0; } - while (isspace(buf[i - 1])) + while (isspace(s.buf[i])) { --i; } - buf[i] = '\0'; - for (i = 0; isspace(buf[i]); ++i); + s.buf[i] = '\0'; + for (i = 0; isspace(s.buf[i]); ++i); - return i - (buf[i - 1] == '\n'); + return i - (s.buf[i - 1] == '\n'); } static int run(char * buf) { + int ret = 127; fputs(GREEN "output" RESET ":\n", stderr); - return system(buf); + pid_t pid = fork(); + if (!pid) { + execl("/bin/sh", "-c", "'ls bake.c'", NULL); + } else { + int status; + waitpid(pid, &status, 0); + if (!WIFEXITED(status)) { ret = 126; } + else { ret = WEXITSTATUS(status); } + } + return ret; } int main(int argc, char ** argv) { int ret = 0; - char * buf = NULL; - size_t len; + string_t s = { 0, NULL }; if (argc < 2 - || !strcmp(argv[1], "-h") - || !strcmp(argv[1], "--help")) + || streq(argv[1], "-h") + || streq(argv[1], "--help")) { goto help; } - g_filename = argv[1]; + g_filename = (string_t) { strlen(argv[1]), argv[1] }; - if (!strcmp(argv[1], "-n") - || !strcmp(argv[1], "--dry-run")) { - if (argc > 2) { ret = 1; g_filename = argv[2]; } + if (streq(argv[1], "-n") + || streq(argv[1], "--dry-run")) { + if (argc > 2) { + ret = 1; + g_filename = (string_t) { strlen(argv[2]), argv[2] }; + } else { goto help; } } - { map_t m = map(g_filename); - if (m.str) { - buf = find_region(m, &len); - munmap(m.str, m.len); - } - } + s = file_find_region(g_filename.buf, START, STOP); - if (!buf) { + if (!s.buf) { if (errno) - { fprintf(stderr, BOLD RED "%s" RESET ": %s\n", g_filename, strerror(errno)); } + { fprintf(stderr, BOLD RED "%s" RESET ": %s\n", g_filename.buf, strerror(errno)); } else - { fprintf(stderr, BOLD RED "%s" RESET ": File unrecognized.\n", g_filename); } + { fprintf(stderr, BOLD RED "%s" RESET ": File unrecognized.\n", g_filename.buf); } return 1; } - g_all = all_args((size_t) argc, argv); + g_all = all_args(argc, argv); g_short = shorten(g_filename); - root(&g_filename); - buf = expand(buf, len); - free(g_short); free(g_all); - if (!buf) { return 1; } + root(g_filename); - fprintf(stderr, GREEN "%s" RESET ": %s\n", argv[0], buf + strip(buf)); - ret = ret ? 0 : run(buf); + s = expand(s); + free(g_short.buf); free(g_all.buf); + if (!s.buf) { return 1; } + + fprintf(stderr, GREEN "%s" RESET ": %s\n", argv[0], s.buf + strip(s)); + ret = ret ? 0 : run(s.buf); if (ret) - { fprintf(stderr, RED "result" RESET ": " BOLD "%d\n" RESET, ret); } + { fprintf(stderr, RED "result" RESET ": " BOLD "%d, %s\n" RESET, errno, strerror(errno)); } - free(buf); + free(s.buf); return ret; help: fprintf(stderr, YELLOW "%s" RESET ": %s", argv[0], HELP DESC); diff --git a/config.h b/config.h index ac0d3fb..ae744d3 100644 --- a/config.h +++ b/config.h @@ -4,12 +4,12 @@ #define ENABLE_COLOR #ifdef ENABLE_COLOR -# define RED "\e[91m" -# define GREEN "\e[92m" -# define YELLOW "\e[93m" -# define DIM "\e[2m" -# define BOLD "\e[1m" -# define RESET "\e[0m" +# define RED "\033[91m" +# define GREEN "\033[92m" +# define YELLOW "\033[93m" +# define DIM "\033[2m" +# define BOLD "\033[1m" +# define RESET "\033[0m" #else # define RED # define GREEN From a5aa6c466a161c5f5e510c79a49b33b55e6b1e40 Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Mon, 26 Feb 2024 23:43:34 +0000 Subject: [PATCH 3/6] anonymous man makes program better --- bake.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bake.c b/bake.c index 54ebd6b..eeaea5a 100644 --- a/bake.c +++ b/bake.c @@ -306,7 +306,7 @@ run(char * buf) { fputs(GREEN "output" RESET ":\n", stderr); pid_t pid = fork(); if (!pid) { - execl("/bin/sh", "-c", "'ls bake.c'", NULL); + execl("/bin/sh", "sh", "-c", buf, NULL); } else { int status; waitpid(pid, &status, 0); From 8d9c0f83b58140e331b0254a41fcc50fe4ff27a2 Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Mon, 26 Feb 2024 23:47:12 +0000 Subject: [PATCH 4/6] remove debug --- bake.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bake.c b/bake.c index eeaea5a..03a8482 100644 --- a/bake.c +++ b/bake.c @@ -358,7 +358,7 @@ main(int argc, char ** argv) { fprintf(stderr, GREEN "%s" RESET ": %s\n", argv[0], s.buf + strip(s)); ret = ret ? 0 : run(s.buf); if (ret) - { fprintf(stderr, RED "result" RESET ": " BOLD "%d, %s\n" RESET, errno, strerror(errno)); } + { fprintf(stderr, RED "result" RESET ": " BOLD "%d\n" RESET, ret); } free(s.buf); return ret; From ab8bce4461219bafad8aa9e68b604f8044cdeeee Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Mon, 26 Feb 2024 23:52:42 +0000 Subject: [PATCH 5/6] fix modified all_args --- bake.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bake.c b/bake.c index 03a8482..b2e4f28 100644 --- a/bake.c +++ b/bake.c @@ -117,7 +117,8 @@ all_args(int argc, char ** argv) { all[len - 1] = '\0'; for (len = 0, i = 2; i < argc; ++i) { strcpy(all + len, argv[i]); - len += strlen(argv[i]) + 1; + len += strlen(argv[i]); + len += (i + 1 < argc); if (i + 1 < argc) { all[len - 1] = ' '; } } } @@ -366,3 +367,4 @@ help: fprintf(stderr, YELLOW "%s" RESET ": %s", argv[0], HELP DESC); return 1; } + From 0530959f92d2a5a96ddd8b1bd75939c5e52bf6ca Mon Sep 17 00:00:00 2001 From: Emil Williams Date: Tue, 27 Feb 2024 04:04:18 +0000 Subject: [PATCH 6/6] all_args updates and output --- bake.c | 58 ++++++++++++++++------------------------------------------ 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/bake.c b/bake.c index b2e4f28..d133736 100644 --- a/bake.c +++ b/bake.c @@ -103,52 +103,27 @@ shorten(string_t s) { static string_t all_args(int argc, char ** argv) { - char * all = NULL; - size_t len = 0; - if (argc > 2) { - int i; - len = (size_t) argc; - - for (i = 2; i < argc; ++i) - { len += strlen(argv[i]); } - - all = malloc(len); - if (all) { - all[len - 1] = '\0'; - for (len = 0, i = 2; i < argc; ++i) { - strcpy(all + len, argv[i]); - len += strlen(argv[i]); - len += (i + 1 < argc); - if (i + 1 < argc) { all[len - 1] = ' '; } - } - } - } - return (string_t) { len, all ? all : calloc(0,0) }; -} - -#if 0 -static string_t -all_args(int argc, char ** argv) { string_t s = (string_t) { 0, NULL }; if (argc > 2) { - size_t i; - for (i = (size_t) argc; i > 2; --i) { - s.len += strlen(argv[i]) + 1; + size_t i, len = 0; + for (i = 2; i < (size_t) argc; ++i) { + len += strlen(argv[i]); } - s.buf = malloc(s.len + (size_t) argc); + s.buf = malloc(len); + s.len = len; if (s.buf) { - size_t len; - s.buf[s.len - 1] = '\0'; - for (len = 0, i = (size_t) argc; i > 2; --i) { + for (len = 0, i = 2; i < (size_t) argc; ++i) { strcpy(s.buf + len, argv[i]); - len += strlen(argv[i]) + 1; - s.buf[len - 1] = ' '; + len += strlen(argv[i]); + if (i + 1 < argc) { + s.buf[len - 1] = ' '; + len++; + } } } } return s; } -#endif /*** Map ***/ @@ -304,7 +279,7 @@ strip(string_t s) { static int run(char * buf) { int ret = 127; - fputs(GREEN "output" RESET ":\n", stderr); + fputs(BOLD GREEN "output" RESET ":\n", stderr); pid_t pid = fork(); if (!pid) { execl("/bin/sh", "sh", "-c", buf, NULL); @@ -342,9 +317,9 @@ main(int argc, char ** argv) { if (!s.buf) { if (errno) - { fprintf(stderr, BOLD RED "%s" RESET ": %s\n", g_filename.buf, strerror(errno)); } + { fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s\n", argv[0], g_filename.buf, strerror(errno)); } else - { fprintf(stderr, BOLD RED "%s" RESET ": File unrecognized.\n", g_filename.buf); } + { fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' File unrecognized.\n", argv[0], g_filename.buf); } return 1; } @@ -356,10 +331,10 @@ main(int argc, char ** argv) { free(g_short.buf); free(g_all.buf); if (!s.buf) { return 1; } - fprintf(stderr, GREEN "%s" RESET ": %s\n", argv[0], s.buf + strip(s)); + fprintf(stderr, BOLD GREEN "%s" RESET ": %s\n", argv[0], s.buf + strip(s)); ret = ret ? 0 : run(s.buf); if (ret) - { fprintf(stderr, RED "result" RESET ": " BOLD "%d\n" RESET, ret); } + { fprintf(stderr, BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret); } free(s.buf); return ret; @@ -367,4 +342,3 @@ help: fprintf(stderr, YELLOW "%s" RESET ": %s", argv[0], HELP DESC); return 1; } -