⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tinysh.c

📁 一个简易的Linux下的shell源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
          putchar('\n');          return 0;        }      else if(ret==UNMATCH)        {          puts("\nno match: ");          puts(str);          putchar('\n');          return 0;        }      else /* NULLMATCH */        {          if(cur_cmd_ctx)            display_child_help(cur_cmd_ctx->child);          else            display_child_help(root_cmd);          return 0;        }    }}/* try to complete current command line */static int complete_command_line(tinysh_cmd_t *cmd, uchar *_str){  uchar *str=_str;  while(1)    {      int ret;      int common_len=BUFFER_SIZE;      int _str_len;      int i;      uchar *__str=str;      tinysh_cmd_t *_cmd=cmd;      ret=parse_command(&cmd,&str);      for(_str_len=0;__str[_str_len]&&__str[_str_len]!=' ';_str_len++);      if(ret==MATCH && *str)        {          cmd=cmd->child;        }      else if(ret==AMBIG || ret==MATCH || ret==NULLMATCH)        {          tinysh_cmd_t *cm;          tinysh_cmd_t *matched_cmd=0;          int nb_match=0;                        for(cm=cmd;cm;cm=cm->next)            {              int r=strstart(cm->name,__str);              if(r==FULLMATCH)                {                  for(i=_str_len;cmd->name[i];i++)                    tinysh_char_in(cmd->name[i]);                  if(*(str-1)!=' ')                    tinysh_char_in(' ');                  if(!cmd->child)                    {                      if(cmd->usage)                        {                          puts(cmd->usage);                          putchar('\n');                          return 1;                        }                      else                        return 0;                    }                  else                    {                      cmd=cmd->child;                      break;                    }                }              else if(r==PARTMATCH)                {                  nb_match++;                  if(!matched_cmd)                    {                      matched_cmd=cm;                      common_len=strlen(cm->name);                    }                  else                    {                      for(i=_str_len;cm->name[i] && i<common_len &&                            cm->name[i]==matched_cmd->name[i];i++);                      if(i<common_len)                        common_len=i;                    }                }            }          if(cm)            continue;          if(matched_cmd)            {              if(_str_len==common_len)                {                  putchar('\n');                  for(cm=cmd;cm;cm=cm->next)                    {                      int r=strstart(cm->name,__str);                      if(r==FULLMATCH || r==PARTMATCH)                        {                          puts(cm->name);                          putchar('\n');                        }                    }                  return 1;                }              else                {                  for(i=_str_len;i<common_len;i++)                    tinysh_char_in(matched_cmd->name[i]);                  if(nb_match==1)                    tinysh_char_in(' ');                }            }          return 0;        }      else /* UNMATCH */        {           return 0;        }    }}/* start a new line  */static void start_of_line(){  /* display start of new line */  puts(prompt);  if(cur_context)    {      puts(context_buffer);      puts("> ");    }  cur_index=0;}/* character input  */static void _tinysh_char_in(uchar c){  uchar *line=input_buffers[cur_buf_index];  if(c=='\n' || c=='\r') /* validate command */    {      tinysh_cmd_t *cmd;      int context=0;      /* first, echo the newline */      if(echo)        putchar(c);      while(*line && *line==' ') line++;      if(*line) /* not empty line */        {          cmd=cur_cmd_ctx?cur_cmd_ctx->child:root_cmd;          exec_command_line(cmd,line);          cur_buf_index=(cur_buf_index+1)%HISTORY_DEPTH;          cur_index=0;          input_buffers[cur_buf_index][0]=0;        }      start_of_line();    }  else if(c==TOPCHAR) /* return to top level */    {      if(echo)        putchar(c);      cur_context=0;      cur_cmd_ctx=0;    }  else if(c==8 || c==127) /* backspace */    {      if(cur_index>0)        {          puts("\b \b");          cur_index--;          line[cur_index]=0;        }    }  else if(c==16) /* CTRL-P: back in history */    {      int prevline=(cur_buf_index+HISTORY_DEPTH-1)%HISTORY_DEPTH;      if(input_buffers[prevline][0])        {          line=input_buffers[prevline];          /* fill the rest of the line with spaces */          while(cur_index-->strlen(line))            puts("\b \b");          putchar('\r');          start_of_line();          puts(line);          cur_index=strlen(line);          cur_buf_index=prevline;        }    }  else if(c==14) /* CTRL-N: next in history */    {      int nextline=(cur_buf_index+1)%HISTORY_DEPTH;      if(input_buffers[nextline][0])        {          line=input_buffers[nextline];          /* fill the rest of the line with spaces */          while(cur_index-->strlen(line))            puts("\b \b");          putchar('\r');          start_of_line();          puts(line);          cur_index=strlen(line);          cur_buf_index=nextline;        }    }  else if(c=='?') /* display help */    {      tinysh_cmd_t *cmd;      cmd=cur_cmd_ctx?cur_cmd_ctx->child:root_cmd;      help_command_line(cmd,line);      start_of_line();      puts(line);      cur_index=strlen(line);    }  else if(c==9 || c=='!') /* TAB: autocompletion */    {      tinysh_cmd_t *cmd;      cmd=cur_cmd_ctx?cur_cmd_ctx->child:root_cmd;      if(complete_command_line(cmd,line))        {          start_of_line();          puts(line);        }      cur_index=strlen(line);    }        else /* any input character */    {      if(cur_index<BUFFER_SIZE)        {          if(echo)            putchar(c);          line[cur_index++]=c;          line[cur_index]=0;        }    }}/* new character input */void tinysh_char_in(uchar c){  /*   * filter characters here   */  _tinysh_char_in(c);}/* add a new command */void tinysh_add_command(tinysh_cmd_t *cmd){  tinysh_cmd_t *cm;  if(cmd->parent)    {      cm=cmd->parent->child;      if(!cm)        {          cmd->parent->child=cmd;        }      else        {          while(cm->next) cm=cm->next;          cm->next=cmd;        }    }  else if(!root_cmd)    {      root_cmd=cmd;    }  else    {      cm=root_cmd;      while(cm->next) cm=cm->next;      cm->next=cmd;          }}/* modify shell prompt */void tinysh_set_prompt(char *str){  int i;  for(i=0;str[i] && i<PROMPT_SIZE;i++)    prompt[i]=str[i];  prompt[i]=0;  /* force prompt display by generating empty command */  tinysh_char_in('\r');}/* return current command argument */void *tinysh_get_arg(){  return tinysh_arg;}/* string to decimal/hexadecimal conversion */unsigned long tinysh_atoxi(char *s){  int ishex=0;  unsigned long res=0;  if(*s==0) return 0;  if(*s=='0' && *(s+1)=='x')    {      ishex=1;      s+=2;    }  while(*s)    {      if(ishex)	res*=16;      else	res*=10;      if(*s>='0' && *s<='9')	res+=*s-'0';      else if(ishex && *s>='a' && *s<='f')	res+=*s+10-'a';      else if(ishex && *s>='A' && *s<='F')	res+=*s+10-'A';      else	break;            s++;    }  return res;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -