9 static struct termios save_termios;
10 static int ttysavefd = -1;
11 static enum { CBREAK, RESET } ttystate = RESET;
13 #define HISTORY_SIZE 100
14 char history[HISTORY_SIZE][255];
16 int tty_cbreak(
int fd)
19 if(tcgetattr(fd, & save_termios) < 0)
24 buf.c_lflag &= ~(ECHO | ICANON);
28 if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
38 if(ttystate != CBREAK)
return 0;
39 if(tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
54 static int c_line = 0;
56 void get_line(
char *buff)
76 if(tty_cbreak(STDIN_FILENO) < 0)
92 memset(history[c_line],0,
sizeof(history[c_line]));
94 while( (i = read(STDIN_FILENO, &c, 1)) == 1)
106 if(c_char == 0)
break;
107 for(t=c_char;t<=l_char;t++)
109 history[c_line][t-1] = history[c_line][t];
111 history[c_line][l_char] =
'\0';
117 for(t=c_char;t<l_char;t++)
119 putchar(history[c_line][t]);
122 for(t=c_char;t<l_char+1;t++)
130 i = read(STDIN_FILENO, &c, 1);
131 i = read(STDIN_FILENO, &c, 1);
155 if(h_line < 0) h_line = HISTORY_SIZE-1;
157 for(t=c_char;t>0;t--)
161 for(t=0;t<l_char;t++)
165 for(t=l_char;t>0;t--)
171 memset(history[c_line],0,
sizeof(history[c_line]));
172 while(history[h_line][c_char])
174 putchar(history[h_line][c_char]);
175 history[c_line][c_char] = history[h_line][c_char];
183 if(h_line >= HISTORY_SIZE) h_line = 0;
184 for(t=c_char;t>0;t--)
188 for(t=0;t<l_char;t++)
192 for(t=l_char;t>0;t--)
198 memset(history[c_line],0,
sizeof(history[c_line]));
199 while(history[h_line][c_char])
201 putchar(history[h_line][c_char]);
202 history[c_line][c_char] = history[h_line][c_char];
213 for(t=l_char;t>c_char;t--)
215 history[c_line][t] = history[c_line][t-1];
217 history[c_line][c_char] = c;
222 for(t=c_char-1;t<l_char;t++)
224 putchar(history[c_line][t]);
226 for(t=c_char;t<l_char;t++)
238 strcpy(buff,history[c_line]);
241 if(c_line >= HISTORY_SIZE) c_line = 0;
243 tty_reset(STDIN_FILENO);
246 char one_line_buff[255];
248 void get_one_line(
char *buff)
251 if(tty_cbreak(STDIN_FILENO) < 0)
265 memset(one_line_buff,0,
sizeof(one_line_buff));
267 while( (i = read(STDIN_FILENO, &c, 1)) == 1)
279 if(c_char == 0)
break;
280 for(t=c_char;t<=l_char;t++)
282 one_line_buff[t-1] = one_line_buff[t];
284 one_line_buff[l_char] =
'\0';
290 for(t=c_char;t<l_char;t++)
292 putchar(one_line_buff[t]);
295 for(t=c_char;t<l_char+1;t++)
303 i = read(STDIN_FILENO, &c, 1);
304 i = read(STDIN_FILENO, &c, 1);
331 for(t=l_char;t>c_char;t--)
333 one_line_buff[t] = one_line_buff[t-1];
335 one_line_buff[c_char] = c;
340 for(t=c_char-1;t<l_char;t++)
342 putchar(one_line_buff[t]);
344 for(t=c_char;t<l_char;t++)
356 strcpy(buff,one_line_buff);
358 tty_reset(STDIN_FILENO);