Fix arg parsing in ExecuteFile.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15326

(cherry picked from commit 0f94b467ba)
This commit is contained in:
Jeff Young 2023-09-23 14:32:44 +01:00
parent 9f814c0f6a
commit c895816cb0
1 changed files with 72 additions and 12 deletions

View File

@ -38,6 +38,7 @@
#include <gestfich.h> #include <gestfich.h>
#include <string_utils.h> #include <string_utils.h>
#include <launch_ext.h> #include <launch_ext.h>
#include "wx/tokenzr.h"
void QuoteString( wxString& string ) void QuoteString( wxString& string )
{ {
@ -115,21 +116,77 @@ wxString FindKicadFile( const wxString& shortname )
int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess *aCallback ) int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess *aCallback )
{ {
wxString fullEditorName; wxString fullEditorName;
wxString param; std::vector<wxString> params;
#ifdef __UNIX__ #ifdef __UNIX__
int space = aEditorName.Find( ' ' ); wxString param;
bool inSingleQuotes = false;
bool inDoubleQuotes = false;
if( space > 0 && !aEditorName.Contains( "\"" ) && !aEditorName.Contains( "\'" ) ) auto pushParam =
[&]()
{ {
fullEditorName = FindKicadFile( aEditorName.Mid( 0, space ) ); if( !param.IsEmpty() )
param = aEditorName.Mid( space + 1 ); {
params.push_back( param );
param.clear();
}
};
for( wxUniChar ch : aEditorName )
{
if( inSingleQuotes )
{
if( ch == '\'' )
{
pushParam();
inSingleQuotes = false;
continue;
} }
else else
#endif
{ {
fullEditorName = FindKicadFile( aEditorName ); param += ch;
} }
}
else if( inDoubleQuotes )
{
if( ch == '"' )
{
pushParam();
inDoubleQuotes = false;
}
else
{
param += ch;
}
}
else if( ch == '\'' )
{
pushParam();
inSingleQuotes = true;
}
else if( ch == '"' )
{
pushParam();
inDoubleQuotes = true;
}
else if( ch == ' ' )
{
pushParam();
}
else
{
param += ch;
}
}
pushParam();
fullEditorName = FindKicadFile( params[0] );
params.erase( params.begin() );
#else
fullEditorName = FindKicadFile( aEditorName );
#endif
if( wxFileExists( fullEditorName ) ) if( wxFileExists( fullEditorName ) )
{ {
@ -138,8 +195,11 @@ int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProce
args[i++] = fullEditorName.wc_str(); args[i++] = fullEditorName.wc_str();
if( !param.IsEmpty() ) if( !params.empty() )
args[i++] = param.wc_str(); {
for( const wxString& p : params )
args[i++] = p.wc_str();
}
if( !aFileName.IsEmpty() ) if( !aFileName.IsEmpty() )
args[i++] = aFileName.wc_str(); args[i++] = aFileName.wc_str();