Skip to content

Commit

Permalink
Rebased against ksh from OpenBSD 6.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Krasner committed Oct 18, 2018
2 parents e52701b + 9addb72 commit d72102d
Show file tree
Hide file tree
Showing 22 changed files with 233 additions and 99 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ BIN_NAME ?= ksh
MAN_DIR ?= $(PREFIX)/share/man
DOC_DIR ?= $(PREFIX)/share/doc/loksh

NCURSES_CFLAGS = $(shell pkg-config --cflags ncurses)
NCURSES_LDFLAGS = $(shell pkg-config --libs ncurses)

OBJECTS = alloc.o c_ksh.o c_sh.o c_test.o c_ulimit.o edit.o emacs.o eval.o \
exec.o expr.o history.o io.o jobs.o lex.o mail.o main.o misc.o \
path.o shf.o syn.o table.o trap.o tree.o tty.o var.o version.o vi.o \
Expand All @@ -19,10 +22,10 @@ HEADERS = c_test.h charclass.h config.h edit.h expand.h ksh_limval.h lex.h \
all: ksh

%.o: %.c $(HEADERS)
$(CC) -c -o $@ $< $(CFLAGS)
$(CC) -c -o $@ $< $(CFLAGS) $(NCURSES_CFLAGS)

ksh: $(OBJECTS)
$(CC) -o $@ $^ $(LDFLAGS)
$(CC) -o $@ $^ $(LDFLAGS) $(NCURSES_LDFLAGS)

clean:
rm -f $(BIN_NAME) *.o
Expand Down
34 changes: 29 additions & 5 deletions c_ksh.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: c_ksh.c,v 1.59 2018/03/15 16:51:29 anton Exp $ */
/* $OpenBSD: c_ksh.c,v 1.61 2018/05/18 13:25:20 benno Exp $ */

/*
* built-in Korn commands: c_*
Expand Down Expand Up @@ -410,9 +410,26 @@ c_whence(char **wp)
int pflag = 0, vflag = 0, Vflag = 0;
int ret = 0;
int optc;
int iam_whence = wp[0][0] == 'w';
int iam_whence;
int fcflags;
const char *options = iam_whence ? "pv" : "pvV";
const char *options;

switch (wp[0][0]) {
case 'c': /* command */
iam_whence = 0;
options = "pvV";
break;
case 't': /* type */
vflag = 1;
/* FALLTHROUGH */
case 'w': /* whence */
iam_whence = 1;
options = "pv";
break;
default:
bi_errorf("builtin not handled by %s", __func__);
return 1;
}

while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
switch (optc) {
Expand All @@ -430,7 +447,6 @@ c_whence(char **wp)
}
wp += builtin_opt.optind;


fcflags = FC_BI | FC_PATH | FC_FUNC;
if (!iam_whence) {
/* Note that -p on its own is dealt with in comexec() */
Expand Down Expand Up @@ -530,6 +546,13 @@ c_command(char **wp)
return c_whence(wp);
}

int
c_type(char **wp)
{
/* Let c_whence do the work. type = command -V = whence -v */
return c_whence(wp);
}

