001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.util;
007:
008: import java.io.*;
009:
010: public class Getopt {
011:
012: String argList[] = null; // the argument List to be parsed
013: String optStr = null; // the string of arguments in the form "ab:" see getopt man
014: public String optArg = null; // argument of an option
015: public int optInd = 0; // index of the option
016: int nbArgs = 0; // number of args in the argList
017: int optPos = 1; // position of option letter in current argument scanned
018:
019: static final int EOF = -1; // returned when no option left
020: static final int ERROR = -2; // returned on error
021:
022: public Getopt(String[] args, String opts) {
023: argList = args;
024: nbArgs = argList.length;
025: optStr = opts;
026: optInd = 0;
027: optPos = 1;
028: optArg = null;
029: }
030:
031: private void optError(String msg, char optLetter) {
032: //System.err.println("Getopt: " + msg + " / " + optLetter);
033: }
034:
035: public char getOptLetter() {
036: return argList[optInd].charAt(optPos);
037: }
038:
039: public int getopt() {
040: optArg = null;
041:
042: // checking boundaries cases
043: if (argList == null || optStr == null)
044: return EOF;
045: if (optInd < 0 || optInd >= nbArgs)
046: return EOF;
047:
048: // get current Arg out of the argList
049: String currentArg = argList[optInd];
050: int argLength = currentArg.length();
051:
052: // checking special cases
053: // if arg starts with optLetter "a", "xyz"..
054: // if arg only 1 char in length "a", "-"
055: if (argLength <= 1 || currentArg.charAt(0) != '-') {
056: return EOF;
057: } else if (currentArg.equals("--")) { // end of options case
058: optInd++;
059: return EOF;
060: }
061:
062: // get next "letter" from option argument
063: char optLetter = currentArg.charAt(optPos);
064:
065: // find position of option in optStr
066: int pos = optStr.indexOf(optLetter);
067: if (pos == -1 || optLetter == ':') {
068: optError("illegal option", optLetter);
069: return ERROR;
070: } else { // check if option requires argument
071: if (pos < optStr.length() - 1
072: && optStr.charAt(pos + 1) == ':') {
073: // if remain characters after the current option in currentArg
074: if (optPos != argLength - 1) {
075: // take rest of current arg as the option argument
076: optArg = currentArg.substring(optPos + 1);
077: optPos = argLength - 1; // go to next arg below in argList
078: } else { // take next arg as optArg
079: optInd++;
080: if (optInd < nbArgs
081: && (argList[optInd].charAt(0) != '-' || argList[optInd]
082: .length() >= 2
083: && (optStr.indexOf(argList[optInd]
084: .charAt(1)) == -1 || argList[optInd]
085: .charAt(1) == ':'))) {
086: optArg = argList[optInd];
087: } else {
088: optError("option '" + optLetter
089: + "' requires an argument", optLetter);
090: optArg = null;
091: optLetter = '?';
092: }
093: }
094: }
095: }
096:
097: // next option argument,
098: // either in currentArg or next arg in argList
099: optPos++;
100:
101: // if no more option in currentArg
102: if (optPos >= argLength) {
103: optInd++;
104: optPos = 1; // reset postion of opt letter
105: }
106: return optLetter;
107: }
108:
109: }
|