From 0423e79c7af4448f24b701ddb4145018cc2e83a9 Mon Sep 17 00:00:00 2001 From: Peter Dell Date: Tue, 30 Jan 2024 01:50:43 +0100 Subject: [PATCH] Make command line/parameter parsing tolerant regarding consecutive spaces Use REGEX instead of standard split(" ") --- .../editor/LanguageEditorCompileCommand.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/editor/LanguageEditorCompileCommand.java b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/editor/LanguageEditorCompileCommand.java index 94c0fa2b..7e2c6db8 100644 --- a/com.wudsn.ide.lng/src/com/wudsn/ide/lng/editor/LanguageEditorCompileCommand.java +++ b/com.wudsn.ide.lng/src/com/wudsn/ide/lng/editor/LanguageEditorCompileCommand.java @@ -186,6 +186,18 @@ final class LanguageEditorCompileCommand { throw new RuntimeException("Cannot show view.", ex); } } + + /** + * Splits a string a one or more spaces, unless they are quoted. + * @param commandLine The command line to be split, not null. + * @return The array of string, may be empty, not null. + */ + private static String[] splitAtSpaces(String commandLine) { + if (commandLine == null) { + throw new IllegalArgumentException("Parameter 'commandLine' must not be null."); + } + return commandLine.split("\s+(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1); + } private boolean executeInternal(ILanguageEditor languageEditor, CompilerFiles files, String commandId, String runnerId) { @@ -280,14 +292,16 @@ final class LanguageEditorCompileCommand { compilerParameters = compilerDefinition.getDefaultParameters(); } - // The parameters are first split and then substituted. + // The parameters are first split at spaces (unless they are quotes) and then + // substituted. // This allows for parameters and file paths inner spaces to be used. // In some case addition quotes must be places around parameters, for // example for the "${sourceFilePath}". This can be used to avoid // problems with absolute file path under Unix starting with "/" or path // containing white spaces. compilerParameters = compilerParameters.trim(); - String compilerParameterArray[] = compilerParameters.split(" "); + String compilerParameterArray[] = splitAtSpaces(compilerParameters); + if (compilerParameterArray.length == 0) { // ERROR: The {0} '{1}' does not specify default parameters. createMainSourceFileMessage(files, IMarker.SEVERITY_ERROR, Texts.MESSAGE_E101, compilerDefinition.getText(), @@ -570,8 +584,7 @@ final class LanguageEditorCompileCommand { // parameters, for example for the "${outputFilePath}" for // MADS. Otherwise using absolute file path under Unix starting // "/" may cause conflicts. - String[] commandLineArray; - commandLineArray = runnerCommandLine.split(" "); + String[] commandLineArray = splitAtSpaces(runnerCommandLine); // Execution type: DEFAULT_APPLICATION if (runnerId.equals(RunnerId.DEFAULT_APPLICATION)) {