+= epicsGetopt to declare (default) or implement (WIN) getopt()

This commit is contained in:
Ralph Lange
2004-04-14 14:38:25 +00:00
parent 30efff9cd0
commit 1ab4c344aa
8 changed files with 190 additions and 191 deletions
+2 -1
View File
@@ -18,7 +18,8 @@ SHARED_LIBRARIES = NO
LIBRARY_HOST += catools
catools_SRCS += tool_lib.c parseOptions.c
catools_SRCS += tool_lib.c
catools_LIBS += ca Com
#Remove LIBNAME from the library names to be installed (make it a local library)
+21 -21
View File
@@ -25,7 +25,6 @@
#include <cadef.h>
#include "tool_lib.h"
#include "parseOptions.h"
#define VALID_DOUBLE_DIGITS 18 /* Max usable precision for a double */
#define PEND_EVENT_SLICES 5 /* No. of pend_event slices for callback requests */
@@ -51,7 +50,6 @@ void usage (void)
" Default output format is \"name value\"\n"
" -t: Terse mode - print only value, without name\n"
" -a: Wide mode \"name timestamp value stat sevr\" (read PVs as DBR_TIME_xxx)\n"
" -n: Print DBF_ENUM values as number (default are enum string values)\n"
" -d <type>: Request specific dbr type; use string (DBR_ prefix may be omitted)\n"
" or number of one of the following types:\n"
" DBR_STRING 0 DBR_STS_FLOAT 9 DBR_TIME_LONG 19 DBR_CTRL_SHORT 29\n"
@@ -65,6 +63,8 @@ void usage (void)
" DBR_STS_STRING 7 DBR_TIME_FLOAT 16 DBR_GR_LONG 26 DBR_CLASS_NAME 38\n"
" DBR_STS_SHORT 8 DBR_TIME_ENUM 17 DBR_GR_DOUBLE 27\n"
" DBR_STS_INT 8 DBR_TIME_CHAR 18 DBR_CTRL_STRING 28\n"
"Enum format:\n"
" -n: Print DBF_ENUM values as number (default are enum string values)\n"
"Arrays: Value format: print number of requested values, then list of values\n"
" Default: Print all values\n"
" -# <count>: Print first <count> elements of an array\n"
@@ -319,7 +319,7 @@ int main (int argc, char *argv[])
setvbuf(stdout,NULL,_IOLBF,0); /* Set stdout to line buffering */
while ((opt = parseOptions(argc, argv, ":taicnhe:f:#:d:0:w:")) != -1) {
while ((opt = getopt(argc, argv, ":taicnhe:f:#:d:0:w:")) != -1) {
switch (opt) {
case 'h': /* Print usage */
usage();
@@ -336,13 +336,13 @@ int main (int argc, char *argv[])
case 'd': /* Data type specification */
complainIfNotPlainAndSet(&format, specifiedDbr);
/* Argument (type) may be text or number */
if (sscanf(optArgument, "%d", &type) != 1)
if (sscanf(optarg, "%d", &type) != 1)
{
dbr_text_to_type(optArgument, type);
dbr_text_to_type(optarg, type);
if (type == -1) /* Invalid? Try prefix DBR_ */
{
char str[30] = "DBR_";
strncat(str, optArgument, 25);
strncat(str, optarg, 25);
dbr_text_to_type(str, type);
}
}
@@ -358,26 +358,26 @@ int main (int argc, char *argv[])
charAsNr=1;
break;
case 'w': /* Set CA timeout value */
if(sscanf(optArgument,"%lf", &timeout) != 1)
if(sscanf(optarg,"%lf", &timeout) != 1)
{
fprintf(stderr, "'%s' is not a valid timeout value "
"- ignored. ('caget -h' for help.)\n", optArgument);
"- ignored. ('caget -h' for help.)\n", optarg);
timeout = DEFAULT_TIMEOUT;
}
break;
case '#': /* Array count */
if (sscanf(optArgument,"%d", &count) != 1)
if (sscanf(optarg,"%d", &count) != 1)
{
fprintf(stderr, "'%s' is not a valid array element count "
"- ignored. ('caget -h' for help.)\n", optArgument);
"- ignored. ('caget -h' for help.)\n", optarg);
count = 0;
}
break;
case 'e': /* Select %e format, using <arg> digits */
if (sscanf(optArgument, "%d", &digits) != 1)
if (sscanf(optarg, "%d", &digits) != 1)
fprintf(stderr,
"Invalid precision argument '%s' "
"for option '-e' - ignored.\n", optArgument);
"for option '-e' - ignored.\n", optarg);
else
{
if (digits>=0 && digits<=VALID_DOUBLE_DIGITS)
@@ -388,9 +388,9 @@ int main (int argc, char *argv[])
}
break;
case 'f': /* Select %f format, using <arg> digits */
if (sscanf(optArgument, "%d", &digits) != 1)
if (sscanf(optarg, "%d", &digits) != 1)
fprintf(stderr, "Invalid precision argument '%s' "
"for option '-f' - ignored.\n", optArgument);
"for option '-f' - ignored.\n", optarg);
else
{
if (digits>=0 && digits<=VALID_DOUBLE_DIGITS)
@@ -402,24 +402,24 @@ int main (int argc, char *argv[])
break;
case '0': /* Select integer format */
type = DBR_LONG;
switch ((char) *optArgument) {
switch ((char) *optarg) {
case 'x': outType = hex; break; /* 0x print Hex */
case 'b': outType = bin; break; /* 0b print Binary */
case 'o': outType = oct; break; /* 0o print Octal */
default :
fprintf(stderr, "Invalid argument '%s' "
"for option '-0' - ignored.\n", optArgument);
"for option '-0' - ignored.\n", optarg);
}
break;
case '?':
fprintf(stderr,
"Unrecognized option: '-%c'. ('caget -h' for help.)\n",
optChar);
optopt);
return 1;
case ':':
fprintf(stderr,
"Option '-%c' requires an argument. ('caget -h' for help.)\n",
optChar);
optopt);
return 1;
default :
usage();
@@ -427,7 +427,7 @@ int main (int argc, char *argv[])
}
}
nPvs = argc - optIndex; /* Remaining arg list are PV names */
nPvs = argc - optind; /* Remaining arg list are PV names */
if (nPvs < 1)
{
@@ -452,8 +452,8 @@ int main (int argc, char *argv[])
}
/* Connect channels */
for (n = 0; optIndex < argc; n++, optIndex++)
pvs[n].name = argv[optIndex]; /* Copy PV names from command line */
for (n = 0; optind < argc; n++, optind++)
pvs[n].name = argv[optind] ; /* Copy PV names from command line */
connect_pvs(pvs, nPvs);
-101
View File
@@ -1,101 +0,0 @@
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* Copyright (c) 2002 Berliner Elektronenspeicherringgesellschaft fuer
* Synchrotronstrahlung.
* EPICS BASE Versions 3.13.7
* and higher are distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
\*************************************************************************/
/*
* $Id$
*
* Author: Ralph Lange (BESSY)
*
*/
/* IMPORTANT NOTE:
* ---------------
* This library is a workaround for achieving getopt() functionality in
* EPICS Base 3.14.5 - it will be replaced as soon as possible without
* further notice.
*
* DO NOT WRITE ANY CODE BASED ON THIS LIBRARY - IT WILL BREAK.
*
*/
#include <string.h>
#include "parseOptions.h"
int optIndex = 1;
char *optArgument = NULL;
char optChar;
static unsigned int charIndex;
int parseOptions (int argc, char * const argv[], const char *options)
{
unsigned int i;
char *opt;
if (optIndex >= argc) return -1;
opt = argv[optIndex];
if (charIndex >= strlen(opt)) /* End of option group */
{
charIndex = 0;
optIndex++;
}
if (charIndex == 0) /* Search for next option group */
{
for (; optIndex < argc; optIndex++)
{
if (*(opt = argv[optIndex]) == '-')
{
if (*(opt+1) != '-')
{ /* New option group found */
charIndex++;
break;
} else
continue;
}
/* No more options */
return -1;
}
if (charIndex == 0) return -1; /* No more options */
}
optChar = opt[charIndex]; /* Test candidate */
for (i = 0; i < strlen(options); i++) /* Check for validity */
{
if (optChar == options[i]) /* Found option in option string! */
{
if (options[i+1] == ':') /* Option has an argument? */
{ /* Set optArgument accordingly */
optIndex++; /* Point to next argument */
if (charIndex+1 >= strlen(opt))
{ /* End of option group */
optArgument = argv[optIndex];
charIndex = 0;
if (*optArgument == '-')
return options[0] == ':' ? ':' : '?';
optIndex++;
} else { /* point to remaining argument */
optArgument = opt + charIndex+1;
}
charIndex = 0;
} else {
optArgument = 0;
charIndex++;
}
return optChar;
}
}
/* Option not found */
charIndex++;
return '?';
}
-68
View File
@@ -1,68 +0,0 @@
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* Copyright (c) 2002 Berliner Elektronenspeicherringgesellschaft fuer
* Synchrotronstrahlung.
* EPICS BASE Versions 3.13.7
* and higher are distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
\*************************************************************************/
/*
* $Id$
*
* Author: Ralph Lange (BESSY)
*
*/
#ifndef INCLparseOptionsh
#define INCLparseOptionsh
/* IMPORTANT NOTE:
* ---------------
* This library is a workaround for achieving getopt() functionality in
* EPICS Base 3.14.5 - it will be replaced as soon as possible without
* further notice.
*
* DO NOT WRITE ANY CODE BASED ON THIS LIBRARY - IT WILL BREAK.
*
*/
/* This is a minimal version of a parser whose function is somewhat getopt()
* compatible. Subsequent calls return the command line options one by one.
* The special option "--" forces the end of option scanning.
*
* parseOptions returns the option character found on the command line
* returns '?' if an option is invalid / misses an argument (see below)
* returns -1 if there are no more options (argv[optIndex] is
* the first non-option argument)
*
* options - valid options string (format is similar to getopt):
* <letter> for a valid option
* <letter>: for an option that takes an argument
* : as first character: parseOptions will return ':'
* instead of '?' for a missing argument
*
* optArgument - Points to an options argument (NULL otherwise)
* optChar - Contains the last tested character
* (i.e. the failing one for an unrecognized option)
* optIndex - contains the index to the next (first non-option) argument
*
* Differences to getopt():
* o No error messages printed
* o No argv[] shuffling
* o No special characters '+' or '-' as first options character
*
*/
extern int optIndex;
extern char *optArgument;
extern char optChar;
extern int parseOptions (int argc, char * const argv[], const char *options);
/*
* no additions below this endif
*/
#endif /* ifndef INCLparseOptionsh */
+3
View File
@@ -167,6 +167,7 @@ INC += osiWireFormat.h
INC += epicsReadline.h
INC += epicsMessageQueue.h
INC += epicsStdio.h
INC += epicsGetopt.h
INC += devLib.h
@@ -201,6 +202,8 @@ SRCS += osdProcess.c
SRCS += osdNetIntf.c
SRCS += osdMessageQueue.c
SRCS_WIN32 += epicsGetopt.c
SRCS_vxWorks += devLib.c
SRCS_vxWorks += devLibOSD.c
SRCS_RTEMS += devLib.c
+118
View File
@@ -0,0 +1,118 @@
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _getprogname() nargv[0]
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
optopt, /* character checked for validity */
optreset; /* reset getopt */
char *optarg; /* argument associated with option */
#define BADCH (int)'?'
#define BADARG (int)':'
#define EMSG ""
/*
* getopt --
* Parse argc/argv argument vector.
*/
int
getopt(nargc, nargv, ostr)
int nargc;
char * const *nargv;
const char *ostr;
{
static char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
if (optreset || !*place) { /* update scanning pointer */
optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
return (-1);
}
if (place[1] && *++place == '-') { /* found "--" */
++optind;
place = EMSG;
return (-1);
}
} /* option letter okay? */
if ((optopt = (int)*place++) == (int)':' ||
!(oli = strchr(ostr, optopt))) {
/*
* if the user didn't specify '-' as an option,
* assume it means -1.
*/
if (optopt == (int)'-')
return (-1);
if (!*place)
++optind;
if (opterr && *ostr != ':' && optopt != BADCH)
(void)fprintf(stderr, "%s: illegal option -- %c\n",
_getprogname(), optopt);
return (BADCH);
}
if (*++oli != ':') { /* don't need argument */
optarg = NULL;
if (!*place)
++optind;
}
else { /* need an argument */
if (*place) /* no white space */
optarg = place;
else if (nargc <= ++optind) { /* no arg */
place = EMSG;
if (*ostr == ':')
return (BADARG);
if (opterr)
(void)fprintf(stderr,
"%s: option requires an argument -- %c\n",
_getprogname(), optopt);
return (BADCH);
}
else /* white space */
optarg = nargv[optind];
place = EMSG;
++optind;
}
return (optopt); /* dump back option letter */
}
+29
View File
@@ -0,0 +1,29 @@
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* Copyright (c) 2002 Berliner Elektronenspeicherringgesellschaft fuer
* Synchrotronstrahlung.
* EPICS BASE Versions 3.13.7
* and higher are distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
\*************************************************************************/
#ifndef _EPICS_GETOPT_H
#define _EPICS_GETOPT_H
#ifdef __cplusplus
extern "C" {
#endif
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#ifdef __cplusplus
}
#endif
#endif /* _EPICS_GETOPT_H */
+17
View File
@@ -0,0 +1,17 @@
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* Copyright (c) 2002 Berliner Elektronenspeicherringgesellschaft fuer
* Synchrotronstrahlung.
* EPICS BASE Versions 3.13.7
* and higher are distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
\*************************************************************************/
#ifndef _EPICS_GETOPT_H
#define _EPICS_GETOPT_H
#include <unistd.h>
#endif /* _EPICS_GETOPT_H */