diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h index e7199bed2efb1eb953f17b3735e9e16ac41d3ccf..a9091eacff72fa2ff1d33b97efd1e8155258af27 100644 --- a/inc/lektor/commands.h +++ b/inc/lektor/commands.h @@ -92,7 +92,7 @@ bool command_set_playback_option(struct lkt_state *srv, size_t c, enum lkt_playb /* Authentificate users */ bool command_password(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]); -bool command_user_add(sqlite3 *db, char *argv[LKT_MESSAGE_ARGS_MAX]); +bool command_user_add(struct lkt_state *srv, size_t c, sqlite3 *db, char *argv[LKT_MESSAGE_ARGS_MAX]); /* Program management control */ bool command_restart(struct lkt_state *srv, size_t c); diff --git a/inc/lektor/defines.h b/inc/lektor/defines.h index 84f263d6d7b056e62e29ae47aab22e002c22ddb3..0b8fb58bd78c0ccc9f4ffd9a8d000955ae00e22a 100644 --- a/inc/lektor/defines.h +++ b/inc/lektor/defines.h @@ -47,3 +47,9 @@ enum mpd_idle_flag { MPD_IDLE_OUTPUT | MPD_IDLE_OPTIONS | MPD_IDLE_PARTITION | MPD_IDLE_STICKER | MPD_IDLE_SUBSCRIPTION | MPD_IDLE_MESSAGE, }; + +#define LKT_BACKLOG 32 +#define COMMAND_LIST_MAX 64 +#define BUFFER_OUT_MAX 16 +#define MPD_VERSION "0.21.16" + diff --git a/inc/lektor/macro.h b/inc/lektor/macro.h index 4b93b9e4b726b41adb61babe3ec9d45f7c817085..712ae9f9a6799646dd2bace4b3d459746631f940 100644 --- a/inc/lektor/macro.h +++ b/inc/lektor/macro.h @@ -49,3 +49,12 @@ #ifndef MIN #define MIN(a, b) ((a) > (b) ? (b) : (a)) #endif /* MIN */ + +#define RETURN_IF(cond, msg, ret) \ + if (cond) { \ + fprintf(stderr, " ! %s: %s\n", __func__, msg); \ + return ret; \ + } + +#define RETURN_UNLESS(cond, msg, ret) RETURN_IF(!(cond), msg, ret) +#define NOTHING /* Usefull to return nothing. */ diff --git a/src/commands.c b/src/commands.c index d03489ed66eb3824dbe1c970feffd1cc5b7f1a9c..024532b0b98aec0a297f80f08099acf60e2e07f9 100644 --- a/src/commands.c +++ b/src/commands.c @@ -27,11 +27,7 @@ command_restart(struct lkt_state *srv, size_t c) { char exe[PATH_MAX]; char *const argv[] = { exe, NULL }; - - if (!lkt_client_auth(srv, c, false)) { - fprintf(stderr, " ! command_restart: Failed to restart, user not authentificated %lu\n", c); - return false; - } + RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false); if (readlink(SELF_EXECUTABLE_LINUX, exe, PATH_MAX - 1) > 0) { fprintf(stderr, " * Restart lektord\n"); @@ -59,20 +55,14 @@ bool command_rescan(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]) { (void) argv; - if (!lkt_client_auth(srv, c, false)) { - fprintf(stderr, " ! command_rescan: Failed, user %lu not authentificated\n", c); - return false; - } + RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false); return ! repo_get_allid_async(); } inline bool command_kill(struct lkt_state *srv, size_t c) { - if (!lkt_client_auth(srv, c, false)) { - fprintf(stderr, " ! command_restart: Failed to restart, user not authentificated %lu\n", c); - return false; - } + RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false); fprintf(stderr, " * Stopping lektord\n"); close(srv->fds[0].fd); exit(EXIT_SUCCESS); @@ -88,7 +78,7 @@ command_currentsong(struct lkt_state *srv, size_t c) memset(&kara, 0, sizeof(struct kara_metadata)); if (!database_queue_current_kara(srv->db, &kara, NULL)) - fprintf(stderr, " ! command_currentsong: Failed to get information about the current kara\n"); + fprintf(stderr, " . command_currentsong: Failed to get information about the current kara\n"); out = lkt_message_new(); idx = snprintf(out->data, LKT_MESSAGE_MAX, @@ -115,16 +105,9 @@ command_status(struct lkt_state *srv, size_t c) int elapsed, duration, songid = 0; const char *play_state; - if (srv == NULL) - return false; - + RETURN_UNLESS(srv, "Invalid argument", false); win = &srv->win; - - if (!database_queue_state(srv->db, &queue_state)) { - fprintf(stderr, " ! command_status: Could not fully determined the status of the playback\n"); - return false; - } - + RETURN_UNLESS(database_queue_state(srv->db, &queue_state), "Can't determine playback status", false); database_queue_current_kara(srv->db, NULL, &songid); win->get_elapsed(win, &elapsed); win->get_duration(win, &duration); @@ -180,25 +163,10 @@ static inline bool __play_that_file(sqlite3 *db, struct lkt_win *win, int pos) { char filepath[PATH_MAX]; - - if (pos == 0) - return false; - - if (!database_queue_play(db, (int) pos)) { - fprintf(stderr, " ! __play_that_file: command failed because of db interactions\n"); - return false; - } - - if (!database_queue_get_current_file(db, filepath)) { - fprintf(stderr, " ! __play_that_file: command failed because of get current\n"); - return false; - } - - if (!win->load_file(win, filepath)) { - fprintf(stderr, " ! __play_that_file: command failed because of get current\n"); - return false; - } - + RETURN_UNLESS(pos, "Invalid argument", false); + RETURN_UNLESS(database_queue_play(db, (int) pos), "DB error", false); + RETURN_UNLESS(database_queue_get_current_file(db, filepath), "Can't get current kara", false); + RETURN_UNLESS(win->load_file(win, filepath), "Can't load file", false); return true; } @@ -213,27 +181,14 @@ command_play(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], /* Argument handle. */ pos = args[0] == NULL ? 1 : strtol(args[0], &endptr, 10); - - if ((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN)) - || (errno != 0 && pos == 0)) { - fprintf(stderr, " ! command_play: strtol failed: %s\n", strerror(errno)); - return false; - } - - if (endptr == args[0]) { - fprintf(stderr, " . command_play: to digit found in string '%s'\n", args[1]); - return false; - } + RETURN_IF((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN)) || (errno != 0 && pos == 0), + "Failed: strtol", false); + RETURN_IF(endptr == args[0], "No digit found", false); /* Do the actual job here. */ database_queue_stop(db); - - if (!win->new (win)) { - fprintf(stderr, " ! command_play: command failed because of null mpv ctx\n"); - return false; - } - + RETURN_UNLESS(win->new (win), "Can't create window", false); return __play_that_file(db, win, pos); } @@ -248,29 +203,16 @@ command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX /* Argument handle. */ - if (args[0] == NULL) - return false; - + RETURN_IF(args[0] == NULL, "Invalid argument", false); id = strtol(args[0], &endptr, 10); - - if ((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) - || (errno != 0 && id == 0)) { - fprintf(stderr, " ! command_playid: strtol failed: %s\n", strerror(errno)); - return false; - } - - if (endptr == args[0]) { - fprintf(stderr, " . command_playid: to digit found in string '%s'\n", args[1]); - return false; - } + RETURN_IF((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0), + "Failed: strtol", false); + RETURN_IF(endptr == args[0], "No digit found", false); /* Do the work. */ database_queue_stop(db); - - if (!win->new (win)) - return false; - + RETURN_UNLESS(win->new (win), "Can't create window", false); database_queue_seekid(db, (int) id, &pos); return __play_that_file(db, win, pos); } @@ -279,34 +221,23 @@ command_playid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX bool command_stop(sqlite3 *db, struct lkt_win *win, enum mpd_idle_flag *watch_mask_ptr) { - if (database_queue_stop(db)) { - win->close(win); - *watch_mask_ptr |= MPD_IDLE_PLAYER; - return true; - } - return false; + RETURN_UNLESS(database_queue_stop(db), "DB error", false); + win->close(win); + *watch_mask_ptr |= MPD_IDLE_PLAYER; + return true; } bool command_add(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { - if (args == NULL) { - fprintf(stderr, " ! command_add: invalid NULL arguments\n"); - return false; - } - + RETURN_UNLESS(args, "Invalid argument", false); *watch_mask_ptr |= MPD_IDLE_PLAYLIST; struct lkt_uri_t uri; char *query = args[0]; int ret, priority = 1; /* To be modified according to the command (insert or add) later (TODO) */ - (void) win; // No callbacks to the window for the moment - - if (!lkt_uri_from(&uri, query)) { - fprintf(stderr, " ! command_add: failed to parse query '%s', invalid uri\n", query); - return false; - } + RETURN_UNLESS(lkt_uri_from(&uri, query), "Failed to parse query", false); switch (uri.type) { case uri_query: @@ -349,26 +280,14 @@ command_addid(sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_ARGS_MAX] char *endptr, *id_str; int priority = 1; - if (args == NULL) { - fprintf(stderr, " ! command_add: invalid NULL arguments\n"); - return false; - } - + RETURN_UNLESS(args, "Invalid argument", false); id_str = args[0]; errno = 0; *watch_mask_ptr |= MPD_IDLE_PLAYLIST; id = strtol(id_str, &endptr, 10); - - if ((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0)) { - fprintf(stderr, " ! command_addid: strtol failed: %s\n", strerror(errno)); - return false; - } - - if (endptr == id_str) { - fprintf(stderr, " . command_addid: to digit found in string '%s'\n", id_str); - return false; - } - + RETURN_IF((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0), + "Failed: strtol", false); + RETURN_IF(endptr == args[0], "No digit found", false); return database_queue_add_id(db, id, priority); } @@ -397,23 +316,15 @@ command_delid(sqlite3 *db, struct lkt_win *win, char *id_str, enum mpd_idle_flag errno = 0; *watch_mask_ptr |= MPD_IDLE_PLAYLIST; id = strtol(id_str, &endptr, 10); - - if ((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0)) { - fprintf(stderr, " ! command_delid: strtol failed: %s\n", strerror(errno)); - return false; - } - - if (endptr == id_str) { - fprintf(stderr, " . command_delid: to digit found in string '%s'\n", id_str); - return false; - } + RETURN_IF((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) || (errno != 0 && id == 0), + "Failed: strtol", false); + RETURN_IF(endptr == id_str, "No digit found", false); /* If one day we allow suppression of the current kara, will need the `win` pointer to reload the kara in the same position (but the kara won't be the same). */ database_queue_current_kara(db, NULL, &uri); - if (id == (long) uri) - return false; + RETURN_IF(id == (long) uri, "Can't delete current kara", false); return database_queue_del_id(db, id); } @@ -423,41 +334,23 @@ command_move(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag * long from, to; char *endptr; - if (args == NULL || args[0] == NULL || args[1] == NULL) { - fprintf(stderr, " ! command_move: invalide arguments\n"); - return false; - } - + RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); errno = 0; *watch_mask_ptr |= MPD_IDLE_PLAYLIST; /* First argument: from */ from = strtol(args[0], &endptr, 10); - - if ((errno == ERANGE && (from == LONG_MAX || from == LONG_MIN)) || (errno != 0 && from == 0)) { - fprintf(stderr, " ! command_move: strtol failed: %s\n", strerror(errno)); - return false; - } - - if (endptr == args[0]) { - fprintf(stderr, " . command_delid: to digit found in string '%s'\n", args[0]); - return false; - } + RETURN_IF((errno == ERANGE && (from == LONG_MAX || from == LONG_MIN)) || (errno != 0 && from == 0), + "Failed: strtol", false); + RETURN_IF(endptr == args[0], "No digit found", false); /* Second argument: to */ to = strtol(args[1], &endptr, 10); - - if ((errno == ERANGE && (to == LONG_MAX || to == LONG_MIN)) || (errno != 0 && to == 0)) { - fprintf(stderr, " ! command_move: strtol failed: %s\n", strerror(errno)); - return false; - } - - if (endptr == args[0]) { - fprintf(stderr, " . command_delid: to digit found in string '%s'\n", args[1]); - return false; - } + RETURN_IF((errno == ERANGE && (to == LONG_MAX || to == LONG_MIN)) || (errno != 0 && to == 0), + "Failed: strtol", false); + RETURN_IF(endptr == args[0], "No digit found", false); return database_queue_move(db, from, to); } @@ -588,10 +481,7 @@ __find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], lo /* Check args */ - if (cmd_args == NULL || cmd_args[0] == NULL) { - fprintf(stderr, " ! __find: Argument invalid, empty cmd_args\n"); - return false; - } + RETURN_UNLESS(cmd_args && cmd_args[0], "Invalid argument", false); /* Select callback */ @@ -639,8 +529,7 @@ __find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], lo /* Get the regex */ - if (!cmd_args[1]) - goto no_rgx; + RETURN_UNLESS(cmd_args[1], "No regex", false); memset(rgx, 0, PATH_MAX * sizeof(char)); for (int i = 1; cmd_args[i]; ++i) { @@ -651,19 +540,15 @@ __find(struct lkt_state *srv, size_t c, char *cmd_args[LKT_MESSAGE_ARGS_MAX], lo /* Make the search langand do the right action */ - if (!init(srv->db, col_name, rgx, &search)) { - fprintf(stderr, " ! __find: Failed to init the search\n"); - return false; - } + RETURN_UNLESS(init(srv->db, col_name, rgx, &search), "Failed to init search", false); for (count = 0; database_search_iter(&search); ++count) continue; if (count) lkt_set_continuation(srv, c, continuation + count); + return true; -no_rgx: - return false; } bool @@ -683,8 +568,7 @@ bool command_set_playback_option(struct lkt_state *srv, size_t c, enum lkt_playback_option opt, char *args[LKT_MESSAGE_MAX]) { - if (srv == NULL) - return false; + RETURN_UNLESS(srv, "Invalid argument", false); (void) c; long val, ret = false; @@ -696,14 +580,9 @@ command_set_playback_option(struct lkt_state *srv, size_t c, enum lkt_playback_o else { errno = 0; val = strtol(args[0], &endptr, 10); - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) { - fprintf(stderr, " ! command_set_playback_option: strtol failed: %s\n", strerror(errno)); - return false; - } - if (endptr == args[0]) { - fprintf(stderr, " ! command_set_playback_option: strtol failed: %s\n", strerror(errno)); - return false; - } + RETURN_IF((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0), + "Failed: strtol", false); + RETURN_IF(endptr == args[0], "No digit found", false); // No values can exceed those boundings, no matter the option // if (val < 0) @@ -739,7 +618,7 @@ command_set_playback_option(struct lkt_state *srv, size_t c, enum lkt_playback_o if (ret) srv->mpd_idle_events |= MPD_IDLE_OPTIONS; - return !ret; + return ! ret; } bool @@ -747,27 +626,15 @@ command_set_pos(sqlite3 *db, struct lkt_win *win, enum mpd_idle_flag *watch_mask { char filepath[PATH_MAX]; *watch_mask_ptr |= MPD_IDLE_PLAYER; - - if (!database_queue_set_current_index(db, index)) { - fprintf(stderr, " ! command_set_pos: Failed to set position in database\n"); - return false; - } - - if (!database_queue_get_current_file(db, filepath)) { - fprintf(stderr, " ! command_set_pos: Failed to get filename\n"); - return false; - } - + RETURN_UNLESS(database_queue_set_current_index(db, index), "Failed to set position in queue", false); + RETURN_UNLESS(database_queue_get_current_file(db, filepath), "Failed to get filename", false); return win->load_file(win, filepath); } bool command_plt_add(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { - if (args == NULL || args[0] == NULL) { - fprintf(stderr, " ! command_plt_add: Invalid argument\n"); - return false; - } + RETURN_UNLESS(args && args[0], "Invalid argument", false); bool ret = false; struct lkt_uri_t uri; @@ -803,8 +670,7 @@ command_plt_remove(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_ char *endptr; long pos; - if (args == NULL || args[0] == NULL) - return false; + RETURN_UNLESS(args && args[0], "Invalid argument", false); if (args[1] == NULL) { *watch_mask_ptr |= MPD_IDLE_PLAYLIST; @@ -812,17 +678,9 @@ command_plt_remove(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_ } pos = strtol(args[1], &endptr, 10); - - if ((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN)) - || (errno != 0 && pos == 0)) { - fprintf(stderr, " ! command_plt_remove: strtol failed: %s\n", strerror(errno)); - return false; - } - - if (endptr == args[1]) { - fprintf(stderr, " . command_plt_remove: to digit found in string '%s'\n", args[1]); - return false; - } + RETURN_IF((errno == ERANGE && (pos == LONG_MAX || pos == LONG_MIN)) || (errno != 0 && pos == 0), + "Failed: strtol", false); + RETURN_IF(endptr == args[1], "No digit found", false); *watch_mask_ptr |= MPD_IDLE_PLAYLIST; return database_plt_remove_pos(db, args[0], pos); @@ -831,26 +689,17 @@ command_plt_remove(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_ bool command_plt_clear(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { - if (args == NULL || args[0] == NULL) - return false; - - if (!database_plt_clear(db, args[0])) - return false; - + RETURN_UNLESS(args && args[0], "Invalid argument", false); + RETURN_UNLESS(database_plt_clear(db, args[0]), "Failed to clear playlist", false); *watch_mask_ptr |= MPD_IDLE_PLAYLIST; return true; - } bool command_plt_rename(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { - if (args == NULL || args[0] == NULL || args[1] == NULL) - return false; - - if (!database_plt_rename(db, args[0], args[1])) - return false; - + RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); + RETURN_UNLESS(database_plt_rename(db, args[0], args[1]), "Failed to rename playlist", false); *watch_mask_ptr |= MPD_IDLE_PLAYLIST; return true; } @@ -858,18 +707,10 @@ command_plt_rename(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_ bool command_plt_export(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { - if (args == NULL || args[0] == NULL || args[1] == NULL) - return false; - - if (!database_attach(db, args[0], args[1])) - return false; - - if (!database_plt_export(db, args[0])) - return false; - - if (!database_detach(db, args[0])) - return false; - + RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); + RETURN_UNLESS(database_attach(db, args[0], args[1]), "Failed to attach database", false); + RETURN_UNLESS(database_plt_export(db, args[0]), "Failed to export playlist", false); + RETURN_UNLESS(database_detach(db, args[0]), "Failed to detach database", false); fprintf(stderr, " * Exported playlist %s with path '%s'\n", args[0], args[1]); *watch_mask_ptr |= MPD_IDLE_PLAYLIST; return true; @@ -878,18 +719,10 @@ command_plt_export(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_ bool command_plt_import(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { - if (args == NULL || args[0] == NULL || args[1] == NULL) - return false; - - if (!database_attach(db, args[0], args[1])) - return false; - - if (!database_plt_import(db, args[0])) - return false; - - if (!database_detach(db, args[0])) - return false; - + RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); + RETURN_UNLESS(database_attach(db, args[0], args[1]), "Failed to attach database", false); + RETURN_UNLESS(database_plt_import(db, args[0]), "Failed to import playlist", false); + RETURN_UNLESS(database_detach(db, args[0]), "Failed to detach playlist", false); fprintf(stderr, " * Imported playlist %s with path '%s'\n", args[0], args[1]); *watch_mask_ptr |= MPD_IDLE_PLAYLIST; return true; @@ -898,34 +731,22 @@ command_plt_import(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_ bool command_plt_add_uri(sqlite3 *db, char *args[LKT_MESSAGE_ARGS_MAX], enum mpd_idle_flag *watch_mask_ptr) { - if (args == NULL || args[0] == NULL || args[1] == NULL) - return false; - + RETURN_UNLESS(args && args[0] && args[1], "Invalid argument", false); struct lkt_uri_t uri; - - if (!lkt_uri_from(&uri, args[1])) - return false; - + RETURN_UNLESS(lkt_uri_from(&uri, args[1]), "Failed to parse uri", false); bool ret = database_plt_add_uri(db, args[0], &uri); lkt_uri_free(&uri); - - if (ret) { - *watch_mask_ptr |= MPD_IDLE_PLAYLIST; - return true; - } - - return false; + RETURN_UNLESS(ret, "Failed to add uri to plt", false); + *watch_mask_ptr |= MPD_IDLE_PLAYLIST; + return true; } bool command_shuffle(sqlite3 *db, enum mpd_idle_flag *watch_mask_ptr) { - if (database_queue_shuffle(db)) { - *watch_mask_ptr |= MPD_IDLE_PLAYER; - return true; - } - - return false; + RETURN_UNLESS(database_queue_shuffle(db), "Failed to shuffle", false); + *watch_mask_ptr |= MPD_IDLE_PLAYER; + return true; } bool @@ -940,26 +761,15 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_ .c = c, }; - if (!args || !args[0] || strlen(args[0]) == 0) { - fprintf(stderr, " ! command_queue_list: invalid argument, is empty\n"); - return false; - } + RETURN_UNLESS(args && args[0] && strlen(args[0]), "Invalid argument", false); /* Convert the first integer. */ errno = 0; str = args[0] + strspn(args[0], "-+ "); // Skip not permited chars (NO NEGATIVE!) // val = (unsigned int) strtol(str, &endptr, 0); - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) - || (errno != 0 && val == 0)) { - fprintf(stderr, " ! command_queue_list: invalid argument, %s\n", strerror(errno)); - return false; - } - - if (endptr == args[0]) { - fprintf(stderr, " ! command_queue_list: invalid argument: no digit found\n"); - return false; - } + RETURN_IF((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0), + "Invalid argument", false); + RETURN_IF(endptr == args[0], "No digit found", false); from = labs(val); @@ -972,17 +782,9 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_ else { str = endptr + strspn(endptr, "-+: "); // NO NEGATIVE! // val = strtol(str, &endptr, 0); - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) - || (errno != 0 && val == 0)) { - fprintf(stderr, " ! command_queue_list: invalid argument, %s\n", strerror(errno)); - return false; - } - - if (endptr == args[0]) { - fprintf(stderr, " ! command_queue_list: invalid argument: no digit found\n"); - return false; - } + RETURN_IF((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0), + "Invalid argument", false); + RETURN_IF(endptr == args[0], "No digit found", false); to = labs(val); @@ -1018,35 +820,18 @@ is_a_range: bool command_password(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]) { - if (argv[0] == NULL || argv[1] != NULL) { - fprintf(stderr, " ! command_password: Invalid arguments, need only one argument\n"); - return false; - } - - if (database_user_authentificate(srv->db, argv[0])) { - fprintf(stderr, " . command_password: Authentificate user successfully for client %lu\n", c); - lkt_client_auth(srv, c, true); - return true; - } else { - fprintf(stderr, " . command_password: Failed to authentificate client %lu\n", c); - return false; - } + RETURN_UNLESS(argv[0] && !argv[1], "Invalid argument", false); + RETURN_UNLESS(database_user_authentificate(srv->db, argv[0]), "Failed to authentificate user", false); + lkt_client_auth(srv, c, true); + return true; } bool -command_user_add(sqlite3 *db, char *argv[LKT_MESSAGE_ARGS_MAX]) +command_user_add(struct lkt_state *srv, size_t c, sqlite3 *db, char *argv[LKT_MESSAGE_ARGS_MAX]) { - if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) { - fprintf(stderr, " ! command_user_add: Invalid arguments, need only two arguments\n"); - return false; - } - - if (database_user_add(db, argv[0], argv[1])) { - fprintf(stderr, " . command_user_add: Successfully added user %s\n", argv[0]); - return true; - } - - fprintf(stderr, " . command_user_add: Failed to add user %s\n", argv[0]); + RETURN_UNLESS(argv[0] && argv[1] && !argv[2], "Invalid argument", false); + RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false); + RETURN_UNLESS(database_user_add(db, argv[0], argv[1]), "Failed to add user", false); return false; } @@ -1137,45 +922,25 @@ sticker_check_is_present_gt(void *_args, const char *sticker, const char *type, bool command_sticker_get(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]) { - if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL || argv[3] != NULL) { - fprintf(stderr, " ! command_sticker_get: Invalid argument, need only 3 arguments\n"); - return false; - } - + RETURN_UNLESS(argv[0] && argv[1] && argv[2] && !argv[3], "Invalid argument", false); int uri = atoi(argv[1]); /* FIXME: Use strtol. */ - struct sticker_callback callback = { + struct sticker_callback cb = { .srv = srv, .c = c, .call = sticker_send_one_value, }; - - if (!database_sticker_get(srv->db, argv[0], argv[2], uri, &callback)) { - fprintf(stderr, " . command_sticker_get: Failed to get sticker '%s' for object %s(%d)\n", - argv[2], argv[0], uri); - return false; - } - + RETURN_UNLESS(database_sticker_get(srv->db, argv[0], argv[2], uri, &cb), "Failed to get sticker", false); return true; } bool command_sticker_set(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]) { - if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL || argv[3] == NULL || argv[4] != NULL) { - fprintf(stderr, " ! command_sticker_get: Invalid argument, need only 3 arguments\n"); - return false; - } - (void) c; + RETURN_UNLESS(argv[0] && argv[1] && argv[2] && argv[3] && !argv[4], "Invalid argument", false); int uri = atoi(argv[1]); /* FIXME: Use strtol. */ int value = atoi(argv[4]); /* FIXME: Use strtol. */ - - if (!database_sticker_set(srv->db, argv[0], argv[2], uri, value)) { - fprintf(stderr, " . command_sticker_get: Failed to get sticker '%s' to value %d for object " - "%s(%d)\n", argv[2], value, argv[0], uri); - return false; - } - + RETURN_UNLESS(database_sticker_set(srv->db, argv[0], argv[2], uri, value), "Failed to get sticker", false); srv->mpd_idle_events |= MPD_IDLE_STICKER; return true; } @@ -1252,12 +1017,8 @@ unknown: bool command_sticker_delete(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS_MAX]) { - if (argv[0] == NULL || argv[1] == NULL) { - fprintf(stderr, " . command_sticker_delete: Invalid argument\n"); - return false; - } - (void) c; + RETURN_UNLESS(argv[0] && argv[1], "Invalid argument", false); int uri = atoi(argv[1]); srv->mpd_idle_events |= MPD_IDLE_STICKER; return database_sticker_delete_specify(srv->db, argv[0], uri, argv[2]); diff --git a/src/net/listen.c b/src/net/listen.c index 5727ada3116dc8c76f4e6486c59c81ca3a27523b..0c3d28fcb18bbb02b9f11ff82dfce5016244d7e6 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -1,6 +1,8 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/commands.h> +#include <lektor/macro.h> +#include <lektor/defines.h> #include <lektor/database.h> #include <lektor/repo.h> #include <lektor/net.h> @@ -23,16 +25,6 @@ #include <unistd.h> #include <limits.h> -#ifndef LKT_BACKLOG -#define LKT_BACKLOG 32 -#endif - -#define COMMAND_LIST_MAX 64 - -#define BUFFER_OUT_MAX 16 - -#define MPD_VERSION "0.21.16" - enum lkt_command_list_mode { LKT_COMMAND_LIST_OFF, LKT_COMMAND_LIST_ON, @@ -85,12 +77,8 @@ void lkt_state_send(struct lkt_state *srv, size_t c, struct lkt_message *msg) { struct lkt_client *cli = &srv->clients[c - 1]; - - if (cli->buffer_out_len >= BUFFER_OUT_MAX) { - fprintf(stderr, " ! lkt_state_send: cannot send message to client, buffer_out is full.\n"); - return; - } - + RETURN_IF(cli->buffer_out_len >= BUFFER_OUT_MAX, "Cannot send message to client, buffer_out is full", + NOTHING); cli->buffer_out[cli->buffer_out_len++] = msg; srv->fds[c].events |= POLLOUT; } @@ -164,23 +152,15 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd) switch (*lkt_client_get_mask(srv, c)) { case MPD_IDLE_NONE: - /* Commands that require authentification. - TODO: Move authentification verification inside commands. */ - if (lkt_client_auth(srv, c, false)) { - if (!strcmp(cmd.name, "__adduser")) { - err = !command_user_add(srv->db, cmd.args); - break; - } else if (!strcmp(cmd.name, "__restart")) { - err = !command_restart(srv, c); - break; - } else if (!strcmp(cmd.name, "kill")) { - err = !command_kill(srv, c); - break; - } else if (!strcmp(cmd.name, "rescan")) { - err = !command_rescan(srv, c, cmd.args); - break; - } - } + /* Commands that requires authentification. */ + if (!strcmp(cmd.name, "__adduser")) + err = ! command_user_add(srv, c, srv->db, cmd.args); + else if (!strcmp(cmd.name, "__restart")) + err = ! command_restart(srv, c); + else if (!strcmp(cmd.name, "kill")) + err = ! command_kill(srv, c); + else if (!strcmp(cmd.name, "rescan")) + err = ! command_rescan(srv, c, cmd.args); /* Commands that are available if not in idle mode */ if (!strcmp(cmd.name, "currentsong")) @@ -760,84 +740,45 @@ get_out: int lkt_listen(void) { + char *memory = (char *) calloc(2 * PATH_MAX + HOST_NAME_MAX + 3, sizeof(char)); + RETURN_UNLESS(memory, "Out of memory", 5); + struct lkt_state srv; struct lkt_repo repo; int autoclear; - char *db_path = (char *) calloc(PATH_MAX, sizeof(char)); - char *kara_dir = (char *) calloc(PATH_MAX, sizeof(char)); - char *host = (char *) calloc(HOST_NAME_MAX, sizeof(char)); + char *const db_path = memory; /* Size is PATH_MAX. */ + char *const kara_dir = memory + PATH_MAX + 1; /* Size is PATH_MAX. */ + char *const host = kara_dir + PATH_MAX + 1; /* Size is HOST_NAME_MAX. */ char port[7]; /* Maximal port number is 65535, +2 for '\n' and '\0' */ char player_mod[INI_MAX_LINE]; char conf_file[PATH_MAX]; memset(&srv, 0, sizeof(struct lkt_state)); /* Initialize the system. */ - - if (!database_new(&srv.db)) { - fprintf(stderr, " ! lkt_listen: Failed to initialize memory database\n"); - return 1; - } - - if (config_detect_file(conf_file, PATH_MAX)) { - fprintf(stderr, " ! lkt_listen: error while searching for a config file\n"); - return 1; - } - - if (config_new(srv.db, conf_file)) { - fprintf(stderr, " ! lkt_listen: failed to read configuration file %s\n", conf_file); - return 1; - } + RETURN_UNLESS(database_new(&srv.db), "Failed to initialize the memory database", 1); + RETURN_UNLESS(config_detect_file(conf_file, PATH_MAX), "Failed to find a config file", 1); + RETURN_UNLESS(config_new(srv.db, conf_file), "Failed to read configuration file", 1); /* Finish to initialize. */ - - if (!database_config_get_text(srv.db, "database", "db_path", db_path, PATH_MAX)) { - fprintf(stderr, " ! lkt_listen: Failed to get database path\n"); - goto end_free_strings; - } - - - if (!database_open(srv.db, db_path)) { - fprintf(stderr, " ! lkt_listen: Failed to open database\n"); - return 1; - } + RETURN_UNLESS(database_config_get_text(srv.db, "database", "db_path", db_path, PATH_MAX), "Cfg error", 2); + RETURN_UNLESS(database_open(srv.db, db_path), "Can't open database", 1); /* Read the configuration. */ - - if (!database_config_get_int(srv.db, "player", "autoclear", &autoclear)) { - fprintf(stderr, " ! Failed to get queue autoclear property\n"); - return 1; - } - - if (!database_config_get_text(srv.db, "database", "kara_dir", kara_dir, PATH_MAX)) { - fprintf(stderr, " ! lkt_listen: Failed to get kara directory\n"); - goto end_free_strings; - } - - if (!database_config_get_text(srv.db, "server", "host", host, HOST_NAME_MAX)) { - fprintf(stderr, " ! lkt_listen: Failed to get the host\n"); - goto end_free_strings; - } - - if (!database_config_get_text(srv.db, "server", "port", port, 5)) { - fprintf(stderr, " ! lkt_listen: Failed to get the port\n"); - goto end_free_strings; - } - - if (!database_config_get_text(srv.db, "player", "module", player_mod, INI_MAX_LINE)) { - fprintf(stderr, " ! lkt_listen: Failed to get the module for the player\n"); - goto end_free_strings; - } + RETURN_UNLESS(database_config_get_int(srv.db, "player", "autoclear", &autoclear), "Cfg error", 2); + RETURN_UNLESS(database_config_get_text(srv.db, "database", "kara_dir", kara_dir, PATH_MAX), "Cfg error", 2); + RETURN_UNLESS(database_config_get_text(srv.db, "server", "host", host, HOST_NAME_MAX), "Cfg error", 2); + RETURN_UNLESS(database_config_get_text(srv.db, "server", "port", port, 5), "Cfg error", 2); + RETURN_UNLESS(database_config_get_text(srv.db, "player", "module", player_mod, INI_MAX_LINE), "Cfg error", 2); if (kara_dir[strlen(kara_dir) - 1] != '/') strncat(kara_dir, "/", PATH_MAX - 1); srv.kara_prefix = kara_dir; - database_config_queue_default(srv.db); srv.fds_max = 16; - srv.fds = malloc(srv.fds_max * sizeof(struct pollfd)); - srv.clients = malloc(srv.fds_max * sizeof(struct lkt_client)); + srv.fds = calloc(srv.fds_max, sizeof(struct pollfd)); + srv.clients = calloc(srv.fds_max, sizeof(struct lkt_client)); memset(srv.clients, 0, srv.fds_max * sizeof(struct lkt_client)); if ((srv.fds[0].fd = init_listening_socket(host, port)) < 0) @@ -849,17 +790,10 @@ lkt_listen(void) if (autoclear) database_queue_clear(srv.db); - if (!load_module_by_name(srv.db, player_mod, &srv.win)) - return -2; - - if (!srv.win.new(&srv.win)) - return -2; - - if (repo_new(&repo, "kurisu", "https://kurisu.iiens.net")) - return -11; - - if (repo_new_thread(&repo)) - return -10; + RETURN_UNLESS(load_module_by_name(srv.db, player_mod, &srv.win), "Can't load module", 3); + RETURN_UNLESS(srv.win.new(&srv.win), "Can't create window", 3); + RETURN_IF(repo_new(&repo, "kurisu", "https://kurisu.iiens.net"), "Failed to create repo", 4); + RETURN_IF(repo_new_thread(&repo), "Failed to launch repo thread", 4); for (;;) { if (handle_network_events(&srv) < 0) @@ -874,12 +808,4 @@ lkt_listen(void) srv.win.free(&srv.win); return -1; - - /* End and free strings used to get paths of the database. - There was a failure at some point while getting them. */ -end_free_strings: - free(kara_dir); - free(db_path); - free(host); - return -10; } diff --git a/src/repo/async.c b/src/repo/async.c index 8fdb2ce7756dffcb677eb6ccc215f48f40060d81..8bd48e0fe58a79b7033ecb35b42dba2c685cbae9 100644 --- a/src/repo/async.c +++ b/src/repo/async.c @@ -5,6 +5,7 @@ #include <unistd.h> #include <string.h> +#include <lektor/macro.h> #include <lektor/repo.h> #include <lektor/thread.h> #include <limits.h> @@ -22,11 +23,7 @@ int repo_join_thread(void) { int ret = 1; - - if (pthread_mutex_lock(&mtx)) { - fprintf(stderr, " ! repo_join_thread: Failed to lock mutex\n"); - return 3; - } + RETURN_IF(pthread_mutex_lock(&mtx), "Failed to lock mutex", 3); if (!init) { fprintf(stderr, " ! repo_join_thread: repo thread is not launched, can't join\n"); @@ -44,10 +41,7 @@ repo_join_thread(void) ret = 0; error: - if (pthread_mutex_unlock(&mtx)) { - fprintf(stderr, " ! repo_join_thread: Failed to unlock mutex\n"); - ret = 3; - } + RETURN_IF(pthread_mutex_unlock(&mtx), "Failed to unlock mutex", 3); return ret; } @@ -66,10 +60,7 @@ __handle_got_json(struct lkt_thread *self, struct lkt_repo *repo, struct json_ob struct kara *kara; int err; - if (len <= 0 || NULL == json_object_get_array(json)) { - fprintf(stderr, "__handle_got_json: Got json is invalid or the array is empty\n"); - return; - } + RETURN_UNLESS(len > 0 && json_object_get_array(json), "Json invalid or array empty", NOTHING); for (i = 0; i < len; ++i) { kara_json = json_object_array_get_idx(json, i); @@ -91,10 +82,7 @@ __handle_got_json(struct lkt_thread *self, struct lkt_repo *repo, struct json_ob kara->filename[PATH_MAX - 1] = 0; fprintf(stderr, " . __handle_got_json: Crafted filename is '%s'\n", kara->filename); - if (!kara) { - fprintf(stderr, " ! __handle_got_json: Out of memory\n"); - return; - } + RETURN_UNLESS(kara, "Out of memory", NOTHING); /* Get the fields from the json. */ err |= safe_json_get_string(kara_json, "song_name", kara->mdt.song_name, LEKTOR_TAG_MAX); @@ -216,25 +204,11 @@ end_loop: int repo_new_thread(struct lkt_repo *const repo) { - if (init) { - fprintf(stderr, " * repo_new_thread: Already running\n"); - return 1; - } - + RETURN_IF(init, "Already running", 1); struct lkt_thread_arg *arg = calloc(1, sizeof(struct lkt_thread_arg)); - - if (!arg) { - fprintf(stderr, " ! repo_new_thread: Out of memory\n"); - return errno = ENOMEM; - } - + RETURN_UNLESS(arg, "Out of memory", errno = ENOMEM); arg->args = repo; - - if (lkt_th_new(&repo_thread, LKT_DEFAULT_LIST_SIZE, __repo_thread_function, arg)) { - fprintf(stderr, " ! repo_new_thread: Failed to create the repo thread\n"); - return 1; - } - + RETURN_IF(lkt_th_new(&repo_thread, LKT_DEFAULT_LIST_SIZE, __repo_thread_function, arg), "Thread error", 1); init = 1; return 0; } @@ -242,11 +216,8 @@ repo_new_thread(struct lkt_repo *const repo) int repo_download_id_async(const size_t id) { - if (lkt_th_append_input(&repo_thread, (void *) id)) { - fprintf(stderr, " ! repo_download_id_async: Failed to push kara to download, id was %lu\n", id); - return 1; - } - + RETURN_IF(id == 0, "Invalid argument", 1); + RETURN_IF(lkt_th_append_input(&repo_thread, (void *) id), "Failed to push downloaded id", id); fprintf(stderr, " * Asked to download kara with id %lu\n", id); return 0; } @@ -270,17 +241,8 @@ err: inline int repo_get_allid_async(void) { - if (pthread_mutex_lock(&mtx)) { - fprintf(stderr, " ! repo_get_allid_async: Failed to lock mutex\n"); - return 3; - } - + RETURN_IF(pthread_mutex_lock(&mtx), "Failed to lock mutex", 3); all_json = 1; - - if (pthread_mutex_unlock(&mtx)) { - fprintf(stderr, " ! repo_get_allid_async: Failed to unlock mutex\n"); - return 3; - } - + RETURN_IF(pthread_mutex_unlock(&mtx), "Failed to lock mutex", 3); return 0; } diff --git a/src/repo/curl.c b/src/repo/curl.c index 0f4b8191e615353459a2d77669bf9dace72aa147..d613a7238fcc3368f01ffb85f2e43bdcf928a0a8 100644 --- a/src/repo/curl.c +++ b/src/repo/curl.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/repo.h> +#include <lektor/macro.h> #include <lektor/database.h> #include <errno.h> #include <stdlib.h> @@ -33,15 +34,11 @@ write_mem__(char *data, size_t size, size_t nmem, void *user) struct memory *mem = (struct memory *) user; void *ptr = realloc(mem->mem, mem->size + realsize); - if (ptr == NULL) { - fprintf(stderr, " ! write_mem__: not enough memory (realloc returned NULL)\n"); - return 0; - } + RETURN_UNLESS(ptr, "Out of memory", 0); mem->mem = ptr; memcpy(((uint8_t *) mem->mem) + mem->size, data, realsize); mem->size += realsize; - return realsize; } @@ -50,12 +47,7 @@ write_disk__(char *data, size_t size, size_t nmem, void *user) { ssize_t realsize = size * nmem; struct file *file = (struct file *) user; - - if (write(file->fd, data, realsize) != realsize) { - fprintf(stderr, " ! write_disk__: failed to write to '%s'\n", file->path); - return 0; - } - + RETURN_IF(write(file->fd, data, realsize) != realsize, "Failed to write", 0); return realsize; } @@ -70,14 +62,8 @@ repo_new(struct lkt_repo *const repo_, const char *name_, const char *url_) const size_t init_size = 30; uint64_t *calloc1 = calloc(init_size, sizeof(uint64_t)); - - if (!calloc1) { - fprintf(stderr, " ! repo_new: Out of memory\n"); - return ENOMEM; - } - + RETURN_UNLESS(calloc1, "Out of memory", errno = ENOMEM); uint64_t *calloc2 = calloc(init_size, sizeof(uint64_t)); - if (!calloc2) { free(calloc1); fprintf(stderr, " ! repo_new: Out of memory\n"); @@ -113,65 +99,32 @@ repo_free(struct lkt_repo *const repo) int safe_json_get_string(struct json_object *jobj, const char *key, char *content, const size_t len) { - int ret = 1; const char *got; struct json_object *field; - - if (!json_object_object_get_ex(jobj, key, &field)) { - fprintf(stderr, " ! safe_json_get_string: No object with key %s in json\n", key); - goto err; - } - + RETURN_UNLESS(json_object_object_get_ex(jobj, key, &field), "Key not found in json", 1); got = json_object_get_string(field); - - if (!got) { - fprintf(stderr, " ! safe_json_get_string: Got a NULL for key %s, may be an error\n", key); - goto err; - } - + RETURN_UNLESS(got, "Got a NULL for the key, may be an error", 1); strncpy(content, got, len - 1); content[len - 1] = 0; - - ret = 0; -err: - return ret; + return 0; } int safe_json_get_int32(struct json_object *json, const char *key, int32_t *ret) { struct json_object *field; - - if (!json_object_object_get_ex(json, key, &field)) { - fprintf(stderr, " . safe_json_get_int32: No object with key '%s' in json, error\n", key); - goto err; - } - + RETURN_UNLESS(json_object_object_get_ex(json, key, &field), "Key not found", 1); errno = 0; *ret = json_object_get_int(field); - - if (errno == EINVAL) { - fprintf(stderr, " . __handle_got_json: Invalid integer for field with key 'song_number'\n"); - goto err; - } - - if (*ret == INT32_MAX || *ret == INT32_MIN) { - fprintf(stderr, " . __handle_got_json: Out of bound integer for field with key 'song_number'\n"); - goto err; - } - + RETURN_IF(errno = EINVAL, "Invalid 32bit integer", 1); + RETURN_IF(*ret == INT32_MAX || *ret == INT32_MIN, "Out of bound 32bit integer", 1); return 0; -err: - return 1; } int repo_get_alljson_sync(struct lkt_repo *const repo, struct json_object **json) { - if (!json) { - fprintf(stderr, " ! repo_get_alljson_sync: Invalid argument\n"); - return 1; - } + RETURN_UNLESS(json, "Invalid argument", 1); CURL *curl_handle; CURLcode res; @@ -197,7 +150,6 @@ repo_get_alljson_sync(struct lkt_repo *const repo, struct json_object **json) } *json = json_tokener_parse(file.mem); - ret = 0; err: curl_easy_cleanup(curl_handle); @@ -212,15 +164,9 @@ repo_get_id(struct lkt_repo *const repo, const uint64_t id, struct kara_metadata struct json_object *jobj, *field; int ret = 1, err = 0; - if (!mdt) - fprintf(stderr, " ! repo_get_id: invalid argument\n"); - + RETURN_UNLESS(mdt, "Invalid argument", 1); char *url = calloc(URL_MAX_LEN, sizeof(char)); - - if (!url) { - fprintf(stderr, " ! repo_get_id: out of memory\n"); - return ENOMEM; - } + RETURN_UNLESS(url, "Out of memory", errno = ENOMEM); struct memory file = { .mem = NULL, @@ -297,22 +243,13 @@ int repo_download_id_sync(struct lkt_repo *const repo, sqlite3 *db, const uint64_t id, const char *kara_path, struct kara_metadata *mdt_ret) { - if (!kara_path) { - fprintf(stderr, " ! repo_download_id_sync: Can't download kara to a NULL path\n"); - return 1; - } - + RETURN_UNLESS(kara_path, "Invalid argument", 1); struct kara_metadata mdt; CURL *curl_handle; int ret = 1, fd; char *url = calloc(URL_MAX_LEN, sizeof(char)); char *ct; - - if (!url) { - fprintf(stderr, " ! repo_download_id_sync: Out of memory\n"); - errno = ENOMEM; - return ENOMEM; - } + RETURN_UNLESS(url, "Out of memory", errno = ENOMEM); if (repo_get_id(repo, id, mdt_ret ? mdt_ret : &mdt)) { fprintf(stderr, " ! repo_download_id_sync: Failed to get kara metadata from kurisu\n"); diff --git a/src/uri.c b/src/uri.c index 68d34b210babb154e8c691d602f2e029a7121993..8219ef1a587bd20eb875d1c39363d32327bc45dd 100644 --- a/src/uri.c +++ b/src/uri.c @@ -19,10 +19,8 @@ lkt_uri_from(struct lkt_uri_t *ret, char *const str) len = strlen(str); // The minimal uri is id://1 which is a 6 characters long - if (len <= 5 * sizeof(char)) { - fprintf(stderr, " ! lkt_uri_from: candidate for uri is to short to be an uri: %s\n", str); + if (len <= 5 * sizeof(char)) return false; - } if (!strncasecmp(str, "fs", 2 * sizeof(char))) { ret->type = uri_fs; @@ -48,16 +46,12 @@ lkt_uri_from(struct lkt_uri_t *ret, char *const str) } else if (!strncasecmp(str, "query", 5 * sizeof(char))) { ret->type = uri_query; val = str + 5; - } else { - fprintf(stderr, " ! lkt_uri_from: unknown uri type for %s\n", str); + } else return false; - } // No place for the '://' separator - if (len - (val - str) < 3) { - fprintf(stderr, " ! lkt_uri_from: no separator '://' for uri %s\n", str); + if (len - (val - str) < 3) return false; - } val += 3;