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

shell_sym.c 3.3KB

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