aboutsummaryrefslogtreecommitdiff
path: root/shell/cmdedit.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-07-12 20:26:32 +0000
committerEric Andersen <andersen@codepoet.org>2001-07-12 20:26:32 +0000
commit7467c8d3b6a50e2cbd8db750963d40b420ad38d1 (patch)
tree10dcece1e0bb88e35aa95c3a68896ad426e43f7a /shell/cmdedit.c
parentf69bfc76fa7acb0c87fa3f3b319fde361a8315a8 (diff)
downloadbusybox-w32-7467c8d3b6a50e2cbd8db750963d40b420ad38d1.tar.gz
busybox-w32-7467c8d3b6a50e2cbd8db750963d40b420ad38d1.tar.bz2
busybox-w32-7467c8d3b6a50e2cbd8db750963d40b420ad38d1.zip
Patch from vodz:
Changed email address cmdedit API change optimizations for traceroute and md5sum added a new shared create_icmp_socket() function
Diffstat (limited to 'shell/cmdedit.c')
-rw-r--r--shell/cmdedit.c88
1 files changed, 26 insertions, 62 deletions
diff --git a/shell/cmdedit.c b/shell/cmdedit.c
index 4f124d647..540eb7ea7 100644
--- a/shell/cmdedit.c
+++ b/shell/cmdedit.c
@@ -3,7 +3,7 @@
3 * Termios command line History and Editting. 3 * Termios command line History and Editting.
4 * 4 *
5 * Copyright (c) 1986-2001 may safely be consumed by a BSD or GPL license. 5 * Copyright (c) 1986-2001 may safely be consumed by a BSD or GPL license.
6 * Written by: Vladimir Oleynik <vodz@usa.net> 6 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
7 * 7 *
8 * Used ideas: 8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu> 9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
@@ -123,9 +123,8 @@ volatile int handlers_sets = 0; /* Set next bites: */
123enum { 123enum {
124 SET_ATEXIT = 1, /* when atexit() has been called 124 SET_ATEXIT = 1, /* when atexit() has been called
125 and get euid,uid,gid to fast compare */ 125 and get euid,uid,gid to fast compare */
126 SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */ 126 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
127 SET_WCHG_HANDLERS = 4, /* winchg signal handler */ 127 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
128 SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */
129}; 128};
130 129
131 130
@@ -142,10 +141,6 @@ static
142#endif 141#endif
143char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */ 142char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
144 143
145/* Link into lash to reset context to 0 on ^C and such */
146extern unsigned int shell_context;
147
148
149#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR 144#ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR
150static char *user_buf = ""; 145static char *user_buf = "";
151static char *home_pwd_buf = ""; 146static char *home_pwd_buf = "";
@@ -319,6 +314,7 @@ static void put_prompt(void)
319 out1str(cmdedit_prompt); 314 out1str(cmdedit_prompt);
320 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */ 315 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
321 cursor = 0; 316 cursor = 0;
317 cmdedit_y = 0; /* new quasireal y */
322} 318}
323 319
324#ifndef BB_FEATURE_SH_FANCY_PROMPT 320#ifndef BB_FEATURE_SH_FANCY_PROMPT
@@ -456,7 +452,6 @@ static void redraw(int y, int back_cursor)
456{ 452{
457 if (y > 0) /* up to start y */ 453 if (y > 0) /* up to start y */
458 printf("\033[%dA", y); 454 printf("\033[%dA", y);
459 cmdedit_y = 0; /* new quasireal y */
460 putchar('\r'); 455 putchar('\r');
461 put_prompt(); 456 put_prompt();
462 input_end(); /* rewrite */ 457 input_end(); /* rewrite */
@@ -497,14 +492,6 @@ static void input_forward(void)
497} 492}
498 493
499 494
500static void clean_up_and_die(int sig)
501{
502 goto_new_line();
503 if (sig != SIGINT)
504 exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
505 cmdedit_reset_term();
506}
507
508static void cmdedit_setwidth(int w, int redraw_flg) 495static void cmdedit_setwidth(int w, int redraw_flg)
509{ 496{
510 cmdedit_termw = cmdedit_prmt_len + 2; 497 cmdedit_termw = cmdedit_prmt_len + 2;
@@ -525,7 +512,7 @@ static void cmdedit_setwidth(int w, int redraw_flg)
525 } 512 }
526} 513}
527 514
528extern void cmdedit_init(void) 515static void cmdedit_init(void)
529{ 516{
530 cmdedit_prmt_len = 0; 517 cmdedit_prmt_len = 0;
531 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) { 518 if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
@@ -557,14 +544,6 @@ extern void cmdedit_init(void)
557 handlers_sets |= SET_ATEXIT; 544 handlers_sets |= SET_ATEXIT;
558 atexit(cmdedit_reset_term); /* be sure to do this only once */ 545 atexit(cmdedit_reset_term); /* be sure to do this only once */
559 } 546 }
560
561 if ((handlers_sets & SET_TERM_HANDLERS) == 0) {
562 signal(SIGKILL, clean_up_and_die);
563 signal(SIGINT, clean_up_and_die);
564 signal(SIGQUIT, clean_up_and_die);
565 signal(SIGTERM, clean_up_and_die);
566 handlers_sets |= SET_TERM_HANDLERS;
567 }
568} 547}
569 548
570#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION 549#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
@@ -1220,7 +1199,7 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
1220 1199
1221 fflush(stdout); /* buffered out to fast */ 1200 fflush(stdout); /* buffered out to fast */
1222 1201
1223 if (read(0, &c, 1) < 1) 1202 if (safe_read(0, &c, 1) < 1)
1224 /* if we can't read input then exit */ 1203 /* if we can't read input then exit */
1225 goto prepare_to_die; 1204 goto prepare_to_die;
1226 1205
@@ -1241,22 +1220,21 @@ extern void cmdedit_read_input(char *prompt, char command[BUFSIZ])
1241 break; 1220 break;
1242 case 3: 1221 case 3:
1243 /* Control-c -- stop gathering input */ 1222 /* Control-c -- stop gathering input */
1244
1245 /* Link into lash to reset context to 0 on ^C and such */
1246 shell_context = 0;
1247
1248 /* Go to the next line */
1249 goto_new_line(); 1223 goto_new_line();
1250 command[0] = 0; 1224 command[0] = 0;
1251 1225 len = 0;
1252 return; 1226 lastWasTab = FALSE;
1227 put_prompt();
1228 break;
1253 case 4: 1229 case 4:
1254 /* Control-d -- Delete one character, or exit 1230 /* Control-d -- Delete one character, or exit
1255 * if the len=0 and no chars to delete */ 1231 * if the len=0 and no chars to delete */
1256 if (len == 0) { 1232 if (len == 0) {
1257prepare_to_die: 1233prepare_to_die:
1258 printf("exit"); 1234 printf("exit");
1259 clean_up_and_die(0); 1235 goto_new_line();
1236 /* cmdedit_reset_term() called in atexit */
1237 exit(EXIT_SUCCESS);
1260 } else { 1238 } else {
1261 input_delete(); 1239 input_delete();
1262 } 1240 }
@@ -1307,12 +1285,12 @@ prepare_to_die:
1307 1285
1308 case ESC:{ 1286 case ESC:{
1309 /* escape sequence follows */ 1287 /* escape sequence follows */
1310 if (read(0, &c, 1) < 1) 1288 if (safe_read(0, &c, 1) < 1)
1311 return; 1289 goto prepare_to_die;
1312 /* different vt100 emulations */ 1290 /* different vt100 emulations */
1313 if (c == '[' || c == 'O') { 1291 if (c == '[' || c == 'O') {
1314 if (read(0, &c, 1) < 1) 1292 if (safe_read(0, &c, 1) < 1)
1315 return; 1293 goto prepare_to_die;
1316 } 1294 }
1317 switch (c) { 1295 switch (c) {
1318#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION 1296#ifdef BB_FEATURE_COMMAND_TAB_COMPLETION
@@ -1376,8 +1354,8 @@ prepare_to_die:
1376 } 1354 }
1377 if (c >= '1' && c <= '9') 1355 if (c >= '1' && c <= '9')
1378 do 1356 do
1379 if (read(0, &c, 1) < 1) 1357 if (safe_read(0, &c, 1) < 1)
1380 return; 1358 goto prepare_to_die;
1381 while (c != '~'); 1359 while (c != '~');
1382 break; 1360 break;
1383 } 1361 }
@@ -1386,8 +1364,8 @@ prepare_to_die:
1386#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT 1364#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1387 /* Control-V -- Add non-printable symbol */ 1365 /* Control-V -- Add non-printable symbol */
1388 if (c == 22) { 1366 if (c == 22) {
1389 if (read(0, &c, 1) < 1) 1367 if (safe_read(0, &c, 1) < 1)
1390 return; 1368 goto prepare_to_die;
1391 if (c == 0) { 1369 if (c == 0) {
1392 beep(); 1370 beep();
1393 break; 1371 break;
@@ -1485,23 +1463,10 @@ prepare_to_die:
1485#if defined(BB_FEATURE_SH_FANCY_PROMPT) 1463#if defined(BB_FEATURE_SH_FANCY_PROMPT)
1486 free(cmdedit_prompt); 1464 free(cmdedit_prompt);
1487#endif 1465#endif
1488 return; 1466 cmdedit_reset_term();
1489} 1467}
1490 1468
1491 1469
1492/* Undo the effects of cmdedit_init(). */
1493extern void cmdedit_terminate(void)
1494{
1495 cmdedit_reset_term();
1496 if ((handlers_sets & SET_TERM_HANDLERS) != 0) {
1497 signal(SIGKILL, SIG_DFL);
1498 signal(SIGINT, SIG_DFL);
1499 signal(SIGQUIT, SIG_DFL);
1500 signal(SIGTERM, SIG_DFL);
1501 signal(SIGWINCH, SIG_DFL);
1502 handlers_sets &= ~SET_TERM_HANDLERS;
1503 }
1504}
1505 1470
1506#endif /* BB_FEATURE_COMMAND_EDITING */ 1471#endif /* BB_FEATURE_COMMAND_EDITING */
1507 1472
@@ -1515,8 +1480,6 @@ const char *memory_exhausted = "Memory exhausted";
1515#include <locale.h> 1480#include <locale.h>
1516#endif 1481#endif
1517 1482
1518unsigned int shell_context;
1519
1520int main(int argc, char **argv) 1483int main(int argc, char **argv)
1521{ 1484{
1522 char buff[BUFSIZ]; 1485 char buff[BUFSIZ];
@@ -1532,15 +1495,16 @@ int main(int argc, char **argv)
1532#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT 1495#ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT
1533 setlocale(LC_ALL, ""); 1496 setlocale(LC_ALL, "");
1534#endif 1497#endif
1535 shell_context = 1; 1498 while(1) {
1536 do {
1537 int l; 1499 int l;
1538 cmdedit_read_input(prompt, buff); 1500 cmdedit_read_input(prompt, buff);
1539 l = strlen(buff); 1501 l = strlen(buff);
1502 if(l==0)
1503 break;
1540 if(l > 0 && buff[l-1] == '\n') 1504 if(l > 0 && buff[l-1] == '\n')
1541 buff[l-1] = 0; 1505 buff[l-1] = 0;
1542 printf("*** cmdedit_read_input() returned line =%s=\n", buff); 1506 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
1543 } while (shell_context); 1507 }
1544 printf("*** cmdedit_read_input() detect ^C\n"); 1508 printf("*** cmdedit_read_input() detect ^C\n");
1545 return 0; 1509 return 0;
1546} 1510}