A shell that runs x86_64 assembly
c
x86-64
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

shell.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "shell.h"
  14. int asmsh_init(asmsh_t *sh, const char *child_path)
  15. {
  16. bzero(sh, sizeof(asmsh_t));
  17. if(!(sh->env = asmsh_env(child_path)))
  18. {
  19. asmsh_log_fatal("Unable to start child env");
  20. goto err;
  21. }
  22. if(!(sh->cctx = asmsh_asmc_ctx_default()))
  23. {
  24. asmsh_log_fatal("Unable to start compilation context");
  25. asmsh_env_free(sh->env);
  26. goto err;
  27. }
  28. sh->child_path = strdup(child_path); // TODO check error
  29. return 0;
  30. err:
  31. bzero(sh, sizeof(asmsh_t));
  32. return -1;
  33. }
  34. void asmsh_cleanup(asmsh_t *sh)
  35. {
  36. if(!sh){ return; }
  37. if(sh->cctx)
  38. {
  39. asmsh_asmc_ctx_free(sh->cctx);
  40. }
  41. if(sh->env)
  42. {
  43. asmsh_env_free(sh->env);
  44. }
  45. if(sh->child_path)
  46. {
  47. free(sh->child_path);
  48. }
  49. if(sh->last_instr)
  50. {
  51. free(sh->last_instr);
  52. }
  53. bzero(sh, sizeof(asmsh_t));
  54. }
  55. /** Attempt to compile and step if sucess */
  56. static int _compile_step(asmsh_t *sh, const char *cmd);
  57. /** Attempt to handle given shell internal command (starting with '.') */
  58. static int _handle_command(asmsh_t *sh, const char *cmd);
  59. int asmsh_exec(asmsh_t *sh, const char *cmd)
  60. {
  61. if(!cmd)
  62. {
  63. errno=EINVAL;
  64. return -1;
  65. }
  66. int clen, ret;
  67. //lstrip whitespace
  68. for(cmd; *cmd && (*cmd == ' ' || *cmd == '\t'); cmd++);
  69. if(!*cmd) { return 0; }
  70. switch(*cmd)
  71. {
  72. case '.':
  73. ret = _handle_command(sh, cmd);
  74. break;
  75. default:
  76. ret = _compile_step(sh, cmd);
  77. break;
  78. }
  79. return ret;
  80. }
  81. static int _compile_step(asmsh_t *sh, const char *cmd)
  82. {
  83. int ret, status;
  84. asmsh_bytecode_t bcode;
  85. if(asmsh_asmc_compile(sh->cctx, cmd, &bcode) < 0)
  86. {
  87. return -1;
  88. }
  89. if(!bcode.size)
  90. {
  91. asmsh_log_error("No bytecode returned...");
  92. return -1;
  93. }
  94. sh->last_bcode = bcode;
  95. if(sh->last_instr) { free(sh->last_instr); }
  96. sh->last_instr = strdup(cmd);
  97. char buf[256];
  98. int _ret;
  99. _ret = snprintf(buf, 255, "'%s' = 0x", cmd);
  100. for(int _i=0; _i<bcode.size; _i++)
  101. {
  102. _ret += snprintf(buf+_ret, 255-_ret, "%02x", bcode.bytes[_i]);
  103. }
  104. _ret += snprintf(buf+_ret, 255-_ret, "(%d)", bcode.size);
  105. buf[_ret] = '\0';
  106. asmsh_log_info(buf);
  107. if(asmsh_env_write_code(sh->env, &bcode))
  108. {
  109. return -1;
  110. }
  111. status=0;
  112. ret = asmsh_env_step(sh->env, &status);
  113. if(ret < 0)
  114. {
  115. perror("Unable to step");
  116. asmsh_log_fatal("Error will child step");
  117. return -1;
  118. }
  119. else if(ret > 0)
  120. {
  121. asmsh_log_fatal("Child exited with non-zero status");
  122. return 1+WEXITSTATUS(status);
  123. }
  124. return 0;
  125. }
  126. static int _handle_command(asmsh_t *sh, const char *cmd)
  127. {
  128. const int clen = strlen(cmd);
  129. const asmsh_cmd_t *match;
  130. int ret;
  131. asmsh_cmd_args_t *args;
  132. args = asmsh_cmd_parse(cmd);
  133. if(!args)
  134. {
  135. asmsh_log_error("Parse error"); // TODO better error
  136. return -1;
  137. }
  138. match = asmsh_cmd_match(args->cmd, strlen(args->cmd));
  139. if(!match)
  140. {
  141. asmsh_log_error("Unknow command");
  142. return -1;
  143. }
  144. ret = match->cmd(sh, args);
  145. asmsh_cmd_args_free(args);
  146. return ret;
  147. }