// main.beh -- the Demeter/Java front end. // $Id: main.beh,v 1.78 2003/01/03 11:01:17 dougo Exp $ Main { (@ static final String version = "0.8.6"; static final String optionPrefix = "-"; // "/" on windows? static final String usage = " demeterj [ " + optionPrefix + "project projname ]" + " [ " + optionPrefix + "verbose ]" + " [ " + optionPrefix + "debug ]" + " [ " + optionPrefix + "noblock ]" + " [ " + optionPrefix + "tie ]" + Line.nl + " [ module [ module-options ] ]" + Line.nl + " demeterj " + optionPrefix + "version" + Line.nl + " demeterj " + optionPrefix + "help"; static final Modules modules = Modules.parse("new studio generate weave parsegen compile test clean ridl"); static ProjectName projectName = null; static Module module = null, defaultModule = new Compile(); static Arguments moduleArguments = new Arguments(); static boolean compat = false; @) public static void main(String args[]) (@ tieIfNeeded(args); printVersion(); int exitval = -1; if (processArgs(args)) { Stream.debug.println(System.getProperty("os.name") + " " + System.getProperty("os.arch") + " " + System.getProperty("os.version")); Project project = module.getProject(projectName); if (project != null) { if (module.run(project, moduleArguments) != null) exitval = 0; } } System.exit(exitval); @) /** Tie stderr to stdout, if the user requests it. (Mainly for Windows systems in which stderr is not redirectable.) */ static void tieIfNeeded(String args[]) (@ for (int i = 0; i < args.length; i++) { if (matchOption(args[i], "tie")) { Stream.err = Stream.out; System.setErr(System.out); } } @) /** Print the version number and copyright notice. */ static void printVersion() (@ Stream.err.println("DemeterJ version " + version); Stream.err.println("Copyright (c) 2003 Northeastern University"); @) /** Process each argument in turn. Return true iff there were no errors. */ static boolean processArgs(String args[]) (@ String cdfile = null; for (int i = 0; i < args.length; i++) { String arg = args[i]; if (module != null) { moduleArguments.addElement(arg); } else if (matchOption(arg, "tie")) { // We already did that. } else if (matchOption(arg, "version")) { // We already did that too, and don't do anything else. return false; } else if (matchOption(arg, "project")) { if (++i == arg.length()) { Stream.err.println(arg + ": Please supply the project name."); return false; } projectName = ProjectName.parse(args[i]); } else if (matchOption(arg, "verbose")) { Stream.verbose = Stream.err; } else if (matchOption(arg, "debug")) { Stream.debug = Stream.err; Stream.verbose = Stream.err; } else if (matchOption(arg, "noblock")) { Utils.block = false; } else if (matchOption(arg, "eofbug")) { Utils.eofbug = true; } else if (matchOption(arg, "help")) { printUsage(); return false; } else { module = modules.findModule(arg); if (module == null) { // Assume the option was meant for the module. moduleArguments.addElement(arg); if (arg.endsWith("cd")) { cdfile = arg; } } } } // No modules specified, so do the default. if (module == null) { if (cdfile == null) { module = defaultModule; } else { // No module specified, found a cdfile-- backwards // compatibility mode, just run the generator. compat = true; module = new Generate(); } } return true; @) /** Does the argument match the option? */ static boolean matchOption(String arg, String option) (@ // Any nonempty prefix of the option name matches, ignoring case. int len = arg.length(); int pre = optionPrefix.length(); int opt = option.length(); return arg.startsWith(optionPrefix) && len > pre && len - pre <= opt && option.regionMatches(true, 0, arg, pre, len - pre); @) /** Print usage info. */ static void printUsage() (@ Stream.err.println("Usage:"); Stream.err.println(usage); Stream.err.println("Modules:"); Stream.err.println(modules.descriptions()); Stream.err.println("Each module runs preceding modules that it depends on, if needed."); Stream.err.println("If no module is specified, the \"" + defaultModule + "\" module will be run."); @) } // Backwards compatibility, for scripts that do "java ...demeterj.Program" Program { public static void main(String args[]) (@ Main.main(args); @) }