Added usage and version switches

This commit is contained in:
j4nk 2023-05-22 09:57:01 -04:00
parent 522f64f68d
commit 3d2b590389
1 changed files with 50 additions and 5 deletions

55
main.c
View File

@ -12,9 +12,15 @@
#define OCT_NO_LUA_FILE 1 #define OCT_NO_LUA_FILE 1
#define OCT_LUA_FILE_NOT_FOUND 2 #define OCT_LUA_FILE_NOT_FOUND 2
#define OCT_LUA_FILE_MISSING_OCT_INIT 3 #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_LUA_FILENAME_SIZE 128
#define OCT_VERSION "0.0"
#define OCT_URL "https://git.thejerks.club/j4nk/open-card-table"
int oct_type; int oct_type;
@ -27,6 +33,8 @@ struct {
char lua_file[OCT_LUA_FILENAME_SIZE]; char lua_file[OCT_LUA_FILENAME_SIZE];
} args; } args;
void usage();
void version();
int process_args(int argc, char* argv[]); int process_args(int argc, char* argv[]);
int initialize_everything(char* lua_file); int initialize_everything(char* lua_file);
int deinitialize_everything(); int deinitialize_everything();
@ -35,13 +43,22 @@ int main(int argc, char* argv[]) {
int process_args_result = process_args(argc, argv); int process_args_result = process_args(argc, argv);
switch (process_args_result) { switch (process_args_result) {
case OCT_NO_LUA_FILE: 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; return EXIT_FAILURE;
break; break;
case OCT_LUA_FILE_NOT_FOUND: 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; return EXIT_FAILURE;
break; break;
case OCT_HELP:
usage();
return EXIT_SUCCESS;
break;
case OCT_VERS:
version();
return EXIT_SUCCESS;
break;
} }
struct tb_event ev; struct tb_event ev;
int finish = 0; int finish = 0;
@ -73,7 +90,7 @@ int process_args(int argc, char* argv[]) {
// Set args.port to default // Set args.port to default
strncpy(args.port, OCT_DEFAULT_PORT, 6); 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 // argv's are guaranteed to be zero-terminated, so can use
// strcmp instead of strncmp // strcmp instead of strncmp
if (strcmp(argv[i], "-p") == 0) { if (strcmp(argv[i], "-p") == 0) {
@ -82,6 +99,20 @@ int process_args(int argc, char* argv[]) {
i++; 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); 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_getglobal(L, "oct_init");
lua_call(L, 0, 1); lua_call(L, 0, 1);
int type = lua_tointeger(L, -1); int type = lua_tointeger(L, -1);
//printf("TYPE: %d\n", type);
if (type == OCT_TYPE_SERVER) { if (type == OCT_TYPE_SERVER) {
// TODO make port a command line argument
if (!oct_network_server_init(args.port)) { if (!oct_network_server_init(args.port)) {
printf("Could not establish a socket on port %s\n", args.port); printf("Could not establish a socket on port %s\n", args.port);
return 0; return 0;
@ -130,3 +159,19 @@ int deinitialize_everything() {
oct_tb_sprite_list_deinitialize(); oct_tb_sprite_list_deinitialize();
return 1; 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);
}