tools: Add helper functions to spawn a command from an strarray.
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
parent
97ca9f8a3d
commit
2a08d6ce80
|
@ -25,6 +25,18 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#ifdef HAVE_UNISTD_H
|
||||||
|
# include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
||||||
|
# include <process.h>
|
||||||
|
#else
|
||||||
|
# include <sys/wait.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#if !defined(__GNUC__) && !defined(__attribute__)
|
#if !defined(__GNUC__) && !defined(__attribute__)
|
||||||
#define __attribute__(x)
|
#define __attribute__(x)
|
||||||
|
@ -200,5 +212,41 @@ static inline const char *strarray_bsearch( const struct strarray *array, const
|
||||||
return res ? *res : NULL;
|
return res ? *res : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void strarray_trace( struct strarray args )
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
for (i = 0; i < args.count; i++)
|
||||||
|
{
|
||||||
|
if (strpbrk( args.str[i], " \t\n\r")) printf( "\"%s\"", args.str[i] );
|
||||||
|
else printf( "%s", args.str[i] );
|
||||||
|
putchar( i < args.count - 1 ? ' ' : '\n' );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int strarray_spawn( struct strarray args )
|
||||||
|
{
|
||||||
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
||||||
|
strarray_add( &args, NULL );
|
||||||
|
return _spawnvp( _P_WAIT, args.str[0], args.str );
|
||||||
|
#else
|
||||||
|
pid_t pid, wret;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
if (!(pid = fork()))
|
||||||
|
{
|
||||||
|
strarray_add( &args, NULL );
|
||||||
|
execvp( args.str[0], (char **)args.str );
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
if (pid == -1) return -1;
|
||||||
|
|
||||||
|
while (pid != (wret = waitpid( pid, &status, 0 )))
|
||||||
|
if (wret == -1 && errno != EINTR) break;
|
||||||
|
|
||||||
|
if (pid == wret && WIFEXITED(status)) return WEXITSTATUS(status);
|
||||||
|
return 255; /* abnormal exit with an abort or an interrupt */
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* __WINE_TOOLS_H */
|
#endif /* __WINE_TOOLS_H */
|
||||||
|
|
|
@ -206,17 +206,13 @@ static const char *find_binary( const char *prefix, const char *name )
|
||||||
|
|
||||||
void spawn( struct strarray args )
|
void spawn( struct strarray args )
|
||||||
{
|
{
|
||||||
unsigned int i;
|
|
||||||
int status;
|
int status;
|
||||||
const char *argv0 = find_binary( NULL, args.str[0] );
|
const char *argv0 = find_binary( NULL, args.str[0] );
|
||||||
|
|
||||||
if (argv0) args.str[0] = argv0;
|
if (argv0) args.str[0] = argv0;
|
||||||
strarray_add( &args, NULL );
|
if (verbose) strarray_trace( args );
|
||||||
if (verbose)
|
|
||||||
for (i = 0; args.str[i]; i++)
|
|
||||||
fprintf( stderr, "%s%c", args.str[i], args.str[i+1] ? ' ' : '\n' );
|
|
||||||
|
|
||||||
if ((status = _spawnvp( _P_WAIT, args.str[0], args.str )))
|
if ((status = strarray_spawn( args )))
|
||||||
{
|
{
|
||||||
if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
|
if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
|
||||||
else fatal_perror( "winebuild" );
|
else fatal_perror( "winebuild" );
|
||||||
|
|
|
@ -197,27 +197,14 @@ const char *find_binary( struct strarray prefix, const char *name )
|
||||||
|
|
||||||
int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
|
int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
|
||||||
int status;
|
int status;
|
||||||
const char** argv;
|
|
||||||
|
|
||||||
strarray_add( &args, NULL);
|
args.str[0] = find_binary( prefix, args.str[0] );
|
||||||
argv = args.str;
|
if (verbose) strarray_trace( args );
|
||||||
argv[0] = find_binary( prefix, argv[0] );
|
|
||||||
|
|
||||||
if (verbose)
|
if ((status = strarray_spawn( args )) && !ignore_errors)
|
||||||
{
|
{
|
||||||
for(i = 0; argv[i]; i++)
|
if (status > 0) error("%s failed\n", args.str[0]);
|
||||||
{
|
|
||||||
if (strpbrk(argv[i], " \t\n\r")) printf("\"%s\" ", argv[i]);
|
|
||||||
else printf("%s ", argv[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((status = _spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
|
|
||||||
{
|
|
||||||
if (status > 0) error("%s failed\n", argv[0]);
|
|
||||||
else perror("winegcc");
|
else perror("winegcc");
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue