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
)
71 { fprintf(stderr
, "Path %s could not be accessed\n", pathname
);
76 return scanner_scandir(dirp
);
79 /* Scan directory stream
80 Recursively scans the provided directory, sending CLOPEN and CLCLOSE tokens
81 to the parser when entering new directories (classes)
86 struct dirent
* direntp
;
89 direntp
= readdir(dirp
);
93 { if (*(direntp
->d_name
) == '.') //skip hidden or relative files
94 goto scan_next_dirent
;
95 switch (direntp
->d_type
)
97 printf("lexfile %s\n",direntp
->d_name
);
98 //lexer_lexfile((uint8_t*)direntp->d_name);
99 goto scan_next_dirent
;
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
106 if ((cdirp
= opendir(".")) == NULL
|| errno
) //open it
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
112 if (chdir("..")) //return to the parent dir
114 //PUSHTOK(CLCLOSE, 0); //push "Close Directory" token
115 printf("Scanner returned\n");
116 goto scan_next_dirent
; //continue scan
118 warnx("unknown file %s: ignoring", direntp
->d_name
);
120 goto scan_next_dirent
;
123 return closedir(dirp
);
125 perror("scanner_scandir");