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