/* typeset, export, and readonly */
int
c_typeset(char **wp)
Expand Down Expand Up @@ -1017,7 +1040,7 @@ int
c_let(char **wp)
{
int rv = 1;
long val;
int64_t val;

if (wp[1] == NULL) /* at&t ksh does this */
bi_errorf("no arguments");
Expand Down Expand Up @@ -1392,6 +1415,7 @@ const struct builtin kshbuiltins [] = {
{"print", c_print},
{"pwd", c_pwd},
{"*=readonly", c_typeset},
{"type", c_type},
{"=typeset", c_typeset},
{"+unalias", c_unalias},
{"whence", c_whence},
Expand Down
4 changes: 2 additions & 2 deletions c_sh.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: c_sh.c,v 1.62 2017/12/27 13:02:57 millert Exp $ */
/* $OpenBSD: c_sh.c,v 1.63 2018/04/09 17:53:36 tobias Exp $ */

/*
* built-in Bourne commands
Expand Down Expand Up @@ -34,7 +34,7 @@ c_shift(char **wp)
{
struct block *l = genv->loc;
int n;
long val;
int64_t val;
char *arg;

if (ksh_getopt(wp, &builtin_opt, null) == '?')
Expand Down
4 changes: 2 additions & 2 deletions c_test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: c_test.c,v 1.24 2017/12/26 19:10:31 millert Exp $ */
/* $OpenBSD: c_test.c,v 1.25 2018/04/09 17:53:36 tobias Exp $ */

/*
* test(1); version 7-like -- author Erik Baalbergen
Expand Down Expand Up @@ -308,7 +308,7 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
case TO_INTLE: /* -le */
case TO_INTLT: /* -lt */
{
long v1, v2;
int64_t v1, v2;

if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR, false) ||
!evaluate(opnd2, &v2, KSH_RETURN_ERROR, false)) {
Expand Down
7 changes: 4 additions & 3 deletions c_ulimit.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: c_ulimit.c,v 1.27 2018/03/15 16:51:29 anton Exp $ */
/* $OpenBSD: c_ulimit.c,v 1.28 2018/04/09 17:53:36 tobias Exp $ */

/*
ulimit -- handle "ulimit" builtin
Expand All @@ -23,6 +23,7 @@

#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>

#include "sh.h"
Expand Down Expand Up @@ -140,7 +141,7 @@ set_ulimit(const struct limits *l, const char *v, int how)
if (strcmp(v, "unlimited") == 0)
val = RLIM_INFINITY;
else {
long rval;
int64_t rval;

if (!evaluate(v, &rval, KSH_RETURN_ERROR, false))
return 1;
Expand Down Expand Up @@ -188,6 +189,6 @@ print_ulimit(const struct limits *l, int how)
shprintf("unlimited\n");
else {
val /= l->factor;
shprintf("%ld\n", (long) val);
shprintf("%" PRIi64 "\n", (int64_t) val);
}
}
10 changes: 5 additions & 5 deletions edit.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: edit.c,v 1.64 2018/03/15 16:51:29 anton Exp $ */
/* $OpenBSD: edit.c,v 1.66 2018/06/18 17:03:58 millert Exp $ */

/*
* Command line editing - common code
Expand Down Expand Up @@ -81,10 +81,10 @@ check_sigwinch(void)
ws.ws_col;

if ((vp = typeset("COLUMNS", 0, 0, 0, 0)))
setint(vp, (long) ws.ws_col);
setint(vp, (int64_t) ws.ws_col);
}
if (ws.ws_row && (vp = typeset("LINES", 0, 0, 0, 0)))
setint(vp, (long) ws.ws_row);
setint(vp, (int64_t) ws.ws_row);
}
}
}
Expand Down Expand Up @@ -139,10 +139,10 @@ x_flush(void)
shf_flush(shl_out);
}

void
int
x_putc(int c)
{
shf_putc(c, shl_out);
return shf_putc(c, shl_out);
}

void
Expand Down
4 changes: 2 additions & 2 deletions edit.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: edit.h,v 1.11 2016/01/26 17:39:31 mmcc Exp $ */
/* $OpenBSD: edit.h,v 1.12 2018/06/18 17:03:58 millert Exp $ */

/* NAME:
* edit.h - globals for edit modes
Expand Down Expand Up @@ -37,7 +37,7 @@ extern X_chars edchars;
/* edit.c */
int x_getc(void);
void x_flush(void);
void x_putc(int);
int x_putc(int);
void x_puts(const char *);
bool x_mode(bool);
int promptlen(const char *, const char **);
Expand Down
37 changes: 31 additions & 6 deletions emacs.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: emacs.c,v 1.84 2018/01/16 17:17:18 jca Exp $ */
/* $OpenBSD: emacs.c,v 1.85 2018/06/18 17:03:58 millert Exp $ */

/*
* Emacs-like command line editing and history
Expand All @@ -21,13 +21,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef SMALL
# include <term.h>
# include <curses.h>
#endif

#include "sh.h"
#include "edit.h"

static Area aedit;
#define AEDIT &aedit /* area for kill ring and macro defns */

#undef CTRL
#define CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */
#define UNCTRL(x) ((x) == 0x7F ? '?' : (x) | 0x40) /* ASCII */

Expand Down Expand Up @@ -146,6 +151,7 @@ static int isu8cont(unsigned char);
/* proto's for keybindings */
static int x_abort(int);
static int x_beg_hist(int);
static int x_clear_screen(int);
static int x_comp_comm(int);
static int x_comp_file(int);
static int x_complete(int);
Expand Down Expand Up @@ -202,6 +208,7 @@ static int x_debug_info(int);
static const struct x_ftab x_ftab[] = {
{ x_abort, "abort", 0 },
{ x_beg_hist, "beginning-of-history", 0 },
{ x_clear_screen, "clear-screen", 0 },
{ x_comp_comm, "complete-command", 0 },
{ x_comp_file, "complete-file", 0 },
{ x_complete, "complete", 0 },
Expand Down Expand Up @@ -1004,12 +1011,19 @@ x_draw_line(int c)
{
x_redraw(-1);
return KSTD;
}

static int
x_clear_screen(int c)
{
x_redraw(-2);
return KSTD;
}

/* Redraw (part of) the line. If limit is < 0, the everything is redrawn
* on a NEW line, otherwise limit is the screen column up to which needs
* redrawing.
/* Redraw (part of) the line.
* A non-negative limit is the screen column up to which needs
* redrawing. A limit of -1 redraws on a new line, while a limit
* of -2 (attempts to) clear the screen.
*/
static void
x_redraw(int limit)
Expand All @@ -1018,9 +1032,20 @@ x_redraw(int limit)
char *cp;

x_adj_ok = 0;
if (limit == -1)
if (limit == -2) {
int cleared = 0;
#ifndef SMALL
if (cur_term != NULL && clear_screen != NULL) {
if (tputs(clear_screen, 1, x_putc) != ERR)
cleared = 1;
}
#endif
if (!cleared)
x_e_putc('\n');
}
else if (limit == -1)
x_e_putc('\n');
else
else if (limit >= 0)
x_e_putc('\r');
x_flush();
if (xbp == xbuf) {
Expand Down
41 changes: 36 additions & 5 deletions eval.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: eval.c,v 1.59 2018/01/16 22:52:32 jca Exp $ */
/* $OpenBSD: eval.c,v 1.63 2018/07/09 00:20:35 anton Exp $ */

/*
* Expansion - quoting, separation, substitution, globbing
Expand Down Expand Up @@ -58,6 +58,8 @@ static char *tilde(char *);
static char *homedir(char *);
static void alt_expand(XPtrV *, char *, char *, char *, int);

static struct tbl *varcpy(struct tbl *);

/* compile and expand word */
char *
substitute(const char *cp, int f)
Expand Down Expand Up @@ -190,7 +192,8 @@ expand(char *cp, /* input word */
doblank = 0;
make_magic = 0;
word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
st_head.next = NULL;

memset(&st_head, 0, sizeof(st_head));
st = &st_head;

while (1) {
Expand Down Expand Up @@ -305,7 +308,7 @@ expand(char *cp, /* input word */
st->stype = stype;
st->base = Xsavepos(ds, dp);
st->f = f;
st->var = x.var;
st->var = varcpy(x.var);
st->quote = quote;
/* skip qualifier(s) */
if (stype)
Expand Down Expand Up @@ -577,7 +580,7 @@ expand(char *cp, /* input word */
Xinit(ds, dp, 128, ATEMP);
}
if (c == 0)
return;
goto done;
if (word != IFS_NWS)
word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS;
} else {
Expand Down Expand Up @@ -682,6 +685,14 @@ expand(char *cp, /* input word */
word = IFS_WORD;
}
}

done:
for (st = &st_head; st != NULL; st = st->next) {
if (st->var == NULL || (st->var->flag & RDONLY) == 0)
continue;

afree(st->var, ATEMP);
}
}

/*
Expand Down Expand Up @@ -732,7 +743,7 @@ varsub(Expand *xp, char *sp, char *word,
if (Flag(FNOUNSET) && c == 0 && !zero_ok)
errorf("%s: parameter not set", sp);
*stypep = 0; /* unqualified variable/string substitution */
xp->str = str_save(ulton((unsigned long)c, 10), ATEMP);
xp->str = str_save(u64ton((uint64_t)c, 10), ATEMP);
return XSUB;
}

Expand Down Expand Up @@ -1286,3 +1297,23 @@ alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo)
}
return;
}

/*
* Copy the given variable if it's flagged as read-only.
* Such variables have static storage and only one can therefore be referenced
* at a time.
* This is necessary in order to allow variable expansion expressions to refer
* to multiple read-only variables.
*/
static struct tbl *
varcpy(struct tbl *vp)
{
struct tbl *cpy;

if (vp == NULL || (vp->flag & RDONLY) == 0)
return vp;

cpy = alloc(sizeof(struct tbl), ATEMP);
memcpy(cpy, vp, sizeof(struct tbl));
return cpy;
}
Loading

0 comments on commit d72102d

Please sign in to comment.