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