G:/ScriptBasic/source/getopt.c

Go to the documentation of this file.
00001 /***************************************************************************\
00002 **                                                                         **
00003 **   Function name: getopt()                                               **
00004 **   Author:        Henry Spencer, UofT                                    **
00005 **   Coding date:   84/04/28                                               **
00006 **                                                                         **
00007 **   Description:                                                          **
00008 **                                                                         **
00009 **   Parses argv[] for arguments.                                          **
00010 **   Works with Whitesmith's C compiler.                                   **
00011 **                                                                         **
00012 **   Inputs   - The number of arguments                                    **
00013 **            - The base address of the array of arguments                 **
00014 **            - A string listing the valid options (':' indicates an       **
00015 **              argument to the preceding option is required, a ';'        **
00016 **              indicates an argument to the preceding option is optional) **
00017 **                                                                         **
00018 **   Outputs  - Returns the next option character,                         **
00019 **              '?' for non '-' arguments                                  **
00020 **              or ':' when there is no more arguments.                    **
00021 **                                                                         **
00022 **   Side Effects + The argument to an option is pointed to by 'optarg'    **
00023 **                                                                         **
00024 *****************************************************************************
00025 **                                                                         **
00026 **   REVISION HISTORY:                                                     **
00027 **                                                                         **
00028 **     DATE           NAME                        DESCRIPTION              **
00029 **   YY/MM/DD  ------------------   ------------------------------------   **
00030 **   88/10/20  Janick Bergeron      Returns '?' on unamed arguments        **
00031 **                                  returns '!' on unknown options         **
00032 **                                  and 'EOF' only when exhausted.         **
00033 **   88/11/18  Janick Bergeron      Return ':' when no more arguments      **
00034 **   89/08/11  Janick Bergeron      Optional optarg when ';' in optstring  **
00035 **   99/05/22  Peter Verhas         optarg is argument to be thread safe   **
00036 **   99/08/28  Peter Verhas         optind becomes argument                **
00037 **   02/11/28  Nigel Hathaway       MacOS variation                        **
00038 **                                                                         **
00039 \***************************************************************************/
00040 #include <stdio.h>
00041 #ifdef VMS
00042 #define index  strchr
00043 #endif
00044 #ifdef WIN32
00045 #define index  strchr
00046 #endif
00047 #ifdef __MACOS__
00048 #define index  strchr
00049 #endif
00050 
00051 /*FUNCTION*/
00052 char getoptt(int argc, 
00053             char **argv,
00054             char *optstring,
00055             char **optarg,
00056             int *poptind
00057   ){
00058 #define optind (*poptind)
00059         register int c;
00060         register char *place;
00061         extern char *index();
00062         static char *scan = NULL;
00063 
00064         *optarg = NULL;
00065 
00066         if (scan == NULL || *scan == '\0') {
00067 
00068                 if (optind == 0)
00069                         optind++;
00070                 if (optind >= argc)
00071                         return ':';
00072 
00073                 *optarg = place = argv[optind++];
00074                 if (place[0] != '-' || place[1] == '\0')
00075                         return '?';
00076                 if (place[1] == '-' && place[2] == '\0')
00077                         return '?';
00078                 scan = place + 1;
00079         }
00080 
00081         c = *scan++;
00082         place = index(optstring, c);
00083         if (place == NULL || c == ':' || c == ';') {
00084                 scan = NULL;
00085                 return '!';
00086         }
00087         if (*++place == ':') {
00088 
00089                 if (*scan != '\0') {
00090 
00091                         *optarg = scan;
00092                         scan = NULL;
00093 
00094                 }
00095                 else {
00096 
00097                         if (optind >= argc) {
00098                                 return '!';
00099                         }
00100                         *optarg = argv[optind];
00101                         optind++;
00102                 }
00103         }
00104         else if (*place == ';') {
00105 
00106                 if (*scan != '\0') {
00107 
00108                         *optarg = scan;
00109                         scan = NULL;
00110 
00111                 }
00112                 else {
00113 
00114                         if (optind >= argc || *argv[optind] == '-')
00115                                 *optarg = NULL;
00116                         else {
00117                                 *optarg = argv[optind];
00118                                 optind++;
00119                         }
00120                 }
00121         }
00122         return c;
00123 }

Generated on Sun Mar 12 23:56:30 2006 for ScriptBasic by  doxygen 1.4.6-NO