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
+65 -41
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,62 +75,83 @@ void _parser_y_fini (void)
yylex_destroy(); yylex_destroy();
} }
int _parser_y_include (const char *filename) /**
* parse the file located at filename
*/
int include_file(char *filename)
{ {
glob_t files; unsigned int p = __parser_y_private.stack_ptr + 1;
int i, ret; FILE *f;
ret = glob(filename, GLOB_ERR, NULL, &files); if (p >= MAX_INCLUDE_DEPTH)
if (ret)
{ {
const char *err; yyerror("max inclusion depth reached");
switch (ret)
{
case GLOB_NOSPACE:
err = "include files ran out of memory";
break;
case GLOB_ABORTED:
err = "include files aborted due to read error";
break;
case GLOB_NOMATCH:
err = "include files found no matches";
break;
default:
err = "unknown include files error";
}
yyerror(err);
return 1; return 1;
} }
for (i = 0; i < files.gl_pathc; i++) f = fopen(filename, "r");
if (!f)
{ {
FILE *f; yyerror("can't open include filename");
unsigned int p = __parser_y_private.stack_ptr + 1; return 0; /* ignore this error */
}
if (p >= MAX_INCLUDE_DEPTH) __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 ret = 0;
#ifdef HAVE_GLOB_H
{
glob_t files;
int i;
ret = glob(filename, GLOB_ERR, NULL, &files);
if (ret)
{ {
yyerror("max inclusion depth reached"); const char *err;
switch (ret)
{
case GLOB_NOSPACE:
err = "include files ran out of memory";
break;
case GLOB_ABORTED:
err = "include files aborted due to read error";
break;
case GLOB_NOMATCH:
err = "include files found no matches";
break;
default:
err = "unknown include files error";
}
globfree(&files);
yyerror(err);
return 1; return 1;
} }
f = fopen(files.gl_pathv[i], "r"); for (i = 0; i < files.gl_pathc; i++)
if (!f)
{ {
yyerror("can't open include filename"); if ((ret = include_file(files.gl_pathv[i])))
continue; {
break;
}
} }
globfree(&files);
__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); #else /* HAVE_GLOB_H */
return 0; /* if glob(3) is not available, try to load pattern directly */
ret = include_file(filename);
#endif /* HAVE_GLOB_H */
return ret;
} }
%} %}