MCPcopy Create free account
hub / github.com/assaultcube/AC / commandcomplete

Function commandcomplete

source/src/command.cpp:833–918  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

831COMMANDN(nickcomplete, addnickcomplete, "s");
832
833void commandcomplete(char *s, bool reversedirection)
834{ // s is required to be of size "string"!
835 static int completeidx;
836 if(*s != '/')
837 {
838 string t;
839 copystring(t, s);
840 copystring(s, "/");
841 concatstring(s, t);
842 }
843 if(!s[1]) return;
844
845 // find start position of last command
846 char *cmd = strrchr(s, ';'); // find last ';' (this will not always work properly, because it doesn't take quoted texts into account)
847 if(!cmd) cmd = s; // no ';' found: command starts after '/'
848
849 char *openblock = strrchr(cmd + 1, '('), *closeblock = strrchr(cmd + 1, ')'); // find last open and closed parenthesis
850 if(openblock && (!closeblock || closeblock < openblock)) cmd = openblock; // found opening parenthesis inside the command: assume, a new command starts here
851
852 cmd += strspn(cmd + 1, " ") + 1; // skip blanks and one of "/;( ", cmd now points to the first char of the command
853
854 // check, if the command is complete, and we want argument completion instead
855 char *arg = strrchr(cmd, ' '); // find last space in command -> if there is one, we use argument completion
856
857 completeval *cdata = NULL;
858 if(arg) // full command is present
859 { // extract command name to find argument list
860 string command;
861 copystring(command, cmd);
862 command[strcspn(cmd, " ")] = '\0';
863 completeval **hascomplete = completions.access(command);
864 if(hascomplete) cdata = *hascomplete;
865
866 if(completesize < 0 && cdata && cdata->type == COMPLETE_FILE)
867 { // get directory contents on first run
868 cdata->list.deletearrays();
869 vector<char *> files;
870 loopv(cdata->dirlist)
871 {
872 listfiles(cdata->dirlist[i], cdata->ext, files, stringsort);
873 loopv(files) cdata->list.add(files[i]);
874 files.setsize(0);
875 }
876 }
877 }
878
879 char *cp = arg ? arg + 1 : cmd; // part of string to complete
880 bool firstrun = false;
881 if(completesize < 0)
882 { // first run since resetcomplete()
883 completesize = (int)strlen(cp);
884 completeidx = reversedirection ? 0 : -1;
885 firstrun = true;
886 }
887
888 if(!arg)
889 { // commandname completion
890 vector<const char *> matchingidents;

Callers 1

completeFunction · 0.85

Calls 12

copystringFunction · 0.85
concatstringFunction · 0.85
listfilesFunction · 0.85
minFunction · 0.85
nickcompleteFunction · 0.85
accessMethod · 0.80
deletearraysMethod · 0.80
setsizeMethod · 0.80
sortMethod · 0.80
loopvFunction · 0.70
addMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected