awk: style cleanup. A lot of rw data moved to ro

(still has quite a lot of statics etc...).
getopt32-ification.
This commit is contained in:
Denis Vlasenko 2007-01-01 23:51:30 +00:00
parent b2abef3e54
commit f782f52c8c

View File

@ -77,10 +77,10 @@ typedef struct hash_item_s {
} hash_item;
typedef struct xhash_s {
unsigned int nel; /* num of elements */
unsigned int csize; /* current hash size */
unsigned int nprime; /* next hash size in PRIMES[] */
unsigned int glen; /* summary length of item names */
unsigned nel; /* num of elements */
unsigned csize; /* current hash size */
unsigned nprime; /* next hash size in PRIMES[] */
unsigned glen; /* summary length of item names */
struct hash_item_s **items;
} xhash;
@ -260,7 +260,7 @@ enum {
#define OC_B OC_BUILTIN
static char * const tokenlist =
static const char tokenlist[] =
"\1(" NTC
"\1)" NTC
"\1/" NTC /* REGEXP */
@ -306,7 +306,6 @@ static char * const tokenlist =
;
static const uint32_t tokeninfo[] = {
0,
0,
OC_REGEXP,
@ -371,7 +370,7 @@ enum {
ENVIRON, F0, _intvarcount_
};
static char * vNames =
static const char vNames[] =
"CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
"ORS\0" "RS\0*" "RT\0" "FILENAME\0"
"SUBSEP\0" "ARGIND\0" "ARGC\0" "ARGV\0"
@ -379,7 +378,7 @@ static char * vNames =
"NR\0" "NF\0*" "IGNORECASE\0*"
"ENVIRON\0" "$\0*" "\0";
static char * vValues =
static const char vValues[] =
"%.6g\0" "%.6g\0" " \0" " \0"
"\n\0" "\n\0" "\0" "\0"
"\034\0"
@ -387,8 +386,8 @@ static char * vValues =
/* hash size may grow to these values */
#define FIRST_PRIME 61;
static const unsigned int PRIMES[] = { 251, 1021, 4093, 16381, 65521 };
enum { NPRIMES = sizeof(PRIMES) / sizeof(unsigned int) };
static const unsigned PRIMES[] = { 251, 1021, 4093, 16381, 65521 };
enum { NPRIMES = sizeof(PRIMES) / sizeof(unsigned) };
/* globals */
@ -441,10 +440,15 @@ static const char EMSG_TOO_FEW_ARGS[] = "Too few arguments for builtin";
static const char EMSG_NOT_ARRAY[] = "Not an array";
static const char EMSG_POSSIBLE_ERROR[] = "Possible syntax error";
static const char EMSG_UNDEF_FUNC[] = "Call to undefined function";
#ifndef CONFIG_FEATURE_AWK_MATH
#if !ENABLE_FEATURE_AWK_MATH
static const char EMSG_NO_MATH[] = "Math support is not compiled in";
#endif
static void zero_out_var(var * vp)
{
memset(vp, 0, sizeof(*vp));
}
static void syntax_error(const char * const message) ATTRIBUTE_NORETURN;
static void syntax_error(const char * const message)
{
@ -456,9 +460,9 @@ static void syntax_error(const char * const message)
/* ---- hash stuff ---- */
static unsigned int hashidx(const char *name)
static unsigned hashidx(const char *name)
{
unsigned int idx=0;
unsigned idx = 0;
while (*name) idx = *name++ + (idx << 6) - idx;
return idx;
@ -493,7 +497,7 @@ static void *hash_search(xhash *hash, const char *name)
/* grow hash if it becomes too big */
static void hash_rebuild(xhash *hash)
{
unsigned int newsize, i, idx;
unsigned newsize, i, idx;
hash_item **newitems, *hi, *thi;
if (hash->nprime == NPRIMES)
@ -522,7 +526,7 @@ static void hash_rebuild(xhash *hash)
static void *hash_find(xhash *hash, const char *name)
{
hash_item *hi;
unsigned int idx;
unsigned idx;
int l;
hi = hash_search(hash, name);
@ -542,10 +546,10 @@ static void *hash_find(xhash *hash, const char *name)
return &(hi->data);
}
#define findvar(hash, name) (var *) hash_find ( (hash) , (name) )
#define newvar(name) (var *) hash_find ( vhash , (name) )
#define newfile(name) (rstream *) hash_find ( fdhash , (name) )
#define newfunc(name) (func *) hash_find ( fnhash , (name) )
#define findvar(hash, name) ((var*) hash_find((hash) , (name)))
#define newvar(name) ((var*) hash_find(vhash , (name)))
#define newfile(name) ((rstream*)hash_find(fdhash ,(name)))
#define newfunc(name) ((func*) hash_find(fnhash , (name)))
static void hash_remove(xhash *hash, const char *name)
{
@ -582,7 +586,7 @@ static char *nextword(char **s)
{
char *p = *s;
while (*(*s)++) ;
while (*(*s)++) /* */;
return p;
}
@ -626,7 +630,7 @@ static xhash *iamarray(var *v)
static void clear_array(xhash *array)
{
unsigned int i;
unsigned i;
hash_item *hi, *thi;
for (i=0; i<array->csize; i++) {
@ -833,15 +837,16 @@ static void nvfree(var *v)
*/
static uint32_t next_token(uint32_t expected)
{
char *p, *pp, *s;
char *tl;
uint32_t tc;
const uint32_t *ti;
int l;
static int concat_inserted;
static uint32_t save_tclass, save_info;
static uint32_t ltclass = TC_OPTERM;
char *p, *pp, *s;
const char *tl;
uint32_t tc;
const uint32_t *ti;
int l;
if (t.rollback) {
t.rollback = FALSE;
@ -1403,7 +1408,7 @@ static void fsrealloc(int size)
if (size >= maxfields) {
i = maxfields;
maxfields = size + 16;
Fields = (var *)xrealloc(Fields, maxfields * sizeof(var));
Fields = xrealloc(Fields, maxfields * sizeof(var));
for (; i < maxfields; i++) {
Fields[i].type = VF_SPECIAL;
Fields[i].string = NULL;
@ -1441,11 +1446,11 @@ static int awk_split(char *s, node *spl, char **slist)
if (pmatch[0].rm_eo == 0) { l++; pmatch[0].rm_eo++; }
} else {
pmatch[0].rm_eo = l;
if (*(s+l)) pmatch[0].rm_eo++;
if (s[l]) pmatch[0].rm_eo++;
}
memcpy(s1, s, l);
*(s1+l) = '\0';
s1[l] = '\0';
nextword(&s1);
s += pmatch[0].rm_eo;
n++;
@ -1610,7 +1615,8 @@ static int hashwalk_next(var *v)
/* evaluate node, return 1 when result is true, 0 otherwise */
static int ptest(node *pattern)
{
static var v;
static var v; /* static: to save stack space? */
return istrue(evaluate(pattern, &v));
}
@ -1717,7 +1723,7 @@ static int fmt_num(char *b, int size, const char *format, double n, int int_as_i
if (int_as_int && n == (int)n) {
r = snprintf(b, size, "%d", (int)n);
} else {
do { c = *s; } while (*s && *++s);
do { c = *s; } while (c && *++s);
if (strchr("diouxX", c)) {
r = snprintf(b, size, format, (int)n);
} else if (strchr("eEfgG", c)) {
@ -1752,14 +1758,16 @@ static char *awk_printf(node *n)
incr = (f - s) + MAXVARFMT;
qrealloc(&b, incr + i, &bsize);
c = *f; if (c != '\0') f++;
c1 = *f ; *f = '\0';
c = *f;
if (c != '\0') f++;
c1 = *f;
*f = '\0';
arg = evaluate(nextarg(&n), v);
j = i;
if (c == 'c' || !c) {
i += sprintf(b+i, s,
is_numeric(arg) ? (char)getvar_i(arg) : *getvar_s(arg));
i += sprintf(b+i, s, is_numeric(arg) ?
(char)getvar_i(arg) : *getvar_s(arg));
} else if (c == 's') {
s1 = getvar_s(arg);
@ -1891,7 +1899,7 @@ static var *exec_builtin(node *op, var *res)
switch (info & OPNMASK) {
case B_a2:
#ifdef CONFIG_FEATURE_AWK_MATH
#if ENABLE_FEATURE_AWK_MATH
setvar_i(res, atan2(getvar_i(av[i]), getvar_i(av[1])));
#else
runtime_error(EMSG_NO_MATH);
@ -2043,7 +2051,7 @@ static var *evaluate(node *op, var *res)
{
/* This procedure is recursive so we should count every byte */
static var *fnargs = NULL;
static unsigned int seed = 1;
static unsigned seed = 1;
static regex_t sreg;
node *op1;
var *v1;
@ -2328,7 +2336,7 @@ re_cont:
R.d = (double)rand() / (double)RAND_MAX;
break;
#ifdef CONFIG_FEATURE_AWK_MATH
#if ENABLE_FEATURE_AWK_MATH
case F_co:
R.d = cos(L.d);
break;
@ -2360,7 +2368,7 @@ re_cont:
case F_sr:
R.d = (double)seed;
seed = op1 ? (unsigned int)L.d : (unsigned int)time(NULL);
seed = op1 ? (unsigned)L.d : (unsigned)time(NULL);
srand(seed);
break;
@ -2465,7 +2473,7 @@ re_cont:
strcpy(X.s, L.s);
if ((opinfo & OPCLSMASK) == OC_COMMA) {
L.s = getvar_s(V[SUBSEP]);
X.s = (char *)xrealloc(X.s, opn + strlen(L.s));
X.s = xrealloc(X.s, opn + strlen(L.s));
strcat(X.s, L.s);
}
strcat(X.s, R.s);
@ -2498,7 +2506,7 @@ re_cont:
L.d /= R.d;
break;
case '&':
#ifdef CONFIG_FEATURE_AWK_MATH
#if ENABLE_FEATURE_AWK_MATH
L.d = pow(L.d, R.d);
#else
runtime_error(EMSG_NO_MATH);
@ -2553,9 +2561,11 @@ re_cont:
static int awk_exit(int r)
{
unsigned int i;
var tv;
unsigned i;
hash_item *hi;
static var tv;
zero_out_var(&tv);
if (!exiting) {
exiting = TRUE;
@ -2635,15 +2645,14 @@ int awk_main(int argc, char **argv)
{
unsigned opt;
char *opt_F, *opt_v, *opt_W;
char *s, *s1;
int i, j, c, flen;
int i, j, flen;
var *v;
static var tv;
var tv;
char **envp;
static int from_file = FALSE;
rstream *rsm;
FILE *F, *stdfiles[3];
static char * stdnames = "/dev/stdin\0/dev/stdout\0/dev/stderr";
char *vnames = (char *)vNames; /* cheat */
char *vvalues = (char *)vValues;
zero_out_var(&tv);
/* allocate global buffer */
buf = xmalloc(MAXVARFMT + 1);
@ -2654,82 +2663,76 @@ int awk_main(int argc, char **argv)
fnhash = hash_init();
/* initialize variables */
for (i=0; *vNames; i++) {
V[i] = v = newvar(nextword(&vNames));
if (*vValues != '\377')
setvar_s(v, nextword(&vValues));
for (i = 0; *vnames; i++) {
V[i] = v = newvar(nextword(&vnames));
if (*vvalues != '\377')
setvar_s(v, nextword(&vvalues));
else
setvar_i(v, 0);
if (*vNames == '*') {
if (*vnames == '*') {
v->type |= VF_SPECIAL;
vNames++;
vnames++;
}
}
handle_special(V[FS]);
handle_special(V[RS]);
stdfiles[0] = stdin;
stdfiles[1] = stdout;
stdfiles[2] = stderr;
for (i=0; i<3; i++) {
rsm = newfile(nextword(&stdnames));
rsm->F = stdfiles[i];
}
newfile("/dev/stdin")->F = stdin;
newfile("/dev/stdout")->F = stdout;
newfile("/dev/stderr")->F = stderr;
for (envp = environ; *envp; envp++) {
s = xstrdup(*envp);
s1 = strchr(s, '=');
if (!s1) {
goto keep_going;
}
*(s1++) = '\0';
char *s = xstrdup(*envp);
char *s1 = strchr(s, '=');
if (s1) {
*s1++ = '\0';
setvar_u(findvar(iamarray(V[ENVIRON]), s), s1);
keep_going:
}
free(s);
}
opt = getopt32(argc, argv, "F:v:f:W:", &opt_F, &opt_v, &programname, &opt_W);
argv += optind;
argc -= optind;
if (opt & 0x1) setvar_s(V[FS], opt_F); // -F
if (opt & 0x2) if (!is_assignment(opt_v)) bb_show_usage(); // -v
if (opt & 0x4) { // -f
from_file = TRUE;
F = afopen(programname, "r");
s = NULL;
char *s = s; /* die, gcc, die */
FILE *from_file = afopen(programname, "r");
/* one byte is reserved for some trick in next_token */
if (fseek(F, 0, SEEK_END) == 0) {
flen = ftell(F);
if (fseek(from_file, 0, SEEK_END) == 0) {
flen = ftell(from_file);
s = xmalloc(flen + 4);
fseek(F, 0, SEEK_SET);
i = 1 + fread(s+1, 1, flen, F);
fseek(from_file, 0, SEEK_SET);
i = 1 + fread(s + 1, 1, flen, from_file);
} else {
for (i = j = 1; j > 0; i += j) {
s = (char *)xrealloc(s, i+4096);
j = fread(s+i, 1, 4094, F);
s = xrealloc(s, i + 4096);
j = fread(s + i, 1, 4094, from_file);
}
}
s[i] = '\0';
fclose(F);
fclose(from_file);
parse_program(s + 1);
free(s);
} else { // no -f: take program from 1st parameter
if (!argc)
bb_show_usage();
programname = "cmd. line";
parse_program(*argv++);
argc--;
}
if (opt & 0x8) // -W
bb_error_msg("warning: unrecognized option '-W %s' ignored", opt_W);
if (!from_file) {
if (argc == optind)
bb_show_usage();
programname = "cmd. line";
parse_program(argv[optind++]);
}
/* fill in ARGV array */
setvar_i(V[ARGC], argc - optind + 1);
setvar_i(V[ARGC], argc + 1);
setari_u(V[ARGV], 0, "awk");
for (i = optind; i < argc; i++)
setari_u(V[ARGV], i+1-optind, argv[i]);
i = 0;
while (*argv)
setari_u(V[ARGV], ++i, *argv++);
evaluate(beginseq.first, &tv);
if (!mainseq.first && !endseq.first)
@ -2740,12 +2743,10 @@ keep_going:
/* passing through input files */
while (iF) {
nextfile = FALSE;
setvar_i(V[FNR], 0);
while ((c = awk_getline(iF, V[F0])) > 0) {
while ((i = awk_getline(iF, V[F0])) > 0) {
nextrec = FALSE;
incvar(V[NR]);
incvar(V[FNR]);
@ -2755,14 +2756,12 @@ keep_going:
break;
}
if (c < 0)
if (i < 0)
runtime_error(strerror(errno));
iF = next_input_file();
}
awk_exit(EXIT_SUCCESS);
return 0;
/*return 0;*/
}