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.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_cmds.h"
  14. const asmsh_cmd_t *asmsh_cmd_match(const char *text, int tlen)
  15. {
  16. for(const asmsh_cmd_t *cmd=asmsh_CMDS; cmd->str; cmd++)
  17. {
  18. const int clen = strlen(cmd->str);
  19. if(cmd->sm && tlen >= cmd->sm && tlen < clen)
  20. {
  21. if(!strncmp(cmd->str, text, tlen))
  22. {
  23. return cmd;
  24. }
  25. continue;
  26. }
  27. else if(tlen != clen) { continue; }
  28. else
  29. {
  30. int cmp = strncmp(text, cmd->str, tlen);
  31. if(cmp < 0)
  32. {
  33. break;
  34. }
  35. else if(!cmp)
  36. {
  37. return cmd;
  38. }
  39. }
  40. }
  41. return NULL;
  42. }
  43. asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd)
  44. {
  45. asmsh_cmd_args_t *ret;
  46. int cmdlen, err, argno;
  47. const char *ptr, *arg;
  48. if(!cmd || !*cmd || *cmd != '.') { return NULL; }
  49. if(!(ret = malloc(sizeof(*ret))))
  50. {
  51. asmsh_log_fatal("Unable to allocate command");
  52. return NULL;
  53. }
  54. bzero(ret, sizeof(*ret));
  55. cmdlen=0;
  56. for(ptr=cmd; *ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '\n'; ptr++)
  57. {
  58. cmdlen++;
  59. }
  60. if(cmdlen > ASMSH_CMD_MAXLEN)
  61. {
  62. asmsh_log_error("Invalid command"); //TODO printf command
  63. err=EINVAL;
  64. goto err;
  65. }
  66. strncpy(ret->cmd, cmd, cmdlen);
  67. ret->cmd[cmdlen] = '\0';
  68. // lstrip white chars
  69. for(ptr; *ptr && *ptr == ' ' && *ptr == '\t' && *ptr == '\n'; ptr++);
  70. if(!*ptr)
  71. {
  72. // end of command
  73. ret->args[0] = NULL;
  74. return ret;
  75. }
  76. arg = ptr;
  77. argno=0;
  78. do
  79. {
  80. int arglen = 0;
  81. for(ptr; *ptr && *ptr != ' '&& *ptr != '\t' && *ptr != '\n'; ptr++)
  82. {
  83. arglen++;
  84. }
  85. ret->args[argno] = strndup(arg, arglen);
  86. if(!ret->args[argno])
  87. {
  88. err=errno;
  89. goto err;
  90. }
  91. argno++;
  92. if(argno >= ASMSH_CMD_MAXARG)
  93. {
  94. //too many arguments
  95. err=EINVAL;
  96. goto err;
  97. }
  98. // lstrip white chars
  99. for(ptr; *ptr && *ptr == ' ' && *ptr == '\t' && *ptr == '\n'; ptr++);
  100. }while(*ptr);
  101. ret->args[argno] = NULL;
  102. return ret;
  103. err:
  104. for(char **p=ret->args; *p; p++) { free(*p); }
  105. errno = err;
  106. free(ret);
  107. }