2019-08-17 17:09:43 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2019-08-18 06:39:08 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2019-08-14 10:48:13 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-08-09 16:12:19 -04:00
|
|
|
#include <string.h>
|
2019-08-26 09:52:42 -04:00
|
|
|
#include <termios.h>
|
2019-08-09 16:12:19 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-08-26 15:12:15 -04:00
|
|
|
#include "scheme.h"
|
2019-08-09 16:12:19 -04:00
|
|
|
|
|
|
|
void path_to_dirname(char *path)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!(p = strrchr(path, '/'))) {
|
|
|
|
p = path;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_cwd(char *buf, size_t size) { getcwd(buf, size); }
|
|
|
|
|
|
|
|
int set_cwd(char *buf)
|
|
|
|
{
|
|
|
|
if (chdir(buf) == -1)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-14 10:48:13 -04:00
|
|
|
|
|
|
|
value_t builtin_pid(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("pid", nargs, 0);
|
|
|
|
return fixnum(getpid());
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_parent_pid(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("parent-pid", nargs, 0);
|
|
|
|
return fixnum(getppid());
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_process_group(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("process-group", nargs, 0);
|
|
|
|
return fixnum(getpgid(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_user_effective_gid(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("user-effective-gid", nargs, 0);
|
|
|
|
return fixnum(getegid());
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_user_effective_uid(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("user-effective-uid", nargs, 0);
|
|
|
|
return fixnum(geteuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_user_real_gid(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("user-real-gid", nargs, 0);
|
|
|
|
return fixnum(getgid());
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_user_real_uid(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("user-real-uid", nargs, 0);
|
|
|
|
return fixnum(getuid());
|
|
|
|
}
|
2019-08-18 06:39:08 -04:00
|
|
|
|
|
|
|
int os_path_exists(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st) == -1) {
|
|
|
|
return FL_F;
|
|
|
|
}
|
|
|
|
return FL_T;
|
|
|
|
}
|
2019-08-21 15:16:07 -04:00
|
|
|
|
|
|
|
void os_setenv(const char *name, const char *value)
|
|
|
|
{
|
|
|
|
if (value) {
|
|
|
|
if (setenv(name, value, 1) != 0) {
|
|
|
|
lerror(ArgError, "os.setenv: cannot set environment variable");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unsetenv(name) != 0) {
|
|
|
|
lerror(ArgError, "os.setenv: cannot unset environment variable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 09:52:42 -04:00
|
|
|
|
|
|
|
// TODO: cleanup
|
|
|
|
static struct termios term_mode_orig;
|
|
|
|
static struct termios term_mode_raw;
|
|
|
|
|
|
|
|
static void term_mode_init(void)
|
|
|
|
{
|
|
|
|
static int done;
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
done = 1;
|
|
|
|
tcgetattr(0, &term_mode_orig);
|
|
|
|
cfmakeraw(&term_mode_raw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_term_init(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("term-init", nargs, 0);
|
|
|
|
term_mode_init();
|
|
|
|
tcsetattr(0, TCSAFLUSH, &term_mode_raw);
|
|
|
|
return FL_T;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_term_exit(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
(void)args;
|
|
|
|
argcount("term-exit", nargs, 0);
|
|
|
|
term_mode_init();
|
|
|
|
tcsetattr(0, TCSAFLUSH, &term_mode_orig);
|
|
|
|
return FL_T;
|
|
|
|
}
|