2 \brief lexical analyzer implementation for APC
3 \details lexer for tokenizing filenames from a directory root
6 ----------------------------------------------------------------------------*/
15 #include "fileparser.tab.h"
21 #define STACK_BUF_SIZE (BUF_SIZE >> 2)
22 static DIR* dp_stack_buf
[STACK_BUF_SIZE
];
23 static char path_buf
[BUF_SIZE
];
24 static char storage_buf
[BUF_SIZE
];
26 /* Setup stack and first directory to read from */
29 { char cwd_buf
[MAX_TOK_LEN
];
32 getcwd(path_buf
, MAX_TOK_LEN
);
33 printf("|------cwd is %s------|\n", path_buf
);
34 if(!(*dsp
= opendir(path_buf
)))
35 printf("opendir(cwd) failed in linit()\n");
38 printf("dp_stack is %x, dsp is %x size is %d\n",*dp_stack
, *dsp
, sizeof dp
);
43 /* Returns token identifier and sets yylval */
46 { static DIR** dsp
= &dp_stack_buf
;
48 char buf
[MAX_TOK_LEN
];
51 printf("|------in yylex(), calling tok_dir------|\n");
52 if((tok_t
= tok_dir(*dsp
, yylval
.str
)) == -1)
53 printf("tok_dir returned -1, something is broken\n");
54 printf("|------in yylex(), returning tok_t = %d | err = %s------|\n", tok_t
, strerror(errno
));
58 #define DSP_PUSH(_val) *++dsp = (_val)
61 tok_dir(DIR* dp
, char buf
[])
63 struct dirent
* de
; /* directory entry */
70 if((*dsp
== NULL
) || (de
= readdir(*dsp
)) == NULL
)
73 { printf("Error:%s in tok_dir\n", strerror(errno
));
76 if( (dsp
- dp_stack
) >= 0)
78 printf("Current directory is null, pop one off ");
79 #define DSP_POP() *dsp--
83 /* Remove directory that was popped from path */
84 #define SUB_PATH() tmp_path = strrchr(path, '/'); \
85 path_len = strlen(tmp_path); \
86 memset(tmp_path, 0, path_len)
95 else if(de
->d_type
== DT_REG
)
96 { printf("|------dir_ent is a file, "\
97 "setting yylval to %s------|\n", de
->d_name
);
98 memmove(buf
, de
->d_name
, MAX_TOK_LEN
);
101 else if (de
->d_type
== DT_DIR
)
102 { if ((dsp
- dp_stack
) >= MAX_DIR_DEP
) /* We've opened to many directories */
104 printf("Too many directories!\n");
107 if(strcmp(de
->d_name
,".") == 0 || strcmp(de
->d_name
,"..") == 0)
109 printf("directory is %s \n", de
->d_name
);
113 printf("|------ dir_ent is directory %s, "\
114 "cwd = %s, path = %s ------|\n", de
->d_name
, get_current_dir_name(), path
);
117 /* Add directory name to path */
118 #define ADD_PATH(_name) strcat(path, "/"); \
121 ADD_PATH(de
->d_name
);
123 tmp_dp
= opendir(path
);
125 { printf("opening the directory failed,"\
126 "errno = %s\n", strerror(errno
));
137 printf("A file that is not a diretory or a regular file is unable to be tokenized: %s\n", de
->d_name
);