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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. char **instr, **cmds, **ptr;
  19. int ilen, clen;
  20. cmds = asmsh_compl_cmds(text);
  21. instr = asmsh_compl_instr(text);
  22. ilen=clen=0;
  23. for(ptr=cmds; *ptr; ptr++) { clen++; }
  24. for(ptr=instr; *ptr; ptr++) { ilen++; }
  25. ptr = realloc(cmds, sizeof(*cmds) * (ilen+clen+1));
  26. if(!ptr)
  27. {
  28. perror("Unable to realloc all completions");
  29. free(cmds);
  30. free(instr);
  31. return NULL;
  32. }
  33. cmds=ptr;
  34. ptr=&cmds[clen];
  35. memcpy(&cmds[clen], instr, ilen*sizeof(*instr));
  36. cmds[ilen+clen]=NULL;
  37. return cmds;
  38. }
  39. if(*buf != '.')
  40. {
  41. if(start == 0)
  42. {
  43. return asmsh_compl_instr(text);
  44. }
  45. return asmsh_compl_arg(buf, text, start);
  46. }
  47. return asmsh_compl_cmds(text);
  48. }
  49. /// Replace uppper char leading macro if needed
  50. static int _completion_upper_macro(const char *macro, const char *start, char **ret, size_t *i);
  51. char **asmsh_compl_instr(const char *start)
  52. {
  53. const size_t icount = sizeof(x86_64_instr)/sizeof(*x86_64_instr);
  54. size_t i,j;
  55. char **ret;
  56. int start_len, cmp;
  57. ret = malloc(icount * 2 * sizeof(char*));
  58. if(!ret)
  59. {
  60. perror("Unable to allocate completion result");
  61. return NULL;
  62. }
  63. bzero(ret, icount * 2 * sizeof(char*));
  64. if(!start || !*start)
  65. {
  66. i=0;
  67. for(j=0; j<icount; j++)
  68. {
  69. if(x86_64_instr[j].mnemo)
  70. {
  71. int mret = _completion_upper_macro(
  72. x86_64_instr[j].mnemo,
  73. "", ret, &i);
  74. if(mret < 0)
  75. {
  76. goto err_strdup;
  77. }
  78. else if(mret == 0)
  79. {
  80. ret[i] = strdup(x86_64_instr[j].mnemo);
  81. if(!ret[i])
  82. {
  83. goto err_strdup;
  84. }
  85. i++;
  86. }
  87. }
  88. else
  89. {
  90. ret[i] = NULL;
  91. }
  92. }
  93. return ret;
  94. }
  95. else
  96. {
  97. start_len = strlen(start);
  98. i=0;
  99. for(j=0; x86_64_instr[j].mnemo; j++)
  100. {
  101. int mret = _completion_upper_macro(
  102. x86_64_instr[j].mnemo,
  103. start, ret, &i);
  104. if(mret < 0)
  105. {
  106. goto err_strdup;
  107. }
  108. else if(mret > 0)
  109. {
  110. continue; // handled by macro
  111. }
  112. // normal comparison
  113. cmp = strncmp(start, x86_64_instr[j].mnemo, start_len);
  114. if(cmp == 0)
  115. {
  116. ret[i]=strdup(x86_64_instr[j].mnemo);
  117. if(!ret[i])
  118. {
  119. goto err_strdup;
  120. }
  121. i++;
  122. }
  123. else if (cmp < 0)
  124. {
  125. break;
  126. }
  127. }
  128. }
  129. return ret;
  130. err_strdup:
  131. perror("Error while copying string for completion");
  132. i--;
  133. do
  134. {
  135. free(ret[i]);
  136. }while(i>0);
  137. free(ret);
  138. return NULL;
  139. }
  140. char **asmsh_compl_cmds(const char *start)
  141. {
  142. const size_t ccount = sizeof(asmsh_CMDS)/sizeof(*asmsh_CMDS);
  143. int i, j, slen, cmp;
  144. char **ret;
  145. const asmsh_cmd_t *ccmd;
  146. ret = malloc(sizeof(*ret) * (ccount+1));
  147. if(!ret)
  148. {
  149. perror("Unable to allocate cmd completion result");
  150. return NULL;
  151. }
  152. bzero(ret, sizeof(*ret) * (ccount+1));
  153. slen = (!start || !*start)?0:strlen(start);
  154. j=0;
  155. for(i=0; i<ccount; i++)
  156. {
  157. ccmd = &asmsh_CMDS[i];
  158. if(!ccmd->str) { break; }
  159. cmp = slen?strncmp(start, ccmd->str, slen):1;
  160. if(!slen || !cmp)
  161. {
  162. if(!(ret[j] = strdup(ccmd->str)))
  163. {
  164. perror("Unable to strdup cmd completion");
  165. goto errdup;
  166. }
  167. j++;
  168. }
  169. if(cmp < 0)
  170. {
  171. break;
  172. }
  173. }
  174. return ret;
  175. errdup:
  176. i--;
  177. for(; i>=0; i--)
  178. {
  179. free(ret[i]);
  180. }
  181. free(ret);
  182. return NULL;
  183. }
  184. /// match against macro
  185. static int _completion_macro_match(const char *macro, const char *buf, int mlen);
  186. char **asmsh_compl_arg(const char *buf, const char *text, int start)
  187. {
  188. const asmsh_compinstr_t *match = NULL;
  189. int argno;
  190. int i, tlen;
  191. int matchlen = 0;
  192. int macro = 0;
  193. char **ret = malloc(sizeof(char*)*512); /* large enough */
  194. if(!ret)
  195. {
  196. perror("unable to allocate buffer");
  197. return NULL;
  198. }
  199. bzero(ret, sizeof(char*)*512);
  200. tlen = text?strlen(text):0;
  201. for(i=0; i<(sizeof(x86_64_instr)/sizeof(*x86_64_instr))-1; i++)
  202. {
  203. int cmp;
  204. const char *instr = x86_64_instr[i].mnemo;
  205. int ilen = strlen(instr);
  206. macro=0;
  207. if(instr[ilen-1] >= 'A' && instr[ilen-1] <= 'Z')
  208. {
  209. ilen--;
  210. macro = 1;
  211. cmp = _completion_macro_match(instr, buf, ilen);
  212. if(cmp < 0)
  213. {
  214. goto err;
  215. }
  216. }
  217. else
  218. {
  219. cmp = strncmp(buf, instr, ilen);
  220. }
  221. if(cmp < 0)
  222. {
  223. break;
  224. }
  225. else if(cmp > 0)
  226. {
  227. continue;
  228. }
  229. else if(!macro && (buf[ilen] != ' ' && buf[ilen] != '\t'))
  230. {
  231. continue; //not a full match
  232. }
  233. // match or approximate macro match
  234. match = &x86_64_instr[i];
  235. matchlen = ilen;
  236. while(buf[matchlen] && buf[matchlen] != ' ' && buf[matchlen] != '\t')
  237. {
  238. matchlen++;
  239. }
  240. break;
  241. }
  242. if(!match)
  243. {
  244. ret = malloc(sizeof(char*));
  245. *ret = NULL;
  246. return ret;
  247. }
  248. argno=0;
  249. for(i=matchlen; i<=start; i++)
  250. {
  251. if(buf[i] == ',')
  252. {
  253. argno=1;
  254. }
  255. }
  256. int args = argno?match->arg2:match->arg1;
  257. if(macro && match->mnemo[strlen(match->mnemo)-1] == 'S')
  258. {
  259. int wreg=ASHCOMP_REGALL;
  260. switch(buf[matchlen-1])
  261. {
  262. case 'b'://M_sizes[0]:
  263. wreg = ASHCOMP_REG8;
  264. break;
  265. case 'w'://M_sizes[1]:
  266. wreg = ASHCOMP_REG16;
  267. break;
  268. case 'd'://M_sizes[2]:
  269. wreg = ASHCOMP_REG32;
  270. break;
  271. case 'q'://M_sizes[3]:
  272. wreg = ASHCOMP_REG64;
  273. break;
  274. default:
  275. wreg = ASHCOMP_REGALL;
  276. break;
  277. }
  278. args &= (0xff ^ ASHCOMP_REGALL) | wreg;
  279. }
  280. i=0;// result index
  281. for(const asmsh_symtable_elt_t *elt=asmsh_symtable; elt->regs; elt++)
  282. {
  283. if(!(elt->flag & args))
  284. {
  285. continue;
  286. }
  287. if(elt->flag == ASHCOMP_IMM && tlen > 1 && *text=='$')
  288. {
  289. // immediate value handling
  290. continue;
  291. }
  292. for(const char * const *r=elt->regs; *r; r++)
  293. {
  294. if(tlen && strncmp(text, *r, tlen))
  295. {
  296. continue;
  297. }
  298. if(!(ret[i] = strdup(*r)))
  299. {
  300. goto err_strdup;
  301. }
  302. if(argno == 0 && **r == '%')
  303. {
  304. //full match on arg0 adding ","
  305. char *tmp = ret[i];
  306. char _buf[32];
  307. snprintf(_buf, 31, "%s,", tmp);
  308. ret[i] = strdup(_buf);
  309. }
  310. i++;
  311. }
  312. }
  313. return ret;
  314. err_strdup:
  315. perror("Unable to strdup arg match");
  316. i--;
  317. while(i>=0)
  318. {
  319. free(ret[i]);
  320. i--;
  321. }
  322. err:
  323. free(ret);
  324. return NULL;
  325. }
  326. static int _completion_upper_macro(const char *macro, const char *start, char **ret, size_t *i)
  327. {
  328. int stlen, len, need_cmp;
  329. char buf[32]; /* large enough */
  330. char fmt[32];
  331. len = strlen(macro);
  332. stlen = strlen(start);
  333. switch(macro[len-1])
  334. {
  335. case 'C':
  336. case 'S':
  337. break;
  338. default:
  339. return 0;
  340. }
  341. need_cmp=1;
  342. if(len > stlen)
  343. {
  344. if(strncmp(start, macro, stlen) == 0)
  345. {
  346. need_cmp = 0;
  347. }
  348. else
  349. {
  350. return 0;
  351. }
  352. }
  353. else
  354. {
  355. if(strncmp(start, macro, len-1))
  356. {
  357. return 0;
  358. }
  359. }
  360. switch(macro[len-1])
  361. {
  362. case 'C':
  363. memcpy(fmt, macro, len-1);
  364. fmt[len-1]='%';
  365. fmt[len]='s';
  366. fmt[len+1]='\0';
  367. for(const char * const *cc=M_conds; *cc; cc++)
  368. {
  369. int r = snprintf(buf, 31, fmt, *cc);
  370. if(r < 0) { return -1; }
  371. buf[r] = '\0';
  372. if(!need_cmp || strncmp(start, buf, r<stlen?r:stlen) == 0)
  373. {
  374. ret[*i] = strdup(buf);
  375. (*i)++;
  376. }
  377. }
  378. break;
  379. case 'S':
  380. if(!need_cmp)
  381. {
  382. ret[*i] = strdup(macro);
  383. ret[*i][len-1] = '\0';
  384. (*i)++;
  385. for(const char *s=M_sizes; *s; s++)
  386. {
  387. if(!(ret[*i] = strdup(macro)))
  388. {
  389. return -1;
  390. }
  391. ret[*i][len-1] = *s;
  392. (*i)++;
  393. }
  394. }
  395. else if(stlen == len-1)
  396. {
  397. ret[*i] = strdup(macro);
  398. ret[*i][len-1] = '\0';
  399. (*i)++;
  400. }
  401. else if(stlen == len)
  402. {
  403. for(const char *s=M_sizes; *s; s++)
  404. {
  405. if(*s == start[len-1])
  406. {
  407. if(!(ret[*i]=strdup(macro)))
  408. {
  409. return -1;
  410. }
  411. ret[*i][len-1]=*s;
  412. (*i)++;
  413. break;
  414. }
  415. }
  416. }
  417. break;
  418. }
  419. return 1;
  420. }
  421. static int _completion_macro_match(const char *macro, const char *text, int mlen)
  422. {
  423. char buf[32];
  424. memcpy(buf, macro, mlen);
  425. buf[mlen] = '\0';
  426. switch(macro[mlen])
  427. {
  428. case 'C':
  429. for(const char * const *cond = M_conds;*cond;cond++)
  430. {
  431. int ret=snprintf(&buf[mlen], 31-mlen, "%s", *cond);
  432. int cmp = strncmp(text, buf, mlen+ret);
  433. if(cmp == 0)
  434. {
  435. return 0;
  436. }
  437. }
  438. break;
  439. case 'S':
  440. for(const char * sz=M_sizes; *sz; sz++)
  441. {
  442. buf[mlen] = *sz;
  443. buf[mlen+1] = '\0';
  444. int cmp = strncmp(text, buf, mlen+1);
  445. if(cmp == 0)
  446. {
  447. return 0;
  448. }
  449. }
  450. break;
  451. }
  452. return 1;
  453. }
  454. #if HAVE_LIBREADLINE == 1
  455. static char **_asmsh_completion_res;
  456. static char **_asmsh_completion_cur;
  457. static int _asmsh_completion_st[2];
  458. extern int rl_attempted_completion_over;
  459. extern const char* rl_special_prefixes;
  460. extern const char* rl_basic_word_break_characters;
  461. char *asmsh_rl_completion_gen(const char *text, int state)
  462. {
  463. if(!state)
  464. {
  465. _asmsh_completion_res = asmsh_completion(rl_line_buffer,
  466. text,
  467. _asmsh_completion_st[0],
  468. _asmsh_completion_st[1]);
  469. _asmsh_completion_cur = _asmsh_completion_res;
  470. if(!_asmsh_completion_res)
  471. {
  472. return NULL;
  473. }
  474. }
  475. char *res = *_asmsh_completion_cur;
  476. if(!*_asmsh_completion_cur)
  477. {
  478. free(_asmsh_completion_res);
  479. }
  480. else
  481. {
  482. *_asmsh_completion_cur = NULL;
  483. _asmsh_completion_cur++;
  484. }
  485. return res;
  486. }
  487. char **asmsh_rl_completion(const char *text, int start, int end)
  488. {
  489. rl_attempted_completion_over = 1;
  490. _asmsh_completion_st[0]=start;
  491. _asmsh_completion_st[1]=end;
  492. return rl_completion_matches(text, asmsh_rl_completion_gen);
  493. }
  494. #endif