starter: fallback include handling without glob(3).

This commit is contained in:
Tobias Brunner
2011-10-10 18:05:44 +02:00
parent d46f857511
commit e01fed7eb3
+47 -23
View File
@@ -17,7 +17,10 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef HAVE_GLOB_H
#include <glob.h> #include <glob.h>
#endif
#include "y.tab.h" #include "y.tab.h"
@@ -72,10 +75,44 @@ void _parser_y_fini (void)
yylex_destroy(); yylex_destroy();
} }
/**
* parse the file located at filename
*/
int include_file(char *filename)
{
unsigned int p = __parser_y_private.stack_ptr + 1;
FILE *f;
if (p >= MAX_INCLUDE_DEPTH)
{
yyerror("max inclusion depth reached");
return 1;
}
f = fopen(filename, "r");
if (!f)
{
yyerror("can't open include filename");
return 0; /* ignore this error */
}
__parser_y_private.stack_ptr++;
__parser_y_private.file[p] = f;
__parser_y_private.stack[p] = YY_CURRENT_BUFFER;
__parser_y_private.line[p] = 1;
__parser_y_private.filename[p] = strdup(filename);
yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
return 0;
}
int _parser_y_include (const char *filename) int _parser_y_include (const char *filename)
{
int ret = 0;
#ifdef HAVE_GLOB_H
{ {
glob_t files; glob_t files;
int i, ret; int i;
ret = glob(filename, GLOB_ERR, NULL, &files); ret = glob(filename, GLOB_ERR, NULL, &files);
if (ret) if (ret)
@@ -96,38 +133,25 @@ int _parser_y_include (const char *filename)
default: default:
err = "unknown include files error"; err = "unknown include files error";
} }
globfree(&files);
yyerror(err); yyerror(err);
return 1; return 1;
} }
for (i = 0; i < files.gl_pathc; i++) for (i = 0; i < files.gl_pathc; i++)
{ {
FILE *f; if ((ret = include_file(files.gl_pathv[i])))
unsigned int p = __parser_y_private.stack_ptr + 1;
if (p >= MAX_INCLUDE_DEPTH)
{ {
yyerror("max inclusion depth reached"); break;
return 1;
} }
f = fopen(files.gl_pathv[i], "r");
if (!f)
{
yyerror("can't open include filename");
continue;
}
__parser_y_private.stack_ptr++;
__parser_y_private.file[p] = f;
__parser_y_private.stack[p] = YY_CURRENT_BUFFER;
__parser_y_private.line[p] = 1;
__parser_y_private.filename[p] = strdup(files.gl_pathv[i]);
yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
} }
globfree(&files); globfree(&files);
return 0; }
#else /* HAVE_GLOB_H */
/* if glob(3) is not available, try to load pattern directly */
ret = include_file(filename);
#endif /* HAVE_GLOB_H */
return ret;
} }
%} %}