diff --git a/main.c b/main.c index 30f5d49..f90db9f 100644 --- a/main.c +++ b/main.c @@ -12,9 +12,15 @@ #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_LUA_FILENAME_SIZE 128 +#define OCT_VERSION "0.0" +#define OCT_URL "https://git.thejerks.club/j4nk/open-card-table" + int oct_type; @@ -27,6 +33,8 @@ struct { char lua_file[OCT_LUA_FILENAME_SIZE]; } args; +void usage(); +void version(); int process_args(int argc, char* argv[]); int initialize_everything(char* lua_file); int deinitialize_everything(); @@ -35,13 +43,22 @@ int main(int argc, char* argv[]) { int process_args_result = process_args(argc, argv); switch (process_args_result) { case OCT_NO_LUA_FILE: - fprintf(stderr, "No lua file given\n"); + fprintf(stderr, "Error: No lua file given\n\n"); + usage(); return EXIT_FAILURE; break; case OCT_LUA_FILE_NOT_FOUND: - fprintf(stderr, "Could not open file: %s\n", argv[argc-1]); + fprintf(stderr, "Error: Could not open file: %s\n", argv[argc-1]); return EXIT_FAILURE; break; + case OCT_HELP: + usage(); + return EXIT_SUCCESS; + break; + case OCT_VERS: + version(); + return EXIT_SUCCESS; + break; } struct tb_event ev; int finish = 0; @@ -73,7 +90,7 @@ int process_args(int argc, char* argv[]) { // Set args.port to default strncpy(args.port, OCT_DEFAULT_PORT, 6); - for (int i = 1; i < argc-1; i++) { + for (int i = 1; i < argc; i++) { // argv's are guaranteed to be zero-terminated, so can use // strcmp instead of strncmp if (strcmp(argv[i], "-p") == 0) { @@ -82,6 +99,20 @@ int process_args(int argc, char* argv[]) { i++; } } + else if (strcmp(argv[i], "-h") == 0) { + return OCT_HELP; + } + else if (strcmp(argv[i], "-v") == 0) { + return OCT_VERS; + } + else { + if (i == argc-1) { + break; + } + else { + return OCT_INVALID_ARGUMENT; + } + } } strncpy(args.lua_file, argv[argc-1], OCT_LUA_FILENAME_SIZE); @@ -105,9 +136,7 @@ int initialize_everything(char* lua_file) { lua_getglobal(L, "oct_init"); lua_call(L, 0, 1); int type = lua_tointeger(L, -1); - //printf("TYPE: %d\n", type); if (type == OCT_TYPE_SERVER) { - // TODO make port a command line argument if (!oct_network_server_init(args.port)) { printf("Could not establish a socket on port %s\n", args.port); return 0; @@ -130,3 +159,19 @@ int deinitialize_everything() { oct_tb_sprite_list_deinitialize(); return 1; } + +void usage() { + fprintf(stderr, "Open Card Table, version %s\n", OCT_VERSION); + fprintf(stderr, "%s\n", OCT_URL); + fprintf(stderr, "\n"); + fprintf(stderr, "Usage: ./open_card_table [OPTIONS] FILE\n"); + fprintf(stderr, "\n"); + fprintf(stderr, " -p\tPort on which to listen\n"); + fprintf(stderr, " -v\tPrint version and exit\n"); + fprintf(stderr, " -h\tPrint this message and exit\n"); + fprintf(stderr, "\n"); +} + +void version() { + fprintf(stderr, "%s\n", OCT_VERSION); +}