// Copyright (C) 2023, Jakob Wakeling // All rights reserved. #include "../eval.h" #include "../util/optget.h" #include "../util/util.h" #include #include 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" ;