From c10e5bd2378d0e12c74643df51e8c13fe7f603f4 Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 10 Aug 2013 13:44:33 +0000 Subject: [PATCH] reduce amount of .bss memory needed; initialise via AEDIT at x_init or even first run of x_vi --- Build.sh | 6 +-- Makefile | 4 +- check.t | 6 +-- edit.c | 136 +++++++++++++++++++++++++++++-------------------------- lex.c | 4 +- mksh.1 | 20 ++++++-- sh.h | 10 ++-- 7 files changed, 102 insertions(+), 84 deletions(-) diff --git a/Build.sh b/Build.sh index 243376d..45af9dd 100644 --- a/Build.sh +++ b/Build.sh @@ -1,5 +1,5 @@ #!/bin/sh -srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.644 2013/07/25 17:01:03 tg Exp $' +srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.645 2013/08/10 13:44:25 tg Exp $' #- # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # 2011, 2012, 2013 @@ -1598,7 +1598,7 @@ else #define EXTERN #define MKSH_INCLUDES_ONLY #include "sh.h" - __RCSID("$MirOS: src/bin/mksh/Build.sh,v 1.644 2013/07/25 17:01:03 tg Exp $"); + __RCSID("$MirOS: src/bin/mksh/Build.sh,v 1.645 2013/08/10 13:44:25 tg Exp $"); int main(void) { printf("Hello, World!\n"); return (0); } EOF case $cm in @@ -2113,7 +2113,7 @@ addsrcs USE_PRINTF_BUILTIN printf.c test 1 = "$USE_PRINTF_BUILTIN" && add_cppflags -DMKSH_PRINTF_BUILTIN test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose" test -n "$LDSTATIC" && add_cppflags -DMKSH_OPTSTATIC -add_cppflags -DMKSH_BUILD_R=471 +add_cppflags -DMKSH_BUILD_R=481 $e $bi$me: Finished configuration testing, now producing output.$ao diff --git a/Makefile b/Makefile index f1e2f36..19f6925 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/Makefile,v 1.123 2013/07/21 18:47:15 tg Exp $ +# $MirOS: src/bin/mksh/Makefile,v 1.124 2013/08/10 13:44:26 tg Exp $ #- # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # 2011, 2012, 2013 @@ -54,7 +54,7 @@ CPPFLAGS+= -DMKSH_ASSUME_UTF8 -DMKSH_DISABLE_DEPRECATED \ -DHAVE_SETGROUPS=1 -DHAVE_STRERROR=0 -DHAVE_STRSIGNAL=0 \ -DHAVE_STRLCPY=1 -DHAVE_FLOCK_DECL=1 -DHAVE_REVOKE_DECL=1 \ -DHAVE_SYS_ERRLIST_DECL=1 -DHAVE_SYS_SIGLIST_DECL=1 \ - -DHAVE_PERSISTENT_HISTORY=1 -DMKSH_BUILD_R=471 + -DHAVE_PERSISTENT_HISTORY=1 -DMKSH_BUILD_R=481 CPPFLAGS+= -D${${PROG:L}_tf:C/(Mir${MAN:E}{0,1}){2}/4/:S/x/mksh_BUILD/:U} COPTS+= -std=c99 -Wall .endif diff --git a/check.t b/check.t index 79e3fba..09d1a70 100644 --- a/check.t +++ b/check.t @@ -1,4 +1,4 @@ -# $MirOS: src/bin/mksh/check.t,v 1.625 2013/07/26 20:33:35 tg Exp $ +# $MirOS: src/bin/mksh/check.t,v 1.626 2013/08/10 13:44:27 tg Exp $ # $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ @@ -31,7 +31,7 @@ # http://www.freebsd.org/cgi/cvsweb.cgi/src/tools/regression/bin/test/regress.sh?rev=HEAD expected-stdout: - @(#)MIRBSD KSH R47 2013/07/26 + @(#)MIRBSD KSH R47 2013/08/10 description: Check version of shell. stdin: @@ -40,7 +40,7 @@ name: KSH_VERSION category: shell:legacy-no --- expected-stdout: - @(#)LEGACY KSH R47 2013/07/26 + @(#)LEGACY KSH R47 2013/08/10 description: Check version of legacy shell. stdin: diff --git a/edit.c b/edit.c index 14fb854..c464f31 100644 --- a/edit.c +++ b/edit.c @@ -28,7 +28,7 @@ #ifndef MKSH_NO_CMDLINE_EDITING -__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.268 2013/07/26 20:33:37 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.269 2013/08/10 13:44:29 tg Exp $"); /* * in later versions we might use libtermcap for this, but since external @@ -66,7 +66,7 @@ static X_chars edchars; static char editmode; static int xx_cols; /* for Emacs mode */ static int modified; /* buffer has been "modified" */ -static char holdbuf[LINE]; /* place to hold last edit buffer */ +static char *holdbufp; /* place to hold last edit buffer */ static int x_getc(void); static void x_putcf(int); @@ -81,10 +81,10 @@ static char *x_glob_hlp_tilde_and_rem_qchar(char *, bool); static int x_basename(const char *, const char *); static void x_free_words(int, char **); static int x_escape(const char *, size_t, int (*)(const char *, size_t)); -static int x_emacs(char *, size_t); +static int x_emacs(char *); static void x_init_prompt(void); #if !MKSH_S_NOVI -static int x_vi(char *, size_t); +static int x_vi(char *); #endif #define x_flush() shf_flush(shl_out) @@ -110,17 +110,17 @@ static int x_e_rebuildline(const char *); * read an edited command line */ int -x_read(char *buf, size_t len) +x_read(char *buf) { int i; x_mode(true); modified = 1; if (Flag(FEMACS) || Flag(FGMACS)) - i = x_emacs(buf, len); + i = x_emacs(buf); #if !MKSH_S_NOVI else if (Flag(FVI)) - i = x_vi(buf, len); + i = x_vi(buf); #endif else /* internal error */ @@ -1181,12 +1181,13 @@ x_init_prompt(void) } static int -x_emacs(char *buf, size_t len) +x_emacs(char *buf) { int c, i; unsigned char f; - xbp = xbuf = buf; xend = buf + len; + xbp = xbuf = buf; + xend = buf + LINE; xlp = xcp = xep = buf; *xcp = 0; xlp_valid = true; @@ -1821,7 +1822,7 @@ x_load_hist(char **hp) char *sp = NULL; if (hp == histptr + 1) { - sp = holdbuf; + sp = holdbufp; modified = 0; } else if (hp < history || hp > histptr) { x_e_putc2(7); @@ -1832,7 +1833,7 @@ x_load_hist(char **hp) x_histp = hp; oldsize = x_size_str(xbuf); if (modified) - strlcpy(holdbuf, xbuf, sizeof(holdbuf)); + strlcpy(holdbufp, xbuf, LINE); strlcpy(xbuf, sp, xend - xbuf); xbp = xbuf; xep = xcp = xbuf + strlen(xbuf); @@ -3360,9 +3361,9 @@ static void yank_range(int, int); static int bracktype(int); static void save_cbuf(void); static void restore_cbuf(void); -static int putbuf(const char *, ssize_t, int); +static int putbuf(const char *, ssize_t, bool); static void del_range(int, int); -static int findch(int, int, int, int); +static int findch(int, int, bool, bool); static int forwword(int); static int backword(int); static int endword(int); @@ -3460,24 +3461,22 @@ static const unsigned char classify[128] = { #define VVERSION 10 /* ^V */ #define VPREFIX2 11 /* ^[[ and ^[O in insert mode */ -static char undocbuf[LINE]; - static struct edstate *save_edstate(struct edstate *old); static void restore_edstate(struct edstate *old, struct edstate *news); static void free_edstate(struct edstate *old); static struct edstate ebuf; -static struct edstate undobuf = { undocbuf, 0, LINE, 0, 0 }; +static struct edstate undobuf; -static struct edstate *es; /* current editor state */ +static struct edstate *es; /* current editor state */ static struct edstate *undo; -static char ibuf[LINE]; /* input buffer */ -static int first_insert; /* set when starting in insert mode */ +static char *ibuf; /* input buffer */ +static bool first_insert; /* set when starting in insert mode */ static int saved_inslen; /* saved inslen for first insert */ static int inslen; /* length of input buffer */ static int srchlen; /* length of current search pattern */ -static char ybuf[LINE]; /* yank buffer */ +static char *ybuf; /* yank buffer */ static int yanklen; /* length of yank buffer */ static int fsavecmd = ' '; /* last find command */ static int fsavech; /* character to find */ @@ -3485,7 +3484,7 @@ static char lastcmd[MAXVICMD]; /* last non-move command */ static int lastac; /* argcnt for lastcmd */ static int lastsearch = ' '; /* last search command */ static char srchpat[SRCHLEN]; /* last search pattern */ -static int insert; /* non-zero in insert mode */ +static int insert; /* <>0 in insert mode */ static int hnum; /* position in history */ static int ohnum; /* history line copied (after mod) */ static int hlast; /* 1 past last position in history */ @@ -3511,7 +3510,7 @@ static enum expand_mode { } expanded; static int -x_vi(char *buf, size_t len) +x_vi(char *buf) { int c; @@ -3519,26 +3518,29 @@ x_vi(char *buf, size_t len) ohnum = hnum = hlast = histnum(-1) + 1; insert = INSERT; saved_inslen = inslen; - first_insert = 1; + first_insert = true; inslen = 0; vi_macro_reset(); + ebuf.cbuf = buf; + if (undobuf.cbuf == NULL) { + ibuf = alloc(LINE, AEDIT); + ybuf = alloc(LINE, AEDIT); + undobuf.cbuf = alloc(LINE, AEDIT); + } + undobuf.cbufsize = ebuf.cbufsize = LINE; + undobuf.linelen = ebuf.linelen = 0; + undobuf.cursor = ebuf.cursor = 0; + undobuf.winleft = ebuf.winleft = 0; es = &ebuf; - es->cbuf = buf; undo = &undobuf; - undo->cbufsize = es->cbufsize = len > LINE ? LINE : len; - - es->linelen = undo->linelen = 0; - es->cursor = undo->cursor = 0; - es->winleft = undo->winleft = 0; x_init_prompt(); x_col = pwidth; - if (!wbuf_len || wbuf_len != x_cols - 3) { - wbuf_len = x_cols - 3; - wbuf[0] = aresize(wbuf[0], wbuf_len, APERM); - wbuf[1] = aresize(wbuf[1], wbuf_len, APERM); + if (wbuf_len != x_cols - 3 && ((wbuf_len = x_cols - 3))) { + wbuf[0] = aresize(wbuf[0], wbuf_len, AEDIT); + wbuf[1] = aresize(wbuf[1], wbuf_len, AEDIT); } if (wbuf_len) { memset(wbuf[0], ' ', wbuf_len); @@ -3595,11 +3597,11 @@ x_vi(char *buf, size_t len) x_putc('\n'); x_flush(); - if (c == -1 || (ssize_t)len <= es->linelen) + if (c == -1 || (ssize_t)LINE <= es->linelen) return (-1); if (es->cbuf != buf) - memmove(buf, es->cbuf, es->linelen); + memcpy(buf, es->cbuf, es->linelen); buf[es->linelen++] = '\n'; @@ -3650,10 +3652,8 @@ vi_hook(int ch) save_cbuf(); es->cursor = 0; es->linelen = 0; - if (ch == '/') { - if (putbuf("/", 1, 0) != 0) - return (-1); - } else if (putbuf("?", 1, 0) != 0) + if (putbuf(ch == '/' ? "/" : "?", 1, + false) != 0) return (-1); refresh(0); } @@ -3662,7 +3662,7 @@ vi_hook(int ch) es->cursor = 0; es->linelen = 0; putbuf(KSH_VERSION, - strlen(KSH_VERSION), 0); + strlen(KSH_VERSION), false); refresh(0); } } @@ -3993,7 +3993,7 @@ vi_insert(int ch) case Ctrl('['): expanded = NONE; if (first_insert) { - first_insert = 0; + first_insert = false; if (inslen == 0) { inslen = saved_inslen; return (redo_insert(0)); @@ -4105,12 +4105,12 @@ vi_cmd(int argcnt, const char *cmd) * at this point, it's fairly reasonable that * nlen + olen + 2 doesn't overflow */ - nbuf = alloc(nlen + 1 + olen, APERM); + nbuf = alloc(nlen + 1 + olen, AEDIT); memcpy(nbuf, ap->val.s, nlen); nbuf[nlen++] = cmd[1]; if (macro.p) { memcpy(nbuf + nlen, macro.p, olen); - afree(macro.buf, APERM); + afree(macro.buf, AEDIT); nlen += olen; } else { nbuf[nlen++] = '\0'; @@ -4195,7 +4195,8 @@ vi_cmd(int argcnt, const char *cmd) hnum = hlast; if (es->linelen != 0) es->cursor++; - while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0) + while (putbuf(ybuf, yanklen, false) == 0 && + --argcnt > 0) ; if (es->cursor != 0) es->cursor--; @@ -4207,7 +4208,8 @@ vi_cmd(int argcnt, const char *cmd) modified = 1; hnum = hlast; any = 0; - while (putbuf(ybuf, yanklen, 0) == 0 && --argcnt > 0) + while (putbuf(ybuf, yanklen, false) == 0 && + --argcnt > 0) any = 1; if (any && es->cursor != 0) es->cursor--; @@ -4458,8 +4460,8 @@ vi_cmd(int argcnt, const char *cmd) argcnt++; p++; } - if (putbuf(" ", 1, 0) != 0 || - putbuf(sp, argcnt, 0) != 0) { + if (putbuf(" ", 1, false) != 0 || + putbuf(sp, argcnt, false) != 0) { if (es->cursor != 0) es->cursor--; return (-1); @@ -4602,7 +4604,8 @@ domove(int argcnt, const char *cmd, int sub) t = fsavecmd > 'a'; if (*cmd == ',') t = !t; - if ((ncursor = findch(fsavech, argcnt, t, i)) < 0) + if ((ncursor = findch(fsavech, argcnt, tobool(t), + tobool(i))) < 0) return (-1); if (sub && t) ncursor++; @@ -4702,7 +4705,7 @@ static int redo_insert(int count) { while (count-- > 0) - if (putbuf(ibuf, inslen, insert == REPLACE) != 0) + if (putbuf(ibuf, inslen, tobool(insert == REPLACE)) != 0) return (-1); if (es->cursor > 0) es->cursor--; @@ -4753,9 +4756,9 @@ bracktype(int ch) static void save_cbuf(void) { - memmove(holdbuf, es->cbuf, es->linelen); + memmove(holdbufp, es->cbuf, es->linelen); holdlen = es->linelen; - holdbuf[holdlen] = '\0'; + holdbufp[holdlen] = '\0'; } static void @@ -4763,7 +4766,7 @@ restore_cbuf(void) { es->cursor = 0; es->linelen = holdlen; - memmove(es->cbuf, holdbuf, holdlen); + memmove(es->cbuf, holdbufp, holdlen); } /* return a new edstate */ @@ -4772,8 +4775,8 @@ save_edstate(struct edstate *old) { struct edstate *news; - news = alloc(sizeof(struct edstate), APERM); - news->cbuf = alloc(old->cbufsize, APERM); + news = alloc(sizeof(struct edstate), AEDIT); + news->cbuf = alloc(old->cbufsize, AEDIT); memcpy(news->cbuf, old->cbuf, old->linelen); news->cbufsize = old->cbufsize; news->linelen = old->linelen; @@ -4795,8 +4798,8 @@ restore_edstate(struct edstate *news, struct edstate *old) static void free_edstate(struct edstate *old) { - afree(old->cbuf, APERM); - afree(old, APERM); + afree(old->cbuf, AEDIT); + afree(old, AEDIT); } /* @@ -4805,11 +4808,11 @@ free_edstate(struct edstate *old) static int x_vi_putbuf(const char *s, size_t len) { - return (putbuf(s, len, 0)); + return (putbuf(s, len, false)); } static int -putbuf(const char *buf, ssize_t len, int repl) +putbuf(const char *buf, ssize_t len, bool repl) { if (len == 0) return (0); @@ -4839,7 +4842,7 @@ del_range(int a, int b) } static int -findch(int ch, int cnt, int forw, int incl) +findch(int ch, int cnt, bool forw, bool incl) { int ncursor; @@ -5035,8 +5038,8 @@ grabsearch(int save, int start, int fwd, const char *pat) start--; anchored = *pat == '^' ? (++pat, 1) : 0; if ((hist = findhist(start, fwd, pat, anchored)) < 0) { - /* (start != 0 && fwd && match(holdbuf, pat) >= 0) */ - if (start != 0 && fwd && strcmp(holdbuf, pat) >= 0) { + /* (start != 0 && fwd && match(holdbufp, pat) >= 0) */ + if (start != 0 && fwd && strcmp(holdbufp, pat) >= 0) { restore_cbuf(); return (0); } else @@ -5275,7 +5278,7 @@ expand_word(int cmd) rval = -1; break; } - if (++i < nwords && putbuf(" ", 1, 0) != 0) { + if (++i < nwords && putbuf(" ", 1, false) != 0) { rval = -1; break; } @@ -5393,7 +5396,7 @@ complete_word(int cmd, int count) */ if (match_len > 0 && match[match_len - 1] != '/' && !(flags & XCF_IS_NOSPACE)) - rval = putbuf(" ", 1, 0); + rval = putbuf(" ", 1, false); } x_free_words(nwords, words); @@ -5450,7 +5453,7 @@ static void vi_macro_reset(void) { if (macro.p) { - afree(macro.buf, APERM); + afree(macro.buf, AEDIT); memset((char *)¯o, 0, sizeof(macro)); } } @@ -5471,8 +5474,11 @@ x_init(void) /* ^W */ edchars.werase = 027; - /* initialise Emacs command line editing mode */ + /* command line editing specific memory allocation */ ainit(AEDIT); + holdbufp = alloc(LINE, AEDIT); + + /* initialise Emacs command line editing mode */ x_nextcmd = -1; x_tab = alloc2(X_NTABS, sizeof(*x_tab), AEDIT); diff --git a/lex.c b/lex.c index 95e1ed5..e58d8b8 100644 --- a/lex.c +++ b/lex.c @@ -23,7 +23,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.187 2013/07/26 20:33:38 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.188 2013/08/10 13:44:31 tg Exp $"); /* * states while lexing word @@ -1387,7 +1387,7 @@ getsc_line(Source *s) Flag(FEMACS) || Flag(FGMACS))) { int nread; - nread = x_read(xp, LINE); + nread = x_read(xp); if (nread < 0) /* read error */ nread = 0; diff --git a/mksh.1 b/mksh.1 index 2bb1010..ea47dce 100644 --- a/mksh.1 +++ b/mksh.1 @@ -1,4 +1,4 @@ -.\" $MirOS: src/bin/mksh/mksh.1,v 1.318 2013/07/21 18:47:21 tg Exp $ +.\" $MirOS: src/bin/mksh/mksh.1,v 1.319 2013/08/10 13:44:31 tg Exp $ .\" $OpenBSD: ksh.1,v 1.147 2013/06/13 19:43:09 millert Exp $ .\"- .\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, @@ -74,7 +74,7 @@ .\" with -mandoc, it might implement .Mx itself, but we want to .\" use our own definition. And .Dd must come *first*, always. .\" -.Dd $Mdocdate: July 21 2013 $ +.Dd $Mdocdate: August 10 2013 $ .\" .\" Check which macro package we use, and do other -mdoc setup. .\" @@ -6367,6 +6367,17 @@ all contributors, such as the Debian and OpenBSD projects. .\" Open Source licence. .\" See the documentation, CVS, and web site for details. +.Pp +The BSD daemon is Copyright \(co Marshall Kirk McKusick. +The complete legalese is at: +.Pa https://www.mirbsd.org/TaC\-mksh.txt +.\" +.\" This boils down to: feel free to use mksh.ico as application icon +.\" or shortcut for mksh or mksh/Win32; distro patches are ok (but we +.\" request they amend $KSH_VERSION when modifying mksh). Authors are +.\" Marshall Kirk McKusick (UCB), Rick Collette (ekkoBSD), Thorsten +.\" Glaser, Benny Siegert (MirBSD), Michael Langguth (mksh/Win32). +.\" .Sh CAVEATS .Nm only supports the Unicode BMP (Basic Multilingual Plane). @@ -6381,7 +6392,8 @@ This can cause issues with a to suddenly point to a local variable by accident; fixing this is hard. .Pp The parts of a pipeline, like below, are executed in subshells. -Thus, variable assignments inside them fail. +Thus, variable assignments inside them are not visible in the +surrounding execution environment. Use co-processes instead. .Bd -literal -offset indent foo \*(Ba bar \*(Ba read baz # will not change $baz @@ -6408,7 +6420,7 @@ $ /bin/sleep 666 && echo fubar .Ed .Pp This document attempts to describe -.Nm mksh\ R47 +.Nm mksh\ R48 and up, compiled without any options impacting functionality, such as .Dv MKSH_SMALL , diff --git a/sh.h b/sh.h index 5dd60a1..b869c65 100644 --- a/sh.h +++ b/sh.h @@ -164,9 +164,9 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.663 2013/07/26 20:33:39 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.664 2013/08/10 13:44:33 tg Exp $"); #endif -#define MKSH_VERSION "R47 2013/07/26" +#define MKSH_VERSION "R47 2013/08/10" /* arithmetic types: C implementation */ #if !HAVE_CAN_INTTYPES @@ -518,7 +518,7 @@ char *ucstrstr(char *, const char *); #define mkssert(e) do { } while (/* CONSTCOND */ 0) #endif -#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 471) +#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 481) #error Must run Build.sh to compile this. extern void thiswillneverbedefinedIhope(void); int @@ -991,7 +991,7 @@ EXTERN uint32_t builtin_flag; EXTERN char *current_wd; /* input line size */ -#define LINE 4096 +#define LINE (4096 - ALLOC_SIZE) /* * Minimum required space to work with on a line - if the prompt leaves * less space than this on a line, the prompt is truncated. @@ -1683,7 +1683,7 @@ void x_init(void); #ifdef DEBUG_LEAKS void x_done(void); #endif -int x_read(char *, size_t); +int x_read(char *); #endif void x_mkraw(int, mksh_ttyst *, bool); /* eval.c */