summaryrefslogtreecommitdiff
path: root/sntp/libopts/load.c
diff options
context:
space:
mode:
Diffstat (limited to 'sntp/libopts/load.c')
-rw-r--r--sntp/libopts/load.c229
1 files changed, 92 insertions, 137 deletions
diff --git a/sntp/libopts/load.c b/sntp/libopts/load.c
index eeeb125cc51e1..024dce9301a87 100644
--- a/sntp/libopts/load.c
+++ b/sntp/libopts/load.c
@@ -1,76 +1,44 @@
-/*
- * $Id: load.c,v 4.20 2007/02/04 22:17:39 bkorb Exp $
- * Time-stamp: "2007-02-04 11:54:57 bkorb"
+/**
+ * \file load.c
+ * Time-stamp: "2010-12-18 11:46:07 bkorb"
*
* This file contains the routines that deal with processing text strings
* for options, either from a NUL-terminated string passed in or from an
* rc/ini file.
- */
-
-/*
- * Automated Options copyright 1992-2007 Bruce Korb
- *
- * Automated Options is free software.
- * You may redistribute it and/or modify it under the terms of the
- * GNU General Public License, as published by the Free Software
- * Foundation; either version 2, or (at your option) any later version.
- *
- * Automated Options 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.
*
- * You should have received a copy of the GNU General Public License
- * along with Automated Options. See the file "COPYING". If not,
- * write to: The Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
+ * This file is part of AutoOpts, a companion to AutoGen.
+ * AutoOpts is free software.
+ * AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
*
- * As a special exception, Bruce Korb gives permission for additional
- * uses of the text contained in his release of AutoOpts.
+ * AutoOpts is available under any one of two licenses. The license
+ * in use must be one of these two and the choice is under the control
+ * of the user of the license.
*
- * The exception is that, if you link the AutoOpts library with other
- * files to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the AutoOpts library code into it.
+ * The GNU Lesser General Public License, version 3 or later
+ * See the files "COPYING.lgplv3" and "COPYING.gplv3"
*
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
+ * The Modified Berkeley Software Distribution License
+ * See the file "COPYING.mbsd"
*
- * This exception applies only to the code released by Bruce Korb under
- * the name AutoOpts. If you copy code from other sources under the
- * General Public License into a copy of AutoOpts, as the General Public
- * License permits, the exception does not apply to the code that you add
- * in this way. To avoid misleading anyone as to the status of such
- * modified files, you must delete this exception notice from them.
+ * These files have the following md5sums:
*
- * If you write modifications of your own for AutoOpts, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.
+ * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
+ * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
+ * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
*/
-tOptionLoadMode option_load_mode = OPTION_LOAD_UNCOOKED;
-
/* = = = START-STATIC-FORWARD = = = */
-/* static forward declarations maintained by :mkfwd */
static ag_bool
-insertProgramPath(
- char* pzBuf,
- int bufSize,
- tCC* pzName,
- tCC* pzProgPath );
+insertProgramPath(char * pzBuf, int bufSize, char const * pzName,
+ char const * pzProgPath);
static ag_bool
-insertEnvVal(
- char* pzBuf,
- int bufSize,
- tCC* pzName,
- tCC* pzProgPath );
+insertEnvVal(char * pzBuf, int bufSize, char const * pzName,
+ char const * pzProgPath);
static char*
-assembleArgValue( char* pzTxt, tOptionLoadMode mode );
+assembleArgValue(char* pzTxt, tOptionLoadMode mode);
/* = = = END-STATIC-FORWARD = = = */
/*=export_func optionMakePath
@@ -86,13 +54,13 @@ assembleArgValue( char* pzTxt, tOptionLoadMode mode );
* ret-desc: AG_TRUE if the name was handled, otherwise AG_FALSE.
* If the name does not start with ``$'', then it is handled
* simply by copying the input name to the output buffer and
- * resolving the name with either @code{canonicalize_file_name(3GLIBC)}
- * or @code{realpath(3C)}.
+ * resolving the name with either
+ * @code{canonicalize_file_name(3GLIBC)} or @code{realpath(3C)}.
*
* doc:
*
- * This routine will copy the @code{pzName} input name into the @code{pzBuf}
- * output buffer, carefully not exceeding @code{bufSize} bytes. If the
+ * This routine will copy the @code{pzName} input name into the
+ * @code{pzBuf} output buffer, not exceeding @code{bufSize} bytes. If the
* first character of the input name is a @code{'$'} character, then there
* is special handling:
* @*
@@ -125,30 +93,19 @@ assembleArgValue( char* pzTxt, tOptionLoadMode mode );
* errors (cannot resolve the resulting path).
=*/
ag_bool
-optionMakePath(
- char* pzBuf,
- int bufSize,
- tCC* pzName,
- tCC* pzProgPath )
+optionMakePath(char * pzBuf, int bufSize, char const * pzName,
+ char const * pzProgPath)
{
- size_t name_len = strlen( pzName );
-
-# ifndef PKGDATADIR
-# define PKGDATADIR ""
-# endif
-
- tSCC pkgdatadir[] = PKGDATADIR;
+ size_t name_len = strlen(pzName);
- ag_bool res = AG_TRUE;
-
- if (bufSize <= name_len)
+ if ((bufSize <= name_len) || (name_len == 0))
return AG_FALSE;
/*
* IF not an environment variable, just copy the data
*/
if (*pzName != '$') {
- tCC* pzS = pzName;
+ char const* pzS = pzName;
char* pzD = pzBuf;
int ct = bufSize;
@@ -170,46 +127,52 @@ optionMakePath(
return AG_FALSE;
case '$':
- res = insertProgramPath( pzBuf, bufSize, pzName, pzProgPath );
+ if (! insertProgramPath(pzBuf, bufSize, pzName, pzProgPath))
+ return AG_FALSE;
break;
case '@':
- if (pkgdatadir[0] == NUL)
+ if (program_pkgdatadir[0] == NUL)
return AG_FALSE;
- if (name_len + sizeof (pkgdatadir) > bufSize)
+ if (snprintf(pzBuf, bufSize, "%s%s", program_pkgdatadir, pzName + 2)
+ >= bufSize)
return AG_FALSE;
-
- strcpy(pzBuf, pkgdatadir);
- strcpy(pzBuf + sizeof(pkgdatadir) - 1, pzName + 2);
break;
default:
- res = insertEnvVal( pzBuf, bufSize, pzName, pzProgPath );
+ if (! insertEnvVal(pzBuf, bufSize, pzName, pzProgPath))
+ return AG_FALSE;
}
- if (! res)
- return AG_FALSE;
-
#if defined(HAVE_CANONICALIZE_FILE_NAME)
{
- char* pz = canonicalize_file_name(pzBuf);
+ char * pz = canonicalize_file_name(pzBuf);
if (pz == NULL)
return AG_FALSE;
- if (strlen(pz) < bufSize)
- strcpy(pzBuf, pz);
+
+ name_len = strlen(pz);
+ if (name_len >= bufSize) {
+ free(pz);
+ return AG_FALSE;
+ }
+
+ memcpy(pzBuf, pz, name_len + 1);
free(pz);
}
#elif defined(HAVE_REALPATH)
{
- char z[ PATH_MAX+1 ];
+ char z[PATH_MAX+1];
+
+ if (realpath(pzBuf, z) == NULL)
+ return AG_FALSE;
- if (realpath( pzBuf, z ) == NULL)
+ name_len = strlen(z);
+ if (name_len >= bufSize)
return AG_FALSE;
- if (strlen(z) < bufSize)
- strcpy( pzBuf, z );
+ memcpy(pzBuf, z, name_len + 1);
}
#endif
@@ -218,14 +181,11 @@ optionMakePath(
static ag_bool
-insertProgramPath(
- char* pzBuf,
- int bufSize,
- tCC* pzName,
- tCC* pzProgPath )
+insertProgramPath(char * pzBuf, int bufSize, char const * pzName,
+ char const * pzProgPath)
{
- tCC* pzPath;
- tCC* pz;
+ char const* pzPath;
+ char const* pz;
int skip = 2;
switch (pzName[2]) {
@@ -242,16 +202,16 @@ insertProgramPath(
* If it is, we're done. Otherwise, we have to hunt
* for the program using "pathfind".
*/
- if (strchr( pzProgPath, DIRCH ) != NULL)
+ if (strchr(pzProgPath, DIRCH) != NULL)
pzPath = pzProgPath;
else {
- pzPath = pathfind( getenv( "PATH" ), (char*)pzProgPath, "rx" );
+ pzPath = pathfind(getenv("PATH"), (char*)pzProgPath, "rx");
if (pzPath == NULL)
return AG_FALSE;
}
- pz = strrchr( pzPath, DIRCH );
+ pz = strrchr(pzPath, DIRCH);
/*
* IF we cannot find a directory name separator,
@@ -269,31 +229,28 @@ insertProgramPath(
if ((pz - pzPath)+1 + strlen(pzName) >= bufSize)
return AG_FALSE;
- memcpy( pzBuf, pzPath, (size_t)((pz - pzPath)+1) );
- strcpy( pzBuf + (pz - pzPath) + 1, pzName );
+ memcpy(pzBuf, pzPath, (size_t)((pz - pzPath)+1));
+ strcpy(pzBuf + (pz - pzPath) + 1, pzName);
/*
* If the "pzPath" path was gotten from "pathfind()", then it was
* allocated and we need to deallocate it.
*/
if (pzPath != pzProgPath)
- free( (void*)pzPath );
+ AGFREE(pzPath);
return AG_TRUE;
}
static ag_bool
-insertEnvVal(
- char* pzBuf,
- int bufSize,
- tCC* pzName,
- tCC* pzProgPath )
+insertEnvVal(char * pzBuf, int bufSize, char const * pzName,
+ char const * pzProgPath)
{
char* pzDir = pzBuf;
for (;;) {
int ch = (int)*++pzName;
- if (! ISNAMECHAR( ch ))
+ if (! IS_VALUE_NAME_CHAR(ch))
break;
*(pzDir++) = (char)ch;
}
@@ -303,7 +260,7 @@ insertEnvVal(
*pzDir = NUL;
- pzDir = getenv( pzBuf );
+ pzDir = getenv(pzBuf);
/*
* Environment value not found -- skip the home list entry
@@ -311,32 +268,32 @@ insertEnvVal(
if (pzDir == NULL)
return AG_FALSE;
- if (strlen( pzDir ) + 1 + strlen( pzName ) >= bufSize)
+ if (strlen(pzDir) + 1 + strlen(pzName) >= bufSize)
return AG_FALSE;
- sprintf( pzBuf, "%s%s", pzDir, pzName );
+ sprintf(pzBuf, "%s%s", pzDir, pzName);
return AG_TRUE;
}
LOCAL void
-mungeString( char* pzTxt, tOptionLoadMode mode )
+mungeString(char* pzTxt, tOptionLoadMode mode)
{
char* pzE;
if (mode == OPTION_LOAD_KEEP)
return;
- if (isspace( (int)*pzTxt )) {
+ if (IS_WHITESPACE_CHAR(*pzTxt)) {
char* pzS = pzTxt;
char* pzD = pzTxt;
- while (isspace( (int)*++pzS )) ;
+ while (IS_WHITESPACE_CHAR(*++pzS)) ;
while ((*(pzD++) = *(pzS++)) != NUL) ;
pzE = pzD-1;
} else
- pzE = pzTxt + strlen( pzTxt );
+ pzE = pzTxt + strlen(pzTxt);
- while ((pzE > pzTxt) && isspace( (int)pzE[-1] )) pzE--;
+ while ((pzE > pzTxt) && IS_WHITESPACE_CHAR(pzE[-1])) pzE--;
*pzE = NUL;
if (mode == OPTION_LOAD_UNCOOKED)
@@ -354,15 +311,15 @@ mungeString( char* pzTxt, tOptionLoadMode mode )
case '\'': break;
}
- (void)ao_string_cook( pzTxt, NULL );
+ (void)ao_string_cook(pzTxt, NULL);
}
static char*
-assembleArgValue( char* pzTxt, tOptionLoadMode mode )
+assembleArgValue(char* pzTxt, tOptionLoadMode mode)
{
- tSCC zBrk[] = " \t:=";
- char* pzEnd = strpbrk( pzTxt, zBrk );
+ static char const zBrk[] = " \t\n:=";
+ char* pzEnd = strpbrk(pzTxt, zBrk);
int space_break;
/*
@@ -386,11 +343,11 @@ assembleArgValue( char* pzTxt, tOptionLoadMode mode )
* because we'll have to skip over an immediately following ':' or '='
* (and the white space following *that*).
*/
- space_break = isspace((int)*pzEnd);
+ space_break = IS_WHITESPACE_CHAR(*pzEnd);
*(pzEnd++) = NUL;
- while (isspace((int)*pzEnd)) pzEnd++;
+ while (IS_WHITESPACE_CHAR(*pzEnd)) pzEnd++;
if (space_break && ((*pzEnd == ':') || (*pzEnd == '=')))
- while (isspace((int)*++pzEnd)) ;
+ while (IS_WHITESPACE_CHAR(*++pzEnd)) ;
return pzEnd;
}
@@ -410,12 +367,12 @@ loadOptionLine(
tDirection direction,
tOptionLoadMode load_mode )
{
- while (isspace( (int)*pzLine )) pzLine++;
+ while (IS_WHITESPACE_CHAR(*pzLine)) pzLine++;
{
- char* pzArg = assembleArgValue( pzLine, load_mode );
+ char* pzArg = assembleArgValue(pzLine, load_mode);
- if (! SUCCESSFUL( longOptionFind( pOpts, pzLine, pOS )))
+ if (! SUCCESSFUL(longOptionFind(pOpts, pzLine, pOS)))
return;
if (pOS->flags & OPTST_NO_INIT)
return;
@@ -496,7 +453,7 @@ loadOptionLine(
if (*pOS->pzOptArg == NUL)
pOS->pzOptArg = NULL;
else {
- AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
+ AGDUPSTR(pOS->pzOptArg, pOS->pzOptArg, "option argument");
pOS->flags |= OPTST_ALLOC_ARG;
}
@@ -504,7 +461,7 @@ loadOptionLine(
if (*pOS->pzOptArg == NUL)
pOS->pzOptArg = zNil;
else {
- AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
+ AGDUPSTR(pOS->pzOptArg, pOS->pzOptArg, "option argument");
pOS->flags |= OPTST_ALLOC_ARG;
}
}
@@ -512,7 +469,7 @@ loadOptionLine(
{
tOptionLoadMode sv = option_load_mode;
option_load_mode = load_mode;
- handleOption( pOpts, pOS );
+ handle_opt(pOpts, pOS);
option_load_mode = sv;
}
}
@@ -544,15 +501,13 @@ loadOptionLine(
* will cause a warning to print, but the function should return.
=*/
void
-optionLoadLine(
- tOptions* pOpts,
- tCC* pzLine )
+optionLoadLine(tOptions * pOpts, char const * pzLine)
{
tOptState st = OPTSTATE_INITIALIZER(SET);
char* pz;
- AGDUPSTR( pz, pzLine, "user option line" );
- loadOptionLine( pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED );
- AGFREE( pz );
+ AGDUPSTR(pz, pzLine, "user option line");
+ loadOptionLine(pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED);
+ AGFREE(pz);
}
/*
* Local Variables: