diff options
author | Gaius Mulley <gaiusmod2@gmail.com> | 2023-10-27 15:54:48 +0100 |
---|---|---|
committer | Gaius Mulley <gaiusmod2@gmail.com> | 2023-10-27 15:54:48 +0100 |
commit | e5f6a5ad7ceece5238fc3d63f7ea92574df4264d (patch) | |
tree | 5c841dfc66b6195603dadff8ee74588f58f8269e /gcc | |
parent | 7bcdb777e6a0d1a0159f25616c5d8e35e7cb5fb6 (diff) | |
download | gcc-e5f6a5ad7ceece5238fc3d63f7ea92574df4264d.zip gcc-e5f6a5ad7ceece5238fc3d63f7ea92574df4264d.tar.gz gcc-e5f6a5ad7ceece5238fc3d63f7ea92574df4264d.tar.bz2 |
PR modula2/111530: Build failure on BSD due to getopt_long_only GNU extension dependency
This patch uses the libiberty getopt long functions (wrapped up inside
libgm2/libm2pim/cgetopt.cc) and only enables this implementation if
libgm2/configure.ac detects no getopt_long and friends on the target.
gcc/m2/ChangeLog:
PR modula2/111530
* gm2-libs-ch/cgetopt.c (cgetopt_cgetopt_long): Re-format.
(cgetopt_cgetopt_long_only): Re-format.
(cgetopt_SetOption): Re-format and assign flag to NULL
if name is also NULL.
* gm2-libs/GetOpt.def (AddLongOption): Add index parameter
and change flag to be a VAR parameter rather than a pointer.
(GetOptLong): Re-format.
(GetOpt): Correct comment.
* gm2-libs/GetOpt.mod: Re-write to rely on cgetopt rather
than implement long option creation in GetOpt.
* gm2-libs/cgetopt.def (SetOption): has_arg type is INTEGER.
libgm2/ChangeLog:
PR modula2/111530
* Makefile.in: Regenerate.
* aclocal.m4: Regenerate.
* config.h.in: Regenerate.
* configure: Regenerate.
* configure.ac (AC_CHECK_HEADERS): Include getopt.h.
(GM2_CHECK_LIB): getopt_long check.
(GM2_CHECK_LIB): getopt_long_only check.
* libm2cor/Makefile.in: Regenerate.
* libm2iso/Makefile.in: Regenerate.
* libm2log/Makefile.in: Regenerate.
* libm2min/Makefile.in: Regenerate.
* libm2pim/Makefile.in: Regenerate.
* libm2pim/cgetopt.cc: Re-write using conditional on configure
and long function code from libiberty/getopt.c.
gcc/testsuite/ChangeLog:
PR modula2/111530
* gm2/pimlib/run/pass/testgetopt.mod: New test.
Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/m2/gm2-libs-ch/cgetopt.c | 10 | ||||
-rw-r--r-- | gcc/m2/gm2-libs/GetOpt.def | 13 | ||||
-rw-r--r-- | gcc/m2/gm2-libs/GetOpt.mod | 84 | ||||
-rw-r--r-- | gcc/m2/gm2-libs/cgetopt.def | 3 | ||||
-rw-r--r-- | gcc/testsuite/gm2/pimlib/run/pass/testgetopt.mod | 74 |
5 files changed, 98 insertions, 86 deletions
diff --git a/gcc/m2/gm2-libs-ch/cgetopt.c b/gcc/m2/gm2-libs-ch/cgetopt.c index 017fe91..36aadbd 100644 --- a/gcc/m2/gm2-libs-ch/cgetopt.c +++ b/gcc/m2/gm2-libs-ch/cgetopt.c @@ -59,7 +59,7 @@ cgetopt_getopt (int argc, char *argv[], char *optstring) int cgetopt_cgetopt_long (int argc, char *argv[], char *optstring, const struct option *longopts, - int *longindex) + int *longindex) { int r = getopt_long (argc, argv, optstring, longopts, longindex); @@ -74,7 +74,7 @@ cgetopt_cgetopt_long (int argc, char *argv[], char *optstring, const struct opti int cgetopt_cgetopt_long_only (int argc, char *argv[], char *optstring, - const struct option *longopts, int *longindex) + const struct option *longopts, int *longindex) { int r = getopt_long_only (argc, argv, optstring, longopts, longindex); @@ -121,8 +121,8 @@ cgetopt_KillOptions (cgetopt_Options *o) void cgetopt_SetOption (cgetopt_Options *o, unsigned int index, - char *name, bool has_arg, - int *flag, int val) + char *name, int has_arg, + int *flag, int val) { if (index > o->high) { @@ -131,6 +131,8 @@ cgetopt_SetOption (cgetopt_Options *o, unsigned int index, } o->cinfo[index].name = name; o->cinfo[index].has_arg = has_arg; + if (name == NULL) + flag = NULL; o->cinfo[index].flag = flag; o->cinfo[index].val = val; } diff --git a/gcc/m2/gm2-libs/GetOpt.def b/gcc/m2/gm2-libs/GetOpt.def index 2da5512..bd021e4 100644 --- a/gcc/m2/gm2-libs/GetOpt.def +++ b/gcc/m2/gm2-libs/GetOpt.def @@ -40,7 +40,7 @@ TYPE (* GetOpt - call C getopt and fill in the parameters: - optarg, optind, opterr and optop. + optarg, optind, opterr and optopt. *) PROCEDURE GetOpt (argc: INTEGER; argv: ADDRESS; optstring: String; @@ -83,13 +83,12 @@ PROCEDURE InitLongOptions () : LongOptions ; val is the value to return, or to load into the variable pointed to by flag. - The last element of the array has to be filled with zeros. + The last element of the array must be filled with zeros. *) -PROCEDURE AddLongOption (lo: LongOptions; +PROCEDURE AddLongOption (lo: LongOptions; index: CARDINAL; name: String; has_arg: INTEGER; - flag: PtrToInteger; - val: INTEGER) : LongOptions ; + VAR flag: INTEGER; val: INTEGER) : LongOptions ; (* @@ -106,8 +105,8 @@ PROCEDURE KillLongOptions (lo: LongOptions) : LongOptions ; then optstring should be an empty string, not NIL. *) -PROCEDURE GetOptLong (argc: INTEGER; argv: ADDRESS; optstring: String; - longopts: LongOptions; +PROCEDURE GetOptLong (argc: INTEGER; argv: ADDRESS; + optstring: String; longopts: LongOptions; VAR longindex: INTEGER) : INTEGER ; diff --git a/gcc/m2/gm2-libs/GetOpt.mod b/gcc/m2/gm2-libs/GetOpt.mod index e7eaae6..e3dcb94 100644 --- a/gcc/m2/gm2-libs/GetOpt.mod +++ b/gcc/m2/gm2-libs/GetOpt.mod @@ -34,19 +34,8 @@ IMPORT cgetopt ; TYPE - Crecord = RECORD (* see man 3 getopt. *) - name : ADDRESS ; - has_arg: INTEGER ; - flag : PtrToInteger ; - val : INTEGER ; - END ; - - ptrToCrecord = POINTER TO Crecord ; - LongOptions = POINTER TO RECORD - cptr: ptrToCrecord ; - len : CARDINAL ; - size: CARDINAL ; + cptr: cgetopt.Options END ; @@ -79,9 +68,7 @@ VAR BEGIN NEW (lo) ; WITH lo^ DO - cptr := NIL ; - len := 0 ; - size := 0 + cptr := cgetopt.InitOptions () END ; RETURN lo END InitLongOptions ; @@ -110,69 +97,25 @@ END InitLongOptions ; val is the value to return, or to load into the variable pointed to by flag. - The last element of the array has to be filled with zeros. + The last element of the array must be filled with zeros. *) -PROCEDURE AddLongOption (lo: LongOptions; +PROCEDURE AddLongOption (lo: LongOptions; index: CARDINAL; name: String; has_arg: INTEGER; - flag: PtrToInteger; val: INTEGER) : LongOptions ; -VAR - old, - entry: ptrToCrecord ; + VAR flag: INTEGER; val: INTEGER) : LongOptions ; BEGIN - IF lo^.cptr = NIL - THEN - NEW (lo^.cptr) ; - lo^.len := 1 ; - lo^.size := SIZE (Crecord) ; - entry := lo^.cptr - ELSE - old := lo^.cptr ; - INC (lo^.len) ; - lo^.size := lo^.len * SIZE (Crecord) ; - REALLOCATE (lo^.cptr, lo^.size) ; - IF lo^.cptr = NIL - THEN - entry := NIL - ELSIF old = lo^.cptr - THEN - entry := lo^.cptr ; - INC (entry, SIZE (Crecord) * lo^.len-1) - ELSE - MemCopy (old, lo^.len-1, lo^.cptr) ; - entry := lo^.cptr ; - INC (entry, SIZE (Crecord) * lo^.len-1) - END - END ; - fillIn (entry, name, has_arg, flag, val) ; + cgetopt.SetOption (lo^.cptr, index, name, has_arg, flag, val) ; RETURN lo END AddLongOption ; (* - fillIn - fills in -*) - -PROCEDURE fillIn (entry: ptrToCrecord; - name: String; has_arg: INTEGER; flag: PtrToInteger; val: INTEGER) ; -BEGIN - IF entry # NIL - THEN - entry^.name := name ; - entry^.has_arg := has_arg ; - entry^.flag := flag ; - entry^.val := val - END -END fillIn ; - - -(* KillLongOptions - returns NIL and also frees up memory associated with, lo. *) PROCEDURE KillLongOptions (lo: LongOptions) : LongOptions ; BEGIN - DEALLOCATE (lo^.cptr, lo^.size) ; + lo^.cptr := cgetopt.KillOptions (lo^.cptr) ; DISPOSE (lo) ; RETURN NIL END KillLongOptions ; @@ -186,11 +129,9 @@ END KillLongOptions ; PROCEDURE GetOptLong (argc: INTEGER; argv: ADDRESS; optstring: String; longopts: LongOptions; VAR longindex: INTEGER) : INTEGER ; -VAR - r: INTEGER ; BEGIN - r := cgetopt.getopt_long (argc, argv, string (optstring), longopts^.cptr, longindex) ; - RETURN r + RETURN cgetopt.getopt_long (argc, argv, string (optstring), + cgetopt.GetLongOptionArray (longopts^.cptr), longindex) END GetOptLong ; @@ -201,12 +142,9 @@ END GetOptLong ; PROCEDURE GetOptLongOnly (argc: INTEGER; argv: ADDRESS; optstring: String; longopts: LongOptions; VAR longindex: INTEGER) : INTEGER ; -VAR - r: INTEGER ; BEGIN - r := cgetopt.getopt_long_only (argc, argv, string (optstring), - longopts^.cptr, longindex) ; - RETURN r + RETURN cgetopt.getopt_long_only (argc, argv, string (optstring), + cgetopt.GetLongOptionArray (longopts^.cptr), longindex) END GetOptLongOnly ; diff --git a/gcc/m2/gm2-libs/cgetopt.def b/gcc/m2/gm2-libs/cgetopt.def index 79da9ad..e2162d5 100644 --- a/gcc/m2/gm2-libs/cgetopt.def +++ b/gcc/m2/gm2-libs/cgetopt.def @@ -28,7 +28,6 @@ DEFINITION MODULE cgetopt ; FROM SYSTEM IMPORT ADDRESS ; - TYPE Options = ADDRESS ; @@ -92,7 +91,7 @@ PROCEDURE KillOptions (o: Options) : Options ; *) PROCEDURE SetOption (o: Options; index: CARDINAL; - name: ADDRESS; has_arg: BOOLEAN; + name: ADDRESS; has_arg: INTEGER; VAR flag: INTEGER; val: INTEGER) ; diff --git a/gcc/testsuite/gm2/pimlib/run/pass/testgetopt.mod b/gcc/testsuite/gm2/pimlib/run/pass/testgetopt.mod new file mode 100644 index 0000000..3e322da --- /dev/null +++ b/gcc/testsuite/gm2/pimlib/run/pass/testgetopt.mod @@ -0,0 +1,74 @@ +MODULE testgetopt ; + +FROM libc IMPORT printf, exit ; +FROM GetOpt IMPORT InitLongOptions, KillLongOptions, AddLongOption, + GetOptLong, PtrToInteger, LongOptions ; +FROM DynamicStrings IMPORT String, InitString, string ; +IMPORT UnixArgs ; +FROM Storage IMPORT ALLOCATE ; +FROM SYSTEM IMPORT ADR ; + + +(* + Assert - +*) + +PROCEDURE Assert (condition: BOOLEAN) ; +BEGIN + IF NOT condition + THEN + printf ("assert failed, condition is false\n") ; + exit (1) + END +END Assert ; + + + +(* + test - +*) + +PROCEDURE test ; +VAR + result : INTEGER ; + optstring: String ; + i, val : INTEGER ; + ch : CHAR ; +BEGIN + longopts := AddLongOption (longopts, 0, InitString ('help'), 0, result, 0) ; + longopts := AddLongOption (longopts, 1, InitString ('dir'), 1, result, 0) ; + longopts := AddLongOption (longopts, 2, NIL, 0, result, 0) ; + optstring := InitString ('hd:') ; + i := 1 ; + REPEAT + val := GetOptLong (UnixArgs.GetArgC (), UnixArgs.GetArgV (), + optstring, longopts, i) ; + IF val = 0 + THEN + printf ("long option detected, result = %d, val = %d, index i = %d, optstring = %s\n", + result, val, i, string (optstring)) + ELSIF val > 0 + THEN + ch := VAL (CHAR, val) ; + CASE ch OF + + 'h': printf ("short option 'h' seen\n") + + ELSE + printf ("unknown short option '%c' seen\n", ch) + END + ELSE + printf ("unknown long option\n") + END ; + INC (i) + UNTIL val <= 0 +END test ; + + +VAR + longopts: LongOptions ; +BEGIN + longopts := InitLongOptions () ; + test ; + longopts := KillLongOptions (longopts) +END testgetopt. |