Updating built in Io code to use += instead of x = x + y
[io/quag.git] / addons / SystemCall / source / callsystem.h
blobd5cdb71298828a2692ffeabe58e02b85be12f5a4
1 #ifndef CALLSYSTEM_INCLUDED
2 #define CALLSYSTEM_INCLUDED
3 /*
4 callsystem - system() on steorids
6 features:
7 - asynchonous running of a child process
8 - setup of the environment
9 - substitution of environment variables
10 - connect all 3 standard streams to pipes, null devices, or files
11 - pathname handling
12 - #!shebang interpreter handling on any OS
14 ALL IN A PORTABLE WAY!
16 Pathnames:
17 / seperates a path,
18 : is used as device identifier when the path starts with / ("/C:" becomes "C:\")
19 . is the current directory
20 .. is the parent directory
21 ~ is the users home directory
23 Environment variables:
24 only ${KEY} syntax is substituted in one pass, trying to substitute a non existing env var yields an error
28 first abstraction of a few types and loading OS specific headers
31 #include <assert.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <signal.h>
38 //#define unix 1
39 #if !defined(unix) && defined(__unix__) || defined(__NetBSD__)
40 #define unix
41 #endif
43 #if defined(__APPLE__)
44 #define unix 1
45 #include <sys/unistd.h>
46 #endif
48 #if defined(unix)
49 #define _GNU_SOURCE
50 #include <sys/time.h>
51 #include <sys/resource.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <sys/wait.h>
57 #elif defined(WIN32)
58 #include <process.h>
59 #else
60 #error "system not supported!"
61 #endif
64 #ifdef unix
66 opaque process identifier type and value for an illegal pid
68 typedef pid_t callsystem_pid_t;
69 #define CALLSYSTEM_ILG_PID 0
72 opaque file descriptors and values for illegal, std and potable null fd's
73 fd's are always used as pairs (two element array)
74 [0] is the used for reading
75 [1] is used for writing
76 either side might be CALLSYSTEM_ILG_FD for non-pipes, callsystem cares for closing the aprobiate sides
78 typedef int callsystem_fd_t;
79 /* this is never an fd */
80 #define CALLSYSTEM_ILG_FD -1
82 #elif defined(WIN32)
83 #include <windows.h>
84 typedef HANDLE callsystem_pid_t;
85 #define CALLSYSTEM_ILG_PID NULL
86 typedef HANDLE callsystem_fd_t;
87 #define CALLSYSTEM_ILG_FD INVALID_HANDLE_VALUE
88 #else
89 #error "system unsupported"
90 #endif
95 INVOKING AND CONTROLLING CHILD PROGRAMS
98 int
99 callsystem(const char * cmd,
100 char * argv[],
101 char * env[],
102 callsystem_fd_t in[2],
103 callsystem_fd_t out[2],
104 callsystem_fd_t err[2],
105 const char * wd,
106 const int pri,
107 callsystem_pid_t * const child);
110 callsystem_running(callsystem_pid_t * pid);
113 callsystem_finished(callsystem_pid_t * pid);
116 callsystem_terminate(callsystem_pid_t *);
119 callsystem_abort(callsystem_pid_t *);
123 HANDLING FILE DESCRIPTORS AND PIPES
127 callsystem_pipe(callsystem_fd_t pipe[2]);
130 callsystem_null(callsystem_fd_t null[2]);
133 enum callsystem_filemode{
134 CALLSYSTEM_MODE_READ,
135 CALLSYSTEM_MODE_WRITE,
136 CALLSYSTEM_MODE_CREATE,
137 CALLSYSTEM_MODE_APPEND,
138 CALLSYSTEM_MODE_OVERWRITE,
139 CALLSYSTEM_MODE_BINARY=8
143 callsystem_open(const char * filename,
144 enum callsystem_filemode mode,
145 callsystem_fd_t fd[2]);
147 /* want sockets too? prolly not in the domain of this thing */
150 callsystem_close(callsystem_fd_t fds[2]);
153 create a std C FILE stream to a underlying filedescriptor
155 FILE *
156 callsystem_fdopen(callsystem_fd_t fds[2], enum callsystem_filemode mode);
161 ENVIRONMENT SETUP
165 callsystem_setenv(char ** env[], const char * key, const char * value);
167 const char *
168 callsystem_getenv(char ** env[], const char * key);
171 callsystem_unsetenv(char ** env[], const char * key);
174 callsystem_exportenv(char ** env[], const char * key);
177 callsystem_exportdefaults(char ** env[]);
179 char *
180 callsystem_env_subst(char ** envp[], const char * str);
182 void
183 callsystem_env_clear(char ** env[]);
189 SETUP OF THE ARGV
193 callsystem_argv_pushback(char ** argv[], const char * const arg);
196 callsystem_argv_pushfront(char ** argv[], const char * const arg);
198 void
199 callsystem_argv_clear(char ** argv[]);
203 PORTABLE PATHNAME HANDLING
207 planned:
208 callsystem_canonalize_path_to_host()
209 callsystem_canonalize_path_to_unix()
211 convert unix like filenames to OS specific filenames (and back)
213 checks that relative filenames do not leave their top directory
215 for win32:
216 "/Letter:/" becomes "Letter:\"
217 "/foo/" becomes "\foo\"
219 needs much to worked out (UNC?)
221 callsystem_is_pathname()
222 check if some string could be a legal pathname on the actual host
227 #endif