X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Fapc.c;h=5bd846ddf9e77b9ac4837e1d86989ac389f65bb2;hp=5bd6d73afe95a91fd3ee211991dbf347c71b3473;hb=92a18528b9a7530558fd677f283802ff1bc31d42;hpb=25a23e5134b1d9649f0279a4028b887c9deb9987 diff --git a/src/apc.c b/src/apc.c index 5bd6d73..5bd846d 100644 --- a/src/apc.c +++ b/src/apc.c @@ -18,11 +18,16 @@ /* Posix */ #include //exit #include //getopt, sysconf +#include //opendir /* Internal */ +#include "apc.h" #include "parser.tab.h" //bison +#include "ir.h" +#include "print.h" #define DEFAULT_PAGESIZE 4096 -const char* cargs['Z'] = {0}; +const char** cargs; +const uint8_t* apc_package_name; long sys_pagesize; int main(int, char*[]); @@ -34,23 +39,21 @@ int scanner_init(void); extern //scanner.c void scanner_quit(void); extern //scanner.c -int scanner_scanpath(char const*); +int scanner_scandir(DIR*); extern //ir.c int ir_init(void); extern //ir.c int ir_linker(void); extern //ir.c int ir_condenser(void); +extern +void ir_test(void); + +static +char* getcwd_basename(void); -/* Main entry from terminal - parses the command line and kicks off recursive scanning -*/ -int main -( int argc, - char* argv[] -) #define $($)#$ //stringifier -#define MAXSTR 255 +#define MAXSTR 4096 #define MAXERR "-%c allows at most " $(MAXSTR) " input characters\n", opt #define OPTS "d:o:h-" #define USAGE "Usage %s [-d dir_root][-o output_file][-h]\n", argv[0] @@ -60,12 +63,32 @@ int main "\t\t-o\tOutput filename \t\t[a.asspak]\n" \ "\t\t-h\tPrint this help\n" #define DONE -1 -#define SCANPATH (cargs['d'] ? cargs['d'] : "./") +#undef eprintf_callback +#define free_and_exit(ecode) do { \ + free(cargs); \ + exit(ecode); \ + } while(0) +#define eprintf_callback(...) free_and_exit(EXIT_FAILURE) +/* Main entry from terminal + parses the command line and kicks off recursive scanning +*/ +int main +( int argc, + char* argv[] +) { int opt; - + DIR* dirp; + const char* scanpath; + char* path_iter; + cargs = (const char**) malloc('Z'); getopt: switch (opt = getopt(argc, argv, OPTS)) { case 'd' : + path_iter = optarg; + while (*path_iter++ != '\0'); + path_iter -= 2; + if (*path_iter == '/') + *path_iter = '\0'; case 'o' : if (strnlen(optarg, MAXSTR) != MAXSTR) { cargs[opt] = optarg; @@ -73,12 +96,11 @@ int main } fprintf(stderr, MAXERR); default : - fprintf(stderr, USAGE); - exit(EXIT_FAILURE); + eprintf(USAGE); case 'h' : printf(USAGE); printf(USAGE_LONG); - exit(EXIT_SUCCESS); + free_and_exit(EXIT_SUCCESS); case DONE: break; } @@ -86,13 +108,51 @@ int main sys_pagesize = DEFAULT_PAGESIZE; if (ir_init()) { perror("init"); - exit(EXIT_FAILURE); - } - if (scanner_scanpath(SCANPATH)) - { perror("scanner"); - exit(EXIT_FAILURE); + free_and_exit(EXIT_FAILURE); } + scanpath = cargs['d'] ? cargs['d'] : "./"; + errno = 0; + dirp = opendir(scanpath); + if (dirp == NULL || errno) + eprintf("Path %s could not be accessed: %s\n", scanpath, strerror(errno)); + errno = 0; + if (chdir(scanpath) || errno) + eprintf("Could not change directory to %s: %s\n", scanpath, strerror(errno)); + apc_package_name = (uint8_t*) getcwd_basename(); + if (scanner_scandir(dirp)) + eprintf(strerror(errno)); + ir_test(); ir_linker(); ir_condenser(); - exit(EXIT_SUCCESS); + free_and_exit(EXIT_SUCCESS); +} + +static +char* getcwd_basename +( void ) +{ char* path_iter,* path_buf,* path; + int pages; + path = NULL; + pages = 1; + try_cwd: + path_buf = (char*) malloc(MAXSTR * pages); + errno = 0; + getcwd(path_buf, MAXSTR * pages); + if (errno == ERANGE) + { pages++; + free(path_buf); + goto try_cwd; + } + else if (errno) + { perror(NULL); + free(path_buf); + return NULL; + } + path = path_iter = path_buf; + while (*path_iter != '\0') + { if (*path_iter == '/') + path = path_iter + 1; + path_iter++; + } + return path; }