001    /*
002     * Copyright 2002-2003 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    
027    package com.sun.tools.javah;
028    
029    import com.sun.javadoc.*;
030    import java.io.*;
031    
032    /**
033     * A doclet to parse and execute commandline options.
034     *
035     * @author Sucheta Dambalkar(using code from old javap)
036     */
037    public class MainDoclet{
038    
039        public static  String  odir        = null;
040        public static  String  ofile       = null;
041        public static  boolean stubs       = false;
042        public static  boolean jni         = false;
043        public static  boolean llni        = false;
044        public static  boolean doubleAlign = false;
045        public static  boolean force       = false;
046        public static  String  genclass    = null;
047    
048    
049        /**
050         * Entry point.
051         */
052        public static boolean start(RootDoc root) {
053    
054            int j = 0;
055            int k = 0;
056            /**
057             * Command line options.
058             */
059            String [][] cmdoptions = root.options();
060            /**
061             * Classes specified on command line.
062             */
063            ClassDoc[] classes = root.classes();
064            /**
065             * Generator used by javah.  Default is JNI.
066             */
067            Gen g = new JNI(root);
068    
069            validateOptions(cmdoptions);
070    
071            /*
072             * Select native interface.
073             */
074            if (jni && llni)  Util.error("jni.llni.mixed");
075    
076            if (llni)
077                g = new LLNI(doubleAlign, root);
078    
079            if (g instanceof JNI && stubs)  Util.error("jni.no.stubs");
080    
081            /*
082             * Arrange for output destination.
083             */
084            if (odir != null && ofile != null)
085                Util.error("dir.file.mixed");
086    
087            if (odir != null)
088                g.setOutDir(odir);
089    
090            if (ofile != null)
091                g.setOutFile(ofile);
092    
093            /*
094             * Force set to false will turn off smarts about checking file
095             * content before writing.
096             */
097            g.setForce(force);
098    
099            /*
100             * Grab the rest of argv[] ... this must be the classes.
101             */
102            if (classes.length == 0){
103                Util.error("no.classes.specified");
104            }
105            /*
106             * Set classes.
107             */
108            g.setClasses(classes);
109    
110            try {
111                g.run();
112            } catch (ClassNotFoundException cnfe) {
113                Util.error("class.not.found", cnfe.getMessage());
114            } catch (IOException ioe) {
115                Util.error("io.exception", ioe.getMessage());
116            }
117    
118            return true;
119        }
120    
121        /**
122         * Required doclet method.
123         */
124        public static int optionLength(String option) {
125            if (option.equals("-o")) {
126                return 2;
127            } else if(option.equals("-d")){
128                return 2;
129            } else if (option.equals("-td")) {
130                return 1;
131            } else if (option.equals("-stubs")) {
132                return 1;
133            } else if(option.equals("-help")){
134                return 1;
135            } else if(option.equals("--help")){
136                return 1;
137            } else if(option.equals("-?")){
138                return 1;
139            } else if(option.equals("-h")){
140                return 1;
141            } else if(option.equals("-trace")){
142                return 1;
143            } else if(option.equals("-version")) {
144                return 1;
145            } else if(option.equals("-jni")){
146                return 1;
147            } else if(option.equals("-force")){
148                return 1;
149            } else if(option.equals("-Xllni")){
150                return 1;
151            } else if(option.equals("-llni")){
152                return 1;
153            } else if(option.equals("-llniDouble")){
154                return 1;
155            } else return 0;
156        }
157    
158        /**
159         * Parse the command line options.
160         */
161        public static void validateOptions(String cmdoptions[][]) {
162            /* Default values for options, overridden by user options. */
163            String bootcp = System.getProperty("sun.boot.class.path");
164            String  usercp = System.getProperty("env.class.path");
165    
166            for(int p = 0; p < cmdoptions.length; p++){
167    
168                if (cmdoptions[p][0].equals("-o")) {
169                    ofile = cmdoptions[p][1];
170                } else if(cmdoptions[p][0].equals("-d")){
171                    odir = cmdoptions[p][1];
172                } else if (cmdoptions[p][0].equals("-td")) {
173                    if (p ==cmdoptions.length)
174                        Util.usage(1);
175                } else if (cmdoptions[p][0].equals("-stubs")) {
176                    stubs = true;
177                } else if (cmdoptions[p][0].equals("-verbose")) {
178                    Util.verbose = true;
179                } else if((cmdoptions[p][0].equals("-help"))
180                          || (cmdoptions[p][0].equals("--help"))
181                          || (cmdoptions[p][0].equals("-?"))
182                          || (cmdoptions[p][0].equals("-h"))) {
183                    Util.usage(0);
184                } else if (cmdoptions[p][0].equals("-trace")) {
185                    System.err.println(Util.getText("tracing.not.supported"));
186                } else if (cmdoptions[p][0].equals("-version")) {
187                    Util.version();
188                } else if (cmdoptions[p][0].equals("-jni")) {
189                    jni = true;
190                } else if (cmdoptions[p][0].equals("-force")) {
191                    force = true;
192                } else if (cmdoptions[p][0].equals("-Xllni")) {
193                    llni = true;
194                } else if (cmdoptions[p][0].equals("-llni")) {
195                    llni = true;
196                } else if (cmdoptions[p][0].equals("-llniDouble")) {
197                    llni = true; doubleAlign = true;
198                } else if (cmdoptions[p][0].equals("-classpath")) {
199                    usercp = cmdoptions[p][1];
200                } else if (cmdoptions[p][0].equals("-bootclasspath")) {
201                    bootcp = cmdoptions[p][1];
202                } else if((cmdoptions[p][0].charAt(0) == '-')
203                          && (!cmdoptions[p][0].equals("-private"))){
204                    Util.error("unknown.option", cmdoptions[p][0], null, true);
205                } else {
206                    break; /* The rest must be classes. */
207                }
208            }
209    
210    
211            if (Util.verbose) {
212                    System.err.println("[ Search Path: "
213                                        + bootcp
214                                        + System.getProperty("file.separator")
215                                        + usercp + " ]");
216            }
217        }
218    }