diff --git a/src/main/lktadm.c b/src/main/lktadm.c index e263bdfdbab113abffd05e91d0c7c4b6327e9758..cb4a68bd24f944484547066f4a6957547ff0f40f 100644 --- a/src/main/lktadm.c +++ b/src/main/lktadm.c @@ -19,40 +19,48 @@ #include <string.h> #include <sys/wait.h> +static noreturn inline void +fail(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + write(2, "\n", 1); + exit(EXIT_FAILURE); +} + /* ----------------- * * The main function * * ----------------- */ static sqlite3 *db = NULL; -static char kara_dir[PATH_MAX], db_path[PATH_MAX], mkvpropedit[PATH_MAX], sqlite3_bin[PATH_MAX], init_script[PATH_MAX], buf[100]; +static char kara_dir[PATH_MAX], db_path[PATH_MAX], mkvpropedit[PATH_MAX], sqlite3_bin[PATH_MAX], + init_script[PATH_MAX], buf[100]; void open_db(void) { - if (!database_new(&db)) { - fprintf(stderr, " ! Failed to initialize memory database\n"); - exit(EXIT_FAILURE); - } + if (!database_new(&db)) + fail("Failed to init memory db"); - if (config_open(db)) { - fprintf(stderr, " ! could not load configuration\n"); - exit(EXIT_FAILURE); - } + if (config_open(db)) + fail("Could not load configuration"); if (!database_config_get_text(db, "externals", "mkvpropedit", mkvpropedit, PATH_MAX)) - exit(EXIT_FAILURE); + fail("Not found externals->mkvpropedit"); if (!database_config_get_text(db, "externals", "sqlite3", sqlite3_bin, PATH_MAX)) - exit(EXIT_FAILURE); + fail("Not found externals->sqlite3"); if (!database_config_get_text(db, "database", "db_path", db_path, PATH_MAX)) - exit(EXIT_FAILURE); + fail("Not found database->db_path"); if (!database_config_get_text(db, "database", "kara_dir", kara_dir, PATH_MAX)) - exit(EXIT_FAILURE); + fail("Not found database->kara_dir"); if (!database_config_get_text(db, "database", "init_script", init_script, PATH_MAX)) - exit(EXIT_FAILURE); + fail("Not found database->init_script"); } noreturn void @@ -86,10 +94,8 @@ cat__(struct lkt_cmd_args *args) int i; struct kara_metadata data; for (i = 0; i < args->argc; ++i) { - if (kara_metadata_read(&data, args->argv[i])) { - printf("Failed to read metadata of file %s\n", args->argv[i]); - exit(EXIT_FAILURE); - } + if (kara_metadata_read(&data, args->argv[i])) + fail("Failed to read metadata of file %s", args->argv[i]); printf("Kara path: %s\n" " Song source: %s\n" " Song title: %s\n" @@ -108,10 +114,6 @@ init_metadata__(struct lkt_cmd_args *args) { (void) args; open_db(); - if (!database_config_get_text(db, "database", "kara_dir", kara_dir, PATH_MAX)) { - fprintf(stderr, " ! failed to get the kara directory\n"); - exit(EXIT_FAILURE); - } metadata_set_directory(kara_dir, mkvpropedit); exit(EXIT_SUCCESS); } @@ -144,60 +146,51 @@ init_file_prompt__(struct lkt_cmd_args *args) fprintf(stdout, "Asking for file '%s':\n", args->argv[i]); if (get_stdin_line(" song_name (TITLE): ", data.song_name, LEKTOR_TAG_MAX)) - exit(EXIT_FAILURE); + fail("Input error"); if (get_stdin_line(" source_name (NAME): ", data.source_name, LEKTOR_TAG_MAX)) - exit(EXIT_FAILURE); + fail("Input error"); if (get_stdin_line(" category (cdg, vo, ...): ", data.category, LEKTOR_TAG_MAX)) - exit(EXIT_FAILURE); + fail("Input error"); if (get_stdin_line(" type (OP, AMV, ...): ", data.song_type, LEKTOR_TAG_MAX)) - exit(EXIT_FAILURE); + fail("Input error"); if (get_stdin_line(" language (jp, fr, undefined, ...): ", data.language, LEKTOR_TAG_MAX)) - exit(EXIT_FAILURE); + fail("Input error"); if (get_stdin_line(" author_name (Your peudal): ", data.author_name, LEKTOR_TAG_MAX)) - exit(EXIT_FAILURE); + fail("Input error"); if (!get_stdin_line(" type's number(1, 2, ...): ", buf, 100)) data.song_number = ((num = atoi(buf)) <= 0) ? 1 : num; - else - exit(EXIT_FAILURE); + fail("Input error"); kara_metadata_write(&data, args->argv[i], mkvpropedit); fprintf(stdout, "You may name this kara with the following name: '%s - %s%d - %s'\n", data.source_name, data.song_type, data.song_number, data.song_name); } - exit(EXIT_SUCCESS); } noreturn void get__(struct lkt_cmd_args *args) { - int i; struct kara_metadata data; struct lkt_repo repo; - if (args->argc != 1) { - fprintf(stderr, " ! invalid argument, just need one id\n"); - exit(EXIT_FAILURE); - } + if (args->argc != 1) + fail("Invalid argument"); - i = atoi(args->argv[0]); + int i = atoi(args->argv[0]); - if (repo_new(&repo, "kurisu", "https://kurisu.iiens.net")) { - fprintf(stderr, " ! could not create the repo\n"); - exit(EXIT_FAILURE); - } + if (repo_new(&repo, "kurisu", "https://kurisu.iiens.net")) + fail("Cound not create repo"); - if (repo_get_id(&repo, i, &data)) { - fprintf(stderr, " ! could not download json for kara %d\n", i); - exit(EXIT_FAILURE); - } + if (repo_get_id(&repo, i, &data)) + fail("Cound not download json for kara %d", i); printf("Kara id: %d\n" " Song source: %s\n" @@ -218,41 +211,30 @@ init_database__(struct lkt_cmd_args *args) (void) args; pid_t pid; int wstatus, status, fd; - char * sqlite_args[] = { sqlite3_bin, db_path }; + char *sqlite_args[] = { sqlite3_bin, db_path }; if ((pid = fork()) == 0) { - if ((fd = open(init_script, O_RDONLY)) < 0) { - fprintf(stderr, " ! init_database__: can't to open %s in O_RDONLY\n", init_script); - exit(EXIT_FAILURE); - } + if ((fd = open(init_script, O_RDONLY)) < 0) + fail("Can't open %s in O_RDONLY", init_script); - if (dup2(fd, 0) < 0) { - fprintf(stderr, " ! init_database__: failed to duplicate %s to stdint\n", init_script); - exit(EXIT_FAILURE); - } + if (dup2(fd, 0) < 0) + fail("Failed to duplicate %s to stdin", init_script); execv(sqlite3_bin, sqlite_args); - exit(EXIT_FAILURE); + fail("Failed to execute %s", sqlite3_bin); } - else if (pid < 0) { - fprintf(stderr, " ! init_database__: failed to fork: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } + else if (pid < 0) + fail("Failed to fork: %s", strerror(errno)); else { - do { - if (waitpid(pid, &wstatus, WUNTRACED | WCONTINUED) == -1) { - fprintf(stderr, " ! init_database__: Failed to wait children: %s\n", - strerror(errno)); - exit(EXIT_FAILURE); - } - } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus)); - - if ((status = WEXITSTATUS(wstatus))) { - fprintf(stderr, " ! init_database__: children failed with status %d", status); - exit(EXIT_FAILURE); - } + do + if (waitpid(pid, &wstatus, WUNTRACED | WCONTINUED) == -1) + fail("Failed to wait children: %s\n", strerror(errno)); + while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus)); + + if ((status = WEXITSTATUS(wstatus))) + fail("Children failed with status %d", status); } exit(EXIT_SUCCESS); @@ -262,31 +244,27 @@ noreturn void download__(struct lkt_cmd_args *args) { if (args->argc != 2) - exit(EXIT_FAILURE); + fail("Invalud argument"); struct lkt_repo repo; struct kara_metadata data; int i = atoi(args->argv[0]); - if (repo_new(&repo, "kurisu", "https://kurisu.iiens.net")) { - fprintf(stderr, " ! could not create the repo\n"); - exit(EXIT_FAILURE); - } + if (repo_new(&repo, "kurisu", "https://kurisu.iiens.net")) + fail("Could not create the repo"); - if (repo_download_id_sync(&repo, NULL, i, args->argv[1], &data)) { - fprintf(stderr, " ! could not download json for kara %d\n", i); - exit(EXIT_FAILURE); - } + if (repo_download_id_sync(&repo, NULL, i, args->argv[1], &data)) + fail("Cound not download json for kara %d", i); printf("Kara %d at %s\n" - " Song source: %s\n" - " Song title: %s\n" - " Category: %s\n" - " Type: %s (number %d)\n" - " Author: %s\n" - " Language: %s\n", - i, args->argv[1], data.source_name, data.song_name, data.category, - data.song_type, data.song_number, data.author_name, data.language); + " Song source: %s\n" + " Song title: %s\n" + " Category: %s\n" + " Type: %s (number %d)\n" + " Author: %s\n" + " Language: %s\n", + i, args->argv[1], data.source_name, data.song_name, data.category, + data.song_type, data.song_number, data.author_name, data.language); exit(EXIT_SUCCESS); } @@ -317,4 +295,3 @@ main(int argc, const char **argv) { lkt_cmd_parse(options, argc, argv, help); } -