2 \brief APC Directory Scanner
3 \details This hand-written parser/scanner traverses a directory tree and
4 tokenizes elements of the structure which correspond to APC grammar.
5 The parser is implemented as a 2D stack which populates a list of
6 child directories at each depth, handling only the leaf nodes
7 (regular files) of the directory open at the current depth to
8 conserve memory and speed up traversal.
9 The scanner works with the lexer to lexically analyze text, and
10 assumes the existence of an external 'lex' function
11 \author Jordan Lavatai
13 ----------------------------------------------------------------------------*/
15 #include <stdio.h> //print
16 #include <errno.h> //errno
18 #include <err.h> //warnx
19 #include <stdlib.h> //exit
20 #include <unistd.h> //chdir
21 #include <dirent.h> //opendir
22 #include <unistr.h> //unicode strings
23 #include <string.h> //strlen
25 #include "parser.tab.h"
27 void scanner_quit(void);
28 int scanner_scandir(DIR*);
31 int scanner_scandir_r(DIR*);
35 void lexer_quit(void);
37 int lexer_lexfile(uint8_t*);
39 int lexer_lexdir(uint8_t*);
41 void lexer_closedir(void);
43 /* Scan directory stream
44 Recursively scans the provided directory, sending CLOPEN and CLCLOSE tokens
45 to the parser when entering new directories (classes)
52 scandir_status
= scanner_scandir_r(dirp
);
54 return scandir_status
;
61 struct dirent
* direntp
;
64 direntp
= readdir(dirp
);
68 { if (*(direntp
->d_name
) == '.') //skip hidden or relative files
69 goto scan_next_dirent
;
70 switch (direntp
->d_type
)
72 lexer_lexfile((uint8_t*)direntp
->d_name
);
73 goto scan_next_dirent
;
75 lexer_lexdir((uint8_t*)direntp
->d_name
); //lex the dirname
76 if (chdir(direntp
->d_name
)) //change to the specified dir
79 if ((cdirp
= opendir(".")) == NULL
|| errno
) //open it
81 if(scanner_scandir_r(cdirp
)) //scan the directory
83 if (chdir("..")) //return to the parent dir
85 lexer_closedir(); //push "Close Directory" token
86 goto scan_next_dirent
; //continue scan
88 warnx("unknown file %s: ignoring", direntp
->d_name
);
90 goto scan_next_dirent
;
93 return closedir(dirp
);
95 perror("scanner_scandir");