5 * Text mode entry with history
13 char *Readline(tReadline *Info)
16 int len, pos, space = 1023-8-8; // optimised for the heap manager
18 int scrollbackPos = Info->NumHistory;
21 ret = malloc( space+1 );
25 // Read In Command Line
27 read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call)
35 read(_stdin, 1, &ch); // Read control character
38 //case 'D': if(pos) pos--; break;
39 //case 'C': if(pos<len) pos++; break;
41 read(_stdin, 1, &ch); // Read control character
47 if( scrollbackPos <= 0 ) break;
50 ret = strdup( Info->History[--scrollbackPos] );
53 while(pos--) write(_stdout, 3, "\x1B[D");
54 write(_stdout, len, ret); pos = len;
55 while(pos++ < oldLen) write(_stdout, 1, " ");
61 if( scrollbackPos >= Info->NumHistory ) break;
64 ret = strdup( Info->History[scrollbackPos++] );
67 while(pos--) write(_stdout, 3, "\x1B[D");
68 write(_stdout, len, ret); pos = len;
69 while(pos++ < oldLen) write(_stdout, 1, " ");
75 write(_stdout, 3, "\x1B[D");
80 write(_stdout, 3, "\x1B[C");
88 if(len <= 0) break; // Protect against underflows
89 write(_stdout, 1, &ch);
90 if(pos == len) { // Simple case of end of string
95 char buf[7] = "\x1B[000D";
96 buf[2] += ((len-pos+1)/100) % 10;
97 buf[3] += ((len-pos+1)/10) % 10;
98 buf[4] += (len-pos+1) % 10;
99 write(_stdout, len-pos, &ret[pos]); // Move Text
100 ch = ' '; write(_stdout, 1, &ch); ch = '\b'; // Clear deleted character
101 write(_stdout, 7, buf); // Update Cursor
103 memmove(&ret[pos-1], &ret[pos], len-pos);
111 //TODO: Implement Tab-Completion
112 //Currently just ignore tabs
119 ret = realloc(ret, space+1);
120 if(!ret) return NULL;
123 // Editing inside the buffer
125 char buf[7] = "\x1B[000D";
126 buf[2] += ((len-pos)/100) % 10;
127 buf[3] += ((len-pos)/10) % 10;
128 buf[4] += (len-pos) % 10;
129 write(_stdout, 1, &ch); // Print new character
130 write(_stdout, len-pos, &ret[pos]); // Move Text
131 write(_stdout, 7, buf); // Update Cursor
132 memmove( &ret[pos+1], &ret[pos], len-pos );
135 write(_stdout, 1, &ch);
148 //if(Length) *Length = len;
151 if( Info->UseHistory )
153 if( strcmp( Info->History[ Info->NumHistory-1 ], ret) != 0 )
157 tmp = realloc( Info->History, Info->NumHistory * sizeof(char*) );
161 Info->History[ Info->NumHistory-1 ] = strdup(ret);