| 1 |
(* GetOpt.def allows users to manage long options to getopt. |
| 2 |
|
| 3 |
Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 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 |
DEFINITION MODULE GetOpt ; |
| 28 |
|
| 29 |
FROM SYSTEM IMPORT ADDRESS ; |
| 30 |
FROM DynamicStrings IMPORT String ; |
| 31 |
|
| 32 |
CONST |
| 33 |
no_argument = 0 ; |
| 34 |
required_argument = 1 ; |
| 35 |
optional_argument = 2 ; |
| 36 |
|
| 37 |
TYPE |
| 38 |
LongOptions ; |
| 39 |
PtrToInteger = POINTER TO INTEGER ; |
| 40 |
|
| 41 |
(* |
| 42 |
GetOpt - call C getopt and fill in the parameters: |
| 43 |
optarg, optind, opterr and optopt. |
| 44 |
*) |
| 45 |
|
| 46 |
PROCEDURE GetOpt (argc: INTEGER; argv: ADDRESS; optstring: String; |
| 47 |
VAR optarg: String; |
| 48 |
VAR optind, opterr, optopt: INTEGER) : CHAR ; |
| 49 |
|
| 50 |
|
| 51 |
(* |
| 52 |
InitLongOptions - creates and returns a LongOptions empty array. |
| 53 |
*) |
| 54 |
|
| 55 |
PROCEDURE InitLongOptions () : LongOptions ; |
| 56 |
|
| 57 |
|
| 58 |
(* |
| 59 |
AddLongOption - appends long option {name, has_arg, flag, val} to the |
| 60 |
array of options and new long options array is |
| 61 |
returned. |
| 62 |
The old array, lo, should no longer be used. |
| 63 |
|
| 64 |
(from man 3 getopt) |
| 65 |
The meanings of the different fields are: |
| 66 |
|
| 67 |
name is the name of the long option. |
| 68 |
|
| 69 |
has_arg |
| 70 |
is: no_argument (or 0) if the option does not take an |
| 71 |
argument; required_argument (or 1) if the option |
| 72 |
requires an argument; or optional_argument (or 2) if |
| 73 |
the option takes an optional argument. |
| 74 |
|
| 75 |
flag specifies how results are returned for a long option. |
| 76 |
If flag is NULL, then getopt_long() returns val. |
| 77 |
(For example, the calling program may set val to the |
| 78 |
equivalent short option character). Otherwise, |
| 79 |
getopt_long() returns 0, and flag points to a |
| 80 |
variable which is set to val if the option is found, |
| 81 |
but left unchanged if the option is not found. |
| 82 |
|
| 83 |
val is the value to return, or to load into the variable |
| 84 |
pointed to by flag. |
| 85 |
|
| 86 |
The last element of the array must be filled with zeros. |
| 87 |
*) |
| 88 |
|
| 89 |
PROCEDURE AddLongOption (lo: LongOptions; index: CARDINAL; |
| 90 |
name: String; has_arg: INTEGER; |
| 91 |
VAR flag: INTEGER; val: INTEGER) : LongOptions ; |
| 92 |
|
| 93 |
|
| 94 |
(* |
| 95 |
KillLongOptions - returns NIL and also frees up memory |
| 96 |
associated with, lo. |
| 97 |
*) |
| 98 |
|
| 99 |
PROCEDURE KillLongOptions (lo: LongOptions) : LongOptions ; |
| 100 |
|
| 101 |
|
| 102 |
(* |
| 103 |
GetOptLong - works like GetOpt but will accept long options (using |
| 104 |
two dashes). If the program only accepts long options |
| 105 |
then optstring should be an empty string, not NIL. |
| 106 |
*) |
| 107 |
|
| 108 |
PROCEDURE GetOptLong (argc: INTEGER; argv: ADDRESS; |
| 109 |
optstring: String; longopts: LongOptions; |
| 110 |
VAR longindex: INTEGER) : INTEGER ; |
| 111 |
|
| 112 |
|
| 113 |
(* |
| 114 |
GetOptLongOnly - works like GetOptLong except that a single dash |
| 115 |
can be used for a long option. |
| 116 |
*) |
| 117 |
|
| 118 |
PROCEDURE GetOptLongOnly (argc: INTEGER; argv: ADDRESS; |
| 119 |
optstring: String; longopts: LongOptions; |
| 120 |
VAR longindex: INTEGER) : INTEGER ; |
| 121 |
|
| 122 |
|
| 123 |
END GetOpt. |