From 69ae8f7954a1a3109181cec3dbc3244b8573e828 Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Mon, 31 May 2010 09:17:57 +0000
Subject: [PATCH] Fix issue #4363. Improve dsconfig batch mode with multiple spaces between quotes

---
 opends/src/server/org/opends/server/tools/dsconfig/DSConfig.java |   70 +++++++++++++++++++++++++++--------
 1 files changed, 54 insertions(+), 16 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/dsconfig/DSConfig.java b/opends/src/server/org/opends/server/tools/dsconfig/DSConfig.java
index 6193922..2a587ce 100644
--- a/opends/src/server/org/opends/server/tools/dsconfig/DSConfig.java
+++ b/opends/src/server/org/opends/server/tools/dsconfig/DSConfig.java
@@ -1195,8 +1195,7 @@
     toString();
   }
 
-  private void handleBatchFile(String[] args)
-  {
+  private void handleBatchFile(String[] args) {
     BufferedReader reader = null;
     try {
       // Build a list of initial arguments,
@@ -1205,8 +1204,8 @@
       Collections.addAll(initialArgs, args);
       int batchFileArgIndex = -1;
       for (String elem : initialArgs) {
-        if (elem.startsWith("-" + OPTION_SHORT_BATCH_FILE_PATH) ||
-            elem.contains(OPTION_LONG_BATCH_FILE_PATH)) {
+        if (elem.startsWith("-" + OPTION_SHORT_BATCH_FILE_PATH)
+                || elem.contains(OPTION_LONG_BATCH_FILE_PATH)) {
           batchFileArgIndex = initialArgs.indexOf(elem);
           break;
         }
@@ -1218,28 +1217,43 @@
       }
       String batchFilePath = batchFileArgument.getValue().trim();
       reader =
-        new BufferedReader(new FileReader(batchFilePath));
+              new BufferedReader(new FileReader(batchFilePath));
       String line;
+      String command = "";
+      //
+      // Split the CLI string into arguments array
+      //
       while ((line = reader.readLine()) != null) {
-        line = line.trim();
-        if (line.equals("") || line.startsWith("#")) {
+         if (line.equals("") || line.startsWith("#")) {
           // Empty line or comment
           continue;
         }
-        // Split the CLI string into arguments array
-        // For arguments including spaces, "\ " only is supported.
-        // CLI on several lines (using \) is not supported.
-        line = line.replace("\\ ", "##");
-        String[] fileArguments = line.split("\\s+");
-        for (int ii=0; ii<fileArguments.length; ii++) {
+       // command split in several line support
+        if (line.endsWith("\\")) {
+          // command is split into several lines
+          command += line.substring(0, line.length() - 1);
+          continue;
+        } else {
+          command += line;
+        }
+        command = command.trim();
+        // string between quotes support
+        command = replaceSpacesInQuotes(command);
+        // "\ " support
+        command = command.replace("\\ ", "##");
+        String[] fileArguments = command.split("\\s+");
+        // reset command
+        command = "";
+        for (int ii = 0; ii < fileArguments.length; ii++) {
           fileArguments[ii] = fileArguments[ii].replace("##", " ");
+          System.out.println(fileArguments[ii]);
         }
 
         // Append initial arguments to the file line
         List<String> allArguments = new ArrayList<String>();
         Collections.addAll(allArguments, fileArguments);
         allArguments.addAll(initialArgs);
-        String[] allArgsArray = allArguments.toArray(new String[] {});
+        String[] allArgsArray = allArguments.toArray(new String[]{});
 
         // Build a single CLI string for display
         StringBuffer cli = new StringBuffer();
@@ -1247,9 +1261,8 @@
           cli.append(arg + " ");
         }
 
-        System.out.println("Running : dsconfig " + cli.toString() + "\n");
         int exitCode =
-          main(allArgsArray, false, getOutputStream(), getErrorStream());
+                main(allArgsArray, false, getOutputStream(), getErrorStream());
         if (exitCode != 0) {
           reader.close();
           System.exit(filterExitCode(exitCode));
@@ -1262,4 +1275,29 @@
       println(ERR_DSCFG_ERROR_READING_BATCH_FILE.get(ex.toString()));
     }
   }
+
+  // Replace spaces in quotes by "\ "
+  private String replaceSpacesInQuotes(String line) {
+    String newLine = "";
+    boolean inQuotes = false;
+    for (int ii = 0; ii < line.length(); ii++) {
+      char ch = line.charAt(ii);
+      if ((ch == '\"') || (ch == '\'')) {
+        if (!inQuotes) {
+          // enter in a quoted string
+          inQuotes = true;
+        } else {
+          // end of a quoted string
+          inQuotes = false;
+        }
+        continue;
+      }
+      if (inQuotes && (ch == ' ')) {
+        newLine += "\\ ";
+      } else {
+        newLine += ch;
+      }
+    }
+    return newLine;
+  }
 }

--
Gitblit v1.10.0