From 0b4f34e0a8b418b4ec8185d94c189c41e0859b44 Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 12 Jul 2008 16:56:40 +0000 Subject: [PATCH] =?UTF-8?q?=E2=80=A2=20syn.c:=20replace=20expanded=20use?= =?UTF-8?q?=20of=20str=5Fsave()=20with=20the=20actual=20macro=20=E2=80=A2?= =?UTF-8?q?=20others:=20fix=206=20(!)=20cases=20of=20non-constant=20or=20s?= =?UTF-8?q?ide-effect=20arguments=20=20=20to=20the=20str=5Fsave()=20or=20s?= =?UTF-8?q?tr=5Fnsave()=20macros,=20and=20other=20abuse=20of=20them=20?= =?UTF-8?q?=E2=80=A2=20also=20fix=20some=20cosmetics=20and=20other=20un-ni?= =?UTF-8?q?ce=20code=20while=20here?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eval.c | 10 ++++------ expr.c | 7 +++++-- funcs.c | 11 ++++++----- histrap.c | 25 ++++++++++++------------- lex.c | 9 +++++---- syn.c | 4 ++-- var.c | 11 ++++++----- 7 files changed, 40 insertions(+), 37 deletions(-) diff --git a/eval.c b/eval.c index 527ae73..1aa0e9c 100644 --- a/eval.c +++ b/eval.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.46 2008/05/17 18:46:58 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.47 2008/07/12 16:56:38 tg Exp $"); #ifdef MKSH_SMALL #define MKSH_NOPWNAM @@ -425,7 +425,8 @@ expand(const char *cp, /* input word */ goto no_repl; /* prepare string on which to work */ - sbeg = s = str_save(str_val(st->var), ATEMP); + tpat0 = str_val(st->var); + sbeg = s = str_save(tpat0, ATEMP); /* first see if we have any match at all */ tpat0 = pat; @@ -769,10 +770,7 @@ expand(const char *cp, /* input word */ Xlength(ds, dp) == 0) { char *p; - if ((p = str_nsave(null, 0, ATEMP)) - == NULL) - internal_errorf("unable " - "to allocate memory"); + *(p = alloc(1, ATEMP)) = '\0'; XPput(*wp, p); } type = XSUBMID; diff --git a/expr.c b/expr.c index e9ab4c3..dec4be4 100644 --- a/expr.c +++ b/expr.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.17 2008/04/20 00:24:25 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.18 2008/07/12 16:56:38 tg Exp $"); /* The order of these enums is constrained by the order of opinfo[] */ enum token { @@ -480,9 +480,12 @@ exprtoken(Expr_state *es) tvar = str_nsave(es->tokp, cp - es->tokp, ATEMP); goto process_tvar; } else if (ksh_isdigit(c)) { + int i; + while (c != '_' && (ksh_isalnux(c) || c == '#')) c = *cp++; - tvar = str_nsave(es->tokp, --cp - es->tokp, ATEMP); + i = --cp - es->tokp; + tvar = str_nsave(es->tokp, i, ATEMP); process_tvar: es->val = tempvar(); es->val->flag &= ~INTEGER; diff --git a/funcs.c b/funcs.c index 8d55d92..4170650 100644 --- a/funcs.c +++ b/funcs.c @@ -5,7 +5,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.83 2008/06/08 17:16:25 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.84 2008/07/12 16:56:39 tg Exp $"); /* A leading = means assignments before command are kept; * a leading * means a POSIX special builtin; @@ -1074,8 +1074,10 @@ c_alias(const char **wp) struct tbl *ap; int h; - if ((val = cstrchr(alias, '='))) - alias = xalias = str_nsave(alias, val++ - alias, ATEMP); + if ((val = cstrchr(alias, '='))) { + h = val++ - alias; + alias = xalias = str_nsave(alias, h, ATEMP); + } h = hash(alias); if (val == NULL && !tflag && !xflag) { ap = ktsearch(t, alias, h); @@ -1103,8 +1105,7 @@ c_alias(const char **wp) afree((void*)ap->val.s, APERM); } /* ignore values for -t (at&t ksh does this) */ - newval = tflag ? search(alias, path, X_OK, NULL) : - val; + newval = tflag ? search(alias, path, X_OK, NULL) : val; if (newval) { ap->val.s = str_save(newval, APERM); ap->flag |= ALLOC|ISSET; diff --git a/histrap.c b/histrap.c index bc6add3..d78cf54 100644 --- a/histrap.c +++ b/histrap.c @@ -3,7 +3,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.65 2008/07/06 22:41:08 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.66 2008/07/12 16:56:39 tg Exp $"); /*- * MirOS: This is the default mapping type, and need not be specified. @@ -21,7 +21,7 @@ static int hist_count_lines(unsigned char *, int); static int hist_shrink(unsigned char *, int); static unsigned char *hist_skip_back(unsigned char *,int *,int); static void histload(Source *, unsigned char *, int); -static void histinsert(Source *, int, unsigned char *); +static void histinsert(Source *, int, const char *); static void writehistfile(int, char *); static int sprinkle(int); #endif @@ -68,7 +68,8 @@ c_fc(const char **wp) sflag++; else { size_t len = strlen(p); - editor = str_nsave(p, len + 4, ATEMP); + editor = alloc(len + 4, ATEMP); + memcpy(editor, p, len); memcpy(editor + len, " $_", 4); } break; @@ -639,8 +640,7 @@ hist_init(Source *s) hist_source = s; #if HAVE_PERSISTENT_HISTORY - hname = str_val(global("HISTFILE")); - if (hname == NULL) + if ((hname = str_val(global("HISTFILE"))) == NULL) return; hname = str_save(hname, APERM); @@ -855,7 +855,7 @@ histload(Source *s, unsigned char *base, int bytes) /* worry about line numbers */ if (histptr >= history && lno-1 != s->line) { /* a replacement ? */ - histinsert(s, lno, line); + histinsert(s, lno, (char *)line); } else { s->line = lno; histsave(lno, (char *)line, 0); @@ -870,15 +870,15 @@ histload(Source *s, unsigned char *base, int bytes) * Insert a line into the history at a specified number */ static void -histinsert(Source *s, int lno, unsigned char *line) +histinsert(Source *s, int lno, const char *line) { char **hp; - if (lno >= s->line-(histptr-history) && lno <= s->line) { - hp = &histptr[lno-s->line]; + if (lno >= s->line - (histptr - history) && lno <= s->line) { + hp = &histptr[lno - s->line]; if (*hp) afree((void*)*hp, APERM); - *hp = str_save((char *)line, APERM); + *hp = str_save(line, APERM); } } @@ -1005,9 +1005,8 @@ inittraps(void) else { char *s; - sigtraps[i].name = s = str_save( - !strncasecmp(cs, "SIG", 3) ? cs + 3 : cs, - APERM); + s = !strncasecmp(cs, "SIG", 3) ? cs + 3 : cs; + sigtraps[i].name = s = str_save(s, APERM); while ((*s = ksh_toupper(*s))) ++s; } diff --git a/lex.c b/lex.c index 7923be7..e3c1c1d 100644 --- a/lex.c +++ b/lex.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.65 2008/07/09 21:32:43 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.66 2008/07/12 16:56:39 tg Exp $"); /* * states while lexing word @@ -1290,9 +1290,10 @@ set_prompt(int to, Source *s) * unwinding its stack through this code as it * exits. */ - } else - prompt = str_save(substitute(ps1, 0), - saved_atemp); + } else { + char *cp = substitute(ps1, 0); + prompt = str_save(cp, saved_atemp); + } quitenv(NULL); } break; diff --git a/syn.c b/syn.c index fc523a8..e9dc635 100644 --- a/syn.c +++ b/syn.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.22 2008/06/28 22:51:56 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.23 2008/07/12 16:56:40 tg Exp $"); struct nesting_state { int start_token; /* token than began nesting (eg, FOR) */ @@ -366,7 +366,7 @@ get_command(int cf) if (!is_wdvarname(yylval.cp, true)) yyerror("%s: bad identifier\n", c == FOR ? "for" : "select"); - t->str = str_nsave(ident, strlen(ident), ATEMP); + t->str = str_save(ident, ATEMP); nesting_push(&old_nesting, c); t->vars = wordlist(); t->left = dogroup(); diff --git a/var.c b/var.c index 5cfe7f5..ae38eea 100644 --- a/var.c +++ b/var.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/var.c,v 1.56 2008/05/17 18:47:03 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/var.c,v 1.57 2008/07/12 16:56:40 tg Exp $"); /* * Variables @@ -130,7 +130,7 @@ array_index_calc(const char *n, bool *arrayp, uint32_t *valp) /* Calculate the value of the subscript */ *arrayp = true; - tmp = str_nsave(p+1, len-2, ATEMP); + tmp = str_nsave(p + 1, len - 2, ATEMP); sub = substitute(tmp, 0); afree(tmp, ATEMP); n = str_nsave(n, p - n, ATEMP); @@ -642,9 +642,10 @@ typeset(const char *var, Tflag set, Tflag clr, int field, int base) } val += len; } - if (*val == '=') - tvar = str_nsave(var, val++ - var, ATEMP); - else { + if (*val == '=') { + int i = val++ - var; + tvar = str_nsave(var, i, ATEMP); + } else { /* Importing from original environment: must have an = */ if (set & IMPORT) return NULL;