mirror of
https://github.com/ksherlock/mpw-shell.git
synced 2025-07-19 05:24:10 +00:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
fa804e91f0 | ||
|
0ec15bcd63 | ||
|
06e2e1f309 | ||
|
32c72cb89a | ||
|
f125b533f7 | ||
|
f6c5478063 | ||
|
efd51bcb48 | ||
|
33dd3228f8 | ||
|
548880a517 | ||
|
1da6c3e9da | ||
|
bdba86249e | ||
|
87f5398649 | ||
|
811c8b976a | ||
|
0422976719 | ||
|
2893f7fe79 | ||
|
34a4f431c0 | ||
|
f3db9b7cc0 |
@@ -1,25 +1,50 @@
|
||||
|
||||
# CMAKE_INSTALL_PREFIX defaults to /usr/local.
|
||||
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
project("mpw-shell")
|
||||
set (PROJECT_TYPE "CXX")
|
||||
set (PROJECT_NAME "MPW Shell")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-g -Wall -Wno-unused-variable -Wno-multichar -O1")
|
||||
|
||||
# -std=c++14
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
|
||||
set(CMAKE_CXX_EXTENSIONS FALSE)
|
||||
|
||||
|
||||
#
|
||||
# build config.h
|
||||
#
|
||||
|
||||
INCLUDE (CheckFunctionExists)
|
||||
INCLUDE (CheckLibraryExists)
|
||||
|
||||
SET(CMAKE_EXTRA_INCLUDE_FILES stdio.h)
|
||||
CHECK_FUNCTION_EXISTS(dprintf HAVE_DPRINTF)
|
||||
SET(CMAKE_EXTRA_INCLUDE_FILES)
|
||||
|
||||
CHECK_LIBRARY_EXISTS(edit readline "" HAVE_LIBEDIT)
|
||||
CHECK_LIBRARY_EXISTS(readline readline "" HAVE_LIBREADLINE)
|
||||
CHECK_LIBRARY_EXISTS(history add_history "" HAVE_LIBHISTORY)
|
||||
|
||||
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
|
||||
|
||||
|
||||
add_compile_options(-g -Wall -Wno-unused-variable -Wno-multichar -O1)
|
||||
|
||||
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-const-variable ")
|
||||
add_compile_options(-Wno-unused-const-variable)
|
||||
endif()
|
||||
|
||||
|
||||
if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-but-set-variable")
|
||||
add_compile_options(-Wno-unused-but-set-variable)
|
||||
endif()
|
||||
|
||||
|
||||
|
||||
add_definitions(-I ${CMAKE_SOURCE_DIR}/ -I ${CMAKE_CURRENT_BINARY_DIR}/ )
|
||||
include_directories(${CMAKE_SOURCE_DIR})
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
# from https://github.com/gsauthof/cmake-ragel
|
||||
macro(RAGEL_TARGET Name Input Output)
|
||||
@@ -65,17 +90,17 @@ RAGEL_TARGET(value value.rl value.cpp COMPILE_FLAGS "-p -G2")
|
||||
|
||||
# need to copy all OUTPUT file to the build dir
|
||||
add_custom_command(
|
||||
OUTPUT phase2-parser.cpp phase2-parser.h phase2-parser.out
|
||||
COMMAND lemon++ phase2-parser.lemon
|
||||
COMMAND mv -f phase2-parser.cpp phase2-parser.h phase2-parser.out ${CMAKE_CURRENT_BINARY_DIR}/
|
||||
MAIN_DEPENDENCY phase2-parser.lemon
|
||||
OUTPUT phase3.cpp phase3.h phase3.out
|
||||
COMMAND lemon++ phase3.lemon
|
||||
COMMAND mv -f phase3.cpp phase3.h phase3.out ${CMAKE_CURRENT_BINARY_DIR}/
|
||||
MAIN_DEPENDENCY phase3.lemon
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
|
||||
add_executable(mpw-shell mpw-shell.cpp mpw-shell-token.cpp mpw-shell-expand.cpp
|
||||
mpw-shell-parser.cpp value.cpp mpw-shell-quote.cpp
|
||||
phase1.cpp phase2.cpp phase2-parser.cpp command.cpp environment.cpp builtins.cpp
|
||||
mpw-shell-parser.cpp mpw_parser.cpp value.cpp mpw-shell-quote.cpp
|
||||
phase1.cpp phase2.cpp phase3.cpp command.cpp environment.cpp builtins.cpp
|
||||
pathnames.cpp
|
||||
macroman.cpp
|
||||
cxx/mapped_file.cpp
|
||||
@@ -84,12 +109,19 @@ add_executable(mpw-shell mpw-shell.cpp mpw-shell-token.cpp mpw-shell-expand.cpp
|
||||
cxx/directory_iterator.cpp
|
||||
)
|
||||
|
||||
#
|
||||
# -ledit includes history stuff. gnu -lreadline does not.
|
||||
#
|
||||
if(HAVE_LIBEDIT)
|
||||
target_link_libraries(mpw-shell edit)
|
||||
elseif(HAVE_LIBREADLINE)
|
||||
target_link_libraries(mpw-shell readline)
|
||||
if (HAVE_LIBHISTORY)
|
||||
target_link_libraries(mpw-shell history)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
# all this for -std=c++14
|
||||
set_property (TARGET mpw-shell PROPERTY CXX_STANDARD 14)
|
||||
set_property (TARGET mpw-shell PROPERTY CXX_STANDARD_REQUIRED TRUE)
|
||||
set_property (TARGET mpw-shell PROPERTY CXX_EXTENSIONS FALSE)
|
||||
|
||||
# create a symlink for mpw-make
|
||||
add_custom_command(
|
||||
|
22
README.md
22
README.md
@@ -11,16 +11,17 @@ Supported features
|
||||
* Begin ... End
|
||||
* Loop ... End
|
||||
* For name In [word...] ... End
|
||||
* Break [If], Continue [If]
|
||||
* Break [If], Continue [If], Exit [If]
|
||||
* ( ... )
|
||||
* ||
|
||||
* &&
|
||||
* redirection
|
||||
* Redirection
|
||||
* | "pipes" (via a temporary file. Presumably, that's what MPW did as well.)
|
||||
* Subshells (`...`, ``...``)
|
||||
|
||||
|
||||
Not (yet) supported
|
||||
-------------
|
||||
* subshells (`...`, ``...``)
|
||||
* aliases
|
||||
* regular expressions
|
||||
* text-editing commands (search forward/backward, et cetera)
|
||||
@@ -33,9 +34,11 @@ Builtin Commands
|
||||
* Directory
|
||||
* Echo
|
||||
* Evaluate
|
||||
* Execute
|
||||
* Exists
|
||||
* Export
|
||||
* Parameters
|
||||
* Quit
|
||||
* Quote
|
||||
* Set
|
||||
* Shift
|
||||
@@ -49,7 +52,18 @@ Builtin Commands
|
||||
Setup
|
||||
-----
|
||||
1. Install MPW. The mpw binary should be somewhere in your `$PATH`.
|
||||
It also checks `/usr/local/bin/mpw` and `$HOME/mpw/bin/mpw`
|
||||
It also checks `/usr/local/bin/mpw` and `$HOME/mpw/bin/mpw`. You can
|
||||
use mpw-shell without it but only with builtin commands.
|
||||
2. Copy the `Startup` script to `$HOME/mpw/`. This script is executed
|
||||
when mpw-shell (or mpw-make) starts up (imagine that) and should
|
||||
be used to set environment variables.
|
||||
|
||||
|
||||
Command Line Flags
|
||||
------------------
|
||||
|
||||
-D name=value Define environment variable
|
||||
-v Be verbose (equivalent to -Decho=1)
|
||||
-f Ignore the Startup script
|
||||
-c string Execute string
|
||||
-h Display help
|
||||
|
155
builtins.cpp
155
builtins.cpp
@@ -4,6 +4,7 @@
|
||||
#include "fdset.h"
|
||||
#include "value.h"
|
||||
#include "environment.h"
|
||||
#include "error.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -25,6 +26,7 @@
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "version.h"
|
||||
#include "config.h"
|
||||
|
||||
namespace ToolBox {
|
||||
std::string MacToUnix(const std::string path);
|
||||
@@ -114,16 +116,23 @@ namespace {
|
||||
#define fputc DO_NOT_USE_FPUTC
|
||||
|
||||
inline int fdputs(const char *data, int fd) {
|
||||
auto rv = write(fd, data, strlen(data)); return rv < 0 ? EOF : rv;
|
||||
auto rv = write(fd, data, strlen(data));
|
||||
return rv < 0 ? EOF : rv;
|
||||
}
|
||||
|
||||
inline int fdputs(const std::string &s, int fd) {
|
||||
auto rv = write(fd, s.data(), s.size());
|
||||
return rv < 0 ? EOF : rv;
|
||||
}
|
||||
|
||||
inline int fdputc(int c, int fd) {
|
||||
unsigned char tmp = c;
|
||||
auto rv = write(fd, &tmp, 1); return rv < 0 ? EOF : c;
|
||||
auto rv = write(fd, &tmp, 1);
|
||||
return rv < 0 ? EOF : c;
|
||||
}
|
||||
|
||||
#ifdef HAVE_DPRINTF
|
||||
#define fdprintf(...) dprintf(__VA_ARGS__)
|
||||
#define fdprintf dprintf
|
||||
#else
|
||||
inline int fdprintf(int fd, const char *format, ...) {
|
||||
char *cp = nullptr;
|
||||
@@ -271,22 +280,14 @@ static int export_common(Environment &env, bool export_or_unexport, const std::v
|
||||
|
||||
const char *name = export_or_unexport ? "Export" : "Unexport";
|
||||
|
||||
struct {
|
||||
int _r = 0;
|
||||
int _s = 0;
|
||||
} flags;
|
||||
bool _r = false;
|
||||
bool _s = false;
|
||||
bool error = false;
|
||||
|
||||
std::vector<std::string> argv = getopt(tokens, [&](char c){
|
||||
switch(c) {
|
||||
case 'r':
|
||||
case 'R':
|
||||
flags._r = true;
|
||||
break;
|
||||
case 's':
|
||||
case 'S':
|
||||
flags._s = true;
|
||||
break;
|
||||
auto argv = getopt(tokens, [&](char c){
|
||||
switch(tolower(c)) {
|
||||
case 'r': _r = true; break;
|
||||
case 's': _s = true; break;
|
||||
default:
|
||||
fdprintf(stderr, "### %s - \"-%c\" is not an option.\n", name, c);
|
||||
error = true;
|
||||
@@ -300,7 +301,7 @@ static int export_common(Environment &env, bool export_or_unexport, const std::v
|
||||
}
|
||||
|
||||
if (argv.empty()) {
|
||||
if (flags._r && flags._s) goto conflict;
|
||||
if (_r && _s) goto conflict;
|
||||
|
||||
// list of exported vars.
|
||||
// -r will generate unexport commands for exported variables.
|
||||
@@ -312,14 +313,14 @@ static int export_common(Environment &env, bool export_or_unexport, const std::v
|
||||
for (const auto &kv : env) {
|
||||
const std::string& vname = kv.first;
|
||||
if (kv.second == export_or_unexport)
|
||||
fdprintf(stdout, "%s%s\n", flags._s ? "" : name, quote(vname).c_str());
|
||||
fdprintf(stdout, "%s%s\n", _s ? "" : name, quote(vname).c_str());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
// mark as exported.
|
||||
|
||||
if (flags._r || flags._s) goto conflict;
|
||||
if (_r || _s) goto conflict;
|
||||
|
||||
for (std::string s : argv) {
|
||||
auto iter = env.find(s);
|
||||
@@ -501,12 +502,9 @@ int builtin_directory(Environment &env, const std::vector<std::string> &tokens,
|
||||
bool error = false;
|
||||
|
||||
std::vector<std::string> argv = getopt(tokens, [&](char c){
|
||||
switch(c)
|
||||
switch(tolower(c))
|
||||
{
|
||||
case 'q':
|
||||
case 'Q':
|
||||
q = true;
|
||||
break;
|
||||
case 'q': q = true; break;
|
||||
default:
|
||||
fdprintf(stderr, "### Directory - \"-%c\" is not an option.\n", c);
|
||||
error = true;
|
||||
@@ -577,24 +575,12 @@ int builtin_exists(Environment &env, const std::vector<std::string> &tokens, con
|
||||
std::vector<std::string> argv = getopt(tokens, [&](char c){
|
||||
switch(tolower(c))
|
||||
{
|
||||
case 'a':
|
||||
_a = true;
|
||||
break;
|
||||
case 'd':
|
||||
_d = true;
|
||||
break;
|
||||
case 'f':
|
||||
_f = true;
|
||||
break;
|
||||
case 'n':
|
||||
_n = true;
|
||||
break;
|
||||
case 'q':
|
||||
_q = true;
|
||||
break;
|
||||
case 'w':
|
||||
_w = true;
|
||||
break;
|
||||
case 'a': _a = true; break;
|
||||
case 'd': _d = true; break;
|
||||
case 'f': _f = true; break;
|
||||
case 'n': _n = true; break;
|
||||
case 'q': _q = true; break;
|
||||
case 'w': _w = true; break;
|
||||
default:
|
||||
fdprintf(stderr, "### Exists - \"-%c\" is not an option.\n", c);
|
||||
error = true;
|
||||
@@ -765,15 +751,15 @@ int builtin_evaluate(Environment &env, std::vector<token> &&tokens, const fdmask
|
||||
int builtin_which(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
|
||||
|
||||
// which [-a] [-p] [command]
|
||||
bool a = false;
|
||||
bool p = false;
|
||||
bool _a = false;
|
||||
bool _p = false;
|
||||
bool error = false;
|
||||
|
||||
std::vector<std::string> argv = getopt(tokens, [&](char c){
|
||||
switch(c)
|
||||
switch(tolower(c))
|
||||
{
|
||||
case 'a': case 'A': a = true; break;
|
||||
case 'p': case 'P': p = true; break;
|
||||
case 'a': _a = true; break;
|
||||
case 'p': _p = true; break;
|
||||
|
||||
default:
|
||||
fdprintf(stderr, "### Which - \"-%c\" is not an option.\n", c);
|
||||
@@ -824,7 +810,7 @@ int builtin_which(Environment &env, const std::vector<std::string> &tokens, cons
|
||||
}
|
||||
|
||||
for(; ss; ++ss) {
|
||||
if (p) fdprintf(stderr, "checking %s\n", ss->c_str());
|
||||
if (_p) fdprintf(stderr, "checking %s\n", ss->c_str());
|
||||
|
||||
std::error_code ec;
|
||||
fs::path p(ToolBox::MacToUnix(ss->c_str()));
|
||||
@@ -833,13 +819,13 @@ int builtin_which(Environment &env, const std::vector<std::string> &tokens, cons
|
||||
if (fs::exists(p, ec)) {
|
||||
found = true;
|
||||
fdprintf(stdout, "%s\n", quote(p).c_str());
|
||||
if (!a) break;
|
||||
if (!_a) break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// check builtins...
|
||||
if (!found || a) {
|
||||
if (!found || _a) {
|
||||
|
||||
static const char *builtins[] = {
|
||||
"aboutbox",
|
||||
@@ -847,12 +833,15 @@ int builtin_which(Environment &env, const std::vector<std::string> &tokens, cons
|
||||
"catenate",
|
||||
"directory",
|
||||
"echo",
|
||||
"execute",
|
||||
"exists",
|
||||
"export",
|
||||
"false", // not in MPW
|
||||
"parameters",
|
||||
"quote",
|
||||
"set",
|
||||
"shift",
|
||||
"true", // not in MPW
|
||||
"unalias",
|
||||
"unexport",
|
||||
"unset",
|
||||
@@ -940,9 +929,7 @@ int builtin_version(Environment &env, const std::vector<std::string> &tokens, co
|
||||
auto argv = getopt(tokens, [&](char c){
|
||||
switch(tolower(c))
|
||||
{
|
||||
case 'v':
|
||||
_v = true;
|
||||
break;
|
||||
case 'v': _v = true; break;
|
||||
default:
|
||||
fdprintf(stderr, "### Version - \"-%c\" is not an option.\n", c);
|
||||
error = true;
|
||||
@@ -1026,3 +1013,63 @@ int builtin_true(Environment &, const std::vector<std::string> &, const fdmask &
|
||||
int builtin_false(Environment &, const std::vector<std::string> &, const fdmask &) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int builtin_quit(Environment &, const std::vector<std::string> &tokens, const fdmask &fds) {
|
||||
|
||||
bool error = false;
|
||||
bool _y = false;
|
||||
bool _n = false;
|
||||
bool _c = false;
|
||||
|
||||
auto argv = getopt(tokens, [&](char c){
|
||||
switch(tolower(c))
|
||||
{
|
||||
case 'c': _c = true; break;
|
||||
case 'n': _n = true; break;
|
||||
case 'y': _y = true; break;
|
||||
|
||||
default:
|
||||
fdprintf(stderr, "### Quit - \"-%c\" is not an option.\n", c);
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
});
|
||||
if (_y + _n + _c > 1) {
|
||||
fdprintf(stderr, "### Quit - Conflicting options were specified.\n");
|
||||
error = true;
|
||||
}
|
||||
|
||||
if (argv.size() > 0) {
|
||||
fdprintf(stderr, "### Quit - Too many parameters were specified.\n");
|
||||
error = true;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
fdprintf(stderr, "# Usage - Quit [-y | -n | -c]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
throw quit_command_t{};
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int builtin_execute(Environment &e, const std::vector<std::string> &tokens, const fdmask &fds) {
|
||||
|
||||
// runs argv[1] in the current environment. unlike MPW, argv[1] must be a script.
|
||||
// special enhancement -- '-' means execute stdin.
|
||||
|
||||
if (tokens.size() < 2) return 0;
|
||||
std::string filename = tokens[1];
|
||||
if (filename == "-") {
|
||||
// since we're parsing stdin, don't let any children read it. [???]
|
||||
fdset new_fds;
|
||||
int fd = open("/dev/null", O_RDONLY);
|
||||
new_fds.set(0, fd);
|
||||
|
||||
return read_fd(e, fds[0], new_fds | fds);
|
||||
}
|
||||
|
||||
return read_file(e, filename, fds);
|
||||
}
|
||||
|
@@ -25,6 +25,12 @@ int builtin_which(Environment &e, const std::vector<std::string> &, const fdmask
|
||||
int builtin_alias(Environment &e, const std::vector<std::string> &, const fdmask &);
|
||||
int builtin_unalias(Environment &e, const std::vector<std::string> &, const fdmask &);
|
||||
|
||||
int builtin_execute(Environment &e, const std::vector<std::string> &, const fdmask &);
|
||||
int builtin_true(Environment &e, const std::vector<std::string> &, const fdmask &);
|
||||
int builtin_false(Environment &e, const std::vector<std::string> &, const fdmask &);
|
||||
int builtin_quit(Environment &e, const std::vector<std::string> &, const fdmask &);
|
||||
|
||||
|
||||
int builtin_evaluate(Environment &e, std::vector<token> &&, const fdmask &);
|
||||
|
||||
#endif
|
337
command.cpp
337
command.cpp
@@ -1,10 +1,11 @@
|
||||
#include "command.h"
|
||||
#include "phase2-parser.h"
|
||||
#include "phase3.h"
|
||||
#include "environment.h"
|
||||
#include "fdset.h"
|
||||
#include "builtins.h"
|
||||
#include "mpw-shell.h"
|
||||
#include "error.h"
|
||||
#include "value.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
@@ -17,6 +18,7 @@
|
||||
#include "cxx/filesystem.h"
|
||||
#include "cxx/string_splitter.h"
|
||||
|
||||
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
@@ -40,8 +42,7 @@ typedef std::vector<token> token_vector;
|
||||
|
||||
namespace {
|
||||
|
||||
struct break_command_t {};
|
||||
struct continue_command_t {};
|
||||
|
||||
|
||||
/*
|
||||
* returns:
|
||||
@@ -56,6 +57,12 @@ namespace {
|
||||
return -3;
|
||||
}
|
||||
|
||||
int bad_exit() {
|
||||
fprintf(stderr, "### Exit - Missing if keyword.\n");
|
||||
fprintf(stderr, "# Usage - Exit [Number] [if expression...]\n");
|
||||
return -3;
|
||||
}
|
||||
|
||||
int evaluate(int type, token_vector &&tokens, Environment &env) {
|
||||
std::reverse(tokens.begin(), tokens.end());
|
||||
|
||||
@@ -64,6 +71,8 @@ namespace {
|
||||
switch(type) {
|
||||
default: return 0;
|
||||
|
||||
// exit [number] [if expr] ([number has been removed])
|
||||
case EXIT:
|
||||
case BREAK:
|
||||
case CONTINUE:
|
||||
case ELSE:
|
||||
@@ -77,10 +86,12 @@ namespace {
|
||||
case BREAK: name = "Break"; break;
|
||||
case CONTINUE: name = "Continue"; break;
|
||||
case ELSE: name = "Else"; break;
|
||||
case EXIT: name = "Exit"; return bad_exit(); break;
|
||||
}
|
||||
return bad_if(name);
|
||||
}
|
||||
// fall through.
|
||||
|
||||
case IF:
|
||||
tokens.pop_back();
|
||||
try {
|
||||
@@ -198,9 +209,11 @@ namespace {
|
||||
{"catenate", builtin_catenate},
|
||||
{"directory", builtin_directory},
|
||||
{"echo", builtin_echo},
|
||||
{"execute", builtin_execute},
|
||||
{"exists", builtin_exists},
|
||||
{"export", builtin_export},
|
||||
{"parameters", builtin_parameters},
|
||||
{"quit", builtin_quit},
|
||||
{"quote", builtin_quote},
|
||||
{"set", builtin_set},
|
||||
{"shift", builtin_shift},
|
||||
@@ -209,6 +222,10 @@ namespace {
|
||||
{"unset", builtin_unset},
|
||||
{"version", builtin_version},
|
||||
{"which", builtin_which},
|
||||
|
||||
// not in MPW.
|
||||
{"true", builtin_true},
|
||||
{"false", builtin_false},
|
||||
};
|
||||
|
||||
|
||||
@@ -278,9 +295,6 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
|
||||
//std::string expand_vars(const std::string &s, const class Environment &);
|
||||
|
||||
command::~command()
|
||||
{}
|
||||
|
||||
@@ -293,7 +307,7 @@ command::~command()
|
||||
|
||||
|
||||
template<class F>
|
||||
int exec(std::string command, Environment &env, bool throwup, F &&fx) {
|
||||
int exec(std::string command, Environment &env, const fdmask &fds, bool throwup, F &&fx) {
|
||||
|
||||
bool echo = true;
|
||||
int rv = 0;
|
||||
@@ -302,7 +316,7 @@ int exec(std::string command, Environment &env, bool throwup, F &&fx) {
|
||||
|
||||
try {
|
||||
process p;
|
||||
command = expand_vars(command, env);
|
||||
command = expand_vars(command, env, fds);
|
||||
auto tokens = tokenize(command, false);
|
||||
if (tokens.empty()) return 0;
|
||||
parse_tokens(std::move(tokens), p);
|
||||
@@ -330,7 +344,7 @@ int exec(std::string command, Environment &env, bool throwup, F &&fx) {
|
||||
int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
|
||||
return exec(text, env, throwup, [&](process &p){
|
||||
return exec(text, env, fds, throwup, [&](process &p){
|
||||
|
||||
fdmask newfds = p.fds | fds;
|
||||
|
||||
@@ -367,7 +381,7 @@ int simple_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
}
|
||||
|
||||
template<class F>
|
||||
int eval_exec(std::string command, Environment &env, bool throwup, F &&fx){
|
||||
int eval_exec(std::string command, Environment &env, const fdmask &fds, bool throwup, F &&fx){
|
||||
|
||||
std::string name;
|
||||
bool echo = true;
|
||||
@@ -376,7 +390,7 @@ int eval_exec(std::string command, Environment &env, bool throwup, F &&fx){
|
||||
if (control_c) throw execution_of_input_terminated();
|
||||
|
||||
try {
|
||||
command = expand_vars(command, env);
|
||||
command = expand_vars(command, env, fds);
|
||||
auto tokens = tokenize(command, true);
|
||||
|
||||
if (tokens.empty()) return 0;
|
||||
@@ -386,6 +400,10 @@ int eval_exec(std::string command, Environment &env, bool throwup, F &&fx){
|
||||
|
||||
rv = fx(tokens);
|
||||
}
|
||||
catch (const exit_command_t &ex) {
|
||||
// convert to execution of input terminated.
|
||||
throw execution_of_input_terminated(ex.value);
|
||||
}
|
||||
|
||||
catch (mpw_error &e) {
|
||||
if (echo) env.echo("%s", command.c_str());
|
||||
@@ -406,29 +424,8 @@ int eval_exec(std::string command, Environment &env, bool throwup, F &&fx){
|
||||
|
||||
int evaluate_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
#if 0
|
||||
if (control_c) throw execution_of_input_terminated();
|
||||
|
||||
std::string s = expand_vars(text, env);
|
||||
|
||||
env.set("command", "evaluate");
|
||||
|
||||
env.echo("%s", s.c_str());
|
||||
|
||||
try {
|
||||
auto tokens = tokenize(s, true);
|
||||
if (tokens.empty()) return 0;
|
||||
|
||||
int status = builtin_evaluate(env, std::move(tokens), fds);
|
||||
|
||||
return env.status(status, throwup);
|
||||
} catch (std::exception &e) {
|
||||
fprintf(stderr, "%s\n", e.what());
|
||||
return env.status(1, throwup);
|
||||
}
|
||||
#endif
|
||||
|
||||
return eval_exec(text, env, throwup, [&](token_vector &tokens){
|
||||
return eval_exec(text, env, fds, throwup, [&](token_vector &tokens){
|
||||
env.set("command", "evaluate");
|
||||
return builtin_evaluate(env, std::move(tokens), fds);
|
||||
});
|
||||
@@ -439,7 +436,7 @@ int evaluate_command::execute(Environment &env, const fdmask &fds, bool throwup)
|
||||
|
||||
int break_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return eval_exec(text, env, throwup, [&](token_vector &tokens){
|
||||
return eval_exec(text, env, fds, throwup, [&](token_vector &tokens){
|
||||
env.set("command", "break");
|
||||
if (!env.loop()) throw break_error();
|
||||
int status = evaluate(BREAK, std::move(tokens), env);
|
||||
@@ -447,30 +444,11 @@ int break_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
return status;
|
||||
});
|
||||
|
||||
#if 0
|
||||
if (control_c) throw execution_of_input_terminated();
|
||||
|
||||
std::string s = expand_vars(text, env);
|
||||
|
||||
env.set("command", "break");
|
||||
|
||||
env.echo("%s", s.c_str());
|
||||
if (!env.loop()) {
|
||||
fputs("### MPW Shell - Break must be within for or loop.\n", stderr);
|
||||
return env.status(-3, throwup);
|
||||
}
|
||||
|
||||
// check/evaluate if clause.
|
||||
int status = evaluate(BREAK, s, env);
|
||||
if (status > 0)
|
||||
throw break_command_t();
|
||||
return env.status(status, throwup);
|
||||
#endif
|
||||
}
|
||||
|
||||
int continue_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return eval_exec(text, env, throwup, [&](token_vector &tokens){
|
||||
return eval_exec(text, env, fds, throwup, [&](token_vector &tokens){
|
||||
env.set("command", "continue");
|
||||
if (!env.loop()) throw continue_error();
|
||||
int status = evaluate(CONTINUE, std::move(tokens), env);
|
||||
@@ -478,26 +456,39 @@ int continue_command::execute(Environment &env, const fdmask &fds, bool throwup)
|
||||
return status;
|
||||
});
|
||||
|
||||
#if 0
|
||||
if (control_c) throw execution_of_input_terminated();
|
||||
|
||||
std::string s = expand_vars(text, env);
|
||||
|
||||
env.set("command", "continue");
|
||||
|
||||
env.echo("%s", s.c_str());
|
||||
if (!env.loop()) {
|
||||
fputs("### MPW Shell - Continue must be within for or loop.\n", stderr);
|
||||
return env.status(-3, throwup);
|
||||
}
|
||||
|
||||
|
||||
// check/evaluate if clause.
|
||||
int status = evaluate(CONTINUE, s, env);
|
||||
if (status > 0)
|
||||
throw continue_command_t();
|
||||
return env.status(status, throwup);
|
||||
#endif
|
||||
int exit_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
// exit
|
||||
// exit [number] [if expr ]]
|
||||
// todo --
|
||||
|
||||
return eval_exec(text, env, fds, throwup, [&](token_vector &tokens){
|
||||
env.set("command", "exit");
|
||||
env.status(0);
|
||||
|
||||
if (tokens.size() <= 1) {
|
||||
throw exit_command_t{};
|
||||
}
|
||||
|
||||
|
||||
int status = 0;
|
||||
value v(tokens[1]);
|
||||
// remove the return value to make processing easier.
|
||||
if (v.is_number()) {
|
||||
tokens.erase(tokens.begin() + 1);
|
||||
}
|
||||
status = evaluate(EXIT, std::move(tokens), env);
|
||||
|
||||
if (status) {
|
||||
int ok = v.to_number(0);
|
||||
throw exit_command_t{ok};
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
int or_command::execute(Environment &e, const fdmask &fds, bool throwup) {
|
||||
@@ -550,8 +541,9 @@ int pipe_command::execute(Environment &e, const fdmask &fds, bool throwup) {
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
|
||||
rv = children[1]->execute(e, pipe_fd | fds, throwup);
|
||||
|
||||
return e.status(rv, throwup);
|
||||
}
|
||||
|
||||
if (children[0]) return children[0]->execute(e, fds, throwup);
|
||||
if (children[1]) return children[1]->execute(e, fds, throwup);
|
||||
return e.status(0, throwup);
|
||||
@@ -576,7 +568,7 @@ int error_command::execute(Environment &e, const fdmask &fds, bool throwup) {
|
||||
return e.status(-3);
|
||||
}
|
||||
|
||||
std::string s = expand_vars(text, e);
|
||||
std::string s = expand_vars(text, e, fds);
|
||||
|
||||
e.echo("%s", s.c_str());
|
||||
|
||||
@@ -600,7 +592,7 @@ int error_command::execute(Environment &e, const fdmask &fds, bool throwup) {
|
||||
}
|
||||
|
||||
template<class F>
|
||||
int begin_end_exec(std::string begin, std::string end, Environment &env, bool throwup, F &&fx) {
|
||||
int begin_end_exec(std::string begin, std::string end, Environment &env, const fdmask &fds, bool throwup, F &&fx) {
|
||||
|
||||
|
||||
bool echo = true;
|
||||
@@ -610,8 +602,8 @@ int begin_end_exec(std::string begin, std::string end, Environment &env, bool th
|
||||
|
||||
try {
|
||||
process p;
|
||||
begin = expand_vars(begin, env);
|
||||
end = expand_vars(end, env);
|
||||
begin = expand_vars(begin, env, fds);
|
||||
end = expand_vars(end, env, fds);
|
||||
|
||||
auto b = tokenize(begin, true);
|
||||
auto e = tokenize(end, false);
|
||||
@@ -638,12 +630,11 @@ int begin_end_exec(std::string begin, std::string end, Environment &env, bool th
|
||||
}
|
||||
|
||||
return env.status(rv, throwup);
|
||||
|
||||
}
|
||||
|
||||
int begin_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return begin_end_exec(begin, end, env, throwup, [&](token_vector &b, process &p){
|
||||
return begin_end_exec(begin, end, env, fds, throwup, [&](token_vector &b, process &p){
|
||||
|
||||
env.set("command", type == BEGIN ? "end" : ")");
|
||||
if (b.size() != 1) {
|
||||
@@ -663,53 +654,12 @@ int begin_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return rv;
|
||||
});
|
||||
|
||||
#if 0
|
||||
|
||||
std::string b = expand_vars(begin, env);
|
||||
std::string e = expand_vars(end, env);
|
||||
|
||||
|
||||
env.set("command", type == BEGIN ? "end" : ")");
|
||||
|
||||
// echo!
|
||||
env.echo("%s ... %s",
|
||||
b.c_str(),
|
||||
e.c_str()
|
||||
);
|
||||
|
||||
|
||||
|
||||
// tokenize the begin and end commands.
|
||||
// begin may not have extra arguments. end may have redirection.
|
||||
|
||||
auto bt = tokenize(b, true);
|
||||
if (bt.size() != 1) {
|
||||
fprintf(stderr, "### Begin - Too many parameters were specified.\n");
|
||||
fprintf(stderr, "Usage - Begin\n");
|
||||
return env.status(-3);
|
||||
}
|
||||
|
||||
fdmask newfds;
|
||||
int status = check_ends(e, newfds);
|
||||
newfds |= fds;
|
||||
if (status) return env.status(status);
|
||||
|
||||
int rv;
|
||||
env.indent_and([&]{
|
||||
rv = vector_command::execute(env, newfds);
|
||||
});
|
||||
|
||||
env.echo("%s", type == BEGIN ? "end" : ")");
|
||||
|
||||
return env.status(rv);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int loop_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return begin_end_exec(begin, end, env, throwup, [&](token_vector &b, process &p){
|
||||
return begin_end_exec(begin, end, env, fds, throwup, [&](token_vector &b, process &p){
|
||||
|
||||
env.set("command", "end");
|
||||
if (b.size() != 1) {
|
||||
@@ -741,58 +691,11 @@ int loop_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return rv;
|
||||
});
|
||||
|
||||
|
||||
#if 0
|
||||
std::string b = expand_vars(begin, env);
|
||||
std::string e = expand_vars(end, env);
|
||||
|
||||
|
||||
env.set("command", "end");
|
||||
|
||||
// echo!
|
||||
env.echo("%s ... %s",
|
||||
b.c_str(),
|
||||
e.c_str()
|
||||
);
|
||||
|
||||
// check for extra tokens...
|
||||
auto bt = tokenize(b, true);
|
||||
if (bt.size() != 1) {
|
||||
fprintf(stderr, "### Loop - Too many parameters were specified.\n");
|
||||
fprintf(stderr, "Usage - Loop\n");
|
||||
return env.status(-3);
|
||||
}
|
||||
|
||||
fdmask newfds;
|
||||
int status = check_ends(e, newfds);
|
||||
newfds |= fds;
|
||||
if (status) return env.status(status);
|
||||
|
||||
int rv = 0;
|
||||
for(;;) {
|
||||
|
||||
if (control_c) throw execution_of_input_terminated();
|
||||
|
||||
try {
|
||||
env.loop_indent_and([&]{
|
||||
rv = vector_command::execute(env, newfds);
|
||||
});
|
||||
}
|
||||
catch (break_command_t &ex) {
|
||||
env.echo("end");
|
||||
break;
|
||||
}
|
||||
catch (continue_command_t &ex) {}
|
||||
env.echo("end");
|
||||
}
|
||||
return env.status(rv);
|
||||
#endif
|
||||
}
|
||||
|
||||
int for_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return begin_end_exec(begin, end, env, throwup, [&](token_vector &b, process &p){
|
||||
return begin_end_exec(begin, end, env, fds, throwup, [&](token_vector &b, process &p){
|
||||
|
||||
env.set("command", "end");
|
||||
|
||||
@@ -826,56 +729,6 @@ int for_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
|
||||
return rv;
|
||||
});
|
||||
|
||||
#if 0
|
||||
std::string b = expand_vars(begin, env);
|
||||
std::string e = expand_vars(end, env);
|
||||
|
||||
|
||||
env.set("command", "end");
|
||||
|
||||
// echo!
|
||||
env.echo("%s ... %s",
|
||||
b.c_str(),
|
||||
e.c_str()
|
||||
);
|
||||
|
||||
// check for extra tokens...
|
||||
auto bt = tokenize(b, true);
|
||||
if (bt.size() < 3 || strcasecmp(bt[2].string.c_str(), "in")) {
|
||||
fprintf(stderr, "### For - Missing in keyword.\n");
|
||||
fprintf(stderr, "Usage - For name in [word...]\n");
|
||||
return env.status(-3);
|
||||
}
|
||||
|
||||
fdmask newfds;
|
||||
int status = check_ends(e, newfds);
|
||||
newfds |= fds;
|
||||
if (status) return env.status(status);
|
||||
|
||||
int rv = 0;
|
||||
for (int i = 3; i < bt.size(); ++i ) {
|
||||
|
||||
if (control_c) throw execution_of_input_terminated();
|
||||
|
||||
env.set(bt[1].string, bt[i].string);
|
||||
|
||||
try {
|
||||
env.loop_indent_and([&]{
|
||||
rv = vector_command::execute(env, newfds);
|
||||
});
|
||||
}
|
||||
catch (break_command_t &ex) {
|
||||
env.echo("end");
|
||||
break;
|
||||
}
|
||||
catch (continue_command_t &ex) {}
|
||||
env.echo("end");
|
||||
}
|
||||
|
||||
return env.status(rv);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -900,7 +753,7 @@ int if_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
int tmp;
|
||||
if (first) {
|
||||
first = false;
|
||||
tmp = begin_end_exec(c->clause, end, env, false, [&](token_vector &b, process &p){
|
||||
tmp = begin_end_exec(c->clause, end, env, fds, false, [&](token_vector &b, process &p){
|
||||
|
||||
newfds = p.fds | fds;
|
||||
|
||||
@@ -922,7 +775,7 @@ int if_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
}
|
||||
else {
|
||||
// second...
|
||||
tmp = eval_exec(c->clause, env, false, [&](token_vector &b){
|
||||
tmp = eval_exec(c->clause, env, fds, false, [&](token_vector &b){
|
||||
if (skip || error) return 0;
|
||||
|
||||
int status = evaluate(c->type, std::move(b), env);
|
||||
@@ -945,49 +798,5 @@ int if_command::execute(Environment &env, const fdmask &fds, bool throwup) {
|
||||
env.echo("end");
|
||||
if (error) return env.status(error, throwup);
|
||||
return env.status(rv, throwup);
|
||||
|
||||
#if 0
|
||||
|
||||
|
||||
|
||||
|
||||
int rv = 0;
|
||||
bool ok = false;
|
||||
|
||||
std::string e = expand_vars(end, env);
|
||||
|
||||
env.set("command", "end");
|
||||
|
||||
// parse end for indirection.
|
||||
fdmask newfds;
|
||||
int status = check_ends(e, newfds);
|
||||
newfds |= fds;
|
||||
if (status) {
|
||||
rv = status;
|
||||
ok = true;
|
||||
}
|
||||
|
||||
for (auto &c : clauses) {
|
||||
std::string s = expand_vars(c->clause, env);
|
||||
|
||||
if (c->type == IF)
|
||||
env.echo("%s ... %s", s.c_str(), e.c_str());
|
||||
else
|
||||
env.echo("%s", s.c_str());
|
||||
|
||||
if (ok) continue;
|
||||
|
||||
int tmp = evaluate(c->type, s, env);
|
||||
if (tmp < 0) { ok = true; rv = tmp; continue; }
|
||||
if (tmp == 0) continue;
|
||||
|
||||
env.indent_and([&](){
|
||||
rv = c->execute(env, newfds);
|
||||
});
|
||||
}
|
||||
|
||||
env.echo("end");
|
||||
return env.status(rv);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
13
command.h
13
command.h
@@ -6,7 +6,7 @@
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include "phase2-parser.h"
|
||||
#include "phase3.h"
|
||||
|
||||
typedef std::unique_ptr<struct command> command_ptr;
|
||||
typedef std::vector<command_ptr> command_ptr_vector;
|
||||
@@ -22,7 +22,7 @@ struct command {
|
||||
{}
|
||||
|
||||
virtual bool terminal() const noexcept {
|
||||
return type == EVALUATE || type == COMMAND || type == BREAK || type == CONTINUE || type == ERROR;
|
||||
return type == EVALUATE || type == COMMAND || type == BREAK || type == CONTINUE || type == ERROR || type == EXIT;
|
||||
}
|
||||
|
||||
int type = 0;
|
||||
@@ -81,6 +81,15 @@ struct continue_command : public command {
|
||||
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
|
||||
};
|
||||
|
||||
struct exit_command : public command {
|
||||
|
||||
template<class S>
|
||||
exit_command(S &&s) : command(EXIT), text(std::forward<S>(s))
|
||||
{}
|
||||
|
||||
std::string text;
|
||||
virtual int execute(Environment &e, const fdmask &fds, bool throwup) final override;
|
||||
};
|
||||
|
||||
|
||||
struct binary_command : public command {
|
||||
|
7
config.h.in
Normal file
7
config.h.in
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef __mpw_shell_config_h__
|
||||
#define __mpw_shell_config_h__
|
||||
|
||||
#cmakedefine HAVE_DPRINTF
|
||||
|
||||
#endif
|
||||
|
@@ -269,10 +269,10 @@ namespace filesystem {
|
||||
path current_path(error_code& ec) {
|
||||
|
||||
char *cp;
|
||||
char buffer[PATH_MAX];
|
||||
char buffer[PATH_MAX+1];
|
||||
|
||||
ec.clear();
|
||||
cp = ::getcwd(buffer, sizeof(buffer));
|
||||
cp = ::getcwd(buffer, PATH_MAX);
|
||||
if (cp) return path(cp);
|
||||
|
||||
ec = error_code(errno, std::system_category());
|
||||
@@ -304,7 +304,7 @@ namespace filesystem {
|
||||
|
||||
path canonical(const path& p, error_code& ec) {
|
||||
char *cp;
|
||||
char buffer[PATH_MAX];
|
||||
char buffer[PATH_MAX+1];
|
||||
|
||||
ec.clear();
|
||||
cp = realpath(p.c_str(), buffer);
|
||||
@@ -316,7 +316,7 @@ namespace filesystem {
|
||||
path canonical(const path& p, const path& base, error_code& ec) {
|
||||
|
||||
char *cp;
|
||||
char buffer[PATH_MAX];
|
||||
char buffer[PATH_MAX+1];
|
||||
|
||||
ec.clear();
|
||||
|
||||
|
@@ -23,17 +23,12 @@ namespace {
|
||||
FX _fx;
|
||||
};
|
||||
|
||||
void throw_error(int error) {
|
||||
throw std::system_error(error, std::system_category());
|
||||
|
||||
void set_or_throw_error(std::error_code *ec, int error, const std::string &what) {
|
||||
if (ec) *ec = std::error_code(error, std::system_category());
|
||||
else throw std::system_error(error, std::system_category(), what);
|
||||
}
|
||||
|
||||
|
||||
void throw_error(int error, const std::string &what) {
|
||||
throw std::system_error(error, std::system_category(), what);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -41,12 +36,8 @@ namespace {
|
||||
|
||||
namespace {
|
||||
|
||||
void throw_error() {
|
||||
throw_error(GetLastError());
|
||||
}
|
||||
|
||||
void throw_error(const std::string &what) {
|
||||
throw_error(GetLastError(), what);
|
||||
void set_or_throw_error(std::error_code *ec, const std::string &what) {
|
||||
set_or_throw_error(ec, GetLastError(), what);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -61,7 +52,7 @@ void mapped_file_base::close() {
|
||||
}
|
||||
}
|
||||
|
||||
void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, size_t offset) {
|
||||
void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, size_t offset, std::error_code *ec) {
|
||||
|
||||
|
||||
HANDLE fh;
|
||||
@@ -70,9 +61,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si
|
||||
// length of 0 in CreateFileMapping / MapViewOfFile
|
||||
// means map the entire file.
|
||||
|
||||
if (is_open()) {
|
||||
throw std::runtime_error("mapped_file_base::open - file already open");
|
||||
}
|
||||
if (is_open()) close();
|
||||
|
||||
fh = CreateFile(p.c_str(),
|
||||
flags == readonly ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
|
||||
@@ -83,7 +72,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si
|
||||
nullptr
|
||||
);
|
||||
if (fh == INVALID_HANDLE_VALUE) {
|
||||
throw_error();
|
||||
return set_or_throw_error(ec, "CreateFile");
|
||||
}
|
||||
|
||||
auto fh_close = make_unique_resource(fh, CloseHandle);
|
||||
@@ -116,7 +105,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si
|
||||
|
||||
mh = CreateFileMapping(fh, nullptr, protect, 0, 0, 0);
|
||||
if (mh == INVALID_HANDLE_VALUE) {
|
||||
throw_error();
|
||||
return set_or_throw_error(ec, "CreateFileMapping");
|
||||
}
|
||||
|
||||
auto mh_close = make_unique_resource(mh, CloseHandle);
|
||||
@@ -129,7 +118,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si
|
||||
length,
|
||||
nullptr);
|
||||
if (!_data) {
|
||||
throw_error();
|
||||
return set_or_throw_error(ec, "MapViewOfFileEx");
|
||||
}
|
||||
|
||||
|
||||
@@ -152,10 +141,7 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
const DWORD access = FILE_MAP_WRITE;
|
||||
|
||||
|
||||
if (is_open()) {
|
||||
throw std::runtime_error("mapped_file_base::create - file already open");
|
||||
}
|
||||
|
||||
if (is_open()) close();
|
||||
|
||||
fh = CreateFile(p.c_str(),
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
@@ -166,7 +152,7 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
nullptr
|
||||
);
|
||||
if (fh == INVALID_HANDLE_VALUE) {
|
||||
throw_error();
|
||||
return set_or_throw_error(nullptr, "CreateFile");
|
||||
}
|
||||
|
||||
auto fh_close = make_unique_resource(fh, CloseHandle);
|
||||
@@ -174,11 +160,11 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
|
||||
file_size.QuadPart = length;
|
||||
if (!SetFilePointerEx(fh, file_size, nullptr, FILE_BEGIN));
|
||||
if (!SetEndOfFile(fh)) throw_error();
|
||||
if (!SetEndOfFile(fh)) return set_or_throw_error(nullptr, "SetEndOfFile");
|
||||
|
||||
mh = CreateFileMapping(fh, nullptr, protect, 0, 0, 0);
|
||||
if (mh == INVALID_HANDLE_VALUE) {
|
||||
throw_error();
|
||||
return set_or_throw_error(nullptr, "CreateFileMapping");
|
||||
}
|
||||
|
||||
auto mh_close = make_unique_resource(mh, CloseHandle);
|
||||
@@ -191,7 +177,7 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
nullptr);
|
||||
|
||||
if (!_data) {
|
||||
throw_error();
|
||||
return set_or_throw_error(nullptr, "MapViewOfFileEx");
|
||||
}
|
||||
|
||||
_file_handle = fh_close.release();
|
||||
@@ -213,13 +199,10 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
|
||||
namespace {
|
||||
|
||||
void throw_error() {
|
||||
throw_error(errno);
|
||||
void set_or_throw_error(std::error_code *ec, const std::string &what) {
|
||||
set_or_throw_error(ec, errno, what);
|
||||
}
|
||||
|
||||
void throw_error(const std::string &what) {
|
||||
throw_error(errno, what);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -232,15 +215,15 @@ void mapped_file_base::close() {
|
||||
}
|
||||
|
||||
|
||||
void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, size_t offset) {
|
||||
void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, size_t offset, std::error_code *ec) {
|
||||
|
||||
if (ec) ec->clear();
|
||||
|
||||
int fd;
|
||||
|
||||
int oflags = 0;
|
||||
|
||||
if (is_open()) {
|
||||
throw std::runtime_error("mapped_file_base::open - file already open");
|
||||
}
|
||||
if (is_open()) close();
|
||||
|
||||
switch (flags) {
|
||||
case readonly:
|
||||
@@ -253,7 +236,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si
|
||||
|
||||
fd = ::open(p.c_str(), oflags);
|
||||
if (fd < 0) {
|
||||
throw_error(errno);
|
||||
return set_or_throw_error(ec, "open");
|
||||
}
|
||||
|
||||
//defer([fd](){::close(fd); });
|
||||
@@ -265,7 +248,8 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si
|
||||
struct stat st;
|
||||
|
||||
if (::fstat(fd, &st) < 0) {
|
||||
throw_error(errno);
|
||||
set_or_throw_error(ec, "stat");
|
||||
return;
|
||||
}
|
||||
length = st.st_size;
|
||||
}
|
||||
@@ -279,7 +263,7 @@ void mapped_file_base::open(const path_type& p, mapmode flags, size_t length, si
|
||||
|
||||
if (_data == MAP_FAILED) {
|
||||
_data = nullptr;
|
||||
throw_error(errno);
|
||||
return set_or_throw_error(ec, "mmap");
|
||||
}
|
||||
|
||||
_fd = close_fd.release();
|
||||
@@ -292,15 +276,11 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
int fd;
|
||||
const size_t offset = 0;
|
||||
|
||||
if (is_open()) {
|
||||
throw std::runtime_error("mapped_file_base::create - file already open");
|
||||
}
|
||||
|
||||
|
||||
if (is_open()) close();
|
||||
|
||||
fd = ::open(p.c_str(), O_RDWR | O_CREAT | O_TRUNC);
|
||||
if (fd < 0) {
|
||||
throw_error(errno);
|
||||
return set_or_throw_error(nullptr, "open");
|
||||
}
|
||||
|
||||
//defer([fd](){::close(fd); });
|
||||
@@ -309,7 +289,7 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
|
||||
|
||||
if (::ftruncate(fd, length) < 0) {
|
||||
throw_error(errno);
|
||||
return set_or_throw_error(nullptr, "ftruncate");
|
||||
}
|
||||
|
||||
|
||||
@@ -320,7 +300,7 @@ void mapped_file_base::create(const path_type& p, size_t length) {
|
||||
|
||||
if (_data == MAP_FAILED) {
|
||||
_data = nullptr;
|
||||
throw_error(errno);
|
||||
return set_or_throw_error(nullptr, "mmap");
|
||||
}
|
||||
|
||||
_fd = close_fd.release();
|
||||
|
@@ -8,7 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <system_error>
|
||||
|
||||
class mapped_file_base {
|
||||
public:
|
||||
@@ -40,7 +40,7 @@ protected:
|
||||
|
||||
void swap(mapped_file_base &rhs);
|
||||
|
||||
void open(const path_type& p, mapmode flags, size_t length, size_t offset);
|
||||
void open(const path_type& p, mapmode flags, size_t length, size_t offset, std::error_code *ec);
|
||||
void create(const path_type &p, size_t new_size); // always creates readwrite.
|
||||
void reset();
|
||||
|
||||
@@ -79,6 +79,22 @@ public:
|
||||
open(p, flags, length, offset);
|
||||
}
|
||||
|
||||
mapped_file(const path_type &p, std::error_code &ec) noexcept {
|
||||
open(p, readonly, -1, 0, ec);
|
||||
}
|
||||
mapped_file(const path_type &p, mapmode flags, std::error_code &ec) noexcept {
|
||||
open(p, flags, -1, 0, ec);
|
||||
}
|
||||
|
||||
mapped_file(const path_type &p, mapmode flags, size_t length, std::error_code &ec) noexcept {
|
||||
open(p, flags, length, 0, ec);
|
||||
}
|
||||
|
||||
mapped_file(const path_type &p, mapmode flags, size_t length, size_t offset, std::error_code &ec) noexcept {
|
||||
open(p, flags, length, offset, ec);
|
||||
}
|
||||
|
||||
|
||||
mapped_file(mapped_file &&);
|
||||
mapped_file(const mapped_file &) = delete;
|
||||
|
||||
@@ -87,7 +103,20 @@ public:
|
||||
|
||||
|
||||
void open(const path_type& p, mapmode flags, size_t length = -1, size_t offset = 0) {
|
||||
base::open(p, flags, length, offset);
|
||||
base::open(p, flags, length, offset, nullptr);
|
||||
}
|
||||
|
||||
void open(const path_type &p, std::error_code &ec) noexcept {
|
||||
base::open(p, readonly, -1, 0, &ec);
|
||||
}
|
||||
void open(const path_type &p, mapmode flags, std::error_code &ec) noexcept {
|
||||
base::open(p, flags, -1, 0, &ec);
|
||||
}
|
||||
void open(const path_type &p, mapmode flags, size_t length, std::error_code &ec) noexcept {
|
||||
base::open(p, flags, length, 0, &ec);
|
||||
}
|
||||
void open(const path_type &p, mapmode flags, size_t length, size_t offset, std::error_code &ec) noexcept {
|
||||
base::open(p, flags, length, offset, &ec);
|
||||
}
|
||||
|
||||
|
||||
|
11
error.h
11
error.h
@@ -67,4 +67,15 @@ public:
|
||||
{}
|
||||
};
|
||||
|
||||
/*
|
||||
these are used for flow-control.
|
||||
they do not inherit from std::exception to prevent being caught
|
||||
by normal handlers.
|
||||
*/
|
||||
|
||||
struct break_command_t {};
|
||||
struct continue_command_t {};
|
||||
struct exit_command_t { int value = 0; };
|
||||
struct quit_command_t {};
|
||||
|
||||
#endif
|
@@ -49,3 +49,9 @@ set -e MWCIncludes "{CIncludes}"
|
||||
set -e ARMCIncludes "{MPW}Interfaces:ARMCIncludes:"
|
||||
|
||||
set -e AsmMatOpts "-t 4"
|
||||
|
||||
# customizations can go in the UserStartup file.
|
||||
if `exists {MPW}UserStartup` != ""
|
||||
execute {MPW}UserStartup
|
||||
end
|
||||
|
||||
|
@@ -81,7 +81,7 @@ std::string utf8_to_macroman(const std::string &s) {
|
||||
rv.reserve(s.size());
|
||||
|
||||
unsigned cs = 0;
|
||||
uint16_t tmp;
|
||||
uint16_t tmp = 0;
|
||||
|
||||
for (unsigned char c : s) {
|
||||
switch(cs) {
|
||||
|
@@ -4,6 +4,8 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
#include <algorithm>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -46,12 +48,25 @@
|
||||
action epush { /* epush */ ev.push_back(fc); }
|
||||
action efinish1 {
|
||||
/* efinish1 */
|
||||
|
||||
/*
|
||||
throw std::runtime_error("MPW Shell - `...` not yet supported.");
|
||||
*/
|
||||
|
||||
std::string s = subshell(ev, env, fds);
|
||||
scratch.append(s);
|
||||
|
||||
fgoto *xcs;
|
||||
}
|
||||
action efinish2 {
|
||||
/* efinish2 */
|
||||
throw std::runtime_error("MPW Shell - `...` not yet supported.");
|
||||
|
||||
std::string s = subshell(ev, env, fds);
|
||||
for (auto c : s) {
|
||||
if (c == '\'' || c == '"' ) scratch.push_back(escape);
|
||||
scratch.push_back(c);
|
||||
}
|
||||
|
||||
fgoto *xcs;
|
||||
}
|
||||
|
||||
@@ -110,9 +125,48 @@
|
||||
|
||||
namespace {
|
||||
%% write data;
|
||||
|
||||
|
||||
std::string subshell(const std::string &s, Environment &env, const fdmask &fds) {
|
||||
|
||||
|
||||
char temp[32] = "/tmp/mpw-shell-XXXXXXXX";
|
||||
|
||||
int fd = mkstemp(temp);
|
||||
unlink(temp);
|
||||
|
||||
fdset new_fds;
|
||||
new_fds.set(1, fd);
|
||||
|
||||
int rv = 0;
|
||||
env.indent_and([&](){
|
||||
|
||||
rv = read_string(env, s, new_fds | fds);
|
||||
|
||||
});
|
||||
|
||||
std::string tmp;
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
for(;;) {
|
||||
uint8_t buffer[1024];
|
||||
ssize_t len = read(fd, buffer, sizeof(buffer));
|
||||
if (len == 0) break;
|
||||
if (len < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
throw std::system_error(errno, std::system_category(), "read");
|
||||
}
|
||||
tmp.append(buffer, buffer + len);
|
||||
}
|
||||
std::transform(tmp.begin(), tmp.end(), tmp.begin(), [](uint8_t x){
|
||||
if (x == '\r' || x == '\n') x = ' ';
|
||||
return x;
|
||||
});
|
||||
return tmp;
|
||||
}
|
||||
|
||||
std::string expand_vars(const std::string &s, const Environment &env) {
|
||||
}
|
||||
|
||||
std::string expand_vars(const std::string &s, Environment &env, const fdmask &fds) {
|
||||
if (s.find_first_of("{`", 0, 2) == s.npos) return s;
|
||||
|
||||
int cs;
|
||||
|
@@ -106,7 +106,7 @@
|
||||
|
||||
|
||||
'-' when eval
|
||||
%push_token => { tokens.emplace_back("+", '-'); };
|
||||
%push_token => { tokens.emplace_back("-", '-'); };
|
||||
|
||||
'!' when eval
|
||||
%push_token => { tokens.emplace_back("!", '!'); };
|
||||
|
289
mpw-shell.cpp
289
mpw-shell.cpp
@@ -2,6 +2,8 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <atomic>
|
||||
#include <algorithm>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
@@ -10,16 +12,15 @@
|
||||
#include <cerrno>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include "mpw-shell.h"
|
||||
#include "mpw_parser.h"
|
||||
|
||||
#include "fdset.h"
|
||||
|
||||
#include "macroman.h"
|
||||
|
||||
#include "phase1.h"
|
||||
#include "phase2.h"
|
||||
#include "command.h"
|
||||
|
||||
#include "cxx/mapped_file.h"
|
||||
#include "cxx/filesystem.h"
|
||||
#include "cxx/string_splitter.h"
|
||||
@@ -33,7 +34,6 @@
|
||||
#include <pwd.h>
|
||||
#include <sysexits.h>
|
||||
#include <paths.h>
|
||||
#include <atomic>
|
||||
|
||||
#include "version.h"
|
||||
|
||||
@@ -80,42 +80,73 @@ void init(Environment &env) {
|
||||
}
|
||||
|
||||
|
||||
int read_file(phase1 &p, const std::string &file) {
|
||||
const mapped_file mf(file, mapped_file::readonly);
|
||||
|
||||
p.process(mf.begin(), mf.end(), false);
|
||||
p.finish();
|
||||
return 0;
|
||||
int read_file(Environment &e, const std::string &file, const fdmask &fds) {
|
||||
std::error_code ec;
|
||||
const mapped_file mf(file, mapped_file::readonly, ec);
|
||||
if (ec) {
|
||||
fprintf(stderr, "# Error reading %s: %s\n", file.c_str(), ec.message().c_str());
|
||||
return e.status(-1, false);
|
||||
}
|
||||
|
||||
int read_fd(phase1 &p, int fd) {
|
||||
|
||||
mpw_parser p(e, fds);
|
||||
e.status(0, false);
|
||||
|
||||
try {
|
||||
p.parse(mf.begin(), mf.end());
|
||||
p.finish();
|
||||
} catch(const execution_of_input_terminated &ex) {
|
||||
return ex.status();
|
||||
}
|
||||
return e.status();
|
||||
}
|
||||
|
||||
|
||||
int read_string(Environment &e, const std::string &s, const fdmask &fds) {
|
||||
mpw_parser p(e, fds);
|
||||
e.status(0, false);
|
||||
try {
|
||||
p.parse(s);
|
||||
p.finish();
|
||||
} catch(const execution_of_input_terminated &ex) {
|
||||
return ex.status();
|
||||
}
|
||||
return e.status();
|
||||
|
||||
}
|
||||
|
||||
|
||||
int read_fd(Environment &e, int fd, const fdmask &fds) {
|
||||
|
||||
unsigned char buffer[2048];
|
||||
ssize_t size;
|
||||
|
||||
mpw_parser p(e, fds);
|
||||
e.status(0, false);
|
||||
|
||||
try {
|
||||
for (;;) {
|
||||
size = read(fd, buffer, sizeof(buffer));
|
||||
if (size < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
perror("read: ");
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
if (size == 0) p.finish();
|
||||
else p.process(buffer, buffer + size);
|
||||
} catch(std::exception &ex) {
|
||||
fprintf(stderr, "### %s\n", ex.what());
|
||||
p.reset();
|
||||
perror("read");
|
||||
e.status(-1, false);
|
||||
}
|
||||
if (size == 0) break;
|
||||
p.parse(buffer, buffer + size);
|
||||
}
|
||||
|
||||
return 0;
|
||||
p.finish();
|
||||
} catch(const execution_of_input_terminated &ex) {
|
||||
return ex.status();
|
||||
}
|
||||
return e.status();
|
||||
}
|
||||
|
||||
void launch_mpw(const Environment &env, const std::vector<std::string> &argv, const fdmask &fds);
|
||||
fs::path which(const Environment &env, const std::string &name);
|
||||
|
||||
int read_make(phase1 &p1, phase2 &p2, Environment &env, const std::vector<std::string> &argv) {
|
||||
int read_make(Environment &env, const std::vector<std::string> &argv) {
|
||||
|
||||
int out[2];
|
||||
int ok;
|
||||
@@ -147,10 +178,9 @@ int read_make(phase1 &p1, phase2 &p2, Environment &env, const std::vector<std::s
|
||||
}
|
||||
|
||||
close(out[1]);
|
||||
int rv = read_fd(p1, out[0]);
|
||||
int rv = read_fd(env, out[0]);
|
||||
close(out[0]);
|
||||
p1.finish();
|
||||
p2.finish();
|
||||
|
||||
|
||||
// check for make errors.
|
||||
for(;;) {
|
||||
@@ -176,7 +206,7 @@ int read_make(phase1 &p1, phase2 &p2, Environment &env, const std::vector<std::s
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
|
||||
return env.status();
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::atomic<int> control_c{0};
|
||||
@@ -190,7 +220,7 @@ void control_c_handler(int signal, siginfo_t *sinfo, void *context) {
|
||||
}
|
||||
|
||||
|
||||
int interactive(Environment &env, phase1 &p1, phase2& p2) {
|
||||
int interactive(Environment &env) {
|
||||
|
||||
std::string history_file = root();
|
||||
history_file += ".history";
|
||||
@@ -207,18 +237,18 @@ int interactive(Environment &env, phase1 &p1, phase2& p2) {
|
||||
|
||||
sigaction(SIGINT, &act, &old_act);
|
||||
|
||||
mpw_parser p(env, true);
|
||||
|
||||
|
||||
for(;;) {
|
||||
const char *prompt = "# ";
|
||||
if (p1.continuation() || p2.continuation()) prompt = "> ";
|
||||
if (p.continuation()) prompt = "> ";
|
||||
char *cp = readline(prompt);
|
||||
if (!cp) {
|
||||
if (control_c) {
|
||||
control_c = 0;
|
||||
fprintf(stdout, "\n");
|
||||
p1.abort();
|
||||
p2.abort();
|
||||
p.abort();
|
||||
env.status(-9, false);
|
||||
continue;
|
||||
}
|
||||
@@ -240,29 +270,17 @@ int interactive(Environment &env, phase1 &p1, phase2& p2) {
|
||||
s = utf8_to_macroman(s);
|
||||
|
||||
s.push_back('\n');
|
||||
try {
|
||||
p1.process(s);
|
||||
|
||||
} catch(std::exception &ex) {
|
||||
fprintf(stderr, "### %s\n", ex.what());
|
||||
p1.reset();
|
||||
}
|
||||
p.parse(s);
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
p1.finish();
|
||||
} catch(std::exception &ex) {
|
||||
fprintf(stderr, "### %s\n", ex.what());
|
||||
p1.reset();
|
||||
}
|
||||
p.finish();
|
||||
|
||||
sigaction(SIGINT, &old_act, nullptr);
|
||||
|
||||
write_history(history_file.c_str());
|
||||
fprintf(stdout, "\n");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -294,6 +312,10 @@ void define(Environment &env, const std::string &s) {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* todo: prevent -r and -s (don't generate shell code)
|
||||
*/
|
||||
void make_help(void) {
|
||||
|
||||
#undef _
|
||||
@@ -329,59 +351,92 @@ int make(int argc, char **argv) {
|
||||
|
||||
std::vector<std::string> args;
|
||||
args.reserve(argc+1);
|
||||
bool __ = false;
|
||||
int c;
|
||||
bool passthrough = false;
|
||||
|
||||
args.emplace_back("make");
|
||||
|
||||
for (auto iter = argv; *iter; ++iter) {
|
||||
std::string tmp(*iter);
|
||||
|
||||
if (!__) {
|
||||
if (tmp == "--")
|
||||
{ __ = true; continue; }
|
||||
|
||||
if (tmp == "--help")
|
||||
{ make_help(); exit(0); }
|
||||
|
||||
if (tmp == "--test" || tmp == "--dry-run")
|
||||
{ e.set("test", "1"); continue; }
|
||||
}
|
||||
args.emplace_back(std::move(tmp));
|
||||
|
||||
}
|
||||
|
||||
phase1 p1;
|
||||
phase2 p2;
|
||||
|
||||
p1 >>= [&p2](std::string &&s) {
|
||||
if (s.empty()) p2.finish();
|
||||
else p2(std::move(s));
|
||||
static struct option longopts[] = {
|
||||
{ "help", no_argument, nullptr, 'h' },
|
||||
{ "verbose", no_argument, nullptr, 'v' },
|
||||
{ "test", no_argument, nullptr, 1 },
|
||||
{ "dry-run", no_argument, nullptr, 2 },
|
||||
{ nullptr, 0, nullptr, 0},
|
||||
};
|
||||
|
||||
p2 >>= [&e](command_ptr_vector &&v) {
|
||||
args.push_back(""); // place-holder.
|
||||
|
||||
for (auto iter = v.begin(); iter != v.end(); ++iter) {
|
||||
auto &ptr = *iter;
|
||||
fdmask fds;
|
||||
try {
|
||||
ptr->execute(e, fds);
|
||||
} catch (execution_of_input_terminated &ex) {
|
||||
control_c = 0;
|
||||
fprintf(stderr, "### %s\n", ex.what());
|
||||
if (e.exit())
|
||||
exit(ex.status());
|
||||
e.status(ex.status(), false);
|
||||
while ((c = getopt_long(argc, argv, "d:ef:i:prstuvwy", longopts, nullptr)) != -1) {
|
||||
std::string flag = "-"; flag.push_back(c);
|
||||
switch(c) {
|
||||
default:
|
||||
make_help();
|
||||
return EX_USAGE;
|
||||
|
||||
case 'h':
|
||||
make_help();
|
||||
return 0;
|
||||
|
||||
case 1:
|
||||
e.set("test", 1);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
passthrough = true;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
case 'f':
|
||||
case 'i':
|
||||
args.push_back(std::move(flag));
|
||||
args.push_back(optarg);
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
case 'p':
|
||||
case 't':
|
||||
case 'u':
|
||||
case 'v':
|
||||
case 'w':
|
||||
case 'y':
|
||||
args.push_back(std::move(flag));
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
case 's':
|
||||
args.push_back(std::move(flag));
|
||||
passthrough = true;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
std::transform(argv, argv+argc, std::back_inserter(args), [](const char *cp){
|
||||
return std::string(cp);
|
||||
});
|
||||
|
||||
|
||||
|
||||
e.startup(true);
|
||||
read_file(p1, root() / "Startup");
|
||||
read_file(e, root() / "Startup");
|
||||
e.startup(false);
|
||||
|
||||
auto path = which(e, "Make");
|
||||
if (path.empty()) {
|
||||
fputs("### MPW Shell - Command \"Make\" was not found.\n", stderr);
|
||||
return -1;
|
||||
}
|
||||
e.set("command", path);
|
||||
args[0] = path;
|
||||
|
||||
return read_make(p1, p2, e, args);
|
||||
if (passthrough) {
|
||||
|
||||
launch_mpw(e, args, fdmask());
|
||||
exit(EX_OSERR);
|
||||
}
|
||||
|
||||
return read_make(e, args);
|
||||
|
||||
}
|
||||
|
||||
@@ -458,8 +513,11 @@ int main(int argc, char **argv) {
|
||||
mpw_path();
|
||||
|
||||
fs::path self = fs::path(argv[0]).filename();
|
||||
if (self == "mpw-make") return make(argc - 1, argv + 1);
|
||||
if (self == "mpw-shell" && argc > 1 && !strcmp(argv[1],"make")) return make(argc - 2, argv + 2);
|
||||
if (self == "mpw-make") return make(argc, argv);
|
||||
if (self == "mpw-shell" && argc > 1 && !strcmp(argv[1],"make")) {
|
||||
argv[1] = (char *)"mpw-make";
|
||||
return make(argc - 1, argv + 1);
|
||||
}
|
||||
|
||||
Environment e;
|
||||
init(e);
|
||||
@@ -499,59 +557,38 @@ int main(int argc, char **argv) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
phase1 p1;
|
||||
phase2 p2;
|
||||
|
||||
p1 >>= [&p2](std::string &&s) {
|
||||
if (s.empty()) p2.finish();
|
||||
else p2(std::move(s));
|
||||
};
|
||||
|
||||
p2 >>= [&e](command_ptr_vector &&v) {
|
||||
|
||||
for (auto iter = v.begin(); iter != v.end(); ++iter) {
|
||||
auto &ptr = *iter;
|
||||
fdmask fds;
|
||||
try {
|
||||
ptr->execute(e, fds);
|
||||
} catch (execution_of_input_terminated &ex) {
|
||||
control_c = 0;
|
||||
if (!(ptr->terminal() && ++iter == v.end())) {
|
||||
fprintf(stderr, "### %s\n", ex.what());
|
||||
}
|
||||
e.status(ex.status(), false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!cflag) fprintf(stdout, "MPW Shell " VERSION "\n");
|
||||
if (!fflag) {
|
||||
fs::path startup = root() / "Startup";
|
||||
e.startup(true);
|
||||
mpw_parser p(e);
|
||||
|
||||
try {
|
||||
read_file(p1, startup);
|
||||
read_file(e, startup);
|
||||
} catch (const std::system_error &ex) {
|
||||
fprintf(stderr, "### %s: %s\n", startup.c_str(), ex.what());
|
||||
} catch (const quit_command_t &) {
|
||||
}
|
||||
|
||||
e.startup(false);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
int rv = 0;
|
||||
if (cflag) {
|
||||
std::string s(cflag);
|
||||
s.push_back('\n');
|
||||
p1.process(s, true);
|
||||
p2.finish();
|
||||
exit(e.status());
|
||||
rv = read_string(e, cflag);
|
||||
exit(rv);
|
||||
}
|
||||
|
||||
if (isatty(STDIN_FILENO))
|
||||
interactive(e, p1, p2);
|
||||
rv = interactive(e);
|
||||
else
|
||||
read_fd(p1, STDIN_FILENO);
|
||||
p2.finish();
|
||||
rv = read_fd(e, STDIN_FILENO);
|
||||
|
||||
exit(e.status());
|
||||
exit(rv);
|
||||
}
|
||||
catch (const quit_command_t &) {
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include "environment.h"
|
||||
#include "fdset.h"
|
||||
|
||||
const unsigned char escape = 0xb6;
|
||||
|
||||
@@ -49,7 +50,7 @@ public:
|
||||
|
||||
|
||||
std::vector<token> tokenize(std::string &s, bool eval = false);
|
||||
std::string expand_vars(const std::string &s, const class Environment &);
|
||||
std::string expand_vars(const std::string &s, class Environment &, const fdmask &fds = fdmask());
|
||||
|
||||
//std::string quote(std::string &&s);
|
||||
std::string quote(const std::string &s);
|
||||
@@ -67,4 +68,9 @@ int32_t evaluate_expression(const std::string &name, std::vector<token> &&tokens
|
||||
|
||||
|
||||
|
||||
int read_file(Environment &e, const std::string &file, const fdmask &fds = fdmask());
|
||||
int read_string(Environment &e, const std::string &s, const fdmask &fds = fdmask());
|
||||
int read_fd(Environment &e, int fd, const fdmask &fds = fdmask());
|
||||
|
||||
|
||||
#endif
|
||||
|
106
mpw_parser.cpp
Normal file
106
mpw_parser.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "mpw_parser.h"
|
||||
#include "phase3_parser.h"
|
||||
|
||||
#include "command.h"
|
||||
#include "error.h"
|
||||
|
||||
mpw_parser::mpw_parser(Environment &e, fdmask fds, bool interactive) : _env(e), _fds(fds), _interactive(interactive)
|
||||
{
|
||||
|
||||
_p3 = phase3::make();
|
||||
_p2.set_next([this](int type, std::string &&s){
|
||||
_p3->parse(type, std::move(s));
|
||||
});
|
||||
|
||||
_p1.set_next([this](std::string &&s){
|
||||
_p2.parse(std::move(s));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
mpw_parser::~mpw_parser() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool mpw_parser::continuation() const {
|
||||
if (_p1.continuation()) return true;
|
||||
if (_p2.continuation()) return true;
|
||||
if (_p3->continuation()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void mpw_parser::finish() {
|
||||
_p1.finish();
|
||||
_p2.finish();
|
||||
_p3->parse(0, "");
|
||||
|
||||
// and now execute the commands...
|
||||
execute();
|
||||
}
|
||||
|
||||
|
||||
void mpw_parser::reset() {
|
||||
_p1.reset();
|
||||
_p2.reset();
|
||||
_p3->reset();
|
||||
_abort = false;
|
||||
}
|
||||
|
||||
void mpw_parser::abort() {
|
||||
_p1.abort();
|
||||
_p2.abort();
|
||||
//_p3->abort();
|
||||
_p3->reset();
|
||||
_abort = true;
|
||||
}
|
||||
|
||||
|
||||
void mpw_parser::parse(const void *begin, const void *end) {
|
||||
if (_abort) return;
|
||||
_p1.parse((const unsigned char *)begin, (const unsigned char *)end);
|
||||
|
||||
// and execute...
|
||||
execute();
|
||||
}
|
||||
|
||||
|
||||
void mpw_parser::execute() {
|
||||
if (_abort) {
|
||||
_p3->command_queue.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
auto commands = std::move(_p3->command_queue);
|
||||
_p3->command_queue.clear();
|
||||
|
||||
|
||||
std::reverse(commands.begin(), commands.end());
|
||||
|
||||
command_ptr cmd;
|
||||
|
||||
|
||||
try {
|
||||
while (!commands.empty()) {
|
||||
cmd = std::move(commands.back());
|
||||
commands.pop_back();
|
||||
cmd->execute(_env, _fds);
|
||||
}
|
||||
|
||||
} catch (execution_of_input_terminated &ex) {
|
||||
|
||||
_env.status(ex.status(), false);
|
||||
|
||||
if (_interactive) {
|
||||
if (!cmd->terminal() || !commands.empty()) {
|
||||
if (ex.status()) fprintf(stderr, "### %s\n", ex.what());
|
||||
}
|
||||
return;
|
||||
}
|
||||
_abort = true;
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
59
mpw_parser.h
Normal file
59
mpw_parser.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef __mpw_parser__
|
||||
#define __mpw_parser__
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include "environment.h"
|
||||
#include "fdset.h"
|
||||
|
||||
#include "phase1.h"
|
||||
#include "phase2.h"
|
||||
|
||||
class mpw_parser {
|
||||
|
||||
public:
|
||||
|
||||
mpw_parser(Environment &e, fdmask fds = fdmask(), bool interactive = false);
|
||||
mpw_parser(Environment &e, bool interactive) : mpw_parser(e, fdmask(), interactive)
|
||||
{}
|
||||
|
||||
~mpw_parser();
|
||||
|
||||
void parse(const void *begin, const void *end);
|
||||
void parse(const std::string &s) {
|
||||
parse(s.data(), s.data() + s.size());
|
||||
}
|
||||
void parse(const void *begin, size_t length) {
|
||||
parse(begin, (const char *)begin + length);
|
||||
}
|
||||
|
||||
void reset();
|
||||
void abort();
|
||||
void finish();
|
||||
|
||||
bool continuation() const;
|
||||
|
||||
private:
|
||||
|
||||
mpw_parser& operator=(const mpw_parser &) = delete;
|
||||
mpw_parser& operator=(mpw_parser &&) = delete;
|
||||
|
||||
mpw_parser(const mpw_parser &) = delete;
|
||||
mpw_parser(mpw_parser &&) = delete;
|
||||
|
||||
void execute();
|
||||
|
||||
Environment &_env;
|
||||
fdmask _fds;
|
||||
bool _interactive = false;
|
||||
bool _abort = false;
|
||||
|
||||
phase1 _p1;
|
||||
phase2 _p2;
|
||||
std::unique_ptr<class phase3> _p3;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
14
phase1.cpp
14
phase1.cpp
@@ -150,19 +150,10 @@ text:
|
||||
}
|
||||
|
||||
|
||||
void phase1::process(const std::string &s, bool final) {
|
||||
|
||||
for (auto c : s) {
|
||||
cs = process(c, cs);
|
||||
}
|
||||
if (final) finish();
|
||||
}
|
||||
|
||||
void phase1::process(const unsigned char *begin, const unsigned char *end, bool final) {
|
||||
void phase1::parse(const unsigned char *begin, const unsigned char *end) {
|
||||
while (begin != end) {
|
||||
cs = process(*begin++, cs);
|
||||
}
|
||||
if (final) finish();
|
||||
}
|
||||
|
||||
void phase1::finish() {
|
||||
@@ -181,7 +172,6 @@ void phase1::reset() {
|
||||
void phase1::flush() {
|
||||
multiline = false;
|
||||
if (scratch.empty()) return;
|
||||
// strip trailing whitespace?
|
||||
if (pipe_to) pipe_to(std::move(scratch));
|
||||
if (_then) _then(std::move(scratch));
|
||||
scratch.clear();
|
||||
}
|
||||
|
32
phase1.h
32
phase1.h
@@ -8,47 +8,33 @@
|
||||
class phase1 {
|
||||
|
||||
public:
|
||||
typedef std::function<void(std::string &&)> pipe_function;
|
||||
|
||||
typedef std::function<void(std::string &&)> next_function_type;
|
||||
|
||||
phase1() = default;
|
||||
|
||||
void process(const unsigned char *begin, const unsigned char *end, bool final = false);
|
||||
|
||||
void process(const char *begin, const char *end, bool final = false) {
|
||||
process((const unsigned char *)begin, (const unsigned char *)end, final);
|
||||
}
|
||||
|
||||
void process(const std::string &s, bool final = false);// { process(s.data(), s.data() + s.size(), final); }
|
||||
|
||||
void finish();// { const char *tmp = "\n"; process(tmp, tmp+1, true); }
|
||||
void parse(const unsigned char *begin, const unsigned char *end);
|
||||
void parse(const std::string &s) { parse((const unsigned char *)s.data(), (const unsigned char *)s.data() + s.size()); }
|
||||
void finish();
|
||||
|
||||
void reset();
|
||||
void abort() { reset(); }
|
||||
|
||||
|
||||
phase1 &operator >>= (pipe_function f) { pipe_to = f; return *this; }
|
||||
|
||||
|
||||
template<class F>
|
||||
phase1 &operator >>= (F &f) {
|
||||
using std::placeholders::_1;
|
||||
pipe_to = std::bind(&F::operator(), &f, _1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
bool continuation() const { return multiline; }
|
||||
|
||||
void set_next(next_function_type &&fx) { _then = std::move(fx); }
|
||||
|
||||
private:
|
||||
|
||||
int process(unsigned char, int);
|
||||
void flush();
|
||||
|
||||
std::string scratch;
|
||||
pipe_function pipe_to;
|
||||
int line = 1;
|
||||
int cs = 0;
|
||||
bool multiline = false;
|
||||
|
||||
next_function_type _then;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
72
phase2.h
72
phase2.h
@@ -3,95 +3,41 @@
|
||||
#define __phase2_h__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "command.h"
|
||||
#include <lemon_base.h>
|
||||
|
||||
//typedef std::unique_ptr<struct command> command_ptr;
|
||||
//typedef std::vector<command_ptr> command_ptr_vector;
|
||||
|
||||
class phase2_parser : public lemon_base<std::string> {
|
||||
|
||||
public:
|
||||
static std::unique_ptr<phase2_parser> make();
|
||||
|
||||
virtual void syntax_error(int yymajor, std::string &yyminor) override final;
|
||||
virtual void parse_accept() override final;
|
||||
virtual void parse_failure() override final;
|
||||
|
||||
bool continuation() const;
|
||||
|
||||
private:
|
||||
friend class phase2;
|
||||
|
||||
phase2_parser(const phase2_parser &) = delete;
|
||||
phase2_parser(phase2_parser &&) = delete;
|
||||
|
||||
phase2_parser& operator=(const phase2_parser &) = delete;
|
||||
phase2_parser& operator=(phase2_parser &&) = delete;
|
||||
|
||||
protected:
|
||||
// these need to be accessible to the lemon-generated parser.
|
||||
phase2_parser() = default;
|
||||
|
||||
command_ptr_vector command_queue;
|
||||
bool error = false;
|
||||
};
|
||||
|
||||
|
||||
class phase2 {
|
||||
|
||||
public:
|
||||
typedef std::function<void(command_ptr_vector &&)> pipe_function;
|
||||
|
||||
phase2();
|
||||
phase2(const phase2 &) = delete;
|
||||
phase2(phase2 &&) = default;
|
||||
typedef std::function<void(int, std::string &&)> next_function_type;
|
||||
|
||||
phase2 & operator=(const phase2 &) = delete;
|
||||
phase2 & operator=(phase2 &&) = default;
|
||||
phase2() = default;
|
||||
|
||||
void operator()(const std::string &line) { process(line); }
|
||||
void process(const std::string &line);
|
||||
void parse(std::string &&);
|
||||
void finish();
|
||||
|
||||
phase2 &operator >>=(pipe_function f) { pipe_to = f; return *this; }
|
||||
void reset();
|
||||
void abort() { reset(); }
|
||||
|
||||
template<class F>
|
||||
phase2 &operator >>= (F &f) {
|
||||
using std::placeholders::_1;
|
||||
pipe_to = std::bind(&F::operator(), &f, _1);
|
||||
return *this;
|
||||
}
|
||||
bool continuation() const { return false; }
|
||||
|
||||
bool continuation() const {
|
||||
return parser ? parser->continuation() : false;
|
||||
}
|
||||
|
||||
void abort();
|
||||
void set_next(next_function_type &&fx) { _then = std::move(fx); }
|
||||
|
||||
private:
|
||||
|
||||
void parse(int, std::string &&);
|
||||
|
||||
|
||||
std::unique_ptr<phase2_parser> parser;
|
||||
void parse(int type, std::string &&s);
|
||||
|
||||
std::string scratch;
|
||||
int type = 0;
|
||||
int pcount = 0;
|
||||
|
||||
pipe_function pipe_to;
|
||||
|
||||
void flush();
|
||||
bool special();
|
||||
int classify();
|
||||
void exec();
|
||||
|
||||
|
||||
next_function_type _then;
|
||||
};
|
||||
|
||||
|
||||
|
99
phase2.rl
99
phase2.rl
@@ -4,9 +4,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "phase2-parser.h"
|
||||
#include "phase2.h"
|
||||
#include "command.h"
|
||||
#include "phase3.h"
|
||||
|
||||
%%{
|
||||
machine main;
|
||||
@@ -194,6 +193,7 @@ int phase2::classify() {
|
||||
_("else", ELSE)
|
||||
_("end", END)
|
||||
_("evaluate", EVALUATE)
|
||||
_("exit", EXIT)
|
||||
_("for", FOR)
|
||||
_("if", IF)
|
||||
_("loop", LOOP)
|
||||
@@ -217,9 +217,7 @@ void phase2::flush() {
|
||||
while (!scratch.empty() && isspace(scratch.back())) scratch.pop_back();
|
||||
|
||||
|
||||
if (!scratch.empty()) {
|
||||
parse(classify(), std::move(scratch));
|
||||
}
|
||||
if (!scratch.empty()) parse(classify(), std::move(scratch));
|
||||
|
||||
type = 0;
|
||||
pcount = 0;
|
||||
@@ -236,20 +234,21 @@ bool phase2::special() {
|
||||
case EVALUATE:
|
||||
case BREAK:
|
||||
case CONTINUE:
|
||||
case EXIT:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void phase2::process(const std::string &line) {
|
||||
void phase2::parse(int type, std::string &&s) {
|
||||
if (_then) _then(type, std::move(s));
|
||||
}
|
||||
|
||||
void phase2::parse(std::string &&line) {
|
||||
|
||||
//fprintf(stderr, "-> %s\n", line.c_str());
|
||||
|
||||
// still needed?
|
||||
if (line.empty()) { finish(); return; }
|
||||
|
||||
|
||||
int cs;
|
||||
const unsigned char *p = (const unsigned char *)line.data();
|
||||
const unsigned char *pe = p + line.size();
|
||||
@@ -264,81 +263,19 @@ void phase2::process(const std::string &line) {
|
||||
%% write exec;
|
||||
|
||||
flush();
|
||||
// 2 NLs to make the stack reduce. harmless if in a multi-line constuct.
|
||||
parse(NL, "");
|
||||
parse(NL, "");
|
||||
|
||||
exec();
|
||||
if (_then) {
|
||||
_then(NL, "");
|
||||
_then(NL, "");
|
||||
}
|
||||
}
|
||||
|
||||
void phase2::finish() {
|
||||
parse(0, "");
|
||||
exec();
|
||||
}
|
||||
|
||||
void phase2::parse(int token, std::string &&s) {
|
||||
if (parser) parser->parse(token, std::move(s));
|
||||
}
|
||||
|
||||
void phase2::exec() {
|
||||
|
||||
if (pipe_to && parser) {
|
||||
command_ptr_vector tmp;
|
||||
for (auto &p : parser->command_queue) {
|
||||
if (p) tmp.emplace_back(std::move(p));
|
||||
}
|
||||
parser->command_queue.clear();
|
||||
|
||||
if (!tmp.empty()) pipe_to(std::move(tmp));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
phase2::phase2() {
|
||||
parser = phase2_parser::make();
|
||||
//parser->trace(stdout, " ] ");
|
||||
|
||||
}
|
||||
|
||||
void phase2::abort() {
|
||||
parser = nullptr;
|
||||
parser = phase2_parser::make();
|
||||
}
|
||||
|
||||
#pragma mark - phase2_parser
|
||||
|
||||
void phase2_parser::parse_accept() {
|
||||
error = false;
|
||||
}
|
||||
|
||||
void phase2_parser::parse_failure() {
|
||||
error = false;
|
||||
}
|
||||
|
||||
void phase2_parser::syntax_error(int yymajor, std::string &yyminor) {
|
||||
/*
|
||||
switch (yymajor) {
|
||||
case END:
|
||||
fprintf(stderr, "### MPW Shell - Extra END command.\n");
|
||||
break;
|
||||
|
||||
case RPAREN:
|
||||
fprintf(stderr, "### MPW Shell - Extra ) command.\n");
|
||||
break;
|
||||
|
||||
case ELSE:
|
||||
case ELSE_IF:
|
||||
fprintf(stderr, "### MPW Shell - ELSE must be within IF ... END.\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "### Parse error near %s\n", yyminor.c_str());
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
fprintf(stderr, "### MPW Shell - Parse error near %s\n", yymajor ? yyminor.c_str() : "EOF");
|
||||
error = true;
|
||||
void phase2::reset() {
|
||||
|
||||
type = 0;
|
||||
pcount = 0;
|
||||
scratch.clear();
|
||||
}
|
||||
|
||||
|
@@ -6,19 +6,19 @@
|
||||
|
||||
%include {
|
||||
|
||||
#include "phase2.h"
|
||||
#include "phase3_parser.h"
|
||||
#include "command.h"
|
||||
#define LEMON_SUPER phase2_parser
|
||||
#include "phase2-parser.h"
|
||||
#define LEMON_SUPER phase3
|
||||
}
|
||||
|
||||
|
||||
%code {
|
||||
|
||||
std::unique_ptr<phase2_parser> phase2_parser::make() {
|
||||
std::unique_ptr<phase3> phase3::make() {
|
||||
return std::make_unique<yypParser>();
|
||||
}
|
||||
|
||||
bool phase2_parser::continuation() const {
|
||||
bool phase3::continuation() const {
|
||||
yypParser *self = (yypParser *)this;
|
||||
|
||||
for (const auto &e : *self) {
|
||||
@@ -36,6 +36,43 @@ bool phase2_parser::continuation() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void phase3::parse_accept() {
|
||||
error = false;
|
||||
}
|
||||
|
||||
void phase3::parse_failure() {
|
||||
error = false;
|
||||
}
|
||||
|
||||
void phase3::syntax_error(int yymajor, std::string &yyminor) {
|
||||
/*
|
||||
switch (yymajor) {
|
||||
case END:
|
||||
fprintf(stderr, "### MPW Shell - Extra END command.\n");
|
||||
break;
|
||||
|
||||
case RPAREN:
|
||||
fprintf(stderr, "### MPW Shell - Extra ) command.\n");
|
||||
break;
|
||||
|
||||
case ELSE:
|
||||
case ELSE_IF:
|
||||
fprintf(stderr, "### MPW Shell - ELSE must be within IF ... END.\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "### Parse error near %s\n", yyminor.c_str());
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
fprintf(stderr, "### MPW Shell - Parse error near %s\n", yymajor ? yyminor.c_str() : "EOF");
|
||||
error = true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
%left PIPE_PIPE AMP_AMP.
|
||||
@@ -102,6 +139,7 @@ term(RV) ::= COMMAND(C). { RV = std::make_unique<simple_command>(std::move
|
||||
term(RV) ::= EVALUATE(C). { RV = std::make_unique<evaluate_command>(std::move(C)); }
|
||||
term(RV) ::= BREAK(C). { RV = std::make_unique<break_command>(std::move(C)); }
|
||||
term(RV) ::= CONTINUE(C). { RV = std::make_unique<continue_command>(std::move(C)); }
|
||||
term(RV) ::= EXIT(C). { RV = std::make_unique<exit_command>(std::move(C)); }
|
||||
term(C) ::= if_command(C).
|
||||
term(C) ::= begin_command(C).
|
||||
term(C) ::= paren_command(C).
|
43
phase3_parser.h
Normal file
43
phase3_parser.h
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
#ifndef __phase3_h__
|
||||
#define __phase3_h__
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "command.h"
|
||||
#include <lemon_base.h>
|
||||
|
||||
class phase3 : public lemon_base<std::string> {
|
||||
|
||||
public:
|
||||
static std::unique_ptr<phase3> make();
|
||||
|
||||
virtual void syntax_error(int yymajor, std::string &yyminor) override final;
|
||||
virtual void parse_accept() override final;
|
||||
virtual void parse_failure() override final;
|
||||
|
||||
bool continuation() const;
|
||||
|
||||
//void parse(int type, std::string &&s) override final;
|
||||
|
||||
private:
|
||||
|
||||
phase3(const phase3 &) = delete;
|
||||
phase3(phase3 &&) = delete;
|
||||
|
||||
phase3& operator=(const phase3 &) = delete;
|
||||
phase3& operator=(phase3 &&) = delete;
|
||||
|
||||
protected:
|
||||
// these need to be accessible to the lemon-generated parser.
|
||||
phase3() = default;
|
||||
|
||||
command_ptr_vector command_queue;
|
||||
bool error = false;
|
||||
|
||||
friend class mpw_parser;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user