merge non-Plan9-specific stuff from the branch, add KNF, etc.

This commit is contained in:
tg
2006-08-24 20:32:53 +00:00
parent 38e8974e3e
commit c81966621a
6 changed files with 61 additions and 12 deletions

View File

@ -1,8 +1,13 @@
#!/bin/sh #!/bin/sh
# $MirOS: src/bin/mksh/Build.sh,v 1.49 2006/08/22 22:01:59 tg Exp $ # $MirOS: src/bin/mksh/Build.sh,v 1.50 2006/08/24 20:32:52 tg Exp $
#- #-
# Environment: CC, CFLAGS, CPPFLAGS, LDFLAGS, LIBS, NROFF # Environment: CC, CFLAGS, CPPFLAGS, LDFLAGS, LIBS, NROFF
if test -d mksh; then
echo "$0: Error: ./mksh is a directory!" >&2
exit 1
fi
: ${CFLAGS='-O2 -fno-strict-aliasing -fno-strength-reduce -Wall'} : ${CFLAGS='-O2 -fno-strict-aliasing -fno-strength-reduce -Wall'}
: ${CC=gcc} ${NROFF=nroff} : ${CC=gcc} ${NROFF=nroff}
curdir=`pwd` srcdir=`dirname "$0"` curdir=`pwd` srcdir=`dirname "$0"`

14
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.37 2006/08/22 22:49:34 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/edit.c,v 1.38 2006/08/24 20:32:52 tg Exp $");
/* tty driver characters we are interested in */ /* tty driver characters we are interested in */
typedef struct { typedef struct {
@ -42,9 +42,11 @@ void x_init_emacs(void);
void x_emacs_keys(X_chars *); void x_emacs_keys(X_chars *);
int x_vi(char *, size_t); int x_vi(char *, size_t);
#if defined(TIOCGWINSZ) && defined(SIGWINCH)
static void x_sigwinch(int); static void x_sigwinch(int);
static volatile sig_atomic_t got_sigwinch; static volatile sig_atomic_t got_sigwinch;
static void check_sigwinch(void); static void check_sigwinch(void);
#endif
static int path_order_cmp(const void *aa, const void *bb); static int path_order_cmp(const void *aa, const void *bb);
static char *add_glob(const char *, int); static char *add_glob(const char *, int);
@ -66,14 +68,17 @@ x_init(void)
/* default value for deficient systems */ /* default value for deficient systems */
edchars.werase = 027; /* ^W */ edchars.werase = 027; /* ^W */
#if defined(TIOCGWINSZ) && defined(SIGWINCH)
if (setsig(&sigtraps[SIGWINCH], x_sigwinch, if (setsig(&sigtraps[SIGWINCH], x_sigwinch,
SS_RESTORE_ORIG | SS_SHTRAP)) SS_RESTORE_ORIG | SS_SHTRAP))
sigtraps[SIGWINCH].flags |= TF_SHELL_USES; sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
check_sigwinch(); /* force initial check */ check_sigwinch(); /* force initial check */
#endif
x_init_emacs(); x_init_emacs();
} }
#if defined(TIOCGWINSZ) && defined(SIGWINCH)
/* ARGSUSED */ /* ARGSUSED */
static void static void
x_sigwinch(int sig __attribute__((unused))) x_sigwinch(int sig __attribute__((unused)))
@ -106,6 +111,7 @@ check_sigwinch(void)
setint(vp, (long)ws.ws_row); setint(vp, (long)ws.ws_row);
} }
} }
#endif
/* /*
* read an edited command line * read an edited command line
@ -123,7 +129,9 @@ x_read(char *buf, size_t len)
else else
i = -1; /* internal error */ i = -1; /* internal error */
x_mode(false); x_mode(false);
#if defined(TIOCGWINSZ) && defined(SIGWINCH)
check_sigwinch(); check_sigwinch();
#endif
return i; return i;
} }
@ -188,11 +196,15 @@ x_mode(bool onoff)
edchars.intr = cb.c_cc[VINTR]; edchars.intr = cb.c_cc[VINTR];
edchars.quit = cb.c_cc[VQUIT]; edchars.quit = cb.c_cc[VQUIT];
edchars.eof = cb.c_cc[VEOF]; edchars.eof = cb.c_cc[VEOF];
#ifdef VWERASE
edchars.werase = cb.c_cc[VWERASE]; edchars.werase = cb.c_cc[VWERASE];
#endif
cb.c_iflag &= ~(INLCR | ICRNL); cb.c_iflag &= ~(INLCR | ICRNL);
cb.c_lflag &= ~(ISIG | ICANON | ECHO); cb.c_lflag &= ~(ISIG | ICANON | ECHO);
#ifdef VLNEXT
/* osf/1 processes lnext when ~icanon */ /* osf/1 processes lnext when ~icanon */
cb.c_cc[VLNEXT] = _POSIX_VDISABLE; cb.c_cc[VLNEXT] = _POSIX_VDISABLE;
#endif
/* sunos 4.1.x & osf/1 processes discard(flush) when ~icanon */ /* sunos 4.1.x & osf/1 processes discard(flush) when ~icanon */
#ifdef VDISCARD #ifdef VDISCARD
cb.c_cc[VDISCARD] = _POSIX_VDISABLE; cb.c_cc[VDISCARD] = _POSIX_VDISABLE;

