X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/3b52fd1a20c639d7a716d3e0f3176a55f2fcba72..aabea98c2f0415ea5882dad2b9ff9fc3b1d5c97c:/Console.cpp diff --git a/Console.cpp b/Console.cpp index 9a60679..ef3de63 100644 --- a/Console.cpp +++ b/Console.cpp @@ -37,9 +37,6 @@ */ /* }}} */ -#define _GNU_SOURCE - -#include #include "cycript.hpp" #include @@ -65,6 +62,9 @@ #include #include #include +#include + +#include static volatile enum { Working, @@ -119,7 +119,11 @@ void Run(int socket, const char *data, size_t size, FILE *fout = NULL, bool expa const char *json; if (socket == -1) { mode_ = Running; +#ifdef CY_EXECUTE json = CYExecute(pool, data); +#else + json = NULL; +#endif mode_ = Working; if (json != NULL) size = strlen(json); @@ -170,7 +174,20 @@ void Run(int socket, std::string &code, FILE *fout = NULL, bool expand = false) Run(socket, code.c_str(), code.size(), fout, expand); } -static void Console(int socket) { +static void Console(apr_pool_t *pool, int socket) { + passwd *passwd; + if (const char *username = getenv("LOGNAME")) + passwd = getpwnam(username); + else + passwd = getpwuid(getuid()); + + const char *basedir(apr_psprintf(pool, "%s/.cycript", passwd->pw_dir)); + const char *histfile(apr_psprintf(pool, "%s/history", basedir)); + size_t histlines(0); + + mkdir(basedir, 0700); + read_history(histfile); + bool bypass(false); bool debug(false); bool expand(false); @@ -224,6 +241,7 @@ static void Console(int socket) { fflush(fout); } add_history(line); + ++histlines; goto restart; } } @@ -277,6 +295,7 @@ static void Console(int socket) { std::cerr << error->message_ << std::endl; add_history(command.c_str()); + ++histlines; goto restart; } } @@ -303,6 +322,7 @@ static void Console(int socket) { } add_history(command.c_str()); + ++histlines; if (debug) std::cout << code << std::endl; @@ -310,6 +330,9 @@ static void Console(int socket) { Run(socket, code, fout, expand); } + _syscall(close(_syscall(open(histfile, O_CREAT | O_WRONLY, 0600)))); + append_history(histlines, histfile); + fputs("\n", fout); fflush(fout); } @@ -331,62 +354,140 @@ static void *Map(const char *path, size_t *psize) { return base; } -int main(int argc, char *argv[]) { +int main(int argc, char const * const argv[], char const * const envp[]) { + _aprcall(apr_app_initialize(&argc, &argv, &envp)); + bool tty(isatty(STDIN_FILENO)); - pid_t pid(_not(pid_t)); bool compile(false); - for (;;) switch (getopt(argc, argv, "cg:n:p:s")) { - case -1: - goto getopt; - case '?': - fprintf(stderr, "usage: cycript [-c] [-p ] [