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_sym.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_sym.h"
  14. int asmsh_symtable_init(asmsh_symtable_t *table)
  15. {
  16. int err;
  17. //table->freeval = freeval?1:0;
  18. table->alloc = ASMSH_SYMALLOC;
  19. table->syms = malloc(table->alloc*sizeof(*table->syms));
  20. if(!table->syms)
  21. {
  22. err = errno;
  23. goto err;
  24. }
  25. bzero(table->syms, table->alloc*sizeof(*table->syms));
  26. table->syms_sz = 0;
  27. return 0;
  28. err:
  29. bzero(table, sizeof(*table));
  30. errno=err;
  31. return -1;
  32. }
  33. void asmsh_symtable_clean(asmsh_symtable_t *table)
  34. {
  35. /*
  36. if(table->freeval)
  37. {
  38. for(int i=0; i<table->syms_sz; i++)
  39. {
  40. free(table->syms[i].val);
  41. }
  42. }
  43. */
  44. free(table->syms);
  45. bzero(table, sizeof(*table));
  46. }
  47. int asmsh_symtable_add(asmsh_symtable_t *table, const char *name, unsigned long val)
  48. {
  49. int i;
  50. if(strlen(name)>ASMSH_VARNAME_MAX)
  51. {
  52. errno=EINVAL;
  53. return -1;
  54. }
  55. if(table->syms_sz >= table->alloc-1)
  56. {
  57. table->alloc += ASMSH_SYMALLOC;
  58. void *tmp = realloc(table->syms, sizeof(*table->syms)*table->alloc);
  59. if(!tmp)
  60. {
  61. return -1;
  62. }
  63. table->syms = tmp;
  64. }
  65. for(i=0; i<table->syms_sz; i++)
  66. {
  67. int cmp = strcmp(name, table->syms[i].name);
  68. if(cmp == 0)
  69. {
  70. //update value
  71. /*
  72. if(table->freeval)
  73. {
  74. free(table->syms[i].val);
  75. }
  76. */
  77. table->syms[i].addr = val;
  78. return 0;
  79. }
  80. else if(cmp > 0)
  81. {
  82. break;
  83. }
  84. }
  85. if(i<table->syms_sz)
  86. {
  87. memmove(&table->syms[i+1], &table->syms[i],
  88. sizeof(*table->syms)*(table->syms_sz - i));
  89. }
  90. strncpy(table->syms[i].name, name, ASMSH_VARNAME_MAX);
  91. table->syms[i].name[ASMSH_VARNAME_MAX] = '\0';
  92. table->syms[i].addr = val;
  93. table->syms_sz++;
  94. return 0;
  95. }
  96. int asmsh_symtable_del(asmsh_symtable_t *table, const char *name)
  97. {
  98. if(strlen(name) > ASMSH_VARNAME_MAX)
  99. {
  100. return 0;
  101. }
  102. int i=0;
  103. for(i=0; i<table->syms_sz; i++)
  104. {
  105. int cmp = strncmp(name, table->syms[i].name, ASMSH_VARNAME_MAX);
  106. if(!cmp) { break; }
  107. }
  108. if(i==table->syms_sz)
  109. {
  110. return 0; // not found
  111. }
  112. //if(table->freeval) { free(table->syms[i].val); }
  113. if(table->syms_sz > i+1)
  114. {
  115. memmove(&table->syms[i], &table->syms[i+1],
  116. sizeof(*table->syms)*(table->syms_sz - (i+1)));
  117. if((table->alloc - table->syms_sz) > ASMSH_SYMALLOC*2)
  118. {
  119. table->alloc -= ASMSH_SYMALLOC;
  120. void *tmp = realloc(table->syms,
  121. sizeof(*table->syms)*table->alloc);
  122. if(!tmp)
  123. {
  124. return 0; // :/
  125. }
  126. table->syms = tmp;
  127. }
  128. }
  129. table->syms_sz--;
  130. return 1; // 1 deleted
  131. }
  132. const asmsh_sym_t *asmsh_symtable_get(asmsh_symtable_t *table, const char *name)
  133. {
  134. if(strlen(name) > ASMSH_VARNAME_MAX)
  135. {
  136. return NULL;
  137. }
  138. for(int i=0; i<table->syms_sz; i++)
  139. {
  140. int cmp = strcmp(name, table->syms[i].name);
  141. if(!cmp)
  142. {
  143. return &(table->syms[i]);
  144. }
  145. if(cmp > 0) { break; }
  146. }
  147. return NULL;
  148. }