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
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.apache.maven.shared.utils.Os;
26
27 /**
28 * @author Jason van Zyl
29 */
30 public class BourneShell
31 extends Shell
32 {
33
34 /**
35 * Create instance of BourneShell.
36 */
37 public BourneShell()
38 {
39 setUnconditionalQuoting( true );
40 setShellCommand( "/bin/sh" );
41 setArgumentQuoteDelimiter( '\'' );
42 setExecutableQuoteDelimiter( '\'' );
43 setSingleQuotedArgumentEscaped( true );
44 setSingleQuotedExecutableEscaped( false );
45 setQuotedExecutableEnabled( true );
46 }
47
48 /**
49 * {@inheritDoc}
50 */
51 public String getExecutable()
52 {
53 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
54 {
55 return super.getExecutable();
56 }
57
58 return quoteOneItem( super.getExecutable(), true );
59 }
60
61 /** {@inheritDoc} */
62 public List<String> getShellArgsList()
63 {
64 List<String> shellArgs = new ArrayList<String>();
65 List<String> existingShellArgs = super.getShellArgsList();
66
67 if ( ( existingShellArgs != null ) && !existingShellArgs.isEmpty() )
68 {
69 shellArgs.addAll( existingShellArgs );
70 }
71
72 shellArgs.add( "-c" );
73
74 return shellArgs;
75 }
76
77 /** {@inheritDoc} */
78 public String[] getShellArgs()
79 {
80 String[] shellArgs = super.getShellArgs();
81 if ( shellArgs == null )
82 {
83 shellArgs = new String[0];
84 }
85
86 if ( ( shellArgs.length > 0 ) && !shellArgs[shellArgs.length - 1].equals( "-c" ) )
87 {
88 String[] newArgs = new String[shellArgs.length + 1];
89
90 System.arraycopy( shellArgs, 0, newArgs, 0, shellArgs.length );
91 newArgs[shellArgs.length] = "-c";
92
93 shellArgs = newArgs;
94 }
95
96 return shellArgs;
97 }
98
99 /** {@inheritDoc} */
100 protected String getExecutionPreamble()
101 {
102 if ( getWorkingDirectoryAsString() == null )
103 {
104 return null;
105 }
106
107 String dir = getWorkingDirectoryAsString();
108
109 return "cd " + quoteOneItem( dir, false ) + " && ";
110 }
111
112 /**
113 * <p>Unify quotes in a path for the Bourne Shell.</p>
114 * <pre>
115 * BourneShell.quoteOneItem(null) = null
116 * BourneShell.quoteOneItem("") = ''
117 * BourneShell.quoteOneItem("/test/quotedpath'abc") = '/test/quotedpath'"'"'abc'
118 * BourneShell.quoteOneItem("/test/quoted path'abc") = '/test/quoted pat'"'"'habc'
119 * BourneShell.quoteOneItem("/test/quotedpath\"abc") = '/test/quotedpath"abc'
120 * BourneShell.quoteOneItem("/test/quoted path\"abc") = '/test/quoted path"abc'
121 * BourneShell.quoteOneItem("/test/quotedpath\"'abc") = '/test/quotedpath"'"'"'abc'
122 * BourneShell.quoteOneItem("/test/quoted path\"'abc") = '/test/quoted path"'"'"'abc'
123 * </pre>
124 *
125 * @param path not null path.
126 * @return the path unified correctly for the Bourne shell.
127 */
128 protected String quoteOneItem( String path, boolean isExecutable )
129 {
130 if ( path == null )
131 {
132 return null;
133 }
134
135 return "'" + path.replace( "'", "'\"'\"'" ) + "'";
136 }
137 }