replace incompatible awk with C

This commit is contained in:
Lev Walkin 2017-11-05 20:32:15 -08:00
parent 3f52df00df
commit c9b2f28b98
4 changed files with 80 additions and 7 deletions

3
.gitignore vendored
View File

@ -47,6 +47,9 @@ stamp-h*
/tests/tests-skeletons/check-*
!/tests/tests-skeletons/check-*.c
# /tests/tests-randomized
/tests/tests-randomized/test-param-helper
# /doc/docsrc
doc/docsrc/*.aux
doc/docsrc/*.dvi

View File

@ -1,5 +1,6 @@
@CODE_COVERAGE_RULES@
check_PROGRAMS = test-param-helper
dist_check_SCRIPTS = check-bundles.sh
#Filter out the coverage options from CFLAGS as we don't need
@ -19,6 +20,7 @@ TESTS_ENVIRONMENT= \
FUZZ_TIME="${FUZZ_TIME}" \
ASAN_ENV_FLAGS="@ASAN_ENV_FLAGS@" \
srcdir=${srcdir} \
builddir=${builddir} \
abs_top_srcdir=${abs_top_srcdir} \
abs_top_builddir=${abs_top_builddir} \
${srcdir}/check-bundles.sh

View File

@ -29,6 +29,7 @@ usage() {
RNDTEMP="${RNDTEMP:-.tmp.random}"
srcdir="${srcdir:-.}"
builddir="${builddir:-.}"
abs_top_srcdir="${abs_top_srcdir:-`pwd`/../../}"
abs_top_builddir="${abs_top_builddir:-`pwd`/../../}"
MAKE="${MAKE:-make}"
@ -140,13 +141,7 @@ get_param() {
default="$2"
asn="$3"
if nawk '' >/dev/null 2>&1 ; then
AWK=nawk
else
AWK=awk
fi
echo "$asn" | ${AWK} "BEGIN{FS=\"[^${param}=0-9]+\"};/$param=/{for(i=1;i<=NF;i++)if(substr(\$i,0,length(\"${param}=\"))==\"${param}=\")PARAM=substr(\$i,length(\"${param}=\")+1)}END{if(PARAM)print PARAM;else print \"${default}\";}"
"${builddir}/test-param-helper" "${param}" "${default}" "${asn}"
}
# compile_and_test "<text>" "<where found>"

View File

@ -0,0 +1,73 @@
/*
* Since working awk is not present on every supported platform
* (notably Solaris), and nawk is not the same on Solaris and Linux,
* this program is a replacement for it to extract test parameter from the.
* string specified in the command line.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
static void
usage(const char *progname) {
fprintf(stderr, "Search PARAM=VALUE pattern in the given string\n");
fprintf(stderr, "Usage: %s <parameter-name> <default-value> <string>\n",
progname);
}
static const char *
search(const char *param, const char *haystack) {
const char *p = strstr(haystack, param);
if(p && p[strlen(param)] == '=') {
const char *param_begin = &p[strlen(param) + 1];
const char *param_end = param_begin;
for(param_end = param_begin; param_end; param_end++) {
switch(*param_end) {
case '0' ... '9':
continue;
default:
break;
}
break;
}
static char static_buf[64];
if((param_end - param_begin) <= 0) {
fprintf(stderr, "Parameter %s is malformed after '='\n", param);
return NULL;
}
if((param_end - param_begin) >= (ssize_t)sizeof(static_buf)) {
fprintf(stderr, "Parameter %s value exceeds buffer size %zu\n",
param, sizeof(static_buf));
return NULL;
}
memcpy(static_buf, param_begin, param_end - param_begin);
static_buf[param_end - param_begin] = '\0';
return static_buf;
} else if(p) {
fprintf(stderr, "Parameter %s should contain '='\n", param);
return NULL;
}
return NULL;
}
int
main(int ac, char **av) {
if(ac != 4) {
usage(av[0]);
exit(EX_USAGE);
}
const char *value = search(av[1], av[3]);
if(value) {
printf("%s\n", value);
} else {
printf("%s\n", av[2]);
}
}