| 1 |
(* getopt.def provides access to the getopt C library. |
| 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 cgetopt ; |
| 28 |
|
| 29 |
FROM SYSTEM IMPORT ADDRESS ; |
| 30 |
|
| 31 |
TYPE |
| 32 |
Options = ADDRESS ; |
| 33 |
|
| 34 |
VAR |
| 35 |
optarg : ADDRESS ; |
| 36 |
optind, opterr, optopt: INTEGER ; |
| 37 |
|
| 38 |
|
| 39 |
(* |
| 40 |
getopt - the getopt() function parses the command-line arguments. |
| 41 |
Its arguments argc and argv are the argument count and array as |
| 42 |
passed to the main() function on program invocation. An element of |
| 43 |
argv that starts with '-' (and is not exactly "-" or "--") is an |
| 44 |
option element. The characters of this element (aside from the |
| 45 |
initial '-') are option characters. If getopt() is called |
| 46 |
repeatedly, it returns successively each of the option characters |
| 47 |
from each of the option elements. |
| 48 |
*) |
| 49 |
|
| 50 |
PROCEDURE getopt (argc: INTEGER; argv: ADDRESS; optstring: ADDRESS) : CHAR ; |
| 51 |
|
| 52 |
|
| 53 |
(* |
| 54 |
getopt_long - works like getopt() except that it also accepts long options, |
| 55 |
started with two dashes. (If the program accepts only long |
| 56 |
options, then optstring should be specified as an empty string (""), |
| 57 |
not NULL.) Long option names may be abbreviated if the abbreviation |
| 58 |
is unique or is an exact match for some defined option. A |
| 59 |
long option may take a parameter, of the form --arg=param or |
| 60 |
--arg param. |
| 61 |
*) |
| 62 |
|
| 63 |
PROCEDURE getopt_long (argc: INTEGER; argv: ADDRESS; optstring: ADDRESS; |
| 64 |
longopts: ADDRESS; VAR longindex: INTEGER) : INTEGER ; |
| 65 |
|
| 66 |
|
| 67 |
(* |
| 68 |
getopt_long_only - a wrapper for the C getopt_long_only. |
| 69 |
*) |
| 70 |
|
| 71 |
PROCEDURE getopt_long_only (argc: INTEGER; argv: ADDRESS; optstring: ADDRESS; |
| 72 |
longopts: ADDRESS; VAR longindex: INTEGER) : INTEGER ; |
| 73 |
|
| 74 |
|
| 75 |
(* |
| 76 |
InitOptions - constructor for empty Options. |
| 77 |
*) |
| 78 |
|
| 79 |
PROCEDURE InitOptions () : Options ; |
| 80 |
|
| 81 |
|
| 82 |
(* |
| 83 |
KillOptions - deconstructor for empty Options. |
| 84 |
*) |
| 85 |
|
| 86 |
PROCEDURE KillOptions (o: Options) : Options ; |
| 87 |
|
| 88 |
|
| 89 |
(* |
| 90 |
SetOption - set option[index] with {name, has_arg, flag, val}. |
| 91 |
*) |
| 92 |
|
| 93 |
PROCEDURE SetOption (o: Options; index: CARDINAL; |
| 94 |
name: ADDRESS; has_arg: INTEGER; |
| 95 |
VAR flag: INTEGER; val: INTEGER) ; |
| 96 |
|
| 97 |
|
| 98 |
(* |
| 99 |
GetLongOptionArray - return a pointer to the C array containing all |
| 100 |
long options. |
| 101 |
*) |
| 102 |
|
| 103 |
PROCEDURE GetLongOptionArray (o: Options) : ADDRESS ; |
| 104 |
|
| 105 |
|
| 106 |
END cgetopt. |