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.

completion.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 "completion.h"
  14. char **asmsh_completion(const char *buf, const char *text, int start, int end)
  15. {
  16. if(end == 0)
  17. {
  18. // TODO catenate all !
  19. return asmsh_compl_instr(text);
  20. }
  21. if(*buf != '.')
  22. {
  23. if(start == 0)
  24. {
  25. return asmsh_compl_instr(text);
  26. }
  27. return asmsh_compl_arg(buf, text, start);
  28. }
  29. // command completion handling
  30. }
  31. /// Replace uppper char leading macro if needed
  32. static int _completion_upper_macro(const char *macro, const char *start, char **ret, size_t *i);
  33. char **asmsh_compl_instr(const char *start)
  34. {
  35. const size_t icount = sizeof(x86_64_instr)/sizeof(*x86_64_instr);
  36. size_t i,j;
  37. char **ret;
  38. int start_len, cmp;
  39. ret = malloc(icount * 2 * sizeof(char*));
  40. bzero(ret, icount * sizeof(char*));
  41. if(!ret)
  42. {
  43. perror("Unable to allocate completion result");
  44. return NULL;
  45. }
  46. if(!start || !*start)
  47. {
  48. i=0;
  49. for(j=0; j<icount; j++)
  50. {
  51. if(x86_64_instr[j].mnemo)
  52. {
  53. int mret = _completion_upper_macro(
  54. x86_64_instr[j].mnemo,
  55. "", ret, &i);
  56. if(mret < 0)
  57. {
  58. goto err_strdup;
  59. }
  60. else if(mret == 0)
  61. {
  62. ret[i] = strdup(x86_64_instr[j].mnemo);
  63. if(!ret[i])
  64. {
  65. goto err_strdup;
  66. }
  67. i++;
  68. }
  69. }
  70. else
  71. {
  72. ret[i] = NULL;
  73. }
  74. }
  75. return ret;
  76. }
  77. else
  78. {
  79. start_len = strlen(start);
  80. i=0;
  81. for(j=0; x86_64_instr[j].mnemo; j++)
  82. {
  83. int mret = _completion_upper_macro(
  84. x86_64_instr[j].mnemo,
  85. start, ret, &i);
  86. if(mret < 0)
  87. {
  88. goto err_strdup;
  89. }
  90. else if(mret > 0)
  91. {
  92. continue; // handled by macro
  93. }
  94. // normal comparison
  95. cmp = strncmp(start, x86_64_instr[j].mnemo, start_len);
  96. if(cmp == 0)
  97. {
  98. ret[i]=strdup(x86_64_instr[j].mnemo);
  99. if(!ret[i])
  100. {
  101. goto err_strdup;
  102. }
  103. i++;
  104. }
  105. else if (cmp < 0)
  106. {
  107. break;
  108. }
  109. }
  110. }
  111. return ret;
  112. err_strdup:
  113. perror("Error while copying string for completion");
  114. i--;
  115. do
  116. {
  117. free(ret[i]);
  118. }while(i>0);
  119. free(ret);
  120. return NULL;
  121. }
  122. /// match against macro
  123. static int _completion_macro_match(const char *macro, const char *buf, int mlen);
  124. char **asmsh_compl_arg(const char *buf, const char *text, int start)
  125. {
  126. const asmsh_compinstr_t *match = NULL;
  127. int argno;
  128. int i, tlen;
  129. int matchlen = 0;
  130. int macro = 0;
  131. char **ret = malloc(sizeof(char*)*512); /* large enough */
  132. if(!ret)
  133. {
  134. perror("unable to allocate buffer");
  135. return NULL;
  136. }
  137. bzero(ret, sizeof(char*)*512);
  138. tlen = text?strlen(text):0;
  139. for(int i=0; i<(sizeof(x86_64_instr)/sizeof(*x86_64_instr))-1; i++)
  140. {
  141. int cmp;
  142. const char *instr = x86_64_instr[i].mnemo;
  143. int ilen = strlen(instr);
  144. macro=0;
  145. if(instr[ilen-1] >= 'A' && instr[ilen-1] <= 'Z')
  146. {
  147. ilen--;
  148. macro = 1;
  149. cmp = _completion_macro_match(instr, buf, ilen);
  150. if(cmp < 0)
  151. {
  152. goto err;
  153. }
  154. }
  155. else
  156. {
  157. cmp = strncmp(buf, instr, ilen);
  158. }
  159. if(cmp < 0)
  160. {
  161. break;
  162. }
  163. else if(cmp > 0)
  164. {
  165. continue;
  166. }
  167. else if(!macro && (buf[ilen] != ' ' && buf[ilen] != '\t'))
  168. {
  169. continue; //not a full match
  170. }
  171. // match or approximate macro match
  172. match = &x86_64_instr[i];
  173. matchlen = ilen;
  174. while(buf[matchlen] && buf[matchlen] != ' ' && buf[matchlen] != '\t')
  175. {
  176. matchlen++;
  177. }
  178. break;
  179. }
  180. if(!match)
  181. {
  182. ret = malloc(sizeof(char*));
  183. *ret = NULL;
  184. return ret;
  185. }
  186. argno=0;
  187. for(i=matchlen; i<=start; i++)
  188. {
  189. if(buf[i] == ',')
  190. {
  191. argno=1;
  192. }
  193. }
  194. int args = argno?match->arg2:match->arg1;
  195. if(macro && match->mnemo[strlen(match->mnemo)-1] == 'S')
  196. {
  197. int wreg=ASHCOMP_REGALL;
  198. switch(buf[matchlen-1])
  199. {
  200. case 'b'://M_sizes[0]:
  201. wreg = ASHCOMP_REG8;
  202. break;
  203. case 'w'://M_sizes[1]:
  204. wreg = ASHCOMP_REG16;
  205. break;
  206. case 'd'://M_sizes[2]:
  207. wreg = ASHCOMP_REG32;
  208. break;
  209. case 'q'://M_sizes[3]:
  210. wreg = ASHCOMP_REG64;
  211. break;
  212. default:
  213. wreg = ASHCOMP_REGALL;
  214. break;
  215. }
  216. args &= (0xff ^ ASHCOMP_REGALL) | wreg;
  217. }
  218. i=0;// result index
  219. for(const asmsh_symtable_elt_t *elt=asmsh_symtable; elt->regs; elt++)
  220. {
  221. if(!(elt->flag & args))
  222. {
  223. continue;
  224. }
  225. if(elt->flag == ASHCOMP_IMM && tlen > 1 && *text=='$')
  226. {
  227. // immediate value handling
  228. continue;
  229. }
  230. for(const char * const *r=elt->regs; *r; r++)
  231. {
  232. if(tlen && strncmp(text, *r, tlen))
  233. {
  234. continue;
  235. }
  236. if(!(ret[i] = strdup(*r)))
  237. {
  238. goto err_strdup;
  239. }
  240. if(argno == 0 && **r == '%')
  241. {
  242. //full match on arg0 adding ","
  243. char *tmp = ret[i];
  244. char buf[32];
  245. snprintf(buf, 31, "%s,", tmp);
  246. ret[i] = strdup(buf);
  247. }
  248. i++;
  249. }
  250. }
  251. return ret;
  252. err_strdup:
  253. perror("Unable to strdup arg match");
  254. i--;
  255. while(i>=0)
  256. {
  257. free(ret[i]);
  258. i--;
  259. }
  260. err:
  261. free(ret);
  262. return NULL;
  263. }
  264. static int _completion_upper_macro(const char *macro, const char *start, char **ret, size_t *i)
  265. {
  266. int stlen, len, oi, need_cmp;
  267. char buf[32]; /* large enough */
  268. char fmt[32];
  269. oi = *i;
  270. len = strlen(macro);
  271. stlen = strlen(start);
  272. switch(macro[len-1])
  273. {
  274. case 'C':
  275. case 'S':
  276. break;
  277. default:
  278. return 0;
  279. }
  280. need_cmp=1;
  281. if(len > stlen)
  282. {
  283. if(strncmp(start, macro, stlen) == 0)
  284. {
  285. need_cmp = 0;
  286. }
  287. else
  288. {
  289. return 0;
  290. }
  291. }
  292. else
  293. {
  294. if(strncmp(start, macro, len-1))
  295. {
  296. return 0;
  297. }
  298. }
  299. switch(macro[len-1])
  300. {
  301. case 'C':
  302. memcpy(fmt, macro, len-1);
  303. fmt[len-1]='%';
  304. fmt[len]='s';
  305. fmt[len+1]='\0';
  306. for(const char * const *cc=M_conds; *cc; cc++)
  307. {
  308. int r = snprintf(buf, 31, fmt, *cc);
  309. if(r < 0) { return -1; }
  310. buf[r] = '\0';
  311. if(!need_cmp || strncmp(start, buf, r<stlen?r:stlen) == 0)
  312. {
  313. ret[*i] = strdup(buf);
  314. (*i)++;
  315. }
  316. }
  317. break;
  318. case 'S':
  319. if(!need_cmp)
  320. {
  321. ret[*i] = strdup(macro);
  322. ret[*i][len-1] = '\0';
  323. (*i)++;
  324. for(const char *s=M_sizes; *s; s++)
  325. {
  326. if(!(ret[*i] = strdup(macro)))
  327. {
  328. return -1;
  329. }
  330. ret[*i][len-1] = *s;
  331. (*i)++;
  332. }
  333. }
  334. else if(stlen == len-1)
  335. {
  336. ret[*i] = strdup(macro);
  337. ret[*i][len-1] = '\0';
  338. (*i)++;
  339. }
  340. else if(stlen == len)
  341. {
  342. for(const char *s=M_sizes; *s; s++)
  343. {
  344. if(*s == start[len-1])
  345. {
  346. if(!(ret[*i]=strdup(macro)))
  347. {
  348. return -1;
  349. }
  350. ret[*i][len-1]=*s;
  351. (*i)++;
  352. break;
  353. }
  354. }
  355. }
  356. break;
  357. }
  358. return 1;
  359. }
  360. static int _completion_macro_match(const char *macro, const char *text, int mlen)
  361. {
  362. char buf[32];
  363. memcpy(buf, macro, mlen);
  364. buf[mlen] = '\0';
  365. switch(macro[mlen])
  366. {
  367. case 'C':
  368. for(const char * const *cond = M_conds;*cond;cond++)
  369. {
  370. int ret=snprintf(&buf[mlen], 31-mlen, "%s", *cond);
  371. int cmp = strncmp(text, buf, mlen+ret);
  372. if(cmp == 0)
  373. {
  374. return 0;
  375. }
  376. }
  377. break;
  378. case 'S':
  379. for(const char * sz=M_sizes; *sz; sz++)
  380. {
  381. buf[mlen] = *sz;
  382. buf[mlen+1] = '\0';
  383. int cmp = strncmp(text, buf, mlen+1);
  384. if(cmp == 0)
  385. {
  386. return 0;
  387. }
  388. }
  389. break;
  390. }
  391. return 1;
  392. }