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