From 62b347a1b0767070be1e54884da2b0f63fe36714 Mon Sep 17 00:00:00 2001 From: tg Date: Sun, 4 Mar 2007 00:13:17 +0000 Subject: [PATCH] merge the const branch +- a few --- Makefile | 3 +- edit.c | 5 +- eval.c | 78 +++++++++++++---------- exec.c | 111 +++++++++++++++++--------------- funcs.c | 185 +++++++++++++++++++++++++++++------------------------- histrap.c | 18 +++--- main.c | 27 ++++---- misc.c | 14 ++--- sh.h | 151 ++++++++++++++++++++++++++------------------ syn.c | 24 +++---- tree.c | 36 +++++------ var.c | 6 +- 12 files changed, 358 insertions(+), 300 deletions(-) diff --git a/Makefile b/Makefile index e7c30f1..e3edff4 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/Makefile,v 1.32 2007/03/03 21:48:33 tg Exp $ +# $MirOS: src/bin/mksh/Makefile,v 1.33 2007/03/04 00:13:14 tg Exp $ .include @@ -22,7 +22,6 @@ CPPFLAGS+= -DMKSH_ASSUME_UTF8 \ -DHAVE_MULTI_IDSTRING=1 -DHAVE_PERSISTENT_HISTORY=1 COPTS+= -std=gnu99 -Wall .endif -CDIAGFLAGS+= -Wno-cast-qual LINKS+= ${BINDIR}/${PROG} ${BINDIR}/sh MLINKS+= ${PROG}.1 sh.1 diff --git a/edit.c b/edit.c index f09bb3d..307ef79 100644 --- a/edit.c +++ b/edit.c @@ -5,7 +5,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.82 2007/02/16 17:46:42 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.83 2007/03/04 00:13:14 tg Exp $"); /* tty driver characters we are interested in */ typedef struct { @@ -602,8 +602,7 @@ x_free_words(int nwords, char **words) int i; for (i = 0; i < nwords; i++) - if (words[i]) - afree(words[i], ATEMP); + afreechk(words[i]) afree(words, ATEMP); } diff --git a/eval.c b/eval.c index 3bce92f..c1e5848 100644 --- a/eval.c +++ b/eval.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.22 2007/01/17 18:01:51 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.23 2007/03/04 00:13:15 tg Exp $"); #ifdef MKSH_SMALL #define MKSH_NOPWNAM @@ -40,12 +40,12 @@ typedef struct Expand { #define IFS_WS 1 /* have seen IFS white-space */ #define IFS_NWS 2 /* have seen IFS non-white-space */ -static int varsub(Expand *, char *, char *, int *, int *); -static int comsub(Expand *, char *); +static int varsub(Expand *, const char *, const char *, int *, int *); +static int comsub(Expand *, const char *); static char *trimsub(char *, char *, int); static void glob(char *, XPtrV *, int); static void globit(XString *, char **, char *, XPtrV *, int); -static char *maybe_expand_tilde(char *, XString *, char **, int); +static const char *maybe_expand_tilde(const char *, XString *, char **, int); static char *tilde(char *); #ifndef MKSH_NOPWNAM static char *homedir(char *); @@ -73,12 +73,16 @@ substitute(const char *cp, int f) * expand arg-list */ char ** -eval(char **ap, int f) +eval(const char **ap, int f) { XPtrV w; - if (*ap == NULL) - return ap; + if (*ap == NULL) { + union mksh_ccphack vap; + + vap.ro = ap; + return (vap.rw); + } XPinit(w, 32); XPput(w, NULL); /* space for shell name */ while (*ap != NULL) @@ -91,15 +95,16 @@ eval(char **ap, int f) * expand string */ char * -evalstr(char *cp, int f) +evalstr(const char *cp, int f) { XPtrV w; + char *dp; XPinit(w, 1); expand(cp, &w, f); - cp = (XPsize(w) == 0) ? null : (char*) *XPptrv(w); + dp = (XPsize(w) == 0) ? null : (char*) *XPptrv(w); XPfree(w); - return cp; + return (dp); } /* @@ -107,25 +112,26 @@ evalstr(char *cp, int f) * used from iosetup to expand redirection files */ char * -evalonestr(char *cp, int f) +evalonestr(const char *cp, int f) { XPtrV w; + char *rv; XPinit(w, 1); expand(cp, &w, f); switch (XPsize(w)) { case 0: - cp = null; + rv = null; break; case 1: - cp = (char*) *XPptrv(w); + rv = (char *) *XPptrv(w); break; default: - cp = evalstr(cp, f&~DOGLOB); + rv = evalstr(cp, f&~DOGLOB); break; } XPfree(w); - return cp; + return (rv); } /* for nested substitution: ${var:=$var2} */ @@ -140,7 +146,7 @@ typedef struct SubType { } SubType; void -expand(char *cp, /* input word */ +expand(const char *cp, /* input word */ XPtrV *wp, /* output words */ int f) /* DO* flags */ { @@ -148,7 +154,8 @@ expand(char *cp, /* input word */ int type; /* expansion type */ int quote = 0; /* quoted */ XString ds; /* destination string */ - char *dp, *sp; /* dest., source */ + char *dp; /* destination */ + const char *sp; /* source */ int fdo, word; /* second pass flags; have word */ int doblank; /* field splitting of parameter/command subst */ Expand x = { /* expansion variables */ @@ -262,26 +269,25 @@ expand(char *cp, /* input word */ * This is where all syntax checking gets done... */ { - char *varname = ++sp; /* skip the { or x (}) */ + const char *varname = ++sp; /* skip the { or x (}) */ int stype; int slen = 0; sp = strchr(sp, '\0') + 1; /* skip variable */ type = varsub(&x, varname, sp, &stype, &slen); if (type < 0) { - char endc; - char *str, *end; + char *beg, *end, *str; sp = varname - 2; /* restore sp */ - end = sp + (wdscan(sp, CSUBST) - sp); + end = (beg = str_save(sp, ATEMP)) + + (wdscan(sp, CSUBST) - sp); /* ({) the } or x is already skipped */ - endc = *end; *end = EOS; - str = snptreef(NULL, 64, "%S", sp); - *end = endc; + str = snptreef(NULL, 64, "%S", beg); + afree(beg, ATEMP); errorf("%s: bad substitution", str); } - if (f&DOBLANK) + if (f & DOBLANK) doblank++; tilde_ok = 0; if (type == XBASE) { /* expand? */ @@ -639,7 +645,8 @@ expand(char *cp, /* input word */ if (type == XBASE && (f & (DOTILDE|DOASNTILDE)) && (tilde_ok & 2)) { - char *p, *dp_x; + const char *p; + char *dp_x; dp_x = dp; p = maybe_expand_tilde(sp, @@ -676,7 +683,7 @@ expand(char *cp, /* input word */ * Prepare to generate the string returned by ${} substitution. */ static int -varsub(Expand *xp, char *sp, char *word, +varsub(Expand *xp, const char *sp, const char *word, int *stypep, /* becomes qualifier type */ int *slenp) /* " " len (=, :=, etc.) valid iff *stypep != 0 */ { @@ -684,7 +691,7 @@ varsub(Expand *xp, char *sp, char *word, int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */ int stype; /* substitution type */ int slen; - char *p; + const char *p; struct tbl *vp; if (sp[0] == '\0') /* Bad variable name */ @@ -701,7 +708,8 @@ varsub(Expand *xp, char *sp, char *word, return -1; sp++; /* Check for size of array */ - if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') { + if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') && + p[2] == ']') { int n = 0; int max = 0; @@ -772,7 +780,8 @@ varsub(Expand *xp, char *sp, char *word, state = XARG; } } else { - if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') { + if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') && + p[2] == ']') { XPtrV wv; switch (stype & 0x7f) { @@ -827,7 +836,7 @@ varsub(Expand *xp, char *sp, char *word, * Run the command in $(...) and read its output. */ static int -comsub(Expand *xp, char *cp) +comsub(Expand *xp, const char *cp) { Source *s, *sold; struct op *t; @@ -1120,12 +1129,13 @@ debunk(char *dp, const char *sp, size_t dlen) * puts the expanded version in *dcp,dp and returns a pointer in p just * past the name, otherwise returns 0. */ -static char * -maybe_expand_tilde(char *p, XString *dsp, char **dpp, int isassign) +static const char * +maybe_expand_tilde(const char *p, XString *dsp, char **dpp, int isassign) { XString ts; char *dp = *dpp; - char *tp, *r; + char *tp; + const char *r; Xinit(ts, tp, 16, ATEMP); /* : only for DOASNTILDE form */ diff --git a/exec.c b/exec.c index abab4e1..a2a6372 100644 --- a/exec.c +++ b/exec.c @@ -2,22 +2,20 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.24 2007/01/17 22:51:46 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.25 2007/03/04 00:13:15 tg Exp $"); -static int comexec(struct op *, struct tbl *volatile, char **, - int volatile); -static void scriptexec(struct op *, char **) +static int comexec(struct op *, struct tbl *volatile, const char **, + int volatile); +static void scriptexec(struct op *, const char **) __attribute__((noreturn)); -static int call_builtin(struct tbl *, char **); -static int iosetup(struct ioword *, struct tbl *); -static int herein(const char *, int); -static char *do_selectargs(char **, bool); -static int dbteste_isa(Test_env *, Test_meta); +static int call_builtin(struct tbl *, const char **); +static int iosetup(struct ioword *, struct tbl *); +static int herein(const char *, int); +static const char *do_selectargs(const char **, bool); +static int dbteste_isa(Test_env *, Test_meta); static const char *dbteste_getopnd(Test_env *, Test_op, int); -static int dbteste_eval(Test_env *, Test_op, const char *, const char *, - int); -static void dbteste_error(Test_env *, int, const char *); - +static int dbteste_eval(Test_env *, Test_op, const char *, const char *, int); +static void dbteste_error(Test_env *, int, const char *); /* * execute command tree @@ -29,8 +27,9 @@ execute(struct op *volatile t, int i; volatile int rv = 0; int pv[2]; - char ** volatile ap; - char *s, *cp; + const char ** volatile ap; + char ** volatile up; + const char *s, *cp; struct ioword **iowp; struct tbl *tp = NULL; @@ -63,10 +62,11 @@ execute(struct op *volatile t, /* POSIX says expand command words first, then redirections, * and assignments last.. */ - ap = eval(t->args, t->u.evalflags | DOBLANK | DOGLOB | DOTILDE); + up = eval(t->args, t->u.evalflags | DOBLANK | DOGLOB | DOTILDE); if (flags & XTIME) /* Allow option parsing (bizarre, but POSIX) */ - timex_hook(t, &ap); + timex_hook(t, &up); + ap = (const char **)up; if (Flag(FXTRACE) && ap[0]) { shf_fprintf(shl_out, "%s", substitute(str_val(global("PS4")), 0)); @@ -104,7 +104,7 @@ execute(struct op *volatile t, switch (t->type) { case TCOM: - rv = comexec(t, tp, ap, flags); + rv = comexec(t, tp, (const char **)ap, flags); break; case TPAREN: @@ -245,8 +245,9 @@ execute(struct op *volatile t, case TSELECT: { volatile bool is_first = true; - ap = (t->vars != NULL) ? eval(t->vars, DOBLANK|DOGLOB|DOTILDE) : - e->loc->argv + 1; + ap = (t->vars == NULL) ? e->loc->argv + 1 : + (const char **)eval((const char **)t->vars, + DOBLANK | DOGLOB | DOTILDE); e->type = E_LOOP; while (1) { i = sigsetjmp(e->jbuf, 0); @@ -314,7 +315,7 @@ execute(struct op *volatile t, case TCASE: cp = evalstr(t->str, DOTILDE); for (t = t->left; t != NULL && t->type == TPAT; t = t->right) - for (ap = t->vars; *ap; ap++) + for (ap = (const char **)t->vars; *ap; ap++) if ((s = evalstr(*ap, DOTILDE|DOPAT)) && gmatchx(cp, s, false)) goto Found; @@ -340,12 +341,17 @@ execute(struct op *volatile t, case TEXEC: /* an eval'd TCOM */ s = t->args[0]; - ap = makenv(); + up = makenv(); restoresigs(); cleanup_proc_env(); - execve(t->str, t->args, ap); + { + union mksh_ccphack cargs; + + cargs.ro = t->args; + execve(t->str, cargs.rw, up); + } if (errno == ENOEXEC) - scriptexec(t, ap); + scriptexec(t, (const char **)up); else errorf("%s: %s", s, strerror(errno)); } @@ -368,12 +374,13 @@ execute(struct op *volatile t, */ static int -comexec(struct op *t, struct tbl *volatile tp, char **ap, volatile int flags) +comexec(struct op *t, struct tbl *volatile tp, const char **ap, + volatile int flags) { int i; volatile int rv = 0; - char *cp; - char **lastp; + const char *cp; + const char **lastp; static struct op texec; /* Must be static (XXX but why?) */ int type_flags; int keepasn_ok; @@ -503,14 +510,14 @@ comexec(struct op *t, struct tbl *volatile tp, char **ap, volatile int flags) switch (tp->type) { case CSHELL: /* shell built-in */ - rv = call_builtin(tp, ap); + rv = call_builtin(tp, (const char **)ap); break; case CFUNC: /* function call */ { volatile int old_xflag; volatile Tflag old_inuse; - char *volatile old_kshname; + const char *volatile old_kshname; if (!(tp->flag & ISSET)) { struct tbl *ftp; @@ -555,7 +562,7 @@ comexec(struct op *t, struct tbl *volatile tp, char **ap, volatile int flags) if (tp->flag & FKSH) kshname = ap[0]; else - ap[0] = (char *) kshname; + ap[0] = kshname; e->loc->argv = ap; for (i = 0; *ap++ != NULL; i++) ; @@ -664,35 +671,37 @@ comexec(struct op *t, struct tbl *volatile tp, char **ap, volatile int flags) } static void -scriptexec(struct op *tp, char **ap) +scriptexec(struct op *tp, const char **ap) { - static char execshell[] = "/bin/sh"; const char *sh; + union mksh_ccphack args, cap; sh = str_val(global("EXECSHELL")); if (sh && *sh) sh = search(sh, path, X_OK, NULL); if (!sh || !*sh) - sh = execshell; + sh = "/bin/sh"; *tp->args-- = tp->str; - *tp->args = str_save(sh, ATEMP); + args.ro = tp->args; + *args.ro = sh; - execve(tp->args[0], tp->args, ap); + cap.ro = ap; + execve(args.rw[0], args.rw, cap.rw); /* report both the program that was run and the bogus shell */ errorf("%s: %s: %s", tp->str, sh, strerror(errno)); } int -shcomexec(char **wp) +shcomexec(const char **wp) { struct tbl *tp; tp = ktsearch(&builtins, *wp, hash(*wp)); if (tp == NULL) internal_errorf(1, "shcomexec: %s", *wp); - return call_builtin(tp, wp); + return (call_builtin(tp, wp)); } /* @@ -768,7 +777,7 @@ define(const char *name, struct op *t) * add builtin */ void -builtin(const char *name, int (*func) (char **)) +builtin(const char *name, int (*func) (const char **)) { struct tbl *tp; Tflag flag; @@ -970,7 +979,7 @@ search(const char *name, const char *lpath, } static int -call_builtin(struct tbl *tp, char **wp) +call_builtin(struct tbl *tp, const char **wp) { int rv; @@ -1193,13 +1202,11 @@ herein(const char *content, int sub) * ksh special - the select command processing section * print the args in column form - assuming that we can */ -static char * -do_selectargs(char **ap, bool print_menu) +static const char * +do_selectargs(const char **ap, bool print_menu) { - static char read_args0[] = "read", - read_args1[] = "-r", read_args2[] = "REPLY", - *read_args[] = { - read_args0, read_args1, read_args2, NULL + static const char *read_args[] = { + "read", "-r", "REPLY", NULL }; char *s; int i, argct; @@ -1227,9 +1234,9 @@ do_selectargs(char **ap, bool print_menu) } struct select_menu_info { - char *const *args; - int arg_width; - int num_width; + const char *const *args; + int arg_width; + int num_width; }; static char *select_fmt_entry(const void *, int, char *, int); @@ -1250,10 +1257,10 @@ select_fmt_entry(const void *arg, int i, char *buf, int buflen) * print a select style menu */ int -pr_menu(char *const *ap) +pr_menu(const char *const *ap) { struct select_menu_info smi; - char *const *pp; + const char *const *pp; int nwidth, dwidth; int i, n; @@ -1331,7 +1338,7 @@ dbteste_isa(Test_env *te, Test_meta meta) { int ret = 0; int uqword; - char *p; + const char *p; if (!*te->pos.wp) return meta == TM_END; @@ -1367,7 +1374,7 @@ dbteste_isa(Test_env *te, Test_meta meta) static const char * dbteste_getopnd(Test_env *te, Test_op op, int do_eval) { - char *s = *te->pos.wp; + const char *s = *te->pos.wp; if (!s) return NULL; diff --git a/funcs.c b/funcs.c index 30b0c37..7b79960 100644 --- a/funcs.c +++ b/funcs.c @@ -5,10 +5,10 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.46 2007/01/26 18:37:26 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.47 2007/03/04 00:13:15 tg Exp $"); int -c_cd(char **wp) +c_cd(const char **wp) { int optc; int physical = Flag(FPHYSICAL); @@ -21,6 +21,7 @@ c_cd(char **wp) char *dir, *try, *pwd; int phys_path; char *cdpath; + bool dir_ = false; while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1) switch (optc) { @@ -51,15 +52,17 @@ c_cd(char **wp) } } else if (!wp[1]) { /* One argument: - or dir */ - dir = wp[0]; + dir = str_save(wp[0], ATEMP); if (strcmp(dir, "-") == 0) { + afree(dir, ATEMP); dir = str_val(oldpwd_s); if (dir == null) { bi_errorf("no OLDPWD"); return 1; } printpath++; - } + } else + dir_ = true; } else if (!wp[2]) { /* Two arguments - substitute arg1 in PWD for arg2 */ int ilen, olen, nlen, elen; @@ -83,6 +86,7 @@ c_cd(char **wp) nlen = strlen(wp[1]); elen = strlen(current_wd + ilen + olen) + 1; dir = alloc(ilen + nlen + elen, ATEMP); + dir_ = true; memcpy(dir, current_wd, ilen); memcpy(dir + ilen, wp[1], nlen); memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen); @@ -114,6 +118,7 @@ c_cd(char **wp) bi_errorf("%s: bad directory", dir); else bi_errorf("%s - %s", try, strerror(errno)); + afreechv(dir_, dir); return 1; } @@ -147,11 +152,12 @@ c_cd(char **wp) if (printpath || cdnode) shprintf("%s\n", pwd); + afreechv(dir_, dir); return 0; } int -c_pwd(char **wp) +c_pwd(const char **wp) { int optc; int physical = Flag(FPHYSICAL); @@ -187,7 +193,7 @@ c_pwd(char **wp) } int -c_print(char **wp) +c_print(const char **wp) { #define PO_NL BIT(0) /* print newline */ #define PO_EXPAND BIT(1) /* expand backslash sequences */ @@ -196,7 +202,7 @@ c_print(char **wp) #define PO_COPROC BIT(4) /* printing to coprocess: block SIGPIPE */ int fd = 1; int flags = PO_EXPAND|PO_NL; - char *s; + const char *s; const char *emsg; XString xs; char *xp; @@ -375,10 +381,10 @@ c_print(char **wp) } int -c_whence(char **wp) +c_whence(const char **wp) { struct tbl *tp; - char *id; + const char *id; int pflag = 0, vflag = 0, Vflag = 0; int ret = 0; int optc; @@ -495,7 +501,7 @@ c_whence(char **wp) /* Deal with command -vV - command -p dealt with in comexec() */ int -c_command(char **wp) +c_command(const char **wp) { /* Let c_whence do the work. Note that c_command() must be * a distinct function from c_whence() (tested in comexec()). @@ -505,14 +511,14 @@ c_command(char **wp) /* typeset, export, and readonly */ int -c_typeset(char **wp) +c_typeset(const char **wp) { struct block *l = e->loc; struct tbl *vp, **p; Tflag fset = 0, fclr = 0; int thing = 0, func = 0, localv = 0; const char *opts = "L#R#UZ#fi#lprtux"; /* see comment below */ - char *fieldstr, *basestr; + const char *fieldstr, *basestr; int field, base; int optc; Tflag flag; @@ -802,7 +808,7 @@ c_typeset(char **wp) } int -c_alias(char **wp) +c_alias(const char **wp) { struct table *t = &aliases; int rv = 0, rflag = 0, tflag, Uflag = 0, pflag = 0; @@ -856,8 +862,9 @@ c_alias(char **wp) /* "hash -r" means reset all the tracked aliases.. */ if (rflag) { - static char args0[] = "unalias", args1[] = "-ta", - *args[] = { args0, args1, NULL }; + static const char *args[] = { + "unalias", "-ta", NULL + }; if (!tflag || *wp) { shprintf("alias: -r flag can only be used with -t" @@ -885,14 +892,15 @@ c_alias(char **wp) } for (; *wp != NULL; wp++) { - char *alias = *wp; - char *val = strchr(alias, '='); + const char *alias = *wp; + char *xalias = NULL; + const char *val; const char *newval; struct tbl *ap; int h; - if (val) - alias = str_nsave(alias, val++ - alias, ATEMP); + if ((val = cstrchr(alias, '='))) + alias = xalias = str_nsave(alias, val++ - alias, ATEMP); h = hash(alias); if (val == NULL && !tflag && !xflag) { ap = ktsearch(t, alias, h); @@ -933,15 +941,14 @@ c_alias(char **wp) ap->flag &= ~xflag; else ap->flag |= xflag; - if (val) - afree(alias, ATEMP); + afreechk(xalias); } return rv; } int -c_unalias(char **wp) +c_unalias(const char **wp) { struct table *t = &aliases; struct tbl *ap; @@ -997,7 +1004,7 @@ c_unalias(char **wp) } int -c_let(char **wp) +c_let(const char **wp) { int rv = 1; long val; @@ -1015,7 +1022,7 @@ c_let(char **wp) } int -c_jobs(char **wp) +c_jobs(const char **wp) { int optc; int flag = 0; @@ -1052,7 +1059,7 @@ c_jobs(char **wp) } int -c_fgbg(char **wp) +c_fgbg(const char **wp) { int bg = strcmp(*wp, "bg") == 0; int rv = 0; @@ -1094,10 +1101,10 @@ kill_fmt_entry(const void *arg, int i, char *buf, int buflen) int -c_kill(char **wp) +c_kill(const char **wp) { Trap *t = NULL; - char *p; + const char *p; int lflag = 0; int i, n, rv, sig; @@ -1207,7 +1214,7 @@ getopts_reset(int val) } int -c_getopts(char **wp) +c_getopts(const char **wp) { int argc; const char *opts; @@ -1304,7 +1311,7 @@ c_getopts(char **wp) } int -c_bind(char **wp) +c_bind(const char **wp) { int optc, rv = 0, macro = 0, list = 0; char *cp; @@ -1369,18 +1376,18 @@ static void p_time(struct shf *, int, struct timeval *, int, /* :, false and true */ int -c_label(char **wp) +c_label(const char **wp) { return wp[0][0] == 'f' ? 1 : 0; } int -c_shift(char **wp) +c_shift(const char **wp) { struct block *l = e->loc; int n; long val; - char *arg; + const char *arg; if (ksh_getopt(wp, &builtin_opt, null) == '?') return 1; @@ -1406,10 +1413,10 @@ c_shift(char **wp) } int -c_umask(char **wp) +c_umask(const char **wp) { int i; - char *cp; + const char *cp; int symbolic = 0; mode_t old_umask; int optc; @@ -1427,20 +1434,20 @@ c_umask(char **wp) old_umask = umask(0); umask(old_umask); if (symbolic) { - char buf[18]; + char buf[18], *p; int j; old_umask = ~old_umask; - cp = buf; + p = buf; for (i = 0; i < 3; i++) { - *cp++ = "ugo"[i]; - *cp++ = '='; + *p++ = "ugo"[i]; + *p++ = '='; for (j = 0; j < 3; j++) if (old_umask & (1 << (8 - (3*i + j)))) - *cp++ = "rwx"[j]; - *cp++ = ','; + *p++ = "rwx"[j]; + *p++ = ','; } - cp[-1] = '\0'; + p[-1] = '\0'; shprintf("%s\n", buf); } else shprintf("%#3.3o\n", (unsigned) old_umask); @@ -1533,11 +1540,11 @@ c_umask(char **wp) } int -c_dot(char **wp) +c_dot(const char **wp) { const char *file; - char *cp; - char **argv; + const char *cp; + const char **argv; int argc; int i; int err; @@ -1572,7 +1579,7 @@ c_dot(char **wp) } int -c_wait(char **wp) +c_wait(const char **wp) { int rv = 0; int sig; @@ -1594,20 +1601,21 @@ c_wait(char **wp) } int -c_read(char **wp) +c_read(const char **wp) { int c = 0; int expande = 1, historyr = 0; int expanding; int ecode = 0; - char *cp; + const char *cp; + char *ccp; int fd = 0; struct shf *shf; int optc; const char *emsg; XString cs, xs = { NULL, NULL, 0, NULL}; struct tbl *vp; - char *xp = NULL; + char *xp = NULL, *wpalloc = NULL; static char REPLY[] = "REPLY"; while ((optc = ksh_getopt(wp, &builtin_opt, "prsu,")) != -1) @@ -1645,8 +1653,10 @@ c_read(char **wp) */ shf = shf_reopen(fd, SHF_RD | SHF_INTERRUPT | can_seek(fd), shl_spare); - if ((cp = strchr(*wp, '?')) != NULL) { - *cp = 0; + if ((cp = cstrchr(*wp, '?')) != NULL) { + wpalloc = str_save(*wp, ATEMP); + wpalloc[cp - *wp] = '\0'; + *wp = wpalloc; if (isatty(fd)) { /* at&t ksh says it prints prompt on fd if it's open * for writing and is a tty, but it doesn't do it @@ -1671,9 +1681,9 @@ c_read(char **wp) if (historyr) Xinit(xs, xp, 128, ATEMP); expanding = 0; - Xinit(cs, cp, 128, ATEMP); + Xinit(cs, ccp, 128, ATEMP); for (; *wp != NULL; wp++) { - for (cp = Xstring(cs, cp); ; ) { + for (ccp = Xstring(cs, ccp); ; ) { if (c == '\n' || c == EOF) break; while (1) { @@ -1700,7 +1710,7 @@ c_read(char **wp) Xcheck(xs, xp); Xput(xs, xp, c); } - Xcheck(cs, cp); + Xcheck(cs, ccp); if (expanding) { expanding = 0; if (c == '\n') { @@ -1713,7 +1723,7 @@ c_read(char **wp) pprompt(prompt, 0); } } else if (c != EOF) - Xput(cs, cp, c); + Xput(cs, ccp, c); continue; } if (expande && c == '\\') { @@ -1723,31 +1733,33 @@ c_read(char **wp) if (c == '\n' || c == EOF) break; if (ctype(c, C_IFS)) { - if (Xlength(cs, cp) == 0 && ctype(c, C_IFSWS)) + if (Xlength(cs, ccp) == 0 && ctype(c, C_IFSWS)) continue; if (wp[1]) break; } - Xput(cs, cp, c); + Xput(cs, ccp, c); } /* strip trailing IFS white space from last variable */ if (!wp[1]) - while (Xlength(cs, cp) && ctype(cp[-1], C_IFS) && - ctype(cp[-1], C_IFSWS)) - cp--; - Xput(cs, cp, '\0'); + while (Xlength(cs, ccp) && ctype(ccp[-1], C_IFS) && + ctype(ccp[-1], C_IFSWS)) + ccp--; + Xput(cs, ccp, '\0'); vp = global(*wp); /* Must be done before setting export. */ if (vp->flag & RDONLY) { shf_flush(shf); bi_errorf("%s is read only", *wp); + afreechk(wpalloc); return 1; } if (Flag(FEXPORT)) typeset(*wp, EXPORT, 0, 0, 0); - if (!setstr(vp, Xstring(cs, cp), KSH_RETURN_ERROR)) { - shf_flush(shf); - return 1; + if (!setstr(vp, Xstring(cs, ccp), KSH_RETURN_ERROR)) { + shf_flush(shf); + afreechk(wpalloc); + return 1; } } @@ -1765,11 +1777,12 @@ c_read(char **wp) if (c == EOF && !ecode) coproc_read_close(fd); + afreechk(wpalloc); return ecode ? ecode : c == EOF; } int -c_eval(char **wp) +c_eval(const char **wp) { struct source *s, *saves = source; int savef, rv; @@ -1814,10 +1827,10 @@ c_eval(char **wp) } int -c_trap(char **wp) +c_trap(const char **wp) { int i; - char *s; + const char *s; Trap *p; if (ksh_getopt(wp, &builtin_opt, null) == '?') @@ -1861,11 +1874,11 @@ c_trap(char **wp) } int -c_exitreturn(char **wp) +c_exitreturn(const char **wp) { int how = LEXIT; int n; - char *arg; + const char *arg; if (ksh_getopt(wp, &builtin_opt, null) == '?') return 1; @@ -1903,11 +1916,11 @@ c_exitreturn(char **wp) } int -c_brkcont(char **wp) +c_brkcont(const char **wp) { int n, quit; struct env *ep, *last_ep = NULL; - char *arg; + const char *arg; if (ksh_getopt(wp, &builtin_opt, null) == '?') return 1; @@ -1956,15 +1969,14 @@ c_brkcont(char **wp) } int -c_set(char **wp) +c_set(const char **wp) { int argi, setargs; struct block *l = e->loc; - char **owp = wp; + const char **owp = wp; if (wp[1] == NULL) { - static char args0[] = "set", args1[] = "-", - *args[] = { args0, args1, NULL }; + static const char *args [] = { "set", "-", NULL }; return c_typeset(args); } @@ -1978,7 +1990,8 @@ c_set(char **wp) while (*++wp != NULL) *wp = str_save(*wp, &l->area); l->argc = wp - owp - 1; - l->argv = (char **) alloc(sizeofN(char *, l->argc+2), &l->area); + l->argv = (const char **) alloc(sizeofN(char *, l->argc+2), + &l->area); for (wp = l->argv; (*wp++ = *owp++) != NULL; ) ; } @@ -1992,9 +2005,9 @@ c_set(char **wp) } int -c_unset(char **wp) +c_unset(const char **wp) { - char *id; + const char *id; int optc, unset_var = 1; int ret = 0; @@ -2042,7 +2055,7 @@ p_time(struct shf *shf, int posix, struct timeval *tv, int width, } int -c_times(char **wp __unused) +c_times(const char **wp __unused) { struct rusage usage; @@ -2147,7 +2160,7 @@ timex_hook(struct op *t, char **volatile *app) ksh_getopt_reset(&opt, 0); opt.optind = 0; /* start at the start */ - while ((optc = ksh_getopt(wp, &opt, ":p")) != -1) + while ((optc = ksh_getopt((const char **)wp, &opt, ":p")) != -1) switch (optc) { case 'p': t->str[0] |= TF_POSIX; @@ -2172,7 +2185,7 @@ timex_hook(struct op *t, char **volatile *app) /* exec with no args - args case is taken care of in comexec() */ int -c_exec(char **wp __unused) +c_exec(const char **wp __unused) { int i; @@ -2192,11 +2205,11 @@ c_exec(char **wp __unused) #if !defined(MKSH_SMALL) || defined(MKSH_NEED_MKNOD) static int -c_mknod(char **wp) +c_mknod(const char **wp) { int argc, optc, rv = 0; bool ismkfifo = false; - char **argv; + const char **argv; void *set = NULL; mode_t mode = 0, oldmode = 0; @@ -2278,7 +2291,7 @@ c_mknod(char **wp) /* dummy function, special case in comexec() */ int -c_builtin(char **wp __unused) +c_builtin(const char **wp __unused) { return 0; } @@ -2401,7 +2414,7 @@ static int ptest_eval(Test_env *, Test_op, const char *, static void ptest_error(Test_env *, int, const char *); int -c_test(char **wp) +c_test(const char **wp) { int argc; int res; @@ -2432,7 +2445,7 @@ c_test(char **wp) * our parser does the right thing for the omitted steps. */ if (argc <= 5) { - char **owp = wp; + const char **owp = wp; int invert = 0; Test_op op; const char *opnd1, *opnd2; @@ -2834,7 +2847,7 @@ ptest_error(Test_env *te, int offset, const char *msg) #define HARD 0x2 int -c_ulimit(char **wp) +c_ulimit(const char **wp) { static const struct limits { const char *name; diff --git a/histrap.c b/histrap.c index 4e97322..4f170b1 100644 --- a/histrap.c +++ b/histrap.c @@ -3,7 +3,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.43 2007/03/03 21:36:07 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.44 2007/03/04 00:13:16 tg Exp $"); Trap sigtraps[NSIG + 1]; static struct sigaction Sigact_ign, Sigact_trap; @@ -35,14 +35,15 @@ static int hsize; #endif int -c_fc(char **wp) +c_fc(const char **wp) { struct shf *shf; struct temp *tf = NULL; - char *p, *editor = NULL; + const char *p; + char *editor = NULL; int gflag = 0, lflag = 0, nflag = 0, sflag = 0, rflag = 0; int optc; - char *first = NULL, *last = NULL; + const char *first = NULL, *last = NULL; char **hfirst, **hlast, **hp; if (!Flag(FTALKING_I)) { @@ -107,11 +108,10 @@ c_fc(char **wp) } /* Check for pattern replacement argument */ - if (*wp && **wp && (p = strchr(*wp + 1, '='))) { + if (*wp && **wp && (p = cstrchr(*wp + 1, '='))) { pat = str_save(*wp, ATEMP); - p = pat + (p - *wp); - *p++ = '\0'; - rep = p; + rep = pat + (p - *wp); + *rep++ = '\0'; wp++; } /* Check for search prefix */ @@ -1239,7 +1239,7 @@ restoresigs(void) } void -settrap(Trap *p, char *s) +settrap(Trap *p, const char *s) { sig_t f; diff --git a/main.c b/main.c index 6cb4da6..bb17f8c 100644 --- a/main.c +++ b/main.c @@ -13,7 +13,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/main.c,v 1.71 2007/03/03 21:36:07 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/main.c,v 1.72 2007/03/04 00:13:16 tg Exp $"); extern char **environ; @@ -60,14 +60,14 @@ static const char *initcoms[] = { static int initio_done; int -main(int argc, char *argv[]) +main(int argc, const char *argv[]) { int i; int argi; Source *s; struct block *l; int restricted, errexit; - char **wp; + const char **wp; struct env env; pid_t ppid; struct tbl *vp; @@ -82,8 +82,9 @@ main(int argc, char *argv[]) /* make sure argv[] is sane */ if (!*argv) { - static char empty_argv0[] = "mksh", - *empty_argv[] = { empty_argv0, NULL }; + static const char *empty_argv[] = { + "mksh", NULL + }; argv = empty_argv; argc = 1; @@ -180,7 +181,7 @@ main(int argc, char *argv[]) /* import environment */ if (environ != NULL) - for (wp = environ; *wp != NULL; wp++) + for (wp = (const char **)environ; *wp != NULL; wp++) typeset(*wp, IMPORT | EXPORT, 0, 0, 0); kshpid = procpid = getpid(); @@ -219,7 +220,7 @@ main(int argc, char *argv[]) setint(global("PPID"), (long)ppid); /* execute initialisation statements */ - for (wp = (char **)initcoms; *wp != NULL; wp++) { + for (wp = initcoms; *wp != NULL; wp++) { shcomexec(wp); for (; *wp != NULL; wp++) ; @@ -312,7 +313,7 @@ main(int argc, char *argv[]) l = e->loc; l->argv = &argv[argi - 1]; l->argc = argc - argi; - l->argv[0] = (char *)kshname; + l->argv[0] = kshname; getopts_reset(1); /* Disable during .profile/ENV reading */ @@ -346,12 +347,12 @@ main(int argc, char *argv[]) } if (restricted) { - static const char *const restr_com[] = { + static const char *restr_com[] = { "typeset", "-r", "PATH", "ENV", "SHELL", NULL }; - shcomexec((char **)restr_com); + shcomexec(restr_com); /* After typeset command... */ Flag(FRESTRICTED) = 1; } @@ -369,11 +370,11 @@ main(int argc, char *argv[]) } int -include(const char *name, int argc, char **argv, int intr_ok) +include(const char *name, int argc, const char **argv, int intr_ok) { Source *volatile s = NULL; struct shf *shf; - char **volatile old_argv; + const char **volatile old_argv; volatile int old_argc; int i; @@ -975,7 +976,7 @@ closepipe(int *pv) * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor. */ int -check_fd(char *name, int mode, const char **emsgp) +check_fd(const char *name, int mode, const char **emsgp) { int fd, fl; diff --git a/misc.c b/misc.c index 98b6a8c..ac0fa8a 100644 --- a/misc.c +++ b/misc.c @@ -6,7 +6,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.50 2007/01/26 18:27:34 tg Exp $\t" +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.51 2007/03/04 00:13:16 tg Exp $\t" MKSH_SH_H_ID); #undef USE_CHVT @@ -88,9 +88,9 @@ str_save(const char *s, Area *ap) /* called from XcheckN() to grow buffer */ char * -Xcheck_grow_(XString *xsp, char *xp, unsigned more) +Xcheck_grow_(XString *xsp, const char *xp, unsigned more) { - char *old_beg = xsp->beg; + const char *old_beg = xsp->beg; xsp->len += more > xsp->len ? more : xsp->len; xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap); @@ -268,14 +268,14 @@ change_flag(enum sh_flag f, * non-option arguments, -1 if there is an error. */ int -parse_args(char **argv, +parse_args(const char **argv, int what, /* OF_CMDLINE or OF_SET */ int *setargsp) { static char cmd_opts[NELEM(options) + 5]; /* o:T:\0 */ static char set_opts[NELEM(options) + 6]; /* A:o;s\0 */ char *opts; - char *array = NULL; + const char *array = NULL; Getopt go; int optc, set, sortargs = 0, arrayset = 0; unsigned i; @@ -808,13 +808,13 @@ ksh_getopt_reset(Getopt *go, int flags) * in go->info. */ int -ksh_getopt(char **argv, Getopt *go, const char *optionsp) +ksh_getopt(const char **argv, Getopt *go, const char *optionsp) { char c; char *o; if (go->p == 0 || (c = argv[go->optind - 1][go->p]) == '\0') { - char *arg = argv[go->optind], flag = arg ? *arg : '\0'; + const char *arg = argv[go->optind], flag = arg ? *arg : '\0'; go->p = 1; if (flag == '-' && arg[1] == '-' && arg[2] == '\0') { diff --git a/sh.h b/sh.h index 646c882..30f7060 100644 --- a/sh.h +++ b/sh.h @@ -8,7 +8,7 @@ /* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */ /* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */ -#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.113 2007/03/03 21:36:07 tg Exp $" +#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.114 2007/03/04 00:13:16 tg Exp $" #define MKSH_VERSION "R29 2007/02/16" #if HAVE_SYS_PARAM_H @@ -220,7 +220,7 @@ typedef int32_t Tflag; #define PATH_MAX 1024 /* pathname size */ #endif -EXTERN char *kshname; /* $0 */ +EXTERN const char *kshname; /* $0 */ EXTERN pid_t kshpid; /* $$, shell pid */ EXTERN pid_t procpid; /* pid of executing process */ EXTERN uid_t ksheuid; /* effective uid of shell */ @@ -230,6 +230,25 @@ EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */ EXTERN const char initvsn[] I__("KSH_VERSION=@(#)MIRBSD KSH " MKSH_VERSION); #define KSH_VERSION (initvsn + 16) +/* + * Evil hack for const correctness due to API brokenness + */ +union mksh_cchack { + char *rw; + const char *ro; +}; +union mksh_ccphack { + char **rw; + const char **ro; +}; +#define cstrchr(s,c) __extension__({ \ + union mksh_cchack in, out; \ + \ + in.ro = (s); \ + out.rw = strchr(in.rw, (c)); \ + (out.ro); \ +}) + /* * Area-based allocation built on malloc/free */ @@ -471,7 +490,7 @@ EXTERN int ifs0 I__(' '); /* for "$*" */ typedef struct { int optind; int uoptind;/* what user sees in $OPTIND */ - char *optarg; + const char *optarg; int flags; /* see GF_* */ int info; /* see GI_* */ unsigned int p; /* 0 or index into argv[optind - 1] */ @@ -498,8 +517,8 @@ EXTERN struct coproc coproc; EXTERN sigset_t sm_default, sm_sigchld; /* name of called builtin function (used by error functions) */ -EXTERN char *builtin_argv0; -EXTERN Tflag builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */ +EXTERN const char *builtin_argv0; +EXTERN Tflag builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */ /* current working directory, and size of memory allocated for same */ EXTERN char *current_wd; @@ -604,7 +623,7 @@ struct tbl { /* table item */ union { char *s; /* string */ long i; /* integer */ - int (*f)(char **); /* int function */ + int (*f)(const char **); /* int function */ struct op *t; /* "function" tree */ } val; /* value */ int index; /* index for an array */ @@ -693,7 +712,7 @@ struct arg_info { */ struct block { Area area; /* area to allocate things */ - char **argv; + const char **argv; int argc; int flags; /* see BF_* */ struct table vars; /* local variables */ @@ -726,7 +745,7 @@ EXTERN struct table homedirs; /* homedir() cache */ struct builtin { const char *name; - int (*func)(char **); + int (*func)(const char **); }; extern const struct builtin shbuiltins [], kshbuiltins []; @@ -769,7 +788,7 @@ struct op { short evalflags; /* TCOM: arg expansion eval() flags */ short ksh_func; /* TFUNC: function x (vs x()) */ } u; - char **args; /* arguments to a command */ + const char **args; /* arguments to a command */ char **vars; /* variable assignments */ struct ioword **ioact; /* IO actions (eg, < > >>) */ struct op *left, *right; /* descendents */ @@ -937,7 +956,7 @@ typedef char *XStringP; #define Xsavepos(xs, xp) ((xp) - (xs).beg) #define Xrestpos(xs, xp, n) ((xs).beg + (n)) -char *Xcheck_grow_(XString *, char *, unsigned); +char *Xcheck_grow_(XString *, const char *, unsigned); /* * expandable vector of generic pointers @@ -982,10 +1001,10 @@ struct source { int type; /* input type */ const char *start; /* start of current buffer */ union { - char **strv; /* string [] */ - struct shf *shf; /* shell file */ - struct tbl *tblp; /* alias (SALIAS) */ - char *freeme; /* also for SREREAD */ + const char **strv; /* string [] */ + struct shf *shf; /* shell file */ + struct tbl *tblp; /* alias (SALIAS) */ + char *freeme; /* also for SREREAD */ } u; char ugbuf[2]; /* buffer for ungetsc() (SREREAD) and * alias (SALIAS) */ @@ -1101,6 +1120,14 @@ void afreeall(Area *); void *alloc(size_t, Area *); void *aresize(void *, size_t, Area *); void afree(void *, Area *); +#define afreechk(s) do { \ + if (s) \ + afree(s, ATEMP); \ +} while (0) +#define afreechv(v,s) do { \ + if (v) \ + afree(s, ATEMP); \ +} while (0) /* edit.c */ void x_init(void); int x_read(char *, size_t); @@ -1110,63 +1137,63 @@ int utf_widthadj(const char *, const char **); #define utf_width(x) utf_widthadj(x, NULL); /* eval.c */ char *substitute(const char *, int); -char **eval(char **, int); -char *evalstr(char *cp, int); -char *evalonestr(char *cp, int); +char **eval(const char **, int); +char *evalstr(const char *cp, int); +char *evalonestr(const char *cp, int); char *debunk(char *, const char *, size_t); -void expand(char *, XPtrV *, int); +void expand(const char *, XPtrV *, int); int glob_str(char *, XPtrV *, int); /* exec.c */ int execute(struct op * volatile, volatile int); -int shcomexec(char **); +int shcomexec(const char **); struct tbl *findfunc(const char *, unsigned int, int); int define(const char *, struct op *); -void builtin(const char *, int (*)(char **)); +void builtin(const char *, int (*)(const char **)); struct tbl *findcom(const char *, int); void flushcom(int); const char *search(const char *, const char *, int, int *); int search_access(const char *, int, int *); -int pr_menu(char *const *); +int pr_menu(const char *const *); int pr_list(char *const *); /* expr.c */ int evaluate(const char *, long *, int, bool); int v_evaluate(struct tbl *, const char *, volatile int, bool); /* funcs.c */ -int c_hash(char **); -int c_cd(char **); -int c_pwd(char **); -int c_print(char **); -int c_whence(char **); -int c_command(char **); -int c_typeset(char **); -int c_alias(char **); -int c_unalias(char **); -int c_let(char **); -int c_jobs(char **); -int c_fgbg(char **); -int c_kill(char **); +int c_hash(const char **); +int c_cd(const char **); +int c_pwd(const char **); +int c_print(const char **); +int c_whence(const char **); +int c_command(const char **); +int c_typeset(const char **); +int c_alias(const char **); +int c_unalias(const char **); +int c_let(const char **); +int c_jobs(const char **); +int c_fgbg(const char **); +int c_kill(const char **); void getopts_reset(int); -int c_getopts(char **); -int c_bind(char **); -int c_label(char **); -int c_shift(char **); -int c_umask(char **); -int c_dot(char **); -int c_wait(char **); -int c_read(char **); -int c_eval(char **); -int c_trap(char **); -int c_brkcont(char **); -int c_exitreturn(char **); -int c_set(char **); -int c_unset(char **); -int c_ulimit(char **); -int c_times(char **); +int c_getopts(const char **); +int c_bind(const char **); +int c_label(const char **); +int c_shift(const char **); +int c_umask(const char **); +int c_dot(const char **); +int c_wait(const char **); +int c_read(const char **); +int c_eval(const char **); +int c_trap(const char **); +int c_brkcont(const char **); +int c_exitreturn(const char **); +int c_set(const char **); +int c_unset(const char **); +int c_ulimit(const char **); +int c_times(const char **); int timex(struct op *, int); void timex_hook(struct op *, char ** volatile *); -int c_exec(char **); -int c_builtin(char **); -int c_test(char **); +int c_exec(const char **); +int c_builtin(const char **); +int c_test(const char **); /* histrap.c */ void init_histvec(void); void hist_init(Source *); @@ -1174,7 +1201,7 @@ void hist_init(Source *); void hist_finish(void); #endif void histsave(int, const char *, int); -int c_fc(char **); +int c_fc(const char **); void sethistsize(int); #if HAVE_PERSISTENT_HISTORY void sethistfile(const char *); @@ -1195,7 +1222,7 @@ void runtraps(int intr); void runtrap(Trap *); void cleartraps(void); void restoresigs(void); -void settrap(Trap *, char *); +void settrap(Trap *, const char *); int block_pipe(void); void restore_pipe(int); int setsig(Trap *, sig_t, int); @@ -1225,7 +1252,7 @@ void set_prompt(int, Source *); void pprompt(const char *, int); int promptlen(const char *); /* main.c */ -int include(const char *, int, char **, int); +int include(const char *, int, const char **, int); int command(const char *); int shell(Source *volatile, int volatile); void unwind(int) @@ -1255,7 +1282,7 @@ int savefd(int); void restfd(int, int); void openpipe(int *); void closepipe(int *); -int check_fd(char *, int, const char **); +int check_fd(const char *, int, const char **); void coproc_init(void); void coproc_read_close(int); void coproc_readw_close(int); @@ -1283,7 +1310,7 @@ char *str_nsave(const char *, int, Area *); int option(const char *); char *getoptions(void); void change_flag(enum sh_flag, int, int); -int parse_args(char **, int, int *); +int parse_args(const char **, int, int *); int getn(const char *, int *); int bi_getn(const char *, int *); int gmatchx(const char *, const char *, int); @@ -1291,7 +1318,7 @@ int has_globbing(const char *, const char *); const unsigned char *pat_scan(const unsigned char *, const unsigned char *, int); int xstrcmp(const void *, const void *); void ksh_getopt_reset(Getopt *, int); -int ksh_getopt(char **, Getopt *, const char *); +int ksh_getopt(const char **, Getopt *, const char *); void print_value_quoted(const char *); void print_columns(struct shf *, int, char *(*)(const void *, int, char *, int), @@ -1362,7 +1389,7 @@ char **makenv(void); void change_random(void); int array_ref_len(const char *); char *arrayname(const char *); -void set_array(const char *, int, char **); +void set_array(const char *, int, const char **); enum Test_op { TO_NONOP = 0, /* non-operator */ @@ -1399,10 +1426,10 @@ typedef struct test_env Test_env; struct test_env { int flags; /* TEF_* */ union { - char **wp; /* used by ptest_* */ + const char **wp;/* used by ptest_* */ XPtrV *av; /* used by dbtestp_* */ } pos; - char **wp_end; /* used by ptest_* */ + const char **wp_end; /* used by ptest_* */ int (*isa)(Test_env *, Test_meta); const char *(*getopnd) (Test_env *, Test_op, int); int (*eval)(Test_env *, Test_op, const char *, const char *, int); diff --git a/syn.c b/syn.c index 041f669..a8bbdf6 100644 --- a/syn.c +++ b/syn.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.10 2007/01/17 22:51:47 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.11 2007/03/04 00:13:17 tg Exp $"); struct nesting_state { int start_token; /* token than began nesting (eg, FOR) */ @@ -376,7 +376,7 @@ get_command(int cf) } if (iopn == 0) { - afree((void*) iops, ATEMP); + afree((void*)iops, ATEMP); t->ioact = NULL; } else { iops[iopn++] = NULL; @@ -387,7 +387,7 @@ get_command(int cf) if (t->type == TCOM || t->type == TDBRACKET) { XPput(args, NULL); - t->args = (char **) XPclose(args); + t->args = (const char **)XPclose(args); XPput(vars, NULL); t->vars = (char **) XPclose(vars); } else { @@ -549,6 +549,7 @@ function_body(char *name, old_func_parse = e->flags & EF_FUNC_PARSE; e->flags |= EF_FUNC_PARSE; if ((t->left = get_command(CONTIN)) == NULL) { + char *tv; /* * Probably something like foo() followed by eof or ;. * This is accepted by sh and ksh88. @@ -556,13 +557,13 @@ function_body(char *name, * be used as input), we pretend there is a colon here. */ t->left = newtp(TCOM); - t->left->args = (char **) alloc(sizeof(char *) * 2, ATEMP); - t->left->args[0] = alloc(sizeof(char) * 3, ATEMP); - t->left->args[0][0] = CHAR; - t->left->args[0][1] = ':'; - t->left->args[0][2] = EOS; + t->left->args = (const char **)alloc(sizeof(char *) * 2, ATEMP); + t->left->args[0] = tv = alloc(sizeof(char) * 3, ATEMP); + tv[0] = CHAR; + tv[1] = ':'; + tv[2] = EOS; t->left->args[1] = NULL; - t->left->vars = (char **) alloc(sizeof(char *), ATEMP); + t->left->vars = (char **)alloc(sizeof(char *), ATEMP); t->left->vars[0] = NULL; t->left->lineno = 1; } @@ -739,10 +740,11 @@ newtp(int type) { struct op *t; - t = (struct op *) alloc(sizeof(*t), ATEMP); + t = (struct op *)alloc(sizeof(*t), ATEMP); t->type = type; t->u.evalflags = 0; - t->args = t->vars = NULL; + t->args = NULL; + t->vars = NULL; t->ioact = NULL; t->left = t->right = NULL; t->str = NULL; diff --git a/tree.c b/tree.c index 0a11d35..dfbf056 100644 --- a/tree.c +++ b/tree.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.7 2006/11/12 14:58:16 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.8 2007/03/04 00:13:17 tg Exp $"); #define INDENT 4 @@ -18,11 +18,10 @@ static void iofree(struct ioword **, Area *); /* * print a command tree */ - static void ptree(struct op *t, int indent, struct shf *shf) { - char **w; + const char **w; struct ioword **ioact; struct op *t1; @@ -32,7 +31,7 @@ ptree(struct op *t, int indent, struct shf *shf) switch (t->type) { case TCOM: if (t->vars) - for (w = t->vars; *w != NULL; ) + for (w = (const char **)t->vars; *w != NULL; ) fptreef(shf, indent, "%S ", *w++); else fptreef(shf, indent, "#no-vars# "); @@ -83,7 +82,7 @@ ptree(struct op *t, int indent, struct shf *shf) fptreef(shf, indent, "for %s ", t->str); if (t->vars != NULL) { fptreef(shf, indent, "in "); - for (w = t->vars; *w; ) + for (w = (const char **)t->vars; *w; ) fptreef(shf, indent, "%S ", *w++); fptreef(shf, indent, "%;"); } @@ -94,7 +93,7 @@ ptree(struct op *t, int indent, struct shf *shf) fptreef(shf, indent, "case %S in", t->str); for (t1 = t->left; t1 != NULL; t1 = t1->right) { fptreef(shf, indent, "%N("); - for (w = t1->vars; *w != NULL; w++) + for (w = (const char **)t1->vars; *w != NULL; w++) fptreef(shf, indent, "%S%c", *w, (w[1] != NULL) ? '|' : ')'); fptreef(shf, indent + INDENT, "%;%T%N;;", t1->left); @@ -240,7 +239,6 @@ pioact(struct shf *shf, int indent, struct ioword *iop) /* * variants of fputc, fputs for ptreef and snptreef */ - static void tputC(int c, struct shf *shf) { @@ -419,12 +417,12 @@ vfptreef(struct shf *shf, int indent, const char *fmt, va_list va) /* * copy tree (for function definition) */ - struct op * tcopy(struct op *t, Area *ap) { struct op *r; - char **tw, **rw; + const char **tw; + char **rw; if (t == NULL) return NULL; @@ -439,11 +437,11 @@ tcopy(struct op *t, Area *ap) if (t->vars == NULL) r->vars = NULL; else { - for (tw = t->vars; *tw++ != NULL; ) + for (tw = (const char **)t->vars; *tw++ != NULL; ) ; rw = r->vars = (char **) - alloc((tw - t->vars + 1) * sizeof(*tw), ap); - for (tw = t->vars; *tw != NULL; ) + alloc((tw - (const char **)t->vars + 1) * sizeof(*tw), ap); + for (tw = (const char **)t->vars; *tw != NULL; ) *rw++ = wdcopy(*tw++, ap); *rw = NULL; } @@ -453,8 +451,8 @@ tcopy(struct op *t, Area *ap) else { for (tw = t->args; *tw++ != NULL; ) ; - rw = r->args = (char **) - alloc((tw - t->args + 1) * sizeof(*tw), ap); + r->args = (const char **)(rw = (char **) + alloc((tw - t->args + 1) * sizeof(*tw), ap)); for (tw = t->args; *tw != NULL; ) *rw++ = wdcopy(*tw++, ap); *rw = NULL; @@ -596,7 +594,7 @@ wdstrip(const char *wp) } } -static struct ioword ** +static struct ioword ** iocopy(struct ioword **iow, Area *ap) { struct ioword **ior; @@ -628,7 +626,6 @@ iocopy(struct ioword **iow, Area *ap) /* * free tree (for function definition) */ - void tfree(struct op *t, Area *ap) { @@ -647,7 +644,10 @@ tfree(struct op *t, Area *ap) } if (t->args != NULL) { - for (w = t->args; *w != NULL; w++) + union mksh_ccphack cw; + /* XXX we assume the caller is right */ + cw.ro = t->args; + for (w = cw.rw; *w != NULL; w++) afree((void*)*w, ap); afree((void*)t->args, ap); } @@ -661,7 +661,7 @@ tfree(struct op *t, Area *ap) afree((void*)t, ap); } -static void +static void iofree(struct ioword **iow, Area *ap) { struct ioword **iop; diff --git a/var.c b/var.c index 9fcf2ec..568e0ab 100644 --- a/var.c +++ b/var.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/var.c,v 1.35 2007/03/03 21:36:08 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/var.c,v 1.36 2007/03/04 00:13:17 tg Exp $"); /* * Variables @@ -32,7 +32,7 @@ void newblock(void) { struct block *l; - static char *empty[] = { null }; + static const char *empty[] = { null }; l = (struct block *) alloc(sizeof(struct block), ATEMP); l->flags = 0; @@ -1149,7 +1149,7 @@ arrayname(const char *str) /* Set (or overwrite, if !reset) the array variable var to the values in vals. */ void -set_array(const char *var, int reset, char **vals) +set_array(const char *var, int reset, const char **vals) { struct tbl *vp, *vq; int i;