[go: up one dir, main page]

Menu

[r9]: / trunk / src / cli / exec / gets.c  Maximize  Restore  History

Download this file

167 lines (155 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (C) 2012-2013 RCP100 Team (rcpteam@yahoo.com)
*
* This file is part of RCP100 project
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "cli.h"
// ANSI esc sequences
static char save_cursor[4] = { 0x1b, 0x5b, 's', 0};
static char restore_cursor[4] = { 0x1b, 0x5b, 'u', 0};
static char clear_line[4] = { 0x1b, 0x5b, 'K', 0};
static char cursor_back[4] = { 0x1b, 0x5b, 'D', 0};
static char cursor_forward[4] = { 0x1b, 0x5b, 'C', 0};
//--------------------------
//Keyboard
//--------------------------
// arrow up 1b 5b A
// arrow down 1b 5b B
// arrow right1b 5b C
// arrow left 1b 5b D
// home 1b 4f 48
// end 1b 4f 46
// delete 1b 5b 33
// backspace 7f
//--------------------------
int cliGets(char *buf, int size) {
char *ptr = buf + strlen(buf);
int rv = GETS_OK;
int c;
// clear the string memory after \0
int i;
for (i = strlen(buf); i < size; i++)
*(buf + i) = '\0';
// process chars as they are coming in
while ((c = cliTimeoutGetc()) != '\n') {
if (c == '?') {
// ? is never printed
rv = GETS_QUESTION;
}
else if (c == '\t') {
// TAB is never printed
rv = GETS_TAB;
}
else if (c == 0x7f) { // backspace
if (ptr <= buf) {
ptr = buf;
continue;
}
// move the string
ptr--;
memmove(ptr, ptr + 1, strlen(ptr + 1) + 1); // +1 for \0
// print new string on the screen and position the cursor
printf("%s%s%s%s%s", cursor_back, save_cursor, ptr, clear_line, restore_cursor );
fflush(0);
}
// ansi esc
else if (c == 0x1b) {
int c2 = cliTimeoutGetc();
int c3 = cliTimeoutGetc();
//printf("%x %x\n", c2, c3);
// arrow up
if (c2 == 0x5b && c3 == 'A') {
rv = GETS_ARROW_UP;
}
// arrow down
else if (c2 == 0x5b && c3 == 'B') {
rv = GETS_ARROW_DOWN;
}
// arrow right
else if (c2 == 0x5b && c3 == 'C') {
if (*ptr == '\0') // we seem to be at the end of the buffer
continue;
ptr++;
printf("%s", cursor_forward);
}
// arrow left
else if (c2 == 0x5b && c3 == 'D') {
if (ptr <= buf) { // we seem to be at the beginning of the buffer
ptr = buf;
continue;
}
ptr--;
printf("%s", cursor_back);
}
// home
else if (c2 == 0x4f && c3 == 0x48) {
if (ptr <= buf) { // we seem to be at the beginning of the buffer
ptr = buf;
continue;
}
while (ptr > buf) {
printf("%s", cursor_back);
--ptr;
}
}
// end
else if (c2 == 0x4f && c3 == 0x46) {
if (*ptr == '\0') // we seem to be at the end of the buffer
continue;
while (*ptr != '\0') {
printf("%s", cursor_forward);
++ptr;
}
}
// delete
else if (c2 == 0x5b && c3 == 0x33) {
c2 = cliTimeoutGetc(); // finish the esc sequence
if (*ptr == '\0') // we don't delete at the end of buffer
continue;
// move the string
memmove(ptr, ptr + 1, strlen(ptr + 1) + 1); // +1 for \0
// print new string on the screen and position the cursor
printf("%s%s%s%s", save_cursor, ptr, clear_line, restore_cursor );
}
fflush(0);
}
else if (*ptr == '\0') { // adding chars at the end of the buffer
*ptr++ = (char) c;
char b[2];
b[0] = (char) c;
b[1] = '\0';
printf("%s", b);
fflush(0);
}
else { // inserting chars in the buffer
char tmp[CLI_MAX_LINE];
*tmp = (char) c;
strcpy(tmp + 1, ptr);
strcpy(ptr, tmp);
printf("%s", save_cursor);
printf("%s", tmp);
printf("%s", restore_cursor);
ptr++;
printf("%s", cursor_forward);
fflush(0);
}
fflush(0);
if (rv !=0)
break;
}
return rv;
}