Fixed wxExecute() to handle filenames with spaces and quoted arguments.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1945 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Karsten Ballüder 1999-03-19 10:57:18 +00:00
parent 2387e0f207
commit fad866f4b7

View File

@ -39,7 +39,7 @@
#include <signal.h> #include <signal.h>
#include <fcntl.h> // for O_WRONLY and friends #include <fcntl.h> // for O_WRONLY and friends
#include <time.h> // nanosleep() and/or usleep() #include <time.h> // nanosleep() and/or usleep()
#include <ctype.h> // isspace()
#ifdef HAVE_UNAME #ifdef HAVE_UNAME
#include <sys/utsname.h> // for uname() #include <sys/utsname.h> // for uname()
#endif // HAVE_UNAME #endif // HAVE_UNAME
@ -132,24 +132,61 @@ int wxKill(long pid, int sig)
return kill(pid, sig); return kill(pid, sig);
} }
#define WXEXECUTE_NARGS 127
long wxExecute( const wxString& command, bool sync, wxProcess *process ) long wxExecute( const wxString& command, bool sync, wxProcess *process )
{ {
static const char *IFS = " \t\n";
wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" ); wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
int argc = 0; int argc = 0;
char *argv[127]; char *argv[WXEXECUTE_NARGS];
char *tmp = new char[command.Len() + 1]; wxString argument;
strcpy(tmp, command); const char *cptr = command.c_str();
char quotechar = '\0'; // is arg quoted?
argv[argc++] = strtok(tmp, IFS); bool escaped = FALSE;
while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
/* loop */ ;
do
{
argument="";
quotechar = '\0';
// eat leading whitespace:
while(*cptr && isspace(*cptr))
cptr++;
if(*cptr == '\'' || *cptr == '"')
quotechar = *cptr++;
do
{
if(*cptr == '\\' && ! escaped)
{
escaped = TRUE;
cptr++;
continue;
}
// all other characters:
argument += *cptr ++;
escaped = FALSE;
// Have we reached the end of the argument?
if((*cptr == quotechar && ! escaped)
|| (quotechar == '\0' && isspace(*cptr))
|| *cptr == '\0')
{
wxASSERT(argc < WXEXECUTE_NARGS);
argv[argc] = new char[argument.Len()+1];
strcpy(argv[argc], argument.c_str());
argc++;
// if not at end of buffer, swallow last character:
if(*cptr) cptr++;
break; // done with this one, start over
}
}while(*cptr);
}while(*cptr);
argv[argc] = NULL;
long lRc = wxExecute(argv, sync, process); long lRc = wxExecute(argv, sync, process);
delete [] tmp; argc = 0;
while(argv[argc])
delete [] argv[argc++];
return lRc; return lRc;
} }