2016-01-27 15:43:34 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-01-30 17:45:50 +00:00
|
|
|
#include <cerrno>
|
2016-01-27 15:43:34 +00:00
|
|
|
|
|
|
|
#include "mpw-shell.h"
|
2016-02-02 01:38:29 +00:00
|
|
|
#include "fdset.h"
|
2016-01-27 15:43:34 +00:00
|
|
|
|
2016-01-30 17:45:50 +00:00
|
|
|
#include "phase1.h"
|
|
|
|
#include "phase2.h"
|
|
|
|
#include "command.h"
|
2016-01-27 15:43:34 +00:00
|
|
|
|
2016-02-02 03:32:21 +00:00
|
|
|
#include "mapped_file.h"
|
|
|
|
|
2016-02-02 02:17:46 +00:00
|
|
|
//#include <histedit.h>
|
|
|
|
#include <editline/readline.h>
|
2016-01-27 15:43:34 +00:00
|
|
|
|
|
|
|
// should set {MPW}, {MPWVersion}, then execute {MPW}StartUp
|
2016-02-02 01:38:29 +00:00
|
|
|
void init(Environment &e) {
|
|
|
|
e.set("status", std::string("0"));
|
|
|
|
e.set("exit", std::string("1")); // terminate script on error.
|
|
|
|
e.set("echo", std::string("1"));
|
2016-01-27 15:43:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 03:32:21 +00:00
|
|
|
int read_file(phase1 &p, const std::string &file) {
|
|
|
|
mapped_file mf;
|
|
|
|
|
|
|
|
mf.open(file, mapped_file::readonly);
|
|
|
|
p.process(mf.const_begin(), mf.const_end(), true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-30 17:45:50 +00:00
|
|
|
int read_stdin(phase1 &p) {
|
|
|
|
|
|
|
|
unsigned char buffer[2048];
|
|
|
|
ssize_t size;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size = read(STDIN_FILENO, buffer, sizeof(buffer));
|
|
|
|
if (size == 0) break;
|
|
|
|
if (size < 0) {
|
|
|
|
if (errno == EINTR) continue;
|
|
|
|
perror("read: ");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
p.process(buffer, buffer + size);
|
|
|
|
} catch(std::exception &ex) {
|
|
|
|
fprintf(stderr, "%s\n", ex.what());
|
|
|
|
p.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
p.finish();
|
|
|
|
} catch(std::exception &ex) {
|
|
|
|
fprintf(stderr, "%s\n", ex.what());
|
|
|
|
p.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-02 02:17:46 +00:00
|
|
|
int interactive(phase1 &p) {
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
char *cp = readline("# ");
|
|
|
|
if (!cp) break;
|
|
|
|
|
|
|
|
std::string s(cp);
|
|
|
|
free(cp);
|
|
|
|
|
|
|
|
if (s.empty()) continue;
|
|
|
|
add_history(s.c_str());
|
|
|
|
s.push_back('\n');
|
|
|
|
try {
|
|
|
|
p.process(s);
|
|
|
|
|
|
|
|
} catch(std::exception &ex) {
|
|
|
|
fprintf(stderr, "%s\n", ex.what());
|
|
|
|
p.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
p.finish();
|
|
|
|
} catch(std::exception &ex) {
|
|
|
|
fprintf(stderr, "%s\n", ex.what());
|
|
|
|
p.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-27 15:43:34 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
2016-02-02 01:38:29 +00:00
|
|
|
Environment e;
|
|
|
|
init(e);
|
2016-01-27 15:43:34 +00:00
|
|
|
|
2016-01-30 17:45:50 +00:00
|
|
|
phase1 p1;
|
|
|
|
phase2 p2;
|
2016-01-31 05:41:30 +00:00
|
|
|
p1 >>= p2;
|
|
|
|
|
2016-02-02 01:38:29 +00:00
|
|
|
p2 >>= [&e](command_ptr &&ptr) {
|
|
|
|
fdmask fds;
|
|
|
|
ptr->execute(e, fds);
|
2016-01-31 05:41:30 +00:00
|
|
|
};
|
|
|
|
/*
|
2016-01-30 17:45:50 +00:00
|
|
|
p1 >>= [&p2](std::string &&s) {
|
|
|
|
|
|
|
|
fprintf(stdout, " -> %s\n", s.c_str());
|
|
|
|
p2.process(s);
|
|
|
|
};
|
2016-01-31 05:41:30 +00:00
|
|
|
*/
|
2016-02-02 03:32:21 +00:00
|
|
|
read_file(p1, "/Users/kelvin/mpw/Startup");
|
|
|
|
p2.finish();
|
|
|
|
|
2016-02-02 02:17:46 +00:00
|
|
|
interactive(p1);
|
2016-01-31 05:41:30 +00:00
|
|
|
p2.finish();
|
2016-01-30 17:45:50 +00:00
|
|
|
|
2016-02-02 02:17:46 +00:00
|
|
|
return 0;
|
2016-01-27 15:43:34 +00:00
|
|
|
}
|