OILS / cpp / core.cc View on Github | oilshell.org

435 lines, 267 significant
1// core.cc
2
3#include "cpp/core.h"
4
5#include <ctype.h> // ispunct()
6#include <errno.h>
7#include <float.h>
8#include <math.h> // fmod()
9#include <pwd.h> // passwd
10#include <signal.h>
11#include <sys/resource.h> // getrusage
12#include <sys/select.h> // select(), FD_ISSET, FD_SET, FD_ZERO
13#include <sys/stat.h> // stat
14#include <sys/time.h> // gettimeofday
15#include <sys/times.h> // tms / times()
16#include <sys/utsname.h> // uname
17#include <sys/wait.h> // waitpid()
18#include <termios.h> // tcgetattr(), tcsetattr()
19#include <time.h> // time()
20#include <unistd.h> // getuid(), environ
21
22#include "_build/detected-cpp-config.h" // HAVE_PWENT
23#include "_gen/cpp/build_stamp.h" // gCommitHash
24#include "_gen/frontend/consts.h" // gVersion
25#include "cpp/embedded_file.h"
26
27extern char** environ;
28
29namespace pyos {
30
31SignalSafe* gSignalSafe = nullptr;
32
33Tuple2<int, int> WaitPid(int waitpid_options) {
34 int status;
35 int result = ::waitpid(-1, &status, WUNTRACED | waitpid_options);
36 if (result < 0) {
37 if (errno == EINTR && gSignalSafe->PollSigInt()) {
38 throw Alloc<KeyboardInterrupt>();
39 }
40 return Tuple2<int, int>(-1, errno);
41 }
42 return Tuple2<int, int>(result, status);
43}
44
45Tuple2<int, int> Read(int fd, int n, List<BigStr*>* chunks) {
46 BigStr* s = OverAllocatedStr(n); // Allocate enough for the result
47
48 int length = ::read(fd, s->data(), n);
49 if (length < 0) {
50 if (errno == EINTR && gSignalSafe->PollSigInt()) {
51 throw Alloc<KeyboardInterrupt>();
52 }
53 return Tuple2<int, int>(-1, errno);
54 }
55 if (length == 0) {
56 return Tuple2<int, int>(length, 0);
57 }
58
59 // Now we know how much data we got back
60 s->MaybeShrink(length);
61 chunks->append(s);
62
63 return Tuple2<int, int>(length, 0);
64}
65
66Tuple2<int, int> ReadByte(int fd) {
67 unsigned char buf[1];
68 ssize_t n = read(fd, &buf, 1);
69 if (n < 0) { // read error
70 if (errno == EINTR && gSignalSafe->PollSigInt()) {
71 throw Alloc<KeyboardInterrupt>();
72 }
73 return Tuple2<int, int>(-1, errno);
74 } else if (n == 0) { // EOF
75 return Tuple2<int, int>(EOF_SENTINEL, 0);
76 } else { // return character
77 return Tuple2<int, int>(buf[0], 0);
78 }
79}
80
81Dict<BigStr*, BigStr*>* Environ() {
82 auto d = Alloc<Dict<BigStr*, BigStr*>>();
83
84 for (char** env = environ; *env; ++env) {
85 char* pair = *env;
86
87 char* eq = strchr(pair, '=');
88 assert(eq != nullptr); // must look like KEY=value
89
90 int len = strlen(pair);
91
92 int key_len = eq - pair;
93 BigStr* key = StrFromC(pair, key_len);
94
95 int val_len = len - key_len - 1;
96 BigStr* val = StrFromC(eq + 1, val_len);
97
98 d->set(key, val);
99 }
100
101 return d;
102}
103
104int Chdir(BigStr* dest_dir) {
105 if (chdir(dest_dir->data_) == 0) {
106 return 0; // success
107 } else {
108 return errno;
109 }
110}
111
112BigStr* GetMyHomeDir() {
113 uid_t uid = getuid(); // always succeeds
114
115 // Don't free this. (May return a pointer to a static area)
116 struct passwd* entry = getpwuid(uid);
117 if (entry == nullptr) {
118 return nullptr;
119 }
120 BigStr* s = StrFromC(entry->pw_dir);
121 return s;
122}
123
124BigStr* GetHomeDir(BigStr* user_name) {
125 // Don't free this. (May return a pointer to a static area)
126 struct passwd* entry = getpwnam(user_name->data_);
127 if (entry == nullptr) {
128 return nullptr;
129 }
130 BigStr* s = StrFromC(entry->pw_dir);
131 return s;
132}
133
134List<PasswdEntry*>* GetAllUsers() {
135#ifdef HAVE_PWENT
136 auto* ret = NewList<PasswdEntry*>();
137 struct passwd* entry = nullptr;
138
139 setpwent();
140 while (true) {
141 errno = 0;
142 entry = getpwent();
143 if (entry == nullptr) {
144 if (errno == EINTR) {
145 continue; // try again
146 } else if (errno != 0) {
147 throw Alloc<OSError>(errno);
148 }
149 break;
150 }
151 ret->append(Alloc<PasswdEntry>(entry));
152 }
153 endpwent();
154
155 return ret;
156#else
157 fprintf(
158 stderr,
159 "Oils compiled without libc *pwent() functions. Can't list users.\n");
160 return NewList<PasswdEntry*>();
161#endif
162}
163
164BigStr* GetUserName(int uid) {
165 BigStr* result = kEmptyString;
166
167 if (passwd* pw = getpwuid(uid)) {
168 result = StrFromC(pw->pw_name);
169 } else {
170 throw Alloc<IOError>(errno);
171 }
172
173 return result;
174}
175
176BigStr* OsType() {
177 BigStr* result = kEmptyString;
178
179 utsname un = {};
180 if (::uname(&un) == 0) {
181 result = StrFromC(un.sysname);
182 } else {
183 throw Alloc<IOError>(errno);
184 }
185
186 return result;
187}
188
189Tuple2<mops::BigInt, mops::BigInt> GetRLimit(int resource) {
190 struct rlimit lim;
191 if (::getrlimit(resource, &lim) < 0) {
192 throw Alloc<IOError>(errno);
193 }
194 return Tuple2<mops::BigInt, mops::BigInt>(lim.rlim_cur, lim.rlim_max);
195}
196
197void SetRLimit(int resource, mops::BigInt soft, mops::BigInt hard) {
198 struct rlimit lim;
199 lim.rlim_cur = soft;
200 lim.rlim_max = hard;
201
202 if (::setrlimit(resource, &lim) < 0) {
203 throw Alloc<IOError>(errno);
204 }
205}
206
207Tuple3<double, double, double> Time() {
208 struct timeval now;
209 if (gettimeofday(&now, nullptr) < 0) {
210 throw Alloc<IOError>(errno); // could be a permission error
211 }
212 double real = now.tv_sec + static_cast<double>(now.tv_usec) / 1e6;
213
214 struct rusage ru;
215 if (::getrusage(RUSAGE_SELF, &ru) == -1) {
216 throw Alloc<IOError>(errno);
217 }
218 struct timeval* u = &(ru.ru_utime);
219 struct timeval* s = &(ru.ru_stime);
220
221 double user = u->tv_sec + static_cast<double>(u->tv_usec) / 1e6;
222 double sys = s->tv_sec + static_cast<double>(s->tv_usec) / 1e6;
223
224 return Tuple3<double, double, double>(real, user, sys);
225}
226
227static void PrintClock(clock_t ticks, long ticks_per_sec) {
228 double seconds = static_cast<double>(ticks) / ticks_per_sec;
229 printf("%ldm%.3fs", static_cast<long>(seconds) / 60, fmod(seconds, 60));
230}
231
232// bash source: builtins/times.def
233void PrintTimes() {
234 struct tms t;
235 if (times(&t) == -1) {
236 throw Alloc<IOError>(errno);
237 }
238 long ticks_per_sec = sysconf(_SC_CLK_TCK);
239
240 PrintClock(t.tms_utime, ticks_per_sec);
241 putc(' ', stdout);
242 PrintClock(t.tms_stime, ticks_per_sec);
243 putc('\n', stdout);
244 PrintClock(t.tms_cutime, ticks_per_sec);
245 putc(' ', stdout);
246 PrintClock(t.tms_cstime, ticks_per_sec);
247 putc('\n', stdout);
248}
249
250bool InputAvailable(int fd) {
251 fd_set fds;
252 FD_ZERO(&fds);
253 struct timeval timeout = {0}; // return immediately
254 FD_SET(fd, &fds);
255 return select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0;
256}
257
258IOError_OSError* FlushStdout() {
259 // Flush libc buffers
260 if (::fflush(stdout) != 0) {
261 return Alloc<IOError>(errno);
262 }
263 return nullptr;
264}
265
266SignalSafe* InitSignalSafe() {
267 gSignalSafe = Alloc<SignalSafe>();
268 gHeap.RootGlobalVar(gSignalSafe);
269
270 RegisterSignalInterest(SIGINT); // for KeyboardInterrupt checks
271
272 return gSignalSafe;
273}
274
275// Note that the Python implementation of pyos.sigaction() calls
276// signal.signal(), which calls PyOS_setsig(), which calls sigaction() #ifdef
277// HAVE_SIGACTION.
278void sigaction(int sig_num, void (*handler)(int)) {
279 // SIGINT must be registered through SignalSafe
280 DCHECK(sig_num != SIGINT);
281
282 struct sigaction act = {};
283 act.sa_handler = handler;
284 if (sigaction(sig_num, &act, nullptr) != 0) {
285 throw Alloc<OSError>(errno);
286 }
287}
288
289static void OurSignalHandler(int sig_num) {
290 assert(gSignalSafe != nullptr);
291 gSignalSafe->UpdateFromSignalHandler(sig_num);
292}
293
294void RegisterSignalInterest(int sig_num) {
295 struct sigaction act = {};
296 act.sa_handler = OurSignalHandler;
297 if (sigaction(sig_num, &act, nullptr) != 0) {
298 throw Alloc<OSError>(errno);
299 }
300}
301
302Tuple2<BigStr*, int>* MakeDirCacheKey(BigStr* path) {
303 struct stat st;
304 if (::stat(path->data(), &st) == -1) {
305 throw Alloc<OSError>(errno);
306 }
307
308 return Alloc<Tuple2<BigStr*, int>>(path, st.st_mtime);
309}
310
311Tuple2<int, void*> PushTermAttrs(int fd, int mask) {
312 struct termios* term_attrs =
313 static_cast<struct termios*>(malloc(sizeof(struct termios)));
314
315 if (tcgetattr(fd, term_attrs) < 0) {
316 throw Alloc<OSError>(errno);
317 }
318 // Flip the bits in one field
319 int orig_local_modes = term_attrs->c_lflag;
320 term_attrs->c_lflag = orig_local_modes & mask;
321
322 if (tcsetattr(fd, TCSANOW, term_attrs) < 0) {
323 throw Alloc<OSError>(errno);
324 }
325
326 return Tuple2<int, void*>(orig_local_modes, term_attrs);
327}
328
329void PopTermAttrs(int fd, int orig_local_modes, void* term_attrs) {
330 struct termios* t = static_cast<struct termios*>(term_attrs);
331 t->c_lflag = orig_local_modes;
332 if (tcsetattr(fd, TCSANOW, t) < 0) {
333 ; // Like Python, ignore error because of issue #1001
334 }
335}
336
337} // namespace pyos
338
339namespace pyutil {
340
341double infinity() {
342 return INFINITY; // float.h
343}
344
345double nan() {
346 return NAN; // float.h
347}
348
349// TODO: SHARE with pyext
350bool IsValidCharEscape(BigStr* c) {
351 DCHECK(len(c) == 1);
352
353 int ch = c->data_[0];
354
355 if (ch == '/' || ch == '.' || ch == '-') {
356 return false;
357 }
358 if (ch == ' ') { // foo\ bar is idiomatic
359 return true;
360 }
361 return ispunct(ch);
362}
363
364BigStr* ChArrayToString(List<int>* ch_array) {
365 int n = len(ch_array);
366 BigStr* result = NewStr(n);
367 for (int i = 0; i < n; ++i) {
368 result->data_[i] = ch_array->at(i);
369 }
370 result->data_[n] = '\0';
371 return result;
372}
373
374BigStr* _ResourceLoader::Get(BigStr* path) {
375 TextFile* t = gEmbeddedFiles; // start of generated data
376 while (t->rel_path != nullptr) {
377 if (strcmp(t->rel_path, path->data_) == 0) {
378 return t->contents;
379 }
380 t++;
381 }
382 // Emulate Python
383 throw Alloc<IOError>(ENOENT);
384}
385
386_ResourceLoader* GetResourceLoader() {
387 return Alloc<_ResourceLoader>();
388}
389
390BigStr* GetVersion(_ResourceLoader* loader) {
391 return consts::gVersion;
392}
393
394void PrintVersionDetails(_ResourceLoader* loader) {
395 // Invoked by core/util.py VersionFlag()
396 printf("git commit = %s\n", gCommitHash);
397
398 // TODO: I would like the CPU, OS, compiler
399 // How do we get those? Look at CPython
400}
401
402BigStr* BackslashEscape(BigStr* s, BigStr* meta_chars) {
403 int upper_bound = len(s) * 2;
404 BigStr* buf = OverAllocatedStr(upper_bound);
405 char* p = buf->data_;
406
407 for (int i = 0; i < len(s); ++i) {
408 char c = s->data_[i];
409 if (memchr(meta_chars->data_, c, len(meta_chars))) {
410 *p++ = '\\';
411 }
412 *p++ = c;
413 }
414 buf->MaybeShrink(p - buf->data_);
415 return buf;
416}
417
418BigStr* strerror(IOError_OSError* e) {
419 BigStr* s = StrFromC(::strerror(e->errno_));
420 return s;
421}
422
423static grammar::Grammar* gOilGrammar = nullptr;
424
425grammar::Grammar* LoadYshGrammar(_ResourceLoader*) {
426 if (gOilGrammar != nullptr) {
427 return gOilGrammar;
428 }
429
430 gOilGrammar = Alloc<grammar::Grammar>();
431 gHeap.RootGlobalVar(gOilGrammar);
432 return gOilGrammar;
433}
434
435} // namespace pyutil