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
24 #include "parser.tab.h"
26 int scanner_init(void);
27 void scanner_quit(void);
28 int scanner_scanpath(char const*);
29 int scanner_scandir(DIR*);
34 int lexer_lexfile(uint8_t const*);
36 int lexer_lexstring(uint8_t const*, int);
37 #define PUSHTOK(T,L) yypush_parse(apc_pstate, T, L, apc_cstate)
44 { if (apc_pstate
!= NULL
|| apc_cstate
!= NULL
)
46 apc_pstate
= yypstate_new();
47 apc_cstate
= yycstate_new();
48 return (apc_pstate
!= NULL
&& apc_cstate
!= NULL
);
52 Free initialized memory
56 { yypstate_delete(apc_pstate
);
57 yycstate_delete(apc_cstate
);
62 /* Scan the provided path
63 Changes working directory to the provided pathname and, if successful, sends
64 a directory stream of the provided path to scanner_scandir
67 ( char const* pathname
)
70 if ((dirp
= opendir(pathname
)) == NULL
|| errno
)
74 return scanner_scandir(dirp
);
77 /* Scan directory stream
78 Recursively scans the provided directory, sending CLOPEN and CLCLOSE tokens
79 to the parser when entering new directories (classes)
84 struct dirent
* direntp
;
87 direntp
= readdir(dirp
);
91 { if (*(direntp
->d_name
) == '.') //skip hidden or relative files
92 goto scan_next_dirent
;
93 switch (direntp
->d_type
)
95 printf("lexfile %s\n",direntp
->d_name
);
96 //lexer_lexfile((uint8_t*)direntp->d_name);
97 goto scan_next_dirent
;
99 //lexer_lexstring((uint8_t*)direntp->d_name); //lex the dirname
100 printf("lexdir %s\n",direntp
->d_name
);
101 if (chdir(direntp
->d_name
)) //change to the specified dir
104 if ((cdirp
= opendir(".")) == NULL
|| errno
) //open it
106 //PUSHTOK(CLOPEN, 0); //push "Open Directory" token
107 printf("Scanner entered [%s]\n",direntp
->d_name
);
108 if(scanner_scandir(cdirp
)) //scan the directory
110 if (chdir("..")) //return to the parent dir
112 //PUSHTOK(CLCLOSE, 0); //push "Close Directory" token
113 printf("Scanner returned\n");
114 goto scan_next_dirent
; //continue scan
116 warnx("unknown file %s: ignoring", direntp
->d_name
);
118 goto scan_next_dirent
;
121 return closedir(dirp
);
123 perror("scanner_scandir");