Revamped errors in main, much more concise now
This commit is contained in:
parent
f89129d82e
commit
1d6d8f69ca
86
main.c
86
main.c
|
@ -10,17 +10,6 @@
|
|||
#define TB_IMPL
|
||||
#include "termbox.h"
|
||||
|
||||
|
||||
// Errors
|
||||
#define OCT_NO_LUA_FILE 1
|
||||
#define OCT_LUA_FILE_NOT_FOUND 2
|
||||
#define OCT_LUA_FILE_MISSING_OCT_INIT 3
|
||||
#define OCT_HELP 4
|
||||
#define OCT_VERS 5
|
||||
#define OCT_INVALID_ARGUMENT 6
|
||||
#define OCT_NETWORK_ERROR 7
|
||||
#define OCT_LUA_FILE_ERROR 8
|
||||
|
||||
#define OCT_MAX_FILENAME_SIZE 1024
|
||||
|
||||
#define OCT_VERSION "0.0"
|
||||
|
@ -50,40 +39,7 @@ int finish = 0;
|
|||
void handle_sigint() {finish = 1;};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int process_args_result = process_args(argc, argv);
|
||||
switch (process_args_result) {
|
||||
case OCT_NO_LUA_FILE:
|
||||
// Can't use oct_log as logging is not guaranteed to be init
|
||||
fprintf(stderr, "Error: No lua file given\n\n");
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
break;
|
||||
case OCT_LUA_FILE_NOT_FOUND:
|
||||
// oct_log is init by this time, but don't want PITA bug in case this changes
|
||||
fprintf(stderr, "Error: Could not open file: %s\n", argv[argc-1]);
|
||||
deinitialize_everything();
|
||||
return EXIT_FAILURE;
|
||||
break;
|
||||
case OCT_INVALID_ARGUMENT:
|
||||
// Can't use oct_log as logging is not guaranteed to be init
|
||||
fprintf(stderr, "Error: unknown argument\n");
|
||||
return EXIT_FAILURE;
|
||||
break;
|
||||
case OCT_HELP:
|
||||
usage();
|
||||
return EXIT_SUCCESS;
|
||||
break;
|
||||
case OCT_VERS:
|
||||
version();
|
||||
return EXIT_SUCCESS;
|
||||
break;
|
||||
case OCT_NETWORK_ERROR:
|
||||
fprintf(stderr, "Error: network\n");
|
||||
deinitialize_everything();
|
||||
return EXIT_FAILURE;
|
||||
case OCT_LUA_FILE_ERROR:
|
||||
fprintf(stderr, "Error: given lua file had error\n");
|
||||
deinitialize_everything();
|
||||
if (!process_args(argc, argv)) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
@ -131,7 +87,10 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
int process_args(int argc, char* argv[]) {
|
||||
if (argc == 1) { // Didn't specify a file
|
||||
return OCT_NO_LUA_FILE;
|
||||
// Can't use oct_log as logging is not init
|
||||
fprintf(stderr, "Error: No lua file given\n\n");
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set config.port to default
|
||||
|
@ -151,14 +110,16 @@ int process_args(int argc, char* argv[]) {
|
|||
}
|
||||
else {
|
||||
fprintf(stderr, "Error: need to specify a port after -p\n");
|
||||
return OCT_INVALID_ARGUMENT;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (strcmp(argv[i], "-h") == 0) {
|
||||
return OCT_HELP;
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
else if (strcmp(argv[i], "-v") == 0) {
|
||||
return OCT_VERS;
|
||||
version();
|
||||
return 0;
|
||||
}
|
||||
else if (strcmp(argv[i], "-ll") == 0) {
|
||||
if (i+1 < argc) {
|
||||
|
@ -166,13 +127,13 @@ int process_args(int argc, char* argv[]) {
|
|||
log_level = strtoul(argv[i+1], &endptr, 10);
|
||||
if (log_level == 0 && endptr == argv[i+1]) {
|
||||
printf("Error: invalid log level\n");
|
||||
return OCT_INVALID_ARGUMENT;
|
||||
return 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Error: need to specify a log level after -ll\n");
|
||||
return OCT_INVALID_ARGUMENT;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (strcmp(argv[i], "-lf") == 0) {
|
||||
|
@ -183,7 +144,7 @@ int process_args(int argc, char* argv[]) {
|
|||
}
|
||||
else {
|
||||
fprintf(stderr, "Error: need to specify a log file after -lf\n");
|
||||
return OCT_INVALID_ARGUMENT;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -194,12 +155,13 @@ int process_args(int argc, char* argv[]) {
|
|||
}
|
||||
// Invalid argument
|
||||
else {
|
||||
return OCT_INVALID_ARGUMENT;
|
||||
fprintf(stderr, "Error: invalid argument %s\n", argv[i]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!valid_last_argument) {
|
||||
return OCT_NO_LUA_FILE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
oct_log_init(log_file_spec ? log_file : NULL, log_level);
|
||||
|
@ -213,7 +175,10 @@ int process_args(int argc, char* argv[]) {
|
|||
int initialize_everything(char* lua_file) {
|
||||
// Check if file exists
|
||||
if (access(lua_file, F_OK) != 0) {
|
||||
return OCT_LUA_FILE_NOT_FOUND;
|
||||
// oct_log is init by this time, but don't want PITA bug in case this changes
|
||||
fprintf(stderr, "Error: Could not open file: %s\n", lua_file);
|
||||
deinitialize_everything();
|
||||
return 0;
|
||||
}
|
||||
if (!oct_tb_sprite_list_initialize()) {
|
||||
return 0;
|
||||
|
@ -230,13 +195,15 @@ int initialize_everything(char* lua_file) {
|
|||
oct_log_init_lua(L);
|
||||
if (luaL_dofile(L, lua_file)) {
|
||||
OCT_LOG_ERROR("%s", luaL_checkstring(L, -1));
|
||||
return OCT_LUA_FILE_ERROR;
|
||||
deinitialize_everything();
|
||||
return 0;
|
||||
}
|
||||
OCT_LOG_INFO("Begin running oct_init()");
|
||||
lua_getglobal(L, "oct_init");
|
||||
if (lua_pcall(L, 0, 2, 0) != LUA_OK) {
|
||||
OCT_LOG_ERROR("%s", luaL_checkstring(L, -1));
|
||||
return OCT_LUA_FILE_ERROR;
|
||||
deinitialize_everything();
|
||||
return 0;
|
||||
}
|
||||
config.needs_networking = lua_tointeger(L, -2);
|
||||
config.needs_termbox = lua_tointeger(L, -1);
|
||||
|
@ -253,14 +220,15 @@ int initialize_everything(char* lua_file) {
|
|||
if (config.needs_networking) {
|
||||
if (!oct_network_node_init(config.port, L)) {
|
||||
OCT_LOG_ERROR("Could not establish a socket on port %s\n", config.port);
|
||||
return OCT_NETWORK_ERROR;
|
||||
deinitialize_everything();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (config.needs_termbox) {
|
||||
tb_init();
|
||||
}
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int deinitialize_everything() {
|
||||
|
|
Loading…
Reference in New Issue