#include #include #include #include #include #include #include #include #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; }