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 |
|
27 | extern char** environ;
|
28 |
|
29 | namespace pyos {
|
30 |
|
31 | SignalSafe* gSignalSafe = nullptr;
|
32 |
|
33 | Tuple2<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 |
|
45 | Tuple2<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 |
|
66 | Tuple2<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 |
|
81 | Dict<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 |
|
104 | int Chdir(BigStr* dest_dir) {
|
105 | if (chdir(dest_dir->data_) == 0) {
|
106 | return 0; // success
|
107 | } else {
|
108 | return errno;
|
109 | }
|
110 | }
|
111 |
|
112 | BigStr* 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 |
|
124 | BigStr* 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 |
|
134 | List<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 |
|
164 | BigStr* 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 |
|
176 | BigStr* 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 |
|
189 | Tuple2<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 |
|
197 | void 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 |
|
207 | Tuple3<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 |
|
227 | static 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
|
233 | void 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 |
|
250 | bool 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 |
|
258 | IOError_OSError* FlushStdout() {
|
259 | // Flush libc buffers
|
260 | if (::fflush(stdout) != 0) {
|
261 | return Alloc<IOError>(errno);
|
262 | }
|
263 | return nullptr;
|
264 | }
|
265 |
|
266 | SignalSafe* InitSignalSafe() {
|
267 | gSignalSafe = Alloc<SignalSafe>();
|
268 | gHeap.RootGlobalVar(gSignalSafe);
|
269 |
|
270 | RegisterSignalInterest(SIGINT); // for KeyboardInterrupt checks
|
271 |
|
272 | return gSignalSafe;
|
273 | }
|
274 |
|
275 | void Sigaction(int sig_num, void (*handler)(int)) {
|
276 | struct sigaction act = {};
|
277 | act.sa_handler = handler;
|
278 | if (sigaction(sig_num, &act, nullptr) != 0) {
|
279 | throw Alloc<OSError>(errno);
|
280 | }
|
281 | }
|
282 |
|
283 | static void signal_handler(int sig_num) {
|
284 | assert(gSignalSafe != nullptr);
|
285 | gSignalSafe->UpdateFromSignalHandler(sig_num);
|
286 | }
|
287 |
|
288 | void RegisterSignalInterest(int sig_num) {
|
289 | struct sigaction act = {};
|
290 | act.sa_handler = signal_handler;
|
291 | assert(sigaction(sig_num, &act, nullptr) == 0);
|
292 | }
|
293 |
|
294 | Tuple2<BigStr*, int>* MakeDirCacheKey(BigStr* path) {
|
295 | struct stat st;
|
296 | if (::stat(path->data(), &st) == -1) {
|
297 | throw Alloc<OSError>(errno);
|
298 | }
|
299 |
|
300 | return Alloc<Tuple2<BigStr*, int>>(path, st.st_mtime);
|
301 | }
|
302 |
|
303 | Tuple2<int, void*> PushTermAttrs(int fd, int mask) {
|
304 | struct termios* term_attrs =
|
305 | static_cast<struct termios*>(malloc(sizeof(struct termios)));
|
306 |
|
307 | if (tcgetattr(fd, term_attrs) < 0) {
|
308 | throw Alloc<OSError>(errno);
|
309 | }
|
310 | // Flip the bits in one field
|
311 | int orig_local_modes = term_attrs->c_lflag;
|
312 | term_attrs->c_lflag = orig_local_modes & mask;
|
313 |
|
314 | if (tcsetattr(fd, TCSANOW, term_attrs) < 0) {
|
315 | throw Alloc<OSError>(errno);
|
316 | }
|
317 |
|
318 | return Tuple2<int, void*>(orig_local_modes, term_attrs);
|
319 | }
|
320 |
|
321 | void PopTermAttrs(int fd, int orig_local_modes, void* term_attrs) {
|
322 | struct termios* t = static_cast<struct termios*>(term_attrs);
|
323 | t->c_lflag = orig_local_modes;
|
324 | if (tcsetattr(fd, TCSANOW, t) < 0) {
|
325 | ; // Like Python, ignore error because of issue #1001
|
326 | }
|
327 | }
|
328 |
|
329 | } // namespace pyos
|
330 |
|
331 | namespace pyutil {
|
332 |
|
333 | float infinity() {
|
334 | return INFINITY; // float.h
|
335 | }
|
336 |
|
337 | float nan() {
|
338 | return NAN; // float.h
|
339 | }
|
340 |
|
341 | // TODO: SHARE with pyext
|
342 | bool IsValidCharEscape(BigStr* c) {
|
343 | DCHECK(len(c) == 1);
|
344 |
|
345 | int ch = c->data_[0];
|
346 |
|
347 | if (ch == '/' || ch == '.' || ch == '-') {
|
348 | return false;
|
349 | }
|
350 | if (ch == ' ') { // foo\ bar is idiomatic
|
351 | return true;
|
352 | }
|
353 | return ispunct(ch);
|
354 | }
|
355 |
|
356 | BigStr* ChArrayToString(List<int>* ch_array) {
|
357 | int n = len(ch_array);
|
358 | BigStr* result = NewStr(n);
|
359 | for (int i = 0; i < n; ++i) {
|
360 | result->data_[i] = ch_array->at(i);
|
361 | }
|
362 | result->data_[n] = '\0';
|
363 | return result;
|
364 | }
|
365 |
|
366 | BigStr* _ResourceLoader::Get(BigStr* path) {
|
367 | TextFile* t = gEmbeddedFiles; // start of generated data
|
368 | while (t->rel_path != nullptr) {
|
369 | if (strcmp(t->rel_path, path->data_) == 0) {
|
370 | return t->contents;
|
371 | }
|
372 | t++;
|
373 | }
|
374 | // Emulate Python
|
375 | throw Alloc<IOError>(ENOENT);
|
376 | }
|
377 |
|
378 | _ResourceLoader* GetResourceLoader() {
|
379 | return Alloc<_ResourceLoader>();
|
380 | }
|
381 |
|
382 | BigStr* GetVersion(_ResourceLoader* loader) {
|
383 | return consts::gVersion;
|
384 | }
|
385 |
|
386 | void PrintVersionDetails(_ResourceLoader* loader) {
|
387 | // Invoked by core/util.py VersionFlag()
|
388 | printf("git commit = %s\n", gCommitHash);
|
389 |
|
390 | // TODO: I would like the CPU, OS, compiler
|
391 | // How do we get those? Look at CPython
|
392 | }
|
393 |
|
394 | BigStr* BackslashEscape(BigStr* s, BigStr* meta_chars) {
|
395 | int upper_bound = len(s) * 2;
|
396 | BigStr* buf = OverAllocatedStr(upper_bound);
|
397 | char* p = buf->data_;
|
398 |
|
399 | for (int i = 0; i < len(s); ++i) {
|
400 | char c = s->data_[i];
|
401 | if (memchr(meta_chars->data_, c, len(meta_chars))) {
|
402 | *p++ = '\\';
|
403 | }
|
404 | *p++ = c;
|
405 | }
|
406 | buf->MaybeShrink(p - buf->data_);
|
407 | return buf;
|
408 | }
|
409 |
|
410 | BigStr* strerror(IOError_OSError* e) {
|
411 | BigStr* s = StrFromC(::strerror(e->errno_));
|
412 | return s;
|
413 | }
|
414 |
|
415 | static grammar::Grammar* gOilGrammar = nullptr;
|
416 |
|
417 | grammar::Grammar* LoadYshGrammar(_ResourceLoader*) {
|
418 | if (gOilGrammar != nullptr) {
|
419 | return gOilGrammar;
|
420 | }
|
421 |
|
422 | gOilGrammar = Alloc<grammar::Grammar>();
|
423 | gHeap.RootGlobalVar(gOilGrammar);
|
424 | return gOilGrammar;
|
425 | }
|
426 |
|
427 | } // namespace pyutil
|