GnuCOBOL  2.0
A free COBOL compiler
cobgetopt.c
Go to the documentation of this file.
1 /* Getopt for GNU.
2  NOTE: getopt is part of the C library, so if you don't know what
3  "Keep this file name-space clean" means, talk to drepper@gnu.org
4  before changing it!
5  Copyright (C) 1987-2002,2011 Free Software Foundation, Inc.
6  This file is part of the GNU C Library.
7 
8  The GNU C Library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  The GNU C Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with the GNU C Library; if not, see
20  <http://www.gnu.org/licenses/>. */
21 
22 /*
23  Copyright (C) 2010-2012, 2014-2016 Free Software Foundation, Inc.
24  Modified for use in GnuCOBOL by Roger While, Simon Sobisch
25 */
26 
27 #include "config.h"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include <ctype.h>
34 
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 
39 #ifdef ENABLE_NLS
40 #include "lib/gettext.h"
41 #define _(msgid) gettext(msgid)
42 #define N_(msgid) gettext_noop(msgid)
43 #else
44 #define _(msgid) msgid
45 #define N_(msgid) msgid
46 #endif
47 
48 /* Force symbol exports */
49 #define COB_LIB_EXPIMP
50 #include "libcob.h"
51 
52 #define NONOPTION_P (argv[cob_optind][0] != '-' || argv[cob_optind][1] == '\0')
53 
54 
55 /* This version of 'getopt' appears to the caller like standard Unix 'getopt'
56  but it behaves differently for the user, since it allows the user
57  to intersperse the options with the other arguments.
58 
59  As 'getopt' works, it permutes the elements of ARGV so that,
60  when it is done, all the options precede everything else. Thus
61  all application programs are extended to handle flexible argument order.
62 
63  Setting the environment variable POSIXLY_CORRECT disables permutation.
64  Then the behavior is completely standard.
65 
66  GNU application programs can use a third alternative mode in which
67  they can distinguish the relative order of options and other arguments. */
68 
69 #include "cobgetopt.h"
70 
71 /* For communication from 'getopt' to the caller.
72  When 'getopt' finds an option that takes an argument,
73  the argument value is returned here.
74  Also, when 'ordering' is RETURN_IN_ORDER,
75  each non-option ARGV-element is returned here. */
76 
77 char *cob_optarg = NULL;
78 
79 /* Index in ARGV of the next element to be scanned.
80  This is used for communication to and from the caller
81  and for communication between successive calls to 'getopt'.
82 
83  On entry to 'getopt', zero means this is the first call; initialize.
84 
85  When 'getopt' returns -1, this is the index of the first of the
86  non-option elements that the caller should itself scan.
87 
88  Otherwise, 'optind' communicates from one call to the next
89  how much of ARGV has been scanned so far. */
90 
91 /* 1003.2 says this must be 1 before any call. */
92 int cob_optind = 1;
93 
94 /* Formerly, initialization of getopt depended on optind==0, which
95  causes problems with re-calling getopt as programs generally don't
96  know that. */
97 
98 static int cob_getopt_initialized = 0;
99 
100 /* The next char to be scanned in the option-element
101  in which the last option character we returned was found.
102  This allows us to pick up the scan where we left off.
103 
104  If this is zero, or a null string, it means resume the scan
105  by advancing to the next ARGV-element. */
106 
107 static char *nextchar = NULL;
108 
109 /* Callers store zero here to inhibit the error message
110  for unrecognized options. */
111 
112 int cob_opterr = 1;
113 
114 /* Set to an option character which was unrecognized.
115  This must be initialized on some systems to avoid linking in the
116  system's own getopt implementation. */
117 
118 int cob_optopt = '?';
119 
120 /* Describe how to deal with options that follow non-option ARGV-elements.
121 
122  If the caller did not specify anything,
123  the default is REQUIRE_ORDER if the environment variable
124  POSIXLY_CORRECT is defined, PERMUTE otherwise.
125 
126  REQUIRE_ORDER means don't recognize them as options;
127  stop option processing when the first non-option is seen.
128  This is what Unix does.
129  This mode of operation is selected by either setting the environment
130  variable POSIXLY_CORRECT, or using '+' as the first character
131  of the list of option characters.
132 
133  PERMUTE is the default. We permute the contents of ARGV as we scan,
134  so that eventually all the non-options are at the end. This allows options
135  to be given in any order, even with programs that were not written to
136  expect this.
137 
138  RETURN_IN_ORDER is an option available to programs that were written
139  to expect options and other ARGV-elements in any order and that care about
140  the ordering of the two. We describe each non-option ARGV-element
141  as if it were the argument of an option with character code 1.
142  Using '-' as the first character of the list of option characters
143  selects this mode of operation.
144 
145  The special argument '--' forces an end of option-scanning regardless
146  of the value of 'ordering'. In the case of RETURN_IN_ORDER, only
147  '--' can cause 'getopt' to return -1 with 'optind' != ARGC. */
148 
149 static enum {
151 } ordering;
152 
153 /* Handle permutation of arguments. */
154 
155 /* Describe the part of ARGV that contains non-options that have
156  been skipped. 'first_nonopt' is the index in ARGV of the first of them;
157  'last_nonopt' is the index after the last of them. */
158 
159 static int first_nonopt;
160 static int last_nonopt;
161 
162 static int seen_short = 0;
163 
164 /* Exchange two adjacent subsequences of ARGV.
165  One subsequence is elements [first_nonopt,last_nonopt)
166  which contains all the non-options that have been skipped so far.
167  The other is elements [last_nonopt,optind), which contains all
168  the options processed since those non-options were skipped.
169 
170  'first_nonopt' and 'last_nonopt' are relocated so that they describe
171  the new indices of the non-options in ARGV after they are moved. */
172 
173 static void
174 exchange (char **argv)
175 {
176  int bottom = first_nonopt;
177  int middle = last_nonopt;
178  int top = cob_optind;
179  char *tem;
180 
181  /* Exchange the shorter segment with the far end of the longer segment.
182  That puts the shorter segment into the right place.
183  It leaves the longer segment in the right place overall,
184  but it consists of two parts that need to be swapped next. */
185 
186  while (top > middle && middle > bottom)
187  {
188  if (top - middle > middle - bottom)
189  {
190  /* Bottom segment is the short one. */
191  int len = middle - bottom;
192  register int i;
193 
194  /* Swap it with the top part of the top segment. */
195  for (i = 0; i < len; i++)
196  {
197  tem = argv[bottom + i];
198  argv[bottom + i] = argv[top - (middle - bottom) + i];
199  argv[top - (middle - bottom) + i] = tem;
200 #if 0 /* RXWRXW - swap flags */
201  SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
202 #endif
203  }
204  /* Exclude the moved bottom segment from further swapping. */
205  top -= len;
206  }
207  else
208  {
209  /* Top segment is the short one. */
210  int len = top - middle;
211  register int i;
212 
213  /* Swap it with the bottom part of the bottom segment. */
214  for (i = 0; i < len; i++)
215  {
216  tem = argv[bottom + i];
217  argv[bottom + i] = argv[middle + i];
218  argv[middle + i] = tem;
219 #if 0 /* RXWRXW - swap flags */
220  SWAP_FLAGS (bottom + i, middle + i);
221 #endif
222  }
223  /* Exclude the moved top segment from further swapping. */
224  bottom += len;
225  }
226  }
227 
228  /* Update records for the slots the non-options now occupy. */
229 
232 }
233 
234 /* Initialize the internal data when the first call is made. */
235 
236 static const char *_getopt_initialize (const char *optstring)
237 {
238  /* Start processing options with ARGV-element 1 (since ARGV-element 0
239  is the program name); the sequence of previously skipped
240  non-option ARGV-elements is empty. */
241 
243 
244  nextchar = NULL;
245 
246  /* Determine how to handle the ordering of options and nonoptions. */
247 
248  if (optstring[0] == '-')
249  {
251  ++optstring;
252  }
253  else if (optstring[0] == '+')
254  {
256  ++optstring;
257  }
258  else
259  ordering = PERMUTE;
260 
261  return optstring;
262 }
263 
264 /* Scan elements of ARGV (whose length is ARGC) for option characters
265  given in OPTSTRING.
266 
267  If an element of ARGV starts with '-', and is not exactly "-" or "--",
268  then it is an option element. The characters of this element
269  (aside from the initial '-') are option characters. If 'getopt'
270  is called repeatedly, it returns successively each of the option characters
271  from each of the option elements.
272 
273  If 'getopt' finds another option character, it returns that character,
274  updating 'optind' and 'nextchar' so that the next call to 'getopt' can
275  resume the scan with the following option character or ARGV-element.
276 
277  If there are no more option characters, 'getopt' returns -1.
278  Then 'optind' is the index in ARGV of the first ARGV-element
279  that is not an option. (The ARGV-elements have been permuted
280  so that those that are not options now come last.)
281 
282  OPTSTRING is a string containing the legitimate option characters.
283  If an option character is seen that is not listed in OPTSTRING,
284  return '?' after printing an error message. If you set 'opterr' to
285  zero, the error message is suppressed but we still return '?'.
286 
287  If a char in OPTSTRING is followed by a colon, that means it wants an arg,
288  so the following text in the same ARGV-element, or the text of the following
289  ARGV-element, is returned in 'optarg'. Two colons mean an option that
290  wants an optional arg; if there is text in the current ARGV-element,
291  it is returned in 'optarg', otherwise 'optarg' is set to zero.
292 
293  If OPTSTRING starts with '-' or '+', it requests different methods of
294  handling the non-option ARGV-elements.
295  See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
296 
297  Long-named options begin with '--' instead of '-'.
298  Their names may be abbreviated as long as the abbreviation is unique
299  or is an exact match for some defined option. If they have an
300  argument, it follows the option name in the same ARGV-element, separated
301  from the option name by a '=', or else the in next ARGV-element.
302  When 'getopt' finds a long-named option, it returns 0 if that option's
303  'flag' field is nonzero, the value of the option's 'val' field
304  if the 'flag' field is zero.
305 
306  The elements of ARGV aren't really const, because we permute them.
307  But we pretend they're const in the prototype to be compatible
308  with other systems.
309 
310  LONGOPTS is a vector of 'struct option' terminated by an
311  element containing a name which is zero.
312 
313  LONGIND returns the index in LONGOPT of the long-named option found.
314  It is only valid when a long-named option has been found by the most
315  recent call.
316 
317  If LONG_ONLY is nonzero, '-' as well as '--' can introduce
318  long-named options. */
319 
320 int
321 cob_getopt_long_long (const int argc, char *const *argv, const char *optstring,
322  const struct option *longopts, int *longind,
323  const int long_only)
324 {
325  if (argc < 1)
326  return -1;
327 
328  cob_optarg = NULL;
329 
330  if (cob_optind == 0 || !cob_getopt_initialized)
331  {
332  if (cob_optind == 0)
333  cob_optind = 1; /* Don't scan ARGV[0], the program name. */
334  optstring = _getopt_initialize (optstring);
336  }
337 
338  /* Test whether ARGV[optind] points to a non-option argument.
339  Either it does not have option syntax, or there is an environment flag
340  from the shell indicating it is not an option. The later information
341  is only used when the used in the GNU libc. */
342 
343  if (nextchar == NULL || *nextchar == '\0')
344  {
345  /* Advance to the next ARGV-element. */
346 
347  seen_short = 0;
348  /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
349  moved back by the user (who may also have changed the arguments). */
350  if (last_nonopt > cob_optind)
352  if (first_nonopt > cob_optind)
354 
355  if (ordering == PERMUTE)
356  {
357  /* If we have just processed some options following some non-options,
358  exchange them so that the options come first. */
359 
361  exchange ((char **) argv);
362  else if (last_nonopt != cob_optind)
364 
365  /* Skip any additional non-options
366  and extend the range of non-options previously skipped. */
367 
368  while (cob_optind < argc && NONOPTION_P)
369  cob_optind++;
371  }
372 
373  /* The special ARGV-element '--' means premature end of options.
374  Skip it like a null option,
375  then exchange with previous non-options as if it were an option,
376  then skip everything else like a non-option. */
377 
378  if (cob_optind != argc && !strcmp (argv[cob_optind], "--"))
379  {
380  cob_optind++;
381 
382  if (first_nonopt != last_nonopt && last_nonopt != cob_optind)
383  exchange ((char **) argv);
384  else if (first_nonopt == last_nonopt)
386  last_nonopt = argc;
387 
388  cob_optind = argc;
389  }
390 
391  /* If we have done all the ARGV-elements, stop the scan
392  and back over any non-options that we skipped and permuted. */
393 
394  if (cob_optind == argc)
395  {
396  /* Set the next-arg-index to point at the non-options
397  that we previously skipped, so the caller will digest them. */
398  if (first_nonopt != last_nonopt)
399  cob_optind = first_nonopt;
400  return -1;
401  }
402 
403  /* If we have come to a non-option and did not permute it,
404  either stop the scan or describe it to the caller and pass it by. */
405 
406  if (NONOPTION_P)
407  {
408  if (ordering == REQUIRE_ORDER)
409  return -1;
410  cob_optarg = argv[cob_optind++];
411  return 1;
412  }
413 
414  /* We have found another option-ARGV-element.
415  Skip the initial punctuation. */
416 
417  nextchar = (argv[cob_optind] + 1
418  + (longopts != NULL && argv[cob_optind][1] == '-'));
419  }
420 
421  /* Decode the current option-ARGV-element. */
422 
423  /* Check whether the ARGV-element is a long option.
424 
425  If long_only and the ARGV-element has the form "-f", where f is
426  a valid short option, don't consider it an abbreviated form of
427  a long option that starts with f. Otherwise there would be no
428  way to give the -f short option.
429 
430  On the other hand, if there's a long option "fubar" and
431  the ARGV-element is "-fu", do consider that an abbreviation of
432  the long option, just like "--fu", and not "-f" with arg "u".
433 
434  This distinction seems to be the most useful approach. */
435 
436  if (longopts != NULL && (argv[cob_optind][1] == '-'
437  || (long_only && !seen_short && (argv[cob_optind][2] || !strchr (optstring, argv[cob_optind][1])))))
438  {
439  char *nameend;
440  unsigned int namelen;
441  const struct option *p;
442  const struct option *pfound = NULL;
443  struct option_list {
444  const struct option *p;
445  struct option_list *next;
446  } *ambig_list = NULL;
447  struct option_list *ambig_last = NULL;
448  int exact = 0;
449  int indfound = -1;
450  int option_index;
451 
452  for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
453  /* Do nothing. */ ;
454  namelen = nameend - nextchar;
455 
456  /* Test all long options for either exact match
457  or abbreviated matches. */
458  for (p = longopts, option_index = 0; p->name; p++, option_index++)
459  if (!strncmp (p->name, nextchar, namelen)) {
460  if (namelen == (unsigned int) strlen (p->name)) {
461  /* Exact match found. */
462  pfound = p;
463  indfound = option_index;
464  exact = 1;
465  break;
466  } else if (pfound == NULL) {
467  /* First nonexact match found. */
468  pfound = p;
469  indfound = option_index;
470  } else if (long_only
471  || pfound->has_arg != p->has_arg
472  || pfound->flag != p->flag
473  || pfound->val != p->val) {
474  /* Second or later nonexact match found. */
475  struct option_list *newp = cob_fast_malloc (sizeof (*newp));
476  newp->p = p;
477  newp->next = ambig_list;
478  ambig_list = newp;
479  ambig_last = ambig_list;
480  }
481  }
482 
483  if (ambig_list != NULL){
484  if (!exact) {
485  if (cob_opterr) {
486  struct option_list first;
487  first.p = pfound;
488  first.next = ambig_list;
489  ambig_list = &first;
490 
491  fprintf (stderr, _("%s: option '%s' is ambiguous; possibilities:"),
492  argv[0], argv[cob_optind]);
493 
494  do {
495  fprintf (stderr, " '--%s'", ambig_list->p->name);
496  ambig_list = ambig_list->next;
497  }
498  while (ambig_list != NULL);
499 
500  fputc ('\n', stderr);
501  }
502  }
503  while (ambig_last != NULL) {
504  ambig_list = ambig_last;
505  ambig_last = ambig_last->next;
506  cob_free (ambig_list);
507  }
508  if (!exact) {
509  nextchar += strlen (nextchar);
510  cob_optind++;
511  cob_optopt = 0;
512  return '?';
513  }
514  }
515 
516  if (pfound != NULL) {
517  option_index = indfound;
518  cob_optind++;
519  if (*nameend)
520  {
521  /* Don't test has_arg with >, because some C compilers don't
522  allow it to be used on enums. */
523  if (pfound->has_arg)
524  cob_optarg = nameend + 1;
525  else
526  {
527  if (cob_opterr)
528  {
529  if (argv[cob_optind - 1][1] == '-')
530  {
531  /* --option */
532  fprintf (stderr, _("%s: option '--%s' doesn't allow an argument"),
533  argv[0], pfound->name);
534  fputc ('\n', stderr);
535  }
536  else
537  {
538  /* +option or -option */
539  fprintf (stderr, _("%s: option '%c%s' doesn't allow an argument"),
540  argv[0], argv[cob_optind - 1][0], pfound->name);
541  fputc ('\n', stderr);
542  }
543  }
544 
545  nextchar += strlen (nextchar);
546 
547  cob_optopt = pfound->val;
548  return '?';
549  }
550  }
551  else if (pfound->has_arg == 1)
552  {
553  if (cob_optind < argc)
554  cob_optarg = argv[cob_optind++];
555  else
556  {
557  if (cob_opterr)
558  {
559  fprintf (stderr, _("%s: option '--%s' requires an argument"),
560  argv[0], argv[cob_optind - 1]);
561  fputc ('\n', stderr);
562  }
563  nextchar += strlen (nextchar);
564  cob_optopt = pfound->val;
565  return optstring[0] == ':' ? ':' : '?';
566  }
567  }
568  nextchar += strlen (nextchar);
569  if (longind != NULL)
570  *longind = option_index;
571  if (pfound->flag)
572  {
573  *(pfound->flag) = pfound->val;
574  return 0;
575  }
576  return pfound->val;
577  }
578 
579  /* Can't find it as a long option. If this is not getopt_long_only,
580  or the option starts with '--' or is not a valid short
581  option, then it's an error.
582  Otherwise interpret it as a short option. */
583  if (!long_only || argv[cob_optind][1] == '-'
584  || strchr (optstring, *nextchar) == NULL)
585  {
586  if (cob_opterr)
587  {
588  if (argv[cob_optind][1] == '-')
589  {
590  /* --option */
591  fprintf (stderr, _("%s: unrecognized option '--%s'"),
592  argv[0], nextchar);
593  fputc ('\n', stderr);
594  }
595  else
596  {
597  /* +option or -option */
598  fprintf (stderr, _("%s: unrecognized option '%c%s'"),
599  argv[0], argv[cob_optind][0], nextchar);
600  fputc ('\n', stderr);
601  }
602  }
603  nextchar = (char *) "";
604  cob_optind++;
605  cob_optopt = 0;
606  return '?';
607  }
608  }
609 
610  /* Look at and handle the next short option-character. */
611 
612  {
613  char c = *nextchar++;
614  char *temp = strchr (optstring, c);
615 
616  /* Increment 'cob_optind' when we start to process its last character. */
617  if (*nextchar == '\0')
618  {
619  ++cob_optind;
620  seen_short = 0;
621  }
622 
623  if (temp == NULL || c == ':')
624  {
625  if (cob_opterr)
626  {
627  fprintf (stderr, _("%s: invalid option -- %c"), argv[0], c);
628  fputc ('\n', stderr);
629  }
630  cob_optopt = c;
631  seen_short = 0;
632  return '?';
633  }
634  /* Convenience. Treat POSIX -W foo same as long option --foo */
635  if (temp[0] == 'W' && temp[1] == ';')
636  {
637  char *nameend;
638  const struct option *p;
639  const struct option *pfound = NULL;
640  int exact = 0;
641  int ambig = 0;
642  int indfound = 0;
643  int option_index;
644 
645  /* This is an option that requires an argument. */
646  if (*nextchar != '\0')
647  {
649  /* If we end this ARGV-element by taking the rest as an arg,
650  we must advance to the next element now. */
651  cob_optind++;
652  }
653  else if (cob_optind == argc)
654  {
655  if (cob_opterr)
656  {
657  /* 1003.2 specifies the format of this message. */
658  fprintf (stderr, _("%s: option requires an argument -- %c"),
659  argv[0], c);
660  fputc ('\n', stderr);
661  }
662  cob_optopt = c;
663  if (optstring[0] == ':')
664  c = ':';
665  else
666  c = '?';
667  seen_short = 0;
668  return c;
669  }
670  else
671  /* We already incremented 'cob_optind' once;
672  increment it again when taking next ARGV-elt as argument. */
673  cob_optarg = argv[cob_optind++];
674 
675  /* cob_optarg is now the argument, see if it's in the
676  table of longopts. */
677  if (!longopts) return '?'; /* silence warning */
678  for (nextchar = nameend = cob_optarg; *nameend && *nameend != '='; nameend++)
679  /* Do nothing. */ ;
680 
681  /* Test all long options for either exact match
682  or abbreviated matches. */
683  for (p = longopts, option_index = 0; p->name; p++, option_index++)
684  if (!strncmp (p->name, nextchar, (size_t)(nameend - nextchar)))
685  {
686  if ((unsigned int) (nameend - nextchar) == strlen (p->name))
687  {
688  /* Exact match found. */
689  pfound = p;
690  indfound = option_index;
691  exact = 1;
692  break;
693  }
694  else if (pfound == NULL)
695  {
696  /* First nonexact match found. */
697  pfound = p;
698  indfound = option_index;
699  }
700  else
701  /* Second or later nonexact match found. */
702  ambig = 1;
703  }
704  if (ambig && !exact)
705  {
706  if (cob_opterr)
707  {
708  fprintf (stderr, _("%s: option '-W %s' is ambiguous"),
709  argv[0], argv[cob_optind]);
710  fputc ('\n', stderr);
711  }
712  nextchar += strlen (nextchar);
713  cob_optind++;
714  seen_short = 0;
715  return '?';
716  }
717  if (pfound != NULL)
718  {
719  option_index = indfound;
720  if (*nameend)
721  {
722  /* Don't test has_arg with >, because some C compilers don't
723  allow it to be used on enums. */
724  if (pfound->has_arg)
725  cob_optarg = nameend + 1;
726  else
727  {
728  if (cob_opterr)
729  {
730  fprintf (stderr, _("%s: option '-W %s' doesn't allow an argument"),
731  argv[0], pfound->name);
732  fputc ('\n', stderr);
733  }
734 
735  nextchar += strlen (nextchar);
736  seen_short = 0;
737  return '?';
738  }
739  }
740  else if (pfound->has_arg == 1)
741  {
742  if (cob_optind < argc)
743  cob_optarg = argv[cob_optind++];
744  else
745  {
746  if (cob_opterr)
747  {
748  fprintf (stderr, _("%s: option '%s' requires an argument"),
749  argv[0], argv[cob_optind - 1]);
750  fputc ('\n', stderr);
751  }
752  nextchar += strlen (nextchar);
753  seen_short = 0;
754  return optstring[0] == ':' ? ':' : '?';
755  }
756  }
757  nextchar += strlen (nextchar);
758  if (longind != NULL)
759  *longind = option_index;
760  if (pfound->flag)
761  {
762  *(pfound->flag) = pfound->val;
763  return 0;
764  }
765  return pfound->val;
766  }
767  nextchar = NULL;
768  return 'W'; /* Let the application handle it. */
769  }
770  if (temp[1] == ':')
771  {
772  if (temp[2] == ':')
773  {
774  /* This is an option that accepts an argument optionally. */
775  if (*nextchar != '\0')
776  {
778  cob_optind++;
779  }
780  else
781  cob_optarg = NULL;
782  nextchar = NULL;
783  }
784  else
785  {
786  /* This is an option that requires an argument. */
787  if (*nextchar != '\0')
788  {
790  /* If we end this ARGV-element by taking the rest as an arg,
791  we must advance to the next element now. */
792  cob_optind++;
793  }
794  else if (cob_optind == argc)
795  {
796  if (cob_opterr)
797  {
798  /* 1003.2 specifies the format of this message. */
799  fprintf (stderr, _("%s: option requires an argument -- %c"),
800  argv[0], c);
801  fputc ('\n', stderr);
802  }
803  cob_optopt = c;
804  seen_short = 0;
805  if (optstring[0] == ':')
806  c = ':';
807  else
808  c = '?';
809  }
810  else
811  /* We already incremented 'cob_optind' once;
812  increment it again when taking next ARGV-elt as argument. */
813  cob_optarg = argv[cob_optind++];
814  nextchar = NULL;
815  }
816  }
817  seen_short = 1;
818  return c;
819  }
820 }
821 
void cob_free(void *mptr)
Definition: common.c:1284
int cob_getopt_long_long(const int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, const int long_only)
Definition: cobgetopt.c:321
int val
Definition: cobgetopt.h:87
static int last_nonopt
Definition: cobgetopt.c:160
static void exchange(char **argv)
Definition: cobgetopt.c:174
static int cob_getopt_initialized
Definition: cobgetopt.c:98
static int seen_short
Definition: cobgetopt.c:162
int cob_optind
Definition: cobgetopt.c:92
#define NONOPTION_P
Definition: cobgetopt.c:52
static const char * _getopt_initialize(const char *optstring)
Definition: cobgetopt.c:236
static char * nextchar
Definition: cobgetopt.c:107
EC ARGUMENT EC EC BOUND EC BOUND EC BOUND EC BOUND TABLE EC DATA EC DATA EC DATA PTR NULL
Definition: exception.def:95
#define _(msgid)
Definition: cobgetopt.c:44
const char * name
Definition: cobgetopt.h:82
static enum @0 ordering
static int first_nonopt
Definition: cobgetopt.c:159
int * flag
Definition: cobgetopt.h:86
int cob_opterr
Definition: cobgetopt.c:112
void * cob_fast_malloc(const size_t size)
Definition: common.c:1296
int has_arg
Definition: cobgetopt.h:85
int cob_optopt
Definition: cobgetopt.c:118
char * cob_optarg
Definition: cobgetopt.c:77