Skip to content

Commit

Permalink
glibc/libbsd and musl port of 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 3, 2016
1 parent 1689b91 commit 41570f1
Show file tree
Hide file tree
Showing 13 changed files with 804 additions and 141 deletions.
51 changes: 39 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
# $OpenBSD: Makefile,v 1.33 2016/03/30 06:38:40 jmc Exp $
CC ?= cc
CFLAGS ?= -O2
CFLAGS += -I. -D_GNU_SOURCE -std=gnu99 -Wall -pedantic -Wno-format-security -Wno-pointer-sign
LDFLAGS ?=
DESTDIR ?= /
PREFIX ?= /usr
BIN_DIR ?= $(PREFIX)/bin
BIN_NAME ?= ksh
MAN_DIR ?= $(PREFIX)/share/man
DOC_DIR ?= $(PREFIX)/share/doc/loksh
HAVE_LIBBSD ?= 0

PROG= ksh
SRCS= alloc.c c_ksh.c c_sh.c c_test.c c_ulimit.c edit.c emacs.c eval.c \
exec.c expr.c history.c io.c jobs.c lex.c mail.c main.c \
misc.c path.c shf.c syn.c table.c trap.c tree.c tty.c var.c \
version.c vi.c
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
HEADERS = c_test.h charclass.h config.h edit.h expand.h ksh_limval.h lex.h \
proto.h sh.h shf.h table.h tree.h tty.h

DEFS= -Wall
CFLAGS+=${DEFS} -I. -I${.CURDIR} -I${.CURDIR}/../../lib/libc/gen
MAN= ksh.1 sh.1
ifeq (1,$(HAVE_LIBBSD))
CFLAGS += $(shell pkg-config --cflags libbsd-overlay)
LDFLAGS += $(shell pkg-config --libs libbsd-overlay)
endif

LINKS= ${BINDIR}/ksh ${BINDIR}/rksh
LINKS+= ${BINDIR}/ksh ${BINDIR}/sh
all: ksh

.include <bsd.prog.mk>
%.o: %.c $(HEADERS)
$(CC) -c -o $@ $< $(CFLAGS)

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

clean:
rm -f $(BIN_NAME) *.o

install: all
install -v -D -m 755 ksh $(DESTDIR)/$(BIN_DIR)/$(BIN_NAME)
install -v -D -m 644 ksh.1 $(DESTDIR)/$(MAN_DIR)/man1/$(BIN_NAME).1
install -v -m 644 sh.1 $(DESTDIR)/$(MAN_DIR)/man1/sh.1
install -v -D -m 644 README $(DESTDIR)/$(DOC_DIR)/README
install -v -m 644 README.upstream $(DESTDIR)/$(DOC_DIR)/README.upstream
install -v -m 644 CONTRIBUTORS $(DESTDIR)/$(DOC_DIR)/CONTRIBUTORS
install -v -m 644 PROJECTS $(DESTDIR)/$(DOC_DIR)/PROJETCS
install -v -m 644 LEGAL $(DESTDIR)/$(DOC_DIR)/LEGAL
30 changes: 14 additions & 16 deletions README
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
$OpenBSD: README,v 1.15 2015/12/05 19:40:45 mmcc Exp $
_ _ _
| | ___ | | _____| |__
| |/ _ \| |/ / __| '_ \
| | (_) | <\__ \ | | |
|_|\___/|_|\_\___/_| |_|

Last updated Jul '99 for pdksh-5.2.14.
Overview
========

