void initedit() { editing = 1; if (!elc && histc) { elc = el_init(__progname, stdin, stdout, stderr); el_set(elc, EL_HIST, history, histc); /* use history */ el_set(elc, EL_EDITOR, "emacs"); /* default type */ el_set(elc, EL_PROMPT, cprompt); /* set the prompt * function */ el_set(elc, EL_ADDFN, "complt", "Command completion", complt_c); el_set(elc, EL_BIND, "\t", "complt", NULL); el_source(elc, NULL); /* read ~/.editrc */ el_set(elc, EL_SIGNAL, 1); } if (!eli && histi) { eli = el_init(__progname, stdin, stdout, stderr); /* again */ el_set(eli, EL_HIST, history, histi); el_set(eli, EL_EDITOR, "emacs"); el_set(eli, EL_PROMPT, iprompt); el_set(eli, EL_ADDFN, "complt", "Command completion", complt_i); el_set(eli, EL_BIND, "\t", "complt", NULL); #ifdef notyet el_set(eli, EL_ADDFN, "exit", "Exit", NULL); el_set(eli, EL_BIND, "\026", "exit", NULL); #endif el_source(eli, NULL); el_set(eli, EL_SIGNAL, 1); } }
void initialize_libedit(const char *prog) { /* Init the builtin history */ hist = history_init(); /* Remember 100 events */ #ifdef XDC_OLD_LIBEDIT history(hist, XDC_H_SETSIZE, 100); #else history(hist, &ev, XDC_H_SETSIZE, 100); #endif /* Initialize editline */ #ifdef XDC_OLD_LIBEDIT el = el_init(prog, stdin, stdout); #else el = el_init(prog, stdin, stdout, stderr); #endif el_set(el, EL_EDITOR, "emacs"); /* Default editor is emacs */ el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */ el_set(el, EL_PROMPT, get_prompt); /* Set the prompt function */ /* Tell editline to use this history interface */ el_set(el, EL_HIST, history, hist); /* * Source the user's defaults file. */ /* el_source(el, "xdebug"); */ }
/* * ntp_readline_init - setup, set or reset prompt string */ int ntp_readline_init( const char * prompt ) { int success; success = 1; if (prompt) { if (lineedit_prompt) free(lineedit_prompt); lineedit_prompt = estrdup(prompt); } #ifdef LE_EDITLINE if (NULL == ntp_el) { # if 4 == EL_INIT_ARGS ntp_el = el_init(progname, stdin, stdout, stderr); # else ntp_el = el_init(progname, stdin, stdout); # endif if (ntp_el) { el_set(ntp_el, EL_PROMPT, ntp_prompt_callback); el_set(ntp_el, EL_EDITOR, "emacs"); ntp_hist = history_init(); if (NULL == ntp_hist) { mfprintf(stderr, "history_init(): %m\n"); fflush(stderr); el_end(ntp_el); ntp_el = NULL; success = 0; } else { ZERO(hev); #ifdef H_SETSIZE history(ntp_hist, &hev, H_SETSIZE, 128); #endif el_set(ntp_el, EL_HIST, history, ntp_hist); /* use any .editrc */ el_source(ntp_el, NULL); } } else success = 0; } #endif /* LE_EDITLINE */ ntp_readline_initted = success; return success; }
void * cmdinit(struct c**t *cmds, int ncmds) { struct clitenv *env; HistEvent ev; if ((env = malloc(sizeof(*env))) == NULL) err(1, "Can't init cmd interpreter."); env->cmds = cmds; env->ncmds = ncmds; env->hist = history_init(); history(env->hist, &ev, H_SETSIZE, 100); env->el = el_init(__progname, stdin, stdout, stderr); el_set(env->el, EL_EDITOR, "emacs"); el_set(env->el, EL_PROMPT, prompt); el_set(env->el, EL_HIST, history, env->hist); el_set(env->el, EL_ADDFN, "complt", "complete", complt); el_set(env->el, EL_BIND, "\t", "complt"); el_source(env->el, NULL); /* XXX - EL_SIGNAL ? */ return env; }
static char *fetchline(void) { static EditLine *el; static History *hist; HistEvent hevent; char *line; int count; if (!el) { hist = history_init(); history(hist, &hevent, H_SETSIZE, 100); el = el_init(progname, stdin, stdout, stderr); el_source(el, NULL); el_set(el, EL_SIGNAL, 1); el_set(el, EL_PROMPT, el_get_prompt); el_set(el, EL_HIST, history, (const char *)hist); } line = strdup(el_gets(el, &count)); if (line) { if (count > 0) { line[count-1] = '\0'; } if (*line) { history(hist, &hevent, H_ENTER, line); } } return line; }
/* * Process an ioctl request. This code needs some work - it looks * pretty ugly. */ static int el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr) { int error = 0; switch (command) { case SIOCSIFFLAGS: /* * If interface is marked down and it is running, then stop it */ if (((ifp->if_flags & IFF_UP) == 0) && (ifp->if_flags & IFF_RUNNING)) { el_stop(ifp->if_softc); ifp->if_flags &= ~IFF_RUNNING; } else { /* * If interface is marked up and it is stopped, then start it */ if ((ifp->if_flags & IFF_UP) && ((ifp->if_flags & IFF_RUNNING) == 0)) el_init(ifp->if_softc); } break; default: error = ether_ioctl(ifp, command, data); break; } return (error); }
static int acc_el_initialize(void) { HistEvent ev; if (el != NULL) el_end(el); if (el_hist != NULL) history_end(el_hist); el = el_init("accedian", stdin, stdout, stderr); el_set(el, EL_PROMPT, cli_prompt); el_set(el, EL_EDITMODE, 1); el_set(el, EL_EDITOR, "emacs"); el_hist = history_init(); if (!el || !el_hist) return -1; /* setup history with 100 entries */ history(el_hist, &ev, H_SETSIZE, 100); el_set(el, EL_HIST, history, el_hist); el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete); /* Bind <tab> to command completion */ el_set(el, EL_BIND, "^I", "ed-complete", NULL); #if 0 // Ticket #8152 - This corrupts show_rc passwords containing question marks /* Bind ? to command completion */ el_set(el, EL_BIND, "?", "ed-complete", NULL); #endif /* Bind ^D to redisplay */ el_set(el, EL_BIND, "^D", "ed-redisplay", NULL); return 0; }
/* This routine resets the interface. */ static void el_reset(void *xsc) { struct el_softc *sc = xsc; dprintf(("elreset()\n")); el_stop(sc); el_init(sc); }
/* * ntp_readline_init - setup, set or reset prompt string */ int ntp_readline_init( const char * prompt ) { int success; success = 1; if (prompt) { if (lineedit_prompt) free(lineedit_prompt); lineedit_prompt = estrdup(prompt); } #ifdef LE_EDITLINE if (NULL == ntp_el) { ntp_el = el_init(progname, stdin, stdout, stderr); if (ntp_el) { el_set(ntp_el, EL_PROMPT, ntp_prompt_callback); el_set(ntp_el, EL_EDITOR, "emacs"); ntp_hist = history_init(); if (NULL == ntp_hist) { fprintf(stderr, "history_init(): %s\n", strerror(errno)); fflush(stderr); el_end(ntp_el); ntp_el = NULL; success = 0; } else { memset(&hev, 0, sizeof hev); history(ntp_hist, &hev, H_SETSIZE, 128); el_set(ntp_el, EL_HIST, history, ntp_hist); /* use any .editrc */ el_source(ntp_el, NULL); } } else success = 0; } #endif /* LE_EDITLINE */ ntp_readline_initted = success; return success; }
/* This routine resets the interface. */ void el_reset(int unit) { int s; dprintf(("elreset()\n")); s = splimp(); el_stop(unit); el_init(unit); splx(s); }
EditLineReader::EditLineReader() : _history(history_init()) , _editLine(el_init("ccons", stdin, stdout, stderr)) { reader = this; history(_history, &_event, H_SETSIZE, INT_MAX); el_set(_editLine, EL_PROMPT, ccons_prompt); el_set(_editLine, EL_EDITOR, "emacs"); el_set(_editLine, EL_HIST, history, _history); el_set(_editLine, EL_ADDFN, "ccons-ac", "autocomplete", ccons_autocomplete); el_set(_editLine, EL_BIND, "^I", "ccons-ac", NULL); }
char * readline(const char* prompt) { static EditLine *e; #ifdef H_SETSIZE HistEvent ev; #endif int count; const char *str; if(e == NULL){ #ifdef EL_INIT_FOUR e = el_init("", stdin, stdout, stderr); #else e = el_init("", stdin, stdout); #endif el_set(e, EL_PROMPT, ret_prompt); h = history_init(); #ifdef H_SETSIZE history(h, &ev, H_SETSIZE, 25); #else history(h, H_EVENT, 25); #endif el_set(e, EL_HIST, history, h); el_set(e, EL_EDITOR, "emacs"); /* XXX? */ } pr = prompt ? prompt : ""; str = el_gets(e, &count); if (str && count > 0) { char *ret = strdup (str); if (ret == NULL) return NULL; if (ret[strlen(ret) - 1] == '\n') ret[strlen(ret) - 1] = '\0'; return ret; } return NULL; }
void mtranApi_ussrSetup(int portRC, int portEvent, char* host, void (*msgHandler)(char*, char, char)) { /*Initialize Message Handler*/ mtranApi_msgHandler = msgHandler; /*Initialize command sender*/ cs_init(portRC, host); cs_sendCommand_void("setup"); //initializes simulator /*Initialize event listener*/ el_init(portEvent, host); el_installEvent("handleMessage", mtranApi_handleMessage); el_startEventListen(); }
EditLine::EditLine(std::string argv0, FILE* input, FILE* output, FILE* error) : el(el_init(argv0.c_str(), input, output, error)), mPromptCallback(0) { el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_SIGNAL, 1); el_set(el, EL_CLIENTDATA, this); el_set(el, EL_SETTY, "-d", "intr=^@", NULL); el_set(el, EL_SETTY, "-d", "eof=^@", NULL); el_set(el, EL_ADDFN, "cust-control-c", "", controlC); el_set(el, EL_BIND, "^C", "cust-control-c", NULL); el_set(el, EL_BIND, "^D", "ed-end-of-file", NULL); }
static EditLine *initEditLine() { EditLine *e; HistEvent ev; cmdHistory = history_init(); history(cmdHistory, &ev, H_SETSIZE, 100); e = el_init("ejs", stdin, stdout, stderr); el_set(e, EL_EDITOR, "vi"); el_set(e, EL_HIST, history, cmdHistory); el_source(e, NULL); return e; }
CommandPrompt::CommandPrompt() { #ifdef ZORBA_HAVE_LIBEDIT_H theEditLine = el_init("xqdb", stdin, stdout, stderr); theHistory = history_init(); HistEvent lHistoryEvent; history(theHistory, &lHistoryEvent, H_SETSIZE, 100); el_set(theEditLine, EL_PROMPT, prompt); el_set(theEditLine, EL_HIST, history, theHistory); el_set(theEditLine, EL_EDITOR, "emacs"); #endif }
int main(int argc, char *argv[]) { HistEvent he; static EditLine *el; static History *hist; bool interactive; acting_as_client = 1; peer = -1; strcpy(mode, "netascii"); signal(SIGINT, intr); interactive = isatty(STDIN_FILENO); if (interactive) { el = el_init("tftp", stdin, stdout, stderr); hist = history_init(); history(hist, &he, H_SETSIZE, 100); el_set(el, EL_HIST, history, hist); el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_PROMPT, command_prompt); el_set(el, EL_SIGNAL, 1); el_source(el, NULL); } if (argc > 1) { if (setjmp(toplevel) != 0) exit(txrx_error); if (strncmp(argv[1], "tftp://", 7) == 0) { urihandling(argv[1]); exit(txrx_error); } setpeer(argc, argv); } if (setjmp(toplevel) != 0) { if (interactive) el_reset(el); (void)putchar('\n'); } init_options(); command(interactive, el, hist, &he); }
static void _cliDebuggerInit(struct Debugger* debugger) { struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger; // TODO: get argv[0] cliDebugger->elstate = el_init(binaryName, stdin, stdout, stderr); el_set(cliDebugger->elstate, EL_PROMPT, _prompt); el_set(cliDebugger->elstate, EL_EDITOR, "emacs"); el_set(cliDebugger->elstate, EL_CLIENTDATA, cliDebugger); el_set(cliDebugger->elstate, EL_ADDFN, "tab-complete", "Tab completion", _tabComplete); el_set(cliDebugger->elstate, EL_BIND, "\t", "tab-complete", 0); cliDebugger->histate = history_init(); HistEvent ev; history(cliDebugger->histate, &ev, H_SETSIZE, 200); el_set(cliDebugger->elstate, EL_HIST, history, cliDebugger->histate); _activeDebugger = cliDebugger; signal(SIGINT, _breakIntoDefault); }
void repl::init_editline(FILE * infile, FILE * outfile) noexcept { setlocale(LC_CTYPE, ""); hist = history_init(); history(hist, &ev, H_SETSIZE, 100); el = el_init("./MoNS", infile, outfile, outfile); el_set(el, EL_EDITOR, "vi"); // el_set(el, EL_PROMPT_ESC, // +[](EditLine *el) -> const char * { // const char *prompt = "λ "; // return prompt; // }, // '\1'); el_set(el, EL_PROMPT_ESC, &prompt, '\1'); el_set(el, EL_HIST, history, hist); el_source(el, NULL); }
status_t CliContext::Init(Team* team, UserInterfaceListener* listener) { fTeam = team; fListener = listener; fTeam->AddListener(this); status_t error = fLock.InitCheck(); if (error != B_OK) return error; fBlockingSemaphore = create_sem(0, "CliContext block"); if (fBlockingSemaphore < 0) return fBlockingSemaphore; fEditLine = el_init("Debugger", stdin, stdout, stderr); if (fEditLine == NULL) return B_ERROR; fHistory = history_init(); if (fHistory == NULL) return B_ERROR; HistEvent historyEvent; history(fHistory, &historyEvent, H_SETSIZE, 100); el_set(fEditLine, EL_HIST, &history, fHistory); el_set(fEditLine, EL_EDITOR, "emacs"); el_set(fEditLine, EL_PROMPT, &_GetPrompt); fNodeManager = new(std::nothrow) ValueNodeManager(); if (fNodeManager == NULL) return B_NO_MEMORY; fNodeManager->AddListener(this); fExpressionInfo = new(std::nothrow) ExpressionInfo(); if (fExpressionInfo == NULL) return B_NO_MEMORY; fExpressionInfo->AddListener(this); return B_OK; }
int main(int argc, char **argv) { EditLine *el; History *hist; const char *buf; int num, state; check_args(argc, argv); if(connect_uri) { sql_conn = sql_connect(connect_uri); if(!sql_conn) { fprintf(stderr, "%s: [%s] %s\n", short_program_name, sql_sqlstate(NULL), sql_error(NULL)); exit(EXIT_FAILURE); } } fprintf(stderr, "%s interactive SQL shell (%s)\n\n", PACKAGE, VERSION); fprintf(stderr, "Type: \\c URI to establish a new connection\n" " \\g or ; to execute query\n" " \\G to execute the query showing results in long format\n" " \\q to end the SQL session\n" "\n" ); hist = history_init(); el = el_init(argv[0], stdin, stdout, stderr); el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_SIGNAL, 1); el_set(el, EL_PROMPT, prompt); el_set(el, EL_HIST, history, hist); el_source(el, NULL); while((buf = el_gets(el, &num)) != NULL && num != 0) { state = parse_query(buf); if(state == 0) { exec_queries(hist); } } return 0; }
IoEditLine *IoEditLine_proto(void *state) { IoMethodTable methodTable[] = { {"hasEditLib", IoEditLine_hasEditLib}, {"readLine", IoEditLine_readLine}, {"addHistory", IoEditLine_addHistory}, {NULL, NULL}, }; IoObject *self = IoObject_new(state); IoObject_tag_(self, IoEditLine_newTag(state)); /* Make sure editline returns characters in the multi-byte charset of the locale */ setlocale(LC_CTYPE, ""); IoObject_setDataPointer_(self, io_calloc(1, sizeof(IoEditLineData))); DATA(self)->prompt = IOSYMBOL(""); DATA(self)->editline = el_init("io", stdin, stdout, stderr); DATA(self)->history = history_init(); el_set(DATA(self)->editline, EL_CLIENTDATA, self); el_set(DATA(self)->editline, EL_HIST, history, DATA(self)->history); el_set(DATA(self)->editline, EL_PROMPT, promptCallback); el_set(DATA(self)->editline, EL_SIGNAL, 1); el_set(DATA(self)->editline, EL_EDITOR, "emacs"); { HistEvent ev; history(DATA(self)->history, &ev, H_SETSIZE, 300); } el_source(DATA(self)->editline, NULL); IoState_registerProtoWithFunc_((IoState *)state, self, IoEditLine_proto); IoObject_addMethodTable_(self, methodTable); return self; }
char *x_readline(const char *prompt) { static char buffer[1024]; int count = 0; const char *cp; if (!el) { hist = history_init(); history(hist, &ev, H_SETSIZE, HISTORY_SIZE); history(hist, &ev, H_SETUNIQUE, 1); el = el_init("GS+", stdin, stdout, stderr); el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_BIND, "-e", NULL, NULL, NULL); el_set(el, EL_HIST, history, hist); el_set(el, EL_PROMPT, prompt_fn); el_set(el, EL_SIGNAL, 1); el_source(el, NULL); } el_prompt = prompt; cp = el_gets(el, &count); el_prompt = NULL; if (count <= 0) { if (prompt) fputc('\n', stdout); return NULL; } if (count > sizeof(buffer) - 1) return ""; memcpy(buffer, cp, count); cleanup_buffer(buffer, count); if (*buffer) history(hist, &ev, H_ENTER, buffer); return buffer; }
int main(int argc, char *argv[]) { EditLine *el = NULL; int num; const char *buf; Tokenizer *tok; #if 0 int lastevent = 0; #endif int ncontinuation; History *hist; HistEvent ev; (void) setlocale(LC_CTYPE, ""); (void) signal(SIGINT, sig); (void) signal(SIGQUIT, sig); (void) signal(SIGHUP, sig); (void) signal(SIGTERM, sig); hist = history_init(); /* Init the builtin history */ /* Remember 100 events */ history(hist, &ev, H_SETSIZE, 100); tok = tok_init(NULL); /* Initialize the tokenizer */ /* Initialize editline */ el = el_init(*argv, stdin, stdout, stderr); el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */ el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */ el_set(el, EL_PROMPT_ESC, prompt, '\1');/* Set the prompt function */ /* Tell editline to use this history interface */ el_set(el, EL_HIST, history, hist); /* Add a user-defined function */ el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete); /* Bind tab to it */ el_set(el, EL_BIND, "^I", "ed-complete", NULL); /* * Bind j, k in vi command mode to previous and next line, instead * of previous and next history. */ el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL); el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL); /* * Source the user's defaults file. */ el_source(el, NULL); while ((buf = el_gets(el, &num)) != NULL && num != 0) { int ac, cc, co; #ifdef DEBUG int i; #endif const char **av; const LineInfo *li; li = el_line(el); #ifdef DEBUG (void) fprintf(stderr, "==> got %d %s", num, buf); (void) fprintf(stderr, " > li `%.*s_%.*s'\n", (li->cursor - li->buffer), li->buffer, (li->lastchar - 1 - li->cursor), (li->cursor >= li->lastchar) ? "" : li->cursor); #endif if (gotsig) { (void) fprintf(stderr, "Got signal %d.\n", (int)gotsig); gotsig = 0; el_reset(el); } if (!continuation && num == 1) continue; ac = cc = co = 0; ncontinuation = tok_line(tok, li, &ac, &av, &cc, &co); if (ncontinuation < 0) { (void) fprintf(stderr, "Internal error\n"); continuation = 0; continue; } #ifdef DEBUG (void) fprintf(stderr, " > nc %d ac %d cc %d co %d\n", ncontinuation, ac, cc, co); #endif #if 0 if (continuation) { /* * Append to the right event in case the user * moved around in history. */ if (history(hist, &ev, H_SET, lastevent) == -1) err(1, "%d: %s", lastevent, ev.str); history(hist, &ev, H_ADD , buf); } else { history(hist, &ev, H_ENTER, buf); lastevent = ev.num; } #else /* Simpler */ history(hist, &ev, continuation ? H_APPEND : H_ENTER, buf); #endif continuation = ncontinuation; ncontinuation = 0; if (continuation) continue; #ifdef DEBUG for (i = 0; i < ac; i++) { (void) fprintf(stderr, " > arg# %2d ", i); if (i != cc) (void) fprintf(stderr, "`%s'\n", av[i]); else (void) fprintf(stderr, "`%.*s_%s'\n", co, av[i], av[i] + co); } #endif if (strcmp(av[0], "history") == 0) { int rv; switch (ac) { case 1: for (rv = history(hist, &ev, H_LAST); rv != -1; rv = history(hist, &ev, H_PREV)) (void) fprintf(stdout, "%4d %s", ev.num, ev.str); break; case 2: if (strcmp(av[1], "clear") == 0) history(hist, &ev, H_CLEAR); else goto badhist; break; case 3: if (strcmp(av[1], "load") == 0) history(hist, &ev, H_LOAD, av[2]); else if (strcmp(av[1], "save") == 0) history(hist, &ev, H_SAVE, av[2]); break; badhist: default: (void) fprintf(stderr, "Bad history arguments\n"); break; } } else if (el_parse(el, ac, av) == -1) { switch (fork()) { case 0: execvp(av[0], __DECONST(char **, av)); perror(av[0]); _exit(1); /*NOTREACHED*/ break; case -1: perror("fork"); break; default: if (wait(&num) == -1) perror("wait"); (void) fprintf(stderr, "Exit %x\n", num); break; } } tok_reset(tok); } el_end(el); tok_end(tok); history_end(hist); return (0); }
/* * Set history and editing status. Called whenever the status may * have changed (figures out what to do). */ void histedit(void) { FILE *el_err; #define editing (Eflag || Vflag) if (iflag == 1) { if (!hist) { /* * turn history on */ INTOFF; hist = history_init(); INTON; if (hist != NULL) sethistsize(histsizeval()); else out2str("sh: can't initialize history\n"); } if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ /* * turn editing on */ char *term, *shname; INTOFF; if (el_in == NULL) el_in = fdopen(0, "r"); if (el_out == NULL) el_out = fdopen(2, "w"); if (el_in == NULL || el_out == NULL) goto bad; el_err = el_out; #if DEBUG if (tracefile) el_err = tracefile; #endif term = lookupvar("TERM"); if (term) setenv("TERM", term, 1); else unsetenv("TERM"); shname = arg0; if (shname[0] == '-') shname++; el = el_init(shname, el_in, el_out, el_err); if (el != NULL) { if (hist) el_set(el, EL_HIST, history, hist); el_set(el, EL_PROMPT, getprompt); el_set(el, EL_SIGNAL, 1); el_set(el, EL_ALIAS_TEXT, alias_text, NULL); el_set(el, EL_ADDFN, "rl-complete", "ReadLine compatible completion function", _el_fn_complete); } else { bad: out2str("sh: can't initialize editing\n"); } INTON; } else if (!editing && el) { INTOFF; el_end(el); el = NULL; INTON; } if (el) { el_source(el, NULL); if (Vflag) el_set(el, EL_EDITOR, "vi"); else if (Eflag) el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_BIND, "^I", tabcomplete ? "rl-complete" : "ed-insert", NULL); } } else { INTOFF; if (el) { /* no editing if not interactive */ el_end(el); el = NULL; } if (hist) { history_end(hist); hist = NULL; } INTON; } }
int cmdloop(void) { char *line; const char *elline; int cmd_argc, rval = 0, known; #define scratch known char **cmd_argv; struct cmdtable *cmdp; History *hist; EditLine *elptr; HistEvent he; curinode = check_ginode(ROOTINO); curinum = ROOTINO; printactive(0); hist = history_init(); history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */ elptr = el_init("fsdb", stdin, stdout, stderr); el_set(elptr, EL_EDITOR, "emacs"); el_set(elptr, EL_PROMPT, prompt); el_set(elptr, EL_HIST, history, hist); el_source(elptr, NULL); while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) { if (check_debug) printf("command `%s'\n", elline); history(hist, &he, H_ENTER, elline); line = strdup(elline); cmd_argv = crack(line, &cmd_argc); /* * el_parse returns -1 to signal that it's not been handled * internally. */ if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1) continue; if (cmd_argc) { known = 0; for (cmdp = cmds; cmdp->cmd; cmdp++) { if (!strcmp(cmdp->cmd, cmd_argv[0])) { if ((cmdp->flags & FL_WR) == FL_WR && nflag) warnx("`%s' requires write access", cmd_argv[0]), rval = 1; else if (cmd_argc >= cmdp->minargc && cmd_argc <= cmdp->maxargc) rval = (*cmdp->handler)(cmd_argc, cmd_argv); else if (cmd_argc >= cmdp->minargc && (cmdp->flags & FL_ST) == FL_ST) { strcpy(line, elline); cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc); rval = (*cmdp->handler)(cmd_argc, cmd_argv); } else rval = argcount(cmdp, cmd_argc, cmd_argv); known = 1; break; } } if (!known) warnx("unknown command `%s'", cmd_argv[0]), rval = 1; } else rval = 0; free(line); if (rval < 0) /* user typed "quit" */ return 0; if (rval) warnx("rval was %d", rval); } el_end(elptr); history_end(hist); return rval; }
/* * Set history and editing status. Called whenever the status may * have changed (figures out what to do). */ void histedit(void) { #define editing (Eflag || Vflag) if (iflag) { if (!hist) { /* * turn history on */ INTOFF; hist = history_init(); INTON; if (hist != NULL) sethistsize(histsizeval()); else out2fmt_flush("sh: can't initialize history\n"); } if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ /* * turn editing on */ char *term; INTOFF; if (el_in == NULL) el_in = fdopen(0, "r"); if (el_err == NULL) el_err = fdopen(1, "w"); if (el_out == NULL) el_out = fdopen(2, "w"); if (el_in == NULL || el_err == NULL || el_out == NULL) goto bad; term = lookupvar("TERM"); if (term) { if (setenv("TERM", term, 1) == -1) error("setenv: cannot set TERM=1"); } else unsetenv("TERM"); el = el_init(arg0, el_in, el_out, el_err); if (el != NULL) { if (hist) el_set(el, EL_HIST, history, hist); el_set(el, EL_PROMPT, getprompt); el_set(el, EL_ADDFN, "rl-complete", "ReadLine compatible completion function", _el_fn_complete); } else { bad: out2fmt_flush("sh: can't initialize editing\n"); } INTON; } else if (!editing && el) { INTOFF; el_end(el); el = NULL; INTON; } if (el) { if (Vflag) el_set(el, EL_EDITOR, "vi"); else if (Eflag) el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_BIND, "^I", tabcomplete ? "rl-complete" : "ed-insert", NULL); el_source(el, NULL); } } else { INTOFF; if (el) { /* no editing if not interactive */ el_end(el); el = NULL; } if (hist) { history_end(hist); hist = NULL; } INTON; } }
/* * Set history and editing status. Called whenever the status may * have changed (figures out what to do). */ void histedit() { #define editing (Eflag || Vflag) if (iflag) { if (!hist) { /* * turn history on */ INTOFF; hist = history_init(); INTON; if (hist != NULL) sethistsize(histsizeval()); else out2str("sh: can't initialize history\n"); } if (editing && !el && isatty(0)) { /* && isatty(2) ??? */ /* * turn editing on */ INTOFF; if (el_in == NULL) el_in = fdopen(0, "r"); if (el_out == NULL) el_out = fdopen(2, "w"); if (el_in == NULL || el_out == NULL) goto bad; el = el_init(arg0, el_in, el_out); if (el != NULL) { if (hist) el_set(el, EL_HIST, history, hist); el_set(el, EL_PROMPT, getprompt); } else { bad: out2str("sh: can't initialize editing\n"); } INTON; } else if (!editing && el) { INTOFF; el_end(el); el = NULL; INTON; } if (el) { if (Vflag) el_set(el, EL_EDITOR, "vi"); else if (Eflag) el_set(el, EL_EDITOR, "emacs"); } } else { INTOFF; if (el) { /* no editing if not interactive */ el_end(el); el = NULL; } if (hist) { history_end(hist); hist = NULL; } INTON; } }
void interact( const char *const argv_0) { EditLine *const el = el_init(argv_0, stdin, stdout, stderr); el_set(el, EL_PROMPT, &prompt); el_set(el, EL_EDITOR, "emacs"); History *const hist = history_init(); if (!hist) { fprintf(stderr, "Could not initalize history\n"); exit(EXIT_FAILURE); } HistEvent ev; history(hist, &ev, H_SETSIZE, 100); el_set(el, EL_HIST, history, hist); const pid_t child_pid = _gen_child(); verbose_printf("child process is %d\n", child_pid); if (options.verbose) help(); char buf[PAGE_SIZE]; size_t buf_sz = 0; int end = 0; struct proc_info_t info = {}; ARCH_INIT_PROC_INFO(info); ptrace_launch(child_pid); ptrace_cont(child_pid, &info); ptrace_reap(child_pid, &info); display(&info); for (;;) { int count; const char *const line = el_gets(el, &count); if (count == -1) { perror("el_gets"); exit(EXIT_FAILURE); } // count is 0 == ^d if (!count || strcasestr(line, ".quit") || strcasestr(line, ".exit")) break; // We have input, add it to the our history history(hist, &ev, H_ENTER, line); // If we start with a ., we have a command if (line[0] == '.') { if (strcasestr(line, "help")) { help(); continue; } if (strcasestr(line, "info")) { display(&info); continue; } if (strcasestr(line, "showmap")) { char cmd[PATH_MAX] = { 0 }; snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", child_pid); if (system(cmd)) fprintf(stderr, "sh: %s failed\n", cmd); continue; } if (strcasestr(line, "read")) { ui_read(child_pid, line); continue; } if (strcasestr(line, "write")) { continue; } if (strcasestr(line, "begin")) { in_block = 1; continue; } // Note the lack of continue. Need to fall through... if (strcasestr(line, "end")) { in_block = 0; end = 1; } } if (buf_sz + count > sizeof(buf)) { printf("Buffer full (max: 0x%lx), please use '.end'\n", sizeof(buf)); continue; } // Since we fell through, we want to avoid adding adding .end to our buffer if (!end) { memcpy(buf + buf_sz, line, count); buf_sz += count; } if (!in_block) { verbose_printf("Trying to assemble(%zu):\n%s", buf_sz, buf); uint8_t bytecode[PAGE_SIZE]; const size_t bytecode_sz = assemble(bytecode, sizeof(bytecode), buf, buf_sz); memset(buf, 0, sizeof(buf)); buf_sz = 0; end = 0; verbose_printf("Got asm(%zu):\n", bytecode_sz); verbose_dump(bytecode, bytecode_sz, -1); if (!bytecode_sz) { fprintf(stderr, "'%s' assembled to 0 length bytecode\n", buf); continue; } ptrace_write(child_pid, (void *)options.start, bytecode, bytecode_sz); ptrace_reset(child_pid, options.start); ptrace_cont(child_pid, &info); ptrace_reap(child_pid, &info); display(&info); } } ptrace_detatch(child_pid, &info); printf("\n"); history_end(hist); el_end(el); }
/* * Command parser. */ static void cmdscanner(void) { register struct cmd *c; static EditLine *el; static History *hist; HistEvent he; size_t len; int num; const char *bp; num = 0; bp = NULL; el = NULL; hist = NULL; for (;;) { if (fromatty) { if (!el) { el = el_init("lpc", stdin, stdout, stderr); hist = history_init(); history(hist, &he, H_SETSIZE, 100); el_set(el, EL_HIST, history, hist); el_set(el, EL_EDITOR, "emacs"); el_set(el, EL_PROMPT, lpc_prompt); el_set(el, EL_SIGNAL, 1); el_source(el, NULL); /* * EditLine init may call 'cgetset()' to set a * capability-db meant for termcap (eg: to set * terminal type 'xterm'). Reset that now, or * that same db-information will be used for * printcap (giving us an "xterm" printer, with * all kinds of invalid capabilities...). */ cgetset(NULL); } if ((bp = el_gets(el, &num)) == NULL || num == 0) quit(0, NULL); len = (num > MAX_CMDLINE - 1) ? MAX_CMDLINE - 1 : num; memcpy(cmdline, bp, len); cmdline[len] = 0; history(hist, &he, H_ENTER, bp); } else { if (fgets(cmdline, MAX_CMDLINE, stdin) == NULL) quit(0, NULL); if (cmdline[0] == 0 || cmdline[0] == '\n') break; } makeargv(); if (margc == 0) continue; if (el != NULL && el_parse(el, margc, margv) != -1) continue; c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); continue; } if (c == NULL) { printf("?Invalid command\n"); continue; } if ((c->c_opts & LPC_PRIVCMD) && getuid() && ingroup(LPR_OPER) == 0) { printf("?Privileged command\n"); continue; } /* * Two different commands might have the same generic rtn * (eg: "clean" and "tclean"), and just use different * handler routines for distinct command-setup. The handler * routine might also be set on a generic routine for * initial parameter processing. */ if (c->c_generic != NULL) generic(c->c_generic, c->c_opts, c->c_handler, margc, margv); else (*c->c_handler)(margc, margv); } }