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 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. int main(int argc, char *argv[], char *envp[])
  22. {
  23. asmsh_logger_t *logger;
  24. asmsh_t sh;
  25. int ret;
  26. char *cmd;
  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. perror("Unable to init shell");
  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. while(1)
  41. {
  42. snprintf(prompt, sizeof(prompt)-1,
  43. "asmsh@%p > ",
  44. sh.env->regs.rip);
  45. cmd = readline(prompt);
  46. if(!cmd)
  47. {
  48. break;
  49. }
  50. ret = asmsh_exec(&sh, cmd);
  51. asmsh_logger_dprint_fmt(2, logger, asmsh_log_lvl_fmt);
  52. if(ret > 0)
  53. {
  54. //unrecoverable error or exit (if ret == 1)
  55. ret--; // exit status
  56. if(ret)
  57. {
  58. dprintf(2, "Exit with status %d\n", ret);
  59. }
  60. break;
  61. }
  62. //else if (ret < 0) {} //recoverable errors
  63. add_history(cmd);
  64. }
  65. asmsh_cleanup(&sh);
  66. return ret;
  67. err:
  68. asmsh_cleanup(&sh);
  69. return 1;
  70. }