From 3708b1d092b391603779110a3594ee1bb8565409 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 18 Feb 2024 15:44:37 +0000 Subject: [PATCH] refactor: Allow format strings in fatal error messages. --- src/audio_call.c | 2 +- src/audio_device.c | 2 +- src/autocomplete.c | 2 +- src/chat.c | 8 ++++---- src/conference.c | 12 ++++++------ src/configdir.c | 2 +- src/execute.c | 2 +- src/friendlist.c | 10 +++++----- src/game_base.c | 2 +- src/groupchats.c | 8 ++++---- src/line_info.c | 4 ++-- src/main.c | 48 ++++++++++++++++++++++----------------------- src/message_queue.c | 2 +- src/misc_tools.c | 4 ++-- src/prompt.c | 12 ++++++------ src/toxic.c | 18 +++++++++++------ src/toxic.h | 2 +- src/windows.c | 10 +++++----- 18 files changed, 78 insertions(+), 72 deletions(-) diff --git a/src/audio_call.c b/src/audio_call.c index 10b35a34e..221a1615a 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -993,7 +993,7 @@ static void realloc_calls(uint32_t n) Call *temp = realloc(CallControl.calls, n * sizeof(Call)); if (temp == NULL) { - exit_toxic_err("failed in realloc_calls", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in realloc_calls"); } CallControl.calls = temp; diff --git a/src/audio_device.c b/src/audio_device.c index a44ff8beb..6b49427b8 100644 --- a/src/audio_device.c +++ b/src/audio_device.c @@ -691,7 +691,7 @@ static void *poll_input(void *arg) int16_t *frame_buf = malloc(FRAME_BUF_SIZE * sizeof(int16_t)); if (frame_buf == NULL) { - exit_toxic_err("failed in thread_poll", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in thread_poll"); } while (1) { diff --git a/src/autocomplete.c b/src/autocomplete.c index 018594e67..b078c78df 100644 --- a/src/autocomplete.c +++ b/src/autocomplete.c @@ -139,7 +139,7 @@ static int complete_line_helper(ToxWindow *self, Toxic *toxic, const char *const char *sub = calloc(1, strlen(ubuf) + 1); if (sub == NULL) { - exit_toxic_err("failed in complete_line_helper", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in complete_line_helper"); } if (!s && !dir_search) { diff --git a/src/chat.c b/src/chat.c index 504fafdf7..9a5518082 100644 --- a/src/chat.c +++ b/src/chat.c @@ -1692,7 +1692,7 @@ static void chat_onInit(ToxWindow *self, Toxic *toxic) getmaxyx(self->window, y2, x2); if (y2 <= 0 || x2 <= 0) { - exit_toxic_err("failed in chat_onInit", FATALERR_CURSES); + exit_toxic_err(FATALERR_CURSES, "failed in chat_onInit"); } self->x = x2; @@ -1735,7 +1735,7 @@ static void chat_onInit(ToxWindow *self, Toxic *toxic) ctx->cqueue = calloc(1, sizeof(struct chat_queue)); if (ctx->log == NULL || ctx->hst == NULL || ctx->cqueue == NULL) { - exit_toxic_err("failed in chat_onInit", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in chat_onInit"); } line_info_init(ctx->hst); @@ -1761,7 +1761,7 @@ ToxWindow *new_chat(Tox *tox, uint32_t friendnum) ToxWindow *ret = calloc(1, sizeof(ToxWindow)); if (ret == NULL) { - exit_toxic_err("failed in new_chat", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_chat"); } ret->type = WINDOW_TYPE_CHAT; @@ -1812,7 +1812,7 @@ ToxWindow *new_chat(Tox *tox, uint32_t friendnum) Help *help = calloc(1, sizeof(Help)); if (stb == NULL || chatwin == NULL || help == NULL) { - exit_toxic_err("failed in new_chat", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_chat"); } ret->chatwin = chatwin; diff --git a/src/conference.c b/src/conference.c index 44a48897c..bd478f247 100644 --- a/src/conference.c +++ b/src/conference.c @@ -544,7 +544,7 @@ static void conference_update_name_list(uint32_t conferencenum) chat->name_list = malloc(chat->num_peers * sizeof(NameListEntry)); if (chat->name_list == NULL) { - exit_toxic_err("failed in conference_update_name_list", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in conference_update_name_list"); } uint32_t count = 0; @@ -703,7 +703,7 @@ static void update_peer_list(ToxWindow *self, Toxic *toxic, uint32_t conferencen ConferencePeer *old_peer_list = malloc(old_num_peers * sizeof(ConferencePeer)); if (!old_peer_list) { - exit_toxic_err("failed in update_peer_list", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in update_peer_list"); } if (chat->peer_list != NULL) { @@ -1261,7 +1261,7 @@ static void conference_onInit(ToxWindow *self, Toxic *toxic) getmaxyx(self->window, y2, x2); if (x2 <= 0 || y2 <= 0) { - exit_toxic_err("failed in conference_onInit", FATALERR_CURSES); + exit_toxic_err(FATALERR_CURSES, "failed in conference_onInit"); } ChatContext *ctx = self->chatwin; @@ -1275,7 +1275,7 @@ static void conference_onInit(ToxWindow *self, Toxic *toxic) ctx->log = calloc(1, sizeof(struct chatlog)); if (ctx->log == NULL || ctx->hst == NULL) { - exit_toxic_err("failed in conference_onInit", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in conference_onInit"); } line_info_init(ctx->hst); @@ -1366,7 +1366,7 @@ static ToxWindow *new_conference_chat(uint32_t conferencenum) ToxWindow *ret = calloc(1, sizeof(ToxWindow)); if (ret == NULL) { - exit_toxic_err("failed in new_conference_chat", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_conference_chat"); } ret->type = WINDOW_TYPE_CONFERENCE; @@ -1385,7 +1385,7 @@ static ToxWindow *new_conference_chat(uint32_t conferencenum) Help *help = calloc(1, sizeof(Help)); if (chatwin == NULL || help == NULL) { - exit_toxic_err("failed in new_conference_chat", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_conference_chat"); } ret->chatwin = chatwin; diff --git a/src/configdir.c b/src/configdir.c index 4cc5e7463..198c4de72 100644 --- a/src/configdir.c +++ b/src/configdir.c @@ -123,7 +123,7 @@ int create_user_config_dirs(char *path) char *logpath = malloc(strlen(path) + strlen(LOGDIR) + 1); if (fullpath == NULL || logpath == NULL) { - exit_toxic_err("failed in load_data_structures", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in load_data_structures"); } strcpy(fullpath, path); diff --git a/src/execute.c b/src/execute.c index 0203d45f4..1f4944165 100644 --- a/src/execute.c +++ b/src/execute.c @@ -224,7 +224,7 @@ static int parse_command(const char *input, char (*args)[MAX_STR_SIZE]) char *cmd = strdup(input); if (cmd == NULL) { - exit_toxic_err("failed in parse_command", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in parse_command"); } int num_args = 0; diff --git a/src/friendlist.c b/src/friendlist.c index fb936a273..1a6951e81 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -97,7 +97,7 @@ static void realloc_friends(int n) uint32_t *f_idx = realloc(Friends.index, n * sizeof(uint32_t)); if (f == NULL || f_idx == NULL) { - exit_toxic_err("failed in realloc_friends", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in realloc_friends"); } Friends.list = f; @@ -118,7 +118,7 @@ static void realloc_blocklist(int n) uint32_t *b_idx = realloc(Blocked.index, n * sizeof(uint32_t)); if (b == NULL || b_idx == NULL) { - exit_toxic_err("failed in realloc_blocklist", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in realloc_blocklist"); } Blocked.list = b; @@ -1346,7 +1346,7 @@ void friendlist_onInit(ToxWindow *self, Toxic *toxic) getmaxyx(self->window, y2, x2); if (y2 <= 0 || x2 <= 0) { - exit_toxic_err("failed in friendlist_onInit", FATALERR_CURSES); + exit_toxic_err(FATALERR_CURSES, "failed in friendlist_onInit"); } self->window_bar = subwin(self->window, WINDOW_BAR_HEIGHT, x2, y2 - 2, 0); @@ -1641,7 +1641,7 @@ ToxWindow *new_friendlist(void) ToxWindow *ret = calloc(1, sizeof(ToxWindow)); if (ret == NULL) { - exit_toxic_err("failed in new_friendlist", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_friendlist"); } ret->type = WINDOW_TYPE_FRIEND_LIST; @@ -1682,7 +1682,7 @@ ToxWindow *new_friendlist(void) Help *help = calloc(1, sizeof(Help)); if (help == NULL) { - exit_toxic_err("failed in new_friendlist", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_friendlist"); } ret->help = help; diff --git a/src/game_base.c b/src/game_base.c index 9fb641170..5cebe503a 100644 --- a/src/game_base.c +++ b/src/game_base.c @@ -843,7 +843,7 @@ static void game_onInit(ToxWindow *self, Toxic *toxic) getmaxyx(self->window, max_y, max_x); if (max_y <= 0 || max_x <= 0) { - exit_toxic_err("failed in game_onInit", FATALERR_CURSES); + exit_toxic_err(FATALERR_CURSES, "failed in game_onInit"); } self->window_bar = subwin(self->window, WINDOW_BAR_HEIGHT, max_x, max_y - 2, 0); diff --git a/src/groupchats.c b/src/groupchats.c index 19db6ea11..bbc17bbb6 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -2263,7 +2263,7 @@ static void groupchat_onInit(ToxWindow *self, Toxic *toxic) getmaxyx(self->window, y2, x2); if (x2 <= 0 || y2 <= 0) { - exit_toxic_err("failed in groupchat_onInit", FATALERR_CURSES); + exit_toxic_err(FATALERR_CURSES, "failed in groupchat_onInit"); } self->x = x2; @@ -2282,7 +2282,7 @@ static void groupchat_onInit(ToxWindow *self, Toxic *toxic) ctx->log = calloc(1, sizeof(struct chatlog)); if (ctx->log == NULL || ctx->hst == NULL) { - exit_toxic_err("failed in groupchat_onInit", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in groupchat_onInit"); } line_info_init(ctx->hst); @@ -2344,7 +2344,7 @@ static ToxWindow *new_group_chat(Tox *tox, uint32_t groupnumber, const char *gro ToxWindow *ret = calloc(1, sizeof(ToxWindow)); if (ret == NULL) { - exit_toxic_err("failed in new_group_chat", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_group_chat"); } ret->type = WINDOW_TYPE_GROUPCHAT; @@ -2373,7 +2373,7 @@ static ToxWindow *new_group_chat(Tox *tox, uint32_t groupnumber, const char *gro Help *help = calloc(1, sizeof(Help)); if (stb == NULL || chatwin == NULL || help == NULL) { - exit_toxic_err("failed in new_group_chat", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_group_chat"); } ret->chatwin = chatwin; diff --git a/src/line_info.c b/src/line_info.c index fcb103657..0b85271df 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -45,7 +45,7 @@ void line_info_init(struct history *hst) hst->line_root = calloc(1, sizeof(struct line_info)); if (hst->line_root == NULL) { - exit_toxic_err("failed in line_info_init", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in line_info_init"); } hst->line_start = hst->line_root; @@ -412,7 +412,7 @@ int line_info_add(ToxWindow *self, const Client_Config *c_config, bool show_time struct line_info *new_line = calloc(1, sizeof(struct line_info)); if (new_line == NULL) { - exit_toxic_err("failed in line_info_add", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in line_info_add"); } char frmt_msg[MAX_LINE_INFO_MSG_SIZE]; diff --git a/src/main.c b/src/main.c index 93593fabc..4e9f8656a 100644 --- a/src/main.c +++ b/src/main.c @@ -209,13 +209,13 @@ static void queue_init_message(const char *msg, ...) char **new_msgs = realloc(init_messages.msgs, sizeof(char *) * init_messages.num); if (new_msgs == NULL) { - exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "Failed in queue_init_message"); } new_msgs[i] = malloc(MAX_STR_SIZE); if (new_msgs[i] == NULL) { - exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "Failed in queue_init_message"); } snprintf(new_msgs[i], MAX_STR_SIZE, "%s", frmt_msg); @@ -587,20 +587,20 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne if (len == 0) { fclose(fp); - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); + exit_toxic_err(FATALERR_FILEOP, "failed in load_tox"); } char *data = malloc(len); if (data == NULL) { fclose(fp); - exit_toxic_err("failed in load_tox", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in load_tox"); } if (fread(data, len, 1, fp) != 1) { fclose(fp); free(data); - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); + exit_toxic_err(FATALERR_FILEOP, "failed in load_tox"); } const bool is_encrypted = tox_is_data_encrypted((uint8_t *) data); @@ -609,7 +609,7 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne if (run_opts->encrypt_data && is_encrypted) { fclose(fp); free(data); - exit_toxic_err("failed in load_tox", FATALERR_ENCRYPT); + exit_toxic_err(FATALERR_ENCRYPT, "failed in load_tox"); } Client_Data *client_data = &toxic->client_data; @@ -640,7 +640,7 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne if (plain == NULL) { fclose(fp); free(data); - exit_toxic_err("failed in load_tox", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in load_tox"); } while (true) { @@ -696,7 +696,7 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne fclose(fp); free(data); free(plain); - exit_toxic_err("tox_pass_decrypt() failed", pwerr); + exit_toxic_err(pwerr, "tox_pass_decrypt() failed"); } } @@ -718,7 +718,7 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne free(data); } else { /* Data file does not/should not exist */ if (file_exists(toxic->client_data.data_path)) { - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); + exit_toxic_err(FATALERR_FILEOP, "failed in load_tox"); } tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_NONE); @@ -730,7 +730,7 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne } if (store_data(toxic) == -1) { - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); + exit_toxic_err(FATALERR_FILEOP, "failed in load_tox"); } } @@ -743,7 +743,7 @@ static bool load_toxic(Toxic *toxic) struct Tox_Options *tox_opts = tox_options_new(&options_new_err); if (tox_opts == NULL) { - exit_toxic_err("tox_options_new returned fatal error", options_new_err); + exit_toxic_err(options_new_err, "tox_options_new returned fatal error"); } init_tox_options(toxic->run_opts, tox_opts); @@ -1020,7 +1020,7 @@ static void parse_args(Toxic *toxic, int argc, char *argv[]) client_data->data_path = malloc(strlen(optarg) + 1); if (client_data->data_path == NULL) { - exit_toxic_err("failed in parse_args", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in parse_args"); } strcpy(client_data->data_path, optarg); @@ -1028,7 +1028,7 @@ static void parse_args(Toxic *toxic, int argc, char *argv[]) client_data->block_path = malloc(strlen(optarg) + strlen("-blocklist") + 1); if (client_data->block_path == NULL) { - exit_toxic_err("failed in parse_args", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in parse_args"); } strcpy(client_data->block_path, optarg); @@ -1103,13 +1103,13 @@ static void parse_args(Toxic *toxic, int argc, char *argv[]) snprintf(run_opts->proxy_address, sizeof(run_opts->proxy_address), "%s", optarg); if (++optind > argc || argv[optind - 1][0] == '-') { - exit_toxic_err("Proxy error", FATALERR_PROXY); + exit_toxic_err(FATALERR_PROXY, "Proxy error"); } long int port = strtol(argv[optind - 1], NULL, 10); if (port <= 0 || port > MAX_PORT_RANGE) { - exit_toxic_err("Proxy error", FATALERR_PROXY); + exit_toxic_err(FATALERR_PROXY, "Proxy error"); } run_opts->proxy_port = port; @@ -1182,7 +1182,7 @@ static void init_default_data_files(Client_Data *client_data) char *user_config_dir = get_user_config_dir(); if (user_config_dir == NULL) { - exit_toxic_err("failed in init_default_data_files()", FATALERR_FILEOP); + exit_toxic_err(FATALERR_FILEOP, "failed in init_default_data_files()"); } int config_err = create_user_config_dirs(user_config_dir); @@ -1192,14 +1192,14 @@ static void init_default_data_files(Client_Data *client_data) client_data->block_path = strdup(BLOCKNAME); if (client_data->data_path == NULL || client_data->block_path == NULL) { - exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in init_default_data_files()"); } } else { client_data->data_path = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(DATANAME) + 1); client_data->block_path = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(BLOCKNAME) + 1); if (client_data->data_path == NULL || client_data->block_path == NULL) { - exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in init_default_data_files()"); } strcpy(client_data->data_path, user_config_dir); @@ -1267,7 +1267,7 @@ int main(int argc, char **argv) Toxic *toxic = toxic_init(); if (toxic == NULL) { - exit_toxic_err("failed in main", FATALERR_TOXIC_INIT); + exit_toxic_err(FATALERR_TOXIC_INIT, "failed in main"); } parse_args(toxic, argc, argv); @@ -1335,7 +1335,7 @@ int main(int argc, char **argv) #endif /* X11 */ if (!load_toxic(toxic)) { - exit_toxic_err("Failed in main", FATALERR_TOX_INIT); + exit_toxic_err(FATALERR_TOX_INIT, "Failed in main"); } if (run_opts->encrypt_data && !datafile_exists) { @@ -1373,7 +1373,7 @@ int main(int argc, char **argv) set_active_window_by_type(windows, WINDOW_TYPE_PROMPT); if (pthread_mutex_init(&Winthread.lock, NULL) != 0) { - exit_toxic_err("failed in main", FATALERR_MUTEX_INIT); + exit_toxic_err(FATALERR_MUTEX_INIT, "failed in main"); } #ifdef AUDIO @@ -1395,7 +1395,7 @@ int main(int argc, char **argv) /* AV thread */ if (pthread_create(&av_thread.tid, NULL, thread_av, (void *) toxic->av) != 0) { - exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); + exit_toxic_err(FATALERR_THREAD_CREATE, "failed in main"); } set_al_device(input, c_config->audio_in_dev); @@ -1411,12 +1411,12 @@ int main(int argc, char **argv) /* thread for ncurses UI */ if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) toxic) != 0) { - exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); + exit_toxic_err(FATALERR_THREAD_CREATE, "failed in main"); } /* thread for message queue */ if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *) toxic) != 0) { - exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); + exit_toxic_err(FATALERR_THREAD_CREATE, "failed in main"); } #ifdef PYTHON diff --git a/src/message_queue.c b/src/message_queue.c index a8c485584..e79eee118 100644 --- a/src/message_queue.c +++ b/src/message_queue.c @@ -51,7 +51,7 @@ void cqueue_add(struct chat_queue *q, const char *msg, size_t len, uint8_t type, struct cqueue_msg *new_m = calloc(1, sizeof(struct cqueue_msg)); if (new_m == NULL) { - exit_toxic_err("failed in cqueue_message", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in cqueue_message"); } snprintf(new_m->message, sizeof(new_m->message), "%s", msg); diff --git a/src/misc_tools.c b/src/misc_tools.c index e6a0c14a0..da907a1ae 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -370,7 +370,7 @@ size_t get_file_name(char *namebuf, size_t bufsize, const char *pathname) char *path = strdup(pathname); if (path == NULL) { - exit_toxic_err("failed in get_file_name", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in get_file_name"); } while (len >= 0 && pathname[len] == '/') { @@ -380,7 +380,7 @@ size_t get_file_name(char *namebuf, size_t bufsize, const char *pathname) char *finalname = strdup(path); if (finalname == NULL) { - exit_toxic_err("failed in get_file_name", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in get_file_name"); } const char *basenm = strrchr(path, '/'); diff --git a/src/prompt.c b/src/prompt.c index 996e8754e..0900c5bb8 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -602,14 +602,14 @@ void prompt_init_statusbar(Toxic *toxic, bool first_time_run) ToxWindow *self = toxic->home_window; if (self == NULL) { - exit_toxic_err("failed in prompt_init_statusbar", FATALERR_WININIT); + exit_toxic_err(FATALERR_WININIT, "failed in prompt_init_statusbar"); } int x2, y2; getmaxyx(self->window, y2, x2); if (y2 <= 0 || x2 <= 0) { - exit_toxic_err("failed in prompt_init_statusbar", FATALERR_CURSES); + exit_toxic_err(FATALERR_CURSES, "failed in prompt_init_statusbar"); } UNUSED_VAR(y2); @@ -697,7 +697,7 @@ static void prompt_onInit(ToxWindow *self, Toxic *toxic) getmaxyx(self->window, y2, x2); if (y2 <= 0 || x2 <= 0) { - exit_toxic_err("failed in prompt_onInit", FATALERR_CURSES); + exit_toxic_err(FATALERR_CURSES, "failed in prompt_onInit"); } ChatContext *ctx = self->chatwin; @@ -710,7 +710,7 @@ static void prompt_onInit(ToxWindow *self, Toxic *toxic) ctx->hst = calloc(1, sizeof(struct history)); if (ctx->log == NULL || ctx->hst == NULL) { - exit_toxic_err("failed in prompt_onInit", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in prompt_onInit"); } line_info_init(ctx->hst); @@ -730,7 +730,7 @@ ToxWindow *new_prompt(void) ToxWindow *ret = calloc(1, sizeof(ToxWindow)); if (ret == NULL) { - exit_toxic_err("failed in new_prompt", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_prompt"); } ret->num = -1; @@ -749,7 +749,7 @@ ToxWindow *new_prompt(void) Help *help = calloc(1, sizeof(Help)); if (stb == NULL || chatwin == NULL || help == NULL) { - exit_toxic_err("failed in new_prompt", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "failed in new_prompt"); } ret->chatwin = chatwin; diff --git a/src/toxic.c b/src/toxic.c index 263dcdd66..ec394c2bf 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -153,12 +153,17 @@ void exit_toxic_success(Toxic *toxic) exit(EXIT_SUCCESS); } -void exit_toxic_err(const char *errmsg, int errcode) +void exit_toxic_err(int errcode, const char *errmsg, ...) { endwin(); if (freopen("/dev/tty", "w", stderr)) { - fprintf(stderr, "Toxic session aborted with error code %d (%s)\n", errcode, errmsg); + va_list args; + va_start(args, errmsg); + vfprintf(stderr, errmsg, args); + va_end(args); + + fprintf(stderr, "; toxic session aborted with error code %d\n", errcode); } exit(EXIT_FAILURE); @@ -292,8 +297,9 @@ void init_term(const Client_Config *c_config, bool use_default_locale) if (!use_default_locale) { if (setlocale(LC_ALL, "") == NULL) - exit_toxic_err("Could not set your locale. Please check your locale settings or " - "disable unicode support with the -d flag.", FATALERR_LOCALE_NOT_SET); + exit_toxic_err(FATALERR_LOCALE_NOT_SET, + "Could not set your locale. Please check your locale settings or " + "disable unicode support with the -d flag."); } #endif @@ -401,13 +407,13 @@ static void queue_init_message(const char *msg, ...) char **new_msgs = realloc(init_messages.msgs, sizeof(char *) * init_messages.num); if (new_msgs == NULL) { - exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "Failed in queue_init_message"); } new_msgs[i] = malloc(MAX_STR_SIZE); if (new_msgs[i] == NULL) { - exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "Failed in queue_init_message"); } snprintf(new_msgs[i], MAX_STR_SIZE, "%s", frmt_msg); diff --git a/src/toxic.h b/src/toxic.h index 4ea38b29d..e40f8954a 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -101,7 +101,7 @@ void flag_interface_refresh(void); void set_window_refresh_rate(size_t refresh_rate); void exit_toxic_success(Toxic *toxic) __attribute__((__noreturn__)); -void exit_toxic_err(const char *errmsg, int errcode) __attribute__((__noreturn__)); +void exit_toxic_err(int errcode, const char *errmsg, ...) __attribute__((__noreturn__, format(printf, 2, 3))); int store_data(const Toxic *toxic); diff --git a/src/windows.c b/src/windows.c index 5597729df..2154bf67b 100644 --- a/src/windows.c +++ b/src/windows.c @@ -808,7 +808,7 @@ int64_t add_window(Toxic *toxic, ToxWindow *w) ToxWindow **tmp_list = (ToxWindow **)realloc(windows->list, (windows->count + 1) * sizeof(ToxWindow *)); if (tmp_list == NULL) { - exit_toxic_err("realloc() failed in add_window()", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "realloc(_, %d * sizeof(ToxWindow *)) failed in add_window()", windows->count + 1); } tmp_list[new_index] = w; @@ -885,7 +885,7 @@ void del_window(ToxWindow *w, Windows *windows, const Client_Config *c_config) ToxWindow **tmp_list = (ToxWindow **)realloc(windows->list, windows->count * sizeof(ToxWindow *)); if (tmp_list == NULL && windows->count != 0) { - exit_toxic_err("realloc() failed in del_window()", FATALERR_MEMORY); + exit_toxic_err(FATALERR_MEMORY, "realloc(_, %d * sizeof(ToxWindow *)) failed in del_window()", windows->count); } windows->list = tmp_list; @@ -903,7 +903,7 @@ void del_window(ToxWindow *w, Windows *windows, const Client_Config *c_config) void init_windows(Toxic *toxic) { if (COLS <= CHATBOX_HEIGHT + WINDOW_BAR_HEIGHT) { - exit_toxic_err("add_window() for prompt failed in init_windows", FATALERR_WININIT); + exit_toxic_err(FATALERR_WININIT, "add_window() for prompt failed in init_windows"); } toxic->home_window = new_prompt(); @@ -911,11 +911,11 @@ void init_windows(Toxic *toxic) const int win_num = add_window(toxic, toxic->home_window); if (win_num != 0) { // prompt window is always index 0 - exit_toxic_err("add_window() for prompt failed in init_windows", FATALERR_WININIT); + exit_toxic_err(FATALERR_WININIT, "add_window() for prompt failed in init_windows"); } if (add_window(toxic, new_friendlist()) != 1) { // friendlist is always index 1 - exit_toxic_err("add_window() for friendlist failed in init_windows", FATALERR_WININIT); + exit_toxic_err(FATALERR_WININIT, "add_window() for friendlist failed in init_windows"); } set_active_window_by_id(toxic->windows, win_num);