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.

shell_cmds.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef ASMSH_SHELL_CMDS_H
  14. #define ASMSH_SHELL_CMDS_H
  15. #include "config.h"
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include "logger.h"
  21. #include "syscalls.h"
  22. #define ASMSH_CMD_MAXLEN 31
  23. #define ASMSH_CMD_MAXARG 15
  24. typedef struct asmsh_cmd_s asmsh_cmd_t;
  25. typedef struct asmsh_cmd_args_s asmsh_cmd_args_t;
  26. #include "shell.h"
  27. /** @return <0 on recoverable error 0 on ok, 1+status on exit */
  28. typedef int (asmsh_cmd_f)(asmsh_t*, asmsh_cmd_args_t*);
  29. /** Represent a single command */
  30. struct asmsh_cmd_s
  31. {
  32. /** The command (with the leading '.') */
  33. const char *str;
  34. /** The command function pointer */
  35. asmsh_cmd_f *cmd;
  36. /**If non-zero indicate the minimum of chars to
  37. * match. ".quit" while have 2 to match starting
  38. * from ".q"
  39. */
  40. unsigned char sm;
  41. /* Help information */
  42. /** Small command name (like .h(elp) )*/
  43. const char *sms;
  44. /** Command usage */
  45. const char *usage;
  46. /** Command description */
  47. const char *desc;
  48. };
  49. /** A command arguments with command name & NULL terminated array of args */
  50. struct asmsh_cmd_args_s
  51. {
  52. char cmd[ASMSH_CMD_MAXLEN+1];
  53. int argc;
  54. char *args[ASMSH_CMD_MAXARG+1];
  55. };
  56. /**@param const char* the text to match
  57. * @param int the len of the text to match
  58. * @return a ptr or NULL
  59. */
  60. const asmsh_cmd_t *asmsh_cmd_match(const char *cmd, int stop);
  61. /** @return NULL on error or ptr on new cmd args */
  62. asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd);
  63. void asmsh_cmd_args_free(asmsh_cmd_args_t *args);
  64. const char *asmsh_cmd_help(asmsh_t *sh);
  65. /*
  66. * Commands declaration
  67. *
  68. * A command can return 0 if no errors, -X for a recoverable error
  69. * 1 for exit with status 0, 1+S for exit with status S
  70. */
  71. // Quit the shell
  72. int asmsh_cmd_quit(asmsh_t *sh, asmsh_cmd_args_t *args);
  73. // Print an instruction bytecode
  74. int asmsh_cmd_bcode_(asmsh_t *sh, char *buf, int bufsz, int argc, char **args);
  75. int asmsh_cmd_bcode(asmsh_t *sh, asmsh_cmd_args_t *args);
  76. int asmsh_cmd_maps(asmsh_t *sh, asmsh_cmd_args_t *args);
  77. int asmsh_cmd_print_regs(asmsh_t *sh, asmsh_cmd_args_t *args);
  78. int asmsh_cmd_syscalls(asmsh_t *sh, asmsh_cmd_args_t *args);
  79. // Reset the shell (restart the child etc)a
  80. int asmsh_cmd_reset(asmsh_t *sh, asmsh_cmd_args_t *args);
  81. // Display CPU flag resister values
  82. int asmsh_cmd_flags(asmsh_t *sh, asmsh_cmd_args_t *args);
  83. int asmsh_cmd_help_(asmsh_t *sh, asmsh_cmd_args_t *args);
  84. /*
  85. * The list of shell commands
  86. */
  87. static const asmsh_cmd_t asmsh_CMDS[] = {
  88. {".bytecode", asmsh_cmd_bcode, 2,
  89. ".b(ytecode)", "",
  90. "display last instruction bytecode"},
  91. {".flags", asmsh_cmd_flags, 2,
  92. ".f(lags)", "",
  93. "display CPU flags"},
  94. {".help", asmsh_cmd_help_, 2,
  95. ".h(elp)","[cmd]",
  96. "display this help or the help of specified command"},
  97. {".maps", asmsh_cmd_maps, 2,
  98. ".m(aps)", "",
  99. "display memory maps"},
  100. {".quit", asmsh_cmd_quit, 2,
  101. ".q(uit)","",
  102. "quit asmsh"},
  103. {".regs", asmsh_cmd_print_regs, 1,
  104. ".(regs)", "",
  105. "display registers value"},
  106. {".syscalls", asmsh_cmd_syscalls, 2,
  107. ".s(yscalls)", "",
  108. "print syscalls name & numbers"},
  109. {".reset", asmsh_cmd_reset, 0,
  110. ".reset", "",
  111. "reset the shell"},
  112. {NULL, NULL, 0},
  113. };
  114. #endif