Executive Shell
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

136 lines
4.0 KiB

// main.c
// Main source file for ESH
// Copyright (C) 2020, Jakob Wakeling
// All rights reserved.
/*
OMKOV Permissive Licence, version 1.0
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal with
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimers.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimers in the documentation and/or
other materials provided with the distribution.
* Neither the names of the copyright holders, nor the names of its contributors
may be used to endorse or promote products derived from this Software without
specific prior written permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
*/
#include "esh.h"
#include "lineread.h"
#include "util/util.h"
#include "cll/error.h"
#include "cll/optget.h"
#include <sys/stat.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static struct lop lops[] = {
{ "help", ARG_NUL, 256 },
{ "version", ARG_NUL, 257 },
{ "debug-lex", ARG_NUL, 258 },
{ "debug-parse", ARG_NUL, 259 },
{ NULL, 0, 0 }
};
bool Lflag = false, Pflag = false;
int _loop = 1, _ret;
static jmp_buf jmp;
static sig_atomic_t jmpflag = false;
static void reset(int signo);
static void eshrc(void);
static void hlp(void);
static void ver(void);
int main(int ac, char *av[]) { (void)(ac); A0 = av[0];
struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops;
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
case 256: { hlp(); return 0; }
case 257: { ver(); return 0; }
case 258: { Lflag = true; } break;
case 259: { Pflag = true; } break;
default: { return 1; }
}
signal(SIGINT, &reset); signal(SIGTSTP, SIG_IGN); signal(SIGQUIT, SIG_IGN);
if (errno) { warn("%s", serr()); }
eshrc();
do {
if (sigsetjmp(jmp, 1)) { fputc('\n', stdout); } jmpflag = true;
u8 *line = lineread();
if (!line) { if (errno) { warn("%s, %d", serr(), errno); } break; }
eval(line, strlen((char *)line)); free(line);
} while (_loop);
linefree(); return warned;
}
static void reset(int signo) { (void)(signo);
if (jmpflag) { siglongjmp(jmp, 1); }
}
/* Evaluate $XDG_CONFIG_HOME/esh/eshrc file. */
static void eshrc(void) {
char *dir = strdup(getenv("XDG_CONFIG_HOME"));
/* If XDG_CONFIG_HOME is unset or empty, use the default */
if (!dir || !dir[0]) {
char *home = getenv("HOME");
if (!home || !home[0]) { warn("$HOME is unset or empty"); return; }
dir = assert_malloc(strlen(home) + strlen("/.config") + 1);
strcpy(dir, home); strcat(dir, "/.config");
}
char *path, *base = "/esh/eshrc";
path = assert_malloc(strlen(dir) + strlen(base) + 1);
strcpy(path, dir); strcat(path, base); free(dir);
eval_file((u8 *)path); free(path);
}
static void hlp(void) {
puts("ESH - Executive Shell\n");
puts("Usage: esh\n");
puts("Options:");
puts(" --help Display help information");
puts(" --version Display version information");
return;
}
static void ver(void) {
puts("ESH, version " PROJECT_VERSION);
puts("Copyright (C) 2020, Jakob Wakeling");
puts("All rights reserved.");
puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
return;
}