kolibrios-fun/programs/system/shell/module_command.c

337 lines
4.8 KiB
C
Raw Normal View History

/// ===========================================================
void command_clear()
{
for (;CMD_POS;CMD_POS--)
printf("%c %c", 8, 8);
CMD[0]='\0';
}
/// ===========================================================
void command_history_add(char command[])
{
if ( (0 != strcmp( CMD_HISTORY[0], CMD)) &&
(0 != strcmp( CMD_HISTORY[1], CMD)) &&
(0 != strcmp( CMD_HISTORY[2], CMD)) &&
(0 != strcmp( CMD_HISTORY[3], CMD)) &&
(0 != strcmp( CMD_HISTORY[4], CMD)) )
{
strcpy(CMD_HISTORY[4], CMD_HISTORY[3]);
strcpy(CMD_HISTORY[3], CMD_HISTORY[2]);
strcpy(CMD_HISTORY[2], CMD_HISTORY[1]);
strcpy(CMD_HISTORY[1], CMD_HISTORY[0]);
strcpy(CMD_HISTORY[0], CMD);
}
}
/// ===========================================================
void command_get()
{
unsigned key;
//unsigned pos = 0;
int hist;
CMD_POS = 0;
CMD_NUM = 0;
for (;;)
{
key = getch();
if ( 0 != (key & 0xff) )
{
key &= 0xff;
switch (key)
{
case 27: // ESC
command_clear();
break;
case 13: // ENTER
CMD[CMD_POS] = '\0';
printf("\n\r");
command_history_add(CMD);
return;
case 8: // BACKSPACE
if (CMD_POS > 0)
{
printf ("%c %c", 8, 8);
CMD_POS--;
}
break;
case 9: // TAB
break;
default:
if (CMD_POS < 255)
{
CMD[CMD_POS] = key;
CMD_POS++;
printf("%c", key);
}
break;
};
}
else // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
key = (key>>8)&0xff;
// printf ("%d\n\r", key);
switch (key)
{
case 72: // UP
for (hist = 0; hist < CMD_HISTORY_NUM; hist++)
{
command_clear();
if (CMD_NUM < CMD_HISTORY_NUM-1)
CMD_NUM++;
else
CMD_NUM = 0;
printf( CMD_HISTORY[CMD_NUM] );
strcpy(CMD, CMD_HISTORY[CMD_NUM]);
CMD_POS = strlen(CMD);
if (0!=strlen(CMD))
break;
}
break;
case 80: // DOWN
for (hist = 0; hist < CMD_HISTORY_NUM; hist++)
{
command_clear();
if (CMD_NUM > 0)
CMD_NUM--;
else
CMD_NUM = CMD_HISTORY_NUM-1;
printf( CMD_HISTORY[CMD_NUM] );
strcpy(CMD, CMD_HISTORY[CMD_NUM]);
CMD_POS = strlen(CMD);
if (0!=strlen(CMD))
break;
}
break;
};
}
}
}
/// ===========================================================
int command_get_cmd(char cmd[])
{
unsigned i;
for (i=0;;i++)
{
cmd[i] = CMD[i];
if (0 == cmd[i])
{
i = -2;
break;
}
if ( iswhite(cmd[i]) )
{
cmd[i] = '\0';
break;
}
}
return i+1;
}
/// ===========================================================
void command_execute()
{
char cmd[256];
char args[256];
unsigned arg;
trim(CMD);
arg = command_get_cmd(cmd);
if ( !strlen(cmd) )
return;
strcpy(args, CMD+arg);
trim(args);
if ( !strcmp(cmd, "help") )
{
if (-1 == arg)
cmd_help(NULL);
else
cmd_help(args);
return;
}
if ( !strcmp(cmd, "ver") )
{
cmd_ver();
return;
}
if ( !strcmp(cmd, "cd") )
{
if (-1 == arg)
cmd_cd(NULL);
else
cmd_cd(args);
if ( '/' != cur_dir[strlen(cur_dir)-1] )
strcat(cur_dir, "/");
return;
}
if ( !strcmp(cmd, "ls") )
{
if (-1 == arg)
cmd_ls(cur_dir);
else
cmd_ls(args);
return;
}
if ( !strcmp(cmd, "ps") )
{
cmd_ps();
return;
}
if ( !strcmp(cmd, "kill") )
{
if (-1 == arg)
cmd_kill(NULL);
else
cmd_kill(args);
return;
}
if ( !strcmp(cmd, "pwd") )
{
cmd_pwd();
return;
}
if ( !strcmp(cmd, "mkdir") )
{
if (-1 == arg)
cmd_mkdir(NULL);
else
cmd_mkdir(args);
return;
}
if ( !strcmp(cmd, "rmdir") )
{
if (-1 == arg)
cmd_rmdir(NULL);
else
cmd_rmdir(args);
return;
}
if ( !strcmp(cmd, "rm") )
{
if (-1 == arg)
cmd_rm(NULL);
else
cmd_rm(args);
return;
}
if ( !strcmp(cmd, "more") )
{
if (-1 == arg)
cmd_more(NULL);
else
cmd_more(args);
return;
}
if ( !strcmp(cmd, "echo") )
{
cmd_echo(args);
return;
}
if ( !strcmp(cmd, "exit") )
{
cmd_exit();
return;
}
if ( !strcmp(cmd, "date") )
{
cmd_date();
return;
}
if ( !strcmp(cmd, "about") )
{
cmd_about();
return;
}
if ( !strcmp(cmd, "free") )
{
cmd_free();
return;
}
if ( !strcmp(cmd, "reboot") )
{
cmd_reboot();
return;
}
if ( !strcmp(cmd, "touch") )
{
if (-1 == arg)
cmd_touch(NULL);
else
cmd_touch(args);
return;
}
if ( !strcmp(cmd, "alias") )
{
if (-1 == arg)
cmd_alias(NULL);
else
cmd_alias(args);
return;
}
if ( -1 != alias_search(CMD) )
{
strcpy(CMD, ALIASES+64*1024+256*alias_search(CMD));
command_execute();
return;
}
executable_run(cmd, args);
}
/// ===========================================================