Commit c3aa4952 authored by Alexandre Julliard's avatar Alexandre Julliard

makedep: Add more helpers for file output and error handling.

parent cf34a967
...@@ -74,11 +74,12 @@ static const char *top_src_dir; ...@@ -74,11 +74,12 @@ static const char *top_src_dir;
static const char *top_obj_dir; static const char *top_obj_dir;
static const char *OutputFileName = "Makefile"; static const char *OutputFileName = "Makefile";
static const char *Separator = "### Dependencies"; static const char *Separator = "### Dependencies";
static const char *ProgramName; static const char *input_file_name;
static int input_line; static int input_line;
static FILE *output_file;
static const char Usage[] = static const char Usage[] =
"Usage: %s [options] [files]\n" "Usage: makedep [options] [files]\n"
"Options:\n" "Options:\n"
" -Idir Search for include files in directory 'dir'\n" " -Idir Search for include files in directory 'dir'\n"
" -Cdir Search for source files in directory 'dir'\n" " -Cdir Search for source files in directory 'dir'\n"
...@@ -88,6 +89,14 @@ static const char Usage[] = ...@@ -88,6 +89,14 @@ static const char Usage[] =
" -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n"; " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
#ifndef __GNUC__
#define __attribute__(x)
#endif
static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static int output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
/******************************************************************* /*******************************************************************
* fatal_error * fatal_error
*/ */
...@@ -95,6 +104,13 @@ static void fatal_error( const char *msg, ... ) ...@@ -95,6 +104,13 @@ static void fatal_error( const char *msg, ... )
{ {
va_list valist; va_list valist;
va_start( valist, msg ); va_start( valist, msg );
if (input_file_name)
{
fprintf( stderr, "%s:", input_file_name );
if (input_line) fprintf( stderr, "%d:", input_line );
fprintf( stderr, " error: " );
}
else fprintf( stderr, "makedep: error: " );
vfprintf( stderr, msg, valist ); vfprintf( stderr, msg, valist );
va_end( valist ); va_end( valist );
exit(1); exit(1);
...@@ -102,13 +118,33 @@ static void fatal_error( const char *msg, ... ) ...@@ -102,13 +118,33 @@ static void fatal_error( const char *msg, ... )
/******************************************************************* /*******************************************************************
* fatal_perror
*/
static void fatal_perror( const char *msg, ... )
{
va_list valist;
va_start( valist, msg );
if (input_file_name)
{
fprintf( stderr, "%s:", input_file_name );
if (input_line) fprintf( stderr, "%d:", input_line );
fprintf( stderr, " error:" );
}
else fprintf( stderr, "makedep: error:" );
perror( " " );
va_end( valist );
exit(1);
}
/*******************************************************************
* xmalloc * xmalloc
*/ */
static void *xmalloc( size_t size ) static void *xmalloc( size_t size )
{ {
void *res; void *res;
if (!(res = malloc (size ? size : 1))) if (!(res = malloc (size ? size : 1)))
fatal_error( "%s: error: Virtual memory exhausted.\n", ProgramName ); fatal_error( "Virtual memory exhausted.\n" );
return res; return res;
} }
...@@ -121,7 +157,7 @@ static void *xrealloc (void *ptr, size_t size) ...@@ -121,7 +157,7 @@ static void *xrealloc (void *ptr, size_t size)
void *res; void *res;
assert( size ); assert( size );
if (!(res = realloc( ptr, size ))) if (!(res = realloc( ptr, size )))
fatal_error( "%s: error: Virtual memory exhausted.\n", ProgramName ); fatal_error( "Virtual memory exhausted.\n" );
return res; return res;
} }
...@@ -131,7 +167,7 @@ static void *xrealloc (void *ptr, size_t size) ...@@ -131,7 +167,7 @@ static void *xrealloc (void *ptr, size_t size)
static char *xstrdup( const char *str ) static char *xstrdup( const char *str )
{ {
char *res = strdup( str ); char *res = strdup( str );
if (!res) fatal_error( "%s: error: Virtual memory exhausted.\n", ProgramName ); if (!res) fatal_error( "Virtual memory exhausted.\n" );
return res; return res;
} }
...@@ -170,6 +206,37 @@ static int strendswith( const char* str, const char* end ) ...@@ -170,6 +206,37 @@ static int strendswith( const char* str, const char* end )
return l >= m && strcmp(str + l - m, end) == 0; return l >= m && strcmp(str + l - m, end) == 0;
} }
/*******************************************************************
* output
*/
static int output( const char *format, ... )
{
int ret;
va_list valist;
va_start( valist, format );
ret = vfprintf( output_file, format, valist );
va_end( valist );
if (ret < 0) fatal_perror( "output" );
return ret;
}
/*******************************************************************
* output_filename
*/
static void output_filename( const char *name, int *column )
{
if (*column + strlen(name) + 1 > 100)
{
output( " \\\n" );
*column = 0;
}
*column += output( " %s", name );
}
/******************************************************************* /*******************************************************************
* get_extension * get_extension
*/ */
...@@ -278,7 +345,7 @@ static struct incl_file *find_include_file( const char *name ) ...@@ -278,7 +345,7 @@ static struct incl_file *find_include_file( const char *name )
* *
* Add an include file if it doesn't already exists. * Add an include file if it doesn't already exists.
*/ */
static struct incl_file *add_include( struct incl_file *pFile, const char *name, int line, int system ) static struct incl_file *add_include( struct incl_file *pFile, const char *name, int system )
{ {
struct incl_file *include; struct incl_file *include;
char *ext; char *ext;
...@@ -286,37 +353,29 @@ static struct incl_file *add_include( struct incl_file *pFile, const char *name, ...@@ -286,37 +353,29 @@ static struct incl_file *add_include( struct incl_file *pFile, const char *name,
for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break; for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
if (pos >= MAX_INCLUDES) if (pos >= MAX_INCLUDES)
fatal_error( "%s: %s: error: too many included files, please fix MAX_INCLUDES\n", fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
ProgramName, pFile->name );
/* enforce some rules for the Wine tree */ /* enforce some rules for the Wine tree */
if (!memcmp( name, "../", 3 )) if (!memcmp( name, "../", 3 ))
fatal_error( "%s:%d: error: #include directive with relative path not allowed\n", fatal_error( "#include directive with relative path not allowed\n" );
pFile->filename, line );
if (!strcmp( name, "config.h" )) if (!strcmp( name, "config.h" ))
{ {
if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" )) if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
fatal_error( "%s:%d: error: config.h must not be included by a header file\n", fatal_error( "config.h must not be included by a header file\n" );
pFile->filename, line );
if (pos) if (pos)
fatal_error( "%s:%d: error: config.h must be included before anything else\n", fatal_error( "config.h must be included before anything else\n" );
pFile->filename, line );
} }
else if (!strcmp( name, "wine/port.h" )) else if (!strcmp( name, "wine/port.h" ))
{ {
if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" )) if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
fatal_error( "%s:%d: error: wine/port.h must not be included by a header file\n", fatal_error( "wine/port.h must not be included by a header file\n" );
pFile->filename, line ); if (!pos) fatal_error( "config.h must be included before wine/port.h\n" );
if (!pos) fatal_error( "%s:%d: error: config.h must be included before wine/port.h\n",
pFile->filename, line );
if (pos > 1) if (pos > 1)
fatal_error( "%s:%d: error: wine/port.h must be included before everything except config.h\n", fatal_error( "wine/port.h must be included before everything except config.h\n" );
pFile->filename, line );
if (strcmp( pFile->files[0]->name, "config.h" )) if (strcmp( pFile->files[0]->name, "config.h" ))
fatal_error( "%s:%d: error: config.h must be included before wine/port.h\n", fatal_error( "config.h must be included before wine/port.h\n" );
pFile->filename, line );
} }
LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry ) LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
...@@ -326,7 +385,7 @@ static struct incl_file *add_include( struct incl_file *pFile, const char *name, ...@@ -326,7 +385,7 @@ static struct incl_file *add_include( struct incl_file *pFile, const char *name,
memset( include, 0, sizeof(*include) ); memset( include, 0, sizeof(*include) );
include->name = xstrdup(name); include->name = xstrdup(name);
include->included_by = pFile; include->included_by = pFile;
include->included_line = line; include->included_line = input_line;
include->system = system; include->system = system;
list_add_tail( &includes, &include->entry ); list_add_tail( &includes, &include->entry );
found: found:
...@@ -354,12 +413,7 @@ static FILE *open_src_file( struct incl_file *pFile ) ...@@ -354,12 +413,7 @@ static FILE *open_src_file( struct incl_file *pFile )
pFile->filename = strmake( "%s/%s", src_dir, pFile->name ); pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
file = fopen( pFile->filename, "r" ); file = fopen( pFile->filename, "r" );
} }
if (!file) if (!file) fatal_perror( "open %s", pFile->name );
{
fprintf( stderr, "%s: error: ", ProgramName );
perror( pFile->name );
exit(1);
}
return file; return file;
} }
...@@ -557,14 +611,14 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file ...@@ -557,14 +611,14 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
{ {
char *buffer, *include; char *buffer, *include;
input_line = 0;
if (for_h_file) if (for_h_file)
{ {
/* generated .h file always includes these */ /* generated .h file always includes these */
add_include( pFile, "rpc.h", 0, 1 ); add_include( pFile, "rpc.h", 1 );
add_include( pFile, "rpcndr.h", 0, 1 ); add_include( pFile, "rpcndr.h", 1 );
} }
input_line = 0;
while ((buffer = get_line( file ))) while ((buffer = get_line( file )))
{ {
char quote; char quote;
...@@ -578,11 +632,10 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file ...@@ -578,11 +632,10 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
if (*p != '"') continue; if (*p != '"') continue;
include = ++p; include = ++p;
while (*p && (*p != '"')) p++; while (*p && (*p != '"')) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed import directive\n", if (!*p) fatal_error( "malformed import directive\n" );
pFile->filename, input_line );
*p = 0; *p = 0;
if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" ); if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
add_include( pFile, include, input_line, 0 ); add_include( pFile, include, 0 );
continue; continue;
} }
...@@ -612,11 +665,10 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file ...@@ -612,11 +665,10 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
include = p; include = p;
while (*p && (*p != quote)) p++; while (*p && (*p != quote)) p++;
if (!*p || (quote == '"' && p[-1] != '\\')) if (!*p || (quote == '"' && p[-1] != '\\'))
fatal_error( "%s:%d: error: Malformed #include directive inside cpp_quote\n", fatal_error( "malformed #include directive inside cpp_quote\n" );
pFile->filename, input_line );
if (quote == '"') p--; /* remove backslash */ if (quote == '"') p--; /* remove backslash */
*p = 0; *p = 0;
add_include( pFile, include, input_line, (quote == '>') ); add_include( pFile, include, (quote == '>') );
continue; continue;
} }
...@@ -631,10 +683,9 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file ...@@ -631,10 +683,9 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
if (quote == '<') quote = '>'; if (quote == '<') quote = '>';
include = p; include = p;
while (*p && (*p != quote)) p++; while (*p && (*p != quote)) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n", if (!*p) fatal_error( "malformed #include directive\n" );
pFile->filename, input_line );
*p = 0; *p = 0;
add_include( pFile, include, input_line, (quote == '>') ); add_include( pFile, include, (quote == '>') );
} }
} }
...@@ -662,10 +713,9 @@ static void parse_c_file( struct incl_file *pFile, FILE *file ) ...@@ -662,10 +713,9 @@ static void parse_c_file( struct incl_file *pFile, FILE *file )
if (quote == '<') quote = '>'; if (quote == '<') quote = '>';
include = p; include = p;
while (*p && (*p != quote)) p++; while (*p && (*p != quote)) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n", if (!*p) fatal_error( "malformed #include directive\n" );
pFile->filename, input_line );
*p = 0; *p = 0;
add_include( pFile, include, input_line, (quote == '>') ); add_include( pFile, include, (quote == '>') );
} }
} }
...@@ -703,7 +753,7 @@ static void parse_rc_file( struct incl_file *pFile, FILE *file ) ...@@ -703,7 +753,7 @@ static void parse_rc_file( struct incl_file *pFile, FILE *file )
while (*p && !isspace(*p) && *p != '*') p++; while (*p && !isspace(*p) && *p != '*') p++;
} }
if (!*p) if (!*p)
fatal_error( "%s:%d: error: Malformed makedep comment\n", pFile->filename, input_line ); fatal_error( "malformed makedep comment\n" );
*p = 0; *p = 0;
} }
else /* check for #include */ else /* check for #include */
...@@ -718,11 +768,10 @@ static void parse_rc_file( struct incl_file *pFile, FILE *file ) ...@@ -718,11 +768,10 @@ static void parse_rc_file( struct incl_file *pFile, FILE *file )
if (quote == '<') quote = '>'; if (quote == '<') quote = '>';
include = p; include = p;
while (*p && (*p != quote)) p++; while (*p && (*p != quote)) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n", if (!*p) fatal_error( "malformed #include directive\n" );
pFile->filename, input_line );
*p = 0; *p = 0;
} }
add_include( pFile, include, input_line, (quote == '>') ); add_include( pFile, include, (quote == '>') );
} }
} }
...@@ -741,30 +790,30 @@ static void parse_generated_idl( struct incl_file *source ) ...@@ -741,30 +790,30 @@ static void parse_generated_idl( struct incl_file *source )
if (strendswith( source->name, "_c.c" )) if (strendswith( source->name, "_c.c" ))
{ {
add_include( source, header, 0, 0 ); add_include( source, header, 0 );
} }
else if (strendswith( source->name, "_i.c" )) else if (strendswith( source->name, "_i.c" ))
{ {
add_include( source, "rpc.h", 0, 1 ); add_include( source, "rpc.h", 1 );
add_include( source, "rpcndr.h", 0, 1 ); add_include( source, "rpcndr.h", 1 );
add_include( source, "guiddef.h", 0, 1 ); add_include( source, "guiddef.h", 1 );
} }
else if (strendswith( source->name, "_p.c" )) else if (strendswith( source->name, "_p.c" ))
{ {
add_include( source, "objbase.h", 0, 1 ); add_include( source, "objbase.h", 1 );
add_include( source, "rpcproxy.h", 0, 1 ); add_include( source, "rpcproxy.h", 1 );
add_include( source, "wine/exception.h", 0, 1 ); add_include( source, "wine/exception.h", 1 );
add_include( source, header, 0, 0 ); add_include( source, header, 0 );
} }
else if (strendswith( source->name, "_s.c" )) else if (strendswith( source->name, "_s.c" ))
{ {
add_include( source, "wine/exception.h", 0, 1 ); add_include( source, "wine/exception.h", 1 );
add_include( source, header, 0, 0 ); add_include( source, header, 0 );
} }
else if (!strcmp( source->name, "dlldata.c" )) else if (!strcmp( source->name, "dlldata.c" ))
{ {
add_include( source, "objbase.h", 0, 1 ); add_include( source, "objbase.h", 1 );
add_include( source, "rpcproxy.h", 0, 1 ); add_include( source, "rpcproxy.h", 1 );
} }
free( header ); free( header );
...@@ -800,6 +849,7 @@ static void parse_file( struct incl_file *pFile, int src ) ...@@ -800,6 +849,7 @@ static void parse_file( struct incl_file *pFile, int src )
file = src ? open_src_file( pFile ) : open_include_file( pFile ); file = src ? open_src_file( pFile ) : open_include_file( pFile );
if (!file) return; if (!file) return;
input_file_name = pFile->filename;
if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" )) if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
parse_idl_file( pFile, file, 1 ); parse_idl_file( pFile, file, 1 );
...@@ -814,6 +864,7 @@ static void parse_file( struct incl_file *pFile, int src ) ...@@ -814,6 +864,7 @@ static void parse_file( struct incl_file *pFile, int src )
else if (strendswith( pFile->filename, ".rc" )) else if (strendswith( pFile->filename, ".rc" ))
parse_rc_file( pFile, file ); parse_rc_file( pFile, file );
fclose(file); fclose(file);
input_file_name = NULL;
} }
...@@ -839,56 +890,55 @@ static struct incl_file *add_src_file( const char *name ) ...@@ -839,56 +890,55 @@ static struct incl_file *add_src_file( const char *name )
/******************************************************************* /*******************************************************************
* output_include * output_include
*/ */
static void output_include( FILE *file, struct incl_file *pFile, static void output_include( struct incl_file *pFile, struct incl_file *owner, int *column )
struct incl_file *owner, int *column )
{ {
int i; int i;
if (pFile->owner == owner) return; if (pFile->owner == owner) return;
if (!pFile->filename) return; if (!pFile->filename) return;
pFile->owner = owner; pFile->owner = owner;
if (*column + strlen(pFile->filename) + 1 > 70) output_filename( pFile->filename, column );
{
fprintf( file, " \\\n" );
*column = 0;
}
fprintf( file, " %s", pFile->filename );
*column += strlen(pFile->filename) + 1;
for (i = 0; i < MAX_INCLUDES; i++) for (i = 0; i < MAX_INCLUDES; i++)
if (pFile->files[i]) output_include( file, pFile->files[i], if (pFile->files[i]) output_include( pFile->files[i], owner, column );
owner, column );
} }
/******************************************************************* /*******************************************************************
* output_src * output_sources
*/ */
static int output_src( FILE *file, struct incl_file *pFile, int *column ) static void output_sources(void)
{ {
char *obj = xstrdup( pFile->name ); struct incl_file *source;
char *ext = get_extension( obj ); int i, column;
if (ext)
LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
{ {
char *obj = xstrdup( source->name );
char *ext = get_extension( obj );
if (!ext) fatal_error( "unsupported file type %s\n", source->name );
*ext++ = 0; *ext++ = 0;
column = 0;
if (!strcmp( ext, "y" )) /* yacc file */ if (!strcmp( ext, "y" )) /* yacc file */
{ {
/* add source file dependency for parallel makes */ /* add source file dependency for parallel makes */
char *header = strmake( "%s.tab.h", obj ); char *header = strmake( "%s.tab.h", obj );
if (find_include_file( header )) fprintf( file, "%s.tab.c: %s\n", obj, header ); if (find_include_file( header )) output( "%s.tab.c: %s\n", obj, header );
free( header ); free( header );
*column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj ); column += output( "%s.tab.o: %s.tab.c", obj, obj );
} }
else if (!strcmp( ext, "l" )) /* lex file */ else if (!strcmp( ext, "l" )) /* lex file */
{ {
*column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj ); column += output( "%s.yy.o: %s.yy.c", obj, obj );
} }
else if (!strcmp( ext, "rc" )) /* resource file */ else if (!strcmp( ext, "rc" )) /* resource file */
{ {
*column += fprintf( file, "rsrc.pot %s.res: %s", obj, pFile->filename ); column += output( "rsrc.pot %s.res: %s", obj, source->filename );
} }
else if (!strcmp( ext, "mc" )) /* message file */ else if (!strcmp( ext, "mc" )) /* message file */
{ {
*column += fprintf( file, "msg.pot %s.res: %s", obj, pFile->filename ); column += output( "msg.pot %s.res: %s", obj, source->filename );
} }
else if (!strcmp( ext, "idl" )) /* IDL file */ else if (!strcmp( ext, "idl" )) /* IDL file */
{ {
...@@ -897,11 +947,11 @@ static int output_src( FILE *file, struct incl_file *pFile, int *column ) ...@@ -897,11 +947,11 @@ static int output_src( FILE *file, struct incl_file *pFile, int *column )
const char *suffix = "cips"; const char *suffix = "cips";
name = strmake( "%s.tlb", obj ); name = strmake( "%s.tlb", obj );
if (find_src_file( name )) *column += fprintf( file, "%s %s_t.res", name, obj ); if (find_src_file( name )) column += output( "%s %s_t.res", name, obj );
else else
{ {
got_header = 1; got_header = 1;
*column += fprintf( file, "%s.h", obj ); column += output( "%s.h", obj );
} }
free( name ); free( name );
...@@ -910,33 +960,36 @@ static int output_src( FILE *file, struct incl_file *pFile, int *column ) ...@@ -910,33 +960,36 @@ static int output_src( FILE *file, struct incl_file *pFile, int *column )
name = strmake( "%s_%c.c", obj, *suffix ); name = strmake( "%s_%c.c", obj, *suffix );
if (find_src_file( name )) if (find_src_file( name ))
{ {
if (!got_header++) *column += fprintf( file, " %s.h", obj ); if (!got_header++) column += output( " %s.h", obj );
*column += fprintf( file, " %s", name ); column += output( " %s", name );
} }
free( name ); free( name );
suffix++; suffix++;
} }
name = strmake( "%s_r.res", obj ); name = strmake( "%s_r.res", obj );
if (find_src_file( name )) *column += fprintf( file, " %s", name ); if (find_src_file( name )) column += output( " %s", name );
free( name ); free( name );
*column += fprintf( file, ": %s", pFile->filename ); column += output( ": %s", source->filename );
} }
else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" )) else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ))
{ {
return 0; /* nothing to do for typelib files */ continue; /* nothing to do for typelib files */
} }
else else
{ {
struct object_extension *ext; struct object_extension *ext;
LIST_FOR_EACH_ENTRY( ext, &object_extensions, struct object_extension, entry ) LIST_FOR_EACH_ENTRY( ext, &object_extensions, struct object_extension, entry )
*column += fprintf( file, "%s.%s ", obj, ext->extension ); column += output( "%s.%s ", obj, ext->extension );
*column += fprintf( file, ": %s", pFile->filename ); column += output( ": %s", source->filename );
} }
free( obj );
for (i = 0; i < MAX_INCLUDES; i++)
if (source->files[i]) output_include( source->files[i], source, &column );
output( "\n" );
} }
free( obj );
return 1;
} }
...@@ -961,8 +1014,7 @@ static FILE *create_temp_file( char **tmp_name ) ...@@ -961,8 +1014,7 @@ static FILE *create_temp_file( char **tmp_name )
if (errno != EEXIST) break; if (errno != EEXIST) break;
id += 7777; id += 7777;
} }
if (!ret) fatal_error( "%s: error: failed to create output file for '%s'\n", if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
ProgramName, OutputFileName );
*tmp_name = name; *tmp_name = name;
return ret; return ret;
} }
...@@ -973,45 +1025,34 @@ static FILE *create_temp_file( char **tmp_name ) ...@@ -973,45 +1025,34 @@ static FILE *create_temp_file( char **tmp_name )
*/ */
static void output_dependencies(void) static void output_dependencies(void)
{ {
struct incl_file *pFile;
int i, column;
FILE *file = NULL;
char *tmp_name = NULL; char *tmp_name = NULL;
if (Separator && ((file = fopen( OutputFileName, "r" )))) if (Separator && ((output_file = fopen( OutputFileName, "r" ))))
{ {
char buffer[1024]; char buffer[1024];
FILE *tmp_file = create_temp_file( &tmp_name ); FILE *tmp_file = create_temp_file( &tmp_name );
int found = 0; int found = 0;
while (fgets( buffer, sizeof(buffer), file ) && !found) while (fgets( buffer, sizeof(buffer), output_file ) && !found)
{ {
if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
fatal_error( "%s: error: failed to write to %s\n", ProgramName, tmp_name ); fatal_error( "failed to write to %s\n", tmp_name );
found = !strncmp( buffer, Separator, strlen(Separator) ); found = !strncmp( buffer, Separator, strlen(Separator) );
} }
fclose( file ); fclose( output_file );
file = tmp_file; output_file = tmp_file;
if (!found && list_head(&sources)) fprintf( file, "\n%s\n", Separator ); if (!found && list_head(&sources)) output( "\n%s\n", Separator );
} }
else else
{ {
if (!(file = fopen( OutputFileName, Separator ? "a" : "w" ))) if (!(output_file = fopen( OutputFileName, Separator ? "a" : "w" )))
{ fatal_perror( "%s", OutputFileName );
perror( OutputFileName );
exit(1);
}
}
LIST_FOR_EACH_ENTRY( pFile, &sources, struct incl_file, entry )
{
column = 0;
if (!output_src( file, pFile, &column )) continue;
for (i = 0; i < MAX_INCLUDES; i++)
if (pFile->files[i]) output_include( file, pFile->files[i],
pFile, &column );
fprintf( file, "\n" );
} }
fclose( file );
output_sources();
fclose( output_file );
output_file = NULL;
if (tmp_name) if (tmp_name)
{ {
...@@ -1025,8 +1066,7 @@ static void output_dependencies(void) ...@@ -1025,8 +1066,7 @@ static void output_dependencies(void)
if (ret == -1) if (ret == -1)
{ {
unlink( tmp_name ); unlink( tmp_name );
fatal_error( "%s: error: failed to rename output file to '%s'\n", fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
ProgramName, OutputFileName );
} }
free( tmp_name ); free( tmp_name );
} }
...@@ -1063,8 +1103,7 @@ static void parse_option( const char *opt ) ...@@ -1063,8 +1103,7 @@ static void parse_option( const char *opt )
if (opt[2]) add_object_extension( opt + 2 ); if (opt[2]) add_object_extension( opt + 2 );
break; break;
default: default:
fprintf( stderr, "Unknown option '%s'\n", opt ); fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
fprintf( stderr, Usage, ProgramName );
exit(1); exit(1);
} }
} }
...@@ -1079,9 +1118,6 @@ int main( int argc, char *argv[] ) ...@@ -1079,9 +1118,6 @@ int main( int argc, char *argv[] )
struct incl_path *path, *next; struct incl_path *path, *next;
int i, j; int i, j;
if ((ProgramName = strrchr( argv[0], '/' ))) ProgramName++;
else ProgramName = argv[0];
i = 1; i = 1;
while (i < argc) while (i < argc)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment