1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Cosmetics

Change-Id: I8036046d2e755de2e77ef5bf6502008d4ada0e49
This commit is contained in:
Jonah Graham 2016-11-14 20:46:24 +00:00
parent 334777eb6e
commit b4cc24e31e

View file

@ -17,12 +17,13 @@ import org.eclipse.osgi.service.environment.Constants;
/** /**
* Utilities to work with command line, parse arguments, etc. * Utilities to work with command line, parse arguments, etc.
*
* @noextend This class is not intended to be subclassed by clients. * @noextend This class is not intended to be subclassed by clients.
* @noinstantiate This class is not intended to be instantiated by clients. * @noinstantiate This class is not intended to be instantiated by clients.
* @since 5.1 * @since 5.1
*/ */
public class CommandLineUtil { public class CommandLineUtil {
public static String[] argumentsToArray(String line) { public static String[] argumentsToArray(String line) {
boolean osWin; boolean osWin;
try { try {
@ -36,16 +37,18 @@ public class CommandLineUtil {
return argumentsToArrayUnixStyle(line); return argumentsToArrayUnixStyle(line);
} }
} }
/** /**
* Parsing arguments in a shell style. * Parsing arguments in a shell style. i.e.
* i.e. *
* <code> * <pre>
* ["a b c" d] -> [[a b c],[d]] * ["a b c" d] -> [[a b c],[d]]
* [a d] -> [[a],[d]] * [a d] -> [[a],[d]]
* ['"quoted"'] -> [["quoted"]] * ['"quoted"'] -> [["quoted"]]
* [\\ \" \a] -> [[\],["],[a]] * [\\ \" \a] -> [[\],["],[a]]
* ["str\\str\a"] -> [[str\str\a]] * ["str\\str\a"] -> [[str\str\a]]
* </code> * </pre>
*
* @param line * @param line
* @return array of arguments, or empty array if line is null or empty * @return array of arguments, or empty array if line is null or empty
*/ */
@ -69,97 +72,99 @@ public class CommandLineUtil {
char c = array[i]; char c = array[i];
switch (state) { switch (state) {
case IN_ARG: case IN_ARG:
// fall through // fall through
case INITIAL: case INITIAL:
if (Character.isWhitespace(c)) { if (Character.isWhitespace(c)) {
if (state == INITIAL) break; // ignore extra spaces if (state == INITIAL)
// add argument break; // ignore extra spaces
state = INITIAL; // add argument
String arg = buffer.toString(); state = INITIAL;
buffer = new StringBuilder(); String arg = buffer.toString();
aList.add(arg); buffer = new StringBuilder();
} else { aList.add(arg);
switch (c) { } else {
case '\\':
state = ESCAPED;
break;
case '\'':
state = IN_SINGLE_QUOTES;
break;
case '\"':
state = IN_DOUBLE_QUOTES;
break;
default:
state = IN_ARG;
buffer.append(c);
break;
}
}
break;
case IN_DOUBLE_QUOTES:
switch (c) { switch (c) {
case '\\': case '\\':
state = IN_DOUBLE_QUOTES_ESCAPED; state = ESCAPED;
break; break;
case '\"': case '\'':
state = IN_ARG; state = IN_SINGLE_QUOTES;
break; break;
default: case '\"':
buffer.append(c); state = IN_DOUBLE_QUOTES;
break; break;
default:
state = IN_ARG;
buffer.append(c);
break;
} }
}
break;
case IN_DOUBLE_QUOTES:
switch (c) {
case '\\':
state = IN_DOUBLE_QUOTES_ESCAPED;
break; break;
case IN_SINGLE_QUOTES: case '\"':
switch (c) {
case '\'':
state = IN_ARG;
break;
default:
buffer.append(c);
break;
}
break;
case IN_DOUBLE_QUOTES_ESCAPED:
switch (c) {
case '\"':
case '\\':
buffer.append(c);
break;
case 'n':
buffer.append("\n"); //$NON-NLS-1$
break;
default:
buffer.append('\\');
buffer.append(c);
break;
}
state = IN_DOUBLE_QUOTES;
break;
case ESCAPED:
buffer.append(c);
state = IN_ARG; state = IN_ARG;
break; break;
default:
buffer.append(c);
break;
}
break;
case IN_SINGLE_QUOTES:
switch (c) {
case '\'':
state = IN_ARG;
break;
default:
buffer.append(c);
break;
}
break;
case IN_DOUBLE_QUOTES_ESCAPED:
switch (c) {
case '\"':
case '\\':
buffer.append(c);
break;
case 'n':
buffer.append("\n"); //$NON-NLS-1$
break;
default:
buffer.append('\\');
buffer.append(c);
break;
}
state = IN_DOUBLE_QUOTES;
break;
case ESCAPED:
buffer.append(c);
state = IN_ARG;
break;
} }
} }
if (state != INITIAL) { // this allow to process empty string as an argument if (state != INITIAL) { // this allow to process empty string as an
// argument
aList.add(buffer.toString()); aList.add(buffer.toString());
} }
return aList.toArray(new String[aList.size()]); return aList.toArray(new String[aList.size()]);
} }
/** /**
* Parsing arguments in a cmd style. * Parsing arguments in a cmd style. i.e.
* i.e. *
* <code> * <pre>
* ["a b c" d] -> [[a b c],[d]] * ["a b c" d] -> [[a b c],[d]]
* [a d] -> [[a],[d]] * [a d] -> [[a],[d]]
* ['"quoted"'] -> [['quoted']] * ['"quoted"'] -> [['quoted']]
* [\\ \" \a] -> [[\\],["],[\a]] * [\\ \" \a] -> [[\\],["],[\a]]
* ["str\\str\a"] -> [[str\\str\a]] * ["str\\str\a"] -> [[str\\str\a]]
* </code> * </pre>
*
* @param line * @param line
* @return array of arguments, or empty array if line is null or empty * @return array of arguments, or empty array if line is null or empty
*/ */
@ -182,73 +187,75 @@ public class CommandLineUtil {
char c = array[i]; char c = array[i];
switch (state) { switch (state) {
case IN_ARG: case IN_ARG:
// fall through // fall through
case INITIAL: case INITIAL:
if (Character.isWhitespace(c)) { if (Character.isWhitespace(c)) {
if (state == INITIAL) break; // ignore extra spaces if (state == INITIAL)
// add argument break; // ignore extra spaces
state = INITIAL; // add argument
String arg = buffer.toString(); state = INITIAL;
buffer = new StringBuilder(); String arg = buffer.toString();
aList.add(arg); buffer = new StringBuilder();
} else { aList.add(arg);
switch (c) { } else {
case '\\':
state = ESCAPED;
break;
case '\"':
state = IN_DOUBLE_QUOTES;
break;
default:
state = IN_ARG;
buffer.append(c);
break;
}
}
break;
case IN_DOUBLE_QUOTES:
switch (c) { switch (c) {
case '\\': case '\\':
state = IN_DOUBLE_QUOTES_ESCAPED; state = ESCAPED;
break; break;
case '\"':
state = IN_ARG;
break;
default:
buffer.append(c);
break;
}
break;
case IN_DOUBLE_QUOTES_ESCAPED:
switch (c) {
case '\"':
buffer.append(c);
break;
default:
buffer.append('\\');
buffer.append(c);
break;
}
state = IN_DOUBLE_QUOTES;
break;
case ESCAPED:
state = IN_ARG;
switch (c) {
case ' ':
case '\"': case '\"':
buffer.append(c); state = IN_DOUBLE_QUOTES;
break; break;
default: default:
buffer.append('\\'); state = IN_ARG;
buffer.append(c); buffer.append(c);
break; break;
} }
}
break;
case IN_DOUBLE_QUOTES:
switch (c) {
case '\\':
state = IN_DOUBLE_QUOTES_ESCAPED;
break; break;
case '\"':
state = IN_ARG;
break;
default:
buffer.append(c);
break;
}
break;
case IN_DOUBLE_QUOTES_ESCAPED:
switch (c) {
case '\"':
buffer.append(c);
break;
default:
buffer.append('\\');
buffer.append(c);
break;
}
state = IN_DOUBLE_QUOTES;
break;
case ESCAPED:
state = IN_ARG;
switch (c) {
case ' ':
case '\"':
buffer.append(c);
break;
default:
buffer.append('\\');
buffer.append(c);
break;
}
break;
} }
} }
if (state != INITIAL) { // this allow to process empty string as an argument if (state != INITIAL) { // this allow to process empty string as an
// argument
aList.add(buffer.toString()); aList.add(buffer.toString());
} }
return aList.toArray(new String[aList.size()]); return aList.toArray(new String[aList.size()]);