123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- /* Copyright Yann Weber <asmsh@yannweb.net>
- This file is part of asmsh.
-
- asmsh is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- details.
-
- You should have received a copy of the GNU General Public License along
- with asmsh. If not, see <https://www.gnu.org/licenses/>.
- */
- #include "completion.h"
-
-
- char **asmsh_completion(const char *buf, const char *text, int start, int end)
- {
- if(end == 0)
- {
- char **instr, **cmds, **ptr;
- int ilen, clen;
- cmds = asmsh_compl_cmds(text);
- instr = asmsh_compl_instr(text);
-
- ilen=clen=0;
- for(ptr=cmds; *ptr; ptr++) { clen++; }
- for(ptr=instr; *ptr; ptr++) { ilen++; }
-
- ptr = realloc(cmds, sizeof(*cmds) * (ilen+clen+1));
- if(!ptr)
- {
- perror("Unable to realloc all completions");
- free(cmds);
- free(instr);
- return NULL;
- }
- cmds=ptr;
- ptr=&cmds[clen];
- memcpy(&cmds[clen], instr, ilen*sizeof(*instr));
- cmds[ilen+clen]=NULL;
- return cmds;
- }
- if(*buf != '.')
- {
- if(start == 0)
- {
- return asmsh_compl_instr(text);
- }
- return asmsh_compl_arg(buf, text, start);
- }
- return asmsh_compl_cmds(text);
- }
-
-
- /// Replace uppper char leading macro if needed
- static int _completion_upper_macro(const char *macro, const char *start, char **ret, size_t *i);
- char **asmsh_compl_instr(const char *start)
- {
- const size_t icount = sizeof(x86_64_instr)/sizeof(*x86_64_instr);
- size_t i,j;
- char **ret;
- int start_len, cmp;
- ret = malloc(icount * 2 * sizeof(char*));
- if(!ret)
- {
- perror("Unable to allocate completion result");
- return NULL;
- }
- bzero(ret, icount * 2 * sizeof(char*));
- if(!start || !*start)
- {
- i=0;
- for(j=0; j<icount; j++)
- {
- if(x86_64_instr[j].mnemo)
- {
- int mret = _completion_upper_macro(
- x86_64_instr[j].mnemo,
- "", ret, &i);
- if(mret < 0)
- {
- goto err_strdup;
- }
- else if(mret == 0)
- {
- ret[i] = strdup(x86_64_instr[j].mnemo);
- if(!ret[i])
- {
- goto err_strdup;
- }
- i++;
- }
- }
- else
- {
- ret[i] = NULL;
- }
- }
- return ret;
- }
- else
- {
- start_len = strlen(start);
- i=0;
- for(j=0; x86_64_instr[j].mnemo; j++)
- {
-
- int mret = _completion_upper_macro(
- x86_64_instr[j].mnemo,
- start, ret, &i);
- if(mret < 0)
- {
- goto err_strdup;
- }
- else if(mret > 0)
- {
- continue; // handled by macro
- }
- // normal comparison
- cmp = strncmp(start, x86_64_instr[j].mnemo, start_len);
- if(cmp == 0)
- {
- ret[i]=strdup(x86_64_instr[j].mnemo);
- if(!ret[i])
- {
- goto err_strdup;
- }
- i++;
- }
- else if (cmp < 0)
- {
- break;
- }
- }
- }
- return ret;
-
- err_strdup:
- perror("Error while copying string for completion");
- i--;
- do
- {
- free(ret[i]);
- }while(i>0);
- free(ret);
- return NULL;
- }
-
-
- char **asmsh_compl_cmds(const char *start)
- {
- const size_t ccount = sizeof(asmsh_CMDS)/sizeof(*asmsh_CMDS);
- int i, j, slen, cmp;
- char **ret;
- const asmsh_cmd_t *ccmd;
-
- ret = malloc(sizeof(*ret) * (ccount+1));
- if(!ret)
- {
- perror("Unable to allocate cmd completion result");
- return NULL;
- }
- bzero(ret, sizeof(*ret) * (ccount+1));
-
- slen = (!start || !*start)?0:strlen(start);
- j=0;
- for(i=0; i<ccount; i++)
- {
- ccmd = &asmsh_CMDS[i];
- if(!ccmd->str) { break; }
- cmp = slen?strncmp(start, ccmd->str, slen):1;
- if(!slen || !cmp)
- {
- if(!(ret[j] = strdup(ccmd->str)))
- {
- perror("Unable to strdup cmd completion");
- goto errdup;
- }
- j++;
- }
- if(cmp < 0)
- {
- break;
- }
- }
- return ret;
-
- errdup:
- i--;
- for(; i>=0; i--)
- {
- free(ret[i]);
- }
- free(ret);
- return NULL;
- }
-
-
- /// match against macro
- static int _completion_macro_match(const char *macro, const char *buf, int mlen);
- char **asmsh_compl_arg(const char *buf, const char *text, int start)
- {
- const asmsh_compinstr_t *match = NULL;
- int argno;
- int i, tlen;
- int matchlen = 0;
- int macro = 0;
-
- char **ret = malloc(sizeof(char*)*512); /* large enough */
- if(!ret)
- {
- perror("unable to allocate buffer");
- return NULL;
- }
- bzero(ret, sizeof(char*)*512);
-
- tlen = text?strlen(text):0;
-
- for(i=0; i<(sizeof(x86_64_instr)/sizeof(*x86_64_instr))-1; i++)
- {
- int cmp;
- const char *instr = x86_64_instr[i].mnemo;
- int ilen = strlen(instr);
- macro=0;
- if(instr[ilen-1] >= 'A' && instr[ilen-1] <= 'Z')
- {
- ilen--;
- macro = 1;
- cmp = _completion_macro_match(instr, buf, ilen);
- if(cmp < 0)
- {
- goto err;
- }
- }
- else
- {
- cmp = strncmp(buf, instr, ilen);
- }
- if(cmp < 0)
- {
- break;
- }
- else if(cmp > 0)
- {
- continue;
- }
- else if(!macro && (buf[ilen] != ' ' && buf[ilen] != '\t'))
- {
- continue; //not a full match
- }
- // match or approximate macro match
- match = &x86_64_instr[i];
- matchlen = ilen;
- while(buf[matchlen] && buf[matchlen] != ' ' && buf[matchlen] != '\t')
- {
- matchlen++;
- }
- break;
- }
- if(!match)
- {
- ret = malloc(sizeof(char*));
- *ret = NULL;
- return ret;
- }
- argno=0;
- for(i=matchlen; i<=start; i++)
- {
- if(buf[i] == ',')
- {
- argno=1;
- }
- }
-
- int args = argno?match->arg2:match->arg1;
- if(macro && match->mnemo[strlen(match->mnemo)-1] == 'S')
- {
- int wreg=ASHCOMP_REGALL;
- switch(buf[matchlen-1])
- {
- case 'b'://M_sizes[0]:
- wreg = ASHCOMP_REG8;
- break;
- case 'w'://M_sizes[1]:
- wreg = ASHCOMP_REG16;
- break;
- case 'd'://M_sizes[2]:
- wreg = ASHCOMP_REG32;
- break;
- case 'q'://M_sizes[3]:
- wreg = ASHCOMP_REG64;
- break;
- default:
- wreg = ASHCOMP_REGALL;
- break;
- }
- args &= (0xff ^ ASHCOMP_REGALL) | wreg;
- }
-
- i=0;// result index
- for(const asmsh_symtable_elt_t *elt=asmsh_symtable; elt->regs; elt++)
- {
- if(!(elt->flag & args))
- {
- continue;
- }
- if(elt->flag == ASHCOMP_IMM && tlen > 1 && *text=='$')
- {
- // immediate value handling
- continue;
- }
- for(const char * const *r=elt->regs; *r; r++)
- {
- if(tlen && strncmp(text, *r, tlen))
- {
- continue;
- }
- if(!(ret[i] = strdup(*r)))
- {
- goto err_strdup;
- }
- if(argno == 0 && **r == '%')
- {
- //full match on arg0 adding ","
- char *tmp = ret[i];
- char _buf[32];
- snprintf(_buf, 31, "%s,", tmp);
- ret[i] = strdup(_buf);
- }
- i++;
- }
- }
-
- return ret;
-
- err_strdup:
- perror("Unable to strdup arg match");
- i--;
- while(i>=0)
- {
- free(ret[i]);
- i--;
- }
- err:
- free(ret);
- return NULL;
-
- }
-
-
-
- static int _completion_upper_macro(const char *macro, const char *start, char **ret, size_t *i)
- {
- int stlen, len, need_cmp;
- char buf[32]; /* large enough */
- char fmt[32];
- len = strlen(macro);
- stlen = strlen(start);
-
- switch(macro[len-1])
- {
- case 'C':
- case 'S':
- break;
- default:
- return 0;
- }
-
- need_cmp=1;
- if(len > stlen)
- {
- if(strncmp(start, macro, stlen) == 0)
- {
- need_cmp = 0;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- if(strncmp(start, macro, len-1))
- {
- return 0;
- }
- }
-
- switch(macro[len-1])
- {
- case 'C':
- memcpy(fmt, macro, len-1);
- fmt[len-1]='%';
- fmt[len]='s';
- fmt[len+1]='\0';
- for(const char * const *cc=M_conds; *cc; cc++)
- {
- int r = snprintf(buf, 31, fmt, *cc);
- if(r < 0) { return -1; }
- buf[r] = '\0';
- if(!need_cmp || strncmp(start, buf, r<stlen?r:stlen) == 0)
- {
- ret[*i] = strdup(buf);
- (*i)++;
- }
- }
- break;
- case 'S':
- if(!need_cmp)
- {
- ret[*i] = strdup(macro);
- ret[*i][len-1] = '\0';
- (*i)++;
- for(const char *s=M_sizes; *s; s++)
- {
- if(!(ret[*i] = strdup(macro)))
- {
- return -1;
- }
- ret[*i][len-1] = *s;
- (*i)++;
- }
- }
- else if(stlen == len-1)
- {
- ret[*i] = strdup(macro);
- ret[*i][len-1] = '\0';
- (*i)++;
- }
- else if(stlen == len)
- {
- for(const char *s=M_sizes; *s; s++)
- {
- if(*s == start[len-1])
- {
- if(!(ret[*i]=strdup(macro)))
- {
- return -1;
- }
- ret[*i][len-1]=*s;
- (*i)++;
- break;
- }
- }
- }
- break;
- }
- return 1;
- }
-
-
- static int _completion_macro_match(const char *macro, const char *text, int mlen)
- {
- char buf[32];
- memcpy(buf, macro, mlen);
- buf[mlen] = '\0';
- switch(macro[mlen])
- {
- case 'C':
- for(const char * const *cond = M_conds;*cond;cond++)
- {
- int ret=snprintf(&buf[mlen], 31-mlen, "%s", *cond);
- int cmp = strncmp(text, buf, mlen+ret);
- if(cmp == 0)
- {
- return 0;
- }
- }
- break;
- case 'S':
- for(const char * sz=M_sizes; *sz; sz++)
- {
- buf[mlen] = *sz;
- buf[mlen+1] = '\0';
- int cmp = strncmp(text, buf, mlen+1);
- if(cmp == 0)
- {
- return 0;
- }
- }
- break;
- }
- return 1;
- }
-
- #if HAVE_LIBREADLINE == 1
- static char **_asmsh_completion_res;
- static char **_asmsh_completion_cur;
- static int _asmsh_completion_st[2];
- extern int rl_attempted_completion_over;
- extern const char* rl_special_prefixes;
- extern const char* rl_basic_word_break_characters;
- char *asmsh_rl_completion_gen(const char *text, int state)
- {
- if(!state)
- {
- _asmsh_completion_res = asmsh_completion(rl_line_buffer,
- text,
- _asmsh_completion_st[0],
- _asmsh_completion_st[1]);
- _asmsh_completion_cur = _asmsh_completion_res;
- if(!_asmsh_completion_res)
- {
- return NULL;
- }
- }
- char *res = *_asmsh_completion_cur;
- if(!*_asmsh_completion_cur)
- {
- free(_asmsh_completion_res);
- }
- else
- {
- *_asmsh_completion_cur = NULL;
- _asmsh_completion_cur++;
- }
- return res;
- }
- char **asmsh_rl_completion(const char *text, int start, int end)
- {
- rl_attempted_completion_over = 1;
- _asmsh_completion_st[0]=start;
- _asmsh_completion_st[1]=end;
- return rl_completion_matches(text, asmsh_rl_completion_gen);
- }
- #endif
|