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