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: package com.sun.tools.javah;
027:
028: import com.sun.javadoc.*;
029: import java.io.*;
030:
031: /**
032: * A doclet to parse and execute commandline options.
033: *
034: * @author Sucheta Dambalkar(using code from old javap)
035: */
036: public class MainDoclet {
037:
038: public static String odir = null;
039: public static String ofile = null;
040: public static boolean stubs = false;
041: public static boolean jni = false;
042: public static boolean llni = false;
043: public static boolean doubleAlign = false;
044: public static boolean force = false;
045: public static String genclass = null;
046:
047: /**
048: * Entry point.
049: */
050: public static boolean start(RootDoc root) {
051:
052: int j = 0;
053: int k = 0;
054: /**
055: * Command line options.
056: */
057: String[][] cmdoptions = root.options();
058: /**
059: * Classes specified on command line.
060: */
061: ClassDoc[] classes = root.classes();
062: /**
063: * Generator used by javah. Default is JNI.
064: */
065: Gen g = new JNI(root);
066:
067: validateOptions(cmdoptions);
068:
069: /*
070: * Select native interface.
071: */
072: if (jni && llni)
073: Util.error("jni.llni.mixed");
074:
075: if (llni)
076: g = new LLNI(doubleAlign, root);
077:
078: if (g instanceof JNI && stubs)
079: 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
156: return 0;
157: }
158:
159: /**
160: * Parse the command line options.
161: */
162: public static void validateOptions(String cmdoptions[][]) {
163: /* Default values for options, overridden by user options. */
164: String bootcp = System.getProperty("sun.boot.class.path");
165: String usercp = System.getProperty("env.class.path");
166:
167: for (int p = 0; p < cmdoptions.length; p++) {
168:
169: if (cmdoptions[p][0].equals("-o")) {
170: ofile = cmdoptions[p][1];
171: } else if (cmdoptions[p][0].equals("-d")) {
172: odir = cmdoptions[p][1];
173: } else if (cmdoptions[p][0].equals("-td")) {
174: if (p == cmdoptions.length)
175: Util.usage(1);
176: } else if (cmdoptions[p][0].equals("-stubs")) {
177: stubs = true;
178: } else if (cmdoptions[p][0].equals("-verbose")) {
179: Util.verbose = true;
180: } else if ((cmdoptions[p][0].equals("-help"))
181: || (cmdoptions[p][0].equals("--help"))
182: || (cmdoptions[p][0].equals("-?"))
183: || (cmdoptions[p][0].equals("-h"))) {
184: Util.usage(0);
185: } else if (cmdoptions[p][0].equals("-trace")) {
186: System.err.println(Util
187: .getText("tracing.not.supported"));
188: } else if (cmdoptions[p][0].equals("-version")) {
189: Util.version();
190: } else if (cmdoptions[p][0].equals("-jni")) {
191: jni = true;
192: } else if (cmdoptions[p][0].equals("-force")) {
193: force = true;
194: } else if (cmdoptions[p][0].equals("-Xllni")) {
195: llni = true;
196: } else if (cmdoptions[p][0].equals("-llni")) {
197: llni = true;
198: } else if (cmdoptions[p][0].equals("-llniDouble")) {
199: llni = true;
200: doubleAlign = true;
201: } else if (cmdoptions[p][0].equals("-classpath")) {
202: usercp = cmdoptions[p][1];
203: } else if (cmdoptions[p][0].equals("-bootclasspath")) {
204: bootcp = cmdoptions[p][1];
205: } else if ((cmdoptions[p][0].charAt(0) == '-')
206: && (!cmdoptions[p][0].equals("-private"))) {
207: Util.error("unknown.option", cmdoptions[p][0], null,
208: true);
209: } else {
210: break; /* The rest must be classes. */
211: }
212: }
213:
214: if (Util.verbose) {
215: System.err.println("[ Search Path: " + bootcp
216: + System.getProperty("file.separator") + usercp
217: + " ]");
218: }
219: }
220: }
|