Ejemplo n.º 1
0
// The read-eval-execute loop of the shell.
void RunShell(v8::Handle<v8::Context> context) {
	printf("djondb shell version %s\n", VERSION);
	printf("Welcome to djondb shell.\n");
	printf("Use help(); to get the commands available. \n(hint: The first command should be \"connect\" to playing with a server)\n");

	static const int kBufferSize = 256;
	// Enter the execution environment before evaluating any code.
	v8::Context::Scope context_scope(context);
	v8::Local<v8::String> name(v8::String::New("(shell)"));

#ifndef WINDOWS
	char* file = getHistoryFilename();
	linenoiseHistoryLoad(file);
	free(file);
	linenoiseSetCompletionCallback(completion);
#endif
	std::stringstream ss;
	bool line = false;
	const char* lastCmd = "";
	while (true) {
		char buffer[kBufferSize];
		char* prompt;
		if (!line) {
			prompt = "> ";
		} else {
			prompt = ". ";
		}
		line = false;
		std::string str = readLine(prompt);
		if (str.length() > 0) {
			//char* str = fgets(buffer, kBufferSize, stdin);
			if (str[0] == '~') {
				system("vi .tmp.js");
				lastCmd = getFile(".tmp.js");
				printf("Buffer loaded.\n");
				continue;
			} else if (str[0] == '.') {
				if ((lastCmd == NULL) || (strlen(lastCmd) == 0)) {
					lastCmd = getFile(".tmp.js");
					printf("Buffer loaded.\n");
				}
				str = const_cast<char*>(lastCmd);
			} else 
				if ((str.length() >= 2) && (str[str.length() - 2] == '\\')) {
					str[str.length() - 2] = ' '; 
					line = true;
				}
			ss << str;
			if (!line) {
				v8::HandleScope handle_scope;
				std::string sCmd = ss.str();
				const char* cmd = sCmd.c_str();
				ss.str("");
				lastCmd = cmd;
				_commands.push_back(std::string(cmd));
				if ((startsWith(cmd, "exit") == 0) || (startsWith(cmd, "quit") == 0)) {
					if (strlen(cmd) < 5)
						cmd = "quit();";
				}
				ExecuteString(v8::String::New(cmd), name, true, true);
			}
		}
	}
	printf("\n");
}