6 #include "fileparser.tab.h"
9 int tok_dir(DIR*, char *);
11 #define MAX_TOK_LEN 256
13 #define MAX_PATH_LEN 64
14 static DIR* dp_stack[MAX_DIR_DEP];
16 static char path[MAX_PATH_LEN];
18 /* Setup stack and first directory to read from */
23 char cwd[MAX_TOK_LEN];
27 getcwd(cwd, MAX_TOK_LEN);
29 printf("|------cwd is %s------|\n", cwd);
30 if(!(dp = opendir(cwd)))
31 printf("opendir(cwd) failed in linit()\n");
34 /* Dont use ADD_PATH because it concats a "/", which is already
37 printf("dp_stack is %x, dsp is %x size is %d\n",*dp_stack, *dsp, sizeof dp);
46 char buf[MAX_TOK_LEN];
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
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);
58 printf("|------in yylex(), returning tok_t = %d | err = %s------|\n", tok_t, strerror(errno));
63 #define DSP_PUSH(_val) *++dsp = (_val)
66 tok_dir(DIR* dp, char buf[])
68 struct dirent* de; /* directory entry */
75 if((*dsp == NULL) || (de = readdir(*dsp)) == NULL)
78 { printf("Error:%s in tok_dir\n", strerror(errno));
81 if( (dsp - dp_stack) >= 0)
83 printf("Current directory is null, pop one off ");
84 #define DSP_POP() *dsp--
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)
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);
106 else if (de->d_type == DT_DIR )
107 { if ((dsp - dp_stack) >= MAX_DIR_DEP) /* We've opened to many directories */
109 printf("Too many directories!\n");
112 if(strcmp(de->d_name,".") == 0 || strcmp(de->d_name,"..") == 0)
114 printf("directory is %s \n", de->d_name);
118 printf("|------ dir_ent is directory %s, "\
119 "cwd = %s, path = %s ------|\n", de->d_name, get_current_dir_name(), path);
122 /* Add directory name to path */
123 #define ADD_PATH(_name) strcat(path, "/"); \
126 ADD_PATH(de->d_name);
128 tmp_dp = opendir(path);
130 { printf("opening the directory failed,"\
131 "errno = %s\n", strerror(errno));
142 printf("A file that is not a diretory or a regular file is unable to be tokenized: %s\n", de->d_name);