18
funcs.c
View File

@ -5,7 +5,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.29 2006/08/01 13:43:26 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.30 2006/08/24 20:32:52 tg Exp $");
int int
c_cd(char **wp) c_cd(char **wp)
@ -2049,13 +2049,17 @@ c_times(char **wp __attribute__((unused)))
{ {
struct rusage usage; struct rusage usage;
#ifdef RUSAGE_SELF
(void) getrusage(RUSAGE_SELF, &usage); (void) getrusage(RUSAGE_SELF, &usage);
p_time(shl_stdout, 0, &usage.ru_utime, 0, NULL, " "); p_time(shl_stdout, 0, &usage.ru_utime, 0, NULL, " ");
p_time(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n"); p_time(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n");
#endif
#ifdef RUSAGE_CHILDREN
(void) getrusage(RUSAGE_CHILDREN, &usage); (void) getrusage(RUSAGE_CHILDREN, &usage);
p_time(shl_stdout, 0, &usage.ru_utime, 0, NULL, " "); p_time(shl_stdout, 0, &usage.ru_utime, 0, NULL, " ");
p_time(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n"); p_time(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n");
#endif
return 0; return 0;
} }
@ -2069,6 +2073,9 @@ timex(struct op *t, int f)
#define TF_NOARGS BIT(0) #define TF_NOARGS BIT(0)
#define TF_NOREAL BIT(1) /* don't report real time */ #define TF_NOREAL BIT(1) /* don't report real time */
#define TF_POSIX BIT(2) /* report in posix format */ #define TF_POSIX BIT(2) /* report in posix format */
#if !defined(RUSAGE_SELF) || !defined(RUSAGE_CHILDREN)
return (0);
#else
int rv = 0; int rv = 0;
struct rusage ru0, ru1, cru0, cru1; struct rusage ru0, ru1, cru0, cru1;
struct timeval usrtime, systime, tv0, tv1; struct timeval usrtime, systime, tv0, tv1;
@ -2129,7 +2136,8 @@ timex(struct op *t, int f)
p_time(shl_out, 0, &systime, 5, NULL, " system\n"); p_time(shl_out, 0, &systime, 5, NULL, " system\n");
shf_flush(shl_out); shf_flush(shl_out);
return rv; return (rv);
#endif
} }
void void
@ -2549,8 +2557,12 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
return stat(opnd1, &b1) == 0 && return stat(opnd1, &b1) == 0 &&
(b1.st_mode & S_ISGID) == S_ISGID; (b1.st_mode & S_ISGID) == S_ISGID;
case TO_FILSTCK: /* -k */ case TO_FILSTCK: /* -k */
#ifdef S_ISVTX
return stat(opnd1, &b1) == 0 && return stat(opnd1, &b1) == 0 &&
(b1.st_mode & S_ISVTX) == S_ISVTX; (b1.st_mode & S_ISVTX) == S_ISVTX;
#else
return (0);
#endif
case TO_FILGZ: /* -s */ case TO_FILGZ: /* -s */
return stat(opnd1, &b1) == 0 && b1.st_size > 0L; return stat(opnd1, &b1) == 0 && b1.st_size > 0L;
case TO_FILTT: /* -t */ case TO_FILTT: /* -t */
@ -3013,5 +3025,5 @@ c_ulimit(char **wp)
shprintf("%ld\n", (long) val); shprintf("%ld\n", (long) val);
} }
} }
return 0; return (0);
} }

