Browse Source

-c --color disable option added

Signed-off-by: Emil Williams <emilemilemil@cock.li>
tags/v20240413
Chad C. Starz 1 month ago
parent
commit
4fc2c69e96
No known key found for this signature in database GPG Key ID: CEEBC9208C287297
3 changed files with 110 additions and 36 deletions
  1. +1
    -0
      README
  2. +26
    -9
      bake.1
  3. +83
    -27
      bake.c

+ 1
- 0
README View File

@@ -78,6 +78,7 @@ Options must come before the filename, and may be merged together, such as -xn.
-h, --help: display the help message, similarly to empty input. -h, --help: display the help message, similarly to empty input.
-n, --dry-run: DRYRUN, does NOT run anything! -n, --dry-run: DRYRUN, does NOT run anything!
-x, --expunge: See above Expunge Extension section. -x, --expunge: See above Expunge Extension section.
-c, --color: Disables color for a clean output.


--- Shake --- --- Shake ---




+ 26
- 9
bake.1 View File

@@ -17,18 +17,18 @@ appears.
\fBShake\fP does not support some features of \fBBake\fP, such as \fB@STOP\fP or \fBbinary files\fP, \fBShake\fP does not support some features of \fBBake\fP, such as \fB@STOP\fP or \fBbinary files\fP,
please avoid its use. please avoid its use.


Options [Must always be put first, may be merged together]
Options must always be put first, and may be merged together.


.HP .HP
.B \-v \-\-version, \-h \-\-help, \-n \-\-dry\-run, \-x \-\-expunge
\-v \-\-version, \-h \-\-help, \fB\-n \-\-dry\-run\fP, \fB\-x \-\-expunge\fP,
\fB\-c \-\-color\fP
.PP .PP
Expansions Expansions


These will expand to their counterpart statically first thing.
These may only be inserted inside of markers.
These symbols will expand to their counterpart before execution.
.TP .TP
.B @FILENAME, $@ .B @FILENAME, $@
returns target\-file (abc.x.txt)
returns target\-file (abc.x.txt)
.TP .TP
.B @SHORT, $* .B @SHORT, $*
returns target\-file without suffix (abc.x.txt \-> abc.x) returns target\-file without suffix (abc.x.txt \-> abc.x)
@@ -40,18 +40,35 @@ returns
.PP .PP
Additional Features And Notes Additional Features And Notes


\fB@{\fPEXPUNGE_THIS_FILE\fB}\fP is a inline block to delete files or directories, non-recursive, only one file per block, removed from left to right. Has no influence on the normal command execution.
Shell execution may be disabled with the
.B -n or --dry-run
option.


\\SPECIAL_NAME will result in SPECIAL_NAME in the executed shell command. Backslashes are applicable to all symbols used by Bake, they are ignored otherwise.
\fB@{\fPEXPUNGE_THIS_FILE\fB}\fP is a inline block to delete files or
directories, non-recursive, only one file per block, removed from left to right.
Has no influence on the normal command execution. The deletion only occurs if
you use the
.B -x or --expunge
option.

Colors may be disabled with the
.B -c or --color
option.

\\SPECIAL_NAME will result in SPECIAL_NAME in the executed shell
command. Backslashes are applicable to all symbols used by Bake, they are
ignored otherwise.


.SH EXAMPLE .SH EXAMPLE
.\" SRC BEGIN (example.c) .\" SRC BEGIN (example.c)
.EX .EX
// example.c // example.c
// @BAKE cc $@ -o $*
// @BAKE cc -o @{@SHORT} @FILENAME @ARGS
// or, simply, @BAKE cc -o @{$*} $@ $+
#include <stdio.h> #include <stdio.h>
int main (void) { int main (void) {
printf("Hello.");
puts("Hello.");
return 0;
} }
.EE .EE
.SH COPYRIGHT .SH COPYRIGHT


+ 83
- 27
bake.c View File

@@ -6,6 +6,7 @@
* @BAKE cc -std=c89 -O2 @FILENAME -o @{@SHORT} @ARGS @STOP * @BAKE cc -std=c89 -O2 @FILENAME -o @{@SHORT} @ARGS @STOP
*/ */


#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L


