Use our getopt implementation on all platforms

* rename xgetopt.* to sc_getopt.*, move to src/base
* rename getopt() to sc_getopt()
This commit is contained in:
Mark Pictor
2012-06-25 21:44:56 -04:00
parent fb45e4e581
commit 1a60cf9fab
8 changed files with 27 additions and 247 deletions
+1 -4
View File
@@ -2,10 +2,7 @@
set(SCL_BASE_SOURCES
scl_memmgr.cc
scl_trace_fprintf.c
)
include_directories(
${SCL_SOURCE_DIR}/include
sc_getopt.cc
)
if(MSVC OR BORLAND)
@@ -1,10 +1,12 @@
/****** RENAMED from xgetopt.cc to sc_getopt.cc ***********/
// XGetopt.cpp Version 1.2
//
// Author: Hans Dietrich
// hdietrich2@hotmail.com
//
// Description:
// XGetopt.cpp implements getopt(), a function to parse command lines.
// XGetopt.cpp implements sc_getopt(), a function to parse command lines.
//
// History
// Version 1.2 - 2003 May 17
@@ -36,7 +38,7 @@
#include <string.h>
///////////////////////////////////////////////////////////////////////////////
#include "XGetopt.h"
#include "sc_getopt.h"
///////////////////////////////////////////////////////////////////////////////
//
@@ -44,21 +46,21 @@
//
//
// NAME
// getopt -- parse command line options
// sc_getopt -- parse command line options
//
// SYNOPSIS
// int getopt(int argc, char *argv[], char *optstring)
// int sc_getopt(int argc, char *argv[], char *optstring)
//
// extern char *optarg;
// extern int optind;
//
// DESCRIPTION
// The getopt() function parses the command line arguments. Its
// The sc_getopt() function parses the command line arguments. Its
// arguments argc and argv are the argument count and array as
// passed into the application on program invocation. In the case
// of Visual C++ programs, argc and argv are available via the
// variables __argc and __argv (double underscores), respectively.
// getopt returns the next option letter in argv that matches a
// sc_getopt returns the next option letter in argv that matches a
// letter in optstring. (Note: Unicode programs should use
// __targv instead of __argv. Also, all character and string
// literals should be enclosed in _T( ) ).
@@ -67,17 +69,17 @@
// is followed by a colon, the option is expected to have an argument
// that may or may not be separated from it by white space. optarg
// is set to point to the start of the option argument on return from
// getopt.
// sc_getopt.
//
// Option letters may be combined, e.g., "-ab" is equivalent to
// "-a -b". Option letters are case sensitive.
//
// getopt places in the external variable optind the argv index
// sc_getopt places in the external variable optind the argv index
// of the next argument to be processed. optind is initialized
// to 0 before the first call to getopt.
// to 0 before the first call to sc_getopt.
//
// When all options have been processed (i.e., up to the first
// non-option argument), getopt returns EOF, optarg will point
// non-option argument), sc_getopt returns EOF, optarg will point
// to the argument, and optind will be set to the argv index of
// the argument. If there are no non-option arguments, optarg
// will be set to NULL.
@@ -87,8 +89,8 @@
// will be skipped.
//
// RETURN VALUE
// For option letters contained in the string optstring, getopt
// will return the option letter. getopt returns a question mark (?)
// For option letters contained in the string optstring, sc_getopt
// will return the option letter. sc_getopt returns a question mark (?)
// when it encounters an option letter not included in optstring.
// EOF is returned when processing is finished.
//
@@ -98,7 +100,7 @@
// 3) The environment variable POSIXLY_CORRECT is not supported.
// 4) The + syntax is not supported.
// 5) The automatic permutation of arguments is not supported.
// 6) This implementation of getopt() returns EOF if an error is
// 6) This implementation of sc_getopt() returns EOF if an error is
// encountered, instead of -1 as the latest standard requires.
//
// EXAMPLE
@@ -106,7 +108,7 @@
// {
// int c;
//
// while ((c = getopt(argc, argv, _T("aBn:"))) != EOF)
// while ((c = sc_getopt(argc, argv, _T("aBn:"))) != EOF)
// {
// switch (c)
// {
@@ -153,7 +155,7 @@
char * optarg; // global argument pointer
int optind = 0; // global argv index
int getopt( int argc, char * argv[], char * optstring ) {
int sc_getopt( int argc, char * argv[], char * optstring ) {
static char * next = NULL;
if( optind == 0 ) {
next = NULL;
+2 -1
View File
@@ -1,3 +1,4 @@
/****** RENAMED from xgetopt.h to sc_getopt.h ***********/
// XGetopt.h Version 1.2
//
// Author: Hans Dietrich
@@ -22,7 +23,7 @@ extern "C" {
extern int optind, opterr;
extern char * optarg;
int getopt( int argc, char * argv[], char * optstring );
int sc_getopt( int argc, char * argv[], char * optstring );
#ifdef __cplusplus
}
+1 -2
View File
@@ -56,8 +56,7 @@ SET(CHECK_EXPRESS_SOURCES
IF(MSVC OR BORLAND)
set(CHECK_EXPRESS_EXTRA_SOURCES
xgetopt.cc
inithook.c
inithook.c
)
ENDIF()
+2 -4
View File
@@ -84,9 +84,7 @@
/* process.h defines getpid() function on WIN32 systems */
# include <process.h>
#endif
#ifndef HAVE_GETOPT
# include "xgetopt.h"
#endif
#include "sc_getopt.h"
#include "express/error.h"
#include "express/express.h"
#include "express/resolve.h"
@@ -159,7 +157,7 @@ int main( int argc, char ** argv ) {
}
optind = 1;
while( ( c = getopt( argc, argv, EXPRESSgetopt_options ) ) != -1 )
while( ( c = sc_getopt( argc, argv, EXPRESSgetopt_options ) ) != -1 )
switch( c ) {
case 'd':
ERRORdebugging = 1;
+1 -5
View File
@@ -28,14 +28,10 @@ include_directories(
${SCL_SOURCE_DIR}/src/base
)
IF(MSVC OR BORLAND)
set(fedex_plus_EXTRA_SOURCES xgetopt.cc )
ENDIF()
IF ("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
add_definitions( -DYYDEBUG ) #-Ddebugging )
ENDIF()
SCL_ADDEXEC(fedex_plus "${fedex_plus_SOURCES} ${fedex_plus_EXTRA_SOURCES}" "libexppp express base")
SCL_ADDEXEC(fedex_plus "${fedex_plus_SOURCES}" "libexppp express base")
add_dependencies( fedex_plus version_string )
-212
View File
@@ -1,212 +0,0 @@
// XGetopt.cpp Version 1.2
//
// Author: Hans Dietrich
// hdietrich2@hotmail.com
//
// Description:
// XGetopt.cpp implements getopt(), a function to parse command lines.
//
// History
// Version 1.2 - 2003 May 17
// - Added Unicode support
//
// Version 1.1 - 2002 March 10
// - Added example to XGetopt.cpp module header
//
// This software is released into the public domain.
// You are free to use it in any way you like.
//
// This software is provided "as is" with no expressed
// or implied warranty. I accept no liability for any
// damage or loss of business that this software may cause.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// if you are using precompiled headers then include this line:
//#include "stdafx.h"
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// if you are not using precompiled headers then include these lines:
//#include <windows.h>
#include <stdio.h>
#include <string.h>
///////////////////////////////////////////////////////////////////////////////
#include "XGetopt.h"
///////////////////////////////////////////////////////////////////////////////
//
// X G e t o p t . c p p
//
//
// NAME
// getopt -- parse command line options
//
// SYNOPSIS
// int getopt(int argc, char *argv[], char *optstring)
//
// extern char *optarg;
// extern int optind;
//
// DESCRIPTION
// The getopt() function parses the command line arguments. Its
// arguments argc and argv are the argument count and array as
// passed into the application on program invocation. In the case
// of Visual C++ programs, argc and argv are available via the
// variables __argc and __argv (double underscores), respectively.
// getopt returns the next option letter in argv that matches a
// letter in optstring. (Note: Unicode programs should use
// __targv instead of __argv. Also, all character and string
// literals should be enclosed in _T( ) ).
//
// optstring is a string of recognized option letters; if a letter
// is followed by a colon, the option is expected to have an argument
// that may or may not be separated from it by white space. optarg
// is set to point to the start of the option argument on return from
// getopt.
//
// Option letters may be combined, e.g., "-ab" is equivalent to
// "-a -b". Option letters are case sensitive.
//
// getopt places in the external variable optind the argv index
// of the next argument to be processed. optind is initialized
// to 0 before the first call to getopt.
//
// When all options have been processed (i.e., up to the first
// non-option argument), getopt returns EOF, optarg will point
// to the argument, and optind will be set to the argv index of
// the argument. If there are no non-option arguments, optarg
// will be set to NULL.
//
// The special option "--" may be used to delimit the end of the
// options; EOF will be returned, and "--" (and everything after it)
// will be skipped.
//
// RETURN VALUE
// For option letters contained in the string optstring, getopt
// will return the option letter. getopt returns a question mark (?)
// when it encounters an option letter not included in optstring.
// EOF is returned when processing is finished.
//
// BUGS
// 1) Long options are not supported.
// 2) The GNU double-colon extension is not supported.
// 3) The environment variable POSIXLY_CORRECT is not supported.
// 4) The + syntax is not supported.
// 5) The automatic permutation of arguments is not supported.
// 6) This implementation of getopt() returns EOF if an error is
// encountered, instead of -1 as the latest standard requires.
//
// EXAMPLE
// BOOL CMyApp::ProcessCommandLine(int argc, char *argv[])
// {
// int c;
//
// while ((c = getopt(argc, argv, _T("aBn:"))) != EOF)
// {
// switch (c)
// {
// case _T('a'):
// TRACE(_T("option a\n"));
// //
// // set some flag here
// //
// break;
//
// case _T('B'):
// TRACE( _T("option B\n"));
// //
// // set some other flag here
// //
// break;
//
// case _T('n'):
// TRACE(_T("option n: value=%d\n"), atoi(optarg));
// //
// // do something with value here
// //
// break;
//
// case _T('?'):
// TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]);
// return FALSE;
// break;
//
// default:
// TRACE(_T("WARNING: no handler for option %c\n"), c);
// return FALSE;
// break;
// }
// }
// //
// // check for non-option args here
// //
// return TRUE;
// }
//
///////////////////////////////////////////////////////////////////////////////
char * optarg; // global argument pointer
int optind = 0; // global argv index
int getopt( int argc, char * argv[], char * optstring ) {
static char * next = NULL;
if( optind == 0 ) {
next = NULL;
}
optarg = NULL;
if( next == NULL || *next == '\0' ) {
if( optind == 0 ) {
optind++;
}
if( optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0' ) {
optarg = NULL;
if( optind < argc ) {
optarg = argv[optind];
}
return EOF;
}
if( strcmp( argv[optind], "--" ) == 0 ) {
optind++;
optarg = NULL;
if( optind < argc ) {
optarg = argv[optind];
}
return EOF;
}
next = argv[optind];
next++; // skip past -
optind++;
}
char c = *next++;
char * cp = strchr( optstring, c );
if( cp == NULL || c == ':' ) {
return '?';
}
cp++;
if( *cp == ':' ) {
if( *next != '\0' ) {
optarg = next;
next = NULL;
} else if( optind < argc ) {
optarg = argv[optind];
optind++;
} else {
return '?';
}
}
return c;
}
+3 -4
View File
@@ -25,9 +25,8 @@ extern void SchemaInit( class Registry & );
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifndef HAVE_GETOPT
# include <xgetopt.h>
#endif
#include <sc_getopt.h>
/**
* Compare the schema names from the lib (generated by fedex_plus) and
@@ -112,7 +111,7 @@ int main( int argc, char * argv[] ) {
if( argc > 4 || argc < 2 ) {
printUse( argv[0] );
}
while( ( c = getopt( argc, argv, "isv" ) ) != -1 ) {
while( ( c = sc_getopt( argc, argv, "isv" ) ) != -1 ) {
switch( c ) {
case 'i':
ignoreErr = true;