payredu.c (2360B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 // TODO enable this for GUI 4 //#include <dlfcn.h> 5 #include <signal.h> 6 #ifndef _WIN32 7 #include <unistd.h> 8 #endif 9 10 #include <book.h> 11 12 #define MAX_MEMORY 4064 13 #define WINDOW_WIDTH 512 14 #define WINDOW_HEIGHT 512 15 #define BUFFER_SIZE 256 16 17 int should_exit = 0; 18 19 void sig_handle() { 20 printf("Reloaded\n"); 21 system("date"); 22 should_exit = 1; 23 } 24 25 typedef void* (*module_main_func)(const char*, size_t); 26 27 #define die(...) \ 28 do { printf(__VA_ARGS__); \ 29 exit(-1); } while(0); 30 31 int main(int argc, char* argv[]) { 32 FILE* in = NULL; 33 char* file_to_open = NULL; 34 // FIXME this is for debugging, payredu will not open any file by default 35 if (argc == 1) file_to_open = "october-2023.txt"; 36 else for (int i = 1; i < argc; i++) 37 if (argv[i][0] == '-' && strlen(argv[i]) == 2) { 38 switch (argv[i][1]) { 39 case 'f': 40 file_to_open = argv[++i]; 41 break; 42 default: 43 printf("unknown flag: %s\n", argv[i]); 44 break; 45 } 46 } else die("invalid argument: %s", argv[i]); 47 48 if (file_to_open == NULL) 49 die("require a file to open, pass -f <filename>"); 50 51 in = fopen(file_to_open, "r"); 52 53 char* data = (char*)malloc(2048 * sizeof(char)); 54 size_t data_size = 0; 55 size_t c_read = 0; 56 while((c_read = fread(data + data_size + 0, 1, BUFFER_SIZE, in)) != 0) { 57 data_size += c_read; 58 } 59 if (ferror(in)) fprintf(stderr, "Error reading file\n"); 60 fprintf(stdout, "Startig loop\n"); 61 ledger_parse_data(data, data_size); 62 return 0; 63 } 64 65 66 /* 67 int main(int argc, char* argv[]) { 68 signal(SIGQUIT, sig_handle); 69 //while(1) { 70 void* module = dlopen("./libbook.so", RTLD_NOW); 71 while(module == NULL){ 72 fprintf(stderr, "Failed to load module. (%s)\n", dlerror()); 73 fprintf(stderr, "Press return to try again.\n"); 74 getchar(); 75 module = dlopen("./libbook.so", RTLD_NOW); 76 } 77 module_main_func module_main = dlsym(module, "module_main"); 78 FILE* in = fopen("october-2023.txt", "r"); 79 char* data = (char*)malloc(2048 * sizeof(char)); 80 size_t data_size = 0; 81 size_t c_read = 0; 82 while((c_read = fread(data + data_size + 0, 1, BUFFER_SIZE, in)) != 0) { 83 data_size += c_read; 84 } 85 if (ferror(in)) fprintf(stderr, "Error reading file\n"); 86 fprintf(stdout, "Startig loop\n"); 87 module_main(data, data_size); 88 89 while(should_exit == 0) { 90 sleep(1); 91 } 92 should_exit = 0; 93 dlclose(module); 94 fprintf(stderr, "Continue?\n"); 95 //} 96 97 return 0; 98 } 99 */