optimise error messages, option parsing, and make more builtins

recognise "--", costs us 20 .text 0 .data 0 .bss
This commit is contained in:
tg 2010-09-05 19:51:35 +00:00
parent 07b97d5444
commit 56a69907d5
6 changed files with 51 additions and 48 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.390 2010/08/28 20:22:13 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.391 2010/09/05 19:51:29 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 $
@ -25,7 +25,7 @@
# http://www.research.att.com/~gsf/public/ifs.sh
expected-stdout:
@(#)MIRBSD KSH R39 2010/08/28
@(#)MIRBSD KSH R39 2010/09/05
description:
Check version of shell.
stdin:

4
edit.c
View File

@ -25,7 +25,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.199 2010/08/28 20:22:15 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.200 2010/09/05 19:51:31 tg Exp $");
/*
* in later versions we might use libtermcap for this, but since external
@ -2403,7 +2403,7 @@ x_bind(const char *a1, const char *a2,
m1 = msg;
while (*c && m1 < (msg + sizeof(msg) - 3))
x_mapout2(*c++, &m1);
bi_errorf("%s: %s '%s'", "bind", "too long key sequence", msg);
bi_errorf("%s: %s", "too long key sequence", msg);
return (1);
}
#ifndef MKSH_SMALL

8
exec.c
View File

@ -22,7 +22,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.80 2010/08/28 20:22:17 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.81 2010/09/05 19:51:32 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL "/bin/sh"
@ -444,12 +444,12 @@ comexec(struct op *t, struct tbl *volatile tp, const char **ap,
/* undo effects of command */
fcflags = FC_BI|FC_FUNC|FC_PATH;
if (tp->val.f == c_builtin) {
if ((cp = *++ap) == NULL) {
if ((cp = *++ap) == NULL ||
(!strcmp(cp, "--") && (cp = *++ap) == NULL)) {
tp = NULL;
break;
}
tp = findcom(cp, FC_BI);
if (tp == NULL)
if ((tp = findcom(cp, FC_BI)) == NULL)
errorf("%s: %s: %s", T_builtin, cp, "not a builtin");
continue;
} else if (tp->val.f == c_exec) {

62
funcs.c
View File

@ -25,7 +25,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.160 2010/08/28 20:22:17 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.161 2010/09/05 19:51:33 tg Exp $");
#if HAVE_KILLPG
/*
@ -2678,7 +2678,7 @@ c_mknod(const char **wp)
goto c_mknod_failed;
} else if (mkfifo(argv[0], mode)) {
c_mknod_failed:
bi_errorf("%s: %s", *wp, strerror(errno));
bi_errorf("%s: %s", argv[0], strerror(errno));
c_mknod_err:
rv = 1;
}
@ -2693,13 +2693,6 @@ c_mknod(const char **wp)
}
#endif
/* dummy function, special case in comexec() */
int
c_builtin(const char **wp MKSH_A_UNUSED)
{
return (0);
}
/* test(1) accepts the following grammar:
oexpr ::= aexpr | aexpr "-o" oexpr ;
aexpr ::= nexpr | nexpr "-a" aexpr ;
@ -3397,13 +3390,18 @@ c_rename(const char **wp)
{
int rv = 1;
if (wp == NULL /* argv */ ||
wp[0] == NULL /* name of builtin */ ||
wp[1] == NULL /* first argument */ ||
wp[2] == NULL /* second argument */ ||
wp[3] != NULL /* no further args please */)
/* skip argv[0] */
++wp;
if (wp[0] && !strcmp(wp[0], "--"))
/* skip "--" (options separator) */
++wp;
/* check for exactly two arguments */
if (wp[0] == NULL /* first argument */ ||
wp[1] == NULL /* second argument */ ||
wp[2] != NULL /* no further args please */)
bi_errorf(T_synerr);
else if ((rv = rename(wp[1], wp[2])) != 0) {
else if ((rv = rename(wp[0], wp[1])) != 0) {
rv = errno;
bi_errorf("%s: %s", "failed", strerror(rv));
}
@ -3417,30 +3415,24 @@ c_realpath(const char **wp)
int rv = 1;
char *buf;
if (wp != NULL && wp[0] != NULL && wp[1] != NULL) {
if (strcmp(wp[1], "--")) {
if (wp[2] == NULL) {
wp += 1;
rv = 0;
}
} else {
if (wp[2] != NULL && wp[3] == NULL) {
wp += 2;
rv = 0;
}
}
}
/* skip argv[0] */
++wp;
if (wp[0] && !strcmp(wp[0], "--"))
/* skip "--" (options separator) */
++wp;
if (rv)
/* check for exactly one argument */
if (wp[0] == NULL || wp[1] != NULL)
bi_errorf(T_synerr);
else if ((buf = do_realpath(*wp)) == NULL) {
else if ((buf = do_realpath(wp[0])) == NULL) {
rv = errno;
bi_errorf("%s: %s", *wp, strerror(rv));
bi_errorf("%s: %s", wp[0], strerror(rv));
if ((unsigned int)rv > 255)
rv = 255;
} else {
shprintf("%s\n", buf);
afree(buf, ATEMP);
rv = 0;
}
return (rv);
@ -3462,9 +3454,11 @@ c_cat(const char **wp)
return (1);
}
++wp; /* argv[0] */
if (wp[0] && wp[0][0] == '-' && wp[0][1] == '-' && wp[0][2] == '\0')
++wp; /* "--" (options separator) */
/* skip argv[0] */
++wp;
if (wp[0] && !strcmp(wp[0], "--"))
/* skip "--" (options separator) */
++wp;
do {
if (*wp) {

14
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.238 2010/09/05 19:12:24 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.239 2010/09/05 19:51:33 tg Exp $
.\" $OpenBSD: ksh.1,v 1.136 2010/07/15 20:04:35 schwarze Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
@ -2961,7 +2961,11 @@ loop.
.Ar level
defaults to 1.
.Pp
.It Ic builtin Ar command Op Ar arg ...
.It Xo
.Ic builtin
.Op Fl \-
.Ar command Op Ar arg ...
.Xc
Execute the built-in command
.Ar command .
.Pp
@ -3657,7 +3661,11 @@ it's also checked for existence and whether it is a directory; otherwise,
returns 0 if the pathname either exists or can be created immediately,
i.e. all but the last component exist and are directories.
.Pp
.It Ic rename Ar from to
.It Xo
.Ic rename
.Op Fl \-
.Ar from to
.Xc
Renames the file
.Ar from
to

7
sh.h
View File

@ -150,9 +150,9 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.411 2010/08/28 20:22:22 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.412 2010/09/05 19:51:35 tg Exp $");
#endif
#define MKSH_VERSION "R39 2010/08/28"
#define MKSH_VERSION "R39 2010/09/05"
#ifndef MKSH_INCLUDES_ONLY
@ -1475,7 +1475,8 @@ int c_times(const char **);
int timex(struct op *, int, volatile int *);
void timex_hook(struct op *, char ** volatile *);
int c_exec(const char **);
int c_builtin(const char **);
/* dummy function (just need pointer value), special case in comexec() */
#define c_builtin shcomexec
int c_test(const char **);
#if HAVE_MKNOD
int c_mknod(const char **);