BRL-CAD
getopt.c
Go to the documentation of this file.
1 /* G E T O P T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "bu/getopt.h"
27 
28 /* globals available: bu_opterr, bu_optind, bu_optopt, bu_optarg
29  * see globals.c for details
30  */
31 
32 #define BADCH (int)'?'
33 #define EMSG ""
34 #define tell(s) if (bu_opterr) { \
35  fputs(*nargv, stderr); \
36  fputs(s, stderr); \
37  fputc(bu_optopt, stderr); \
38  fputc('\n', stderr); \
39  } return BADCH;
40 
41 
42 int
43 bu_getopt(int nargc, char * const nargv[], const char *ostr)
44 {
45  static const char *place = EMSG; /* option letter processing */
46  register const char *oli; /* option letter list index */
47 
48  if (*place=='\0') {
49  /* update scanning pointer */
50  if (bu_optind >= nargc
51  || *(place = nargv[bu_optind]) != '-'
52  || !*++place)
53  {
54  place = EMSG;
55  return -1;
56  }
57  if (*place == '-') {
58  /* found "--" */
59  place = EMSG;
60  ++bu_optind;
61  return -1;
62  }
63  } /* option letter okay? */
64 
65  bu_optopt = (int)*place++;
66  oli = strchr(ostr, bu_optopt);
67  if (bu_optopt == (int)':' || !oli) {
68  ++bu_optind;
69  place = EMSG;
70  tell(": illegal option -- ");
71  }
72  if (*++oli != ':') {
73  /* don't need argument */
74  bu_optarg = NULL;
75  if (*place == '\0') {
76  ++bu_optind;
77  place = EMSG;
78  }
79  } else {
80  /* need an argument */
81  if (*place) {
82  /* no white space */
83  bu_optarg = (char *)place;
84  } else if (nargc <= ++bu_optind) {
85  /* no arg */
86  place = EMSG;
87  tell(": option requires an argument -- ");
88  } else {
89  /* white space */
90  bu_optarg = nargv[bu_optind];
91  }
92  place = EMSG;
93  ++bu_optind;
94  }
95  return bu_optopt; /* dump back option letter */
96 }
97 
98 
99 /*
100  * Local Variables:
101  * mode: C
102  * tab-width: 8
103  * indent-tabs-mode: t
104  * c-file-style: "stroustrup"
105  * End:
106  * ex: shiftwidth=4 tabstop=8
107  */
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
char * strchr(const char *sp, int c)
int bu_optopt
Definition: globals.c:90
#define EMSG
Definition: getopt.c:33
#define tell(s)
Definition: getopt.c:34