#include <netinet/in.h>
#include <sys/un.h>
+static volatile enum {
+ Working,
+ Parsing,
+ Running,
+ Sending,
+ Waiting,
+} mode_;
+
static jmp_buf ctrlc_;
static void sigint(int) {
- longjmp(ctrlc_, 1);
+ switch (mode_) {
+ case Working:
+ return;
+ case Parsing:
+ longjmp(ctrlc_, 1);
+ case Running:
+ throw "*** Ctrl-C";
+ case Sending:
+ return;
+ case Waiting:
+ return;
+ }
+}
+
+#if YYDEBUG
+static bool bison_;
+#endif
+static bool strict_;
+
+void Setup(CYDriver &driver, cy::parser &parser) {
+#if YYDEBUG
+ if (bison_)
+ parser.set_debug_level(1);
+#endif
+ if (strict_)
+ driver.strict_ = true;
}
-void Run(int socket, const char *data, size_t size, FILE *fout, bool expand = false) {
+void Run(int socket, const char *data, size_t size, FILE *fout = NULL, bool expand = false) {
CYPool pool;
const char *json;
if (socket == -1) {
+ mode_ = Running;
json = CYExecute(pool, data);
+ mode_ = Working;
if (json != NULL)
size = strlen(json);
} else {
+ mode_ = Sending;
CYSendAll(socket, &size, sizeof(size));
CYSendAll(socket, data, size);
+ mode_ = Waiting;
CYRecvAll(socket, &size, sizeof(size));
if (size == _not(size_t))
json = NULL;
temp[size] = '\0';
json = temp;
}
+ mode_ = Working;
}
if (json != NULL && fout != NULL) {
}
}
-void Run(int socket, std::string &code, FILE *fout, bool expand = false) {
+void Run(int socket, std::string &code, FILE *fout = NULL, bool expand = false) {
Run(socket, code.c_str(), code.size(), fout, expand);
}
const char *prompt("cy# ");
if (setjmp(ctrlc_) != 0) {
+ mode_ = Working;
fputs("\n", fout);
fflush(fout);
goto restart;
}
read:
+ mode_ = Parsing;
char *line(readline(prompt));
+ mode_ = Working;
if (line == NULL)
break;
}
}
- lines.push_back(line);
command += line;
+
+ char *begin(line), *end(line + strlen(line));
+ while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) {
+ *nl = '\0';
+ lines.push_back(begin);
+ begin = nl + 1;
+ }
+
+ lines.push_back(begin);
+
free(line);
std::string code;
else {
CYDriver driver("");
cy::parser parser(driver);
+ Setup(driver, parser);
driver.data_ = command.c_str();
driver.size_ = command.size();
if (parser.parse() != 0 || !driver.errors_.empty()) {
for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) {
cy::position begin(error->location_.begin);
- if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
+ if (begin.line != lines.size() || begin.column - 1 != lines.back().size() || error->warning_) {
cy::position end(error->location_.end);
+
if (begin.line != lines.size()) {
std::cerr << " | ";
std::cerr << lines[begin.line - 1] << std::endl;
}
+
std::cerr << " | ";
for (size_t i(0); i != begin.column - 1; ++i)
std::cerr << '.';
- if (begin.line != end.line)
+ if (begin.line != end.line || begin.column == end.column)
std::cerr << '^';
else for (size_t i(0), e(end.column - begin.column); i != e; ++i)
std::cerr << '^';
std::cerr << std::endl;
+
std::cerr << " | ";
std::cerr << error->message_ << std::endl;
+
add_history(command.c_str());
goto restart;
}
goto read;
}
- if (driver.source_ == NULL)
+ if (driver.program_ == NULL)
goto restart;
if (socket != -1)
code = command;
else {
std::ostringstream str;
- driver.source_->Show(str);
+ CYOutput out(str);
+ driver.program_->Multiple(out);
code = str.str();
}
}
}
int main(int argc, char *argv[]) {
+ bool tty(isatty(STDIN_FILENO));
pid_t pid(_not(pid_t));
+ bool compile(false);
- for (;;) switch (getopt(argc, argv, "p:")) {
+ for (;;) switch (getopt(argc, argv, "cg:p:s")) {
case -1:
goto getopt;
case '?':
- fprintf(stderr, "usage: cycript [-p <pid>] [<script> [<arg>...]]\n");
+ fprintf(stderr, "usage: cycript [-c] [-p <pid>] [<script> [<arg>...]]\n");
return 1;
+ case 'c':
+ compile = true;
+ break;
+
+ case 'g':
+ if (false);
+#if YYDEBUG
+ else if (strcmp(optarg, "bison") == 0)
+ bison_ = true;
+#endif
+ else {
+ fprintf(stderr, "invalid name for -g\n");
+ return 1;
+ }
+ break;
+
case 'p': {
size_t size(strlen(optarg));
char *end;
return 1;
}
} break;
+
+ case 's':
+ strict_ = true;
+ break;
} getopt:;
const char *script;
- if (optind < argc - 1 && pid != _not(pid_t)) {
+ if (pid != _not(pid_t) && optind < argc - 1) {
fprintf(stderr, "-p cannot set argv\n");
return 1;
}
+ if (pid != _not(pid_t) && compile) {
+ fprintf(stderr, "-p conflicts with -c\n");
+ return 1;
+ }
+
if (optind == argc)
script = NULL;
else {
script = NULL;
}
+ if (script == NULL && !tty && pid != _not(pid_t)) {
+ fprintf(stderr, "non-terminal attaching to remove console\n");
+ return 1;
+ }
+
int socket;
if (pid == _not(pid_t))
_syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
}
- if (script == NULL)
+ if (script == NULL && tty)
Console(socket);
else {
- CYDriver driver(script);
+ CYDriver driver(script ?: "<stdin>");
cy::parser parser(driver);
+ Setup(driver, parser);
- size_t size;
- char *start(reinterpret_cast<char *>(Map(script, &size)));
- char *end(start + size);
+ char *start, *end;
- if (size >= 2 && start[0] == '#' && start[1] == '!') {
- start += 2;
+ if (script == NULL) {
+ start = NULL;
+ end = NULL;
- if (void *line = memchr(start, '\n', end - start))
- start = reinterpret_cast<char *>(line);
- else
- start = end;
- }
+ driver.file_ = stdin;
+ } else {
+ size_t size;
+ start = reinterpret_cast<char *>(Map(script, &size));
+ end = start + size;
+
+ if (size >= 2 && start[0] == '#' && start[1] == '!') {
+ start += 2;
- driver.data_ = start;
- driver.size_ = end - start;
+ if (void *line = memchr(start, '\n', end - start))
+ start = reinterpret_cast<char *>(line);
+ else
+ start = end;
+ }
+
+ driver.data_ = start;
+ driver.size_ = end - start;
+ }
if (parser.parse() != 0 || !driver.errors_.empty()) {
for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
- } else if (driver.source_ != NULL)
+ } else if (driver.program_ != NULL)
if (socket != -1)
Run(socket, start, end - start, stdout);
else {
std::ostringstream str;
- driver.source_->Show(str);
+ CYOutput out(str);
+ driver.program_->Multiple(out);
std::string code(str.str());
- Run(socket, code, stdout);
+ if (compile)
+ std::cout << code;
+ else
+ Run(socket, code);
}
}