00001 /************************************************************************** 00002 Copyright (c) 2000-2001, Tony Garnock-Jones 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are 00007 met: 00008 00009 * Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 00012 * Redistributions in binary form must reproduce the above 00013 copyright notice, this list of conditions and the following 00014 disclaimer in the documentation and/or other materials provided 00015 with the distribution. 00016 00017 * Neither the names of the copyright holders nor the names of this 00018 software's contributors may be used to endorse or promote 00019 products derived from this software without specific prior 00020 written permission. 00021 00022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00025 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 00026 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00027 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00028 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 **************************************************************************/ 00034 00035 #ifndef __WIN32__ 00036 #error "You won't need this except on systems missing a native getopt()." 00037 #endif 00038 00039 #include <stdlib.h> 00040 #include <stdio.h> 00041 #include <string.h> 00042 00043 #include <mps/mps.h> 00044 00045 char *MPS_optarg = 0; 00046 int MPS_optind = 1; 00047 int MPS_opterr = 0; 00048 int MPS_optopt = '?'; 00049 00050 static char *nextchar = 0; 00051 00052 extern "C" int MPS_getopt(int argc, char *argv[], char *optspec) { 00053 if (!nextchar || *nextchar == '\0') { 00054 if (argv[MPS_optind] == 0 || 00055 argv[MPS_optind][0] != '-' || 00056 !strcmp(argv[MPS_optind], "-")) { 00057 return -1; 00058 } 00059 00060 if (!strcmp(argv[MPS_optind], "--")) { 00061 MPS_optind++; 00062 return -1; 00063 } 00064 00065 nextchar = argv[MPS_optind++] + 1; 00066 } 00067 00068 MPS_optopt = *nextchar++; 00069 00070 char *optloc = strchr(optspec, MPS_optopt); 00071 if (optloc == 0) { 00072 return '?'; 00073 } 00074 00075 if (optloc[1] == ':') { 00076 if (*nextchar) { 00077 MPS_optarg = nextchar; 00078 } else { 00079 MPS_optarg = argv[MPS_optind++]; 00080 if (MPS_optarg == 0) { 00081 // Missing argument. 00082 return (*optspec == ':') ? ':' : '?'; 00083 } 00084 } 00085 nextchar = 0; 00086 } 00087 00088 return MPS_optopt; 00089 }