Commit b6feb83a authored by Karl Wette's avatar Karl Wette

Use LALSupport to supply getopt() and getopt_long() for all of LALSuite

- Make LALgetopt.h a public header, add LALgetopt() and LALgetopt_long_only()
- Remove getopt* sources from LALApps
- Point all LALSuite codes to LALget(), LALgetopt_long(), etc.
- Remove #include <getopt.h>
- Remove #include <unistd.h> if not needed; if needed, check for it using
  AC_CHECK_HEADERS(), #include <config.h> and use #if HAVE_UNISTD_H
- Refs #1932
Original: 117a25af61111e8692b99a3dc0b27e82afc24436
parent 0433e3dd
......@@ -201,7 +201,7 @@ esac
# check for system headers files
AC_HEADER_STDC
AC_CHECK_HEADERS([sys/time.h sys/resource.h unistd.h getopt.h malloc.h regex.h glob.h])
AC_CHECK_HEADERS([sys/time.h sys/resource.h unistd.h malloc.h regex.h glob.h])
AC_CHECK_HEADERS([stdint.h],,[AC_MSG_ERROR([could not find stdint.h])])
AC_CHECK_HEADERS([inttypes.h],,[AC_MSG_ERROR([could not find inttypes.h])])
AC_MSG_CHECKING([for gethostname prototype in unistd.h])
......@@ -329,12 +329,7 @@ AC_TYPE_SSIZE_T
AC_HEADER_TIME
# checks for library functions
AC_CHECK_FUNCS([gmtime_r localtime_r])
AC_CHECK_FUNCS([stat],,[AC_MSG_ERROR([could not find the stat() function])])
AC_CHECK_FUNCS([getopt],,[AC_MSG_ERROR([could not find the getopt() function])])
AC_CHECK_FUNCS([getopt_long],,[AC_MSG_ERROR([could not find the getopt_long() function])])
AC_CHECK_FUNCS([putenv])
AC_CHECK_FUNCS([posix_memalign])
AC_CHECK_FUNCS([gmtime_r localtime_r stat putenv posix_memalign])
# set automake conditionals
AM_CONDITIONAL([INTELFFT],[test x$intelfft = xtrue])
......
......@@ -20,10 +20,10 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA. */
#ifndef _GETOPT_H
#define _GETOPT_H 1
#ifndef SWIG /* exclude from SWIG interface */
#pragma GCC system_header
#ifndef _LALGETOPT_H
#define _LALGETOPT_H
#ifdef __cplusplus
extern "C" {
......@@ -97,17 +97,17 @@ struct option
#define required_argument 1
#define optional_argument 2
extern int LALgetopt_long (int argc, char *const *argv, const char *shortopts,
const struct option *longopts, int *longind);
/* Internal only. Users should not call this directly. */
extern int _getopt_internal (int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind,
int long_only);
int LALgetopt (int argc, char *const *argv, const char *shortopts);
int LALgetopt_long (int argc, char *const *argv, const char *shortopts,
const struct option *longopts, int *longind);
int LALgetopt_long_only (int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind);
#ifdef __cplusplus
}
#endif
#endif /* getopt.h */
#endif /* LALgetopt.h */
#endif /* SWIG */
......@@ -18,4 +18,6 @@ pkginclude_HEADERS = \
TranslateAngles.h \
TranslateMJD.h \
UserInput.h \
ConfigFile.h
ConfigFile.h \
LALgetopt.h \
$(END_OF_LIST)
......@@ -29,9 +29,6 @@
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* #include <ctype.h> */ /* don't use this, as it binds us to GLIBC_2.3 symbols!! */
......
......@@ -21,7 +21,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <config.h>
......
......@@ -28,9 +28,13 @@
* Look for unions "bad" and "wtf" for these hacks.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
# define _(msgid) (msgid)
......@@ -49,7 +53,7 @@
GNU application programs can use a third alternative mode in which
they can distinguish the relative order of options and other arguments. */
#include "LALgetopt.h"
#include <lal/LALgetopt.h>
/* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument,
......@@ -158,6 +162,18 @@ extern int __libc_argc;
extern char **__libc_argv;
#define SWAP_FLAGS(ch1, ch2)
/* Internal only. Users should not call this directly. */
static int _getopt_internal (int argc, char *const *argv,
const char *shortopts,
const struct option *longopts, int *longind,
int long_only);
int
LALgetopt (int argc, char *const *argv, const char *optstring)
{
return _getopt_internal (argc, argv, optstring, (const struct option *) 0, (int *) 0, 0);
}
int
LALgetopt_long (int argc, char *const *argv, const char *options,
const struct option *long_options, int *opt_index)
......@@ -165,6 +181,18 @@ LALgetopt_long (int argc, char *const *argv, const char *options,
return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
}
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
If an option that starts with '-' (not '--') doesn't match a long option,
but does match a short option, it is parsed as a short option
instead. */
int
LALgetopt_long_only (int argc, char *const *argv, const char *options,
const struct option *long_options, int *opt_index)
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
}
/* Exchange two adjacent subsequences of ARGV.
One subsequence is elements [first_nonopt,last_nonopt)
which contains all the non-options that have been skipped so far.
......@@ -331,7 +359,7 @@ _getopt_initialize (int argc, char *const *argv, const char *optstring)
If LONG_ONLY is nonzero, '-' as well as '--' can introduce
long-named options. */
int
static int
_getopt_internal (int argc, char *const *argv, const char *optstring,
const struct option *longopts, int *longind, int long_only)
{
......
......@@ -30,8 +30,7 @@ liblalsupport_la_SOURCES = \
TranslateMJD.c \
UserInput.c \
LALgetopt.c \
LALgetopt.h
$(END_OF_LIST)
liblalsupport_la_LIBADD = $(top_builddir)/lib/lal/liblal.la $(ZLIB_LIBS)
liblalsupport_la_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS)
......
......@@ -18,9 +18,8 @@
* MA 02111-1307 USA
*/
#include "LALgetopt.h"
#include <lal/LALStdio.h>
#include <lal/LALgetopt.h>
#include <lal/UserInput.h>
#include <lal/LogPrintf.h>
#include <lal/LALString.h>
......@@ -80,10 +79,6 @@ typedef struct tagLALUserVariable {
static LALUserVariable UVAR_vars; /**< empty head */
static const CHAR *program_name; /**< keep a pointer to the program name */
/* needed for command-line parsing */
extern char *LALoptarg;
extern int LALoptind, LALopterr, LALoptopt;
/* ---------- internal prototypes ---------- */
/* ----- XLAL interface ----- */
......@@ -296,7 +291,7 @@ XLALUserVarReadCmdline ( int argc, char *argv[] )
}
long_options[pos].name = ptr->name;
long_options[pos].has_arg = (ptr->type == UVAR_TYPE_BOOL) ? optional_argument : required_argument;
long_options[pos].flag = NULL; // get val returned from getopt_long()
long_options[pos].flag = NULL; // get val returned from LALgetopt_long()
long_options[pos].val = 0; // we use longindex to find long-options
pos ++;
} // while ptr->next
......@@ -312,10 +307,10 @@ XLALUserVarReadCmdline ( int argc, char *argv[] )
* of setting optind to 0. As we're linking our private version of GNU getopt, this should be
* guaranteed to work.
*
* Bruce's notes: read getopt_long() source code, and in particular
* Bruce's notes: read LALgetopt_long() source code, and in particular
* _getopt_internal() to see what is initialized.
*/
LALoptind = 0; // reset our local getopt(), getopt_long()
LALoptind = 0; // reset our local LALgetopt(), LALgetopt_long()
// ---------- parse the command-line
int longindex = -1;
......@@ -402,7 +397,7 @@ XLALUserVarReadCmdline ( int argc, char *argv[] )
check_and_mark_as_set ( ptr );
} // while getopt_long()
} // while LALgetopt_long()
// ---------- check if there's any non-option strings left (except for a config-file specification '@file')
if ( (LALoptind == argc - 1) && (argv[LALoptind][0] == '@' ) ) {
......
......@@ -17,9 +17,9 @@
* MA 02111-1307 USA
*/
#include <getopt.h>
#include <lal/LALStdlib.h>
#include <lal/LALgetopt.h>
#include <lal/XLALError.h>
#include <lal/LALString.h>
#include <lal/FileIO.h>
......@@ -53,7 +53,7 @@ int main(int argc, char **argv){
int option_index = 0;
int c;
c = getopt_long( argc, argv, args, long_options, &option_index );
c = LALgetopt_long( argc, argv, args, long_options, &option_index );
if ( c == -1 ) /* end of options */
break;
......@@ -62,12 +62,12 @@ int main(int argc, char **argv){
if ( long_options[option_index].flag )
break;
else
fprintf(stderr, "Error parsing option %s with argument %s\n", long_options[option_index].name, optarg );
fprintf(stderr, "Error parsing option %s with argument %s\n", long_options[option_index].name, LALoptarg );
case 'h': /* help message */
fprintf(stderr, USAGE, program);
exit(0);
case 'f': /* input file */
filename = XLALStringDuplicate( optarg );
filename = XLALStringDuplicate( LALoptarg );
break;
case 'g': /* gzip the file */
gzip = 1;
......
......@@ -56,7 +56,6 @@
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <lal/AVFactories.h>
#include <lal/LALConfig.h>
#include <lal/LALMalloc.h>
......
......@@ -50,7 +50,6 @@
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <lal/AVFactories.h>
#include <lal/LALConfig.h>
#include <lal/LALMalloc.h>
......
......@@ -46,23 +46,13 @@
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <lal/LALStdlib.h>
#include <lal/LALgetopt.h>
#include <lal/AVFactories.h>
#define CODES_(x) #x
#define CODES(x) CODES_(x)
extern char *optarg;
extern int optind;
int verbose = 0;
static void
......@@ -268,7 +258,7 @@ ParseOptions (int argc, char *argv[])
{
int c = -1;
c = getopt (argc, argv, "hqvd:");
c = LALgetopt (argc, argv, "hqvd:");
if (c == -1)
{
break;
......@@ -308,7 +298,7 @@ ParseOptions (int argc, char *argv[])
}
if (optind < argc)
if (LALoptind < argc)
{
Usage (argv[0], 1);
}
......
......@@ -43,23 +43,13 @@
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <lal/LALStdlib.h>
#include <lal/LALgetopt.h>
#include <lal/SeqFactories.h>
#define CODES_(x) #x
#define CODES(x) CODES_(x)
extern char *optarg;
extern int optind;
int verbose = 0;
static void
......@@ -235,7 +225,7 @@ ParseOptions (int argc, char *argv[])
{
int c = -1;
c = getopt (argc, argv, "hqvd:");
c = LALgetopt (argc, argv, "hqvd:");
if (c == -1)
{
break;
......@@ -275,7 +265,7 @@ ParseOptions (int argc, char *argv[])
}
if (optind < argc)
if (LALoptind < argc)
{
Usage (argv[0], 1);
}
......
......@@ -49,23 +49,13 @@
#include <lal/PrintVector.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <lal/LALStdlib.h>
#include <lal/LALgetopt.h>
#include <lal/AVFactories.h>
#define CODES_(x) #x
#define CODES(x) CODES_(x)
extern char *optarg;
extern int optind;
int verbose = 0;
static void
......@@ -248,7 +238,7 @@ ParseOptions (int argc, char *argv[])
{
int c = -1;
c = getopt (argc, argv, "hqvd:");
c = LALgetopt (argc, argv, "hqvd:");
if (c == -1)
{
break;
......@@ -288,7 +278,7 @@ ParseOptions (int argc, char *argv[])
}
if (optind < argc)
if (LALoptind < argc)
{
Usage (argv[0], 1);
}
......
......@@ -44,23 +44,13 @@
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <lal/LALStdlib.h>
#include <lal/LALgetopt.h>
#include <lal/SeqFactories.h>
#define CODES_(x) #x
#define CODES(x) CODES_(x)
extern char *optarg;
extern int optind;
int verbose = 0;
static void
......@@ -243,7 +233,7 @@ ParseOptions (int argc, char *argv[])
{
int c = -1;
c = getopt (argc, argv, "hqvd:");
c = LALgetopt (argc, argv, "hqvd:");
if (c == -1)
{
break;
......@@ -283,7 +273,7 @@ ParseOptions (int argc, char *argv[])
}
if (optind < argc)
if (LALoptind < argc)
{
Usage (argv[0], 1);
}
......
......@@ -57,15 +57,8 @@
#include <string.h>
#include <math.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <lal/LALStdlib.h>
#include <lal/LALgetopt.h>
#include <lal/AVFactories.h>
#include <lal/ComplexFFT.h>
#include <config.h>
......@@ -73,9 +66,6 @@
#define CODES_(x) #x
#define CODES(x) CODES_(x)
extern char *optarg;
extern int optind;
int verbose = 0;
static void
......@@ -378,7 +368,7 @@ ParseOptions( int argc, char *argv[] )
{
int c = -1;
c = getopt( argc, argv, "hqvd:" );
c = LALgetopt( argc, argv, "hqvd:" );
if ( c == -1 )
{
break;
......@@ -418,7 +408,7 @@ ParseOptions( int argc, char *argv[] )
}
if ( optind < argc )
if ( LALoptind < argc )
{
Usage( argv[0], 1 );
}
......
......@@ -63,15 +63,8 @@
#include <stdlib.h>
#include <math.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <lal/LALStdlib.h>
#include <lal/LALgetopt.h>
#include <lal/LALConstants.h>
#include <lal/SeqFactories.h>
#include <lal/RealFFT.h>
......@@ -81,9 +74,6 @@
#define CODES_(x) #x
#define CODES(x) CODES_(x)
extern char *optarg;
extern int optind;
int verbose = 0;
UINT4 m_ = 1; /* number of random trials */
UINT4 n_ = 0; /* size of each transform */
......@@ -405,7 +395,7 @@ ParseOptions( int argc, char *argv[] )
{
int c = -1;
c = getopt( argc, argv, "hqvd:m:n:" );
c = LALgetopt( argc, argv, "hqvd:m:n:" );
if ( c == -1 )
{
break;
......@@ -414,11 +404,11 @@ ParseOptions( int argc, char *argv[] )
switch ( c )
{
case 'n': /* set FFT size */
n_ = atoi( optarg );
n_ = atoi( LALoptarg );
break;
case 'm': /* set number of trials */
m_ = atoi( optarg );
m_ = atoi( LALoptarg );
break;
case 'd': /* set debug level */
......@@ -453,7 +443,7 @@ ParseOptions( int argc, char *argv[] )
}
if ( optind < argc )
if ( LALoptind < argc )
{
Usage( argv[0], 1 );
}
......