1 /* getopt.c provide access to the C getopt library.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
30 // #include "ansi-decl.h"
45 cgetopt_getopt (int argc
, char *argv
[], char *optstring
)
47 char r
= getopt (argc
, argv
, optstring
);
49 cgetopt_optarg
= optarg
;
50 cgetopt_optind
= optind
;
51 cgetopt_opterr
= opterr
;
52 cgetopt_optopt
= optopt
;
61 cgetopt_cgetopt_long (int argc
, char *argv
[], char *optstring
, const struct option
*longopts
,
64 int r
= getopt_long (argc
, argv
, optstring
, longopts
, longindex
);
66 cgetopt_optarg
= optarg
;
67 cgetopt_optind
= optind
;
68 cgetopt_opterr
= opterr
;
69 cgetopt_optopt
= optopt
;
76 cgetopt_cgetopt_long_only (int argc
, char *argv
[], char *optstring
,
77 const struct option
*longopts
, int *longindex
)
79 int r
= getopt_long_only (argc
, argv
, optstring
, longopts
, longindex
);
81 cgetopt_optarg
= optarg
;
82 cgetopt_optind
= optind
;
83 cgetopt_opterr
= opterr
;
84 cgetopt_optopt
= optopt
;
90 typedef struct cgetopt_Options_s
{
96 /* InitOptions a constructor for Options. */
99 cgetopt_InitOptions (void)
101 cgetopt_Options
*o
= (cgetopt_Options
*) malloc (sizeof (cgetopt_Options
));
102 o
->cinfo
= (struct option
*) malloc (sizeof (struct option
));
108 /* KillOptions a deconstructor for Options. Returns NULL after freeing
109 up all allocated memory associated with o. */
112 cgetopt_KillOptions (cgetopt_Options
*o
)
120 /* SetOption set option[index] with {name, has_arg, flag, val}. */
123 cgetopt_SetOption (cgetopt_Options
*o
, unsigned int index
,
124 char *name
, int has_arg
,
129 o
->cinfo
= (struct option
*) malloc (sizeof (struct option
) * (index
+ 1));
132 o
->cinfo
[index
].name
= name
;
133 o
->cinfo
[index
].has_arg
= has_arg
;
136 o
->cinfo
[index
].flag
= flag
;
137 o
->cinfo
[index
].val
= val
;
141 /* GetLongOptionArray returns a pointer to the C array containing all
145 cgetopt_GetLongOptionArray (cgetopt_Options
*o
)
151 /* GNU Modula-2 linking fodder. */
154 _M2_cgetopt_init (void)
160 _M2_cgetopt_finish (void)