|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* \file cook.c
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file contains the routines that deal with processing quoted strings
|
|
Packit Service |
4684c1 |
* into an internal format.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* @addtogroup autoopts
|
|
Packit Service |
4684c1 |
* @{
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* This file is part of AutoOpts, a companion to AutoGen.
|
|
Packit Service |
4684c1 |
* AutoOpts is free software.
|
|
Packit Service |
4684c1 |
* AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* AutoOpts is available under any one of two licenses. The license
|
|
Packit Service |
4684c1 |
* in use must be one of these two and the choice is under the control
|
|
Packit Service |
4684c1 |
* of the user of the license.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The GNU Lesser General Public License, version 3 or later
|
|
Packit Service |
4684c1 |
* See the files "COPYING.lgplv3" and "COPYING.gplv3"
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The Modified Berkeley Software Distribution License
|
|
Packit Service |
4684c1 |
* See the file "COPYING.mbsd"
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* These files have the following sha256 sums:
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
|
|
Packit Service |
4684c1 |
* 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
|
|
Packit Service |
4684c1 |
* 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*=export_func ao_string_cook_escape_char
|
|
Packit Service |
4684c1 |
* private:
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* what: escape-process a string fragment
|
|
Packit Service |
4684c1 |
* arg: + char const * + pzScan + points to character after the escape +
|
|
Packit Service |
4684c1 |
* arg: + char * + pRes + Where to put the result byte +
|
|
Packit Service |
4684c1 |
* arg: + unsigned int + nl_ch + replacement char if scanned char is \n +
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* ret-type: unsigned int
|
|
Packit Service |
4684c1 |
* ret-desc: The number of bytes consumed processing the escaped character.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* doc:
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function converts "t" into "\t" and all your other favorite
|
|
Packit Service |
4684c1 |
* escapes, including numeric ones: hex and ocatal, too.
|
|
Packit Service |
4684c1 |
* The returned result tells the caller how far to advance the
|
|
Packit Service |
4684c1 |
* scan pointer (passed in). The default is to just pass through the
|
|
Packit Service |
4684c1 |
* escaped character and advance the scan by one.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Some applications need to keep an escaped newline, others need to
|
|
Packit Service |
4684c1 |
* suppress it. This is accomplished by supplying a '\n' replacement
|
|
Packit Service |
4684c1 |
* character that is different from \n, if need be. For example, use
|
|
Packit Service |
4684c1 |
* 0x7F and never emit a 0x7F.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* err: @code{NULL} is returned if the string is mal-formed.
|
|
Packit Service |
4684c1 |
=*/
|
|
Packit Service |
4684c1 |
unsigned int
|
|
Packit Service |
4684c1 |
ao_string_cook_escape_char(char const * pzIn, char * pRes, uint_t nl)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
unsigned int res = 1;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
switch (*pRes = *pzIn++) {
|
|
Packit Service |
4684c1 |
case NUL: /* NUL - end of input string */
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
case '\r':
|
|
Packit Service |
4684c1 |
if (*pzIn != NL)
|
|
Packit Service |
4684c1 |
return 1;
|
|
Packit Service |
4684c1 |
res++;
|
|
Packit Service |
4684c1 |
/* FALLTHROUGH */
|
|
Packit Service |
4684c1 |
case NL: /* NL - emit newline */
|
|
Packit Service |
4684c1 |
*pRes = (char)nl;
|
|
Packit Service |
4684c1 |
return res;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case 'a': *pRes = '\a'; break;
|
|
Packit Service |
4684c1 |
case 'b': *pRes = '\b'; break;
|
|
Packit Service |
4684c1 |
case 'f': *pRes = '\f'; break;
|
|
Packit Service |
4684c1 |
case 'n': *pRes = NL; break;
|
|
Packit Service |
4684c1 |
case 'r': *pRes = '\r'; break;
|
|
Packit Service |
4684c1 |
case 't': *pRes = '\t'; break;
|
|
Packit Service |
4684c1 |
case 'v': *pRes = '\v'; break;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case 'x':
|
|
Packit Service |
4684c1 |
case 'X': /* HEX Escape */
|
|
Packit Service |
4684c1 |
if (IS_HEX_DIGIT_CHAR(*pzIn)) {
|
|
Packit Service |
4684c1 |
char z[4];
|
|
Packit Service |
4684c1 |
unsigned int ct = 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
do {
|
|
Packit Service |
4684c1 |
z[ct] = pzIn[ct];
|
|
Packit Service |
4684c1 |
if (++ct >= 2)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
} while (IS_HEX_DIGIT_CHAR(pzIn[ct]));
|
|
Packit Service |
4684c1 |
z[ct] = NUL;
|
|
Packit Service |
4684c1 |
*pRes = (char)strtoul(z, NULL, 16);
|
|
Packit Service |
4684c1 |
return ct + 1;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case '0': case '1': case '2': case '3':
|
|
Packit Service |
4684c1 |
case '4': case '5': case '6': case '7':
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF the character copied was an octal digit,
|
|
Packit Service |
4684c1 |
* THEN set the output character to an octal value.
|
|
Packit Service |
4684c1 |
* The 3 octal digit result might exceed 0xFF, so check it.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
char z[4];
|
|
Packit Service |
4684c1 |
unsigned long val;
|
|
Packit Service |
4684c1 |
unsigned int ct = 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
z[ct++] = *--pzIn;
|
|
Packit Service |
4684c1 |
while (IS_OCT_DIGIT_CHAR(pzIn[ct])) {
|
|
Packit Service |
4684c1 |
z[ct] = pzIn[ct];
|
|
Packit Service |
4684c1 |
if (++ct >= 3)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
z[ct] = NUL;
|
|
Packit Service |
4684c1 |
val = strtoul(z, NULL, 8);
|
|
Packit Service |
4684c1 |
if (val > 0xFF)
|
|
Packit Service |
4684c1 |
val = 0xFF;
|
|
Packit Service |
4684c1 |
*pRes = (char)val;
|
|
Packit Service |
4684c1 |
return ct;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
default: /* quoted character is result character */;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return res;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* count newlines between start and end
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
static char *
|
|
Packit Service |
4684c1 |
nl_count(char * start, char * end, int * lnct_p)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
while (start < end) {
|
|
Packit Service |
4684c1 |
if (*(start++) == NL)
|
|
Packit Service |
4684c1 |
(*lnct_p)++;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
return end;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* A quoted string has been found.
|
|
Packit Service |
4684c1 |
* Find the end of it and compress any escape sequences.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
static bool
|
|
Packit Service |
4684c1 |
contiguous_quote(char ** pps, char * pq, int * lnct_p)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
char * ps = *pps + 1;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (;;) {
|
|
Packit Service |
4684c1 |
while (IS_WHITESPACE_CHAR(*ps))
|
|
Packit Service |
4684c1 |
if (*(ps++) == NL)
|
|
Packit Service |
4684c1 |
(*lnct_p)++;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF the next character is a quote character,
|
|
Packit Service |
4684c1 |
* THEN we will concatenate the strings.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
switch (*ps) {
|
|
Packit Service |
4684c1 |
case '"':
|
|
Packit Service |
4684c1 |
case '\'':
|
|
Packit Service |
4684c1 |
*pq = *(ps++); /* assign new quote character and return */
|
|
Packit Service |
4684c1 |
*pps = ps;
|
|
Packit Service |
4684c1 |
return true;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case '/':
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Allow for a comment embedded in the concatenated string.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
switch (ps[1]) {
|
|
Packit Service |
4684c1 |
default:
|
|
Packit Service |
4684c1 |
goto fail_return;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case '/':
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Skip to end of line
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
ps = strchr(ps, NL);
|
|
Packit Service |
4684c1 |
if (ps == NULL)
|
|
Packit Service |
4684c1 |
goto fail_return;
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case '*':
|
|
Packit Service |
4684c1 |
ps = nl_count(ps + 2, strstr(ps + 2, "*/"), lnct_p);
|
|
Packit Service |
4684c1 |
if (ps == NULL)
|
|
Packit Service |
4684c1 |
goto fail_return;
|
|
Packit Service |
4684c1 |
ps += 2;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
continue;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
default:
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* The next non-whitespace character is not a quote.
|
|
Packit Service |
4684c1 |
* The series of quoted strings has come to an end.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
*pps = ps;
|
|
Packit Service |
4684c1 |
return false;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fail_return:
|
|
Packit Service |
4684c1 |
*pps = NULL;
|
|
Packit Service |
4684c1 |
return false;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*=export_func ao_string_cook
|
|
Packit Service |
4684c1 |
* private:
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* what: concatenate and escape-process strings
|
|
Packit Service |
4684c1 |
* arg: + char * + pzScan + The *MODIFIABLE* input buffer +
|
|
Packit Service |
4684c1 |
* arg: + int * + lnct_p + The (possibly NULL) pointer to a line count +
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* ret-type: char *
|
|
Packit Service |
4684c1 |
* ret-desc: The address of the text following the processed strings.
|
|
Packit Service |
4684c1 |
* The return value is NULL if the strings are ill-formed.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* doc:
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* A series of one or more quoted strings are concatenated together.
|
|
Packit Service |
4684c1 |
* If they are quoted with double quotes (@code{"}), then backslash
|
|
Packit Service |
4684c1 |
* escapes are processed per the C programming language. If they are
|
|
Packit Service |
4684c1 |
* single quote strings, then the backslashes are honored only when they
|
|
Packit Service |
4684c1 |
* precede another backslash or a single quote character.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* err: @code{NULL} is returned if the string(s) is/are mal-formed.
|
|
Packit Service |
4684c1 |
=*/
|
|
Packit Service |
4684c1 |
char *
|
|
Packit Service |
4684c1 |
ao_string_cook(char * pzScan, int * lnct_p)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int l = 0;
|
|
Packit Service |
4684c1 |
char q = *pzScan;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* It is a quoted string. Process the escape sequence characters
|
|
Packit Service |
4684c1 |
* (in the set "abfnrtv") and make sure we find a closing quote.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
char * pzD = pzScan++;
|
|
Packit Service |
4684c1 |
char * pzS = pzScan;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (lnct_p == NULL)
|
|
Packit Service |
4684c1 |
lnct_p = &l;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (;;) {
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF the next character is the quote character, THEN we may end the
|
|
Packit Service |
4684c1 |
* string. We end it unless the next non-blank character *after* the
|
|
Packit Service |
4684c1 |
* string happens to also be a quote. If it is, then we will change
|
|
Packit Service |
4684c1 |
* our quote character to the new quote character and continue
|
|
Packit Service |
4684c1 |
* condensing text.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
while (*pzS == q) {
|
|
Packit Service |
4684c1 |
*pzD = NUL; /* This is probably the end of the line */
|
|
Packit Service |
4684c1 |
if (! contiguous_quote(&pzS, &q, lnct_p))
|
|
Packit Service |
4684c1 |
return pzS;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* We are inside a quoted string. Copy text.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
switch (*(pzD++) = *(pzS++)) {
|
|
Packit Service |
4684c1 |
case NUL:
|
|
Packit Service |
4684c1 |
return NULL;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case NL:
|
|
Packit Service |
4684c1 |
(*lnct_p)++;
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
case '\\':
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* IF we are escaping a new line,
|
|
Packit Service |
4684c1 |
* THEN drop both the escape and the newline from
|
|
Packit Service |
4684c1 |
* the result string.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if (*pzS == NL) {
|
|
Packit Service |
4684c1 |
pzS++;
|
|
Packit Service |
4684c1 |
pzD--;
|
|
Packit Service |
4684c1 |
(*lnct_p)++;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* ELSE IF the quote character is '"' or '`',
|
|
Packit Service |
4684c1 |
* THEN we do the full escape character processing
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
else if (q != '\'') {
|
|
Packit Service |
4684c1 |
unsigned int ct;
|
|
Packit Service |
4684c1 |
ct = ao_string_cook_escape_char(pzS, pzD-1, (uint_t)NL);
|
|
Packit Service |
4684c1 |
if (ct == 0)
|
|
Packit Service |
4684c1 |
return NULL;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
pzS += ct;
|
|
Packit Service |
4684c1 |
} /* if (q != '\'') */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* OTHERWISE, we only process "\\", "\'" and "\#" sequences.
|
|
Packit Service |
4684c1 |
* The latter only to easily hide preprocessing directives.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
else switch (*pzS) {
|
|
Packit Service |
4684c1 |
case '\\':
|
|
Packit Service |
4684c1 |
case '\'':
|
|
Packit Service |
4684c1 |
case '#':
|
|
Packit Service |
4684c1 |
pzD[-1] = *pzS++;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
} /* switch (*(pzD++) = *(pzS++)) */
|
|
Packit Service |
4684c1 |
} /* for (;;) */
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/** @}
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Local Variables:
|
|
Packit Service |
4684c1 |
* mode: C
|
|
Packit Service |
4684c1 |
* c-file-style: "stroustrup"
|
|
Packit Service |
4684c1 |
* indent-tabs-mode: nil
|
|
Packit Service |
4684c1 |
* End:
|
|
Packit Service |
4684c1 |
* end of autoopts/cook.c */
|