merge the const branch +- a few

This commit is contained in:
tg 2007-03-04 00:13:17 +00:00
parent 90af366ee0
commit 62b347a1b0
12 changed files with 358 additions and 300 deletions

View File

@ -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 <bsd.own.mk> .include <bsd.own.mk>
@ -22,7 +22,6 @@ CPPFLAGS+= -DMKSH_ASSUME_UTF8 \
-DHAVE_MULTI_IDSTRING=1 -DHAVE_PERSISTENT_HISTORY=1 -DHAVE_MULTI_IDSTRING=1 -DHAVE_PERSISTENT_HISTORY=1
COPTS+= -std=gnu99 -Wall COPTS+= -std=gnu99 -Wall
.endif .endif
CDIAGFLAGS+= -Wno-cast-qual
LINKS+= ${BINDIR}/${PROG} ${BINDIR}/sh LINKS+= ${BINDIR}/${PROG} ${BINDIR}/sh
MLINKS+= ${PROG}.1 sh.1 MLINKS+= ${PROG}.1 sh.1

5
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h" #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 */ /* tty driver characters we are interested in */
typedef struct { typedef struct {
@ -602,8 +602,7 @@ x_free_words(int nwords, char **words)
int i; int i;
for (i = 0; i < nwords; i++) for (i = 0; i < nwords; i++)
if (words[i]) afreechk(words[i])
afree(words[i], ATEMP);
afree(words, ATEMP); afree(words, ATEMP);
} }

