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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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;
  27. char *cmd;
  28. char prompt[64];
  29. bzero(prompt, sizeof(prompt));
  30. logger = asmsh_logger_new(ASMSH_INFO);
  31. asmsh_logger_setup(logger);
  32. if(asmsh_init(&sh, CHILD_EXEC_PATH))
  33. {
  34. perror("Unable to init shell");
  35. asmsh_logger_dprint(2, logger);
  36. return 1;
  37. }
  38. rl_special_prefixes="";
  39. rl_basic_word_break_characters=" \t\n";
  40. rl_attempted_completion_function = asmsh_rl_completion;
  41. char * hpath = history_filename_init(NULL);
  42. load_history(hpath, add_history);
  43. while(1)
  44. {
  45. snprintf(prompt, sizeof(prompt)-1,
  46. "asmsh@%p > ",
  47. 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. end:
  69. asmsh_cleanup(&sh);
  70. HIST_ENTRY **hist = history_list();
  71. int hist_count = 0;
  72. for(HIST_ENTRY **p=hist; *p;p++){ hist_count++; }
  73. const char **hstr = alloca((hist_count+1)*sizeof(char*));
  74. for(int i=0; i<hist_count; i++){ hstr[i] = hist[i]->line; }
  75. hstr[hist_count]=NULL;
  76. save_history(hpath, hstr);
  77. free(hpath);
  78. return ret;
  79. err:
  80. ret = 1;
  81. goto end;
  82. }