From 39da05201273a72f72a2ac9d011b84685869225d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Bernon?= Date: Wed, 15 Mar 2023 09:48:20 +0100 Subject: [PATCH] widl: Introduce new (open|close)_input_file helpers. --- tools/widl/parser.h | 3 ++- tools/widl/parser.l | 51 ++++++++++----------------------------------- tools/widl/widl.c | 27 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/tools/widl/parser.h b/tools/widl/parser.h index abe215de02f..81418fca160 100644 --- a/tools/widl/parser.h +++ b/tools/widl/parser.h @@ -37,6 +37,7 @@ int is_type(const char *name); int do_warning(const char *toggle, warning_list_t *wnum); int is_warning_enabled(int warning); -extern char *temp_name; +extern char *find_input_file( const char *name, const char *parent ); +extern FILE *open_input_file( const char *path ); #endif diff --git a/tools/widl/parser.l b/tools/widl/parser.l index 89383fe810e..92955516e9e 100644 --- a/tools/widl/parser.l +++ b/tools/widl/parser.l @@ -524,6 +524,7 @@ void pop_import(void) if (yyin) fclose( yyin ); yy_delete_buffer( YY_CURRENT_BUFFER ); yy_switch_to_buffer( state->buffer ); + free( input_name ); input_name = state->input_name; line_number = state->line_number; free( state ); @@ -532,10 +533,8 @@ void pop_import(void) void push_import( char *import_name ) { struct import_state *state; - FILE *f; - char *path, *name; struct import *import; - int ret; + FILE *file; state = xmalloc( sizeof(struct import_state )); list_add_head( &import_stack, &state->entry ); @@ -556,55 +555,27 @@ void push_import( char *import_name ) import->name = xstrdup( import_name ); list_add_tail( &imports, &import->entry ); - /* don't search for a file name with a path in the include directories, - * for compatibility with MIDL */ - if (strchr( import_name, '/' ) || strchr( import_name, '\\' )) - path = xstrdup( import_name ); - else if (!(path = wpp_find_include( import_name, input_name ))) - error_loc( "Unable to open include file %s\n", import_name ); - - input_name = path; + input_name = find_input_file( import_name, input_name ); + file = open_input_file( input_name ); line_number = 1; - name = make_temp_file( "widl-pp", NULL ); - if (!(f = fopen(name, "wt"))) - error("Could not open fd %s for writing\n", name); - - ret = wpp_parse( path, f ); - fclose( f ); - if (ret) exit(1); - - if((f = fopen(name, "r")) == NULL) - error_loc("Unable to open %s\n", name); - - yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE)); + yy_switch_to_buffer( yy_create_buffer( file, YY_BUF_SIZE ) ); } static void switch_to_acf(void) { - char *name; - int ret; - FILE *f; + FILE *file; if (yyin) fclose( yyin ); yy_delete_buffer( YY_CURRENT_BUFFER ); + free( input_name ); - input_name = acf_name; - acf_name = NULL; + input_name = xstrdup( acf_name ); + file = open_input_file( input_name ); line_number = 1; + acf_name = NULL; - name = make_temp_file( "widl-acf", NULL ); - if (!(f = fopen(name, "wt"))) - error("Could not open fd %s for writing\n", name); - - ret = wpp_parse(input_name, f); - fclose(f); - if (ret) exit(1); - - if((f = fopen(name, "r")) == NULL) - error_loc("Unable to open %s\n", name); - - yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE)); + yy_switch_to_buffer( yy_create_buffer( file, YY_BUF_SIZE ) ); } static void warning_disable(int warning) diff --git a/tools/widl/widl.c b/tools/widl/widl.c index 7ed3bfd1f8c..7c83b4b3ac0 100644 --- a/tools/widl/widl.c +++ b/tools/widl/widl.c @@ -893,3 +893,30 @@ static void rm_tempfile(void) unlink(typelib_name); remove_temp_files(); } + +char *find_input_file( const char *name, const char *parent ) +{ + char *path; + + /* don't search for a file name with a path in the include directories, for compatibility with MIDL */ + if (strchr( name, '/' ) || strchr( name, '\\' )) path = xstrdup( name ); + else if (!(path = wpp_find_include( name, parent ))) error_loc( "Unable to open include file %s\n", name ); + + return path; +} + +FILE *open_input_file( const char *path ) +{ + FILE *file; + char *name; + int ret; + + name = make_temp_file( "widl", NULL ); + if (!(file = fopen( name, "wt" ))) error_loc( "Could not open %s for writing\n", name ); + ret = wpp_parse( path, file ); + fclose( file ); + if (ret) exit( 1 ); + + if (!(file = fopen( name, "r" ))) error_loc( "Unable to open %s\n", name ); + return file; +}