apc initial implementation
[henge/webcc.git] / src / apc / filescanner.rl
1 #include <stdio.h>
2 #include <string.h>
3 #include <dirent.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include "fileparser.tab.h"
7
8 int linit(void);
9 int tok_dir(DIR*, char *);
10
11 #define MAX_TOK_LEN 256
12 #define MAX_DIR_DEP 8
13 #define MAX_PATH_LEN 64
14 static DIR* dp_stack[MAX_DIR_DEP];
15 static DIR** dsp;
16 static char path[MAX_PATH_LEN];
17
18 /* Setup stack and first directory to read from */
19 int
20 linit()
21 {
22 DIR *dp;
23 char cwd[MAX_TOK_LEN];
24
25 dsp = dp_stack;
26
27 getcwd(cwd, MAX_TOK_LEN);
28
29 printf("|------cwd is %s------|\n", cwd);
30 if(!(dp = opendir(cwd)))
31 printf("opendir(cwd) failed in linit()\n");
32
33 *dsp = dp;
34 /* Dont use ADD_PATH because it concats a "/", which is already
35 present with cwd */
36 strcat(path, cwd);
37 printf("dp_stack is %x, dsp is %x size is %d\n",*dp_stack, *dsp, sizeof dp);
38
39 return 0;
40 }
41
42 int
43 yylex()
44 {
45 int tok_t = 0;
46 char buf[MAX_TOK_LEN];
47 char* file_name;
48
49 /* Each call to tok_dir will return a file_name to be
50 tokenized. tok_dir calls readdir(), which saves the
51 position in the directory after returning a file_name
52 for the next call. */
53 printf("|------in yylex(), calling tok_dir------|\n");
54 if((tok_t = tok_dir(*dsp, buf)) == -1)
55 printf("tok_dir returned -1, something is broken\n");
56 yylval.str = strdup(buf);
57
58 printf("|------in yylex(), returning tok_t = %d | err = %s------|\n", tok_t, strerror(errno));
59
60 return tok_t;
61 }
62
63 #define DSP_PUSH(_val) *++dsp = (_val)
64
65 int
66 tok_dir(DIR* dp, char buf[])
67 {
68 struct dirent* de; /* directory entry */
69 static DIR *tmp_dp;
70 char *tmp_path;
71 int path_len;
72
73
74 tok_start:
75 if((*dsp == NULL) || (de = readdir(*dsp)) == NULL)
76 {
77 if(errno)
78 { printf("Error:%s in tok_dir\n", strerror(errno));
79 return errno;
80 }
81 if( (dsp - dp_stack) >= 0)
82 {
83 printf("Current directory is null, pop one off ");
84 #define DSP_POP() *dsp--
85
86 dp = DSP_POP();
87
88 /* Remove directory that was popped from path */
89 #define SUB_PATH() tmp_path = strrchr(path, '/'); \
90 path_len = strlen(tmp_path); \
91 memset(tmp_path, 0, path_len)
92
93 SUB_PATH();
94
95 goto tok_start;
96 }
97 return 0; /* Done */
98 }
99
100 else if(de->d_type == DT_REG)
101 { printf("|------dir_ent is a file, "\
102 "setting yylval to %s------|\n", de->d_name);
103 memmove(buf, de->d_name, MAX_TOK_LEN);
104 return FDAT;
105 }
106 else if (de->d_type == DT_DIR )
107 { if ((dsp - dp_stack) >= MAX_DIR_DEP) /* We've opened to many directories */
108 {
109 printf("Too many directories!\n");
110 return 1;
111 }
112 if(strcmp(de->d_name,".") == 0 || strcmp(de->d_name,"..") == 0)
113 {
114 printf("directory is %s \n", de->d_name);
115 goto tok_start;
116 }
117
118 printf("|------ dir_ent is directory %s, "\
119 "cwd = %s, path = %s ------|\n", de->d_name, get_current_dir_name(), path);
120
121
122 /* Add directory name to path */
123 #define ADD_PATH(_name) strcat(path, "/"); \
124 strcat(path, _name)
125
126 ADD_PATH(de->d_name);
127
128 tmp_dp = opendir(path);
129 if(tmp_dp == 0)
130 { printf("opening the directory failed,"\
131 "errno = %s\n", strerror(errno));
132 return -1;
133 }
134
135 DSP_PUSH(tmp_dp);
136
137 goto tok_start;
138
139 }
140 else
141 {
142 printf("A file that is not a diretory or a regular file is unable to be tokenized: %s\n", de->d_name);
143 return -1;
144 }
145
146
147
148 }