From c89eb42d58321d3e1325a684ef50a05ee3f99cfa Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sun, 26 Apr 2026 12:22:52 +0200 Subject: Add a "input" module --- Makefile | 5 +++-- src/input.c | 33 +++++++++++++++++++++++++++++++++ src/input.h | 12 ++++++++++++ src/main.c | 30 +----------------------------- 4 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 src/input.c create mode 100644 src/input.h diff --git a/Makefile b/Makefile index adec8b4..5ceade2 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ export __LIBS := $(shell pkg-config --libs sqlite3) all: $(PROG) -$(PROG): src/main.o src/system.o src/utils.o +$(PROG): src/main.o src/input.o src/system.o src/utils.o $(CC) -o $@ $^ $(__LIBS) $(LDFLAGS) $(LIBS) # Create cache and config directories at compile time since they will be used by # the user who compiled ftag @@ -35,7 +35,8 @@ $(PROG): src/main.o src/system.o src/utils.o mkdir -p $(FTAG_CACHE_DIR) mkdir -p $(FTAG_CONFIG_DIR) -src/main.o: src/main.c src/system.h src/utils.h +src/main.o: src/main.c src/input.h src/system.h src/utils.h +src/input.o: src/input.c src/input.h src/system.o: src/system.c src/system.h src/utils.o: src/utils.c src/system.h src/utils.h .c.o: diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..9ac68be --- /dev/null +++ b/src/input.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +#include "input.h" + +int prompt_yes_no(void) +{ + puts(" [Y/n]"); + fflush(stdout); + size_t line_size = 3; + char *line = malloc(line_size); + int attempts = 0; + do { + getline(&line, &line_size, stdin); + char input = line[0]; + if (input == '\n'/*no input*/ || input == 'y' || input == 'Y') + return 1; + else if (input == 'n' || input == 'N') + return 0; + attempts++; + } + while(attempts < 3); + return 0; +} + +void remove_ending_newline(char *str) +{ + int idx = strlen(str) - 1; + assert(str[idx] == '\n'); + str[idx] = '\0'; +} diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..7085bac --- /dev/null +++ b/src/input.h @@ -0,0 +1,12 @@ +#ifndef INPUT_H +#define INPUT_H + +/* Functions for command-line user interaction. */ + +/* Prompt the user for yes or no (default is yes). Before calling, a prompt + * should be printed to stdout, eventually not with an ending newline. */ +int prompt_yes_no(void); + +void remove_ending_newline(char *str); + +#endif diff --git a/src/main.c b/src/main.c index a8e4176..58d30ca 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,7 @@ #include #include "config.h" +#include "input.h" #include "system.h" #include "utils.h" @@ -53,35 +54,6 @@ static void __sqlite3_check(int rc, sqlite3 *db, const char *file, int line, con #define sqlite3_check(RC, DB) \ __sqlite3_check(RC, DB, __FILE__, __LINE__, __func__) -/* Prompt the user for yes or no (default is yes). Before calling, a prompt - * should be printed to stdout, eventually not with an ending newline. */ -static int prompt_yes_no(void) -{ - puts(" [Y/n]"); - fflush(stdout); - size_t line_size = 3; - char *line = malloc(line_size); - int attempts = 0; - do { - getline(&line, &line_size, stdin); - char input = line[0]; - if (input == '\n'/*no input*/ || input == 'y' || input == 'Y') - return 1; - else if (input == 'n' || input == 'N') - return 0; - attempts++; - } - while(attempts < 3); - return 0; -} - -static void remove_ending_newline(char *str) -{ - int idx = strlen(str) - 1; - assert(str[idx] == '\n'); - str[idx] = '\0'; -} - /* Convert heap-allocated *STR from C string to SQL string, essentially by * adding single quotes to escape single quotes. */ static void sanitize_sql_str(char **str) -- cgit v1.2.3