d9aa426e5f8b523dd4b8cd48de056f4cf7410e6d
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 int scanner_init(void);
28 void scanner_quit(void);
29 int scanner_scanpath(char const*);
30 int scanner_scandir(DIR*);
35 int lexer_lexfile(uint8_t*);
37 int lexer_lexdir(uint8_t*);
39 void lexer_closedir(void);
46 { if (apc_pstate
!= NULL
|| apc_cstate
!= NULL
)
48 apc_pstate
= yypstate_new();
49 apc_cstate
= yycstate_new();
50 return (apc_pstate
== NULL
|| apc_cstate
== NULL
);
54 Free initialized memory
58 { yypstate_delete(apc_pstate
);
59 yycstate_delete(apc_cstate
);
64 /* Scan the provided path
65 Changes working directory to the provided pathname and, if successful, sends
66 a directory stream of the provided path to scanner_scandir
69 ( char const* pathname
)
72 if ((dirp
= opendir(pathname
)) == NULL
|| errno
)
73 { fprintf(stderr
, "Path %s could not be accessed\n", pathname
);
78 return scanner_scandir(dirp
);
81 /* Scan directory stream
82 Recursively scans the provided directory, sending CLOPEN and CLCLOSE tokens
83 to the parser when entering new directories (classes)
88 struct dirent
* direntp
;
91 direntp
= readdir(dirp
);
95 { if (*(direntp
->d_name
) == '.') //skip hidden or relative files
96 goto scan_next_dirent
;
97 switch (direntp
->d_type
)
99 lexer_lexfile((uint8_t*)direntp
->d_name
);
100 goto scan_next_dirent
;
102 lexer_lexdir((uint8_t*)direntp
->d_name
); //lex the dirname
103 if (chdir(direntp
->d_name
)) //change to the specified dir
106 if ((cdirp
= opendir(".")) == NULL
|| errno
) //open it
108 if(scanner_scandir(cdirp
)) //scan the directory
110 if (chdir("..")) //return to the parent dir
112 lexer_closedir(); //push "Close Directory" token
113 goto scan_next_dirent
; //continue scan
115 warnx("unknown file %s: ignoring", direntp
->d_name
);
117 goto scan_next_dirent
;
120 return closedir(dirp
);
122 perror("scanner_scandir");