diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 51 |
1 files changed, 50 insertions, 1 deletions
@@ -69,6 +69,28 @@ static void assert_db_exists(void) exit(EXIT_FAILURE); } +/* 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) +{ + printf(" [Y/n]\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; +} + /* Convert heap-allocated *STR to C string to SQL string, essentially by adding * single quotes to escape single quotes. */ static void sanitize_sql_str(char **str) @@ -352,19 +374,46 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file) free(description); } +static void ftag_file_add_usage(void) +{ + fprintf(stderr, "Usage: ftag file add [-i] FILE...\n"); +} + /* Add new files to the database. */ static void ftag_file_add(int argc, char **argv) { if (argc == 0) { - fprintf(stderr, "ftag file add: must supply file names\n"); + ftag_file_add_usage(); exit(EXIT_FAILURE); } sqlite3 *db = NULL; + int interactive = 0; int rc; + while (argv[0][0] == '-') { + switch (argv[0][1]) { + case 'i': + interactive = 1; + break; + default: + ftag_file_add_usage(); + exit(EXIT_FAILURE); + } + argv++; + argc--; + } rc = sqlite3_open(DATABASE_PATH, &db); sqlite3_check(rc, db); int next_id = table_next_id(db, "files"); for (int i = 0; i < argc; i++) { + char *file = argv[i]; + if (interactive) { + printf("Add \"%s\" to database ?", file); + if (!prompt_yes_no()) + continue; /* do not add this file */ + } + else { + printf("ftag file add: adding file \"%s\" to database\n", file); + } ftag_add_one_file(db, &next_id, argv[i]); } sqlite3_close(db); |
