LinkedList cmdsl = new LinkedList();
Iterator it = cmds.iterator();
while (it.hasNext()) {
Cmd cmd = (Cmd) it.next();
String scmd = cmd.getExec();
// we leave cmd as it is.
String[] params = cmd.getParams();
LinkedList ops = new LinkedList();
// we process each one - look for $ then look for eof or space.
// we try resolve $ if can't then leave it as it is.
//log.debug("cmdline parameters: ");
for (int i = 0; i < params.length; i++) {
String c = params[i];
//log.debug("*** " + c);
// in here we do several things. We resolve env vars only if " or without
// we do pathname expansion if not " or ' and *, ? or [ is present
/*
if(c.startsWith("'")) {
if(c.endsWith("'")) {
ops.add(c.substring(1, c.length()-1));
continue;
} else {
ops.add(c.substring(1, c.length()));
continue;
}
}
if(c.endsWith("'")) {
ops.add(c.substring(0, c.length()-1));
continue;
}
*/
// if starts with " and ends with " then we need to remove that
// if it does not end with " then we treat it as if it is not within quotes
if (c.startsWith("'") && c.endsWith("'") && c.length() > 1) {
ops.add(c.substring(1, c.length() - 1));
continue;
}
StringBuffer outc = new StringBuffer();
StringTokenizer st = new StringTokenizer(c, "$ ", true);
boolean isVar = false;
while (st.hasMoreTokens()) {
String t = st.nextToken();
//log.debug("[SSH] : t: " + t);
if (t.equals("$")) {
isVar = true;
//log.debug("[SSH] : found $");
} else {
if (isVar) {
// lookup t
String p = null;
int j = 0;
StringBuffer psmp = new StringBuffer();
//log.debug("[SSH] : init psmp: " + psmp);
for (j = 0; j < t.length() && p == null; j++) {
//log.debug("[SSH] : trying " + psmp);
psmp.append(t.charAt(j));
p = getEnvProperty(cmd, psmp.toString());
}
if (p == null) {
//log.debug("[SSH] : no property for this name");
outc.append("$").append(t);
} else {
//log.debug("[SSH] : found property: " + p);
outc.append(p);
if (j < t.length()) {
//log.debug("[SSH] : appending a remnant: " + t.substring(j, t.length()));
outc.append(t.substring(j, t.length()));
}
}
isVar = false;
} else {
outc.append(t);
}
}
}
c = outc.toString();
if (c.startsWith("\"") && c.endsWith("\"") && c.length() > 1) {
ops.add(c.substring(1, c.length() - 1));
continue;
}
if (c.indexOf("?") != -1 || c.indexOf("*") != -1 || (c.indexOf("[") != -1 && c.indexOf("]") != -1)) {
// System.out.println("It's a pattern! : " + c);
try {
FPExpand fpe = new FPExpand(this);
fpe.expand(c);
Vector vx = fpe.getMatches();
Iterator itx = vx.iterator();
while (itx.hasNext()) {
ops.add(((FileInfo) itx.next()).getFileName().toString());
}
} catch (Exception ex) {
log.error("Could not expand", ex);
}
continue;
}
ops.add(c);
}
String[] pout = new String[ops.size()];
Iterator it2 = ops.iterator();
for (int i = 0; it2.hasNext(); i++) {
pout[i] = (String) it2.next();
}
//cmdsl.add(new Cmd(scmd, pout, null, null));
cmd.setParams(pout);
cmdsl.add(cmd);
}
cmds.setCmds(cmdsl);
return cmds;