PD-ksh is a mostly complete AT&T ksh look-alike (see NOTES file for a list
of things not supported). Work is mostly finished to make it fully
compatible with both POSIX and AT&T ksh (when the two don't conflict).
loksh is a Linux (https://www.kernel.org/) port of OpenBSD's
(http://www.openbsd.org/) ksh targeting musl (http://www.musl-libc.org/).

PDksh was being maintained by Michael Rendell ([email protected]),
who took over from Simon J. Gerraty ([email protected]) at the later's
suggestion.
It is a small, interactive shell targeted at resource-constrained systems.

Files of interest:
CONTRIBUTORS short history of pdksh, people who contributed, etc.
NOTES lists of known bugs in pdksh, at&t ksh, and posix.
PROJECTS list of things that need to be done in pdksh.
LEGAL A file detailing legal issues concerning pdksh.
Legal Information
=================

loksh is licensed under the same license as the upstream code it is based on.

BTW, THE MOST FREQUENTLY REPORTED BUG IS
echo hi | read a; echo $a # Does not print hi
I'm aware of this and there is no need to report it.
The ASCII art logo at the top was made using FIGlet (http://www.figlet.org/).
22 changes: 22 additions & 0 deletions README.upstream
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$OpenBSD: README,v 1.15 2015/12/05 19:40:45 mmcc Exp $

Last updated Jul '99 for pdksh-5.2.14.

PD-ksh is a mostly complete AT&T ksh look-alike (see NOTES file for a list
of things not supported). Work is mostly finished to make it fully
compatible with both POSIX and AT&T ksh (when the two don't conflict).

PDksh was being maintained by Michael Rendell ([email protected]),
who took over from Simon J. Gerraty ([email protected]) at the later's
suggestion.

Files of interest:
CONTRIBUTORS short history of pdksh, people who contributed, etc.
NOTES lists of known bugs in pdksh, at&t ksh, and posix.
PROJECTS list of things that need to be done in pdksh.
LEGAL A file detailing legal issues concerning pdksh.


BTW, THE MOST FREQUENTLY REPORTED BUG IS
echo hi | read a; echo $a # Does not print hi
I'm aware of this and there is no need to report it.
21 changes: 0 additions & 21 deletions c_sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,26 +826,6 @@ c_exec(char **wp)
return 0;
}

static int
c_suspend(char **wp)
{
if (wp[1] != NULL) {
bi_errorf("too many arguments");
return 1;
}
if (Flag(FLOGIN)) {
/* Can't suspend an orphaned process group. */
pid_t parent = getppid();
if (getpgid(parent) == getpgid(0) ||
getsid(parent) != getsid(0)) {
bi_errorf("can't suspend a login shell");
return 1;
}
}
j_suspend();
return 0;
}

/* dummy function, special case in comexec() */
int
c_builtin(char **wp)
Expand Down Expand Up @@ -884,6 +864,5 @@ const struct builtin shbuiltins [] = {
{"ulimit", c_ulimit},
{"+umask", c_umask},
{"*=unset", c_unset},
{"suspend", c_suspend},
{NULL, NULL}
};
29 changes: 29 additions & 0 deletions charclass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Public domain, 2008, Todd C. Miller <[email protected]>
*
* $OpenBSD: charclass.h,v 1.1 2008/10/01 23:04:13 millert Exp $
*/

/*
* POSIX character class support for fnmatch() and glob().
*/
static struct cclass {
const char *name;
int (*isctype)(int);
} cclasses[] = {
{ "alnum", isalnum },
{ "alpha", isalpha },
{ "blank", isblank },
{ "cntrl", iscntrl },
{ "digit", isdigit },
{ "graph", isgraph },
{ "lower", islower },
{ "print", isprint },
{ "punct", ispunct },
{ "space", isspace },
{ "upper", isupper },
{ "xdigit", isxdigit },
{ NULL, NULL }
};

#define NCCLASSES (sizeof(cclasses) / sizeof(cclasses[0]) - 1)
7 changes: 2 additions & 5 deletions exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,6 @@ do_selectargs(char **ap, bool print_menu)
static const char *const read_args[] = {
"read", "-r", "REPLY", NULL
};
const char *errstr;
char *s;
int i, argct;

Expand All @@ -1261,10 +1260,8 @@ do_selectargs(char **ap, bool print_menu)
return NULL;
s = str_val(global("REPLY"));
if (*s) {
i = strtonum(s, 1, argct, &errstr);
if (errstr)
return null;
return ap[i - 1];
i = atoi(s);
return (i >= 1 && i <= argct) ? ap[i - 1] : null;
}
print_menu = 1;
}
Expand Down
2 changes: 2 additions & 0 deletions history.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* to work on your system
*/

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
Expand All @@ -25,6 +26,7 @@
#include "sh.h"

#ifdef HISTORY
# include <sys/file.h>
# include <sys/mman.h>

/*
Expand Down
69 changes: 6 additions & 63 deletions jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ static int kill_job(Job *, int);
void
j_init(int mflagset)
{
#ifdef CHILD_MAX
child_max = CHILD_MAX; /* so syscon() isn't always being called */
#else
child_max = sysconf(_SC_CHILD_MAX);
#endif

sigemptyset(&sm_default);
sigprocmask(SIG_SETMASK, &sm_default, NULL);
Expand Down Expand Up @@ -195,59 +199,6 @@ j_init(int mflagset)
tty_init(true);
}

/* suspend the shell */
void
j_suspend(void)
{
struct sigaction sa, osa;

/* Restore tty and pgrp. */
if (ttypgrp_ok) {
tcsetattr(tty_fd, TCSADRAIN, &tty_state);
if (restore_ttypgrp >= 0) {
if (tcsetpgrp(tty_fd, restore_ttypgrp) < 0) {
warningf(false,
"j_suspend: tcsetpgrp() failed: %s",
strerror(errno));
} else {
if (setpgid(0, restore_ttypgrp) < 0) {
warningf(false,
"j_suspend: setpgid() failed: %s",
strerror(errno));
}
}
}
}

/* Suspend the shell. */
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &sa, &osa);
kill(0, SIGTSTP);

/* Back from suspend, reset signals, pgrp and tty. */
sigaction(SIGTSTP, &osa, NULL);
if (ttypgrp_ok) {
if (restore_ttypgrp >= 0) {
if (setpgid(0, kshpid) < 0) {
warningf(false,
"j_suspend: setpgid() failed: %s",
strerror(errno));
ttypgrp_ok = 0;
} else {
if (tcsetpgrp(tty_fd, kshpid) < 0) {
warningf(false,
"j_suspend: tcsetpgrp() failed: %s",
strerror(errno));
ttypgrp_ok = 0;
}
}
}
tty_init(true);
}
}

/* job cleanup before shell exit */
void
j_exit(void)
Expand Down Expand Up @@ -1435,17 +1386,11 @@ static Job *
j_lookup(const char *cp, int *ecodep)
{
Job *j, *last_match;
const char *errstr;
Proc *p;
int len, job = 0;

if (digit(*cp)) {
job = strtonum(cp, 1, INT_MAX, &errstr);
if (errstr) {
if (ecodep)
*ecodep = JL_NOSUCH;
return NULL;
}
job = atoi(cp);
/* Look for last_proc->pid (what $! returns) first... */
for (j = job_list; j != NULL; j = j->next)
if (j->last_proc && j->last_proc->pid == job)
Expand Down Expand Up @@ -1480,9 +1425,7 @@ j_lookup(const char *cp, int *ecodep)

case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
job = strtonum(cp, 1, INT_MAX, &errstr);
if (errstr)
break;
job = atoi(cp);
for (j = job_list; j != NULL; j = j->next)
if (j->job == job)
return j;
Expand Down
10 changes: 4 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include "sh.h"

#ifndef _PW_NAME_LEN
# define _PW_NAME_LEN 32 /* see the useradd(8) man page */
#endif

extern char **environ;

/*
Expand Down Expand Up @@ -152,12 +156,6 @@ main(int argc, char *argv[])

kshname = argv[0];

if (pledge("stdio rpath wpath cpath fattr flock getpw proc exec tty",
NULL) == -1) {
perror("pledge");
exit(1);
}

ainit(&aperm); /* initialize permanent Area */

/* set up base environment */
Expand Down
3 changes: 2 additions & 1 deletion sh.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include <setjmp.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <stdbool.h>
#include <fcntl.h>

/* end of common headers */

Expand Down Expand Up @@ -502,7 +504,6 @@ void coproc_cleanup(int);
struct temp *maketemp(Area *, Temp_type, struct temp **);
/* jobs.c */
void j_init(int);
void j_suspend(void);
void j_exit(void);
void j_change(void);
int exchild(struct op *, int, volatile int *, int);
Expand Down
Loading

0 comments on commit 41570f1

Please sign in to comment.