A shell that runs x86_64 assembly
c
x86-64
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asmsh.c 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright Yann Weber <asmsh@yannweb.net>
  2. This file is part of asmsh.
  3. asmsh is free software: you can redistribute it and/or modify it under the
  4. terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or any later version.
  6. asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. details.
  10. You should have received a copy of the GNU General Public License along
  11. with asmsh. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #include "config.h"
  14. #include <stdio.h>
  15. #include <readline/readline.h>
  16. #include <readline/history.h>
  17. #define CHILD_EXEC_PATH "./child"
  18. #include "logger.h"
  19. #include "shell.h"
  20. #include "completion.h"
  21. #include "history.h"
  22. int main(int argc, char *argv[], char *envp[])
  23. {
  24. asmsh_logger_t *logger;
  25. asmsh_t sh;
  26. int ret = 0;
  27. char prompt[64];
  28. bzero(prompt, sizeof(prompt));
  29. logger = asmsh_logger_new(ASMSH_INFO);
  30. asmsh_logger_setup(logger);
  31. if(asmsh_init(&sh, CHILD_EXEC_PATH))
  32. {
  33. asmsh_log_error("Unable to init shell : %s", strerror(errno));
  34. asmsh_logger_dprint(2, logger);
  35. return 1;
  36. }
  37. rl_special_prefixes="";
  38. rl_basic_word_break_characters=" \t\n";
  39. rl_attempted_completion_function = asmsh_rl_completion;
  40. char * hpath = history_filename_init(NULL);
  41. load_history(hpath, add_history);
  42. while(1)
  43. {
  44. char *cmd;
  45. snprintf(prompt, sizeof(prompt)-1,
  46. "asmsh@%p > ",
  47. (void*)sh.env->regs.rip);
  48. cmd = readline(prompt);
  49. if(!cmd)
  50. {
  51. break;
  52. }
  53. ret = asmsh_exec(&sh, cmd);
  54. asmsh_logger_dprint_fmt(2, logger, asmsh_log_lvl_fmt);
  55. if(ret > 0)
  56. {
  57. //unrecoverable error or exit (if ret == 1)
  58. ret--; // exit status
  59. if(ret)
  60. {
  61. dprintf(2, "Exit with status %d\n", ret);
  62. }
  63. break;
  64. }
  65. //else if (ret < 0) {} //recoverable errors
  66. add_history(cmd);
  67. }
  68. asmsh_cleanup(&sh);
  69. HIST_ENTRY **hist = history_list();
  70. int hist_count = 0;
  71. for(HIST_ENTRY **p=hist; *p;p++){ hist_count++; }
  72. const char *hstr[(hist_count+1)*sizeof(char*)];
  73. for(int i=0; i<hist_count; i++){ hstr[i] = hist[i]->line; }
  74. hstr[hist_count]=NULL;
  75. save_history(hpath, hstr);
  76. free(hpath);
  77. return ret;
  78. }