| 1 |
(* SArgs.mod provides a String interface to the command line arguments. |
| 2 |
|
| 3 |
Copyright (C) 2001-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 |
IMPLEMENTATION MODULE SArgs ; |
| 28 |
|
| 29 |
FROM SYSTEM IMPORT TSIZE, ADDRESS ; |
| 30 |
FROM UnixArgs IMPORT GetArgC, GetArgV ; |
| 31 |
|
| 32 |
FROM DynamicStrings IMPORT InitStringCharStar, |
| 33 |
InitStringDB, InitStringCharStarDB, |
| 34 |
InitStringCharDB, MultDB, DupDB, SliceDB ; |
| 35 |
|
| 36 |
TYPE |
| 37 |
PtrToChar = POINTER TO CHAR ; |
| 38 |
PtrToPtrToChar = POINTER TO PtrToChar ; |
| 39 |
|
| 40 |
(* |
| 41 |
#undef GM2_DEBUG_SARGS |
| 42 |
if defined(GM2_DEBUG_SARGS) |
| 43 |
# define InitString(X) InitStringDB(X, __FILE__, __LINE__) |
| 44 |
# define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__) |
| 45 |
# define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__) |
| 46 |
# define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__) |
| 47 |
# define Dup(X) DupDB(X, __FILE__, __LINE__) |
| 48 |
# define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__) |
| 49 |
#endif |
| 50 |
*) |
| 51 |
|
| 52 |
|
| 53 |
(* |
| 54 |
GetArg - returns the nth argument from the command line. |
| 55 |
The success of the operation is returned. |
| 56 |
If TRUE is returned then the string, s, contains a |
| 57 |
new string, otherwise s is set to NIL. |
| 58 |
*) |
| 59 |
|
| 60 |
PROCEDURE GetArg (VAR s: String; n: CARDINAL) : BOOLEAN ; |
| 61 |
VAR |
| 62 |
i : INTEGER ; |
| 63 |
a : ADDRESS ; |
| 64 |
ppc: PtrToPtrToChar ; |
| 65 |
BEGIN |
| 66 |
i := VAL (INTEGER, n) ; |
| 67 |
IF i < GetArgC () |
| 68 |
THEN |
| 69 |
a := ADDRESS (GetArgV ()) ; |
| 70 |
INC (a, n * TSIZE (PtrToChar)) ; |
| 71 |
ppc := a ; |
| 72 |
s := InitStringCharStar (ppc^) ; |
| 73 |
RETURN TRUE |
| 74 |
ELSE |
| 75 |
s := NIL ; |
| 76 |
RETURN FALSE |
| 77 |
END ; |
| 78 |
END GetArg ; |
| 79 |
|
| 80 |
|
| 81 |
(* |
| 82 |
Narg - returns the number of arguments available from |
| 83 |
command line. |
| 84 |
*) |
| 85 |
|
| 86 |
PROCEDURE Narg () : CARDINAL ; |
| 87 |
BEGIN |
| 88 |
RETURN GetArgC () |
| 89 |
END Narg ; |
| 90 |
|
| 91 |
|
| 92 |
END SArgs. |