001    /*
002     * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.apt.main;
027    
028    import java.io.IOException;
029    import java.io.Reader;
030    import java.io.FileReader;
031    import java.io.BufferedReader;
032    import java.io.StreamTokenizer;
033    import com.sun.tools.javac.util.ListBuffer;
034    
035    /**
036     * Various utility methods for processing Java tool command line arguments.
037     *
038     *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
039     *  you write code that depends on this, you do so at your own risk.
040     *  This code and its internal interfaces are subject to change or
041     *  deletion without notice.</b>
042     */
043    public class CommandLine {
044        /**
045         * Process Win32-style command files for the specified command line
046         * arguments and return the resulting arguments. A command file argument
047         * is of the form '@file' where 'file' is the name of the file whose
048         * contents are to be parsed for additional arguments. The contents of
049         * the command file are parsed using StreamTokenizer and the original
050         * '@file' argument replaced with the resulting tokens. Recursive command
051         * files are not supported. The '@' character itself can be quoted with
052         * the sequence '@@'.
053         */
054        public static String[] parse(String[] args)
055            throws IOException
056        {
057            ListBuffer<String> newArgs = new ListBuffer<String>();
058            for (int i = 0; i < args.length; i++) {
059                String arg = args[i];
060                if (arg.length() > 1 && arg.charAt(0) == '@') {
061                    arg = arg.substring(1);
062                    if (arg.charAt(0) == '@') {
063                        newArgs.append(arg);
064                    } else {
065                        loadCmdFile(arg, newArgs);
066                    }
067                } else {
068                    newArgs.append(arg);
069                }
070            }
071            return newArgs.toList().toArray(new String[newArgs.length()]);
072        }
073    
074        private static void loadCmdFile(String name, ListBuffer<String> args)
075            throws IOException
076        {
077            Reader r = new BufferedReader(new FileReader(name));
078            StreamTokenizer st = new StreamTokenizer(r);
079            st.resetSyntax();
080            st.wordChars(' ', 255);
081            st.whitespaceChars(0, ' ');
082            st.commentChar('#');
083            st.quoteChar('"');
084            st.quoteChar('\'');
085            while (st.nextToken() != st.TT_EOF) {
086                args.append(st.sval);
087            }
088            r.close();
089        }
090    }