Ignore:
Timestamp:
Mar 30, 2018, 11:03:28 PM (7 years ago)
Author:
bird
Message:

kmk: Forked getopt and created a reentrant variant getopt_r.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/getopt_r.h

    r3212 r3213  
    1 /* Declarations for getopt.
     1/* Reentrant version of getopt.
     2
     3Based on ../getopt*.*:
     4
     5   Declarations for getopt.
    26Copyright (C) 1989-2016 Free Software Foundation, Inc.
    37
     
    1519
    1620You should have received a copy of the GNU General Public License along with
    17 this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21this program.  If not, see <http://www.gnu.org/licenses/>.
    1822
    19 #ifndef _GETOPT_H
    20 #define _GETOPT_H 1
     23Modifications:
     24  Copyright (c) 2018 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
     25*/
     26
     27/* Not quite safe to mix when converting code. */
     28#ifdef _GETOPT_H
     29# define _GETOPT_H "getopt.h was included already"
     30# error "getopt.h was included already"
     31#endif
     32
     33#ifndef INCLUDED_GETOPT_R_H
     34#define INCLUDED_GETOPT_R_H 1
    2135
    2236#ifdef  __cplusplus
    2337extern "C" {
    2438#endif
     39
     40typedef struct getopt_state_r
     41{
    2542
    2643/* For communication from `getopt' to the caller.
     
    3047   each non-option ARGV-element is returned here.  */
    3148
    32 extern char *optarg;
     49/*extern*/ char *optarg;
    3350
    3451/* Index in ARGV of the next element to be scanned.
     
    4461   how much of ARGV has been scanned so far.  */
    4562
    46 extern int optind;
     63/*extern*/ int optind;
    4764
    4865/* Callers store zero here to inhibit the error message `getopt' prints
    4966   for unrecognized options.  */
    5067
    51 extern int opterr;
     68/*extern*/ int opterr;
    5269
    5370/* Set to an option character which was unrecognized.  */
    5471
    55 extern int optopt;
     72/*extern*/ int optopt;
     73
     74
     75/* Internal state: */
     76
     77/* The next char to be scanned in the option-element
     78   in which the last option character we returned was found.
     79   This allows us to pick up the scan where we left off.
     80
     81   If this is zero, or a null string, it means resume the scan
     82   by advancing to the next ARGV-element.  */
     83
     84/*static*/ char *nextchar;
     85
     86/* REQUIRE_ORDER, PERMUTE or RETURN_IN_ORDER, see getopt_r.c. */
     87/*static*/ int ordering;
     88
     89/* Value of POSIXLY_CORRECT environment variable.  */
     90/*static*/ char *posixly_correct;
     91
     92/* Describe the part of ARGV that contains non-options that have
     93   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
     94   `last_nonopt' is the index after the last of them.  */
     95
     96/*static*/ int first_nonopt;
     97/*static*/ int last_nonopt;
     98
     99/* Mainly for asserting usage sanity. */
     100/*static*/ void *__getopt_initialized;
     101
     102/* New internal state (to resubmitting same parameters in each call): */
     103  /* new: the argument vector length. */
     104  int argc;
     105  /* new: the argument vector. */
     106  char * const *argv;
     107  /* new: the short option string (can be NULL/empty). */
     108  const char *optstring;
     109  /* new: the short option string length. */
     110  size_t len_optstring;
     111  /* new: the long options (can be NULL) */
     112  const struct option *long_options;
     113  /* Output context for err.h. */
     114  struct KMKBUILTINCTX *pCtx;
     115} getopt_state_r;
     116
     117
     118#ifndef no_argument
    56119
    57120/* Describe the long-named options requested by the application.
     
    96159#define optional_argument       2
    97160
    98 #if defined (__STDC__) && __STDC__
    99 #ifdef __GNU_LIBRARY__
    100 /* Many other libraries have conflicting prototypes for getopt, with
    101    differences in the consts, in stdlib.h.  To avoid compilation
    102    errors, only prototype getopt for the GNU C library.  */
    103 extern int getopt (int argc, char *const *argv, const char *shortopts);
    104 #else /* not __GNU_LIBRARY__ */
    105 extern int getopt ();
    106 #endif /* __GNU_LIBRARY__ */
    107 extern int getopt_long (int argc, char *const *argv, const char *shortopts,
    108                         const struct option *longopts, int *longind);
    109 extern int getopt_long_only (int argc, char *const *argv,
    110                              const char *shortopts,
    111                              const struct option *longopts, int *longind);
     161#endif /* Same as ../getopt.h.  Fix later? */
     162
     163extern void getopt_initialize_r (struct getopt_state_r *gos, int argc,
     164                                 char *const *argv, const char *shortopts,
     165                                 const struct option *longopts,
     166                                 char **envp, struct KMKBUILTINCTX *pCtx);
     167extern int getopt_r (struct getopt_state_r *gos);
     168extern int getopt_long_r (struct getopt_state_r *gos, int *longind);
     169extern int getopt_long_only_r (struct getopt_state_r *gos, int *longind);
    112170
    113171/* Internal only.  Users should not call this directly.  */
    114 extern int _getopt_internal (int argc, char *const *argv,
    115                              const char *shortopts,
    116                              const struct option *longopts, int *longind,
    117                              int long_only);
    118 #else /* not __STDC__ */
    119 extern int getopt ();
    120 extern int getopt_long ();
    121 extern int getopt_long_only ();
    122 
    123 extern int _getopt_internal ();
    124 #endif /* __STDC__ */
     172extern int _getopt_internal_r (struct getopt_state_r *gos,
     173                               const struct option *longopts,
     174                               int *longind, int long_only);
    125175
    126176#ifdef  __cplusplus
     
    128178#endif
    129179
    130 #endif /* getopt.h */
     180#endif /* getopt_r.h */
Note: See TracChangeset for help on using the changeset viewer.