fix(shell): fixed not last word matched
This commit is contained in:
parent
f9b08e5c21
commit
43e6d441bc
|
@ -969,38 +969,43 @@ bool matchVarWord(SWord* word1, SWord* word2) {
|
||||||
// ------------------- match words --------------------------
|
// ------------------- match words --------------------------
|
||||||
//
|
//
|
||||||
|
|
||||||
// compare command cmd1 come from shellCommands , cmd2 come from user input
|
// compare command cmdPattern come from shellCommands , cmdInput come from user input
|
||||||
int32_t compareCommand(SWords* cmd1, SWords* cmd2) {
|
int32_t compareCommand(SWords* cmdPattern, SWords* cmdInput) {
|
||||||
SWord* word1 = cmd1->head;
|
SWord* wordPattern = cmdPattern->head;
|
||||||
SWord* word2 = cmd2->head;
|
SWord* wordInput = cmdInput->head;
|
||||||
|
|
||||||
if (word1 == NULL || word2 == NULL) {
|
if (wordPattern == NULL || wordInput == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int32_t i = 0; i < cmd1->count; i++) {
|
for (int32_t i = 0; i < cmdPattern->count; i++) {
|
||||||
if (word1->type == WT_TEXT) {
|
if (wordPattern->type == WT_TEXT) {
|
||||||
// WT_TEXT match
|
// WT_TEXT match
|
||||||
if (word1->len == word2->len) {
|
if (wordPattern->len == wordInput->len) {
|
||||||
if (strncasecmp(word1->word, word2->word, word1->len) != 0) return -1;
|
if (strncasecmp(wordPattern->word, wordInput->word, wordPattern->len) != 0) return -1;
|
||||||
} else if (word1->len < word2->len) {
|
} else if (wordPattern->len < wordInput->len) {
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
// word1->len > word2->len
|
// wordPattern->len > wordInput->len
|
||||||
if (strncasecmp(word1->word, word2->word, word2->len) == 0) {
|
if (strncasecmp(wordPattern->word, wordInput->word, wordInput->len) == 0) {
|
||||||
cmd1->matchIndex = i;
|
if (i + 1 == cmdInput->count) {
|
||||||
cmd1->matchLen = word2->len;
|
// last word return match
|
||||||
|
cmdPattern->matchIndex = i;
|
||||||
|
cmdPattern->matchLen = wordInput->len;
|
||||||
return i;
|
return i;
|
||||||
} else {
|
} else {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// WT_VAR auto match any one word
|
// WT_VAR auto match any one word
|
||||||
if (word2->next == NULL) { // input words last one
|
if (wordInput->next == NULL) { // input words last one
|
||||||
if (matchVarWord(word1, word2)) {
|
if (matchVarWord(wordPattern, wordInput)) {
|
||||||
cmd1->matchIndex = i;
|
cmdPattern->matchIndex = i;
|
||||||
cmd1->matchLen = word2->len;
|
cmdPattern->matchLen = wordInput->len;
|
||||||
varMode = true;
|
varMode = true;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -1009,9 +1014,9 @@ int32_t compareCommand(SWords* cmd1, SWords* cmd2) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// move next
|
// move next
|
||||||
word1 = word1->next;
|
wordPattern = wordPattern->next;
|
||||||
word2 = word2->next;
|
wordInput = wordInput->next;
|
||||||
if (word1 == NULL || word2 == NULL) {
|
if (wordPattern == NULL || wordInput == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue