View Javadoc
1   package org.codehaus.plexus.util.cli.shell;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  /**
23   * <p>
24   * Implementation to call the CMD Shell present on Windows NT, 2000 and XP
25   * </p>
26   *
27   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
28   * @since 1.2
29   *
30   */
31  public class CmdShell extends Shell {
32      public CmdShell() {
33          setShellCommand("cmd.exe");
34          setQuotedExecutableEnabled(true);
35          setShellArgs(new String[] {"/X", "/C"});
36      }
37  
38      /**
39       * <p>
40       * Specific implementation that quotes all the command line.
41       * </p>
42       * <p>
43       * Workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468220
44       * </p>
45       * <p>
46       * From cmd.exe /? output:
47       * </p>
48       *
49       * <pre>
50       *      If /C or /K is specified, then the remainder of the command line after
51       *      the switch is processed as a command line, where the following logic is
52       *      used to process quote (&quot;) characters:
53       *
54       *      1.  If all of the following conditions are met, then quote characters
55       *      on the command line are preserved:
56       *
57       *      - no /S switch
58       *      - exactly two quote characters
59       *      - no special characters between the two quote characters,
60       *      where special is one of: &amp;&lt;&gt;()@&circ;|
61       *      - there are one or more whitespace characters between the
62       *      the two quote characters
63       *      - the string between the two quote characters is the name
64       *      of an executable file.
65       *
66       *      2.  Otherwise, old behavior is to see if the first character is
67       *      a quote character and if so, strip the leading character and
68       *      remove the last quote character on the command line, preserving
69       *      any text after the last quote character.
70       * </pre>
71       * <p>
72       * Always quoting the entire command line, regardless of these conditions appears to make Windows processes invoke
73       * successfully.
74       * </p>
75       */
76      @Override
77      public List<String> getCommandLine(String executable, String[] arguments) {
78          StringBuilder sb = new StringBuilder();
79          sb.append("\"");
80          sb.append(super.getCommandLine(executable, arguments).get(0));
81          sb.append("\"");
82  
83          return Arrays.asList(new String[] {sb.toString()});
84      }
85  }