add config file argument

This commit is contained in:
Felix Kratz 2023-03-05 18:56:47 +01:00
parent 5bb9d781be
commit c406cfab4d

View file

@ -15,9 +15,12 @@
#define VERSION_OPT_LONG "--version" #define VERSION_OPT_LONG "--version"
#define VERSION_OPT_SHRT "-v" #define VERSION_OPT_SHRT "-v"
#define CONFIG_OPT_LONG "--config"
#define CONFIG_OPT_SHRT "-c"
#define MAJOR 2 #define MAJOR 2
#define MINOR 14 #define MINOR 14
#define PATCH 0 #define PATCH 2
extern int SLSMainConnectionID(void); extern int SLSMainConnectionID(void);
extern int RunApplicationEventLoop(void); extern int RunApplicationEventLoop(void);
@ -36,142 +39,153 @@ bool g_volume_events;
bool g_brightness_events; bool g_brightness_events;
static int client_send_message(int argc, char **argv) { static int client_send_message(int argc, char **argv) {
if (argc <= 1) { if (argc <= 1) {
return EXIT_SUCCESS;
}
char *user = getenv("USER");
if (!user) {
error("sketchybar-msg: 'env USER' not set! abort..\n");
}
int message_length = argc;
int argl[argc];
for (int i = 1; i < argc; ++i) {
argl[i] = strlen(argv[i]);
message_length += argl[i] + 1;
}
char* message = malloc((sizeof(char) * (message_length + 1)));
char* temp = message;
for (int i = 1; i < argc; ++i) {
memcpy(temp, argv[i], argl[i]);
temp += argl[i];
*temp++ = '\0';
}
*temp++ = '\0';
char* rsp = mach_send_message(mach_get_bs_port(MACH_BS_NAME),
message,
message_length,
true );
free(message);
if (!rsp) return EXIT_SUCCESS;
if (strlen(rsp) > 2 && rsp[1] == '!') {
fprintf(stderr, "%s", rsp);
return EXIT_FAILURE;
} else {
fprintf(stdout, "%s", rsp);
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
}
char *user = getenv("USER");
if (!user) {
error("sketchybar-msg: 'env USER' not set! abort..\n");
}
int message_length = argc;
int argl[argc];
for (int i = 1; i < argc; ++i) {
argl[i] = strlen(argv[i]);
message_length += argl[i] + 1;
}
char* message = malloc((sizeof(char) * (message_length + 1)));
char* temp = message;
for (int i = 1; i < argc; ++i) {
memcpy(temp, argv[i], argl[i]);
temp += argl[i];
*temp++ = '\0';
}
*temp++ = '\0';
char* rsp = mach_send_message(mach_get_bs_port(MACH_BS_NAME),
message,
message_length,
true );
free(message);
if (!rsp) return EXIT_SUCCESS;
if (strlen(rsp) > 2 && rsp[1] == '!') {
fprintf(stderr, "%s", rsp);
return EXIT_FAILURE;
} else {
fprintf(stdout, "%s", rsp);
}
return EXIT_SUCCESS;
} }
static void acquire_lockfile(void) { static void acquire_lockfile(void) {
int handle = open(g_lock_file, O_CREAT | O_WRONLY, 0600); int handle = open(g_lock_file, O_CREAT | O_WRONLY, 0600);
if (handle == -1) { if (handle == -1) {
error("sketchybar: could not create lock-file! abort..\n"); error("sketchybar: could not create lock-file! abort..\n");
} }
struct flock lockfd = { struct flock lockfd = {
.l_start = 0, .l_start = 0,
.l_len = 0, .l_len = 0,
.l_pid = getpid(), .l_pid = getpid(),
.l_type = F_WRLCK, .l_type = F_WRLCK,
.l_whence = SEEK_SET .l_whence = SEEK_SET
}; };
if (fcntl(handle, F_SETLK, &lockfd) == -1) { if (fcntl(handle, F_SETLK, &lockfd) == -1) {
error("sketchybar: could not acquire lock-file! abort..\n"); error("sketchybar: could not acquire lock-file... already running?\n");
} }
} }
static bool get_config_file(char *restrict filename, char *restrict buffer, int buffer_size) { static bool get_config_file(char *restrict filename, char *restrict buffer, int buffer_size) {
char *xdg_home = getenv("XDG_CONFIG_HOME"); char *xdg_home = getenv("XDG_CONFIG_HOME");
if (xdg_home && *xdg_home) { if (xdg_home && *xdg_home) {
snprintf(buffer, buffer_size, "%s/sketchybar/%s", xdg_home, filename); snprintf(buffer, buffer_size, "%s/sketchybar/%s", xdg_home, filename);
if (file_exists(buffer)) return true;
}
char *home = getenv("HOME");
if (!home) return false;
snprintf(buffer, buffer_size, "%s/.config/sketchybar/%s", home, filename);
if (file_exists(buffer)) return true; if (file_exists(buffer)) return true;
}
snprintf(buffer, buffer_size, "%s/.%s", home, filename); char *home = getenv("HOME");
return file_exists(buffer); if (!home) return false;
snprintf(buffer, buffer_size, "%s/.config/sketchybar/%s", home, filename);
if (file_exists(buffer)) return true;
snprintf(buffer, buffer_size, "%s/.%s", home, filename);
return file_exists(buffer);
} }
static void exec_config_file(void) { static void exec_config_file(void) {
if (!get_config_file("sketchybarrc", g_config_file, sizeof(g_config_file))) { if (!*g_config_file
printf("could not locate config file.."); && !get_config_file("sketchybarrc", g_config_file, sizeof(g_config_file))) {
return; printf("could not locate config file..");
} return;
}
if (!file_exists(g_config_file)) { if (!file_exists(g_config_file)) {
printf("file '%s' does not exist..", g_config_file); printf("file '%s' does not exist..", g_config_file);
return; return;
} }
if (!ensure_executable_permission(g_config_file)) { if (!ensure_executable_permission(g_config_file)) {
printf("could not set the executable permission bit for '%s'", g_config_file); printf("could not set the executable permission bit for '%s'", g_config_file);
return; return;
} }
if (!fork_exec(g_config_file, NULL)) { if (!fork_exec(g_config_file, NULL)) {
printf("failed to execute file '%s'", g_config_file); printf("failed to execute file '%s'", g_config_file);
return; return;
} }
} }
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
static inline void init_misc_settings(void) { static inline void init_misc_settings(void) {
char *user = getenv("USER"); char *user = getenv("USER");
if (!user) { if (!user) {
error("sketchybar: 'env USER' not set! abort..\n"); error("sketchybar: 'env USER' not set! abort..\n");
} }
snprintf(g_lock_file, sizeof(g_lock_file), LCFILE_PATH_FMT, user); snprintf(g_lock_file, sizeof(g_lock_file), LCFILE_PATH_FMT, user);
if (__builtin_available(macOS 13.0, *)) { if (__builtin_available(macOS 13.0, *)) {
} else { } else {
NSApplicationLoad(); NSApplicationLoad();
} }
signal(SIGCHLD, SIG_IGN); signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
CGSetLocalEventsSuppressionInterval(0.0f); CGSetLocalEventsSuppressionInterval(0.0f);
CGEnableEventStateCombining(false); CGEnableEventStateCombining(false);
g_connection = SLSMainConnectionID(); g_connection = SLSMainConnectionID();
g_volume_events = false; g_volume_events = false;
g_brightness_events = false; g_brightness_events = false;
} }
#pragma clang diagnostic pop #pragma clang diagnostic pop
static void parse_arguments(int argc, char **argv) { static void parse_arguments(int argc, char **argv) {
if ((string_equals(argv[1], VERSION_OPT_LONG)) || (string_equals(argv[1], VERSION_OPT_SHRT))) { if ((string_equals(argv[1], VERSION_OPT_LONG))
|| (string_equals(argv[1], VERSION_OPT_SHRT))) {
fprintf(stdout, "sketchybar-v%d.%d.%d\n", MAJOR, MINOR, PATCH); fprintf(stdout, "sketchybar-v%d.%d.%d\n", MAJOR, MINOR, PATCH);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} } else if ((string_equals(argv[1], CLIENT_OPT_LONG))
|| (string_equals(argv[1], CLIENT_OPT_SHRT))) {
if ((string_equals(argv[1], CLIENT_OPT_LONG)) || (string_equals(argv[1], CLIENT_OPT_SHRT)))
exit(client_send_message(argc-1, argv+1)); exit(client_send_message(argc-1, argv+1));
} else if ((string_equals(argv[1], CONFIG_OPT_LONG))
|| (string_equals(argv[1], CONFIG_OPT_SHRT))) {
if (argc < 3) {
printf("[!] Error: Too few arguments for argument 'config'.\n");
} else {
snprintf(g_config_file, sizeof(g_config_file), "%s", argv[2]);
return;
}
exit(EXIT_FAILURE);
}
exit(client_send_message(argc, argv)); exit(client_send_message(argc, argv));
} }