mlibc: New library layout; remove duplication.
[mes.git] / src / posix.c
1 /* -*-comment-start: "//";comment-end:""-*-
2  * Mes --- Maxwell Equations of Software
3  * Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
4  *
5  * This file is part of Mes.
6  *
7  * 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.
11  *
12  * 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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Mes.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 int
25 ungetchar (int c)
26 {
27   return ungetc (c, g_stdin);
28 }
29
30 int
31 peekchar ()
32 {
33   int c = getchar ();
34   ungetchar (c);
35   return c;
36 }
37
38 SCM
39 peek_byte ()
40 {
41   return MAKE_NUMBER (peekchar ());
42 }
43
44 SCM
45 read_byte ()
46 {
47   return MAKE_NUMBER (getchar ());
48 }
49
50 SCM
51 unread_byte (SCM i)
52 {
53   ungetchar (VALUE (i));
54   return i;
55 }
56
57 SCM
58 write_byte (SCM x) ///((arity . n))
59 {
60   SCM c = car (x);
61   SCM p = cdr (x);
62   int fd = g_stdout;
63   if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER && VALUE (CAR (p)) != 1)
64     fd = VALUE (CAR (p));
65   char cc = VALUE (c);
66   write (fd, (char*)&cc, 1);
67 #if !__MESC__
68   assert (TYPE (c) == TNUMBER || TYPE (c) == TCHAR);
69 #endif
70   return c;
71 }
72
73 char string_to_cstring_buf[1024];
74 char const*
75 string_to_cstring (SCM s)
76 {
77   //static char buf[1024];
78   //char *p = buf;
79   char *p = string_to_cstring_buf;
80   s = STRING(s);
81   while (s != cell_nil)
82     {
83       *p++ = VALUE (car (s));
84       s = cdr (s);
85     }
86   *p = 0;
87   //return buf;
88   return string_to_cstring_buf;
89 }
90
91 SCM
92 getenv_ (SCM s) ///((name . "getenv"))
93 {
94   char *p;
95   p = getenv (string_to_cstring (s));
96   return p ? MAKE_STRING (cstring_to_list (p)) : cell_f;
97 }
98
99 SCM
100 access_p (SCM file_name, SCM mode)
101 {
102   return access (string_to_cstring (file_name), VALUE (mode)) == 0 ? cell_t : cell_f;
103 }
104
105 SCM
106 current_input_port ()
107 {
108   return MAKE_NUMBER (g_stdin);
109 }
110
111 SCM
112 open_input_file (SCM file_name)
113 {
114   return MAKE_NUMBER (open (string_to_cstring (file_name), O_RDONLY));
115 }
116
117 SCM
118 set_current_input_port (SCM port)
119 {
120   g_stdin = VALUE (port) ? VALUE (port) : STDIN;
121   return current_input_port ();
122 }
123
124 SCM
125 current_output_port ()
126 {
127   return MAKE_NUMBER (g_stdout);
128 }
129
130 SCM
131 open_output_file (SCM x) ///((arity . n))
132 {
133   SCM file_name = car (x);
134   x = cdr (x);
135   int mode = S_IRUSR|S_IWUSR;
136   if (TYPE (x) == TPAIR && TYPE (car (x)) == TNUMBER) mode = VALUE (car (x));
137   return MAKE_NUMBER (open (string_to_cstring (file_name), O_WRONLY|O_CREAT|O_TRUNC,mode));
138 }
139
140 SCM
141 set_current_output_port (SCM port)
142 {
143   g_stdout = VALUE (port) ? VALUE (port) : STDOUT;
144   return current_output_port ();
145 }
146
147 SCM
148 force_output (SCM p) ///((arity . n))
149 {
150   return cell_unspecified;
151 }