diff options
| author | iamcheeseman <[email protected]> | 2026-03-03 17:02:48 -0500 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-03-03 17:02:48 -0500 |
| commit | 386a929bf551fd4f05cf7f81a391559a2592cb97 (patch) | |
| tree | a84d66f8f5222b58cc30cf1afc2839781236c6fe | |
| parent | 70f6ddd0529a2d7c457328f0bfbc360e3d143856 (diff) | |
replace tsarc with a c program
| -rw-r--r-- | Makefile | 31 | ||||
| -rw-r--r-- | src/tsarc.c | 152 | ||||
| -rwxr-xr-x | tsarc | bin | 74 -> 21328 bytes |
3 files changed, 171 insertions, 12 deletions
@@ -1,32 +1,39 @@ CC=cc -CFLAGS+=-std=c99 -pedantic -Wall -Wextra -O2 -I/usr/include/freetype2 +CFLAGS+=-std=c99 -pedantic -Wall -Wextra -O2 LDFLAGS+=-lX11 -lXft INSTALL=/usr/local/bin -TSAR_OUT=tsarbar +TSARBAR_OUT=tsarbar +TSARC_OUT=tsarc -HEADERS=src/pipe.h src/bardata.h -SRC=src/tsar.c src/pipe.c src/bardata.c +TSARBAR_SRC=src/tsar.c src/pipe.c src/bardata.c +TSARC_SRC=src/tsarc.c -.PHONY: default run clean +.PHONY: default all run clean install uninstall -default: $(TSAR_OUT) +default: all + +all: $(TSARBAR_OUT) $(TSARC_OUT) # Project should be small enough that we should be able to ignore incremental # compilation -$(TSAR_OUT): $(SRC) - $(CC) $(SRC) -o $(TSAR_OUT) $(CFLAGS) $(LDFLAGS) +$(TSARBAR_OUT): $(TSARBAR_SRC) + $(CC) $(TSARBAR_SRC) -o $(TSARBAR_OUT) $(CFLAGS) -I/usr/include/freetype2 $(LDFLAGS) + +$(TSARC_OUT): $(TSARC_SRC) + $(CC) $(TSARC_SRC) -o $(TSARC_OUT) $(CFLAGS) %.c: -run: $(TSAR_OUT) - ./$(TSAR_OUT) +run: $(TSARBAR_OUT) + ./$(TSARBAR_OUT) clean: - rm $(TSAR_OUT) + rm $(TSARBAR_OUT) + rm $(TSARC_OUT) -install: $(TSAR_OUT) +install: all cp tsarbar $(INSTALL)/tsarbar cp tsarc $(INSTALL)/tsarc diff --git a/src/tsarc.c b/src/tsarc.c new file mode 100644 index 0000000..27ab941 --- /dev/null +++ b/src/tsarc.c @@ -0,0 +1,152 @@ +#include <fcntl.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#define PIPE_PATH "/tmp/tsarc.pipe" +#define STACK_LEN(arr) (sizeof(arr)/sizeof(arr[0])) + +const char help_text[] = + "usage: tsarc [command] ...\n" + "commands:\n" + "\tconfig - configure tsarbar settings\n" + "\tset - set component data and options\n" + "\tlayout - define the layout of the bar\n" + "\n" + "config:\n" + "usage: tsarc config (-[property] [value] ...)\n" + "\t-height - height of the bar, in pixels\n" + "\t-font - font to use\n" + "\t-gap - gap between components, in pixels\n" + "\t-foreground, -fg - foreground color, as a hex code (e.g. #FFAA44)\n" + "\t-background, -bg - background color, as a hex code (e.g. #FFAA44)\n" + "\n" + "set:\n" + "usage: tsarc set [variable] (-[property] [value] ...)\n" + "\t-text - text of the component\n" + "\t-fg - foreground color, as a hex code (e.g. #FFAA44)\n" + "\t-bg - background color, as a hex code (e.g. #FFAA44)\n" + "\t-margin-left - margin to the left, in pixels\n" + "\t-margin-right - margin to the right, in pixels\n" + "\t-margin - sets both -margin-left and -margin-right\n" + "\n" + "layout:\n" + "usage: tsarc layout (-[side] [component list] ...)\n" + "\t-left, -center, -right - valid sides\n" +; + +const char *valid_config_properties[] = { + "-height", + "-font", + "-gap", + "-fg", + "-bg", + "-foreground", + "-background", +}; + +const char *valid_set_properties[] = { + "-text", + "-fg", + "-bg", + "-margin-left", + "-margin-right", + "-margin", +}; + +void show_help(FILE* file) { + fputs(help_text, file); +} + +bool verify_argument_spelling(const char* arg, const char** valid, int validc) { + for (int i = 0; i < validc; i++) { + if (strcmp(arg, valid[i]) == 0) { + return true; + } + } + return false; +} + +void die(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + putc('\n', stderr); + + show_help(stderr); + + exit(1); +} + +int main(int argc, const char* argv[]) { + if (argc < 2) { + die("error: missing a command"); + } + + const char* cmd = argv[1]; + + if (strcmp(cmd, "-h") == 0 || strcmp(cmd, "--help") == 0) { + show_help(stdout); + return 0; + } + + if (strcmp(cmd, "config") == 0) { + if (argc < 3) { + die("error: missing a config property"); + } + + verify_argument_spelling( + argv[2], + valid_config_properties, + STACK_LEN(valid_config_properties) + ); + } + if (strcmp(cmd, "set") == 0) { + if (argc < 3) { + die("error: missing a component name"); + } + if (argc < 4) { + die("error: missing a component property"); + } + + verify_argument_spelling( + argv[3], + valid_set_properties, + STACK_LEN(valid_set_properties) + ); + } + + int cmd_len = 0; + for (int i = 1; i < argc; i++) { + cmd_len += strlen(argv[i]); + } + + cmd_len += argc; + char* cmd_str = (char*)malloc(sizeof(char) * (cmd_len + 1)); + + int start = 0; + for (int i = 1; i < argc; i++) { + size_t arg_len = strlen(argv[i]); + memcpy(cmd_str + start, argv[i], arg_len); + cmd_str[start + arg_len] = '\201'; + start += arg_len + 1; + } + + cmd_str[cmd_len-1] = '\n'; + cmd_str[cmd_len] = '\0'; + + mkfifo(PIPE_PATH, 0666); + FILE* named_pipe = fopen(PIPE_PATH, "w"); + + fwrite(cmd_str, sizeof(char), cmd_len, named_pipe); + + fclose(named_pipe); + + return 0; +} Binary files differ |
