enable lexer
[henge/apc.git] / src / scanner.c
1 /*!@file
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
12 \date Aug 2016
13 ----------------------------------------------------------------------------*/
14 /* Standard */
15 #include <stdio.h> //print
16 #include <errno.h> //errno
17 /* Posix */
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
24 /* Internal */
25 #include "parser.tab.h"
26 /* Public */
27 int scanner_init(void);
28 void scanner_quit(void);
29 int scanner_scanpath(char const*);
30 int scanner_scandir(DIR*);
31 yypstate* apc_pstate;
32 yycstate* apc_cstate;
33 /* Private */
34 extern //lexer.rl
35 int lexer_lexfile(uint8_t const*);
36 #define PUSHTOK(T,L) yypush_parse(apc_pstate, T, L, apc_cstate)
37
38 /* Init
39 Establishes yy states
40 */
41 int scanner_init
42 ( void )
43 { if (apc_pstate != NULL || apc_cstate != NULL)
44 scanner_quit();
45 apc_pstate = yypstate_new();
46 apc_cstate = yycstate_new();
47 return (apc_pstate == NULL || apc_cstate == NULL);
48 }
49
50 /* Quit
51 Free initialized memory
52 */
53 void scanner_quit
54 ( void )
55 { yypstate_delete(apc_pstate);
56 yycstate_delete(apc_cstate);
57 apc_pstate = NULL;
58 apc_cstate = NULL;
59 }
60
61 /* Scan the provided path
62 Changes working directory to the provided pathname and, if successful, sends
63 a directory stream of the provided path to scanner_scandir
64 */
65 int scanner_scanpath
66 ( char const* pathname )
67 { DIR* dirp;
68 errno = 0;
69 if ((dirp = opendir(pathname)) == NULL || errno)
70 { fprintf(stderr, "Path %s could not be accessed\n", pathname);
71 return -1;
72 }
73 if (chdir(pathname))
74 return -1;
75 return scanner_scandir(dirp);
76 }
77
78 /* Scan directory stream
79 Recursively scans the provided directory, sending CLOPEN and CLCLOSE tokens
80 to the parser when entering new directories (classes)
81 */
82 int scanner_scandir
83 ( DIR* dirp )
84 { DIR* cdirp;
85 struct dirent* direntp;
86 scan_next_dirent:
87 errno = 0;
88 direntp = readdir(dirp);
89 if (errno)
90 goto libfail;
91 if (direntp != NULL)
92 { if (*(direntp->d_name) == '.') //skip hidden or relative files
93 goto scan_next_dirent;
94 switch (direntp->d_type)
95 { case DT_REG:
96 printf("lexfile %s\n",direntp->d_name);
97 lexer_lexfile((uint8_t*)direntp->d_name);
98 goto scan_next_dirent;
99 case DT_DIR:
100 lexer_lexfile((uint8_t*)direntp->d_name); //lex the dirname
101 printf("lexdir %s\n",direntp->d_name);
102 if (chdir(direntp->d_name)) //change to the specified dir
103 goto libfail;
104 errno = 0;
105 if ((cdirp = opendir(".")) == NULL || errno) //open it
106 goto libfail;
107 PUSHTOK(CLOPEN, NULL); //push "Open Directory" token
108 printf("Scanner entered [%s]\n",direntp->d_name);
109 if(scanner_scandir(cdirp)) //scan the directory
110 goto libfail;
111 if (chdir("..")) //return to the parent dir
112 goto libfail;
113 PUSHTOK(CLCLOSE, NULL); //push "Close Directory" token
114 printf("Scanner returned\n");
115 goto scan_next_dirent; //continue scan
116 case DT_UNKNOWN:
117 warnx("unknown file %s: ignoring", direntp->d_name);
118 default:
119 goto scan_next_dirent;
120 }
121 }
122 return closedir(dirp);
123 libfail:
124 perror("scanner_scandir");
125 return -1;
126 }