PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
getopt_long.c File Reference
#include "c.h"
#include "getopt_long.h"
Include dependency graph for getopt_long.c:

Go to the source code of this file.

Macros

#define BADCH   '?'
 
#define BADARG   ':'
 
#define EMSG   ""
 

Functions

int getopt_long (int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
 

Macro Definition Documentation

#define BADARG   ':'

Definition at line 42 of file getopt_long.c.

Referenced by getopt_long().

#define BADCH   '?'

Definition at line 41 of file getopt_long.c.

Referenced by getopt_long().

#define EMSG   ""

Definition at line 43 of file getopt_long.c.

Referenced by getopt_long().

Function Documentation

int getopt_long ( int  argc,
char *const  argv[],
const char *  optstring,
const struct option longopts,
int *  longindex 
)

Definition at line 57 of file getopt_long.c.

References BADARG, BADCH, EMSG, option::flag, flag(), option::has_arg, i, option::name, name, no_argument, optarg, opterr, optind, optopt, required_argument, and option::val.

Referenced by handle_args(), main(), parse_psql_options(), parseCommandLine(), and regression_main().

60 {
61  static char *place = EMSG; /* option letter processing */
62  char *oli; /* option letter list index */
63 
64  if (!*place)
65  { /* update scanning pointer */
66  if (optind >= argc)
67  {
68  place = EMSG;
69  return -1;
70  }
71 
72  place = argv[optind];
73 
74  if (place[0] != '-')
75  {
76  place = EMSG;
77  return -1;
78  }
79 
80  place++;
81 
82  if (place[0] && place[0] == '-' && place[1] == '\0')
83  { /* found "--" */
84  ++optind;
85  place = EMSG;
86  return -1;
87  }
88 
89  if (place[0] && place[0] == '-' && place[1])
90  {
91  /* long option */
92  size_t namelen;
93  int i;
94 
95  place++;
96 
97  namelen = strcspn(place, "=");
98  for (i = 0; longopts[i].name != NULL; i++)
99  {
100  if (strlen(longopts[i].name) == namelen
101  && strncmp(place, longopts[i].name, namelen) == 0)
102  {
103  int has_arg = longopts[i].has_arg;
104 
105  if (has_arg != no_argument)
106  {
107  if (place[namelen] == '=')
108  optarg = place + namelen + 1;
109  else if (optind < argc - 1 &&
110  has_arg == required_argument)
111  {
112  optind++;
113  optarg = argv[optind];
114  }
115  else
116  {
117  if (optstring[0] == ':')
118  return BADARG;
119 
120  if (opterr && has_arg == required_argument)
121  fprintf(stderr,
122  "%s: option requires an argument -- %s\n",
123  argv[0], place);
124 
125  place = EMSG;
126  optind++;
127 
128  if (has_arg == required_argument)
129  return BADCH;
130  optarg = NULL;
131  }
132  }
133  else
134  {
135  optarg = NULL;
136  if (place[namelen] != 0)
137  {
138  /* XXX error? */
139  }
140  }
141 
142  optind++;
143 
144  if (longindex)
145  *longindex = i;
146 
147  place = EMSG;
148 
149  if (longopts[i].flag == NULL)
150  return longopts[i].val;
151  else
152  {
153  *longopts[i].flag = longopts[i].val;
154  return 0;
155  }
156  }
157  }
158 
159  if (opterr && optstring[0] != ':')
160  fprintf(stderr,
161  "%s: illegal option -- %s\n", argv[0], place);
162  place = EMSG;
163  optind++;
164  return BADCH;
165  }
166  }
167 
168  /* short option */
169  optopt = (int) *place++;
170 
171  oli = strchr(optstring, optopt);
172  if (!oli)
173  {
174  if (!*place)
175  ++optind;
176  if (opterr && *optstring != ':')
177  fprintf(stderr,
178  "%s: illegal option -- %c\n", argv[0], optopt);
179  return BADCH;
180  }
181 
182  if (oli[1] != ':')
183  { /* don't need argument */
184  optarg = NULL;
185  if (!*place)
186  ++optind;
187  }
188  else
189  { /* need an argument */
190  if (*place) /* no white space */
191  optarg = place;
192  else if (argc <= ++optind)
193  { /* no arg */
194  place = EMSG;
195  if (*optstring == ':')
196  return BADARG;
197  if (opterr)
198  fprintf(stderr,
199  "%s: option requires an argument -- %c\n",
200  argv[0], optopt);
201  return BADCH;
202  }
203  else
204  /* white space */
205  optarg = argv[optind];
206  place = EMSG;
207  ++optind;
208  }
209  return optopt;
210 }
int val
Definition: getopt_long.h:21
#define BADCH
Definition: getopt_long.c:41
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
int optopt
Definition: getopt.c:51
char * flag(int b)
Definition: test-ctype.c:33
int opterr
Definition: getopt.c:50
const char * name
Definition: getopt_long.h:18
#define no_argument
Definition: getopt_long.h:24
int * flag
Definition: getopt_long.h:20
#define EMSG
Definition: getopt_long.c:43
const char * name
Definition: encode.c:521
char * optarg
Definition: getopt.c:53
int i
#define BADARG
Definition: getopt_long.c:42
int has_arg
Definition: getopt_long.h:19