22
jobs.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.9 2006/08/01 13:43:27 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.10 2006/08/24 20:32:53 tg Exp $");
/* Order important! */ /* Order important! */
#define PRUNNING 0 #define PRUNNING 0
@ -54,8 +54,10 @@ struct job {
pid_t pgrp; /* process group of job */ pid_t pgrp; /* process group of job */
pid_t ppid; /* pid of process that forked job */ pid_t ppid; /* pid of process that forked job */
int32_t age; /* number of jobs started */ int32_t age; /* number of jobs started */
#ifdef RUSAGE_CHILDREN
struct timeval systime; /* system time used by job */ struct timeval systime; /* system time used by job */
struct timeval usrtime; /* user time used by job */ struct timeval usrtime; /* user time used by job */
#endif
Proc *proc_list; /* process list */ Proc *proc_list; /* process list */
Proc *last_proc; /* last process in list */ Proc *last_proc; /* last process in list */
Coproc_id coproc_id; /* 0 or id of coprocess output pipe */ Coproc_id coproc_id; /* 0 or id of coprocess output pipe */
@ -83,7 +85,9 @@ static const char *const lookup_msgs[] = {
NULL NULL
}; };
#ifdef RUSAGE_CHILDREN
struct timeval j_systime, j_usrtime; /* user and system time of last j_waitjed job */ struct timeval j_systime, j_usrtime; /* user and system time of last j_waitjed job */
#endif
static Job *job_list; /* job list */ static Job *job_list; /* job list */
static Job *last_job; static Job *last_job;
@ -336,8 +340,10 @@ exchild(struct op *t, int flags,
*/ */
j->flags = (flags & XXCOM) ? JF_XXCOM : j->flags = (flags & XXCOM) ? JF_XXCOM :
((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE)); ((flags & XBGND) ? 0 : (JF_FG|JF_USETTYMODE));
#ifdef RUSAGE_CHILDREN
timerclear(&j->usrtime); timerclear(&j->usrtime);
timerclear(&j->systime); timerclear(&j->systime);
#endif
j->state = PRUNNING; j->state = PRUNNING;
j->pgrp = 0; j->pgrp = 0;
j->ppid = procpid; j->ppid = procpid;
@ -989,8 +995,10 @@ j_waitj(Job *j,
trapsig(WTERMSIG(status)); trapsig(WTERMSIG(status));
} }
#ifdef RUSAGE_CHILDREN
j_usrtime = j->usrtime; j_usrtime = j->usrtime;
j_systime = j->systime; j_systime = j->systime;
#endif
rv = j->status; rv = j->status;
if (!(flags & JW_ASYNCNOTIFY) && if (!(flags & JW_ASYNCNOTIFY) &&
@ -1018,7 +1026,9 @@ j_sigchld(int sig __attribute__((unused)))
Proc *p = NULL; Proc *p = NULL;
int pid; int pid;
int status; int status;
#ifdef RUSAGE_CHILDREN
struct rusage ru0, ru1; struct rusage ru0, ru1;
#endif
/* Don't wait for any processes if a job is partially started. /* Don't wait for any processes if a job is partially started.
* This is so we don't do away with the process group leader * This is so we don't do away with the process group leader
@ -1031,14 +1041,18 @@ j_sigchld(int sig __attribute__((unused)))
return; return;
} }
#ifdef RUSAGE_CHILDREN
getrusage(RUSAGE_CHILDREN, &ru0); getrusage(RUSAGE_CHILDREN, &ru0);
#endif
do { do {
pid = waitpid(-1, &status, (WNOHANG|WUNTRACED)); pid = waitpid(-1, &status, (WNOHANG|WUNTRACED));
if (pid <= 0) /* return if would block (0) ... */ if (pid <= 0) /* return if would block (0) ... */
break; /* ... or no children or interrupted (-1) */ break; /* ... or no children or interrupted (-1) */
#ifdef RUSAGE_CHILDREN
getrusage(RUSAGE_CHILDREN, &ru1); getrusage(RUSAGE_CHILDREN, &ru1);
#endif
/* find job and process structures for this pid */ /* find job and process structures for this pid */
for (j = job_list; j != NULL; j = j->next) for (j = job_list; j != NULL; j = j->next)
@ -1051,15 +1065,19 @@ j_sigchld(int sig __attribute__((unused)))
warningf(true, "bad process waited for (pid = %d)", warningf(true, "bad process waited for (pid = %d)",
pid); pid);
*/ */
#ifdef RUSAGE_CHILDREN
ru0 = ru1; ru0 = ru1;
#endif
continue; continue;
} }
#ifdef RUSAGE_CHILDREN
timeradd(&j->usrtime, &ru1.ru_utime, &j->usrtime); timeradd(&j->usrtime, &ru1.ru_utime, &j->usrtime);
timersub(&j->usrtime, &ru0.ru_utime, &j->usrtime); timersub(&j->usrtime, &ru0.ru_utime, &j->usrtime);
timeradd(&j->systime, &ru1.ru_stime, &j->systime); timeradd(&j->systime, &ru1.ru_stime, &j->systime);
timersub(&j->systime, &ru0.ru_stime, &j->systime); timersub(&j->systime, &ru0.ru_stime, &j->systime);
ru0 = ru1; ru0 = ru1;
#endif
p->status = status; p->status = status;
if (WIFSTOPPED(status)) if (WIFSTOPPED(status))
p->state = PSTOPPED; p->state = PSTOPPED;
@ -1233,8 +1251,10 @@ j_print(Job *j, int how, struct shf *shf)
WEXITSTATUS(p->status)); WEXITSTATUS(p->status));
break; break;
case PSIGNALLED: case PSIGNALLED:
#ifdef WCOREDUMP
if (WCOREDUMP(p->status)) if (WCOREDUMP(p->status))
coredumped = 1; coredumped = 1;
#endif
/* kludge for not reporting 'normal termination signals' /* kludge for not reporting 'normal termination signals'
* (ie, SIGINT, SIGPIPE) * (ie, SIGINT, SIGPIPE)
*/ */

