1 /* -*-comment-start: "//";comment-end:""-*-
2 * GNU Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of GNU Mes.
7 * GNU Mes is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
12 * GNU Mes is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
42 SCM port = current_input_port ();
43 return VALUE (CAR (STRING (port)));
50 return fdgetc (g_stdin);
51 SCM port = current_input_port ();
52 SCM string = STRING (port);
53 if (string == cell_nil)
55 int c = VALUE (CAR (string));
56 STRING (port) = CDR (string);
64 return fdungetc (c, g_stdin);
65 SCM port = current_input_port ();
66 STRING (port) = cons (MAKE_CHAR (c), STRING (port));
73 return MAKE_NUMBER (peekchar ());
79 return MAKE_NUMBER (readchar ());
85 unreadchar (VALUE (i));
92 return MAKE_CHAR (peekchar ());
98 return MAKE_CHAR (readchar ());
104 unreadchar (VALUE (i));
109 write_char (SCM i) ///((arity . n))
116 read_string (SCM port) ///((arity . n))
119 if (TYPE (port) == TPAIR && TYPE (car (port)) == TNUMBER)
120 g_stdin = VALUE (CAR (port));
122 SCM c = read_char ();
123 while (VALUE (c) != -1)
125 lst = append2 (lst, cons (c, cell_nil));
129 return MAKE_STRING (lst);
133 write_byte (SCM x) ///((arity . n))
138 if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER && VALUE (CAR (p)) != 1)
139 fd = VALUE (CAR (p));
140 if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER && VALUE (CAR (p)) == 2)
143 write (fd, (char*)&cc, 1);
145 assert (TYPE (c) == TNUMBER || TYPE (c) == TCHAR);
150 char string_to_cstring_buf[4096];
152 string_to_cstring_ (SCM s, char *buf)
156 while (s != cell_nil)
158 *p++ = VALUE (car (s));
166 string_to_cstring (SCM s)
168 return string_to_cstring_ (s, string_to_cstring_buf);
172 getenv_ (SCM s) ///((name . "getenv"))
175 p = getenv (string_to_cstring (s));
176 return p ? MAKE_STRING (cstring_to_list (p)) : cell_f;
180 setenv_ (SCM s, SCM v) ///((name . "setenv"))
183 strcpy (buf, string_to_cstring (s));
184 setenv (buf, string_to_cstring (v), 1);
185 return cell_unspecified;
189 access_p (SCM file_name, SCM mode)
191 return access (string_to_cstring (file_name), VALUE (mode)) == 0 ? cell_t : cell_f;
195 current_input_port ()
198 return MAKE_NUMBER (g_stdin);
200 while (x && PORT (CAR (x)) != g_stdin)
206 open_input_file (SCM file_name)
208 return MAKE_NUMBER (open (string_to_cstring (file_name), O_RDONLY));
212 open_input_string (SCM string)
214 SCM port = MAKE_STRING_PORT (STRING (string));
215 g_ports = cons (port, g_ports);
220 set_current_input_port (SCM port)
222 SCM prev = current_input_port ();
223 if (TYPE (port) == TNUMBER)
224 g_stdin = VALUE (port) ? VALUE (port) : STDIN;
225 else if (TYPE (port) == TPORT)
226 g_stdin = PORT (port);
231 current_output_port ()
233 return MAKE_NUMBER (g_stdout);
237 current_error_port ()
239 return MAKE_NUMBER (g_stderr);
243 open_output_file (SCM x) ///((arity . n))
245 SCM file_name = car (x);
247 int mode = S_IRUSR|S_IWUSR;
248 if (TYPE (x) == TPAIR && TYPE (car (x)) == TNUMBER)
249 mode = VALUE (car (x));
250 return MAKE_NUMBER (open (string_to_cstring (file_name), O_WRONLY|O_CREAT|O_TRUNC,mode));
254 set_current_output_port (SCM port)
256 g_stdout = VALUE (port) ? VALUE (port) : STDOUT;
257 return current_output_port ();
261 set_current_error_port (SCM port)
263 g_stderr = VALUE (port) ? VALUE (port) : STDERR;
264 return current_error_port ();
268 force_output (SCM p) ///((arity . n))
270 return cell_unspecified;
274 chmod_ (SCM file_name, SCM mode) ///((name . "chmod"))
276 chmod (string_to_cstring (file_name), VALUE (mode));
277 return cell_unspecified;
283 return isatty (VALUE (port)) ? cell_t : cell_f;
289 return MAKE_NUMBER (fork ());
293 execl_ (SCM file_name, SCM args) ///((name . "execl"))
295 char *c_argv[1000]; // POSIX minimum 4096
299 if (length__ (args) > 1000)
300 error (cell_symbol_system_error,
302 cons (MAKE_STRING (cstring_to_list ("too many arguments")),
303 cons (file_name, args))));
304 c_argv[i++] = (char*)string_to_cstring_ (file_name, string_to_cstring_buf+n);
305 n += length__ (STRING (file_name)) + 1;
306 while (args != cell_nil)
308 assert (TYPE (CAR (args)) == TSTRING);
309 c_argv[i++] = (char*)string_to_cstring_ (CAR (args), string_to_cstring_buf+n);
310 n += length__ (STRING (CAR (args))) + 1;
314 eputs ("arg["); eputs (itoa (i)); eputs ("]: "); eputs (c_argv[i-1]); eputs ("\n");
318 return MAKE_NUMBER (execv (c_argv[0], c_argv));
322 waitpid_ (SCM pid, SCM options)
325 int child = waitpid (VALUE (pid), &status, VALUE (options));
326 return cons (MAKE_NUMBER (child), MAKE_NUMBER (status));
330 /* Nanoseconds on 64-bit systems with POSIX timers. */
331 #define TIME_UNITS_PER_SECOND 1000000000
333 /* Milliseconds for everyone else. */
334 #define TIME_UNITS_PER_SECOND 1000
337 struct timespec g_start_time;
339 init_time (SCM a) ///((internal))
341 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &g_start_time);
342 a = acons (cell_symbol_internal_time_units_per_second, MAKE_NUMBER (TIME_UNITS_PER_SECOND), a);
348 return MAKE_NUMBER (time (0));
352 gettimeofday_ () ///((name . "gettimeofday"))
355 gettimeofday (&time, 0);
356 return cons (MAKE_NUMBER (time.tv_sec), MAKE_NUMBER (time.tv_usec));
360 seconds_and_nanoseconds_to_long (long s, long ns)
362 return s * TIME_UNITS_PER_SECOND
363 + ns / (1000000000 / TIME_UNITS_PER_SECOND);
367 get_internal_run_time ()
370 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
371 long time = seconds_and_nanoseconds_to_long (ts.tv_sec - g_start_time.tv_sec,
372 ts.tv_nsec - g_start_time.tv_nsec);
373 return MAKE_NUMBER (time);
377 getcwd_ () ///((name . "getcwd"))
380 return MAKE_STRING (cstring_to_list (getcwd (buf, PATH_MAX)));
384 dup_ (SCM port) ///((name . "dup"))
386 return MAKE_NUMBER (dup (VALUE (port)));
390 dup2_ (SCM old, SCM new) ///((name . "dup2"))
392 dup2 (VALUE (old), VALUE (new));
393 return cell_unspecified;
397 delete_file (SCM file_name)
399 unlink (string_to_cstring (file_name));
400 return cell_unspecified;