dol: initial dol commit
[jump.git] / dol / src / dol / main / Options.java
1 /* $Id: Options.java 203 2010-10-11 08:59:47Z dchokshi $ */
2 package dol.main;
3
4 /**
5  * This class handles the command line options. It checks if an option is
6  * valid, and if so, it calls the appropriate method in the UserInterface,
7  * that reflects the global setting of DOL.
8  */
9 public class Options {
10
11     /**
12      * Parse the command-line arguments, creating models as specified. Then
13      * execute each model that contains a manager.
14      *
15      * @param args
16      *            The command-line arguments.
17      * @exception IllegalArgumentException
18      *            MyException If such and such occurs
19      * @throws IllegalArgumentException
20      *             if an illegal argument is found on the command line.
21      */
22     public Options(String args[]) throws IllegalArgumentException, NumberFormatException {
23         _ui = UserInterface.getInstance();
24         if(args != null) {
25             _parseArgs(args);
26         }
27     }
28
29     /**
30      * Parse the command-line arguments.
31      *
32      * @param args The arguments to be parsed
33      *
34      * @throws IllegalArgumentException
35      *             if an illegal argument is found on the command line.
36      */
37     protected void _parseArgs(String args[]) throws IllegalArgumentException,
38         NumberFormatException {
39         if(args.length > 0) {
40             for(int i = 0; i < args.length; i++ ) {
41                 String arg = args[i];
42                 if(_parseArg(arg) == false ) {
43                     if(arg.startsWith("-") && i < args.length - 1 ) {
44                         if(arg.equals("--platform") || arg.equals("-p") ) {
45                             _ui.setPlatformFileName(args[++i]);
46                         } else if(arg.equals("--processnetwork") || arg.equals("-P") ) {
47                             _ui.setNetworkFileName(args[++i]);
48                         } else if(arg.equals("--mapping") || arg.equals("-m") ) {
49                             _ui.setMappingFileName(args[++i]);
50                         } else if(arg.equals("--scheduler") || arg.equals("-s") ) {
51                             _ui.setSchedulerFileName(args[++i]);
52                         } else if(arg.equals("--systemC") || arg.equals("-C") ) {
53                             _ui.setCodeDirectoryName(args[++i]);
54                             _ui.setSystemCFlag();
55                         } else if(arg.equals("--HdS") || arg.equals("-H") ) {
56                             _ui.setHdsCodeDirectoryName(args[++i]);
57                             _ui.setHdsFlag();
58                         } else if(arg.startsWith("--rtems") || arg.startsWith("-R") ) {
59                             String bsp = arg;
60                             bsp = bsp.replaceAll("--rtems" , "");
61                             bsp = bsp.replaceAll("-R", "");
62                             if (bsp.equals("")) {
63                                 _ui.setRtemsBSP("pc386");
64                             } else if (bsp.equals("pc386") || bsp.equals("mparm")) {
65                                 _ui.setRtemsBSP(bsp);
66                             } else {
67                                 throw new IllegalArgumentException(
68                                         "Board support package \"" + bsp
69                                         + "\" not supported by code generation.");
70                             }
71                             _ui.setRtemsCodeDirectoryName(args[++i]);
72                             _ui.setRtemsFlag();
73                         } else if(arg.equals("--PaF") || arg.equals("-PF") ) {
74                             _ui.setPipeAndFilterCodeDirectoryName(args[++i]);
75                             _ui.setPipeAndFilterFlag();
76                         } else if(arg.equals("--protothread") || arg.equals("-PT") ) {
77                             _ui.setProtothreadCodeDirectoryName(args[++i]);
78                             _ui.setProtothreadFlag();
79                         } else if(arg.equals("--dotty") || arg.equals("-D")) {
80                             _ui.setDottyFileName(args[++i]);
81                         } else if(arg.equals("--profiling") || arg.equals("-T")) {
82                             _ui.setTraceName(args[++i]);
83                             _ui.setProfilingFlag();
84                         } else if (arg.equals("--vsplog") || arg.equals("-L")) {
85                             _ui.setVspLogFileName(args[++i]);
86                             _ui.setVspLogFlag();
87                         } else if (arg.equals("--workload") || arg.equals("-W")) {
88                             _ui.setWorkloadFileName(args[++i]);
89                             _ui.setWorkloadFlag();
90                         } else if(arg.equals("--xmlGen") || arg.equals("-G")) {
91                             _ui.setOutputFileName(args[++i]);
92                             _ui.setXmlGenFlag();
93                         } else if(arg.equals("--cbe") || arg.equals("-CBE")) {
94                                 _ui.setCbeCodeDirectoryName(args[++i]); 
95                                 _ui.setCbeFlag(); 
96                         } else if(arg.equals("--yapi") || arg.equals("-Y")) {
97                             _ui.setYapiCodeDirectoryName(args[++i]); 
98                             _ui.setYapiFlag(); 
99                         } else {
100                             throw new IllegalArgumentException("Unrecognized option: " + arg);
101                         }
102                     } else {
103                         throw new IllegalArgumentException("Unrecognized option: " + arg);
104                     }
105                 }
106             }
107         } else {
108             throw new IllegalArgumentException(_usage());
109         }
110     }
111
112     /**
113      * Parse a single commandline argument.
114      *
115      * @param arg commandline argument
116      * @return true if the argument is understood, false otherwise
117      * @throws IllegalArgumentExcecption thrown if an illegal argument is
118      *                                   found on the command line.
119      */
120     protected boolean _parseArg(String arg) throws IllegalArgumentException {
121         if(arg.equals("--help") || arg.equals("-h") ) {
122             //throw new IllegalArgumentException(_usage());
123             System.out.println(_usage() );
124             System.exit(0);
125         } else if(arg.equals("--version") || arg.equals("-V") ) {
126             System.out.println("DOL version 0.0.1\n");
127             System.exit(0);
128         } else if(arg.equals("--verbose") || arg.equals("-v") ) {
129             _ui.setVerboseFlag();
130         } else if(arg.equals("--check") || arg.equals("-c")) {
131             _ui.setCheckFlag();
132         } else if(arg.equals("--debug")) {
133             _ui.setDebugFlag();
134         } else if(arg.equals("")) { // Ignore blank argument.
135         } else { // Argument not recognized.
136             return false;
137         }
138         return true;
139     }
140
141     /**
142      * Return a string summarizing the command-line arguments.
143      *
144      * @return A usage string.
145      */
146     protected String _usage() {
147         String result = "Usage: " + _commandTemplate + "\n\n"
148             + "Options:\n";
149
150         int i;
151         for(i = 0; i < _commandOptions.length; i++) {
152             result += " " + _commandOptions[i][0] + "\tabbr["
153                 + _commandOptions[i][1] + " " + _commandOptions[i][2]
154                 + "]\n";
155         }
156         result += "\nBoolean flags:\n";
157         for(i = 0; i < _commandFlags.length; i++) {
158             result += " " + _commandFlags[i][0] + "\tabbr["
159                 + _commandFlags[i][1] + "]\n";
160         }
161         return result;
162     }
163
164     /**
165      * The command-line options that are either present or not. Give the full
166      * name preceded with '--' and abbreviated version.
167      */
168     protected String _commandFlags[][] = {
169         { "--check     ", "-c" },
170         { "--flatten   ", "-F" },
171         { "--help      ", "-h" },
172         { "--version   ", "-V" },
173         { "--verbose   ", "-v" },
174         { "--debug     ", "none" },
175     };
176
177     /**
178      * The command-line options that take arguments.
179      */
180     protected String _commandOptions[][] = {
181         { "--platform      ", "-p ",  "<FileName>" },
182         { "--processnetwork", "-P ",  "<FileName>" },
183         { "--mapping       ", "-m ",  "<FileName>" },
184         { "--scheduler     ", "-s ",  "<FileName>" },
185         { "--dotty         ", "-D ",  "<FileName>" },
186         { "--xmlGen        ", "-G",   "<FileName>" },
187         { "--systemC       ", "-C ",  "<DirectoryName>" },
188         { "--HdS           ", "-H ",  "<DirectoryName>" },
189         { "--PaF           ", "-PF",  "<DirectoryName>" },
190         { "--rtems         ", "-R",   "<DirectoryName>" },
191         { "--protothread   ", "-PT",  "<DirectoryName>" },
192         { "--profiling     ", "-T",   "<trace FileName>" },
193         { "--vsplog        ", "-L",   "<log FileName>" },
194         { "--cbe           ", "-CBE", "<DirectoryName>" },
195         { "--yapi          ", "-Y",   "<DirectoryName>" }
196     };
197
198     /**
199      * The form of the command line.
200      */
201     protected String _commandTemplate = "dol [ options ]";
202
203     /**
204      * The UserInterface object.
205      */
206     protected UserInterface _ui = null;
207 }