awk: move locals deeper into scopes where they are used, no logic changes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2021-06-25 19:41:05 +02:00
parent 8c5da0323b
commit 78645d8371

View File

@@ -3254,20 +3254,19 @@ static var *evaluate(node *op, var *res)
static int awk_exit(int r) static int awk_exit(int r)
{ {
var tv;
unsigned i; unsigned i;
hash_item *hi;
zero_out_var(&tv);
if (!exiting) { if (!exiting) {
var tv;
exiting = TRUE; exiting = TRUE;
nextrec = FALSE; nextrec = FALSE;
zero_out_var(&tv);
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++) {
hash_item *hi;
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)
@@ -3348,11 +3347,7 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
llist_t *list_e = NULL; llist_t *list_e = NULL;
#endif #endif
int i; int i;
var *v;
var tv; var tv;
char **envp;
char *vnames = (char *)vNames; /* cheat */
char *vvalues = (char *)vValues;
INIT_G(); INIT_G();
@@ -3361,8 +3356,6 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
if (ENABLE_LOCALE_SUPPORT) if (ENABLE_LOCALE_SUPPORT)
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
zero_out_var(&tv);
/* allocate global buffer */ /* allocate global buffer */
g_buf = xmalloc(MAXVARFMT + 1); g_buf = xmalloc(MAXVARFMT + 1);
@@ -3372,16 +3365,21 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
fnhash = hash_init(); fnhash = hash_init();
/* initialize variables */ /* initialize variables */
for (i = 0; *vnames; i++) { {
intvar[i] = v = newvar(nextword(&vnames)); char *vnames = (char *)vNames; /* cheat */
if (*vvalues != '\377') char *vvalues = (char *)vValues;
setvar_s(v, nextword(&vvalues)); for (i = 0; *vnames; i++) {
else var *v;
setvar_i(v, 0); intvar[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; v->type |= VF_SPECIAL;
vnames++; vnames++;
}
} }
} }
@@ -3393,16 +3391,19 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
newfile("/dev/stderr")->F = stderr; newfile("/dev/stderr")->F = stderr;
/* Huh, people report that sometimes environ is NULL. Oh well. */ /* Huh, people report that sometimes environ is NULL. Oh well. */
if (environ) for (envp = environ; *envp; envp++) { if (environ) {
/* environ is writable, thus we don't strdup it needlessly */ char **envp;
char *s = *envp; for (envp = environ; *envp; envp++) {
char *s1 = strchr(s, '='); /* environ is writable, thus we don't strdup it needlessly */
if (s1) { char *s = *envp;
*s1 = '\0'; char *s1 = strchr(s, '=');
/* Both findvar and setvar_u take const char* if (s1) {
* as 2nd arg -> environment is not trashed */ *s1 = '\0';
setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1); /* Both findvar and setvar_u take const char*
*s1 = '='; * as 2nd arg -> environment is not trashed */
setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
*s1 = '=';
}
} }
} }
opt = getopt32(argv, OPTSTR_AWK, &opt_F, &list_v, &list_f, IF_FEATURE_AWK_GNU_EXTENSIONS(&list_e,) NULL); opt = getopt32(argv, OPTSTR_AWK, &opt_F, &list_v, &list_f, IF_FEATURE_AWK_GNU_EXTENSIONS(&list_e,) NULL);
@@ -3466,6 +3467,7 @@ int awk_main(int argc UNUSED_PARAM, char **argv)
setari_u(intvar[ARGV], ++i, *argv++); setari_u(intvar[ARGV], ++i, *argv++);
setvar_i(intvar[ARGC], i + 1); setvar_i(intvar[ARGC], i + 1);
zero_out_var(&tv);
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);