Debugging text for lexer + directory spec CLI
[henge/webcc.git] / src / apc / scanner.c
index b039cda..580ad12 100644 (file)
 #include <stdlib.h> //exit
 #include <unistd.h> //chdir
 #include <dirent.h> //opendir
+
+#include "parser.tab.h"
 /* Public */
-int scanner_init();
+int scanner_init(void);
 int scanner(void);
 /* Private */
 #ifndef DL_STACKSIZE
@@ -37,7 +39,7 @@ static
 int  dredge_current_depth(void);
 extern //lexer.c
 struct dirent* lexer_direntpa[];
-extern //main.c
+extern //SRC_DIR/bin/tools/apc.c
 const char* cargs['Z'];
 
 struct dirlist
@@ -81,11 +83,11 @@ struct dirlist
 */
 int scanner_init
 #define CWDSTR  "./"
-#define ROOTDIR (cargs['r'] ? cargs['r'] : CWDSTR)
+#define ROOTDIR (cargs['d'] ? cargs['d'] : CWDSTR)
 ()
 { DL_INIT();
   DL_STACK[0].dirp = opendir(ROOTDIR);
-  return DL_STACK[0].dirp == NULL || dredge_current_depth() == 0;
+  return !chdir(ROOTDIR) && (DL_STACK[0].dirp == NULL || dredge_current_depth() == 0);
 }
 
 /* Scanner
@@ -102,24 +104,25 @@ int scanner_init
    Returns the number of elements added to the lexer's file array.
 */
 int scanner
-#define S(S)#S //stringifier
-#define ERR_CHILD  "Fatal: Maximum of " S(DL_CD_STACKSIZE)      \
+#define $($)#$ //stringifier
+#define ERR_CHILD  "Fatal: Maximum of " $(DL_CD_STACKSIZE)      \
   " child directories exceeded for directory at depth %i\n"     \
   ,DL_LEN()
-#define ERR_DEPTH  "Fatal: Maximum directory depth of " S(DL_STACKSIZE) \
+#define ERR_DEPTH  "Fatal: Maximum directory depth of " $(DL_STACKSIZE) \
   " exceeded during directory scan\n"
 #define ERR_DL     "Fatal: Directory List Stack Corruption %x\n", DL_LEN()
 #define TOK_CLOPEN  0x55, 0 //TODO
 #define TOK_CLCLOSE 0x56, 0 //TODO
 ()
 { struct dirent* direntp;
+  struct DIR* DIRp;
  parse:
   if (DL_CD_LEN() >= DL_CD_STACKSIZE)//fail if maxchildren exceeded
     { fprintf(stderr, ERR_CHILD);
       goto fail;
     }
   if (DL_CD_LEN() > 0)             //There are entities to process at this depth
-    { if (direntp = DL_CD_POP())      //If the dirent is null, the library
+    { if ((direntp = DL_CD_POP()) == NULL)  //If the dirent is null, the library
         goto libfail;                 //function in dirent has failed
       lexer_lex(direntp->d_name);     //lex the directory name
       if (DL_LEN() >= DL_STACKSIZE)   //fail if maxdepth exceeded
@@ -127,9 +130,10 @@ int scanner
           goto fail;
         }
       if (chdir(direntp->d_name))     //move into the new directory
-        goto libfail;
-      if (DL_PUSH(opendir(CWDSTR)))   //open the cwd
-        goto libfail;
+       goto libfail;
+      DL_PUSH(opendir(CWDSTR));
+      if (DL_CURDIR() == NULL)   //open the cwd
+       goto libfail;
       lexer_pushtok(TOK_CLOPEN);      //Push "Open Directory" token
       return dredge_current_depth();  //Filter and sort the current depth
     }
@@ -175,10 +179,14 @@ int dredge_current_depth
   if ((direntp = readdir(cwd)) != NULL)
     { switch (direntp->d_type)
         { case DT_REG:
+           printf("String to tokenize %s\n", direntp->d_name);
             DPS_PUSH(direntp);
             goto scan_next;
           case DT_DIR:
-            DL_CD_PUSH(direntp);
+           if (*(direntp->d_name) == '.') //skip hidden files and relative dirs
+              goto scan_next;
+            printf("Pushing child directory %s\n", direntp->d_name);
+           DL_CD_PUSH(direntp);
             goto scan_next;
           case DT_UNKNOWN:
             warnx("unknown file %s: ignoring", direntp->d_name);