78
eval.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #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 #ifdef MKSH_SMALL
#define MKSH_NOPWNAM #define MKSH_NOPWNAM
@ -40,12 +40,12 @@ typedef struct Expand {
#define IFS_WS 1 /* have seen IFS white-space */ #define IFS_WS 1 /* have seen IFS white-space */
#define IFS_NWS 2 /* have seen IFS non-white-space */ #define IFS_NWS 2 /* have seen IFS non-white-space */
static int varsub(Expand *, char *, char *, int *, int *); static int varsub(Expand *, const char *, const char *, int *, int *);
static int comsub(Expand *, char *); static int comsub(Expand *, const char *);
static char *trimsub(char *, char *, int); static char *trimsub(char *, char *, int);
static void glob(char *, XPtrV *, int); static void glob(char *, XPtrV *, int);
static void globit(XString *, char **, 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 *); static char *tilde(char *);
#ifndef MKSH_NOPWNAM #ifndef MKSH_NOPWNAM
static char *homedir(char *); static char *homedir(char *);
@ -73,12 +73,16 @@ substitute(const char *cp, int f)
* expand arg-list * expand arg-list
*/ */
char ** char **
eval(char **ap, int f) eval(const char **ap, int f)
{ {
XPtrV w; XPtrV w;
if (*ap == NULL) if (*ap == NULL) {
return ap; union mksh_ccphack vap;
vap.ro = ap;
return (vap.rw);
}
XPinit(w, 32); XPinit(w, 32);
XPput(w, NULL); /* space for shell name */ XPput(w, NULL); /* space for shell name */
while (*ap != NULL) while (*ap != NULL)
@ -91,15 +95,16 @@ eval(char **ap, int f)
* expand string * expand string
*/ */
char * char *
evalstr(char *cp, int f) evalstr(const char *cp, int f)
{ {
XPtrV w; XPtrV w;
char *dp;
XPinit(w, 1); XPinit(w, 1);
expand(cp, &w, f); expand(cp, &w, f);
cp = (XPsize(w) == 0) ? null : (char*) *XPptrv(w); dp = (XPsize(w) == 0) ? null : (char*) *XPptrv(w);
XPfree(w); XPfree(w);
return cp; return (dp);
} }
/* /*
@ -107,25 +112,26 @@ evalstr(char *cp, int f)
* used from iosetup to expand redirection files * used from iosetup to expand redirection files
*/ */
char * char *
evalonestr(char *cp, int f) evalonestr(const char *cp, int f)
{ {
XPtrV w; XPtrV w;
char *rv;
XPinit(w, 1); XPinit(w, 1);
expand(cp, &w, f); expand(cp, &w, f);
switch (XPsize(w)) { switch (XPsize(w)) {
case 0: case 0:
cp = null; rv = null;
break; break;
case 1: case 1:
cp = (char*) *XPptrv(w); rv = (char *) *XPptrv(w);
break; break;
default: default:
cp = evalstr(cp, f&~DOGLOB); rv = evalstr(cp, f&~DOGLOB);
break; break;
} }
XPfree(w); XPfree(w);
return cp; return (rv);
} }
/* for nested substitution: ${var:=$var2} */ /* for nested substitution: ${var:=$var2} */
@ -140,7 +146,7 @@ typedef struct SubType {
} SubType; } SubType;
void void
expand(char *cp, /* input word */ expand(const char *cp, /* input word */
XPtrV *wp, /* output words */ XPtrV *wp, /* output words */
int f) /* DO* flags */ int f) /* DO* flags */
{ {
@ -148,7 +154,8 @@ expand(char *cp, /* input word */
int type; /* expansion type */ int type; /* expansion type */
int quote = 0; /* quoted */ int quote = 0; /* quoted */
XString ds; /* destination string */ 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 fdo, word; /* second pass flags; have word */
int doblank; /* field splitting of parameter/command subst */ int doblank; /* field splitting of parameter/command subst */
Expand x = { /* expansion variables */ Expand x = { /* expansion variables */
@ -262,26 +269,25 @@ expand(char *cp, /* input word */
* This is where all syntax checking gets done... * 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 stype;
int slen = 0; int slen = 0;
sp = strchr(sp, '\0') + 1; /* skip variable */ sp = strchr(sp, '\0') + 1; /* skip variable */
type = varsub(&x, varname, sp, &stype, &slen); type = varsub(&x, varname, sp, &stype, &slen);
if (type < 0) { if (type < 0) {
char endc; char *beg, *end, *str;
char *str, *end;
sp = varname - 2; /* restore sp */ 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 */ /* ({) the } or x is already skipped */
endc = *end;
*end = EOS; *end = EOS;
str = snptreef(NULL, 64, "%S", sp); str = snptreef(NULL, 64, "%S", beg);
*end = endc; afree(beg, ATEMP);
errorf("%s: bad substitution", str); errorf("%s: bad substitution", str);
} }
if (f&DOBLANK) if (f & DOBLANK)
doblank++; doblank++;
tilde_ok = 0; tilde_ok = 0;
if (type == XBASE) { /* expand? */ if (type == XBASE) { /* expand? */
@ -639,7 +645,8 @@ expand(char *cp, /* input word */
if (type == XBASE && if (type == XBASE &&
(f & (DOTILDE|DOASNTILDE)) && (f & (DOTILDE|DOASNTILDE)) &&
(tilde_ok & 2)) { (tilde_ok & 2)) {
char *p, *dp_x; const char *p;
char *dp_x;
dp_x = dp; dp_x = dp;
p = maybe_expand_tilde(sp, p = maybe_expand_tilde(sp,
@ -676,7 +683,7 @@ expand(char *cp, /* input word */
* Prepare to generate the string returned by ${} substitution. * Prepare to generate the string returned by ${} substitution.
*/ */
static int static int
varsub(Expand *xp, char *sp, char *word, varsub(Expand *xp, const char *sp, const char *word,
int *stypep, /* becomes qualifier type */ int *stypep, /* becomes qualifier type */
int *slenp) /* " " len (=, :=, etc.) valid iff *stypep != 0 */ 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 state; /* next state: XBASE, XARG, XSUB, XNULLSUB */
int stype; /* substitution type */ int stype; /* substitution type */
int slen; int slen;
char *p; const char *p;
struct tbl *vp; struct tbl *vp;
if (sp[0] == '\0') /* Bad variable name */ if (sp[0] == '\0') /* Bad variable name */
@ -701,7 +708,8 @@ varsub(Expand *xp, char *sp, char *word,
return -1; return -1;
sp++; sp++;
/* Check for size of array */ /* 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 n = 0;
int max = 0; int max = 0;
@ -772,7 +780,8 @@ varsub(Expand *xp, char *sp, char *word,
state = XARG; state = XARG;
} }
} else { } else {
if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') { if ((p = cstrchr(sp, '[')) && (p[1] == '*' || p[1] == '@') &&
p[2] == ']') {
XPtrV wv; XPtrV wv;
switch (stype & 0x7f) { switch (stype & 0x7f) {
@ -827,7 +836,7 @@ varsub(Expand *xp, char *sp, char *word,
* Run the command in $(...) and read its output. * Run the command in $(...) and read its output.
*/ */
static int static int
comsub(Expand *xp, char *cp) comsub(Expand *xp, const char *cp)
{ {
Source *s, *sold; Source *s, *sold;
struct op *t; 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 * puts the expanded version in *dcp,dp and returns a pointer in p just
* past the name, otherwise returns 0. * past the name, otherwise returns 0.
*/ */
static char * static const char *
maybe_expand_tilde(char *p, XString *dsp, char **dpp, int isassign) maybe_expand_tilde(const char *p, XString *dsp, char **dpp, int isassign)
{ {
XString ts; XString ts;
char *dp = *dpp; char *dp = *dpp;
char *tp, *r; char *tp;
const char *r;
Xinit(ts, tp, 16, ATEMP); Xinit(ts, tp, 16, ATEMP);
/* : only for DOASNTILDE form */ /* : only for DOASNTILDE form */

111
exec.c
View File

@ -2,22 +2,20 @@
#include "sh.h" #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 **, static int comexec(struct op *, struct tbl *volatile, const char **,
int volatile); int volatile);
static void scriptexec(struct op *, char **) static void scriptexec(struct op *, const char **)
__attribute__((noreturn)); __attribute__((noreturn));
static int call_builtin(struct tbl *, char **); static int call_builtin(struct tbl *, const char **);
static int iosetup(struct ioword *, struct tbl *); static int iosetup(struct ioword *, struct tbl *);
static int herein(const char *, int); static int herein(const char *, int);
static char *do_selectargs(char **, bool); static const char *do_selectargs(const char **, bool);
static int dbteste_isa(Test_env *, Test_meta); static int dbteste_isa(Test_env *, Test_meta);
static const char *dbteste_getopnd(Test_env *, Test_op, int); static const char *dbteste_getopnd(Test_env *, Test_op, int);
static int dbteste_eval(Test_env *, Test_op, const char *, const char *, static int dbteste_eval(Test_env *, Test_op, const char *, const char *, int);
int); static void dbteste_error(Test_env *, int, const char *);
static void dbteste_error(Test_env *, int, const char *);
/* /*
* execute command tree * execute command tree
@ -29,8 +27,9 @@ execute(struct op *volatile t,
int i; int i;
volatile int rv = 0; volatile int rv = 0;
int pv[2]; int pv[2];
char ** volatile ap; const char ** volatile ap;
char *s, *cp; char ** volatile up;
const char *s, *cp;
struct ioword **iowp; struct ioword **iowp;
struct tbl *tp = NULL; struct tbl *tp = NULL;
@ -63,10 +62,11 @@ execute(struct op *volatile t,
/* POSIX says expand command words first, then redirections, /* POSIX says expand command words first, then redirections,
* and assignments last.. * 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) if (flags & XTIME)
/* Allow option parsing (bizarre, but POSIX) */ /* Allow option parsing (bizarre, but POSIX) */
timex_hook(t, &ap); timex_hook(t, &up);
ap = (const char **)up;
if (Flag(FXTRACE) && ap[0]) { if (Flag(FXTRACE) && ap[0]) {
shf_fprintf(shl_out, "%s", shf_fprintf(shl_out, "%s",
substitute(str_val(global("PS4")), 0)); substitute(str_val(global("PS4")), 0));
@ -104,7 +104,7 @@ execute(struct op *volatile t,
switch (t->type) { switch (t->type) {
case TCOM: case TCOM:
rv = comexec(t, tp, ap, flags); rv = comexec(t, tp, (const char **)ap, flags);
break; break;
case TPAREN: case TPAREN:
@ -245,8 +245,9 @@ execute(struct op *volatile t,
case TSELECT: case TSELECT:
{ {
volatile bool is_first = true; volatile bool is_first = true;
ap = (t->vars != NULL) ? eval(t->vars, DOBLANK|DOGLOB|DOTILDE) : ap = (t->vars == NULL) ? e->loc->argv + 1 :
e->loc->argv + 1; (const char **)eval((const char **)t->vars,
DOBLANK | DOGLOB | DOTILDE);
e->type = E_LOOP; e->type = E_LOOP;
while (1) { while (1) {
i = sigsetjmp(e->jbuf, 0); i = sigsetjmp(e->jbuf, 0);
@ -314,7 +315,7 @@ execute(struct op *volatile t,
case TCASE: case TCASE:
cp = evalstr(t->str, DOTILDE); cp = evalstr(t->str, DOTILDE);
for (t = t->left; t != NULL && t->type == TPAT; t = t->right) 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)) && if ((s = evalstr(*ap, DOTILDE|DOPAT)) &&
gmatchx(cp, s, false)) gmatchx(cp, s, false))
goto Found; goto Found;
@ -340,12 +341,17 @@ execute(struct op *volatile t,
case TEXEC: /* an eval'd TCOM */ case TEXEC: /* an eval'd TCOM */
s = t->args[0]; s = t->args[0];
ap = makenv(); up = makenv();
restoresigs(); restoresigs();
cleanup_proc_env(); 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) if (errno == ENOEXEC)
scriptexec(t, ap); scriptexec(t, (const char **)up);
else else
errorf("%s: %s", s, strerror(errno)); errorf("%s: %s", s, strerror(errno));
} }
@ -368,12 +374,13 @@ execute(struct op *volatile t,
*/ */
static int 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; int i;
volatile int rv = 0; volatile int rv = 0;
char *cp; const char *cp;
char **lastp; const char **lastp;
static struct op texec; /* Must be static (XXX but why?) */ static struct op texec; /* Must be static (XXX but why?) */
int type_flags; int type_flags;
int keepasn_ok; int keepasn_ok;
@ -503,14 +510,14 @@ comexec(struct op *t, struct tbl *volatile tp, char **ap, volatile int flags)
switch (tp->type) { switch (tp->type) {
case CSHELL: /* shell built-in */ case CSHELL: /* shell built-in */
rv = call_builtin(tp, ap); rv = call_builtin(tp, (const char **)ap);
break; break;
case CFUNC: /* function call */ case CFUNC: /* function call */
{ {
volatile int old_xflag; volatile int old_xflag;
volatile Tflag old_inuse; volatile Tflag old_inuse;
char *volatile old_kshname; const char *volatile old_kshname;
if (!(tp->flag & ISSET)) { if (!(tp->flag & ISSET)) {
struct tbl *ftp; struct tbl *ftp;
@ -555,7 +562,7 @@ comexec(struct op *t, struct tbl *volatile tp, char **ap, volatile int flags)
if (tp->flag & FKSH) if (tp->flag & FKSH)
kshname = ap[0]; kshname = ap[0];
else else
ap[0] = (char *) kshname; ap[0] = kshname;
e->loc->argv = ap; e->loc->argv = ap;
for (i = 0; *ap++ != NULL; i++) 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 static void
scriptexec(struct op *tp, char **ap) scriptexec(struct op *tp, const char **ap)
{ {
static char execshell[] = "/bin/sh";
const char *sh; const char *sh;
union mksh_ccphack args, cap;
sh = str_val(global("EXECSHELL")); sh = str_val(global("EXECSHELL"));
if (sh && *sh) if (sh && *sh)
sh = search(sh, path, X_OK, NULL); sh = search(sh, path, X_OK, NULL);
if (!sh || !*sh) if (!sh || !*sh)
sh = execshell; sh = "/bin/sh";
*tp->args-- = tp->str; *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 */ /* report both the program that was run and the bogus shell */
errorf("%s: %s: %s", tp->str, sh, strerror(errno)); errorf("%s: %s: %s", tp->str, sh, strerror(errno));
} }
int int
shcomexec(char **wp) shcomexec(const char **wp)
{ {
struct tbl *tp; struct tbl *tp;
tp = ktsearch(&builtins, *wp, hash(*wp)); tp = ktsearch(&builtins, *wp, hash(*wp));
if (tp == NULL) if (tp == NULL)
internal_errorf(1, "shcomexec: %s", *wp); 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 * add builtin
*/ */
void void
builtin(const char *name, int (*func) (char **)) builtin(const char *name, int (*func) (const char **))
{ {
struct tbl *tp; struct tbl *tp;
Tflag flag; Tflag flag;
@ -970,7 +979,7 @@ search(const char *name, const char *lpath,
} }
static int static int
call_builtin(struct tbl *tp, char **wp) call_builtin(struct tbl *tp, const char **wp)
{ {
int rv; int rv;
@ -1193,13 +1202,11 @@ herein(const char *content, int sub)
* ksh special - the select command processing section * ksh special - the select command processing section
* print the args in column form - assuming that we can * print the args in column form - assuming that we can
*/ */
static char * static const char *
do_selectargs(char **ap, bool print_menu) do_selectargs(const char **ap, bool print_menu)
{ {
static char read_args0[] = "read", static const char *read_args[] = {
read_args1[] = "-r", read_args2[] = "REPLY", "read", "-r", "REPLY", NULL
*read_args[] = {
read_args0, read_args1, read_args2, NULL
}; };
char *s; char *s;
int i, argct; int i, argct;
@ -1227,9 +1234,9 @@ do_selectargs(char **ap, bool print_menu)
} }
struct select_menu_info { struct select_menu_info {
char *const *args; const char *const *args;
int arg_width; int arg_width;
int num_width; int num_width;
}; };
static char *select_fmt_entry(const void *, int, char *, int); 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 * print a select style menu
*/ */
int int
pr_menu(char *const *ap) pr_menu(const char *const *ap)
{ {
struct select_menu_info smi; struct select_menu_info smi;
char *const *pp; const char *const *pp;
int nwidth, dwidth; int nwidth, dwidth;
int i, n; int i, n;
@ -1331,7 +1338,7 @@ dbteste_isa(Test_env *te, Test_meta meta)
{ {
int ret = 0; int ret = 0;
int uqword; int uqword;
char *p; const char *p;
if (!*te->pos.wp) if (!*te->pos.wp)
return meta == TM_END; return meta == TM_END;
@ -1367,7 +1374,7 @@ dbteste_isa(Test_env *te, Test_meta meta)
static const char * static const char *
dbteste_getopnd(Test_env *te, Test_op op, int do_eval) dbteste_getopnd(Test_env *te, Test_op op, int do_eval)
{ {
char *s = *te->pos.wp; const char *s = *te->pos.wp;
if (!s) if (!s)
return NULL; return NULL;

185
funcs.c
View File

@ -5,10 +5,10 @@
#include "sh.h" #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 int
c_cd(char **wp) c_cd(const char **wp)
{ {
int optc; int optc;
int physical = Flag(FPHYSICAL); int physical = Flag(FPHYSICAL);
@ -21,6 +21,7 @@ c_cd(char **wp)
char *dir, *try, *pwd; char *dir, *try, *pwd;
int phys_path; int phys_path;
char *cdpath; char *cdpath;
bool dir_ = false;
while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1) while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
switch (optc) { switch (optc) {
@ -51,15 +52,17 @@ c_cd(char **wp)
} }
} else if (!wp[1]) { } else if (!wp[1]) {
/* One argument: - or dir */ /* One argument: - or dir */
dir = wp[0]; dir = str_save(wp[0], ATEMP);
if (strcmp(dir, "-") == 0) { if (strcmp(dir, "-") == 0) {
afree(dir, ATEMP);
dir = str_val(oldpwd_s); dir = str_val(oldpwd_s);
if (dir == null) { if (dir == null) {
bi_errorf("no OLDPWD"); bi_errorf("no OLDPWD");
return 1; return 1;
} }
printpath++; printpath++;
} } else
dir_ = true;
} else if (!wp[2]) { } else if (!wp[2]) {
/* Two arguments - substitute arg1 in PWD for arg2 */ /* Two arguments - substitute arg1 in PWD for arg2 */
int ilen, olen, nlen, elen; int ilen, olen, nlen, elen;
@ -83,6 +86,7 @@ c_cd(char **wp)
nlen = strlen(wp[1]); nlen = strlen(wp[1]);
elen = strlen(current_wd + ilen + olen) + 1; elen = strlen(current_wd + ilen + olen) + 1;
dir = alloc(ilen + nlen + elen, ATEMP); dir = alloc(ilen + nlen + elen, ATEMP);
dir_ = true;
memcpy(dir, current_wd, ilen); memcpy(dir, current_wd, ilen);
memcpy(dir + ilen, wp[1], nlen); memcpy(dir + ilen, wp[1], nlen);
memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen); memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
@ -114,6 +118,7 @@ c_cd(char **wp)
bi_errorf("%s: bad directory", dir); bi_errorf("%s: bad directory", dir);
else else
bi_errorf("%s - %s", try, strerror(errno)); bi_errorf("%s - %s", try, strerror(errno));
afreechv(dir_, dir);
return 1; return 1;
} }
@ -147,11 +152,12 @@ c_cd(char **wp)
if (printpath || cdnode) if (printpath || cdnode)
shprintf("%s\n", pwd); shprintf("%s\n", pwd);
afreechv(dir_, dir);
return 0; return 0;
} }
int int
c_pwd(char **wp) c_pwd(const char **wp)
{ {
int optc; int optc;
int physical = Flag(FPHYSICAL); int physical = Flag(FPHYSICAL);
@ -187,7 +193,7 @@ c_pwd(char **wp)
} }
int int
c_print(char **wp) c_print(const char **wp)
{ {
#define PO_NL BIT(0) /* print newline */ #define PO_NL BIT(0) /* print newline */
#define PO_EXPAND BIT(1) /* expand backslash sequences */ #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 */ #define PO_COPROC BIT(4) /* printing to coprocess: block SIGPIPE */
int fd = 1; int fd = 1;
int flags = PO_EXPAND|PO_NL; int flags = PO_EXPAND|PO_NL;
char *s; const char *s;
const char *emsg; const char *emsg;
XString xs; XString xs;
char *xp; char *xp;
@ -375,10 +381,10 @@ c_print(char **wp)
} }
int int
c_whence(char **wp) c_whence(const char **wp)
{ {
struct tbl *tp; struct tbl *tp;
char *id; const char *id;
int pflag = 0, vflag = 0, Vflag = 0; int pflag = 0, vflag = 0, Vflag = 0;
int ret = 0; int ret = 0;
int optc; int optc;
@ -495,7 +501,7 @@ c_whence(char **wp)
/* Deal with command -vV - command -p dealt with in comexec() */ /* Deal with command -vV - command -p dealt with in comexec() */
int int
c_command(char **wp) c_command(const char **wp)
{ {
/* Let c_whence do the work. Note that c_command() must be /* Let c_whence do the work. Note that c_command() must be
* a distinct function from c_whence() (tested in comexec()). * a distinct function from c_whence() (tested in comexec()).
@ -505,14 +511,14 @@ c_command(char **wp)
/* typeset, export, and readonly */ /* typeset, export, and readonly */
int int
c_typeset(char **wp) c_typeset(const char **wp)
{ {
struct block *l = e->loc; struct block *l = e->loc;
struct tbl *vp, **p; struct tbl *vp, **p;
Tflag fset = 0, fclr = 0; Tflag fset = 0, fclr = 0;
int thing = 0, func = 0, localv = 0; int thing = 0, func = 0, localv = 0;
const char *opts = "L#R#UZ#fi#lprtux"; /* see comment below */ const char *opts = "L#R#UZ#fi#lprtux"; /* see comment below */
char *fieldstr, *basestr; const char *fieldstr, *basestr;
int field, base; int field, base;
int optc; int optc;
Tflag flag; Tflag flag;
@ -802,7 +808,7 @@ c_typeset(char **wp)
} }
int int
c_alias(char **wp) c_alias(const char **wp)
{ {
struct table *t = &aliases; struct table *t = &aliases;
int rv = 0, rflag = 0, tflag, Uflag = 0, pflag = 0; 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.. */ /* "hash -r" means reset all the tracked aliases.. */
if (rflag) { if (rflag) {
static char args0[] = "unalias", args1[] = "-ta", static const char *args[] = {
*args[] = { args0, args1, NULL }; "unalias", "-ta", NULL
};
if (!tflag || *wp) { if (!tflag || *wp) {
shprintf("alias: -r flag can only be used with -t" shprintf("alias: -r flag can only be used with -t"
@ -885,14 +892,15 @@ c_alias(char **wp)
} }
for (; *wp != NULL; wp++) { for (; *wp != NULL; wp++) {
char *alias = *wp; const char *alias = *wp;
char *val = strchr(alias, '='); char *xalias = NULL;
const char *val;
const char *newval; const char *newval;
struct tbl *ap; struct tbl *ap;
int h; int h;
if (val) if ((val = cstrchr(alias, '=')))
alias = str_nsave(alias, val++ - alias, ATEMP); alias = xalias = str_nsave(alias, val++ - alias, ATEMP);
h = hash(alias); h = hash(alias);
if (val == NULL && !tflag && !xflag) { if (val == NULL && !tflag && !xflag) {
ap = ktsearch(t, alias, h); ap = ktsearch(t, alias, h);
@ -933,15 +941,14 @@ c_alias(char **wp)
ap->flag &= ~xflag; ap->flag &= ~xflag;
else else
ap->flag |= xflag; ap->flag |= xflag;
if (val) afreechk(xalias);
afree(alias, ATEMP);
} }
return rv; return rv;
} }
int int
c_unalias(char **wp) c_unalias(const char **wp)
{ {
struct table *t = &aliases; struct table *t = &aliases;
struct tbl *ap; struct tbl *ap;
@ -997,7 +1004,7 @@ c_unalias(char **wp)
} }
int int
c_let(char **wp) c_let(const char **wp)
{ {
int rv = 1; int rv = 1;
long val; long val;
@ -1015,7 +1022,7 @@ c_let(char **wp)
} }
int int
c_jobs(char **wp) c_jobs(const char **wp)
{ {
int optc; int optc;
int flag = 0; int flag = 0;
@ -1052,7 +1059,7 @@ c_jobs(char **wp)
} }
int int
c_fgbg(char **wp) c_fgbg(const char **wp)
{ {
int bg = strcmp(*wp, "bg") == 0; int bg = strcmp(*wp, "bg") == 0;
int rv = 0; int rv = 0;
@ -1094,10 +1101,10 @@ kill_fmt_entry(const void *arg, int i, char *buf, int buflen)
int int
c_kill(char **wp) c_kill(const char **wp)
{ {
Trap *t = NULL; Trap *t = NULL;
char *p; const char *p;
int lflag = 0; int lflag = 0;
int i, n, rv, sig; int i, n, rv, sig;
@ -1207,7 +1214,7 @@ getopts_reset(int val)
} }
int int
c_getopts(char **wp) c_getopts(const char **wp)
{ {
int argc; int argc;
const char *opts; const char *opts;
@ -1304,7 +1311,7 @@ c_getopts(char **wp)
} }
int int
c_bind(char **wp) c_bind(const char **wp)
{ {
int optc, rv = 0, macro = 0, list = 0; int optc, rv = 0, macro = 0, list = 0;
char *cp; char *cp;
@ -1369,18 +1376,18 @@ static void p_time(struct shf *, int, struct timeval *, int,
/* :, false and true */ /* :, false and true */
int int
c_label(char **wp) c_label(const char **wp)
{ {
return wp[0][0] == 'f' ? 1 : 0; return wp[0][0] == 'f' ? 1 : 0;
} }
int int
c_shift(char **wp) c_shift(const char **wp)
{ {
struct block *l = e->loc; struct block *l = e->loc;
int n; int n;
long val; long val;
char *arg; const char *arg;
if (ksh_getopt(wp, &builtin_opt, null) == '?') if (ksh_getopt(wp, &builtin_opt, null) == '?')
return 1; return 1;
@ -1406,10 +1413,10 @@ c_shift(char **wp)
} }
int int
c_umask(char **wp) c_umask(const char **wp)
{ {
int i; int i;
char *cp; const char *cp;
int symbolic = 0; int symbolic = 0;
mode_t old_umask; mode_t old_umask;
int optc; int optc;
@ -1427,20 +1434,20 @@ c_umask(char **wp)
old_umask = umask(0); old_umask = umask(0);
umask(old_umask); umask(old_umask);
if (symbolic) { if (symbolic) {
char buf[18]; char buf[18], *p;
int j; int j;
old_umask = ~old_umask; old_umask = ~old_umask;
cp = buf; p = buf;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
*cp++ = "ugo"[i]; *p++ = "ugo"[i];
*cp++ = '='; *p++ = '=';
for (j = 0; j < 3; j++) for (j = 0; j < 3; j++)
if (old_umask & (1 << (8 - (3*i + j)))) if (old_umask & (1 << (8 - (3*i + j))))
*cp++ = "rwx"[j]; *p++ = "rwx"[j];
*cp++ = ','; *p++ = ',';
} }
cp[-1] = '\0'; p[-1] = '\0';
shprintf("%s\n", buf); shprintf("%s\n", buf);
} else } else
shprintf("%#3.3o\n", (unsigned) old_umask); shprintf("%#3.3o\n", (unsigned) old_umask);
@ -1533,11 +1540,11 @@ c_umask(char **wp)
} }
int int
c_dot(char **wp) c_dot(const char **wp)
{ {
const char *file; const char *file;
char *cp; const char *cp;
char **argv; const char **argv;
int argc; int argc;
int i; int i;
int err; int err;
@ -1572,7 +1579,7 @@ c_dot(char **wp)
} }
int int
c_wait(char **wp) c_wait(const char **wp)
{ {
int rv = 0; int rv = 0;
int sig; int sig;
@ -1594,20 +1601,21 @@ c_wait(char **wp)
} }
int int
c_read(char **wp) c_read(const char **wp)
{ {
int c = 0; int c = 0;
int expande = 1, historyr = 0; int expande = 1, historyr = 0;
int expanding; int expanding;
int ecode = 0; int ecode = 0;
char *cp; const char *cp;
char *ccp;
int fd = 0; int fd = 0;
struct shf *shf; struct shf *shf;
int optc; int optc;
const char *emsg; const char *emsg;
XString cs, xs = { NULL, NULL, 0, NULL}; XString cs, xs = { NULL, NULL, 0, NULL};
struct tbl *vp; struct tbl *vp;
char *xp = NULL; char *xp = NULL, *wpalloc = NULL;
static char REPLY[] = "REPLY"; static char REPLY[] = "REPLY";
while ((optc = ksh_getopt(wp, &builtin_opt, "prsu,")) != -1) 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); shf = shf_reopen(fd, SHF_RD | SHF_INTERRUPT | can_seek(fd), shl_spare);
if ((cp = strchr(*wp, '?')) != NULL) { if ((cp = cstrchr(*wp, '?')) != NULL) {
*cp = 0; wpalloc = str_save(*wp, ATEMP);
wpalloc[cp - *wp] = '\0';
*wp = wpalloc;
if (isatty(fd)) { if (isatty(fd)) {
/* at&t ksh says it prints prompt on fd if it's open /* 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 * for writing and is a tty, but it doesn't do it
@ -1671,9 +1681,9 @@ c_read(char **wp)
if (historyr) if (historyr)
Xinit(xs, xp, 128, ATEMP); Xinit(xs, xp, 128, ATEMP);
expanding = 0; expanding = 0;
Xinit(cs, cp, 128, ATEMP); Xinit(cs, ccp, 128, ATEMP);
for (; *wp != NULL; wp++) { for (; *wp != NULL; wp++) {
for (cp = Xstring(cs, cp); ; ) { for (ccp = Xstring(cs, ccp); ; ) {
if (c == '\n' || c == EOF) if (c == '\n' || c == EOF)
break; break;
while (1) { while (1) {
@ -1700,7 +1710,7 @@ c_read(char **wp)
Xcheck(xs, xp); Xcheck(xs, xp);
Xput(xs, xp, c); Xput(xs, xp, c);
} }
Xcheck(cs, cp); Xcheck(cs, ccp);
if (expanding) { if (expanding) {
expanding = 0; expanding = 0;
if (c == '\n') { if (c == '\n') {
@ -1713,7 +1723,7 @@ c_read(char **wp)
pprompt(prompt, 0); pprompt(prompt, 0);
} }
} else if (c != EOF) } else if (c != EOF)
Xput(cs, cp, c); Xput(cs, ccp, c);
continue; continue;
} }
if (expande && c == '\\') { if (expande && c == '\\') {
@ -1723,31 +1733,33 @@ c_read(char **wp)
if (c == '\n' || c == EOF) if (c == '\n' || c == EOF)
break; break;
if (ctype(c, C_IFS)) { if (ctype(c, C_IFS)) {
if (Xlength(cs, cp) == 0 && ctype(c, C_IFSWS)) if (Xlength(cs, ccp) == 0 && ctype(c, C_IFSWS))
continue; continue;
if (wp[1]) if (wp[1])
break; break;
} }
Xput(cs, cp, c); Xput(cs, ccp, c);
} }
/* strip trailing IFS white space from last variable */ /* strip trailing IFS white space from last variable */
if (!wp[1]) if (!wp[1])
while (Xlength(cs, cp) && ctype(cp[-1], C_IFS) && while (Xlength(cs, ccp) && ctype(ccp[-1], C_IFS) &&
ctype(cp[-1], C_IFSWS)) ctype(ccp[-1], C_IFSWS))
cp--; ccp--;
Xput(cs, cp, '\0'); Xput(cs, ccp, '\0');
vp = global(*wp); vp = global(*wp);
/* Must be done before setting export. */ /* Must be done before setting export. */
if (vp->flag & RDONLY) { if (vp->flag & RDONLY) {
shf_flush(shf); shf_flush(shf);
bi_errorf("%s is read only", *wp); bi_errorf("%s is read only", *wp);
afreechk(wpalloc);
return 1; return 1;
} }
if (Flag(FEXPORT)) if (Flag(FEXPORT))
typeset(*wp, EXPORT, 0, 0, 0); typeset(*wp, EXPORT, 0, 0, 0);
if (!setstr(vp, Xstring(cs, cp), KSH_RETURN_ERROR)) { if (!setstr(vp, Xstring(cs, ccp), KSH_RETURN_ERROR)) {
shf_flush(shf); shf_flush(shf);
return 1; afreechk(wpalloc);
return 1;
} }
} }
@ -1765,11 +1777,12 @@ c_read(char **wp)
if (c == EOF && !ecode) if (c == EOF && !ecode)
coproc_read_close(fd); coproc_read_close(fd);
afreechk(wpalloc);
return ecode ? ecode : c == EOF; return ecode ? ecode : c == EOF;
} }
int int
c_eval(char **wp) c_eval(const char **wp)
{ {
struct source *s, *saves = source; struct source *s, *saves = source;
int savef, rv; int savef, rv;
@ -1814,10 +1827,10 @@ c_eval(char **wp)
} }
int int
c_trap(char **wp) c_trap(const char **wp)
{ {
int i; int i;
char *s; const char *s;
Trap *p; Trap *p;
if (ksh_getopt(wp, &builtin_opt, null) == '?') if (ksh_getopt(wp, &builtin_opt, null) == '?')
@ -1861,11 +1874,11 @@ c_trap(char **wp)
} }
int int
c_exitreturn(char **wp) c_exitreturn(const char **wp)
{ {
int how = LEXIT; int how = LEXIT;
int n; int n;
char *arg; const char *arg;
if (ksh_getopt(wp, &builtin_opt, null) == '?') if (ksh_getopt(wp, &builtin_opt, null) == '?')
return 1; return 1;
@ -1903,11 +1916,11 @@ c_exitreturn(char **wp)
} }
int int
c_brkcont(char **wp) c_brkcont(const char **wp)
{ {
int n, quit; int n, quit;
struct env *ep, *last_ep = NULL; struct env *ep, *last_ep = NULL;
char *arg; const char *arg;
if (ksh_getopt(wp, &builtin_opt, null) == '?') if (ksh_getopt(wp, &builtin_opt, null) == '?')
return 1; return 1;
@ -1956,15 +1969,14 @@ c_brkcont(char **wp)
} }
int int
c_set(char **wp) c_set(const char **wp)
{ {
int argi, setargs; int argi, setargs;
struct block *l = e->loc; struct block *l = e->loc;
char **owp = wp; const char **owp = wp;
if (wp[1] == NULL) { if (wp[1] == NULL) {
static char args0[] = "set", args1[] = "-", static const char *args [] = { "set", "-", NULL };
*args[] = { args0, args1, NULL };
return c_typeset(args); return c_typeset(args);
} }
@ -1978,7 +1990,8 @@ c_set(char **wp)
while (*++wp != NULL) while (*++wp != NULL)
*wp = str_save(*wp, &l->area); *wp = str_save(*wp, &l->area);
l->argc = wp - owp - 1; 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; ) for (wp = l->argv; (*wp++ = *owp++) != NULL; )
; ;
} }
@ -1992,9 +2005,9 @@ c_set(char **wp)
} }
int int
c_unset(char **wp) c_unset(const char **wp)
{ {
char *id; const char *id;
int optc, unset_var = 1; int optc, unset_var = 1;
int ret = 0; int ret = 0;
@ -2042,7 +2055,7 @@ p_time(struct shf *shf, int posix, struct timeval *tv, int width,
} }
int int
c_times(char **wp __unused) c_times(const char **wp __unused)
{ {
struct rusage usage; struct rusage usage;
@ -2147,7 +2160,7 @@ timex_hook(struct op *t, char **volatile *app)
ksh_getopt_reset(&opt, 0); ksh_getopt_reset(&opt, 0);
opt.optind = 0; /* start at the start */ 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) { switch (optc) {
case 'p': case 'p':
t->str[0] |= TF_POSIX; 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() */ /* exec with no args - args case is taken care of in comexec() */
int int
c_exec(char **wp __unused) c_exec(const char **wp __unused)
{ {
int i; int i;
@ -2192,11 +2205,11 @@ c_exec(char **wp __unused)
#if !defined(MKSH_SMALL) || defined(MKSH_NEED_MKNOD) #if !defined(MKSH_SMALL) || defined(MKSH_NEED_MKNOD)
static int static int
c_mknod(char **wp) c_mknod(const char **wp)
{ {
int argc, optc, rv = 0; int argc, optc, rv = 0;
bool ismkfifo = false; bool ismkfifo = false;
char **argv; const char **argv;
void *set = NULL; void *set = NULL;
mode_t mode = 0, oldmode = 0; mode_t mode = 0, oldmode = 0;
@ -2278,7 +2291,7 @@ c_mknod(char **wp)
/* dummy function, special case in comexec() */ /* dummy function, special case in comexec() */
int int
c_builtin(char **wp __unused) c_builtin(const char **wp __unused)
{ {
return 0; 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 *); static void ptest_error(Test_env *, int, const char *);
int int
c_test(char **wp) c_test(const char **wp)
{ {
int argc; int argc;
int res; int res;
@ -2432,7 +2445,7 @@ c_test(char **wp)
* our parser does the right thing for the omitted steps. * our parser does the right thing for the omitted steps.
*/ */
if (argc <= 5) { if (argc <= 5) {
char **owp = wp; const char **owp = wp;
int invert = 0; int invert = 0;
Test_op op; Test_op op;
const char *opnd1, *opnd2; const char *opnd1, *opnd2;
@ -2834,7 +2847,7 @@ ptest_error(Test_env *te, int offset, const char *msg)
#define HARD 0x2 #define HARD 0x2
int int
c_ulimit(char **wp) c_ulimit(const char **wp)
{ {
static const struct limits { static const struct limits {
const char *name; const char *name;

View File

@ -3,7 +3,7 @@
#include "sh.h" #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]; Trap sigtraps[NSIG + 1];
static struct sigaction Sigact_ign, Sigact_trap; static struct sigaction Sigact_ign, Sigact_trap;
@ -35,14 +35,15 @@ static int hsize;
#endif #endif
int int
c_fc(char **wp) c_fc(const char **wp)
{ {
struct shf *shf; struct shf *shf;
struct temp *tf = NULL; 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 gflag = 0, lflag = 0, nflag = 0, sflag = 0, rflag = 0;
int optc; int optc;
char *first = NULL, *last = NULL; const char *first = NULL, *last = NULL;
char **hfirst, **hlast, **hp; char **hfirst, **hlast, **hp;
if (!Flag(FTALKING_I)) { if (!Flag(FTALKING_I)) {
@ -107,11 +108,10 @@ c_fc(char **wp)
} }
/* Check for pattern replacement argument */ /* Check for pattern replacement argument */
if (*wp && **wp && (p = strchr(*wp + 1, '='))) { if (*wp && **wp && (p = cstrchr(*wp + 1, '='))) {
pat = str_save(*wp, ATEMP); pat = str_save(*wp, ATEMP);
p = pat + (p - *wp); rep = pat + (p - *wp);
*p++ = '\0'; *rep++ = '\0';
rep = p;
wp++; wp++;
} }
/* Check for search prefix */ /* Check for search prefix */
@ -1239,7 +1239,7 @@ restoresigs(void)
} }
void void
settrap(Trap *p, char *s) settrap(Trap *p, const char *s)
{ {
sig_t f; sig_t f;

27
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h> #include <locale.h>
#endif #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; extern char **environ;
@ -60,14 +60,14 @@ static const char *initcoms[] = {
static int initio_done; static int initio_done;
int int
main(int argc, char *argv[]) main(int argc, const char *argv[])
{ {
int i; int i;
int argi; int argi;
Source *s; Source *s;
struct block *l; struct block *l;
int restricted, errexit; int restricted, errexit;
char **wp; const char **wp;
struct env env; struct env env;
pid_t ppid; pid_t ppid;
struct tbl *vp; struct tbl *vp;
@ -82,8 +82,9 @@ main(int argc, char *argv[])
/* make sure argv[] is sane */ /* make sure argv[] is sane */
if (!*argv) { if (!*argv) {
static char empty_argv0[] = "mksh", static const char *empty_argv[] = {
*empty_argv[] = { empty_argv0, NULL }; "mksh", NULL
};
argv = empty_argv; argv = empty_argv;
argc = 1; argc = 1;
@ -180,7 +181,7 @@ main(int argc, char *argv[])
/* import environment */ /* import environment */
if (environ != NULL) if (environ != NULL)
for (wp = environ; *wp != NULL; wp++) for (wp = (const char **)environ; *wp != NULL; wp++)
typeset(*wp, IMPORT | EXPORT, 0, 0, 0); typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
kshpid = procpid = getpid(); kshpid = procpid = getpid();
@ -219,7 +220,7 @@ main(int argc, char *argv[])
setint(global("PPID"), (long)ppid); setint(global("PPID"), (long)ppid);
/* execute initialisation statements */ /* execute initialisation statements */
for (wp = (char **)initcoms; *wp != NULL; wp++) { for (wp = initcoms; *wp != NULL; wp++) {
shcomexec(wp); shcomexec(wp);
for (; *wp != NULL; wp++) for (; *wp != NULL; wp++)
; ;
@ -312,7 +313,7 @@ main(int argc, char *argv[])
l = e->loc; l = e->loc;
l->argv = &argv[argi - 1]; l->argv = &argv[argi - 1];
l->argc = argc - argi; l->argc = argc - argi;
l->argv[0] = (char *)kshname; l->argv[0] = kshname;
getopts_reset(1); getopts_reset(1);
/* Disable during .profile/ENV reading */ /* Disable during .profile/ENV reading */
@ -346,12 +347,12 @@ main(int argc, char *argv[])
} }
if (restricted) { if (restricted) {
static const char *const restr_com[] = { static const char *restr_com[] = {
"typeset", "-r", "PATH", "typeset", "-r", "PATH",
"ENV", "SHELL", "ENV", "SHELL",
NULL NULL
}; };
shcomexec((char **)restr_com); shcomexec(restr_com);
/* After typeset command... */ /* After typeset command... */
Flag(FRESTRICTED) = 1; Flag(FRESTRICTED) = 1;
} }
@ -369,11 +370,11 @@ main(int argc, char *argv[])
} }
int 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; Source *volatile s = NULL;
struct shf *shf; struct shf *shf;
char **volatile old_argv; const char **volatile old_argv;
volatile int old_argc; volatile int old_argc;
int i; int i;
@ -975,7 +976,7 @@ closepipe(int *pv)
* a string (the X in 2>&X, read -uX, print -uX) into a file descriptor. * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
*/ */
int int
check_fd(char *name, int mode, const char **emsgp) check_fd(const char *name, int mode, const char **emsgp)
{ {
int fd, fl; int fd, fl;

14
misc.c
View File

@ -6,7 +6,7 @@
#include <grp.h> #include <grp.h>
#endif #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); MKSH_SH_H_ID);
#undef USE_CHVT #undef USE_CHVT
@ -88,9 +88,9 @@ str_save(const char *s, Area *ap)
/* called from XcheckN() to grow buffer */ /* called from XcheckN() to grow buffer */
char * 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->len += more > xsp->len ? more : xsp->len;
xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap); 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. * non-option arguments, -1 if there is an error.
*/ */
int int
parse_args(char **argv, parse_args(const char **argv,
int what, /* OF_CMDLINE or OF_SET */ int what, /* OF_CMDLINE or OF_SET */
int *setargsp) int *setargsp)
{ {
static char cmd_opts[NELEM(options) + 5]; /* o:T:\0 */ static char cmd_opts[NELEM(options) + 5]; /* o:T:\0 */
static char set_opts[NELEM(options) + 6]; /* A:o;s\0 */ static char set_opts[NELEM(options) + 6]; /* A:o;s\0 */
char *opts; char *opts;
char *array = NULL; const char *array = NULL;
Getopt go; Getopt go;
int optc, set, sortargs = 0, arrayset = 0; int optc, set, sortargs = 0, arrayset = 0;
unsigned i; unsigned i;
@ -808,13 +808,13 @@ ksh_getopt_reset(Getopt *go, int flags)
* in go->info. * in go->info.
*/ */
int int
ksh_getopt(char **argv, Getopt *go, const char *optionsp) ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
{ {
char c; char c;
char *o; char *o;
if (go->p == 0 || (c = argv[go->optind - 1][go->p]) == '\0') { 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; go->p = 1;
if (flag == '-' && arg[1] == '-' && arg[2] == '\0') { if (flag == '-' && arg[1] == '-' && arg[2] == '\0') {

151
sh.h
View File

@ -8,7 +8,7 @@
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */ /* $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 $ */ /* $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" #define MKSH_VERSION "R29 2007/02/16"
#if HAVE_SYS_PARAM_H #if HAVE_SYS_PARAM_H
@ -220,7 +220,7 @@ typedef int32_t Tflag;
#define PATH_MAX 1024 /* pathname size */ #define PATH_MAX 1024 /* pathname size */
#endif #endif
EXTERN char *kshname; /* $0 */ EXTERN const char *kshname; /* $0 */
EXTERN pid_t kshpid; /* $$, shell pid */ EXTERN pid_t kshpid; /* $$, shell pid */
EXTERN pid_t procpid; /* pid of executing process */ EXTERN pid_t procpid; /* pid of executing process */
EXTERN uid_t ksheuid; /* effective uid of shell */ 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); EXTERN const char initvsn[] I__("KSH_VERSION=@(#)MIRBSD KSH " MKSH_VERSION);
#define KSH_VERSION (initvsn + 16) #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 * Area-based allocation built on malloc/free
*/ */
@ -471,7 +490,7 @@ EXTERN int ifs0 I__(' '); /* for "$*" */
typedef struct { typedef struct {
int optind; int optind;
int uoptind;/* what user sees in $OPTIND */ int uoptind;/* what user sees in $OPTIND */
char *optarg; const char *optarg;
int flags; /* see GF_* */ int flags; /* see GF_* */
int info; /* see GI_* */ int info; /* see GI_* */
unsigned int p; /* 0 or index into argv[optind - 1] */ 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; EXTERN sigset_t sm_default, sm_sigchld;
/* name of called builtin function (used by error functions) */ /* name of called builtin function (used by error functions) */
EXTERN char *builtin_argv0; EXTERN const char *builtin_argv0;
EXTERN Tflag builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */ EXTERN Tflag builtin_flag; /* flags of called builtin (SPEC_BI, etc.) */
/* current working directory, and size of memory allocated for same */ /* current working directory, and size of memory allocated for same */
EXTERN char *current_wd; EXTERN char *current_wd;
@ -604,7 +623,7 @@ struct tbl { /* table item */
union { union {
char *s; /* string */ char *s; /* string */
long i; /* integer */ long i; /* integer */
int (*f)(char **); /* int function */ int (*f)(const char **); /* int function */
struct op *t; /* "function" tree */ struct op *t; /* "function" tree */
} val; /* value */ } val; /* value */
int index; /* index for an array */ int index; /* index for an array */
@ -693,7 +712,7 @@ struct arg_info {
*/ */
struct block { struct block {
Area area; /* area to allocate things */ Area area; /* area to allocate things */
char **argv; const char **argv;
int argc; int argc;
int flags; /* see BF_* */ int flags; /* see BF_* */
struct table vars; /* local variables */ struct table vars; /* local variables */
@ -726,7 +745,7 @@ EXTERN struct table homedirs; /* homedir() cache */
struct builtin { struct builtin {
const char *name; const char *name;
int (*func)(char **); int (*func)(const char **);
}; };
extern const struct builtin shbuiltins [], kshbuiltins []; extern const struct builtin shbuiltins [], kshbuiltins [];
@ -769,7 +788,7 @@ struct op {
short evalflags; /* TCOM: arg expansion eval() flags */ short evalflags; /* TCOM: arg expansion eval() flags */
short ksh_func; /* TFUNC: function x (vs x()) */ short ksh_func; /* TFUNC: function x (vs x()) */
} u; } u;
char **args; /* arguments to a command */ const char **args; /* arguments to a command */
char **vars; /* variable assignments */ char **vars; /* variable assignments */
struct ioword **ioact; /* IO actions (eg, < > >>) */ struct ioword **ioact; /* IO actions (eg, < > >>) */
struct op *left, *right; /* descendents */ struct op *left, *right; /* descendents */
@ -937,7 +956,7 @@ typedef char *XStringP;
#define Xsavepos(xs, xp) ((xp) - (xs).beg) #define Xsavepos(xs, xp) ((xp) - (xs).beg)
#define Xrestpos(xs, xp, n) ((xs).beg + (n)) #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 * expandable vector of generic pointers
@ -982,10 +1001,10 @@ struct source {
int type; /* input type */ int type; /* input type */
const char *start; /* start of current buffer */ const char *start; /* start of current buffer */
union { union {
char **strv; /* string [] */ const char **strv; /* string [] */
struct shf *shf; /* shell file */ struct shf *shf; /* shell file */
struct tbl *tblp; /* alias (SALIAS) */ struct tbl *tblp; /* alias (SALIAS) */
char *freeme; /* also for SREREAD */ char *freeme; /* also for SREREAD */
} u; } u;
char ugbuf[2]; /* buffer for ungetsc() (SREREAD) and char ugbuf[2]; /* buffer for ungetsc() (SREREAD) and
* alias (SALIAS) */ * alias (SALIAS) */
@ -1101,6 +1120,14 @@ void afreeall(Area *);
void *alloc(size_t, Area *); void *alloc(size_t, Area *);
void *aresize(void *, size_t, Area *); void *aresize(void *, size_t, Area *);
void afree(void *, 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 */ /* edit.c */
void x_init(void); void x_init(void);
int x_read(char *, size_t); 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); #define utf_width(x) utf_widthadj(x, NULL);
/* eval.c */ /* eval.c */
char *substitute(const char *, int); char *substitute(const char *, int);
char **eval(char **, int); char **eval(const char **, int);
char *evalstr(char *cp, int); char *evalstr(const char *cp, int);
char *evalonestr(char *cp, int); char *evalonestr(const char *cp, int);
char *debunk(char *, const char *, size_t); char *debunk(char *, const char *, size_t);
void expand(char *, XPtrV *, int); void expand(const char *, XPtrV *, int);
int glob_str(char *, XPtrV *, int); int glob_str(char *, XPtrV *, int);
/* exec.c */ /* exec.c */
int execute(struct op * volatile, volatile int); int execute(struct op * volatile, volatile int);
int shcomexec(char **); int shcomexec(const char **);
struct tbl *findfunc(const char *, unsigned int, int); struct tbl *findfunc(const char *, unsigned int, int);
int define(const char *, struct op *); 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); struct tbl *findcom(const char *, int);
void flushcom(int); void flushcom(int);
const char *search(const char *, const char *, int, int *); const char *search(const char *, const char *, int, int *);
int search_access(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 *); int pr_list(char *const *);
/* expr.c */ /* expr.c */
int evaluate(const char *, long *, int, bool); int evaluate(const char *, long *, int, bool);
int v_evaluate(struct tbl *, const char *, volatile int, bool); int v_evaluate(struct tbl *, const char *, volatile int, bool);
/* funcs.c */ /* funcs.c */
int c_hash(char **); int c_hash(const char **);
int c_cd(char **); int c_cd(const char **);
int c_pwd(char **); int c_pwd(const char **);
int c_print(char **); int c_print(const char **);
int c_whence(char **); int c_whence(const char **);
int c_command(char **); int c_command(const char **);
int c_typeset(char **); int c_typeset(const char **);
int c_alias(char **); int c_alias(const char **);
int c_unalias(char **); int c_unalias(const char **);
int c_let(char **); int c_let(const char **);
int c_jobs(char **); int c_jobs(const char **);
int c_fgbg(char **); int c_fgbg(const char **);
int c_kill(char **); int c_kill(const char **);
void getopts_reset(int); void getopts_reset(int);
int c_getopts(char **); int c_getopts(const char **);
int c_bind(char **); int c_bind(const char **);
int c_label(char **); int c_label(const char **);
int c_shift(char **); int c_shift(const char **);
int c_umask(char **); int c_umask(const char **);
int c_dot(char **); int c_dot(const char **);
int c_wait(char **); int c_wait(const char **);
int c_read(char **); int c_read(const char **);
int c_eval(char **); int c_eval(const char **);
int c_trap(char **); int c_trap(const char **);
int c_brkcont(char **); int c_brkcont(const char **);
int c_exitreturn(char **); int c_exitreturn(const char **);
int c_set(char **); int c_set(const char **);
int c_unset(char **); int c_unset(const char **);
int c_ulimit(char **); int c_ulimit(const char **);
int c_times(char **); int c_times(const char **);
int timex(struct op *, int); int timex(struct op *, int);
void timex_hook(struct op *, char ** volatile *); void timex_hook(struct op *, char ** volatile *);
int c_exec(char **); int c_exec(const char **);
int c_builtin(char **); int c_builtin(const char **);
int c_test(char **); int c_test(const char **);
/* histrap.c */ /* histrap.c */
void init_histvec(void); void init_histvec(void);
void hist_init(Source *); void hist_init(Source *);
@ -1174,7 +1201,7 @@ void hist_init(Source *);
void hist_finish(void); void hist_finish(void);
#endif #endif
void histsave(int, const char *, int); void histsave(int, const char *, int);
int c_fc(char **); int c_fc(const char **);
void sethistsize(int); void sethistsize(int);
#if HAVE_PERSISTENT_HISTORY #if HAVE_PERSISTENT_HISTORY
void sethistfile(const char *); void sethistfile(const char *);
@ -1195,7 +1222,7 @@ void runtraps(int intr);
void runtrap(Trap *); void runtrap(Trap *);
void cleartraps(void); void cleartraps(void);
void restoresigs(void); void restoresigs(void);
void settrap(Trap *, char *); void settrap(Trap *, const char *);
int block_pipe(void); int block_pipe(void);
void restore_pipe(int); void restore_pipe(int);
int setsig(Trap *, sig_t, int); int setsig(Trap *, sig_t, int);
@ -1225,7 +1252,7 @@ void set_prompt(int, Source *);
void pprompt(const char *, int); void pprompt(const char *, int);
int promptlen(const char *); int promptlen(const char *);
/* main.c */ /* main.c */
int include(const char *, int, char **, int); int include(const char *, int, const char **, int);
int command(const char *); int command(const char *);
int shell(Source *volatile, int volatile); int shell(Source *volatile, int volatile);
void unwind(int) void unwind(int)
@ -1255,7 +1282,7 @@ int savefd(int);
void restfd(int, int); void restfd(int, int);
void openpipe(int *); void openpipe(int *);
void closepipe(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_init(void);
void coproc_read_close(int); void coproc_read_close(int);
void coproc_readw_close(int); void coproc_readw_close(int);
@ -1283,7 +1310,7 @@ char *str_nsave(const char *, int, Area *);
int option(const char *); int option(const char *);
char *getoptions(void); char *getoptions(void);
void change_flag(enum sh_flag, int, int); 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 getn(const char *, int *);
int bi_getn(const char *, int *); int bi_getn(const char *, int *);
int gmatchx(const char *, 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); const unsigned char *pat_scan(const unsigned char *, const unsigned char *, int);
int xstrcmp(const void *, const void *); int xstrcmp(const void *, const void *);
void ksh_getopt_reset(Getopt *, int); 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_value_quoted(const char *);
void print_columns(struct shf *, int, void print_columns(struct shf *, int,
char *(*)(const void *, int, char *, int), char *(*)(const void *, int, char *, int),
@ -1362,7 +1389,7 @@ char **makenv(void);
void change_random(void); void change_random(void);
int array_ref_len(const char *); int array_ref_len(const char *);
char *arrayname(const char *); char *arrayname(const char *);
void set_array(const char *, int, char **); void set_array(const char *, int, const char **);
enum Test_op { enum Test_op {
TO_NONOP = 0, /* non-operator */ TO_NONOP = 0, /* non-operator */
@ -1399,10 +1426,10 @@ typedef struct test_env Test_env;
struct test_env { struct test_env {
int flags; /* TEF_* */ int flags; /* TEF_* */
union { union {
char **wp; /* used by ptest_* */ const char **wp;/* used by ptest_* */
XPtrV *av; /* used by dbtestp_* */ XPtrV *av; /* used by dbtestp_* */
} pos; } pos;
char **wp_end; /* used by ptest_* */ const char **wp_end; /* used by ptest_* */
int (*isa)(Test_env *, Test_meta); int (*isa)(Test_env *, Test_meta);
const char *(*getopnd) (Test_env *, Test_op, int); const char *(*getopnd) (Test_env *, Test_op, int);
int (*eval)(Test_env *, Test_op, const char *, const char *, int); int (*eval)(Test_env *, Test_op, const char *, const char *, int);

24
syn.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #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 { struct nesting_state {
int start_token; /* token than began nesting (eg, FOR) */ int start_token; /* token than began nesting (eg, FOR) */
@ -376,7 +376,7 @@ get_command(int cf)
} }
if (iopn == 0) { if (iopn == 0) {
afree((void*) iops, ATEMP); afree((void*)iops, ATEMP);
t->ioact = NULL; t->ioact = NULL;
} else { } else {
iops[iopn++] = NULL; iops[iopn++] = NULL;
@ -387,7 +387,7 @@ get_command(int cf)
if (t->type == TCOM || t->type == TDBRACKET) { if (t->type == TCOM || t->type == TDBRACKET) {
XPput(args, NULL); XPput(args, NULL);
t->args = (char **) XPclose(args); t->args = (const char **)XPclose(args);
XPput(vars, NULL); XPput(vars, NULL);
t->vars = (char **) XPclose(vars); t->vars = (char **) XPclose(vars);
} else { } else {
@ -549,6 +549,7 @@ function_body(char *name,
old_func_parse = e->flags & EF_FUNC_PARSE; old_func_parse = e->flags & EF_FUNC_PARSE;
e->flags |= EF_FUNC_PARSE; e->flags |= EF_FUNC_PARSE;
if ((t->left = get_command(CONTIN)) == NULL) { if ((t->left = get_command(CONTIN)) == NULL) {
char *tv;
/* /*
* Probably something like foo() followed by eof or ;. * Probably something like foo() followed by eof or ;.
* This is accepted by sh and ksh88. * 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. * be used as input), we pretend there is a colon here.
*/ */
t->left = newtp(TCOM); t->left = newtp(TCOM);
t->left->args = (char **) alloc(sizeof(char *) * 2, ATEMP); t->left->args = (const char **)alloc(sizeof(char *) * 2, ATEMP);
t->left->args[0] = alloc(sizeof(char) * 3, ATEMP); t->left->args[0] = tv = alloc(sizeof(char) * 3, ATEMP);
t->left->args[0][0] = CHAR; tv[0] = CHAR;
t->left->args[0][1] = ':'; tv[1] = ':';
t->left->args[0][2] = EOS; tv[2] = EOS;
t->left->args[1] = NULL; 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->vars[0] = NULL;
t->left->lineno = 1; t->left->lineno = 1;
} }
@ -739,10 +740,11 @@ newtp(int type)
{ {
struct op *t; struct op *t;
t = (struct op *) alloc(sizeof(*t), ATEMP); t = (struct op *)alloc(sizeof(*t), ATEMP);
t->type = type; t->type = type;
t->u.evalflags = 0; t->u.evalflags = 0;
t->args = t->vars = NULL; t->args = NULL;
t->vars = NULL;
t->ioact = NULL; t->ioact = NULL;
t->left = t->right = NULL; t->left = t->right = NULL;
t->str = NULL; t->str = NULL;

36
tree.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #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 #define INDENT 4
@ -18,11 +18,10 @@ static void iofree(struct ioword **, Area *);
/* /*
* print a command tree * print a command tree
*/ */
static void static void
ptree(struct op *t, int indent, struct shf *shf) ptree(struct op *t, int indent, struct shf *shf)
{ {
char **w; const char **w;
struct ioword **ioact; struct ioword **ioact;
struct op *t1; struct op *t1;
@ -32,7 +31,7 @@ ptree(struct op *t, int indent, struct shf *shf)
switch (t->type) { switch (t->type) {
case TCOM: case TCOM:
if (t->vars) if (t->vars)
for (w = t->vars; *w != NULL; ) for (w = (const char **)t->vars; *w != NULL; )
fptreef(shf, indent, "%S ", *w++); fptreef(shf, indent, "%S ", *w++);
else else
fptreef(shf, indent, "#no-vars# "); 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); fptreef(shf, indent, "for %s ", t->str);
if (t->vars != NULL) { if (t->vars != NULL) {
fptreef(shf, indent, "in "); fptreef(shf, indent, "in ");
for (w = t->vars; *w; ) for (w = (const char **)t->vars; *w; )
fptreef(shf, indent, "%S ", *w++); fptreef(shf, indent, "%S ", *w++);
fptreef(shf, indent, "%;"); fptreef(shf, indent, "%;");
} }
@ -94,7 +93,7 @@ ptree(struct op *t, int indent, struct shf *shf)
fptreef(shf, indent, "case %S in", t->str); fptreef(shf, indent, "case %S in", t->str);
for (t1 = t->left; t1 != NULL; t1 = t1->right) { for (t1 = t->left; t1 != NULL; t1 = t1->right) {
fptreef(shf, indent, "%N("); 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, fptreef(shf, indent, "%S%c", *w,
(w[1] != NULL) ? '|' : ')'); (w[1] != NULL) ? '|' : ')');
fptreef(shf, indent + INDENT, "%;%T%N;;", t1->left); 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 * variants of fputc, fputs for ptreef and snptreef
*/ */
static void static void
tputC(int c, struct shf *shf) 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) * copy tree (for function definition)
*/ */
struct op * struct op *
tcopy(struct op *t, Area *ap) tcopy(struct op *t, Area *ap)
{ {
struct op *r; struct op *r;
char **tw, **rw; const char **tw;
char **rw;
if (t == NULL) if (t == NULL)
return NULL; return NULL;
@ -439,11 +437,11 @@ tcopy(struct op *t, Area *ap)
if (t->vars == NULL) if (t->vars == NULL)
r->vars = NULL; r->vars = NULL;
else { else {
for (tw = t->vars; *tw++ != NULL; ) for (tw = (const char **)t->vars; *tw++ != NULL; )
; ;
rw = r->vars = (char **) rw = r->vars = (char **)
alloc((tw - t->vars + 1) * sizeof(*tw), ap); alloc((tw - (const char **)t->vars + 1) * sizeof(*tw), ap);
for (tw = t->vars; *tw != NULL; ) for (tw = (const char **)t->vars; *tw != NULL; )
*rw++ = wdcopy(*tw++, ap); *rw++ = wdcopy(*tw++, ap);
*rw = NULL; *rw = NULL;
} }
@ -453,8 +451,8 @@ tcopy(struct op *t, Area *ap)
else { else {
for (tw = t->args; *tw++ != NULL; ) for (tw = t->args; *tw++ != NULL; )
; ;
rw = r->args = (char **) r->args = (const char **)(rw = (char **)
alloc((tw - t->args + 1) * sizeof(*tw), ap); alloc((tw - t->args + 1) * sizeof(*tw), ap));
for (tw = t->args; *tw != NULL; ) for (tw = t->args; *tw != NULL; )
*rw++ = wdcopy(*tw++, ap); *rw++ = wdcopy(*tw++, ap);
*rw = NULL; *rw = NULL;
@ -596,7 +594,7 @@ wdstrip(const char *wp)
} }
} }
static struct ioword ** static struct ioword **
iocopy(struct ioword **iow, Area *ap) iocopy(struct ioword **iow, Area *ap)
{ {
struct ioword **ior; struct ioword **ior;
@ -628,7 +626,6 @@ iocopy(struct ioword **iow, Area *ap)
/* /*
* free tree (for function definition) * free tree (for function definition)
*/ */
void void
tfree(struct op *t, Area *ap) tfree(struct op *t, Area *ap)
{ {
@ -647,7 +644,10 @@ tfree(struct op *t, Area *ap)
} }
if (t->args != NULL) { 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*)*w, ap);
afree((void*)t->args, ap); afree((void*)t->args, ap);
} }
@ -661,7 +661,7 @@ tfree(struct op *t, Area *ap)
afree((void*)t, ap); afree((void*)t, ap);
} }
static void static void
iofree(struct ioword **iow, Area *ap) iofree(struct ioword **iow, Area *ap)
{ {
struct ioword **iop; struct ioword **iop;

6
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #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 * Variables
@ -32,7 +32,7 @@ void
newblock(void) newblock(void)
{ {
struct block *l; struct block *l;
static char *empty[] = { null }; static const char *empty[] = { null };
l = (struct block *) alloc(sizeof(struct block), ATEMP); l = (struct block *) alloc(sizeof(struct block), ATEMP);
l->flags = 0; 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. /* Set (or overwrite, if !reset) the array variable var to the values in vals.
*/ */
void void
set_array(const char *var, int reset, char **vals) set_array(const char *var, int reset, const char **vals)
{ {
struct tbl *vp, *vq; struct tbl *vp, *vq;
int i; int i;