++debugging for ragel
authorken <ken@mihrtec.com>
Sat, 8 Oct 2016 03:26:19 +0000 (20:26 -0700)
committerken <ken@mihrtec.com>
Sat, 8 Oct 2016 03:26:19 +0000 (20:26 -0700)
src/apc/scanner.c

index 580ad12..c669b10 100644 (file)
@@ -22,8 +22,9 @@
 
 #include "parser.tab.h"
 /* Public */
-int scanner_init(void);
-int scanner(void);
+int  scanner_init(void);
+void scanner_quit(void);
+int  scanner(void);
 /* Private */
 #ifndef DL_STACKSIZE
 #define DL_STACKSIZE     64
@@ -38,7 +39,7 @@ void lexer_pushtok(int, int);
 static
 int  dredge_current_depth(void);
 extern //lexer.c
-struct dirent* lexer_direntpa[];
+struct dirent* lexer_direntpa[], **lexer_direntpp;
 extern //SRC_DIR/bin/tools/apc.c
 const char* cargs['Z'];
 
@@ -87,7 +88,15 @@ int scanner_init
 ()
 { DL_INIT();
   DL_STACK[0].dirp = opendir(ROOTDIR);
-  return !chdir(ROOTDIR) && (DL_STACK[0].dirp == NULL || dredge_current_depth() == 0);
+  printf("Root dir %s\n",ROOTDIR);
+  return !chdir(ROOTDIR) && (DL_STACK[0].dirp == NULL || dredge_current_depth() == -1);
+}
+
+/* Quit */
+void scanner_quit
+()
+{ if (DL_CURDIR())
+    closedir(DL_CURDIR());
 }
 
 /* Scanner
@@ -99,9 +108,10 @@ int scanner_init
    after pushing a new directory to the directory list, the scanner will dredge
    the directory and alphabetically sort all file entries into the lexer's file
    array, while placing all subdirectory entries in the current depth's child
-   directory stack to the scanned later.
+   directory stack to be scanned later.
 
-   Returns the number of elements added to the lexer's file array.
+   Returns the number of elements added to the lexer's file array, or -1 on
+   error
 */
 int scanner
 #define $($)#$ //stringifier
@@ -111,8 +121,8 @@ int scanner
 #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
+#define TOK_CLOPEN  0x55, 1 //TODO
+#define TOK_CLCLOSE 0x56, 1 //TODO
 ()
 { struct dirent* direntp;
   struct DIR* DIRp;
@@ -121,30 +131,31 @@ int scanner
     { fprintf(stderr, ERR_CHILD);
       goto fail;
     }
-  if (DL_CD_LEN() > 0)             //There are entities to process at this depth
-    { 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
+  if (DL_CD_LEN() > 0)               //There are entities to process
+    { if ((direntp = DL_CD_POP()) == NULL)//If the dirent is null, the library
+        goto libfail;                     //function in dirent has failed
+      printf("Lexdir %s\n",direntp->d_name);
+      lexer_lex(direntp->d_name);         //lex the directory name
+      if (DL_LEN() >= DL_STACKSIZE)       //fail if maxdepth exceeded
         { fprintf(stderr, ERR_DEPTH);
           goto fail;
         }
-      if (chdir(direntp->d_name))     //move into the new directory
+      if (chdir(direntp->d_name))         //move into the new directory
        goto libfail;
       DL_PUSH(opendir(CWDSTR));
-      if (DL_CURDIR() == NULL)   //open the cwd
+      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
+      lexer_pushtok(TOK_CLOPEN);          //Push "Open Directory" token
+      return dredge_current_depth();      //Filter and sort the current depth
     }
-  else if (DL_LEN() >= 0)       //Any dirs left? (Including root)
-    { if (closedir(DL_POP()))         //close the directory we just left
+  else if (DL_LEN() >= 0)            //Any dirs left? (Including root)
+    { if (closedir(DL_POP()))             //close the directory we just left
         goto libfail;
-      lexer_pushtok(TOK_CLCLOSE);     //Push "Close Directory" token
-      if (DL_LEN() == -1)             //If we just popped root, we're done
-        return 0;
-      if (!chdir(".."))               //Move up a directory and start over
-        goto parse;
+      if (DL_LEN() == -1)                 //If we just popped root,
+        return 0;                         //we're done
+      lexer_pushtok(TOK_CLCLOSE);         //Else push "Close Directory" token,
+      if (!chdir(".."))                   //move up a directory and
+        goto parse;                       //start over
     }
   fprintf(stderr, ERR_DL);
  libfail:
@@ -168,8 +179,8 @@ static inline
 int dredge_current_depth
 #define READDIR_ERROR (-1)
 #define READDIR_DONE  (0)
-#define DPS_LEN()     (direntpp - lexer_direntpa)
-#define DPS_PUSH(E)   (*direntpp++ = E)
+#define DPS_LEN()     (lexer_direntpp - lexer_direntpa)
+#define DPS_PUSH(E)   (*lexer_direntpp++ = E)
 ()
 { struct dirent**  direntpp = lexer_direntpa;
   DIR*             cwd      = DL_CURDIR();
@@ -179,13 +190,11 @@ 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:
            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: