1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 11:33:28 +01:00

made error handling more consistent and added exit function

This commit is contained in:
Jfreegman 2013-09-11 00:02:27 -04:00
parent 2e84836f8c
commit 40dcfc82d2
6 changed files with 21 additions and 23 deletions

View File

@ -17,8 +17,6 @@
#include "friendlist.h" #include "friendlist.h"
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
extern ToxWindow *prompt; extern ToxWindow *prompt;
typedef struct { typedef struct {
@ -136,9 +134,10 @@ static void select_friend(Tox *m, wint_t key)
} else return; /* Bad key input */ } else return; /* Bad key input */
/* If we reach this something is wrong */ /* If we reach this something is wrong */
fprintf(stderr, "select_friend() failed. Aborting...\n");
endwin(); endwin();
tox_kill(m); tox_kill(m);
exit(2); exit(EXIT_FAILURE);
} }
static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)

View File

@ -60,9 +60,9 @@ static void init_term()
signal(SIGWINCH, on_window_resize); signal(SIGWINCH, on_window_resize);
#if HAVE_WIDECHAR #if HAVE_WIDECHAR
if (setlocale(LC_ALL, "") == NULL) { if (setlocale(LC_ALL, "") == NULL) {
printf("Could not set your locale, plese check your locale settings or" fprintf(stderr, "Could not set your locale, plese check your locale settings or"
"disable wide char support\n"); "disable wide char support\n");
exit(1); exit(EXIT_FAILURE);
} }
#endif #endif
initscr(); initscr();
@ -91,7 +91,7 @@ static Tox *init_tox()
{ {
/* Init core */ /* Init core */
Tox *m = tox_new(); Tox *m = tox_new();
if (!m) if (m == NULL)
return NULL; return NULL;
/* Callbacks */ /* Callbacks */
@ -184,7 +184,7 @@ int init_connection(Tox *m)
fp = fopen(SRVLIST_FILE, "r"); fp = fopen(SRVLIST_FILE, "r");
if (!fp) if (fp == NULL)
return 1; return 1;
char servers[MAXSERVERS][MAXLINE]; char servers[MAXSERVERS][MAXLINE];
@ -208,7 +208,7 @@ int init_connection(Tox *m)
char *port = strtok(NULL, " "); char *port = strtok(NULL, " ");
char *key = strtok(NULL, " "); char *key = strtok(NULL, " ");
if (!ip || !port || !key) if (ip == NULL || port == NULL || key == NULL)
return 3; return 3;
tox_IP_Port dht; tox_IP_Port dht;
@ -320,7 +320,7 @@ static void load_data(Tox *m, char *path)
fprintf(stderr, "malloc() failed.\n"); fprintf(stderr, "malloc() failed.\n");
fclose(fd); fclose(fd);
endwin(); endwin();
exit(1); exit(EXIT_FAILURE);
} }
if (fread(buf, len, 1, fd) != 1) { if (fread(buf, len, 1, fd) != 1) {
@ -328,7 +328,7 @@ static void load_data(Tox *m, char *path)
free(buf); free(buf);
fclose(fd); fclose(fd);
endwin(); endwin();
exit(1); exit(EXIT_FAILURE);
} }
tox_load(m, buf, len); tox_load(m, buf, len);
@ -349,7 +349,7 @@ static void load_data(Tox *m, char *path)
if ((st = store_data(m, path)) != 0) { if ((st = store_data(m, path)) != 0) {
fprintf(stderr, "Store messenger failed with return code: %d\n", st); fprintf(stderr, "Store messenger failed with return code: %d\n", st);
endwin(); endwin();
exit(1); exit(EXIT_FAILURE);
} }
} }
} }
@ -405,10 +405,11 @@ int main(int argc, char *argv[])
init_term(); init_term();
Tox *m = init_tox(); Tox *m = init_tox();
if (!m) {
if (m == NULL) {
endwin(); endwin();
fprintf(stderr, "Failed to initialize network. Aborting...\n"); fprintf(stderr, "Failed to initialize network. Aborting...\n");
exit(1); exit(EXIT_FAILURE);
} }
prompt = init_windows(m); prompt = init_windows(m);

View File

@ -10,11 +10,9 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "toxic_windows.h"
#include "prompt.h" #include "prompt.h"
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX uint8_t pending_requests[MAX_STR_SIZE][TOX_CLIENT_ID_SIZE]; // XXX
uint8_t num_requests = 0; // XXX uint8_t num_requests = 0; // XXX
@ -298,7 +296,7 @@ void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv)
store_data(m, DATA_FILE); store_data(m, DATA_FILE);
free(DATA_FILE); free(DATA_FILE);
tox_kill(m); tox_kill(m);
exit(0); exit(EXIT_SUCCESS);
} }
void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)

View File

@ -9,5 +9,3 @@ unsigned char *hex_string_to_bin(char hex_string[]);
void prompt_init_statusbar(ToxWindow *self, Tox *m); void prompt_init_statusbar(ToxWindow *self, Tox *m);
#endif /* end of include guard: PROMPT_H_UZYGWFFL */ #endif /* end of include guard: PROMPT_H_UZYGWFFL */

View File

@ -22,6 +22,9 @@
#define UNKNOWN_NAME "Unknown" #define UNKNOWN_NAME "Unknown"
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#ifndef TOXICVER #ifndef TOXICVER
#define TOXICVER "NOVER" //Use the -D flag to set this #define TOXICVER "NOVER" //Use the -D flag to set this
#endif #endif
@ -85,4 +88,3 @@ int add_window(Tox *m, ToxWindow w);
void del_window(ToxWindow *w); void del_window(ToxWindow *w);
void set_active_window(int ch); void set_active_window(int ch);
#endif #endif

View File

@ -10,7 +10,6 @@
#include "toxic_windows.h" #include "toxic_windows.h"
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
static ToxWindow windows[MAX_WINDOWS_NUM]; static ToxWindow windows[MAX_WINDOWS_NUM];
static ToxWindow *active_window; static ToxWindow *active_window;
@ -177,7 +176,8 @@ void set_next_window(int ch)
if (active_window == inf) { // infinite loop check if (active_window == inf) { // infinite loop check
endwin(); endwin();
exit(2); fprintf(stderr, "set_next_window() failed. Aborting...\n");
exit(EXIT_FAILURE);
} }
} }
} }
@ -195,9 +195,9 @@ ToxWindow *init_windows()
int n_prompt = add_window(m, new_prompt()); int n_prompt = add_window(m, new_prompt());
if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) { if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) {
fprintf(stderr, "add_window() failed.\n"); fprintf(stderr, "add_window() failed. Aborting...\n");
endwin(); endwin();
exit(1); exit(EXIT_FAILURE);
} }
prompt = &windows[n_prompt]; prompt = &windows[n_prompt];