home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libiberty / getopt.c < prev    next >
C/C++ Source or Header  |  1994-04-22  |  22KB  |  748 lines

  1. /* Getopt for GNU.
  2.    NOTE: getopt is now part of the C library, so if you don't know what
  3.    "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
  4.    before changing it!
  5.  
  6.    Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94
  7.        Free Software Foundation, Inc.
  8.  
  9.    This program is free software; you can redistribute it and/or
  10.    modify it under the terms of the GNU Library General Public License
  11.    as published by the Free Software Foundation; either version 2, or
  12.    (at your option) any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU Library General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU Library General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
  24.    Ditto for AIX 3.2 and <stdlib.h>.  */
  25. #ifndef _NO_PROTO
  26. #define _NO_PROTO
  27. #endif
  28.  
  29. #ifdef HAVE_CONFIG_H
  30. #if defined (emacs) || defined (CONFIG_BROKETS)
  31. /* We use <config.h> instead of "config.h" so that a compilation
  32.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  33.    (which it would do because it found this file in $srcdir).  */
  34. #include <config.h>
  35. #else
  36. #include "config.h"
  37. #endif
  38. #endif
  39.  
  40. #ifndef __STDC__
  41. /* This is a separate conditional since some stdc systems
  42.    reject `defined (const)'.  */
  43. #ifndef const
  44. #define const
  45. #endif
  46. #endif
  47.  
  48. #include <stdio.h>
  49.  
  50. /* Comment out all this code if we are using the GNU C Library, and are not
  51.    actually compiling the library itself.  This code is part of the GNU C
  52.    Library, but also included in many other GNU distributions.  Compiling
  53.    and linking in this code is a waste when using the GNU C library
  54.    (especially if it is a shared library).  Rather than having every GNU
  55.    program understand `configure --with-gnu-libc' and omit the object files,
  56.    it is simpler to just do this in the source for each such file.  */
  57.  
  58. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  59.  
  60.  
  61. /* This needs to come after some library #include
  62.    to get __GNU_LIBRARY__ defined.  */
  63. #ifdef    __GNU_LIBRARY__
  64. /* Don't include stdlib.h for non-GNU C libraries because some of them
  65.    contain conflicting prototypes for getopt.  */
  66. #include <stdlib.h>
  67. #endif    /* GNU C library.  */
  68.  
  69. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  70.    but it behaves differently for the user, since it allows the user
  71.    to intersperse the options with the other arguments.
  72.  
  73.    As `getopt' works, it permutes the elements of ARGV so that,
  74.    when it is done, all the options precede everything else.  Thus
  75.    all application programs are extended to handle flexible argument order.
  76.  
  77.    Setting the environment variable POSIXLY_CORRECT disables permutation.
  78.    Then the behavior is completely standard.
  79.  
  80.    GNU application programs can use a third alternative mode in which
  81.    they can distinguish the relative order of options and other arguments.  */
  82.  
  83. #include "getopt.h"
  84.  
  85. /* For communication from `getopt' to the caller.
  86.    When `getopt' finds an option that takes an argument,
  87.    the argument value is returned here.
  88.    Also, when `ordering' is RETURN_IN_ORDER,
  89.    each non-option ARGV-element is returned here.  */
  90.  
  91. char *optarg = NULL;
  92.  
  93. /* Index in ARGV of the next element to be scanned.
  94.    This is used for communication to and from the caller
  95.    and for communication between successive calls to `getopt'.
  96.  
  97.    On entry to `getopt', zero means this is the first call; initialize.
  98.  
  99.    When `getopt' returns EOF, this is the index of the first of the
  100.    non-option elements that the caller should itself scan.
  101.  
  102.    Otherwise, `optind' communicates from one call to the next
  103.    how much of ARGV has been scanned so far.  */
  104.  
  105. /* XXX 1003.2 says this must be 1 before any call.  */
  106. int optind = 0;
  107.  
  108. /* The next char to be scanned in the option-element
  109.    in which the last option character we returned was found.
  110.    This allows us to pick up the scan where we left off.
  111.  
  112.    If this is zero, or a null string, it means resume the scan
  113.    by advancing to the next ARGV-element.  */
  114.  
  115. static char *nextchar;
  116.  
  117. /* Callers store zero here to inhibit the error message
  118.    for unrecognized options.  */
  119.  
  120. int opterr = 1;
  121.  
  122. /* Set to an option character which was unrecognized.
  123.    This must be initialized on some systems to avoid linking in the
  124.    system's own getopt implementation.  */
  125.  
  126. int optopt = '?';
  127.  
  128. /* Describe how to deal with options that follow non-option ARGV-elements.
  129.  
  130.    If the caller did not specify anything,
  131.    the default is REQUIRE_ORDER if the environment variable
  132.    POSIXLY_CORRECT is defined, PERMUTE otherwise.
  133.  
  134.    REQUIRE_ORDER means don't recognize them as options;
  135.    stop option processing when the first non-option is seen.
  136.    This is what Unix does.
  137.    This mode of operation is selected by either setting the environment
  138.    variable POSIXLY_CORRECT, or using `+' as the first character
  139.    of the list of option characters.
  140.  
  141.    PERMUTE is the default.  We permute the contents of ARGV as we scan,
  142.    so that eventually all the non-options are at the end.  This allows options
  143.    to be given in any order, even with programs that were not written to
  144.    expect this.
  145.  
  146.    RETURN_IN_ORDER is an option available to programs that were written
  147.    to expect options and other ARGV-elements in any order and that care about
  148.    the ordering of the two.  We describe each non-option ARGV-element
  149.    as if it were the argument of an option with character code 1.
  150.    Using `-' as the first character of the list of option characters
  151.    selects this mode of operation.
  152.  
  153.    The special argument `--' forces an end of option-scanning regardless
  154.    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
  155.    `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
  156.  
  157. static enum
  158. {
  159.   REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  160. } ordering;
  161.  
  162. #ifdef    __GNU_LIBRARY__
  163. /* We want to avoid inclusion of string.h with non-GNU libraries
  164.    because there are many ways it can cause trouble.
  165.    On some systems, it contains special magic macros that don't work
  166.    in GCC.  */
  167. #include <string.h>
  168. #define    my_index    strchr
  169. #else
  170.  
  171. /* Avoid depending on library functions or files
  172.    whose names are inconsistent.  */
  173.  
  174. char *getenv ();
  175.  
  176. static char *
  177. my_index (str, chr)
  178.      const char *str;
  179.      int chr;
  180. {
  181.   while (*str)
  182.     {
  183.       if (*str == chr)
  184.     return (char *) str;
  185.       str++;
  186.     }
  187.   return 0;
  188. }
  189.  
  190. /* If using GCC, we can safely declare strlen this way.
  191.    If not using GCC, it is ok not to declare it.  */
  192. #ifdef __GNUC__
  193. /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
  194.    That was relevant to code that was here before.  */
  195. #ifndef __STDC__
  196. /* gcc with -traditional declares the built-in strlen to return int,
  197.    and has done so at least since version 2.4.5. -- rms.  */
  198. extern int strlen (const char *);
  199. #endif /* not __STDC__ */
  200. #endif /* __GNUC__ */
  201.  
  202. #endif /* not __GNU_LIBRARY__ */
  203.  
  204. /* Handle permutation of arguments.  */
  205.  
  206. /* Describe the part of ARGV that contains non-options that have
  207.    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
  208.    `last_nonopt' is the index after the last of them.  */
  209.  
  210. static int first_nonopt;
  211. static int last_nonopt;
  212.  
  213. /* Exchange two adjacent subsequences of ARGV.
  214.    One subsequence is elements [first_nonopt,last_nonopt)
  215.    which contains all the non-options that have been skipped so far.
  216.    The other is elements [last_nonopt,optind), which contains all
  217.    the options processed since those non-options were skipped.
  218.  
  219.    `first_nonopt' and `last_nonopt' are relocated so that they describe
  220.    the new indices of the non-options in ARGV after they are moved.  */
  221.  
  222. static void
  223. exchange (argv)
  224.      char **argv;
  225. {
  226.   int bottom = first_nonopt;
  227.   int middle = last_nonopt;
  228.   int top = optind;
  229.   char *tem;
  230.  
  231.   /* Exchange the shorter segment with the far end of the longer segment.
  232.      That puts the shorter segment into the right place.
  233.      It leaves the longer segment in