Lexer actually lexes filenames now, and odats are made out of mapvariants
[henge/webcc.git] / src / apc / 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 <string.h> //strncmp
17 #include <errno.h> //errno
18 #include <ctype.h> //tolower
19 /* Posix */
20 #include <err.h> //warnx
21 #include <stdlib.h> //exit
22 #include <unistd.h> //chdir
23 #include <dirent.h> //opendir
24 #include <unistr.h> //unicode strings
25 /* Internal */
26 #include "parser.tab.h"
27 /* Public */
28 int scanner_init(void);
29 void scanner_quit(void);
30 int scanner(void);
31 int scanner_scanpixels(int*,int);
32 /* Private */
33 extern //lexer.c
34 int lexer_lexstring(const uint8_t*);
35 extern //lexer.c
36 void lexer_pushtok(int, int);
37 static
38 int dredge_current_depth(void);
39 /* Mem */
40 extern //lexer.c
41 struct dirent* lexer_direntpa[], **lexer_direntpp;
42 extern //SRC_DIR/bin/tools/apc.c
43 const char* cargs['Z'];
44 #ifndef DL_STACKSIZE
45 #define DL_STACKSIZE 64
46 #endif
47 #ifndef DL_CD_STACKSIZE
48 #define DL_CD_STACKSIZE DL_STACKSIZE //square tree
49 #endif
50 static
51 struct dirlist
52 { DIR* dirp;
53 struct dirent* child_directory_stack[DL_CD_STACKSIZE],** cds;
54 } directory_list_stack[DL_STACKSIZE + 1],* dls; //+1 for the root dir
55 static
56 FILE* current_open_file = NULL;
57
58 /* Directory Listing Stack
59 FILO Stack for keeping an open DIR* at each directory depth for treewalk.
60 This stack is depth-safe, checking its depth during push operations, but not
61 during pop operations, to ensure the thread doesn't open too many files at
62 once (512 in c runtime), or traverse too far through symbolic links.
63 A directory listing includes a DIR* and all DIR-typed entity in the directory
64 as recognized by dirent, populated externally (and optionally).
65 This stack behaves abnormally by incrementing its PUSH operation prior to
66 evaluation, and the POP operations after evaluation. This behavior allows
67 the 'DL_CURDEPTH' operation to map to the current element in the 'dl_stack'
68 array, and it is always treated as the "current depth". This also allows us
69 to init the root directory to 'directory_list_stack'[0] and pop it in a safe
70 and explicit manner.
71 */
72 #define DL_STACK (directory_list_stack)
73 #define DL_STACKP (dls)
74 #define DL_CD_STACK ((*DL_STACKP).child_directory_stack)
75 #define DL_CD_STACKP ((*DL_STACKP).cds)
76 #define DL_CURDIR() ((*DL_STACKP).dirp)
77 #define DL_LEN() (DL_STACKP - DL_STACK)
78 #define DL_CD_LEN() (DL_CD_STACKP - DL_CD_STACK)
79 #define DL_INIT() (DL_STACKP = DL_STACK)
80 #define DL_CD_INIT() (DL_CD_STACKP = DL_CD_STACK)
81 #define DL_POP() ((*DL_STACKP--).dirp)
82 #define DL_CD() (*DL_CD_STACKP)
83 #define DL_CD_CURNAME() (DL_CD()->d_name)
84 #define DL_CD_POP() (*--DL_CD_STACKP)
85 #define DL_PUSH(D) ((*++DL_STACKP).dirp = D)
86 #define DL_CD_PUSH(E) (*DL_CD_STACKP++ = E)
87
88
89 /* Initializer
90 Initializer expects a function pointer to its lexical analysis function.
91 Sets up stack pointers and returns boolean true if 'opendir' encounters an
92 error, or if dredge_current_depth returns boolean true.
93 */
94 int scanner_init
95 #define CWDSTR "./"
96 #define ROOTDIR (cargs['d'] ? cargs['d'] : CWDSTR)
97 ()
98 { DL_INIT();
99 DL_STACK[0].dirp = opendir(ROOTDIR);
100 if (current_open_file != NULL)
101 { fclose(current_open_file);
102 current_open_file = NULL;
103 }
104 printf("Root dir %s\n",ROOTDIR);
105 return !chdir(ROOTDIR) && (DL_STACK[0].dirp == NULL || dredge_current_depth() == -1);
106 }
107
108 /* Quit */
109 void scanner_quit
110 ()
111 { if (DL_CURDIR())
112 closedir(DL_CURDIR());
113 }
114
115 /* Scanner
116 The main driver of the scanner will advance the current treewalk state and
117 tokenize tree-based push/pop operations. It will call 'lexer_lex' to
118 tokenize directory names prior to making a push operation. safe checking for
119 all returns from the filesystem handler will exit on serious system errors.
120
121 after pushing a new directory to the directory list, the scanner will dredge
122 the directory and alphabetically sort all file entries into the lexer's file
123 array, while placing all subdirectory entries in the current depth's child
124 directory stack to be scanned later.
125
126 Returns the number of tokens generated on success, -1 on error.
127 */
128 int scanner
129 #define $($)#$ //stringifier
130 #ifdef _DIRENT_HAVE_D_NAMLEN
131 #define MAX_DNAME _D_ALLOC_NAMLEN(DL_CD())
132 #else
133 #define MAX_DNAME 1024
134 #endif
135 #define ERR_CHILD "Fatal: Maximum of " $(DL_CD_STACKSIZE) \
136 " child directories exceeded for directory at depth %i\n" \
137 ,DL_LEN()
138 #define ERR_DEPTH "Fatal: Maximum directory depth of " $(DL_STACKSIZE) \
139 " exceeded during directory scan\n"
140 #define ERR_DL "Fatal: Directory List Stack Corruption %x\n", DL_LEN()
141 ()
142 { int ntok = 0;
143 scan:
144 if (DL_CD_LEN() >= DL_CD_STACKSIZE)//fail if maxchildren exceeded
145 { fprintf(stderr, ERR_CHILD);
146 goto fail;
147 }
148 if (DL_CD_LEN() > 0) //There are entities to process
149 { if (DL_CD_POP() == NULL) //If the dirent is null, then the
150 goto libfail; //lib function in dirent has failed
151 ntok += lexer_lexstring(DL_CD_CURNAME()); //lex the directory name
152 if (DL_LEN() >= DL_STACKSIZE) //fail if maxdepth exceeded
153 { fprintf(stderr, ERR_DEPTH);
154 goto fail;
155 }
156 if (chdir(DL_CD_CURNAME())) //move into the new directory
157 goto libfail;
158 if (DL_CURDIR() == NULL) //open the cwd
159 goto libfail;
160 lexer_pushtok(CLOPEN, 0); //Push "Open Directory" token
161 ntok++;
162 return dredge_current_depth(); //Filter and sort the current depth
163 }
164 else if (DL_LEN() >= 0) //Any dirs left? (Including root)
165 { if (closedir(DL_POP())) //close the directory we just left
166 goto libfail;
167 if (DL_LEN() == -1) //If we just popped root,
168 goto done; //we're done
169 lexer_pushtok(CLCLOSE, 0); //Else push "Close Directory" token,
170 ntok++;
171 if (!chdir("..")) //move up a directory and
172 goto scan; //start over
173 }
174 fprintf(stderr, ERR_DL);
175 libfail:
176 perror("scanner: ");
177 fail:
178 return -1;
179 done:
180 return ntok;
181 }
182
183 /* Scan Pixels
184 Scans up to 'len' pixels from the current file into 'buf'.
185 Returns the number of pixels scanned from the file, or -1 on error
186 */
187 int scanner_scanpixels
188 ( int* buf,
189 int max_len
190 )
191 { static int col_len, row_len, row;
192 //Open the current file if not yet open
193 if (current_open_file == NULL)
194 { if ((current_open_file = fopen(DL_CD_CURNAME(),"rb")) == NULL)
195 { perror("fopen: ");
196 return -1;
197 }
198 //Verify file header, get row_len/col_len
199 //if (read_img_header(&row_len, &col_len))
200 //return -1;
201 row = 0;
202 }
203 //Read pixels into the buffer if there are rows left in the image
204 if (row++ < row_len)
205 //TODO: return read_img_pixels(buf, col_len);
206 printf("SCANPIXELS NOT IMPLEMENTED\n.");
207 //Close the file and return 0
208 fclose(current_open_file);
209 current_open_file = NULL;
210 return 0;
211 }
212
213 /* Directory Entity Sort and Filter (Dredge)
214 This filter removes all unhandled file types, and places any 'DT_DIR' type
215 files in the current Directory List's directory stack. Upon finishing,
216 the 'CE_STACK' is sorted alphabetically, and the current 'DL_CD_STACK' is
217 populated. Prints warnings for unhandled files.
218
219 Returns -1 if 'readdir' encounters an error, otherwise returns the number of
220 directory entries sent to the external 'lexer_direntpa' array.
221 */
222 typedef //so we can typecast dirent's 'alphasort()' to take const void*s
223 int (*qcomp)(const void*, const void*);
224 static inline
225 int dredge_current_depth
226 #define READDIR_ERROR (-1)
227 #define READDIR_DONE (0)
228 #define DPS_LEN() (lexer_direntpp - lexer_direntpa)
229 #define DPS_PUSH(E) (*lexer_direntpp++ = E)
230 ()
231 { struct dirent** direntpp = lexer_direntpa;
232 DIR* cwd = DL_CURDIR();
233 struct dirent* direntp;
234 DL_CD_INIT();
235 scan_next:
236 if ((direntp = readdir(cwd)) != NULL)
237 { switch (direntp->d_type)
238 { case DT_REG:
239 DPS_PUSH(direntp);
240 goto scan_next;
241 case DT_DIR:
242 if (*(direntp->d_name) == '.') //skip hidden files and relative dirs
243 goto scan_next;
244 DL_CD_PUSH(direntp);
245 goto scan_next;
246 case DT_UNKNOWN:
247 warnx("unknown file %s: ignoring", direntp->d_name);
248 default:
249 goto scan_next;
250 }
251 }
252 if (errno)
253 return -1;
254 qsort(lexer_direntpa, DPS_LEN(), sizeof direntp, (qcomp)alphasort);
255 return DPS_LEN();
256 }