]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/gm2-libs/GetOpt.mod
08b8214f665ed19859ec9ebe8ec58c5e13d3702a
[thirdparty/gcc.git] / gcc / m2 / gm2-libs / GetOpt.mod
1 (* GetOpt.mod allows users to manage long options to getopt.
2
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6 This file is part of GNU Modula-2.
7
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)
11 any later version.
12
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.
17
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.
21
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/>. *)
26
27 IMPLEMENTATION MODULE GetOpt ; (*!m2pim+gm2*)
28
29 FROM DynamicStrings IMPORT string, InitStringCharStar ;
30 FROM Storage IMPORT ALLOCATE, REALLOCATE, DEALLOCATE ;
31 FROM MemUtils IMPORT MemCopy ;
32
33 IMPORT cgetopt ;
34
35
36 TYPE
37 LongOptions = POINTER TO RECORD
38 cptr: cgetopt.Options
39 END ;
40
41
42 (*
43 GetOpt - call C getopt and fill in the parameters:
44 optarg, optind, opterr and optop.
45 *)
46
47 PROCEDURE GetOpt (argc: INTEGER; argv: ADDRESS; optstring: String;
48 VAR optarg: String;
49 VAR optind, opterr, optopt: INTEGER) : CHAR ;
50 VAR
51 r: CHAR ;
52 BEGIN
53 r := cgetopt.getopt (argc, argv, string (optstring)) ;
54 optarg := InitStringCharStar (cgetopt.optarg) ;
55 opterr := cgetopt.opterr ;
56 optopt := cgetopt.optopt ;
57 RETURN r
58 END GetOpt ;
59
60
61 (*
62 InitLongOptions - creates and returns a LongOptions empty array.
63 *)
64
65 PROCEDURE InitLongOptions () : LongOptions ;
66 VAR
67 lo: LongOptions ;
68 BEGIN
69 NEW (lo) ;
70 WITH lo^ DO
71 cptr := cgetopt.InitOptions ()
72 END ;
73 RETURN lo
74 END InitLongOptions ;
75
76
77 (*
78 AddLongOption - appends long option {name, has_arg, flag, val} to the
79 array of options and new long options array is returned.
80 The old array, lo, should no longer be used.
81
82 (from man 3 getopt)
83 The meanings of the different fields are:
84
85 name is the name of the long option.
86
87 has_arg
88 is: no_argument (or 0) if the option does not take an argument; required_argument
89 (or 1) if the option requires an argument; or optional_argument (or 2) if the
90 option takes an optional argument.
91
92 flag specifies how results are returned for a long option. If flag is NULL, then
93 getopt_long() returns val. (For example, the calling program may set val to the
94 equivalent short option character.) Otherwise, getopt_long() returns 0, and flag
95 points to a variable which is set to val if the option is found, but left unchanged
96 if the option is not found.
97
98 val is the value to return, or to load into the variable pointed to by flag.
99
100 The last element of the array must be filled with zeros.
101 *)
102
103 PROCEDURE AddLongOption (lo: LongOptions; index: CARDINAL;
104 name: String; has_arg: INTEGER;
105 VAR flag: INTEGER; val: INTEGER) : LongOptions ;
106 BEGIN
107 cgetopt.SetOption (lo^.cptr, index, name, has_arg, flag, val) ;
108 RETURN lo
109 END AddLongOption ;
110
111
112 (*
113 KillLongOptions - returns NIL and also frees up memory associated with, lo.
114 *)
115
116 PROCEDURE KillLongOptions (lo: LongOptions) : LongOptions ;
117 BEGIN
118 lo^.cptr := cgetopt.KillOptions (lo^.cptr) ;
119 DISPOSE (lo) ;
120 RETURN NIL
121 END KillLongOptions ;
122
123
124 (*
125 GetOptLong - works like GetOpt but will accept long options (using two dashes).
126 If the program only accepts long options then optstring should be
127 an empty string, not NIL.
128 *)
129
130 PROCEDURE GetOptLong (argc: INTEGER; argv: ADDRESS; optstring: String;
131 longopts: LongOptions; VAR longindex: INTEGER) : INTEGER ;
132 BEGIN
133 RETURN cgetopt.getopt_long (argc, argv, string (optstring),
134 cgetopt.GetLongOptionArray (longopts^.cptr), longindex)
135 END GetOptLong ;
136
137
138 (*
139 GetOptLongOnly - works like GetOptLong except that a single dash can be used
140 for a long option.
141 *)
142
143 PROCEDURE GetOptLongOnly (argc: INTEGER; argv: ADDRESS; optstring: String;
144 longopts: LongOptions; VAR longindex: INTEGER) : INTEGER ;
145 BEGIN
146 RETURN cgetopt.getopt_long_only (argc, argv, string (optstring),
147 cgetopt.GetLongOptionArray (longopts^.cptr), longindex)
148 END GetOptLongOnly ;
149
150
151 END GetOpt.