in bundles/shell/shell_tui/src/shell_tui.c [304:416]
static int shellTui_parseInputForControl(shell_tui_t* shellTui, shell_context_t* ctx) {
char* buffer = ctx->buffer;
char* in = ctx->in;
char* dline = ctx->dline;
celix_shell_tui_history_t* hist = ctx->hist;
int pos = ctx->pos;
const char* line = NULL;
int nr_chars = (int)read(shellTui->inputFd, buffer, LINE_SIZE-pos-1);
for(int bufpos = 0; bufpos < nr_chars; bufpos++) {
if (buffer[bufpos] == KEY_ESC1 && buffer[bufpos+1] == KEY_ESC2) {
switch (buffer[bufpos+2]) {
case KEY_UP:
line = celix_shellTuiHistory_getPrevLine(hist);
if (line) {
strncpy(in, line, LINE_SIZE);
pos = (int)strlen(in);
writeLine(shellTui, line, pos);
}
break;
case KEY_DOWN:
line = celix_shellTuiHistory_getNextLine(hist);
if (line) {
strncpy(in, line, LINE_SIZE);
pos = (int)strlen(in);
writeLine(shellTui, line, pos);
}
break;
case KEY_RIGHT:
if (pos < strlen(in)) {
pos++;
}
writeLine(shellTui, in, pos);
break;
case KEY_LEFT:
if (pos > 0) {
pos--;
}
writeLine(shellTui, in, pos);
break;
case KEY_DEL1:
if(buffer[bufpos+3] == KEY_DEL2) {
bufpos++; // delete cmd takes 4 chars
int len = strlen(in);
if (pos < len) {
for (int i = pos; i <= len; i++) {
in[i] = in[i + 1];
}
}
writeLine(shellTui, in, pos);
}
break;
default:
// Unsupported char, do nothing
break;
}
bufpos+=2;
continue;
} else if (buffer[bufpos] == KEY_BACKSPACE) { // backspace
if(pos > 0) {
int len = strlen(in);
for(int i = pos-1; i <= len; i++) {
in[i] = in[i+1];
}
pos--;
}
writeLine(shellTui, in, pos);
continue;
} else if(buffer[bufpos] == KEY_TAB) {
celixThreadMutex_lock(&shellTui->mutex);
pos = autoComplete(shellTui, shellTui->shell, in, pos, LINE_SIZE);
celixThreadMutex_unlock(&shellTui->mutex);
continue;
} else if (buffer[bufpos] != KEY_ENTER) { //not end of line -> text
if (in[pos] == '\0') {
in[pos+1] = '\0';
}
in[pos] = buffer[bufpos];
pos++;
writeLine(shellTui, in, pos);
fflush(shellTui->output);
continue;
}
//parse enter
writeLine(shellTui, in, pos);
fprintf(shellTui->output, "\n");
remove_newlines(in);
celix_shellTuiHistory_addLine(hist, in);
memset(dline, 0, LINE_SIZE);
strncpy(dline, in, LINE_SIZE);
pos = 0;
in[pos] = '\0';
line = celix_utils_trimInPlace(dline);
if ((strlen(line) == 0)) {
continue;
}
celix_shellTuiHistory_lineReset(hist);
celixThreadMutex_lock(&shellTui->mutex);
if (shellTui->shell != NULL) {
shellTui->shell->executeCommand(shellTui->shell->handle, line, shellTui->output, shellTui->error);
} else {
fprintf(shellTui->output, "%s\n", SHELL_NOT_AVAILABLE_MSG);
}
celixThreadMutex_unlock(&shellTui->mutex);
break;
} // for
ctx->pos = pos;
return nr_chars;
}