Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18791
  • 博文数量: 6
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-10 10:40
文章分类

全部博文(6)

文章存档

2013年(6)

我的朋友

分类: 嵌入式

2013-07-25 22:22:25

程序运行流程:
lash_main -> busy_loop -> get_command ->    (ash.c)
cmdedit_read_input    (cmdedit.c)

cmdedit_read_input:

点击(此处)折叠或打开

  1. int cmdedit_read_input(char *prompt, char command[BUFSIZ])
  2. {

  3.     int break_out = 0;
  4.     int lastWasTab = FALSE;
  5.     unsigned char c = 0;

  6.     /* prepare before init handlers */
  7.     cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
  8.     len = 0;
  9.     command_ps = command;

  10.     getTermSettings(0, (void *) &initial_settings);
  11.     memcpy(&new_settings, &initial_settings, sizeof(struct termios));
  12.     new_settings.c_lflag &= ~ICANON; /* unbuffered input */
  13.     /* Turn off echoing and CTRL-C, so we can trap it */
  14.     new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
  15.     /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
  16.     new_settings.c_cc[VMIN] = 1;
  17.     new_settings.c_cc[VTIME] = 0;
  18.     /* Turn off CTRL-C, so we can trap it */
  19. # ifndef _POSIX_VDISABLE
  20. # define _POSIX_VDISABLE '\0'
  21. # endif
  22.     new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
  23.     command[0] = 0;

  24.     setTermSettings(0, (void *) &new_settings);
  25.     handlers_sets |= SET_RESET_TERM;

  26.     /* Now initialize things */
  27.     cmdedit_init();
  28.     /* Print out the command prompt */
  29.     parse_prompt(prompt);

  30.     while (1) {

  31.         fflush(stdout); /* buffered out to fast */

  32.         if (safe_read(0, &c, 1) < 1)
  33.             /* if we can't read input then exit */
  34.             goto prepare_to_die;

  35.         switch (c) {
  36.         case '\n':
  37.         case '\r':
  38.             /* Enter */
  39.             goto_new_line();
  40.             break_out = 1;
  41.             break;
  42.         case 1:
  43.             /* Control-a -- Beginning of line */
  44.             input_backward(cursor);
  45.             break;
  46.         case 2:
  47.             /* Control-b -- Move back one character */
  48.             input_backward(1);
  49.             break;
  50.         case 3:
  51.             /* Control-c -- stop gathering input */
  52.             goto_new_line();
  53. #ifndef CONFIG_ASH
  54.             command[0] = 0;
  55.             len = 0;
  56.             lastWasTab = FALSE;
  57.             put_prompt();
  58. #else
  59.             len = 0;
  60.             break_out = -1; /* to control traps */
  61. #endif
  62.             break;
  63.         case 4:
  64.             /* Control-d -- Delete one character, or exit
  65.              * if the len=0 and no chars to delete */
  66.             if (len == 0) {
  67.                     errno = 0;
  68. prepare_to_die:
  69. #if !defined(CONFIG_ASH)
  70.                 printf("exit");
  71.                 goto_new_line();
  72.                 /* cmdedit_reset_term() called in atexit */
  73.                 exit(EXIT_SUCCESS);
  74. #else
  75.                 /* to control stopped jobs */
  76.                 len = break_out = -1;
  77.                 break;
  78. #endif
  79.             } else {
  80.                 input_delete();
  81.             }
  82.             break;
  83.         case 5:
  84.             /* Control-e -- End of line */
  85.             input_end();
  86.             break;
  87.         case 6:
  88.             /* Control-f -- Move forward one character */
  89.             input_forward();
  90.             break;
  91.         case '\b':
  92.         case DEL:
  93.             /* Control-h and DEL */
  94.             input_backspace();
  95.             break;
  96.         case '\t':
  97. #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
  98.             input_tab(&lastWasTab);
  99. #endif
  100.             break;
  101.         case 11:
  102.             /* Control-k -- clear to end of line */
  103.             *(command + cursor) = 0;
  104.             len = cursor;
  105.             printf("\033[J");
  106.             break;
  107.         case 12:
  108.             /* Control-l -- clear screen */
  109.             printf("\033[H");
  110.             redraw(0, len-cursor);
  111.             break;
  112. #if MAX_HISTORY >= 1
  113.         case 14:
  114.             /* Control-n -- Get next command in history */
  115.             if (get_next_history())
  116.                 goto rewrite_line;
  117.             break;
  118.         case 16:
  119.             /* Control-p -- Get previous command from history */
  120.             if (cur_history > 0) {
  121.                 get_previous_history();
  122.                 goto rewrite_line;
  123.             } else {
  124.                 beep();
  125.             }
  126.             break;
  127. #endif
  128.         case 21:
  129.             /* Control-U -- Clear line before cursor */
  130.             if (cursor) {
  131.                 strcpy(command, command + cursor);
  132.                 redraw(cmdedit_y, len -= cursor);
  133.             }
  134.             break;
  135.         case 23:
  136.             /* Control-W -- Remove the last word */
  137.             while (cursor > 0 && isspace(command[cursor-1]))
  138.                 input_backspace();
  139.             while (cursor > 0 &&!isspace(command[cursor-1]))
  140.                 input_backspace();
  141.             break;
  142.         case ESC:{
  143.             /* escape sequence follows */
  144.             if (safe_read(0, &c, 1) < 1)
  145.                 goto prepare_to_die;
  146.             /* different vt100 emulations */
  147.             if (c == '[' || c == 'O') {
  148.                 if (safe_read(0, &c, 1) < 1)
  149.                     goto prepare_to_die;
  150.             }
  151.             if (c >= '1' && c <= '9') {
  152.                 unsigned char dummy;

  153.                 if (safe_read(0, &dummy, 1) < 1)
  154.                     goto prepare_to_die;
  155.                 if(dummy != '~')
  156.                     c = 0;
  157.             }
  158.             switch (c) {
  159. #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
  160.             case '\t': /* Alt-Tab */

  161.                 input_tab(&lastWasTab);
  162.                 break;
  163. #endif
  164. #if MAX_HISTORY >= 1
  165.             case 'A':
  166.                 /* Up Arrow -- Get previous command from history */
  167.                 if (cur_history > 0) {
  168.                     get_previous_history();
  169.                     goto rewrite_line;
  170.                 } else {
  171.                     beep();
  172.                 }
  173.                 break;
  174.             case 'B':
  175.                 /* Down Arrow -- Get next command in history */
  176.                 if (!get_next_history())
  177.                 break;
  178.                 /* Rewrite the line with the selected history item */
  179. rewrite_line:
  180.                 /* change command */
  181.                 len = strlen(strcpy(command, history[cur_history]));
  182.                 /* redraw and go to end line */
  183.                 redraw(cmdedit_y, 0);
  184.                 break;
  185. #endif
  186.             case 'C':
  187.                 /* Right Arrow -- Move forward one character */
  188.                 input_forward();
  189.                 break;
  190.             case 'D':
  191.                 /* Left Arrow -- Move back one character */
  192.                 input_backward(1);
  193.                 break;
  194.             case '3':
  195.                 /* Delete */
  196.                 input_delete();
  197.                 break;
  198.             case '1':
  199.             case 'H':
  200.                 /* <Home> */
  201.                 input_backward(cursor);
  202.                 break;
  203.             case '4':
  204.             case 'F':
  205.                 /* <End> */
  206.                 input_end();
  207.                 break;
  208.             default:
  209.                 c = 0;
  210.                 beep();
  211.             }
  212.             break;
  213.         }

  214.         default: /* If it's regular input, do the normal thing */
  215. #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
  216.             /* Control-V -- Add non-printable symbol */
  217.             if (c == 22) {
  218.                 if (safe_read(0, &c, 1) < 1)
  219.                     goto prepare_to_die;
  220.                 if (c == 0) {
  221.                     beep();
  222.                     break;
  223.                 }
  224.             } else
  225. #endif
  226.             if (!Isprint(c)) /* Skip non-printable characters */
  227.                 break;

  228.             if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
  229.                 break;

  230.             len++;

  231.             if (cursor == (len - 1)) { /* Append if at the end of the line */
  232.                 *(command + cursor) = c;
  233.                 *(command + cursor + 1) = 0;
  234.                 cmdedit_set_out_char(0);
  235.             } else { /* Insert otherwise */
  236.                 int sc = cursor;

  237.                 memmove(command + sc + 1, command + sc, len - sc);
  238.                 *(command + sc) = c;
  239.                 sc++;
  240.                 /* rewrite from cursor */
  241.                 input_end();
  242.                 /* to prev x pos + 1 */
  243.                 input_backward(cursor - sc);
  244.             }
  245.             break;
阅读(282) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:安卓root工具——SuperOneClick

给主人留下些什么吧!~~