12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /* Copyright Yann Weber <asmsh@yannweb.net>
- This file is part of asmsh.
-
- asmsh is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- details.
-
- You should have received a copy of the GNU General Public License along
- with asmsh. If not, see <https://www.gnu.org/licenses/>.
- */
- #include "config.h"
- #include <stdio.h>
- #include <readline/readline.h>
- #include <readline/history.h>
-
- #define CHILD_EXEC_PATH "./child"
-
- #include "logger.h"
- #include "shell.h"
- #include "completion.h"
- #include "history.h"
-
- int main(int argc, char *argv[], char *envp[])
- {
- asmsh_logger_t *logger;
- asmsh_t sh;
-
- int ret = 0;
- char prompt[64];
- bzero(prompt, sizeof(prompt));
-
- logger = asmsh_logger_new(ASMSH_INFO);
- asmsh_logger_setup(logger);
-
- if(asmsh_init(&sh, CHILD_EXEC_PATH))
- {
- asmsh_log_error("Unable to init shell : %s", strerror(errno));
- asmsh_logger_dprint(2, logger);
- return 1;
- }
-
- rl_special_prefixes="";
- rl_basic_word_break_characters=" \t\n";
- rl_attempted_completion_function = asmsh_rl_completion;
-
- char * hpath = history_filename_init(NULL);
- load_history(hpath, add_history);
-
- while(1)
- {
- char *cmd;
- snprintf(prompt, sizeof(prompt)-1,
- "asmsh@%p > ",
- (void*)sh.env->regs.rip);
- cmd = readline(prompt);
- if(!cmd)
- {
- break;
- }
- ret = asmsh_exec(&sh, cmd);
- asmsh_logger_dprint_fmt(2, logger, asmsh_log_lvl_fmt);
- if(ret > 0)
- {
- //unrecoverable error or exit (if ret == 1)
- ret--; // exit status
- if(ret)
- {
- dprintf(2, "Exit with status %d\n", ret);
- }
- break;
- }
- //else if (ret < 0) {} //recoverable errors
- add_history(cmd);
- }
-
- asmsh_cleanup(&sh);
-
- HIST_ENTRY **hist = history_list();
- int hist_count = 0;
- for(HIST_ENTRY **p=hist; *p;p++){ hist_count++; }
- const char *hstr[(hist_count+1)*sizeof(char*)];
- for(int i=0; i<hist_count; i++){ hstr[i] = hist[i]->line; }
- hstr[hist_count]=NULL;
-
- save_history(hpath, hstr);
- free(hpath);
-
- return ret;
- }
|