4
main.c
View File

@ -6,7 +6,7 @@
#define EXTERN #define EXTERN
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.47 2006/08/22 22:49:35 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.48 2006/08/24 20:32:53 tg Exp $");
extern char **environ; extern char **environ;
@ -114,7 +114,7 @@ main(int argc, char *argv[])
init_histvec(); init_histvec();
def_path = _PATH_DEFPATH; def_path = _PATH_DEFPATH;
#if !defined(__CYGWIN__) #ifdef _CS_PATH
{ {
size_t len; size_t len;
char *new; char *new;

8
misc.c
View File

@ -3,7 +3,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.15 2006/08/22 22:49:36 tg Exp $\t" __RCSID("$MirOS: src/bin/mksh/misc.c,v 1.16 2006/08/24 20:32:53 tg Exp $\t"
MKSH_SH_H_ID); MKSH_SH_H_ID);
short chtypes[UCHAR_MAX+1]; /* type bits for unsigned char */ short chtypes[UCHAR_MAX+1]; /* type bits for unsigned char */
@ -1129,7 +1129,7 @@ reset_nonblock(int fd)
} }
/* Like getcwd(), except bsize is ignored if buf is 0 (MAXPATHLEN is used) */ /* Like getcwd(), except bsize is ignored if buf is 0 (PATH_MAX is used) */
char * char *
ksh_get_wd(char *buf, int bsize) ksh_get_wd(char *buf, int bsize)
{ {
@ -1140,8 +1140,8 @@ ksh_get_wd(char *buf, int bsize)
* inject possibly allocated space into the ATEMP area. */ * inject possibly allocated space into the ATEMP area. */
/* Assume getcwd() available */ /* Assume getcwd() available */
if (!buf) { if (!buf) {
bsize = MAXPATHLEN; bsize = PATH_MAX;
b = alloc(MAXPATHLEN + 1, ATEMP); b = alloc(PATH_MAX + 1, ATEMP);
} else } else
b = buf; b = buf;