aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--dumbsh.c52
1 files changed, 28 insertions, 24 deletions
diff --git a/dumbsh.c b/dumbsh.c
index a4a475f..d2a265b 100644
--- a/dumbsh.c
+++ b/dumbsh.c
@@ -1,4 +1,4 @@
1/* dumbsh.c - A really dumb shell, to demonstrate handlekeys usage. 1/* dumbsh.c - A really dumb shell, to demonstrate handle_keys usage.
2 * 2 *
3 * Copyright 2014 David Seikel <won_fang@yahoo.com.au> 3 * Copyright 2014 David Seikel <won_fang@yahoo.com.au>
4 * 4 *
@@ -30,7 +30,7 @@ struct keyCommand
30GLOBALS( 30GLOBALS(
31 unsigned h, w; 31 unsigned h, w;
32 int x, y; 32 int x, y;
33 struct double_list *current; 33 struct double_list *history;
34) 34)
35 35
36#define TT this.dumbsh 36#define TT this.dumbsh
@@ -115,16 +115,16 @@ static void leftChar()
115 115
116static void nextHistory() 116static void nextHistory()
117{ 117{
118 TT.current = TT.current->next; 118 TT.history = TT.history->next;
119 strcpy(toybuf, TT.current->data); 119 strcpy(toybuf, TT.history->data);
120 TT.x = strlen(toybuf); 120 TT.x = strlen(toybuf);
121 updateLine(); 121 updateLine();
122} 122}
123 123
124static void prevHistory() 124static void prevHistory()
125{ 125{
126 TT.current = TT.current->prev; 126 TT.history = TT.history->prev;
127 strcpy(toybuf, TT.current->data); 127 strcpy(toybuf, TT.history->data);
128 TT.x = strlen(toybuf); 128 TT.x = strlen(toybuf);
129 updateLine(); 129 updateLine();
130} 130}
@@ -208,34 +208,38 @@ static int handleKeySequence(long extra, char *sequence)
208 208
209void dumbsh_main(void) 209void dumbsh_main(void)
210{ 210{
211 struct termios termio, oldtermio; 211 struct termios termIo, oldTermIo;
212 char *temp = getenv("HOME"); 212 char *t = getenv("HOME");
213 int fd; 213 int fd;
214 214
215 // Load bash history. 215 // Load bash history.
216 temp = xmsprintf("%s/%s", temp ? temp : "", ".bash_history"); 216 t = xmsprintf("%s/%s", t ? t : "", ".bash_history");
217 if (-1 != (fd = open(temp, O_RDONLY))) 217 if (-1 != (fd = open(t, O_RDONLY)))
218 { 218 {
219 while ((temp = get_line(fd))) TT.current = dlist_add(&TT.current, temp); 219 while ((t = get_line(fd))) TT.history = dlist_add(&TT.history, t);
220 close(fd); 220 close(fd);
221 } 221 }
222 if (!TT.current) 222 if (!TT.history)
223 TT.current = dlist_add(&TT.current, ""); 223 TT.history = dlist_add(&TT.history, "");
224 224
225 // Grab the old terminal settings and save it. 225 // Grab the old terminal settings and save it.
226 tcgetattr(0, &oldtermio); 226 tcgetattr(0, &oldTermIo);
227 tcflush(0, TCIFLUSH); 227 tcflush(0, TCIFLUSH);
228 termio = oldtermio; 228 termIo = oldTermIo;
229 229
230 // Mould the terminal to our will. 230 // Mould the terminal to our will.
231 termio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IUCLC | IXON | IXOFF | IXANY); 231 // In this example we are turning off all the terminal smarts, but real code
232 termio.c_oflag &= ~OPOST; 232 // might not want that.
233 termio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | TOSTOP | ICANON | ISIG | IEXTEN); 233 termIo.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL
234 termio.c_cflag &= ~(CSIZE | PARENB); 234 | IUCLC | IXON | IXOFF | IXANY);
235 termio.c_cflag |= CS8; 235 termIo.c_oflag &= ~OPOST;
236 termio.c_cc[VTIME]=0; // deciseconds. 236 termIo.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | TOSTOP | ICANON | ISIG
237 termio.c_cc[VMIN]=1; 237 | IEXTEN);
238 tcsetattr(0, TCSANOW, &termio); 238 termIo.c_cflag &= ~(CSIZE | PARENB);
239 termIo.c_cflag |= CS8;
240 termIo.c_cc[VTIME]=0; // deciseconds.
241 termIo.c_cc[VMIN]=1;
242 tcsetattr(0, TCSANOW, &termIo);
239 243
240 // Let the mouldy old terminal mold us. 244 // Let the mouldy old terminal mold us.
241 TT.w = 80; 245 TT.w = 80;
@@ -245,7 +249,7 @@ void dumbsh_main(void)
245 updateLine(); 249 updateLine();
246 handle_keys(0, handleKeySequence, handleCSI); 250 handle_keys(0, handleKeySequence, handleCSI);
247 251
248 tcsetattr(0, TCSANOW, &oldtermio); 252 tcsetattr(0, TCSANOW, &oldTermIo);
249 puts(""); 253 puts("");
250 fflush(stdout); 254 fflush(stdout);
251} 255}