aboutsummaryrefslogtreecommitdiff
path: root/sntp/libopts/cook.c
diff options
context:
space:
mode:
Diffstat (limited to 'sntp/libopts/cook.c')
-rw-r--r--sntp/libopts/cook.c297
1 files changed, 127 insertions, 170 deletions
diff --git a/sntp/libopts/cook.c b/sntp/libopts/cook.c
index bebd123fca5b..811ce593080a 100644
--- a/sntp/libopts/cook.c
+++ b/sntp/libopts/cook.c
@@ -1,57 +1,35 @@
-
-/*
- * $Id: cook.c,v 4.10 2007/02/04 17:44:12 bkorb Exp $
- * Time-stamp: "2006-09-24 15:21:02 bkorb"
+/**
+ * \file cook.c
+ *
+ * Time-stamp: "2011-03-12 15:05:26 bkorb"
*
* This file contains the routines that deal with processing quoted strings
* into an internal format.
- */
-
-/*
- * 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
*/
/* = = = START-STATIC-FORWARD = = = */
-/* static forward declarations maintained by :mkfwd */
+static ag_bool
+contiguous_quote(char ** pps, char * pq, int * lnct_p);
/* = = = END-STATIC-FORWARD = = = */
/*=export_func ao_string_cook_escape_char
@@ -105,96 +83,132 @@ ao_string_cook_escape_char( char const* pzIn, char* pRes, u_int nl )
case 't': *pRes = '\t'; break;
case 'v': *pRes = '\v'; break;
- case 'x': /* HEX Escape */
- if (isxdigit( (int)*pzIn )) {
- unsigned int val;
- unsigned char ch = *pzIn++;
-
- if ((ch >= 'A') && (ch <= 'F'))
- val = 10 + (ch - 'A');
- else if ((ch >= 'a') && (ch <= 'f'))
- val = 10 + (ch - 'a');
- else val = ch - '0';
-
- ch = *pzIn;
+ case 'x':
+ case 'X': /* HEX Escape */
+ if (IS_HEX_DIGIT_CHAR(*pzIn)) {
+ char z[4], *pz = z;
- if (! isxdigit( ch )) {
- *pRes = val;
- res = 2;
- break;
- }
- val <<= 4;
- if ((ch >= 'A') && (ch <= 'F'))
- val += 10 + (ch - 'A');
- else if ((ch >= 'a') && (ch <= 'f'))
- val += 10 + (ch - 'a');
- else val += ch - '0';
-
- res = 3;
- *pRes = val;
+ do *(pz++) = *(pzIn++);
+ while (IS_HEX_DIGIT_CHAR(*pzIn) && (pz < z + 2));
+ *pz = NUL;
+ *pRes = (unsigned char)strtoul(z, NULL, 16);
+ res += pz - z;
}
break;
- default:
+ case '0': case '1': case '2': case '3':
+ case '4': case '5': case '6': case '7':
+ {
/*
* IF the character copied was an octal digit,
* THEN set the output character to an octal value
*/
- if (isdigit( (int)*pRes ) && (*pRes < '8')) {
- unsigned int val = *pRes - '0';
- unsigned char ch = *pzIn++;
+ char z[4], *pz = z + 1;
+ unsigned long val;
+ z[0] = *pRes;
+
+ while (IS_OCT_DIGIT_CHAR(*pzIn) && (pz < z + 3))
+ *(pz++) = *(pzIn++);
+ *pz = NUL;
+ val = strtoul(z, NULL, 8);
+ if (val > 0xFF)
+ val = 0xFF;
+ *pRes = (unsigned char)val;
+ res = pz - z;
+ break;
+ }
- /*
- * IF the second character is *not* an octal digit,
- * THEN save the value and bail
- */
- if ((ch < '0') || (ch > '7')) {
- *pRes = val;
- break;
- }
+ default: ;
+ }
- val = (val<<3) + (ch - '0');
- ch = *pzIn;
- res = 2;
+ return res;
+}
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ *
+ * A quoted string has been found.
+ * Find the end of it and compress any escape sequences.
+ */
+static ag_bool
+contiguous_quote(char ** pps, char * pq, int * lnct_p)
+{
+ char * ps = *pps + 1;
+ for (;;) {
+ while (IS_WHITESPACE_CHAR(*ps))
+ if (*(ps++) == '\n')
+ (*lnct_p)++;
+
+ /*
+ * IF the next character is a quote character,
+ * THEN we will concatenate the strings.
+ */
+ switch (*ps) {
+ case '"':
+ case '\'':
+ *pq = *(ps++); /* assign new quote character and return */
+ *pps = ps;
+ return AG_TRUE;
+
+ case '/':
/*
- * IF the THIRD character is *not* an octal digit,
- * THEN save the value and bail
+ * Allow for a comment embedded in the concatenated string.
*/
- if ((ch < '0') || (ch > '7')) {
- *pRes = val;
+ switch (ps[1]) {
+ default:
+ *pps = NULL;
+ return AG_FALSE;
+
+ case '/':
+ /*
+ * Skip to end of line
+ */
+ ps = strchr(ps, '\n');
+ if (ps == NULL) {
+ *pps = NULL;
+ return AG_FALSE;
+ }
break;
+
+ case '*':
+ {
+ char* p = strstr( ps+2, "*/" );
+ /*
+ * Skip to terminating star slash
+ */
+ if (p == NULL) {
+ *pps = NULL;
+ return AG_FALSE;
+ }
+
+ while (ps < p) {
+ if (*(ps++) == '\n')
+ (*lnct_p)++;
+ }
+
+ ps = p + 2;
+ }
}
+ continue;
+ default:
/*
- * IF the new value would not be too large,
- * THEN add on the third and last character value
+ * The next non-whitespace character is not a quote.
+ * The series of quoted strings has come to an end.
*/
- if ((val<<3) < 0xFF) {
- val = (val<<3) + (ch - '0');
- res = 3;
- }
-
- *pRes = val;
- break;
+ *pps = ps;
+ return AG_FALSE;
}
}
-
- return res;
}
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- *
- * A quoted string has been found.
- * Find the end of it and compress any escape sequences.
- */
/*=export_func ao_string_cook
* private:
*
* what: concatenate and escape-process strings
- * arg: + char* + pzScan + The *MODIFIABLE* input buffer +
- * arg: + int* + pLineCt + The (possibly NULL) pointer to a line count +
+ * arg: + char* + pzScan + The *MODIFIABLE* input buffer +
+ * arg: + int* + lnct_p + The (possibly NULL) pointer to a line count +
*
* ret-type: char*
* ret-desc: The address of the text following the processed strings.
@@ -210,8 +224,8 @@ ao_string_cook_escape_char( char const* pzIn, char* pRes, u_int nl )
*
* err: @code{NULL} is returned if the string(s) is/are mal-formed.
=*/
-char*
-ao_string_cook( char* pzScan, int* pLineCt )
+char *
+ao_string_cook(char * pzScan, int * lnct_p)
{
int l = 0;
char q = *pzScan;
@@ -223,8 +237,8 @@ ao_string_cook( char* pzScan, int* pLineCt )
char* pzD = pzScan++;
char* pzS = pzScan;
- if (pLineCt == NULL)
- pLineCt = &l;
+ if (lnct_p == NULL)
+ lnct_p = &l;
for (;;) {
/*
@@ -236,65 +250,8 @@ ao_string_cook( char* pzScan, int* pLineCt )
*/
while (*pzS == q) {
*pzD = NUL; /* This is probably the end of the line */
- pzS++;
-
- scan_for_quote:
- while (isspace((int)*pzS))
- if (*(pzS++) == '\n')
- (*pLineCt)++;
-
- /*
- * IF the next character is a quote character,
- * THEN we will concatenate the strings.
- */
- switch (*pzS) {
- case '"':
- case '\'':
- break;
-
- case '/':
- /*
- * Allow for a comment embedded in the concatenated string.
- */
- switch (pzS[1]) {
- default: return NULL;
- case '/':
- /*
- * Skip to end of line
- */
- pzS = strchr( pzS, '\n' );
- if (pzS == NULL)
- return NULL;
- (*pLineCt)++;
- break;
-
- case '*':
- {
- char* p = strstr( pzS+2, "*/" );
- /*
- * Skip to terminating star slash
- */
- if (p == NULL)
- return NULL;
- while (pzS < p) {
- if (*(pzS++) == '\n')
- (*pLineCt)++;
- }
-
- pzS = p + 2;
- }
- }
- goto scan_for_quote;
-
- default:
- /*
- * The next non-whitespace character is not a quote.
- * The series of quoted strings has come to an end.
- */
+ if (! contiguous_quote(&pzS, &q, lnct_p))
return pzS;
- }
-
- q = *(pzS++); /* assign new quote character and advance scan */
}
/*
@@ -305,7 +262,7 @@ ao_string_cook( char* pzScan, int* pLineCt )
return NULL;
case '\n':
- (*pLineCt)++;
+ (*lnct_p)++;
break;
case '\\':
@@ -317,7 +274,7 @@ ao_string_cook( char* pzScan, int* pLineCt )
if (*pzS == '\n') {
pzS++;
pzD--;
- (*pLineCt)++;
+ (*lnct_p)++;
}
/*