X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Fapc.c;h=5bd846ddf9e77b9ac4837e1d86989ac389f65bb2;hp=2307ccc3f73b9dfa1916a2e7c20a9628a402170c;hb=HEAD;hpb=44adcae5601d919ab63f8241ebac4ddfc0bc69f5 diff --git a/src/apc.c b/src/apc.c index 2307ccc..5bd846d 100644 --- a/src/apc.c +++ b/src/apc.c @@ -17,33 +17,43 @@ #include //strndupa /* Posix */ #include //exit -#include //getopt +#include //getopt, sysconf +#include //opendir /* Internal */ -#include //bison - -const char* cargs['Z'] = {0}; +#include "apc.h" +#include "parser.tab.h" //bison +#include "ir.h" +#include "print.h" +#define DEFAULT_PAGESIZE 4096 +const char** cargs; +const uint8_t* apc_package_name; +long sys_pagesize; + int main(int, char*[]); -extern //bison -int yyparse(void); extern //lexer.c int lexer_init(void); +extern //scanner.c +int scanner_init(void); +extern //scanner.c +void scanner_quit(void); +extern //scanner.c +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); -extern //apc/parser.tab.c -YYSTYPE yylval; -extern //lexer.c -int lexer(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] @@ -53,13 +63,32 @@ int main "\t\t-o\tOutput filename \t\t[a.asspak]\n" \ "\t\t-h\tPrint this help\n" #define DONE -1 +#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 DONE: - break; - case 'd' : + { 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; @@ -67,18 +96,63 @@ 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; } - if (lexer_init()) - { perror("lexer"); - exit(EXIT_FAILURE); + if ((sys_pagesize = sysconf(_SC_PAGESIZE)) == 0) + sys_pagesize = DEFAULT_PAGESIZE; + if (ir_init()) + { perror("init"); + free_and_exit(EXIT_FAILURE); } - yyparse(); - exit(EXIT_SUCCESS); + 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(); + 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; +}