Move the big gnarly functions like system() and popen() into sys, and make them
authorDavid Given <dg@cowlark.com>
Sat, 23 Jun 2018 17:18:16 +0000 (19:18 +0200)
committerDavid Given <dg@cowlark.com>
Sat, 23 Jun 2018 17:18:16 +0000 (19:18 +0200)
build.

13 files changed:
lang/cem/libcc.ansi/build.lua
lang/cem/libcc.ansi/headers/ack/config.h
lang/cem/libcc.ansi/headers/unistd.h
lang/cem/libcc.ansi/misc/popen.c [deleted file]
lang/cem/libcc.ansi/stdlib/LIST [deleted file]
lang/cem/libcc.ansi/stdlib/Makefile [deleted file]
lang/cem/libcc.ansi/sys/misc/getpass.c [moved from lang/cem/libcc.ansi/misc/getpass.c with 100% similarity]
lang/cem/libcc.ansi/sys/misc/isatty.c [moved from lang/cem/libcc.ansi/misc/isatty.c with 100% similarity]
lang/cem/libcc.ansi/sys/misc/popen.c [new file with mode: 0644]
lang/cem/libcc.ansi/sys/misc/remove.c [moved from lang/cem/libcc.ansi/stdlib/remove.c with 80% similarity]
lang/cem/libcc.ansi/sys/misc/sleep.c [moved from lang/cem/libcc.ansi/misc/sleep.c with 79% similarity]
lang/cem/libcc.ansi/sys/misc/system.c [moved from lang/cem/libcc.ansi/stdlib/system.c with 96% similarity]
lang/cem/libcc.ansi/sys/stdio/rename.c [deleted file]

index 68a506b..9e06c24 100644 (file)
@@ -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 = {
index c3daa68..cefa902 100644 (file)
 #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
index ef3b816..9ae3156 100644 (file)
     #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 (file)
index b3352e9..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * popen - open a pipe
- */
-/* $Id$ */
-
-#include <errno.h>
-#include <stdio.h>
-#include <signal.h>
-#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 (file)
index daa2f1c..0000000
+++ /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 (file)
index 8c8533f..0000000
+++ /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/sys/misc/popen.c b/lang/cem/libcc.ansi/sys/misc/popen.c
new file mode 100644 (file)
index 0000000..4631ce8
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * popen - open a pipe
+ */
+/* $Id$ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <signal.h>
+#include <unistd.h>
+
+#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
similarity index 80%
rename from lang/cem/libcc.ansi/stdlib/remove.c
rename to lang/cem/libcc.ansi/sys/misc/remove.c
index fb0f4de..4c442df 100644 (file)
@@ -7,7 +7,11 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#if ACKCONF_WANT_EMULATED_REMOVE
+
 int remove(const char* filename)
 {
        return unlink(filename);
 }
+
+#endif
similarity index 79%
rename from lang/cem/libcc.ansi/misc/sleep.c
rename to lang/cem/libcc.ansi/sys/misc/sleep.c
index 1b7c9a8..97db561 100644 (file)
@@ -3,11 +3,12 @@
  */
 /* $Id$ */
 
+#include <stdio.h>
 #include <signal.h>
+#include <unistd.h>
 #include <setjmp.h>
 
-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
similarity index 96%
rename from lang/cem/libcc.ansi/stdlib/system.c
rename to lang/cem/libcc.ansi/sys/misc/system.c
index 49dba63..8ed21d4 100644 (file)
@@ -9,6 +9,8 @@
 #include <signal.h>
 #include <unistd.h>
 
+#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 (file)
index ff177cc..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * rename.c - rename a file
- */
-/* $Id$ */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-
-/* 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