gcc/libgm2/libm2pim/cgetopt.cc
Gaius Mulley 19c5dfc29d PR 109125 Modula2 SIGBUS in m2pim_ldtoa_ldtoa
13 regression failures seen on sparc SIGBUS in m2pim_ldtoa_ldtoa.
This patch fixes int bool parameter mismatches between the
definition modules and their C/C++ implementations.

gcc/m2/ChangeLog:

	PR modula2/109125
	* gm2-libs-ch/cgetopt.c (cgetopt_SetOption): Replace int
	for bool.
	* gm2-libs-ch/termios.c (doSetUnset): Replace int for bool.
	* gm2-libs/Builtins.mod (isfinitef): Correct typo in return
	statement.

libgm2/ChangeLog:

	PR modula2/109125
	* libm2iso/ErrnoCategory.cc (FALSE): Remove.
	(TRUE): Remove.
	* libm2iso/wrapsock.c (TRUE): Remove.
	(FALSE): Remove.
	* libm2iso/wraptime.cc (TRUE): Remove.
	(FALSE): Remove.
	* libm2pim/cgetopt.cc: Replace int for bool for every BOOLEAN
	parameter in the definition module.
	* libm2pim/dtoa.cc: Ditto.
	* libm2pim/ldtoa.cc: Ditto.
	* libm2pim/termios.cc: Ditto.
	(doSetUnset): Replace int for bool.

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
2023-03-14 19:52:11 +00:00

162 lines
4 KiB
C++

/* cgetopt.cc provide access to the C getopt library.
Copyright (C) 2009-2022 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Modula-2 is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <m2rts.h>
#define EXPORT(FUNC) m2pim ## _cgetopt_ ## FUNC
#define M2EXPORT(FUNC) m2pim ## _M2_cgetopt_ ## FUNC
#define M2LIBNAME "m2pim"
extern "C" {char *EXPORT(optarg);}
extern "C" {int EXPORT(optind);}
extern "C" {int EXPORT(opterr);}
extern "C" {int EXPORT(optopt);}
extern "C" char
EXPORT(getopt) (int argc, char *argv[], char *optstring)
{
char r = getopt (argc, argv, optstring);
EXPORT(optarg) = optarg;
EXPORT(optind) = optind;
EXPORT(opterr) = opterr;
EXPORT(optopt) = optopt;
if (r == (char)-1)
return (char)0;
return r;
}
extern "C" int
EXPORT(getopt_long) (int argc, char *argv[], char *optstring,
const struct option *longopts, int *longindex)
{
int r = getopt_long (argc, argv, optstring, longopts, longindex);
EXPORT(optarg) = optarg;
EXPORT(optind) = optind;
EXPORT(opterr) = opterr;
EXPORT(optopt) = optopt;
return r;
}
extern "C" int
EXPORT(getopt_long_only) (int argc, char *argv[], char *optstring,
const struct option *longopts, int *longindex)
{
int r = getopt_long_only (argc, argv, optstring, longopts, longindex);
EXPORT(optarg) = optarg;
EXPORT(optind) = optind;
EXPORT(opterr) = opterr;
EXPORT(optopt) = optopt;
return r;
}
typedef struct cgetopt_Options_s
{
struct option *cinfo;
unsigned int high;
} cgetopt_Options;
/* InitOptions a constructor for Options. */
extern "C" cgetopt_Options *
EXPORT(InitOptions) (void)
{
cgetopt_Options *o = (cgetopt_Options *)malloc (sizeof (cgetopt_Options));
o->cinfo = (struct option *)malloc (sizeof (struct option));
o->high = 0;
return o;
}
/* KillOptions a deconstructor for Options. Returns NULL after freeing
up all allocated memory associated with o. */
extern "C" cgetopt_Options *
EXPORT(KillOptions) (cgetopt_Options *o)
{
free (o->cinfo);
free (o);
return NULL;
}
/* SetOption set option[index] with {name, has_arg, flag, val}. */
extern "C" void
EXPORT(SetOption) (cgetopt_Options *o, unsigned int index, char *name,
bool has_arg, int *flag, int val)
{
if (index > o->high)
{
o->cinfo
= (struct option *)malloc (sizeof (struct option) * (index + 1));
o->high = index + 1;
}
o->cinfo[index].name = name;
o->cinfo[index].has_arg = has_arg;
o->cinfo[index].flag = flag;
o->cinfo[index].val = val;
}
/* GetLongOptionArray returns a pointer to the C array containing all
long options. */
extern "C" struct option *
EXPORT(GetLongOptionArray) (cgetopt_Options *o)
{
return o->cinfo;
}
/* GNU Modula-2 linking fodder. */
extern "C" void
M2EXPORT(init) (int, char *argv[], char *env[])
{
}
extern "C" void
M2EXPORT(fini) (int, char *argv[], char *env[])
{
}
extern "C" void
M2EXPORT(dep) (void)
{
}
extern "C" void __attribute__((__constructor__))
M2EXPORT(ctor) (void)
{
m2pim_M2RTS_RegisterModule ("cgetopt", M2LIBNAME,
M2EXPORT(init), M2EXPORT(fini),
M2EXPORT(dep));
}