//=============================================================
// 文件名称:cmd.c
// 功能描述:提供命令处理函数
// 维护记录:2008-12-25 V1.0
//=============================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // bzero
#include <unistd.h>
#include "utils.h"
#include "cmd.h"
#include "alias.h"
struct cmd_info_t {
char *cmd;
CMD_FUNC func;
};
static int do_cls(int connfd, int argc, char *argv[])
{
const char clscode[] = "\x1b[H\x1b[J";
write(connfd, clscode, strlen(clscode));
return 0;
}
static int do_exit(int connfd, int argc, char *argv[])
{
return -1;
}
static int do_cd(int connfd, int argc, char *argv[])
{
if(chdir(argv[1]) < 0)
{
printf("cd %s, Error directory!\n", argv[1]);
}
return 0;
}
static int do_pwd(int connfd, int argc, char *argv[])
{
char buf[1024];
getcwd(buf, 1023);
write(connfd, buf, strlen(buf));
write(connfd, "\r\n", 2);
return 0;
}
static int do_alias(int connfd, int argc, char *argv[])
{
int status = 0;
printf("argc = %d\n", argc);
if(argc == 1)
display_alias(connfd);
else if(argc == 2)
add_alias(argv[1]);
else
write(connfd, "wrong param!\r\n", strlen("wrong param!\r\n"));
return status;
}
static int do_default(int connfd, int argc, char *argv[])
{
pid_t pid_child;
int piped_fd[2];
int status = 0;
int bytes_read;
char buffer[1024];
char *trueName = find_alias(argv[0]);
printf("trueName = %s\n", trueName);
if(trueName)
{
char tmpBuf[1024];
int i;
char **newargv;
int newargc;
trueName = strcpy(tmpBuf, trueName);
newargc = split_cmdline(trueName, NULL, 0);
newargv = (char **)malloc((argc + newargc) * sizeof(char *));
split_cmdline(trueName, newargv, newargc);
for(i = 1; i <= argc; i++)
newargv[newargc + i - 1] = argv[i];
argc += newargc;
argv = newargv;
printf("argc = %d\n", argc);
}
if(pipe(piped_fd) < 0)
{
perror("pipe error!");
return -1;
}
if((pid_child = fork()) == 0)
{
close(piped_fd[0]);
dup2(piped_fd[1], 1);
dup2(piped_fd[1], 2);
if(execvp(argv[0], argv) < 0)
{
perror("reson");
printf("bash: %s: command not found\n", argv[0]);
}
exit(1);
}
close(piped_fd[1]);
while((bytes_read = read(piped_fd[0], buffer, 1024)) > 0)
{
write_with_dosline(connfd, buffer, bytes_read);
}
close(piped_fd[0]);
if(trueName)
free(argv);
return status;
}
struct cmd_info_t cmdList[] = {
{"cls", do_cls},
{"clear", do_cls},
{"exit", do_exit},
{"cd", do_cd},
{"pwd", do_pwd},
{"alias", do_alias},
};
int exec_cmd(int connfd, char *cmdline)
{
int status;
int i;
int argCount;
char **argList;
// argCount = split_string(cmdline, ' ', NULL, 0);
argCount = split_cmdline(cmdline, NULL, 0);
argList = (char **)malloc((argCount + 1) * sizeof(char *));
// argCount = split_string(cmdline, ' ', argList, argCount);
argCount = split_cmdline(cmdline, argList, argCount);
printf("argCount = %d, cmdline = %s\n", argCount, cmdline);
argList[argCount] = NULL;
for(i = 0; i < (sizeof(cmdList) / sizeof(cmdList[0])); i++)
{
if(strcmp(cmdList[i].cmd, argList[0]) == 0)
{
status = (*cmdList[i].func)(connfd, argCount, argList);
break;
}
}
if(i >= (sizeof(cmdList) / sizeof(cmdList[0])))
status = do_default(connfd, argCount, argList);
free(argList);
return status;
}
|