ibara / ash Goto Github PK
View Code? Open in Web Editor NEWPort of NetBSD sh(1) to OpenBSD.
Port of NetBSD sh(1) to OpenBSD.
Just adding as reference the changes I made in order for this project to build on linux - Ubuntu 18.04 LTS, in case you want to take it further.
I made no attempt to make it the very correct etc - just barely useful to get it to build. It seems working after very little testing, e.g. ./obj/ash -c 'echo hello world'
, but it did segfault on ./ash --version
.
I needed to install (apt) libedit-dev
, I guess I already had the other deps installed.
Built (after the changes below) with bmake obj && bmake
, and completed without warnings or errors.
Copied from oksh to the root dir: strlcpy.c
(unmodified), portable.h
and pconfig.h
modified as follows:
diff --git a/../oksh/portable.h b/./portable.h
index 899c974..50684df 100644
--- a/../oksh/portable.h
+++ b/./portable.h
@@ -116,6 +116,17 @@
#define nice(x) (int)0
#endif /* __HAIKU__ */
+#ifndef WCOREDUMP
+#define WCOREDUMP(x) __WCOREDUMP(x)
+#endif
+
+#ifndef __predict_true
+#define __predict_true(x) (x)
+#endif
+#ifndef __predict_false
+#define __predict_false(x) (x)
+#endif
+
#ifndef HAVE_SETRESGID
#define setresgid(x, y, z) setgid(x); setegid(y); setgid(z)
#endif /* !HAVE_SETRESGID */
diff --git a/../oksh/pconfig.h b/./pconfig.h
index f25eb8a..85ac605 100644
--- a/../oksh/pconfig.h
+++ b/./pconfig.h
@@ -1,5 +1,6 @@
/* This file automatically generated by configure. */
+#define __unused
#define __dead __attribute__((__noreturn__))
#define HAVE_ASPRINTF
#define HAVE_CONFSTR
and the changes to files of this project:
diff --git a/Makefile b/Makefile
index ddaa428..5725184 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ PROG= ash
SHSRCS= alias.c arith_token.c arithmetic.c cd.c echo.c error.c eval.c exec.c \
expand.c histedit.c input.c jobs.c mail.c main.c memalloc.c \
miscbltin.c mystring.c options.c parser.c redir.c show.c trap.c \
- output.c var.c test.c kill.c syntax.c
+ output.c var.c test.c kill.c syntax.c strlcpy.c
GENSRCS=builtins.c init.c nodes.c
GENHDRS=builtins.h nodes.h token.h nodenames.h optinit.h
SRCS= ${SHSRCS} ${GENSRCS}
@@ -15,14 +15,14 @@ BUILDFIRST=${GENHDRS} ${GENSRCS}
DPSRCS+=${GENHDRS}
-LDADD+= -ledit -ltermcap
+LDADD+= -ledit -ltermcap -lbsd
DPADD+= ${LIBEDIT} ${LIBTERMCAP}
# Environment for scripts executed during build.
SCRIPT_ENV= \
AWK=/usr/bin/awk \
- MKTEMP=/usr/bin/mktemp \
- SED=/usr/bin/sed
+ MKTEMP=/bin/mktemp \
+ SED=/bin/sed
CPPFLAGS+=-DSHELL -I. -I${.CURDIR}
CPPFLAGS+= -DUSE_LRAND48
@@ -47,26 +47,26 @@ SRCS+=printf.c
CLEANFILES+= ${GENSRCS} ${GENHDRS} sh.html1
CLEANFILES+= trace.*
-token.h: mktokens
+token.h: ./mktokens
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC}
.ORDER: builtins.h builtins.c
-builtins.h builtins.c: mkbuiltins shell.h builtins.def
+builtins.h builtins.c: ./mkbuiltins shell.h builtins.def
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
[ -f builtins.h ]
-init.c: mkinit.sh ${SHSRCS}
+init.c: ./mkinit.sh ${SHSRCS}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC}
.ORDER: nodes.h nodes.c
-nodes.c nodes.h: mknodes.sh nodetypes nodes.c.pat
+nodes.c nodes.h: ./mknodes.sh nodetypes nodes.c.pat
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
[ -f nodes.h ]
-nodenames.h: mknodenames.sh nodes.h
+nodenames.h: ./mknodenames.sh nodes.h
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} > ${.TARGET}
-optinit.h: mkoptions.sh option.list
+optinit.h: ./mkoptions.sh option.list
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.TARGET} ${.OBJDIR}
SUBDIR.roff+=USD.doc
diff --git a/error.c b/error.c
index 88e14b8..36c6d12 100644
--- a/error.c
+++ b/error.c
@@ -60,6 +60,8 @@ __RCSID("$NetBSD: error.c,v 1.43 2019/02/04 11:16:41 kre Exp $");
#include "error.h"
#include "show.h"
+#define getprogname(x) commandname
+
/*
* Code to handle exceptions in C.
diff --git a/error.h b/error.h
index 67dc864..e64add6 100644
--- a/error.h
+++ b/error.h
@@ -118,6 +118,6 @@ void sh_exit(int) __dead;
*/
#if defined(BSD) && !defined(__SVR4)
-#define setjmp(jmploc) _setjmp(jmploc)
-#define longjmp(jmploc, val) _longjmp(jmploc, val)
+//#define setjmp(jmploc) _setjmp(jmploc)
+//#define longjmp(jmploc, val) _longjmp(jmploc, val)
#endif
diff --git a/eval.c b/eval.c
index 1e96443..1c40984 100644
--- a/eval.c
+++ b/eval.c
@@ -1303,8 +1303,8 @@ evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
argptr = argv + 1;
optptr = NULL;
/* and getopt */
- optreset = 1;
- optind = 1;
+ //optreset = 1;
+ optind = 0;
builtin_flags = flags;
exitstatus = cmdentry.u.bltin(argc, argv);
} else {
diff --git a/histedit.c b/histedit.c
index 71a88c2..a053b99 100644
--- a/histedit.c
+++ b/histedit.c
@@ -290,7 +290,8 @@ histcmd(volatile int argc, char ** volatile argv)
if (argc == 1)
error("missing history argument");
- optreset = 1; optind = 1; /* initialize getopt */
+ //optreset = 1; optind = 1; /* initialize getopt */
+ optind = 0; /* initialize getopt */
while (not_fcnumber(argv[optind]) &&
(ch = getopt(argc, argv, ":e:lnrs")) != -1)
switch ((char)ch) {
diff --git a/jobs.c b/jobs.c
index e1f54da..ee78301 100644
--- a/jobs.c
+++ b/jobs.c
@@ -58,6 +58,8 @@ __RCSID("$NetBSD: jobs.c,v 1.107 2020/02/07 02:06:12 kre Exp $");
#endif
#include <sys/ioctl.h>
+#include <sys/wait.h>
+
#include "shell.h"
#if JOBS
#if OLD_TTY_DRIVER
diff --git a/main.c b/main.c
index 83016ca..45faba5 100644
--- a/main.c
+++ b/main.c
@@ -55,6 +55,7 @@ __RCSID("$NetBSD: main.c,v 1.85 2020/02/07 01:25:08 fox Exp $");
#include <locale.h>
#include <fcntl.h>
+#include <sys/types.h>
#include "shell.h"
#include "main.h"
@@ -207,8 +208,8 @@ main(int argc, char **argv)
* by requiring the -p flag
*/
if (!pflag && (uid != geteuid() || gid != getegid())) {
- setuid(uid);
- setgid(gid);
+ if (!setuid(uid)) fprintf(stderr, "setuid failed\n");
+ if (!setgid(gid)) fprintf(stderr, "setgid failed\n");
/* PS1 might need to be changed accordingly. */
choose_ps1();
}
diff --git a/memalloc.c b/memalloc.c
index 9502794..6e55a0e 100644
--- a/memalloc.c
+++ b/memalloc.c
@@ -43,6 +43,7 @@ __RCSID("$NetBSD: memalloc.c,v 1.33 2019/02/09 03:35:55 kre Exp $");
#include <stdlib.h>
#include <unistd.h>
+#include <stdio.h>
#include "shell.h"
#include "output.h"
@@ -154,7 +155,8 @@ void
stunalloc(pointer p)
{
if (p == NULL) { /*DEBUG */
- write(2, "stunalloc\n", 10);
+ if (write(2, "stunalloc\n", 10) < 0)
+ fprintf(stderr, "write failed\n");
abort();
}
stacknleft += stacknxt - (char *)p;
diff --git a/miscbltin.c b/miscbltin.c
index 8967dc7..a1c3dad 100644
--- a/miscbltin.c
+++ b/miscbltin.c
@@ -55,6 +55,7 @@ __RCSID("$NetBSD: miscbltin.c,v 1.44 2017/05/13 15:03:34 gson Exp $");
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
+#include <bsd/unistd.h>
#include "shell.h"
#include "options.h"
diff --git a/printf.c b/printf.c
index 8c7366a..de7437c 100644
--- a/printf.c
+++ b/printf.c
@@ -65,6 +65,41 @@ __RCSID("$NetBSD: printf.c,v 1.50 2019/07/22 17:34:31 kre Exp $");
#define ESCAPE 033
#endif
+int vasprintf(char **p, const char *fmt, va_list args) {
+ va_list cp;
+ int len;
+ char c, buf[] = {' ', ' ', 0}; /* avoid trunc warning (static analysis) */
+ *p = NULL;
+
+ va_copy(cp, args);
+ /* runtime compatibility probe. performance penalty should be negligible */
+ len = (2 == snprintf(NULL, 0, "%s", buf)) ? vsnprintf(NULL, 0, fmt, cp) /* C99 */
+ : (2 == snprintf(&c, 1, "%s", buf)) ? vsnprintf(&c, 1, fmt, cp) /* *nix */
+ : -1;
+ va_end(cp);
+
+ /* INT_MAX is valid, but disallow to prevent accidental overflows later */
+ if (len < 0 || len == INT_MAX || !(*p = malloc(len + 1)))
+ return -1;
+
+ if (len != vsnprintf(*p, len + 1, fmt, args)) {
+ free(*p);
+ *p = NULL;
+ return -1;
+ }
+
+ return len;
+}
+int asprintf(char **p, const char *fmt, ...) {
+ int len;
+ va_list args;
+ va_start(args, fmt);
+ len = vasprintf(p, fmt, args);
+ va_end(args);
+ return len;
+}
+
+
static void conv_escape_str(char *, void (*)(int), int);
static char *conv_escape(char *, char *, int);
static char *conv_expand(const char *);
diff --git a/redir.c b/redir.c
index 60a8abc..0b34d47 100644
--- a/redir.c
+++ b/redir.c
@@ -523,7 +523,7 @@ copyfd(int from, int to, int cloexec)
int newfd;
if (cloexec && to > 2) {
-#ifdef O_CLOEXEC
+#if 0 && defined O_CLOEXEC
newfd = dup3(from, to, O_CLOEXEC);
#else
newfd = dup2(from, to);
@@ -947,7 +947,8 @@ fdflagscmd(int argc, char *argv[])
int verbose = 0, ch, pos = 0, neg = 0;
char *setflags = NULL;
- optreset = 1; optind = 1; /* initialize getopt */
+ //optreset = 1; optind = 1; /* initialize getopt */
+ optind = 0; /* initialize getopt */
while ((ch = getopt(argc, argv, ":vs:")) != -1)
switch ((char)ch) {
case 'v':
diff --git a/shell.h b/shell.h
index 318d56e..326b4ff 100644
--- a/shell.h
+++ b/shell.h
@@ -219,6 +219,9 @@ extern void set_debug(const char *, int);
#define VFORK_END
#define VFORK_UNDO()
+#include "pconfig.h"
+#include "portable.h"
+
#endif /* DEBUG */
#endif /* SHELL_H */
diff --git a/syntax.h b/syntax.h
index 097077a..146f504 100644
--- a/syntax.h
+++ b/syntax.h
@@ -35,6 +35,10 @@
#include <sys/cdefs.h>
#include <ctype.h>
+#ifdef CEOF
+#undef CEOF
+#endif
+
/* Syntax classes */
#define CWORD 0 /* character is nothing special */
#define CNL 1 /* newline character */
diff --git a/test.c b/test.c
index d2fbb25..9ed0511 100644
--- a/test.c
+++ b/test.c
@@ -29,6 +29,8 @@ __RCSID("$NetBSD: test.c,v 1.43 2018/09/13 22:00:58 kre Exp $");
#include <unistd.h>
#include <stdarg.h>
+#include "portable.h"
+
#define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
/* test(1) accepts the following grammar:
diff --git a/trap.c b/trap.c
index 2c6116e..8eaa8bd 100644
--- a/trap.c
+++ b/trap.c
@@ -102,6 +102,8 @@ static int getsigaction(int, sig_t *);
STATIC const char *trap_signame(int);
void printsignals(struct output *, int);
+const char * const sys_signame[NSIG] = {0};
+
const char *
signalname(int sig)
{
Just FYI.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.