#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 = NULL, bool expand = false) {
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) {
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()) {
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 {
else {
CYDriver driver(script ?: "<stdin>");
cy::parser parser(driver);
+ Setup(driver, parser);
char *start, *end;
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);
+ if (compile)
+ std::cout << code;
+ else
+ Run(socket, code);
}
}