feat: add simple two-thread watch prototype

I feel like I'm crushing it today!
This commit is contained in:
2024-08-03 11:13:09 +02:00
parent 9d1408c174
commit 121643be45
5 changed files with 61 additions and 10 deletions

View File

@ -9,6 +9,9 @@ cli_options new_options() {
result.file.has_value = false;
result.file.value = NULL;
result.file2.has_value = false;
result.file2.value = NULL;
result.verbosity = 1;
result.help = false;
@ -29,10 +32,12 @@ cli_options new_options() {
void free_options(cli_options v) {
if(v.file.has_value)
free(v.file.value);
if(v.file2.has_value)
free(v.file2.value);
}
// <max
const char* optstring = "f:v:Cl:hV";
const char* optstring = "f:F:v:Cl:hV";
const char* help_msg =
"Usage: %s [-v level] [-C] [-l file] [-h] [-V]\n"
"\n"
@ -42,7 +47,8 @@ const char* help_msg =
"ACTUALLY USEFUL YET\n"
"\n"
"OPTIONS:\n"
" -f file set file\n"
" -f file (WIP) set file\n"
" -F file (WIP) set another file\n"
" -v level Set verbosity level [0-4]\n"
" -C Force color output, ignoring $NO_COLOR\n"
" -l file Set log to output to a file\n"
@ -64,6 +70,10 @@ cli_options parse(int argc, char** argv) {
options.file.value = strdup(optarg);
options.file.has_value = true;
break;
case 'F':
options.file2.value = strdup(optarg);
options.file2.has_value = true;
break;
case 'v':
options.verbosity = atoi(optarg);
break;

View File

@ -1,10 +1,13 @@
#include "cli.h"
#include "log.h"
#include "notify.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void on_event(struct inotify_event* const e) {
const char* msg =
"got an event:\n"
@ -17,6 +20,12 @@ void on_event(struct inotify_event* const e) {
log_info(msg, e->wd, e->mask, e->cookie, e->len, e->name);
}
void* listen_for_changes_thread(void* data) {
const char* f = (const char*)data;
listen_for_changes(f, &on_event);
return NULL;
}
int main(int argc, char** argv) {
cli_options args = parse(argc, argv);
log_settings settings;
@ -29,19 +38,46 @@ int main(int argc, char** argv) {
print_help(stdout, argv[0]);
exit(EXIT_SUCCESS);
}
if(args.version) {
fprintf(stdout, SCI_VERSION "\n");
exit(EXIT_SUCCESS);
}
if(args.file.has_value) {
if(access(args.file.value, F_OK) != 0) {
fprintf(stderr, "no such file or directory %s\n", args.file.value);
exit(EXIT_FAILURE);
}
listen_for_changes(args.file.value, &on_event);
if(!args.file.has_value) {
fprintf(stderr, "no file provided see -h for usage\n");
exit(EXIT_FAILURE);
}
if(!args.file2.has_value) {
fprintf(stderr, "no second file provided see -h for usage\n");
exit(EXIT_FAILURE);
}
if(access(args.file.value, F_OK) != 0) {
fprintf(stderr, "no such file or directory %s\n", args.file.value);
exit(EXIT_FAILURE);
}
if(access(args.file2.value, F_OK) != 0) {
fprintf(stderr, "no such file or directory %s\n", args.file2.value);
exit(EXIT_FAILURE);
}
log_trace("spawning trigger thread for %s", args.file.value);
pthread_t file1_thread;
pthread_attr_t attr1;
ASSERT_SYSCALL_SUCCESS(pthread_attr_init(&attr1));
ASSERT_SYSCALL_SUCCESS(pthread_create(&file1_thread, &attr1, &listen_for_changes_thread, (void*)args.file.value));
ASSERT_SYSCALL_SUCCESS(pthread_attr_destroy(&attr1));
log_trace("spawning trigger thread for %s", args.file2.value);
pthread_t file2_thread;
pthread_attr_t attr2;
ASSERT_SYSCALL_SUCCESS(pthread_attr_init(&attr2));
ASSERT_SYSCALL_SUCCESS(pthread_create(&file2_thread, &attr1, &listen_for_changes_thread, (void*)args.file2.value));
ASSERT_SYSCALL_SUCCESS(pthread_attr_destroy(&attr2));
pthread_join(file1_thread, NULL);
pthread_join(file2_thread, NULL);
free_options(args);
}