#include <assert.h> #include <assert.h>
@@ -31,12 +32,13 @@
#define HELP \ #define HELP \
BOLD "[option] target-file" RESET " [" GREEN "arguments" RESET " ...]\n" \ 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" \ "Use the format `" BOLD "@BAKE" RESET " cmd ...' within the target-file, this will execute the\n" \
"rest of line, or if found within the file, until the " BOLD "@STOP" RESET " marker.\n" \
"rest of line, or if found within the file, until the " BOLD "@STOP" RESET " marker.\n"


#define DESC \ #define DESC \
"Options [Must always be put first, may be merged together]\n" \ "Options [Must always be put first, may be merged together]\n" \
"\t" DIM "-v --version" RESET ", " DIM "-h --help" RESET ", " \ "\t" DIM "-v --version" RESET ", " DIM "-h --help" RESET ", " \
BOLD "-n --dry-run" RESET ", " BOLD "-x --expunge\n" RESET \
BOLD "-n --dry-run" RESET ", " BOLD "-x --expunge\n" RESET ", " \
BOLD "-c --color" RESET \
"Expansions\n" \ "Expansions\n" \
"\t" YELLOW "@FILENAME" RESET " returns target-file (abc.x.txt)\n" \ "\t" YELLOW "@FILENAME" RESET " returns target-file (abc.x.txt)\n" \
"\t" YELLOW "@SHORT " RESET " returns target-file without suffix (^-> abc.x)\n" \ "\t" YELLOW "@SHORT " RESET " returns target-file without suffix (^-> abc.x)\n" \
@@ -85,6 +87,48 @@ typedef struct {


typedef string_t map_t; typedef string_t map_t;


/*** nocolor printf ***/

#if ENABLE_COLOR
# define color_printf(...) color_fprintf(stdout, __VA_ARGS__)
/* not perfect, too simple, doesn't work with a var, only a literal. */
# define color_puts(msg) color_fprintf(stdout, msg "\n")

int color = ENABLE_COLOR;

static char * expand(char * buf, char * macro, char * with);

color_fprintf(FILE * fp, char * format, ...) {
va_list ap;
char * buf;
va_start(ap, format);
if (!color) {

vasprintf(&buf, format, ap);

if (buf) {
expand(buf, RED, "");
expand(buf, GREEN, "");
expand(buf, YELLOW, "");
expand(buf, DIM, "");
expand(buf, BOLD, "");
expand(buf, RESET, "");

fwrite(buf, strlen(buf), 1, fp);
}

free(buf);
} else {
vfprintf(fp, format, ap);
}
va_end(ap);

}
#else
# define color_printf(...) fprintf(stdout, __VA_ARGS__)
# define color_puts(msg) puts(msg)
#endif

/*** root ***/ /*** root ***/


static void static void
@@ -372,13 +416,15 @@ remove_expand(char * buf, char * argv0, int rm, char * start, char * stop) {
if (rm & BAKE_EXPUNGE) { if (rm & BAKE_EXPUNGE) {
swap(buf + i + (f - i), x); swap(buf + i + (f - i), x);
#if !ENABLE_EXPUNGE_REMOVE #if !ENABLE_EXPUNGE_REMOVE
printf("%s: %sremoving '%s'\n",
argv0, rm & BAKE_NORUN ? "not " : "", buf + i);
color_printf("%s: %sremoving '%s'\n",
argv0, rm & BAKE_NORUN ? "not " : "", buf + i);

if (!(rm & BAKE_NORUN)) { if (!(rm & BAKE_NORUN)) {
remove(buf + i); remove(buf + i);
} }

#else #else
printf("%s: not removing '%s'\n", argv0, buf + i);
color_printf("%s: not removing '%s'\n", argv0, buf + i);
#endif #endif
swap(buf + i + (f - i), x); swap(buf + i + (f - i), x);
} }
@@ -427,21 +473,21 @@ strip(char * buf) {
static int static int
run(char * buf, char * argv0) { run(char * buf, char * argv0) {
pid_t pid; pid_t pid;
puts(BOLD GREEN "output" RESET ":\n");
color_puts(BOLD GREEN "output" RESET ":\n");


if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
execl("/bin/sh", "sh", "-c", buf, NULL); execl("/bin/sh", "sh", "-c", buf, NULL);
return 0; /* execl overwrites the process anyways */ return 0; /* execl overwrites the process anyways */
} else if (pid == -1) { } else if (pid == -1) {
fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n",
argv0, "Fork Error", strerror(errno));
color_fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n",
argv0, "Fork Error", strerror(errno));
return BAKE_ERROR; return BAKE_ERROR;
} else { } else {
int status; int status;


if (waitpid(pid, &status, 0) < 0) { if (waitpid(pid, &status, 0) < 0) {
fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n",
argv0, "Wait PID Error", strerror(errno));
color_fprintf(stderr, BOLD RED "%s" RESET ": %s, %s\n",
argv0, "Wait PID Error", strerror(errno));
return BAKE_ERROR; return BAKE_ERROR;
} }


@@ -459,8 +505,8 @@ int
main(int argc, char ** argv) { main(int argc, char ** argv) {
int ret = BAKE_RUN; int ret = BAKE_RUN;
char * buf = NULL, char * buf = NULL,
* filename,
* argv0;
* filename,
* argv0;


argv0 = argv[0]; argv0 = argv[0];


@@ -482,8 +528,11 @@ main(int argc, char ** argv) {
ret |= BAKE_EXPUNGE; ret |= BAKE_EXPUNGE;
} else if (!strcmp(argv[0], "dry-run")) { } else if (!strcmp(argv[0], "dry-run")) {
ret |= BAKE_NORUN; ret |= BAKE_NORUN;
} else if (!strcmp(argv[0], "color")) {
#if ENABLE_COLOR
color = 0;
#endif
} else { } else {
puts("UNKNOWN");
goto help; goto help;
} }
} else do { } else do {
@@ -502,11 +551,18 @@ main(int argc, char ** argv) {
ret |= BAKE_NORUN; ret |= BAKE_NORUN;
break; break;


#if ENABLE_COLOR

case 'c':
color = 0;
break;
#endif

case 0 : case 0 :
goto next; goto next;


default : default :
puts("UNKNOWN");
color_puts("UNKNOWN");
goto help; goto help;
} }
} while (++(argv[0])); } while (++(argv[0]));
@@ -518,10 +574,10 @@ main(int argc, char ** argv) {
++argv, --argc; ++argv, --argc;


if (strlen(filename) > FILENAME_LIMIT) { if (strlen(filename) > FILENAME_LIMIT) {
fprintf(stderr, BOLD RED "%s" RESET
": Filename too long (exceeds %d)\n",
argv0,
FILENAME_LIMIT);
color_fprintf(stderr, BOLD RED "%s" RESET
": Filename too long (exceeds %d)\n",
argv0,
FILENAME_LIMIT);
return BAKE_ERROR; return BAKE_ERROR;
} }


@@ -533,14 +589,14 @@ main(int argc, char ** argv) {
error[0] = "File Unrecognized"; error[0] = "File Unrecognized";
error[1] = "Found start without suffix spacing"; error[1] = "Found start without suffix spacing";


fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s.\n",
argv0, filename, errno ? strerror(errno) : error[bake_errno]);
color_fprintf(stderr, BOLD RED "%s" RESET ": '" BOLD "%s" RESET "' %s.\n",
argv0, filename, errno ? strerror(errno) : error[bake_errno]);
return BAKE_ERROR; return BAKE_ERROR;
} }


buf = bake_expand(buf, filename, argc, argv); buf = bake_expand(buf, filename, argc, argv);


printf(BOLD GREEN "%s" RESET ": %s\n", argv0, buf + strip(buf));
color_printf(BOLD GREEN "%s" RESET ": %s\n", argv0, buf + strip(buf));


remove_expand(buf, argv0, ret, EXPUNGE_START, EXPUNGE_STOP); remove_expand(buf, argv0, ret, EXPUNGE_START, EXPUNGE_STOP);


@@ -548,19 +604,19 @@ main(int argc, char ** argv) {
ret = run(buf, argv0); ret = run(buf, argv0);


if (ret) { if (ret) {
printf(BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret);
color_printf(BOLD RED "result" RESET ": " BOLD "%d\n" RESET, ret);
} }
} else { ret = 0; } } else { ret = 0; }


free(buf); free(buf);
return ret; return ret;
help: help:
fprintf(stderr, YELLOW "%s" RESET ": %s\n", argv0, HELP DESC);
color_fprintf(stderr, YELLOW "%s" RESET ": %s\n", argv0, HELP DESC);
return BAKE_ERROR; return BAKE_ERROR;
version: version:
fprintf(stderr,
YELLOW "%s" RESET ": v" VERSION "\n"
"Copyright " COPYRIGHT "\n" LICENSE "\n",
argv0);
color_fprintf(stderr,
YELLOW "%s" RESET ": v" VERSION "\n"
"Copyright " COPYRIGHT "\n" LICENSE "\n",
argv0);
return BAKE_ERROR; return BAKE_ERROR;
} }

Loading…
Cancel
Save