ESH

Executive Shell
git clone http://git.omkov.net/ESH
Log | Tree | Refs | README | Download

ESH/src/main.c (83 lines, 1.8 KiB) -rw-r--r-- blame download

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
// Copyright (C) 2023, Jakob Wakeling
// All rights reserved.

#include "eval.h"
#include "lineread/lineread.h"
#include "util/log.h"
#include "util/optget.h"
#include "util/util.h"

#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void reset(int);

static const char *const help;
const char *const version;

static jmp_buf jmp;
static sig_atomic_t jmpflag = false;
bool _loop = true;

int main(int, char *av[]) {
	struct opt opt = OPTGET_INIT; opt.str = "Ep"; opt.lops = (struct lop[]){
		{ "help",    ARG_NUL, 256 },
		{ "version", ARG_NUL, 257 },
		{ "debug",   ARG_NUL, 258 },
		{ NULL, 0, 0 },
	};

	struct {} args = {};

	for (int c; (c = optget(&opt, av, 1)) != -1;) {
		switch (c) {
			case 'E': { Eflag = true;   } break;
			case 'p': { pflag = true;   } break;
			case 258: { __debug = true; } break;
			case 256: { fputs(help, stdout);    } return 0;
			case 257: { fputs(version, stdout); } return 0;
			default: {} return -1;
		}
	}

	signal(SIGINT, &reset); signal(SIGQUIT, SIG_IGN); signal(SIGTSTP, SIG_IGN);
	atexit(&linefree);

	do {
		if (sigsetjmp(jmp, 1)) { fputc('\n', stdout); } jmpflag = true;

		char *l = lineread();
		if (!l) { if (errno) { log_warn("lineread: %s", strerror(errno)); } break; }

		eval(l, strlen(l)); free(l);
	} while (_loop);

	return __warned;
}

static void reset(int) {
	if (jmpflag) { siglongjmp(jmp, 1); }
}

static const char *const help =
	"ESH - Executive Shell\n"
	"Usage:\n"
	"  esh [--debug]\n"
	"Options:\n"
	"  -E         Output lexer tokens\n"
	"  -p         Output parser AST \n"
	"  --debug    Enable debug logging\n"
	"  --help     Display help information\n"
	"  --version  Display version information\n"
;

const char *const version =
	"ESH, version " PROJECT_VERSION "\n"
	"Copyright (C) 2023, Jakob Wakeling\n"
	"All rights reserved.\n"
;