From c895816cb010af612a6e3ac78aa21d6a62fc5b7b Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sat, 23 Sep 2023 14:32:44 +0100 Subject: [PATCH] Fix arg parsing in ExecuteFile. Fixes https://gitlab.com/kicad/code/kicad/-/issues/15326 (cherry picked from commit 0f94b467ba0f082b4115cbbdba0b7131c2aac2ed) --- common/gestfich.cpp | 84 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 12 deletions(-) diff --git a/common/gestfich.cpp b/common/gestfich.cpp index 7ad46c0a2f..a9c5f57c1a 100644 --- a/common/gestfich.cpp +++ b/common/gestfich.cpp @@ -38,6 +38,7 @@ #include #include #include +#include "wx/tokenzr.h" void QuoteString( wxString& string ) { @@ -114,22 +115,78 @@ wxString FindKicadFile( const wxString& shortname ) int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess *aCallback ) { - wxString fullEditorName; - wxString param; + wxString fullEditorName; + std::vector params; #ifdef __UNIX__ - int space = aEditorName.Find( ' ' ); + wxString param; + bool inSingleQuotes = false; + bool inDoubleQuotes = false; - if( space > 0 && !aEditorName.Contains( "\"" ) && !aEditorName.Contains( "\'" ) ) + auto pushParam = + [&]() + { + if( !param.IsEmpty() ) + { + params.push_back( param ); + param.clear(); + } + }; + + for( wxUniChar ch : aEditorName ) { - fullEditorName = FindKicadFile( aEditorName.Mid( 0, space ) ); - param = aEditorName.Mid( space + 1 ); + if( inSingleQuotes ) + { + if( ch == '\'' ) + { + pushParam(); + inSingleQuotes = false; + continue; + } + else + { + 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; + } } - else + + pushParam(); + + fullEditorName = FindKicadFile( params[0] ); + params.erase( params.begin() ); +#else + fullEditorName = FindKicadFile( aEditorName ); #endif - { - fullEditorName = FindKicadFile( aEditorName ); - } if( wxFileExists( fullEditorName ) ) { @@ -138,8 +195,11 @@ int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProce args[i++] = fullEditorName.wc_str(); - if( !param.IsEmpty() ) - args[i++] = param.wc_str(); + if( !params.empty() ) + { + for( const wxString& p : params ) + args[i++] = p.wc_str(); + } if( !aFileName.IsEmpty() ) args[i++] = aFileName.wc_str();