Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #2. #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions shellZilla.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,137 @@
#define SHELL_HIS_CMD_CNT_MAX 10
#define PATH_LEN_MAX 1024
#define PROMPT_LEN_MAX 1024

/*command we now support*/
char *supCmd[] =
{
"currentdir",
"ver",
NULL
};

/*get current folder name*/
void funShellZillaGetCurFolder(char *pCwd, char *curFolder)
{
char *p = NULL;
int len = 0;

p = pCwd + strlen(pCwd);
/*find the last / from the directory*/
while(*p != '/')
{
len++;
p--;
}
memcpy(curFolder, p+1, len);
return;
}

char *funShellZillaGetRidOfSpace(char *input)
{
char *p = NULL;
char *q = NULL;

p = input;
/*find the first char that is not whitespace*/
while(*p == ' ')
{
p++;
}

/*get rid of the white space at the end*/
q = input+strlen(input)-1;
while(*q == ' ')
{
q--;
}
q++;
*q = '\0';
return p;
}

/*function for command ver*/
void funShellZillaExcVer(char *pCmd, char *pParam)
{
printf("\r\n**********************************************************************\n");
printf("**************************SHELL ZILLA*********************************\n");
printf("**********************************************************************\n");
printf("*current version : 1.1 *\n");
printf("*feature description: 1.get current folder name *\n");
printf("* 2.shell version *\n");
printf("* 3. *\n");
printf("*author : Tianqi Zhou, Wentao Xu, Yuming Zhang, Zhenqing Luo *\n");
printf("**********************************************************************\r\n");
return;
}



int main()
{
char shellPrompt[PROMPT_LEN_MAX];
char cwd[PATH_LEN_MAX];
char *userInput = NULL;
char curFolder[64];
char *inputWithNoSpace = NULL;
char cmd[PROMPT_LEN_MAX];
char param[PROMPT_LEN_MAX];

/*init the completion function*/
funShellZillaReadLineIni();

/*set the command history count to 10*/
stifle_history(SHELL_HIS_CMD_CNT_MAX);
while(1)
{
memset(cwd, 0, sizeof(cwd));
getcwd(cwd, sizeof(cwd));

memset(curFolder, 0, sizeof(curFolder));
/*just get the folder name in current directory*/
funShellZillaGetCurFolder(cwd, curFolder);
memset(shellPrompt, 0, sizeof(shellPrompt));

sprintf(shellPrompt, "%s@%s:%s $", getenv("USER"), "ShellZilla", curFolder);

/*read from stdinput*/
userInput = readline(shellPrompt);
if(NULL == userInput)
{
break;
}

/*get rid of the white space at the beginning and the end*/
inputWithNoSpace = funShellZillaGetRidOfSpace(userInput);
/*printf("%s\n", inputWithNoSpace);*/

/*add the command to history*/
add_history(inputWithNoSpace);
if(!funShellZillaIsValidCmd(inputWithNoSpace))
{
/*if command not supported,print warning info*/
printf("%s :command not found!\n", inputWithNoSpace);
free(userInput);
userInput = NULL;
continue;
}

memset(cmd, 0, PROMPT_LEN_MAX);
memset(param, 0, PROMPT_LEN_MAX);
/*parse the input,get the command name and the parameter*/
funShellZillaParse(inputWithNoSpace, cmd, param);
if(!strcmp(cmd, "quit"))
{
free(userInput);
userInput = NULL;
break;
}
/*excute the command*/
funShellZillaExc(cmd, param);

free(userInput);
userInput = NULL;
}
return OK;
}
#endif