X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Fscanner.c;h=d01be156a96d2812beeaef6ab635c0a8501bb109;hp=6dcbc9daf0d10d13e5c81b92d1e804c0b8598179;hb=25a23e5134b1d9649f0279a4028b887c9deb9987;hpb=423ac0e4bfbee80d7820c04a5355cd964a2ae826 diff --git a/src/scanner.c b/src/scanner.c index 6dcbc9d..d01be15 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -20,45 +20,26 @@ #include //chdir #include //opendir #include //unicode strings +#include //strlen /* Internal */ #include "parser.tab.h" /* Public */ -int scanner_init(void); void scanner_quit(void); int scanner_scanpath(char const*); int scanner_scandir(DIR*); -yypstate* apc_pstate; -yycstate* apc_cstate; /* Private */ -extern //lexer.c -int lexer_lexfile(uint8_t const*); +static +int scanner_scandir_r(DIR*); extern //lexer.rl -int lexer_lexstring(uint8_t const*, int); -#define PUSHTOK(T,L) yypush_parse(apc_pstate, T, L, apc_cstate) - -/* Init - Establishes yy states -*/ -int scanner_init -( void ) -{ if (apc_pstate != NULL || apc_cstate != NULL) - scanner_quit(); - apc_pstate = yypstate_new(); - apc_cstate = yycstate_new(); - return (apc_pstate != NULL && apc_cstate != NULL); -} - -/* Quit - Free initialized memory -*/ -void scanner_quit -( void ) -{ yypstate_delete(apc_pstate); - yycstate_delete(apc_cstate); - apc_pstate = NULL; - apc_cstate = NULL; -} - +int lexer_init(void); +extern //lexer.rl +void lexer_quit(void); +extern //lexer.rl +int lexer_lexfile(uint8_t*); +extern //lexer.rl +int lexer_lexdir(uint8_t*); +extern //lexer.rl +void lexer_closedir(void); /* Scan the provided path Changes working directory to the provided pathname and, if successful, sends a directory stream of the provided path to scanner_scandir @@ -68,7 +49,9 @@ int scanner_scanpath { DIR* dirp; errno = 0; if ((dirp = opendir(pathname)) == NULL || errno) - return -1; + { fprintf(stderr, "Path %s could not be accessed\n", pathname); + return -1; + } if (chdir(pathname)) return -1; return scanner_scandir(dirp); @@ -80,6 +63,17 @@ int scanner_scanpath */ int scanner_scandir ( DIR* dirp ) +{ int scandir_status; + if (lexer_init()) + return -1; + scandir_status = scanner_scandir_r(dirp); + lexer_quit(); + return scandir_status; +} + +static +int scanner_scandir_r +( DIR* dirp ) { DIR* cdirp; struct dirent* direntp; scan_next_dirent: @@ -92,25 +86,20 @@ int scanner_scandir goto scan_next_dirent; switch (direntp->d_type) { case DT_REG: - printf("lexfile %s\n",direntp->d_name); - //lexer_lexfile((uint8_t*)direntp->d_name); + lexer_lexfile((uint8_t*)direntp->d_name); goto scan_next_dirent; case DT_DIR: - //lexer_lexstring((uint8_t*)direntp->d_name); //lex the dirname - printf("lexdir %s\n",direntp->d_name); + lexer_lexdir((uint8_t*)direntp->d_name); //lex the dirname if (chdir(direntp->d_name)) //change to the specified dir goto libfail; errno = 0; if ((cdirp = opendir(".")) == NULL || errno) //open it goto libfail; - //PUSHTOK(CLOPEN, 0); //push "Open Directory" token - printf("Scanner entered [%s]\n",direntp->d_name); - if(scanner_scandir(cdirp)) //scan the directory + if(scanner_scandir_r(cdirp)) //scan the directory goto libfail; if (chdir("..")) //return to the parent dir goto libfail; - //PUSHTOK(CLCLOSE, 0); //push "Close Directory" token - printf("Scanner returned\n"); + lexer_closedir(); //push "Close Directory" token goto scan_next_dirent; //continue scan case DT_UNKNOWN: warnx("unknown file %s: ignoring", direntp->d_name);