From bb78fd158c312d3ba6d3182fb7695fd9995a411b Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 23 Jun 2018 19:18:16 +0200 Subject: [PATCH] Move the big gnarly functions like system() and popen() into sys, and make them build. --- lang/cem/libcc.ansi/build.lua | 6 +- lang/cem/libcc.ansi/headers/ack/config.h | 20 +++ lang/cem/libcc.ansi/headers/unistd.h | 10 +- lang/cem/libcc.ansi/misc/popen.c | 119 ------------------ lang/cem/libcc.ansi/stdlib/LIST | 25 ---- lang/cem/libcc.ansi/stdlib/Makefile | 14 --- lang/cem/libcc.ansi/{ => sys}/misc/getpass.c | 0 lang/cem/libcc.ansi/{ => sys}/misc/isatty.c | 0 lang/cem/libcc.ansi/sys/misc/popen.c | 68 ++++++++++ .../libcc.ansi/{stdlib => sys/misc}/remove.c | 4 + lang/cem/libcc.ansi/{ => sys}/misc/sleep.c | 15 ++- .../libcc.ansi/{stdlib => sys/misc}/system.c | 4 + lang/cem/libcc.ansi/sys/stdio/rename.c | 20 --- 13 files changed, 115 insertions(+), 190 deletions(-) delete mode 100644 lang/cem/libcc.ansi/misc/popen.c delete mode 100644 lang/cem/libcc.ansi/stdlib/LIST delete mode 100644 lang/cem/libcc.ansi/stdlib/Makefile rename lang/cem/libcc.ansi/{ => sys}/misc/getpass.c (100%) rename lang/cem/libcc.ansi/{ => sys}/misc/isatty.c (100%) create mode 100644 lang/cem/libcc.ansi/sys/misc/popen.c rename lang/cem/libcc.ansi/{stdlib => sys/misc}/remove.c (80%) rename lang/cem/libcc.ansi/{ => sys}/misc/sleep.c (79%) rename lang/cem/libcc.ansi/{stdlib => sys/misc}/system.c (96%) delete mode 100644 lang/cem/libcc.ansi/sys/stdio/rename.c diff --git a/lang/cem/libcc.ansi/build.lua b/lang/cem/libcc.ansi/build.lua index 68a506b26..9e06c243b 100644 --- a/lang/cem/libcc.ansi/build.lua +++ b/lang/cem/libcc.ansi/build.lua @@ -46,13 +46,9 @@ for _, plat in ipairs(vars.plats) do "./sys/malloc/*.c", "./sys/exit/*.c", "./sys/stdio/*.c", - "./assert/*.c", + "./sys/misc/*.c", "./stdio/*.c", - "./stdlib/*.c", - "./string/*.c", "./time/*.c", - "./misc/getpass.c", - "./misc/isatty.c", }, hdrs = {}, -- must be empty deps = { diff --git a/lang/cem/libcc.ansi/headers/ack/config.h b/lang/cem/libcc.ansi/headers/ack/config.h index c3daa68fe..cefa902bd 100644 --- a/lang/cem/libcc.ansi/headers/ack/config.h +++ b/lang/cem/libcc.ansi/headers/ack/config.h @@ -29,6 +29,26 @@ #define ACKCONF_WANT_EMULATED_RAISE 1 #endif +#ifndef ACKCONF_WANT_EMULATED_REMOVE +/* Implement remove() as unlink(). */ +#define ACKCONF_WANT_EMULATED_REMOVE 1 +#endif + +#ifndef ACKCONF_WANT_EMULATED_SYSTEM +/* Implement system() as fork()/execve()/wait(). */ +#define ACKCONF_WANT_EMULATED_SYSTEM 1 +#endif + +#ifndef ACKCONF_WANT_EMULATED_SLEEP +/* Implement sleep() with SIGALRM. */ +#define ACKCONF_WANT_EMULATED_SLEEP 1 +#endif + +#ifndef ACKCONF_WANT_EMULATED_POPEN +/* Implement popen() with fork()/dup2() etc. */ +#define ACKCONF_WANT_EMULATED_POPEN 1 +#endif + #ifndef ACKCONF_WANT_MALLOC /* Uses sbrk() to get memory from the system. */ #define ACKCONF_WANT_MALLOC 1 diff --git a/lang/cem/libcc.ansi/headers/unistd.h b/lang/cem/libcc.ansi/headers/unistd.h index ef3b81689..9ae315648 100644 --- a/lang/cem/libcc.ansi/headers/unistd.h +++ b/lang/cem/libcc.ansi/headers/unistd.h @@ -43,8 +43,11 @@ #define SIG_DFL ((sighandler_t) 0) /* Default action. */ #define SIG_IGN ((sighandler_t) 1) /* Ignore signal. */ + #define SIGINT 2 /* Terminal interrupt */ + #define SIGQUIT 3 /* Terminal quit */ #define SIGABRT 6 /* Abort (ANSI) */ #define SIGILL 11 /* Illegal instruction */ + #define SIGALRM 14 /* Alarm clock */ #define _NSIG 16 /* Biggest signal number + 1 (not including real-time signals). */ @@ -79,6 +82,9 @@ extern char** environ; extern int brk(void* ptr); extern int close(int d); extern int creat(const char* path, mode_t mode); +extern int dup(int oldfd); +extern int dup2(int oldfd, int newfd); +extern int execl(const char *path, const char* arg, ...); extern int execve(const char *path, char *const argv[], char *const envp[]); extern int fcntl(int fd, int op, ...); extern int gettimeofday(struct timeval* tv, struct timezone* tz); @@ -86,6 +92,8 @@ extern int isatty(int d); extern int isatty(int d); extern int kill(pid_t old, int sig); extern int open(const char* path, int access, ...); +extern int pause(void); +extern int pipe(int pipefd[2]); extern int raise(int signum); extern int settimeofday(const struct timeval* tv, const struct timezone* tz); extern int sigaction(int, const struct sigaction *, struct sigaction *); @@ -94,6 +102,7 @@ extern int unlink(const char* path); extern off_t lseek(int fildes, off_t offset, int whence); extern pid_t fork(void); extern pid_t getpid(void); +extern unsigned int alarm(unsigned int seconds); extern pid_t wait(int* wstatus); extern sighandler_t signal(int signum, sighandler_t handler); extern ssize_t read(int fd, void* buffer, size_t count); @@ -101,5 +110,4 @@ extern ssize_t write(int fd, void* buffer, size_t count); extern void _exit(int); extern void* sbrk(int increment); - #endif diff --git a/lang/cem/libcc.ansi/misc/popen.c b/lang/cem/libcc.ansi/misc/popen.c deleted file mode 100644 index b3352e9ad..000000000 --- a/lang/cem/libcc.ansi/misc/popen.c +++ /dev/null @@ -1,119 +0,0 @@ -/* - * popen - open a pipe - */ -/* $Id$ */ - -#include -#include -#include -#if defined(__BSD4_2) -union wait { - int w_status; -}; -typedef union wait wait_arg; -#else -typedef int wait_arg; -#endif /* __BSD4_2 */ -#include "../stdio/loc_incl.h" - -int _close(int d); -#if defined(__USG) -static -#endif - int - _dup2(int oldd, int newd); /* not present in System 5 */ -int _execl(const char* name, ...); -int _fork(void); -int _pipe(int fildes[2]); -int _wait(wait_arg* status); -void _exit(int status); - -static int pids[FOPEN_MAX]; - -FILE* popen(const char* command, const char* type) -{ - int piped[2]; - int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2; - int pid; - - if (Xtype == 2 || _pipe(piped) < 0 || (pid = _fork()) < 0) - return 0; - - if (pid == 0) - { - /* child */ - register int* p; - - for (p = pids; p < &pids[FOPEN_MAX]; p++) - { - if (*p) - _close(p - pids); - } - _close(piped[Xtype]); - _dup2(piped[!Xtype], !Xtype); - _close(piped[!Xtype]); - _execl("/bin/sh", "sh", "-c", command, (char*)0); - _exit(127); /* like system() ??? */ - } - - pids[piped[Xtype]] = pid; - _close(piped[!Xtype]); - return fdopen(piped[Xtype], type); -} - -#if defined(__BSD4_2) -#define ret_val status.w_status -#else -#define ret_val status -#endif - -int pclose(FILE* stream) -{ - int fd = fileno(stream); - wait_arg status; - int wret; - void (*intsave)(int) = signal(SIGINT, SIG_IGN); - void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN); - - fclose(stream); - while ((wret = _wait(&status)) != -1) - { - if (wret == pids[fd]) - break; - } - if (wret == -1) - ret_val = -1; - signal(SIGINT, intsave); - signal(SIGQUIT, quitsave); - pids[fd] = 0; - return ret_val; -} - -#if defined(__USG) -int _dup(int fildes); - -static int -_dup2(int oldd, int newd) -{ - int i = 0, fd, tmp; - int fdbuf[FOPEN_MAX]; - - /* ignore the error on the close() */ - tmp = errno; - (void)_close(newd); - errno = tmp; - while ((fd = _dup(oldd)) != newd) - { - if (fd == -1) - break; - fdbuf[i++] = fd; - } - tmp = errno; - while (--i >= 0) - { - _close(fdbuf[i]); - } - errno = tmp; - return -(fd == -1); -} -#endif /* __USG */ diff --git a/lang/cem/libcc.ansi/stdlib/LIST b/lang/cem/libcc.ansi/stdlib/LIST deleted file mode 100644 index daa2f1cf2..000000000 --- a/lang/cem/libcc.ansi/stdlib/LIST +++ /dev/null @@ -1,25 +0,0 @@ -ext_fmt.h -abort.c -abs.c -atof.c -atoi.c -atol.c -bsearch.c -div.c -atexit.c -exit.c -getenv.c -labs.c -ldiv.c -malloc.c -mblen.c -mbstowcs.c -mbtowc.c -qsort.c -rand.c -strtod.c -strtol.c -system.c -wcstombs.c -wctomb.c -ext_comp.c diff --git a/lang/cem/libcc.ansi/stdlib/Makefile b/lang/cem/libcc.ansi/stdlib/Makefile deleted file mode 100644 index 8c8533fbe..000000000 --- a/lang/cem/libcc.ansi/stdlib/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -clean: - rm -f abort.o abs.o atof.o atoi.o atol.o bsearch.o div.o \ - atexit.o exit.o getenv.o labs.o ldiv.o malloc.o mblen.o \ - mbstowcs.o mbtowc.o qsort.o rand.o strtod.o strtol.o \ - system.o wcstombs.o wctomb.o ext_comp.o malloc.c OLIST - -malloc/malloc.c: - -(cd malloc; make) - -malloc.c: malloc/malloc.c - -cp malloc/malloc.c malloc.c - -distr: malloc.c - diff --git a/lang/cem/libcc.ansi/misc/getpass.c b/lang/cem/libcc.ansi/sys/misc/getpass.c similarity index 100% rename from lang/cem/libcc.ansi/misc/getpass.c rename to lang/cem/libcc.ansi/sys/misc/getpass.c diff --git a/lang/cem/libcc.ansi/misc/isatty.c b/lang/cem/libcc.ansi/sys/misc/isatty.c similarity index 100% rename from lang/cem/libcc.ansi/misc/isatty.c rename to lang/cem/libcc.ansi/sys/misc/isatty.c diff --git a/lang/cem/libcc.ansi/sys/misc/popen.c b/lang/cem/libcc.ansi/sys/misc/popen.c new file mode 100644 index 000000000..4631ce85c --- /dev/null +++ b/lang/cem/libcc.ansi/sys/misc/popen.c @@ -0,0 +1,68 @@ +/* + * popen - open a pipe + */ +/* $Id$ */ + +#include +#include +#include +#include + +#if ACKCONF_WANT_EMULATED_POPEN + +static int pids[FOPEN_MAX]; + +FILE* popen(const char* command, const char* type) +{ + int piped[2]; + int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2; + int pid; + + if (Xtype == 2 || pipe(piped) < 0 || (pid = fork()) < 0) + return 0; + + if (pid == 0) + { + /* child */ + register int* p; + + for (p = pids; p < &pids[FOPEN_MAX]; p++) + { + if (*p) + close(p - pids); + } + close(piped[Xtype]); + dup2(piped[!Xtype], !Xtype); + close(piped[!Xtype]); + execl("/bin/sh", "sh", "-c", command, (char*)0); + _exit(127); /* like system() ??? */ + } + + pids[piped[Xtype]] = pid; + close(piped[!Xtype]); + return fdopen(piped[Xtype], type); +} + +int pclose(FILE* stream) +{ + int fd = fileno(stream); + int status; + int wret; + void (*intsave)(int) = signal(SIGINT, SIG_IGN); + void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN); + + fclose(stream); + while ((wret = wait(&status)) != -1) + { + if (wret == pids[fd]) + break; + } + if (wret == -1) + status = -1; + signal(SIGINT, intsave); + signal(SIGQUIT, quitsave); + pids[fd] = 0; + return status; +} + +#endif diff --git a/lang/cem/libcc.ansi/stdlib/remove.c b/lang/cem/libcc.ansi/sys/misc/remove.c similarity index 80% rename from lang/cem/libcc.ansi/stdlib/remove.c rename to lang/cem/libcc.ansi/sys/misc/remove.c index fb0f4de8e..4c442df94 100644 --- a/lang/cem/libcc.ansi/stdlib/remove.c +++ b/lang/cem/libcc.ansi/sys/misc/remove.c @@ -7,7 +7,11 @@ #include #include +#if ACKCONF_WANT_EMULATED_REMOVE + int remove(const char* filename) { return unlink(filename); } + +#endif diff --git a/lang/cem/libcc.ansi/misc/sleep.c b/lang/cem/libcc.ansi/sys/misc/sleep.c similarity index 79% rename from lang/cem/libcc.ansi/misc/sleep.c rename to lang/cem/libcc.ansi/sys/misc/sleep.c index 1b7c9a8d7..97db561de 100644 --- a/lang/cem/libcc.ansi/misc/sleep.c +++ b/lang/cem/libcc.ansi/sys/misc/sleep.c @@ -3,11 +3,12 @@ */ /* $Id$ */ +#include #include +#include #include -int _alarm(int n); -void _pause(void); +#if ACKCONF_WANT_EMULATED_SLEEP static jmp_buf setjmpbuf; @@ -28,10 +29,10 @@ void sleep(int n) if (setjmp(setjmpbuf)) { signal(SIGALRM, oldsig); - _alarm(oldalarm); + alarm(oldalarm); return; } - oldalarm = _alarm(5000); /* Who cares how long, as long + oldalarm = alarm(5000); /* Who cares how long, as long * as it is long enough */ if (oldalarm > n) @@ -42,10 +43,12 @@ void sleep(int n) oldalarm = 1; } oldsig = signal(SIGALRM, alfun); - _alarm(n); + alarm(n); for (;;) { /* allow for other handlers ... */ - _pause(); + pause(); } } + +#endif diff --git a/lang/cem/libcc.ansi/stdlib/system.c b/lang/cem/libcc.ansi/sys/misc/system.c similarity index 96% rename from lang/cem/libcc.ansi/stdlib/system.c rename to lang/cem/libcc.ansi/sys/misc/system.c index 49dba6318..8ed21d405 100644 --- a/lang/cem/libcc.ansi/stdlib/system.c +++ b/lang/cem/libcc.ansi/sys/misc/system.c @@ -9,6 +9,8 @@ #include #include +#if ACKCONF_WANT_EMULATED_SYSTEM + #define FAIL 127 static const char* exec_tab[] = { @@ -56,3 +58,5 @@ int system(const char* str) } return exitstatus; } + +#endif diff --git a/lang/cem/libcc.ansi/sys/stdio/rename.c b/lang/cem/libcc.ansi/sys/stdio/rename.c deleted file mode 100644 index ff177ccd5..000000000 --- a/lang/cem/libcc.ansi/sys/stdio/rename.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * rename.c - rename a file - */ -/* $Id$ */ - -#include -#include -#include - -/* Disabled, dtrg: rename is a system call these days. */ -#if 0 -int _link(const char *name1, const char *name2); - -int -rename(const char *old, const char *new) { - if (!_link(old, new)) - return remove(old); - else return -1; -} -#endif -- 2.34.1