getopt.c

Go to the documentation of this file.
00001 /*                        G E T O P T . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup getopt */
00023 /*@{*/
00024 /** @file getopt.c
00025  * @brief
00026  *  Special re-entrant version of getopt.
00027  *
00028  *  Everything is prefixed with bu_, to distinguish it from the various
00029  *  getopt() routines found in libc.
00030  *
00031  *  Important note -
00032  *      If bu_getopt() it going to be used more than once, it is necessary
00033  *      to reinitialize bu_optind=1 before beginning on the next argument list.
00034  */
00035 
00036 
00037 #ifndef lint
00038 static const char libbu_getopt_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/getopt.c,v 14.11 2006/08/31 05:50:24 lbutler Exp $ (BRL)";
00039 #endif
00040 
00041 #include "common.h"
00042 
00043 #include <stdio.h>
00044 #ifdef HAVE_STRING_H
00045 #  include <string.h>
00046 #else
00047 #  include <strings.h>
00048 #endif
00049 
00050 #include "machine.h"
00051 #include "bu.h"
00052 
00053 
00054 int     bu_opterr = 1;          /**< set to zero to suppress errors */
00055 int     bu_optind = 1;          /**< index into parent argv vector */
00056 int     bu_optopt = 0;          /**< character checked for validity */
00057 char    *bu_optarg = NULL;      /**< argument associated with option */
00058 
00059 #define BADCH   (int)'?'
00060 #define EMSG    ""
00061 #define tell(s) if(bu_opterr)  { \
00062                 fputs(*nargv,stderr);fputs(s,stderr); \
00063                 fputc(bu_optopt,stderr);fputc('\n',stderr); \
00064         } return(BADCH);
00065 
00066 
00067 /**
00068  *                      B U _ G E T O P T
00069  *
00070  * get option letter from argument vector
00071  */
00072 int
00073 bu_getopt(int nargc, char *const *nargv, const char *ostr)
00074 {
00075     static char *place = EMSG;  /* option letter processing */
00076     register char       *oli;           /* option letter list index */
00077 
00078     if(*place=='\0') {                  /* update scanning pointer */
00079         if(bu_optind >= nargc || *(place = nargv[bu_optind]) != '-' ||
00080            !*++place)  {
00081             place = EMSG;
00082             return(EOF);
00083         }
00084         if (*place == '-') {    /* found "--" */
00085             place = EMSG;
00086             ++bu_optind;
00087             return(EOF);
00088         }
00089     }                           /* option letter okay? */
00090     if ((bu_optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,bu_optopt))) {
00091 #if 0
00092         if(*place == '\0') {
00093             ++bu_optind;
00094             place = EMSG;
00095         }
00096 #else
00097         ++bu_optind;
00098         place = EMSG;
00099 #endif
00100         tell(": illegal option -- ");
00101     }
00102     if (*++oli != ':') {                /* don't need argument */
00103         bu_optarg = NULL;
00104         if (*place == '\0') {
00105             ++bu_optind;
00106             place = EMSG;
00107         }
00108     }
00109     else {                              /* need an argument */
00110         if (*place) bu_optarg = place;  /* no white space */
00111         else if (nargc <= ++bu_optind) {        /* no arg */
00112             place = EMSG;
00113             tell(": option requires an argument -- ");
00114         }
00115         else bu_optarg = nargv[bu_optind];      /* white space */
00116         place = EMSG;
00117         ++bu_optind;
00118     }
00119     return(bu_optopt);                  /* dump back option letter */
00120 }
00121 /*@}*/
00122 /*
00123  * Local Variables:
00124  * mode: C
00125  * tab-width: 8
00126  * c-basic-offset: 4
00127  * indent-tabs-mode: t
00128  * End:
00129  * ex: shiftwidth=4 tabstop=8
00130  */

Generated on Mon Sep 18 01:24:47 2006 for BRL-CAD by  doxygen 1.4.6