removed optionsfrom.c from libfreeswan
This commit is contained in:
@@ -3,7 +3,7 @@ libfreeswan_a_SOURCES = addrtoa.c addrtot.c addrtypeof.c anyaddr.c atoaddr.c ato
|
|||||||
atosa.c atosubnet.c atoul.c copyright.c datatot.c freeswan.h \
|
atosa.c atosubnet.c atoul.c copyright.c datatot.c freeswan.h \
|
||||||
goodmask.c initaddr.c initsaid.c initsubnet.c internal.h \
|
goodmask.c initaddr.c initsaid.c initsubnet.c internal.h \
|
||||||
ipsec_param.h ipsec_policy.h \
|
ipsec_param.h ipsec_policy.h \
|
||||||
keyblobtoid.c optionsfrom.c pfkey_v2_build.c pfkey_v2_debug.c \
|
keyblobtoid.c pfkey_v2_build.c pfkey_v2_debug.c \
|
||||||
pfkey_v2_ext_bits.c pfkey_v2_parse.c portof.c prng.c rangetoa.c \
|
pfkey_v2_ext_bits.c pfkey_v2_parse.c portof.c prng.c rangetoa.c \
|
||||||
pfkey.h pfkeyv2.h rangetosubnet.c sameaddr.c satoa.c \
|
pfkey.h pfkeyv2.h rangetosubnet.c sameaddr.c satoa.c \
|
||||||
satot.c subnetof.c subnettoa.c subnettot.c \
|
satot.c subnetof.c subnettoa.c subnettot.c \
|
||||||
@@ -15,6 +15,6 @@ INCLUDES = \
|
|||||||
-I$(top_srcdir)/src/pluto
|
-I$(top_srcdir)/src/pluto
|
||||||
|
|
||||||
dist_man3_MANS = anyaddr.3 atoaddr.3 atoasr.3 atosa.3 atoul.3 goodmask.3 initaddr.3 initsubnet.3 \
|
dist_man3_MANS = anyaddr.3 atoaddr.3 atoasr.3 atosa.3 atoul.3 goodmask.3 initaddr.3 initsubnet.3 \
|
||||||
keyblobtoid.3 optionsfrom.3 portof.3 prng.3 rangetosubnet.3 sameaddr.3 subnetof.3 \
|
keyblobtoid.3 portof.3 prng.3 rangetosubnet.3 sameaddr.3 subnetof.3 \
|
||||||
ttoaddr.3 ttodata.3 ttosa.3 ttoul.3 version.3
|
ttoaddr.3 ttodata.3 ttosa.3 ttoul.3 version.3
|
||||||
|
|
||||||
|
|||||||
@@ -1,181 +0,0 @@
|
|||||||
.TH IPSEC_OPTIONSFROM 3 "16 Oct 1998"
|
|
||||||
.SH NAME
|
|
||||||
ipsec optionsfrom \- read additional ``command-line'' options from file
|
|
||||||
.SH SYNOPSIS
|
|
||||||
.B "#include <freeswan.h>
|
|
||||||
.sp
|
|
||||||
.B "const char *optionsfrom(char *filename, int *argcp,"
|
|
||||||
.ti +1c
|
|
||||||
.B "char ***argvp, int optind, FILE *errsto);"
|
|
||||||
.SH DESCRIPTION
|
|
||||||
.I Optionsfrom
|
|
||||||
is called from within a
|
|
||||||
.IR getopt_long (3)
|
|
||||||
scan,
|
|
||||||
as the result of the appearance of an option (preferably
|
|
||||||
.BR \-\-optionsfrom )
|
|
||||||
to insert additional ``command-line'' arguments
|
|
||||||
into the scan immediately after
|
|
||||||
the option.
|
|
||||||
Typically this would be done to pick up options which are
|
|
||||||
security-sensitive and should not be visible to
|
|
||||||
.IR ps (1)
|
|
||||||
and similar commands,
|
|
||||||
and hence cannot be supplied as part
|
|
||||||
of the actual command line or the environment.
|
|
||||||
.PP
|
|
||||||
.I Optionsfrom
|
|
||||||
reads the additional arguments from the specified
|
|
||||||
.IR filename ,
|
|
||||||
allocates a new argument vector to hold pointers to the existing
|
|
||||||
arguments plus the new ones,
|
|
||||||
and amends
|
|
||||||
.I argc
|
|
||||||
and
|
|
||||||
.I argv
|
|
||||||
(via the pointers
|
|
||||||
.I argcp
|
|
||||||
and
|
|
||||||
.IR argvp ,
|
|
||||||
which must point to the
|
|
||||||
.I argc
|
|
||||||
and
|
|
||||||
.I argv
|
|
||||||
being supplied to
|
|
||||||
.IR getopt_long (3))
|
|
||||||
accordingly.
|
|
||||||
.I Optind
|
|
||||||
must be the index, in the original argument vector,
|
|
||||||
of the next argument.
|
|
||||||
.PP
|
|
||||||
If
|
|
||||||
.I errsto
|
|
||||||
is NULL,
|
|
||||||
.I optionsfrom
|
|
||||||
returns NULL for success and
|
|
||||||
a pointer to a string-literal error message for failure;
|
|
||||||
see DIAGNOSTICS.
|
|
||||||
If
|
|
||||||
.I errsto
|
|
||||||
is non-NULL and an error occurs,
|
|
||||||
.I optionsfrom
|
|
||||||
prints a suitable complaint onto the
|
|
||||||
.I errsto
|
|
||||||
descriptor and invokes
|
|
||||||
.I exit
|
|
||||||
with an exit status of 2;
|
|
||||||
this is a convenience for cases where more sophisticated
|
|
||||||
responses are not required.
|
|
||||||
.PP
|
|
||||||
The text of existing arguments is not disturbed by
|
|
||||||
.IR optionsfrom ,
|
|
||||||
so pointers to them and into them remain valid.
|
|
||||||
.PP
|
|
||||||
The file of additional arguments is an ASCII text file.
|
|
||||||
Lines consisting solely of white space,
|
|
||||||
and lines beginning with
|
|
||||||
.BR # ,
|
|
||||||
are comments and are ignored.
|
|
||||||
Otherwise, a line which does not begin with
|
|
||||||
.BR \-
|
|
||||||
is taken to be a single argument;
|
|
||||||
if it both begins and ends with double-quote ("),
|
|
||||||
those quotes are stripped off (note, no other processing is done within
|
|
||||||
the line!).
|
|
||||||
A line beginning with
|
|
||||||
.B \-
|
|
||||||
is considered to contain multiple arguments separated by white space.
|
|
||||||
.PP
|
|
||||||
Because
|
|
||||||
.I optionsfrom
|
|
||||||
reads its entire file before the
|
|
||||||
.IR getopt_long (3)
|
|
||||||
scan is resumed, an
|
|
||||||
.I optionsfrom
|
|
||||||
file can contain another
|
|
||||||
.B \-\-optionsfrom
|
|
||||||
option.
|
|
||||||
Obviously, infinite loops are possible here.
|
|
||||||
If
|
|
||||||
.I errsto
|
|
||||||
is non-NULL,
|
|
||||||
.I optionsfrom
|
|
||||||
considers it an error to be called more than 100 times.
|
|
||||||
If
|
|
||||||
.I errsto
|
|
||||||
is NULL,
|
|
||||||
loop detection is up to the caller
|
|
||||||
(and the internal loop counter is zeroed out).
|
|
||||||
.SH EXAMPLE
|
|
||||||
A reasonable way to invoke
|
|
||||||
.I optionsfrom
|
|
||||||
would be like so:
|
|
||||||
.PP
|
|
||||||
.nf
|
|
||||||
.ft B
|
|
||||||
#include <getopt.h>
|
|
||||||
|
|
||||||
struct option opts[] = {
|
|
||||||
/* ... */
|
|
||||||
"optionsfrom", 1, NULL, '+',
|
|
||||||
/* ... */
|
|
||||||
};
|
|
||||||
|
|
||||||
int
|
|
||||||
main(argc, argv)
|
|
||||||
int argc;
|
|
||||||
char *argv[];
|
|
||||||
{
|
|
||||||
int opt;
|
|
||||||
extern char *optarg;
|
|
||||||
extern int optind;
|
|
||||||
|
|
||||||
while ((opt = getopt_long(argc, argv, "", opts, NULL)) != EOF)
|
|
||||||
switch (opt) {
|
|
||||||
/* ... */
|
|
||||||
case '+': /* optionsfrom */
|
|
||||||
optionsfrom(optarg, &argc, &argv, optind, stderr);
|
|
||||||
/* does not return on error */
|
|
||||||
break;
|
|
||||||
/* ... */
|
|
||||||
}
|
|
||||||
/* ... */
|
|
||||||
.ft
|
|
||||||
.fi
|
|
||||||
.SH SEE ALSO
|
|
||||||
getopt_long(3)
|
|
||||||
.SH DIAGNOSTICS
|
|
||||||
Errors in
|
|
||||||
.I optionsfrom
|
|
||||||
are:
|
|
||||||
unable to open file;
|
|
||||||
attempt to allocate temporary storage for argument or
|
|
||||||
argument vector failed;
|
|
||||||
read error in file;
|
|
||||||
line too long.
|
|
||||||
.SH HISTORY
|
|
||||||
Written for the FreeS/WAN project by Henry Spencer.
|
|
||||||
.SH BUGS
|
|
||||||
The double-quote convention is rather simplistic.
|
|
||||||
.PP
|
|
||||||
Line length is currently limited to 1023 bytes,
|
|
||||||
and there is no continuation convention.
|
|
||||||
.PP
|
|
||||||
The restriction of error reports to literal strings
|
|
||||||
(so that callers don't need to worry about freeing them or copying them)
|
|
||||||
does limit the precision of error reporting.
|
|
||||||
.PP
|
|
||||||
The error-reporting convention lends itself
|
|
||||||
to slightly obscure code,
|
|
||||||
because many readers will not think of NULL as signifying success.
|
|
||||||
.PP
|
|
||||||
There is a certain element of unwarranted chumminess with
|
|
||||||
the insides of
|
|
||||||
.IR getopt_long (3)
|
|
||||||
here.
|
|
||||||
No non-public interfaces are actually used, but
|
|
||||||
.IR optionsfrom
|
|
||||||
does rely on
|
|
||||||
.IR getopt_long (3)
|
|
||||||
being well-behaved in certain ways that are not actually
|
|
||||||
promised by the specs.
|
|
||||||
@@ -1,299 +0,0 @@
|
|||||||
/*
|
|
||||||
* pick up more options from a file, in the middle of an option scan
|
|
||||||
* Copyright (C) 1998, 1999 Henry Spencer.
|
|
||||||
*
|
|
||||||
* This library is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU Library General Public License as published by
|
|
||||||
* the Free Software Foundation; either version 2 of the License, or (at your
|
|
||||||
* option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
|
|
||||||
*
|
|
||||||
* This library 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 Library General Public
|
|
||||||
* License for more details.
|
|
||||||
*/
|
|
||||||
#include "internal.h"
|
|
||||||
#include "freeswan.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define MAX 100 /* loop-detection limit */
|
|
||||||
|
|
||||||
/* internal work area */
|
|
||||||
struct work {
|
|
||||||
# define LOTS 1024
|
|
||||||
char buf[LOTS];
|
|
||||||
char *line;
|
|
||||||
char *pending;
|
|
||||||
};
|
|
||||||
|
|
||||||
static const char *dowork(const char *, int *, char ***, int);
|
|
||||||
static const char *getanarg(FILE *, struct work *, char **);
|
|
||||||
static char *getline(FILE *, char *, size_t);
|
|
||||||
|
|
||||||
/*
|
|
||||||
- optionsfrom - add some options, taken from a file, to argc/argv
|
|
||||||
* If errsto is non-NULL, does not return in event of error.
|
|
||||||
*/
|
|
||||||
const char * /* NULL for success, else string literal */
|
|
||||||
optionsfrom(filename, argcp, argvp, optind, errsto)
|
|
||||||
const char *filename;
|
|
||||||
int *argcp; /* pointer to argc */
|
|
||||||
char ***argvp; /* pointer to argv */
|
|
||||||
int optind; /* current optind, number of next argument */
|
|
||||||
FILE *errsto; /* where to report errors (NULL means return) */
|
|
||||||
{
|
|
||||||
const char *e;
|
|
||||||
static int nuses = 0;
|
|
||||||
|
|
||||||
if (errsto != NULL) {
|
|
||||||
nuses++;
|
|
||||||
if (nuses >= MAX) {
|
|
||||||
fprintf(errsto,
|
|
||||||
"%s: optionsfrom called %d times, looping?\n",
|
|
||||||
(*argvp)[0], nuses);
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
nuses = 0;
|
|
||||||
|
|
||||||
e = dowork(filename, argcp, argvp, optind);
|
|
||||||
if (e != NULL && errsto != NULL) {
|
|
||||||
fprintf(errsto, "%s: optionsfrom failed: %s\n", (*argvp)[0], e);
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
- dowork - do all the real work of optionsfrom
|
|
||||||
* Does not alter the existing arguments, but does relocate and alter
|
|
||||||
* the argv pointer vector.
|
|
||||||
*/
|
|
||||||
static const char * /* NULL for success, else string literal */
|
|
||||||
dowork(filename, argcp, argvp, optind)
|
|
||||||
const char *filename;
|
|
||||||
int *argcp; /* pointer to argc */
|
|
||||||
char ***argvp; /* pointer to argv */
|
|
||||||
int optind; /* current optind, number of next argument */
|
|
||||||
{
|
|
||||||
char **newargv;
|
|
||||||
char **tmp;
|
|
||||||
int newargc;
|
|
||||||
int next; /* place for next argument */
|
|
||||||
int room; /* how many more new arguments we can hold */
|
|
||||||
# define SOME 10 /* first guess at how many we'll need */
|
|
||||||
FILE *f;
|
|
||||||
int i;
|
|
||||||
const char *p;
|
|
||||||
struct work wa; /* for getanarg() */
|
|
||||||
|
|
||||||
f = fopen(filename, "r");
|
|
||||||
if (f == NULL)
|
|
||||||
return "unable to open file";
|
|
||||||
|
|
||||||
newargc = *argcp + SOME;
|
|
||||||
newargv = malloc((newargc+1) * sizeof(char *));
|
|
||||||
if (newargv == NULL)
|
|
||||||
return "unable to allocate memory";
|
|
||||||
memcpy(newargv, *argvp, optind * sizeof(char *));
|
|
||||||
room = SOME;
|
|
||||||
next = optind;
|
|
||||||
|
|
||||||
newargv[next] = NULL;
|
|
||||||
wa.pending = NULL;
|
|
||||||
while ((p = getanarg(f, &wa, &newargv[next])) == NULL) {
|
|
||||||
if (room == 0) {
|
|
||||||
newargc += SOME;
|
|
||||||
tmp = realloc(newargv, (newargc+1) * sizeof(char *));
|
|
||||||
if (tmp == NULL) {
|
|
||||||
p = "out of space for new argv";
|
|
||||||
break; /* NOTE BREAK OUT */
|
|
||||||
}
|
|
||||||
newargv = tmp;
|
|
||||||
room += SOME;
|
|
||||||
}
|
|
||||||
next++;
|
|
||||||
room--;
|
|
||||||
}
|
|
||||||
if (p != NULL && !feof(f)) { /* error of some kind */
|
|
||||||
for (i = optind+1; i <= next; i++)
|
|
||||||
if (newargv[i] != NULL)
|
|
||||||
free(newargv[i]);
|
|
||||||
free(newargv);
|
|
||||||
fclose(f);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(f);
|
|
||||||
memcpy(newargv + next, *argvp + optind,
|
|
||||||
(*argcp+1-optind) * sizeof(char *));
|
|
||||||
*argcp += next - optind;
|
|
||||||
*argvp = newargv;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
- getanarg - get a malloced argument from the file
|
|
||||||
*/
|
|
||||||
static const char * /* NULL for success, else string literal */
|
|
||||||
getanarg(f, w, linep)
|
|
||||||
FILE *f;
|
|
||||||
struct work *w;
|
|
||||||
char **linep; /* where to store pointer if successful */
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
char *p;
|
|
||||||
char *endp;
|
|
||||||
|
|
||||||
while (w->pending == NULL) { /* no pending line */
|
|
||||||
if ((w->line = getline(f, w->buf, sizeof(w->buf))) == NULL)
|
|
||||||
return "error in line read"; /* caller checks EOF */
|
|
||||||
if (w->line[0] != '#' &&
|
|
||||||
*(w->line + strspn(w->line, " \t")) != '\0')
|
|
||||||
w->pending = w->line;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (w->pending == w->line && w->line[0] != '-') {
|
|
||||||
/* fresh plain line */
|
|
||||||
w->pending = NULL;
|
|
||||||
p = w->line;
|
|
||||||
endp = p + strlen(p);
|
|
||||||
if (*p == '"' && endp > p+1 && *(endp-1) == '"') {
|
|
||||||
p++;
|
|
||||||
endp--;
|
|
||||||
*endp = '\0';
|
|
||||||
}
|
|
||||||
if (w->line == w->buf) {
|
|
||||||
*linep = malloc(endp - p + 1);
|
|
||||||
if (*linep == NULL)
|
|
||||||
return "out of memory for new line";
|
|
||||||
strcpy(*linep, p);
|
|
||||||
} else /* getline already malloced it */
|
|
||||||
*linep = p;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* chip off a piece of a pending line */
|
|
||||||
p = w->pending;
|
|
||||||
p += strspn(p, " \t");
|
|
||||||
endp = p + strcspn(p, " \t");
|
|
||||||
len = endp - p;
|
|
||||||
if (*endp != '\0') {
|
|
||||||
*endp++ = '\0';
|
|
||||||
endp += strspn(endp, " \t");
|
|
||||||
}
|
|
||||||
/* endp now points to next real character, or to line-end NUL */
|
|
||||||
*linep = malloc(len + 1);
|
|
||||||
if (*linep == NULL) {
|
|
||||||
if (w->line != w->buf)
|
|
||||||
free(w->line);
|
|
||||||
return "out of memory for new argument";
|
|
||||||
}
|
|
||||||
strcpy(*linep, p);
|
|
||||||
if (*endp == '\0') {
|
|
||||||
w->pending = NULL;
|
|
||||||
if (w->line != w->buf)
|
|
||||||
free(w->line);
|
|
||||||
} else
|
|
||||||
w->pending = endp;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
- getline - read a line from the file, trim newline off
|
|
||||||
*/
|
|
||||||
static char * /* pointer to line, NULL for eof/error */
|
|
||||||
getline(f, buf, bufsize)
|
|
||||||
FILE *f;
|
|
||||||
char *buf; /* buffer to use, if convenient */
|
|
||||||
size_t bufsize; /* size of buf */
|
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
if (fgets(buf, bufsize, f) == NULL)
|
|
||||||
return NULL;
|
|
||||||
len = strlen(buf);
|
|
||||||
|
|
||||||
if (len < bufsize-1 || buf[bufsize-1] == '\n') {
|
|
||||||
/* it fit */
|
|
||||||
buf[len-1] = '\0';
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* oh crud, buffer overflow */
|
|
||||||
/* for now, to hell with it */
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef TEST
|
|
||||||
|
|
||||||
#include <getopt.h>
|
|
||||||
|
|
||||||
char usage[] = "Usage: tester [--foo] [--bar] [--optionsfrom file] arg ...";
|
|
||||||
struct option opts[] = {
|
|
||||||
"foo", 0, NULL, 'f',
|
|
||||||
"bar", 0, NULL, 'b',
|
|
||||||
"builtin", 0, NULL, 'B',
|
|
||||||
"optionsfrom", 1, NULL, '+',
|
|
||||||
"help", 0, NULL, 'h',
|
|
||||||
"version", 0, NULL, 'v',
|
|
||||||
0, 0, NULL, 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
int
|
|
||||||
main(argc, argv)
|
|
||||||
int argc;
|
|
||||||
char *argv[];
|
|
||||||
{
|
|
||||||
int opt;
|
|
||||||
extern char *optarg;
|
|
||||||
extern int optind;
|
|
||||||
int errflg = 0;
|
|
||||||
const char *p;
|
|
||||||
int i;
|
|
||||||
FILE *errs = NULL;
|
|
||||||
|
|
||||||
while ((opt = getopt_long(argc, argv, "", opts, NULL)) != EOF)
|
|
||||||
switch (opt) {
|
|
||||||
case 'f':
|
|
||||||
case 'b':
|
|
||||||
break;
|
|
||||||
case 'B':
|
|
||||||
errs = stderr;
|
|
||||||
break;
|
|
||||||
case '+': /* optionsfrom */
|
|
||||||
p = optionsfrom(optarg, &argc, &argv, optind, errs);
|
|
||||||
if (p != NULL) {
|
|
||||||
fprintf(stderr, "%s: optionsfrom error: %s\n",
|
|
||||||
argv[0], p);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'h': /* help */
|
|
||||||
printf("%s\n", usage);
|
|
||||||
exit(0);
|
|
||||||
break;
|
|
||||||
case 'v': /* version */
|
|
||||||
printf("1\n");
|
|
||||||
exit(0);
|
|
||||||
break;
|
|
||||||
case '?':
|
|
||||||
default:
|
|
||||||
errflg = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (errflg) {
|
|
||||||
fprintf(stderr, "%s\n", usage);
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 1; i < argc; i++)
|
|
||||||
printf("%d: `%s'\n", i, argv[i]);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* TEST */
|
|
||||||
Reference in New Issue
Block a user