ESH

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

ESH/src/builtin/eval.c (53 lines, 1.2 KiB) -rw-r--r-- blame download

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

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

#include <stdio.h>
#include <string.h>

static const char *const help;
extern const char *const version; /* main.c */

int bltn_eval(int ac, char *av[]) {
	struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = (struct lop[]){
		{ "help",    ARG_NUL, 256 },
		{ "version", ARG_NUL, 257 },
		{ NULL, 0, 0 },
	};
	
	for (int c; (c = optget(&opt, av, 0)) != -1;) {
		switch (c) {
			case 256: { fputs(help, stdout);    } return 0;
			case 257: { fputs(version, stdout); } return 0;
			default: {} return -1;
		}
	}
	
	char *args, *dest; u64 size = 0;
	
	for (u64 i = 0; i < ac - opt.ind; i += 1) {
		size += (strlen(av[opt.ind + i]) + 1);
	}
	
	args = xcalloc(size, sizeof (*args)); dest = args;
	
	for (u64 i = 0; i < ac - opt.ind; i += 1) {
		if (i != 0) { strncat(dest, " ", 1); dest += 1; }
		strcat(dest, av[opt.ind + i]); dest += strlen(av[opt.ind + i]);
	}
	
	eval(args, size); return 0;
}

static const char *const help =
	"eval - Execute arguments as a command\n"
	"Usage:\n"
	"  eval [argument...]\n"
	"Options:\n"
	"  --help     Display help information\n"
	"  --version  Display version information\n"
;