1 22 23 package org.aspectj.debugger.base; 24 25 import java.util.*; 26 27 35 36 public class Options { 37 38 public Options() { 39 set("extra"); 40 } 41 42 public final static String helpString = 43 "Usage: ajdb <options> <class> <arguments>\n" + 44 "\n" + 45 "where options include:\n" + 46 " -help print out this message and exit\n" + 47 " -sourcepath <directory>\n" + 48 " directory in which to look for source files\n" + 49 " -read <file> read the startup file\n" + 67 " -gui start up in GUI mode\n" + 68 " -extra enable extra features unique to ajdb\n" + 69 " -workingdir set the user's workingdir" + 70 "\n" + 71 "options forwarded to debuggee process:\n" + 72 " -v -verbose[:class|gc|jni]\n" + 73 " turn on verbose mode\n" + 74 " -D<name>=<value> set a system property\n" + 75 " -classpath <directories separated by \";\">\n" + 76 " list directories in which to look for classes\n" + 77 " -X<option> non-standard target VM option\n" + 78 "\n" + 79 "<class> is the name of the class to begin debugging\n" + 80 "<arguments> are the arguments passed to the main() method of <class>\n" + 81 "\n" + 82 "For command help type 'help' at jdb prompt\n" + 83 ""; 84 85 public final static String helpString() { 86 return helpString; 87 } 88 89 private HashMap opts = new HashMap(); 90 91 private String className = ""; 92 public String getClassName() { 93 return className; 94 } 95 96 private String commandLine = ""; 97 public String getCommandLine() { 98 return commandLine; 99 } 100 101 public String getFullCommandLine() { 102 String line = ""; 103 if (!empty(className)) { 104 line += className; 105 } 106 if (!empty(commandLine)) { 107 line += " " + commandLine; 108 } 109 line = line.trim(); 110 System.out.println("line="+ line); 111 return line; 112 } 113 114 private static boolean empty(String str) { 115 return str == null || "".equals(str.trim()); 116 } 117 118 public final static String [] javaArgs = { 119 "classpath", 120 "v", 121 "verbose", 122 "verbose:class", 123 "verbose:gc", 124 "verbose:jni", 125 "X", 126 "D", 127 }; 128 129 public void fill(String str) { 130 Vector v = new Vector(); 131 StringTokenizer tok = new StringTokenizer(str); 132 String t; 133 while (tok.hasMoreTokens()) { 134 t = tok.nextToken(); 135 if (t.startsWith("\"")) { 136 String end; 137 boolean done = t.endsWith("\""); 138 while(!done && tok.hasMoreTokens()) { 139 t += (end = tok.nextToken().trim()); 140 if (end.endsWith("\"")) { 141 done = true; 142 } 143 } 144 } 145 v.add(t); 146 } 147 String [] args = new String [v.size()]; 148 Iterator iter = v.iterator(); 149 for (int i = 0; i < args.length; i++) { 150 args[i] = v.get(i) + ""; 151 } 152 fill(args); 153 154 } 155 156 public void fill(String [] args) { 157 realFill(removeNoWarn(args)); 158 Debug.setOptions(this); 159 } 160 161 private String [] removeNoWarn(String [] args) { 162 for (int i = 0; i < args.length; i++) { 163 String opt = args[i]; 164 if (opt(opt, "nowarn")) { 165 String [] newArgs = new String [args.length - 1]; 166 System.arraycopy(args, 0, newArgs, 0, i); 167 System.arraycopy(args, i+1, newArgs, i, args.length-i-1); 168 set("nowarn"); 169 return newArgs; 170 } 171 } 172 return args; 173 } 174 175 private void realFill(String [] args) { 176 int i = 0; 177 String opt = ""; 178 String arg = ""; 179 while (i < args.length) { 180 opt = args[i++]; 181 if (opt(opt, "extra")) { 182 set("extra"); 183 } else if (opt(opt, "now")) { 184 set("now"); 185 } else if (opt(opt, "noextra")) { 186 unset("extra"); 187 } else if (opt(opt, "nowarn")) { 188 set("nowarn"); 189 } else if (opt(opt, "help")) { 190 help(); 191 } else if (opt(opt, "sourcepath")) { 192 check(args, i, "-sourcepath"); 193 set("sourcepath", args[i++]); 194 } else if (opt(opt, "attach")) { 195 check(args, i, "-attach"); 196 set("attach", args[i++]); 197 } else if (opt(opt, "listen")) { 198 check(args, i, "-listen"); 199 set("listen", args[i++]); 200 } else if (opt(opt, "listenany")) { 201 set("listenany"); 202 } else if (opt(opt, "launch")) { 203 set("launch"); 204 } else if (opt(opt, "connect")) { 205 check(args, i, "-connect"); 206 set("connect", args[i++]); 207 } else if (opt(opt, "dbgtrace")) { 208 if (isArg(args, i+1)) { 209 set("dbgtrace", args[i++]); 210 } else { 211 set("dbgtrace"); 212 } 213 } else if (opt(opt, "thotspot")) { 214 set("thotspot"); 215 } else if (opt(opt, "tclassic")) { 216 set("tclassic"); 217 } else if (opt(opt, "v")) { 218 set("v"); 219 } else if (opt(opt, "verbose")) { 220 set("verbose"); 221 } else if (opt(opt, "verbose:class")) { 222 set("verbose:class"); 223 } else if (opt(opt, "verbose:gc")) { 224 set("verbose:gc"); 225 } else if (opt(opt, "verbose:jni")) { 226 set("verbose:jni"); 227 } else if (opt(opt, "demo")) { 228 set("demo"); 229 Debugger.demoMode = true; 230 } else if (opt(opt, "classpath")) { 231 check(args, i, "-classpath"); 232 set("classpath", args[i++]); 233 } else if (opt(opt, "workingdir")) { 234 check(args, i, "-workingdir"); 235 set("workingdir", args[i++]); 236 } else if (opt(opt, "read")) { 237 check(args, i, "-read"); 238 set("read", args[i++]); 239 } else if (opt(opt, "debug")) { 240 set("debug"); 241 } else if (opt.startsWith("-D")) { 242 arg = strip("-D", opt, "<name>=<value>"); 243 if (arg.indexOf("\"") != -1) { 244 while (i < args.length) { 245 String next = args[i++]; 246 arg += " " + next; 247 if (next.endsWith("\"")) { 248 break; 249 } 250 } 251 } 252 set("D", arg); 253 } else if (opt.startsWith("-X")) { 255 arg = strip("-X", opt, "<option>"); 256 set("X", arg); 257 } else if (isArg(opt)) { 258 warn(opt + " is not a valid option"); 259 set(deopt(opt)); 260 } else { 261 className = opt; 262 while (i < args.length) { 264 commandLine += " " + args[i++]; 265 } 266 } 267 } 268 } 269 270 private boolean opt(String opt, String str) { 271 return opt.equals("-" + str) || opt.equals("--" + str); 272 } 273 274 public void setClassName(String className) { 275 this.className = className; 276 } 277 278 public void setMainClass(String mainClass) { 279 setClassName(mainClass); 280 } 281 282 public void setClassPath(String classPath) { 283 set("classpath", classPath); 284 } 285 286 public void setSourcePath(String sourcePath) { 287 set("sourcepath", sourcePath); 288 } 289 290 public String getJavaArgs() { 291 String args = ""; 292 if (isSet("thotspot")) { 293 args += "-hotspot"; 294 } else if (isSet("tclassic")) { 295 args += "-classic"; 296 } 297 for (int i = 0; i < javaArgs.length; i++) { 298 if (isSet(javaArgs[i])) { 299 args += " " + format(javaArgs[i]); 300 } 301 } 302 return args.trim(); 303 } 304 305 308 public String deopt(String opt) { 309 for (int i = 0; i < opt.length(); i++) { 310 if (opt.charAt(i) != '-' && opt.length() >= i) { 311 return opt.substring(i); 312 } 313 } 314 return opt; 315 } 316 317 public String opt(String opt) { 318 return "-" + opt; 319 } 320 321 328 public String format(String opt) { 329 StringBuffer sb = new StringBuffer (); 330 if (isSet(opt)) { 331 if (isListOption(opt)) { 332 List list = (List) get(opt); 333 if (null == list) { 334 warn(opt + " set but no list"); 335 } else { 336 for (Iterator it = list.iterator(); it.hasNext();) { 337 sb.append(" -" + opt + it.next()); 338 } 339 } 340 } else { 341 sb.append(opt(opt)); 342 String value = getOpt(opt); 343 if ((null != value) && (0 < value.length())) { 344 sb.append(" "); 345 sb.append(value); 346 } 347 } 348 } 349 return sb.toString(); 350 } 351 352 public boolean isSet(String opt) { 353 return (get(opt) != null); 354 } 355 356 363 public void set(String opt, String arg) { 364 if (isListOption(opt)) { 365 setListOption(opt, arg); 366 } else { 367 if (hasWhitespace(arg)) { 368 arg = quote(arg); 369 } 370 Object value = get(opt); 371 if ((value != null) && (!("debugging-for-jeff".equals(value)))) { 372 warn("Resetting option '" + opt + "' to '" + arg + "'."); 373 } 374 opts.put(opt, arg); 375 } 376 } 377 378 384 public boolean isListOption(String opt) { 385 return ("D".equals(opt) || "X".equals(opt)); 386 } 387 388 394 public void setListOption(String opt, String arg) { 395 if (!isListOption(opt)) { 396 String args = "(\"" + opt + "\", \"" + arg + "\")"; 397 warn("Options.setListOption" + args + ": not a list option"); 398 return; 400 } 401 if (hasWhitespace(arg)) { 402 arg = quote(arg); 403 } 404 Object value = opts.get(opt); 405 if (null == value) { 406 value = new ArrayList(); 407 opts.put(opt, value); 408 } 409 List list = (List) value; 410 list.add(arg); 411 } 412 413 public void set(String opt) { 414 set(opt, ""); 415 } 416 417 public void unset(String opt) { 418 if (isListOption(opt)) { 419 warn("Options.unset(\"" + opt + "\"): unsetting list"); 420 } 421 set(opt, null); 422 } 423 424 public String quote(String arg) { 425 return "\"" + arg + "\""; 426 } 427 428 public boolean hasWhitespace(String str) { 429 for (int i = 0; i < str.length(); i++) { 430 if (Character.isWhitespace(str.charAt(i))) { 431 return true; 432 } 433 } 434 return false; 435 } 436 437 public Object get(String opt) { 438 return opts.get(opt); 439 } 440 441 public String getOpt(String opt) { 442 return get(opt) + ""; 443 } 444 445 446 public String toString() { 447 return opts + ""; 448 } 449 450 String strip(String start, String opt, String syntax) { 451 int pos = start.length(); 452 String str = ""; 453 if (opt.length() < pos) { 454 fail("Correct syntax: " + opt + syntax); 455 } else { 456 str = opt.substring(pos); 457 } 458 return str; 459 } 460 461 boolean isArg(String arg) { 462 return (arg != null && arg.startsWith("-")); 463 } 464 465 boolean isArg(String [] args, int i) { 466 return (args != null && isArg(args[i])); 467 } 468 469 void check(String [] args, int i, String s) { 470 if (args == null || 471 args.length <= i || 472 args[i] == null || 473 isArg(args[i])) { 474 fail("The option '" + s + "' needs an argument."); 475 } 476 } 477 478 void fail(Object o) { 479 outln(o); 480 exit(1); 481 } 482 483 void warn(Object o) { 484 if (false && !isSet("nowarn")) { 485 outln("Warning: " + o); 486 } 487 } 488 489 void exit(int i) { 490 System.exit(i); 491 } 492 493 void help() { 494 outln(helpString()); 495 } 496 497 void outln(Object o) { 498 System.err.println(o); 499 } 500 } 501 | Popular Tags |