1 package org.apache.maven.shared.utils.cli.shell;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.apache.maven.shared.utils.Os;
26 import org.apache.maven.shared.utils.StringUtils;
27
28
29
30
31 public class BourneShell
32 extends Shell
33 {
34 private static final char[] BASH_QUOTING_TRIGGER_CHARS =
35 { ' ', '$', ';', '&', '|', '<', '>', '*', '?', '(', ')', '[', ']', '{', '}', '`' };
36
37
38
39
40 public BourneShell()
41 {
42 setShellCommand( "/bin/sh" );
43 setArgumentQuoteDelimiter( '\'' );
44 setExecutableQuoteDelimiter( '\"' );
45 setSingleQuotedArgumentEscaped( true );
46 setSingleQuotedExecutableEscaped( false );
47 setQuotedExecutableEnabled( true );
48 }
49
50
51
52
53 public String getExecutable()
54 {
55 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
56 {
57 return super.getExecutable();
58 }
59
60 return unifyQuotes( super.getExecutable() );
61 }
62
63
64 public List<String> getShellArgsList()
65 {
66 List<String> shellArgs = new ArrayList<String>();
67 List<String> existingShellArgs = super.getShellArgsList();
68
69 if ( ( existingShellArgs != null ) && !existingShellArgs.isEmpty() )
70 {
71 shellArgs.addAll( existingShellArgs );
72 }
73
74 shellArgs.add( "-c" );
75
76 return shellArgs;
77 }
78
79
80 public String[] getShellArgs()
81 {
82 String[] shellArgs = super.getShellArgs();
83 if ( shellArgs == null )
84 {
85 shellArgs = new String[0];
86 }
87
88 if ( ( shellArgs.length > 0 ) && !shellArgs[shellArgs.length - 1].equals( "-c" ) )
89 {
90 String[] newArgs = new String[shellArgs.length + 1];
91
92 System.arraycopy( shellArgs, 0, newArgs, 0, shellArgs.length );
93 newArgs[shellArgs.length] = "-c";
94
95 shellArgs = newArgs;
96 }
97
98 return shellArgs;
99 }
100
101
102 protected String getExecutionPreamble()
103 {
104 if ( getWorkingDirectoryAsString() == null )
105 {
106 return null;
107 }
108
109 String dir = getWorkingDirectoryAsString();
110 StringBuilder sb = new StringBuilder();
111 sb.append( "cd " );
112
113 sb.append( unifyQuotes( dir ) );
114 sb.append( " && " );
115
116 return sb.toString();
117 }
118
119
120 protected char[] getQuotingTriggerChars()
121 {
122 return BASH_QUOTING_TRIGGER_CHARS;
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 private static String unifyQuotes( String path )
143 {
144 if ( path == null )
145 {
146 return null;
147 }
148
149 if ( path.indexOf( ' ' ) == -1 && path.indexOf( '\'' ) != -1 && path.indexOf( '"' ) == -1 )
150 {
151 return StringUtils.escape( path );
152 }
153
154 return StringUtils.quoteAndEscape( path, '\"', BASH_QUOTING_TRIGGER_CHARS );
155 }
156 }