/* $Id: getopt.c,v 1.3 2007/10/03 21:00:30 hvengel Exp $  */
//  Little cms - profiler construction set
//  Copyright (C) 1998-2001 Marti Maria
//  Copyright (C) 2006 Hal Engel
//
// THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
// LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
    getopt.c 

*/

#include <errno.h>
#include <string.h>
#include <stdio.h>
 
int     xoptind = 1;    /* index of which argument is next  */
char   *xoptarg;        /* pointer to argument of current option */
int     xopterr = 0;    /* allow error message  */
 
static  char   *letP = NULL;    /* remember next option char's location */
char    SW = '-';   /* DOS switch character, either '-' or '/' */

/*
  Parse the command line options, System V style.

  Standard option syntax is:

    option ::= SW [optLetter]* [argLetter space* argument]

*/

int xgetopt(int argc, char *argv[], char *optionS)
{
    unsigned char ch;
    char *optP;

    if (SW == 0) {
        SW = '/';
    }

    if (argc > xoptind) {
        if (letP == NULL) {
            if ((letP = argv[xoptind]) == NULL ||
                *(letP++) != SW)  goto gopEOF;
            if (*letP == SW) {
                xoptind++;  goto gopEOF;
            }
        }
        if (0 == (ch = *(letP++))) {
            xoptind++;  goto gopEOF;
        }
        if (':' == ch  ||  (optP = strchr(optionS, ch)) == NULL)
            goto gopError;
        if (':' == *(++optP)) {
            xoptind++;
            if (0 == *letP) {
                if (argc <= xoptind)  goto  gopError;
                letP = argv[xoptind++];
            }
            xoptarg = letP;
            letP = NULL;
        } else {
            if (0 == *letP) {
                xoptind++;
                letP = NULL;
            }
            xoptarg = NULL;
        }
        return ch;
    }
gopEOF:
    xoptarg = letP = NULL;
    return EOF;

gopError:
    xoptarg = NULL;
    errno  = EINVAL;
    if (xopterr)
        perror ("get command line option");
    return ('?');
}
