summaryrefslogtreecommitdiff
path: root/tools/gnu
diff options
context:
space:
mode:
authorAndrew John Hughes <gnu_andrew@member.fsf.org>2008-06-05 23:58:45 +0000
committerAndrew John Hughes <gnu_andrew@member.fsf.org>2008-06-05 23:58:45 +0000
commit1fa39147d72da64af6e348ec7c96264de4950bfc (patch)
treeba053311134e00f1d9e78d5d3aa8692cb3dce69d /tools/gnu
parentfc51dcf0d2d49ce24ea8d31af1535a646ae09577 (diff)
downloadclasspath-1fa39147d72da64af6e348ec7c96264de4950bfc.tar.gz
Applying Schuster's @file patch with amendments.
2008-06-06 Andrew John Hughes <gnu_andrew@member.fsf.org> * tools/gnu/classpath/tools/common/ClasspathToolParser.java: Fixed indentation and changed to use OptionException. * tools/gnu/classpath/tools/getopt/OptionException.java: (OptionException(String,Throwable)): Added. 2008-06-03 Robert Schuster <robertschuster@fsfe.org> * tools/gnu/classpath/tools/jar/Main.java: (run): Call different ClasspathToolParser.parse() variant. (getParser): Changed return type to ClasspathToolParser. * tools/gnu/classpath/tools/javah/GcjhMain.java: (getParser): Changed return type to ClasspathToolParser. * tools/gnu/classpath/tools/javah/Main.java: (getParser): Changed return type to ClasspathToolParser. * tools/gnu/classpath/tools/getopt/Parser.java: Make 'programName' protected. * tools/gnu/classpath/tools/common/ClasspathToolParser.java: (parse(String[], FileArgumentCallback,boolean): New method. (parse(String[], boolean): New method. (parseFileList): New method. (parseLine): New method. (AtFileArgumentCallback): New inner class.
Diffstat (limited to 'tools/gnu')
-rw-r--r--tools/gnu/classpath/tools/common/ClasspathToolParser.java152
-rw-r--r--tools/gnu/classpath/tools/getopt/OptionException.java6
-rw-r--r--tools/gnu/classpath/tools/jar/Main.java8
-rw-r--r--tools/gnu/classpath/tools/javah/GcjhMain.java7
-rw-r--r--tools/gnu/classpath/tools/javah/Main.java6
5 files changed, 169 insertions, 10 deletions
diff --git a/tools/gnu/classpath/tools/common/ClasspathToolParser.java b/tools/gnu/classpath/tools/common/ClasspathToolParser.java
index e44b9011c..b2e508729 100644
--- a/tools/gnu/classpath/tools/common/ClasspathToolParser.java
+++ b/tools/gnu/classpath/tools/common/ClasspathToolParser.java
@@ -38,9 +38,16 @@ exception statement from your version. */
package gnu.classpath.tools.common;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.Reader;
import java.text.MessageFormat;
+import java.util.ArrayList;
import gnu.classpath.Configuration;
+import gnu.classpath.tools.getopt.FileArgumentCallback;
import gnu.classpath.tools.getopt.Option;
import gnu.classpath.tools.getopt.OptionException;
import gnu.classpath.tools.getopt.Parser;
@@ -84,4 +91,149 @@ public class ClasspathToolParser
}
});
}
+
+ public void parse(String[] inArgs, FileArgumentCallback files,
+ boolean handleFileLists)
+ {
+ FileArgumentCallback cb;
+
+ if (handleFileLists)
+ cb = new AtFileArgumentCallback(files);
+ else
+ cb = files;
+
+ parse(inArgs, cb);
+ }
+
+ public String[] parse(String[] inArgs, boolean handleFileLists)
+ {
+ final ArrayList<String> fileResult = new ArrayList<String>();
+
+ final FileArgumentCallback cb = new FileArgumentCallback()
+ {
+ public void notifyFile(String fileArgument)
+ {
+ fileResult.add(fileArgument);
+ }
+ };
+
+ if (handleFileLists)
+ parse(inArgs, new AtFileArgumentCallback(cb));
+ else
+ parse(inArgs, cb);
+
+ return fileResult.toArray(new String[fileResult.size()]);
+ }
+
+
+ /**
+ * Simple function that takes the given {@link Reader}, treats it like
+ * a textfile and reads all the whitespace separated entries from it
+ * and adds them to the @{link FileArgumentCallback} instance.
+ *
+ * @param reader the reader to read from.
+ * @param cb the callback to post the filenames to.
+ * @throws OptionException if an error occurs reading the list.
+ */
+ public void parseFileList(Reader reader, FileArgumentCallback cb)
+ throws OptionException
+ {
+ BufferedReader breader = new BufferedReader(reader);
+ String line = null;
+
+ try
+ {
+ while ((line = breader.readLine()) != null)
+ parseLine(line, cb);
+
+ reader.close();
+ }
+ catch (IOException ioe)
+ {
+ throw new OptionException("I/O error while reading a file list", ioe);
+ }
+
+ }
+
+ /**
+ * Parses whitespace separated file entries.
+ *
+ * Note: This is not coping with whitespace in files or quoting.
+ *
+ * @param line the line of the file to parse.
+ * @param cb the callback to pass the parsed file to.
+ * @throws IOException if an I/O error occurs.
+ * @throws OptionException if an error occurs in the callback.
+ */
+ private void parseLine(String line, FileArgumentCallback cb)
+ throws IOException, OptionException
+ {
+ final int length = line.length();
+ int start = 0;
+ int end = 0;
+
+ // While not reached end of line ...
+ while (start < length)
+ {
+ // Search for first non-whitespace character for the start of a word.
+ while (Character.isWhitespace(line.codePointAt(start)))
+ {
+ start++;
+
+ if (start == length)
+ return;
+ }
+
+ end = start + 1;
+
+ // Search for first whitespace character for the end of a word.
+ while (end < length && !Character.isWhitespace(line.codePointAt(end)))
+ end++;
+
+ cb.notifyFile(line.substring(start, end));
+
+ start = end + 1;
+ }
+ }
+
+ /**
+ * Implementation of {@link FileArgumentCallback} that handles
+ * file arguments in {@link #notifyFile} starting with a <code>@</code>
+ * through {@link ClasspathToolParser#parseFileList}.
+ */
+ class AtFileArgumentCallback extends FileArgumentCallback
+ {
+ FileArgumentCallback cb;
+
+ AtFileArgumentCallback(FileArgumentCallback cb)
+ {
+ this.cb = cb;
+ }
+
+ @Override
+ public void notifyFile(String fileArgument)
+ throws OptionException
+ {
+ if (fileArgument.codePointAt(0) == '@')
+ {
+ FileReader fr = null;
+
+ try
+ {
+ fr = new FileReader(fileArgument.substring(1));
+ }
+ catch (FileNotFoundException fnfe)
+ {
+ throw new OptionException("File not found: " + fileArgument.substring(1),
+ fnfe);
+ }
+
+ ClasspathToolParser.this.parseFileList(fr, cb);
+ }
+ else
+ cb.notifyFile(fileArgument);
+ }
+
+ }
+
}
diff --git a/tools/gnu/classpath/tools/getopt/OptionException.java b/tools/gnu/classpath/tools/getopt/OptionException.java
index a09d716f4..2d7f77a55 100644
--- a/tools/gnu/classpath/tools/getopt/OptionException.java
+++ b/tools/gnu/classpath/tools/getopt/OptionException.java
@@ -49,4 +49,10 @@ public class OptionException
{
super(message);
}
+
+ public OptionException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
}
diff --git a/tools/gnu/classpath/tools/jar/Main.java b/tools/gnu/classpath/tools/jar/Main.java
index d52028fef..e5f1a3fb1 100644
--- a/tools/gnu/classpath/tools/jar/Main.java
+++ b/tools/gnu/classpath/tools/jar/Main.java
@@ -172,9 +172,9 @@ public class Main
}
}
- private Parser initializeParser()
+ private ClasspathToolParser initializeParser()
{
- Parser p = new JarParser("jar"); //$NON-NLS-1$
+ ClasspathToolParser p = new JarParser("jar"); //$NON-NLS-1$
p.setHeader(Messages.getString("Main.Usage")); //$NON-NLS-1$
OptionGroup grp = new OptionGroup(Messages.getString("Main.OpMode")); //$NON-NLS-1$
@@ -265,11 +265,11 @@ public class Main
private void run(String[] args)
throws InstantiationException, IllegalAccessException, IOException
{
- Parser p = initializeParser();
+ ClasspathToolParser p = initializeParser();
// Special hack to emulate old tar-style commands.
if (args.length > 0 && args[0].charAt(0) != '-')
args[0] = '-' + args[0];
- p.parse(args, new HandleFile());
+ p.parse(args, new HandleFile(), true);
if (readNamesFromStdin)
readNames();
Action t = (Action) operationMode.newInstance();
diff --git a/tools/gnu/classpath/tools/javah/GcjhMain.java b/tools/gnu/classpath/tools/javah/GcjhMain.java
index 7faed1691..15bcec263 100644
--- a/tools/gnu/classpath/tools/javah/GcjhMain.java
+++ b/tools/gnu/classpath/tools/javah/GcjhMain.java
@@ -38,10 +38,11 @@
package gnu.classpath.tools.javah;
+import gnu.classpath.tools.common.ClasspathToolParser;
+
import gnu.classpath.tools.getopt.Option;
import gnu.classpath.tools.getopt.OptionException;
import gnu.classpath.tools.getopt.OptionGroup;
-import gnu.classpath.tools.getopt.Parser;
import java.io.IOException;
import java.util.ArrayList;
@@ -60,9 +61,9 @@ public class GcjhMain extends Main
return "gcjh";
}
- protected Parser getParser()
+ protected ClasspathToolParser getParser()
{
- Parser result = super.getParser();
+ ClasspathToolParser result = super.getParser();
result.setHeader("usage: gcjh [OPTION]... CLASS...");
diff --git a/tools/gnu/classpath/tools/javah/Main.java b/tools/gnu/classpath/tools/javah/Main.java
index 66453b3bc..bfca44446 100644
--- a/tools/gnu/classpath/tools/javah/Main.java
+++ b/tools/gnu/classpath/tools/javah/Main.java
@@ -188,7 +188,7 @@ public class Main
return "javah";
}
- protected Parser getParser()
+ protected ClasspathToolParser getParser()
{
ClasspathToolParser result = new ClasspathToolParser(getName(), true);
result.setHeader("usage: javah [OPTIONS] CLASS...");
@@ -339,8 +339,8 @@ public class Main
protected void run(String[] args) throws IOException
{
- Parser p = getParser();
- String[] classNames = p.parse(args);
+ ClasspathToolParser p = getParser();
+ String[] classNames = p.parse(args, true);
postParse(classNames);
loader = classpath.getLoader();