1 Star 0 Fork 0

GG_Colin/wshell

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
read_command.c 2.50 KB
一键复制 编辑 原始数据 按行查看 历史
wuyue 提交于 2015-11-09 00:40 +08:00 . chinese README.md added.
/*
* =====================================================================================
* Filename: read_command.c
* Description:
* Version: 1.0
* Created: 2013.10.21 14h12min24s
* Author: wuyue (wy), vvuyve@gmail.com
* Company: UESTC
* =====================================================================================
*/
#include "wshell.h"
#ifdef READLINE_ON
#include <readline/readline.h>
#include <readline/history.h>
#endif
//return value: number of parameters
//0 represents only command without any parameters
//-1 represents wrong input
int read_command(char **command,char **parameters,char *prompt)
{
#ifdef READLINE_ON
free(buffer);
buffer = readline(prompt);
if(feof(stdin)) {
printf("\n");
exit(0);
}
#else
printf("%s",prompt);
char* Res_fgets = fgets(buffer,MAXLINE,stdin);
if(Res_fgets == NULL)
{
printf("\n");
exit(0);
}
#endif
if(buffer[0] == '\0')
return -1;
char *pStart,*pEnd;
int count = 0;
int isFinished = 0;
pStart = pEnd = buffer;
while(isFinished == 0)
{
while((*pEnd == ' ' && *pStart == ' ') || (*pEnd == '\t' && *pStart == '\t'))
{
pStart++;
pEnd++;
}
if(*pEnd == '\0' || *pEnd == '\n')
{
if(count == 0)
return -1;
break;
}
while(*pEnd != ' ' && *pEnd != '\0' && *pEnd != '\n')
pEnd++;
if(count == 0)
{
char *p = pEnd;
*command = pStart;
while(p!=pStart && *p !='/')
p--;
if(*p == '/')
p++;
//else //p==pStart
parameters[0] = p;
count += 2;
#ifdef DEBUG
printf("\ncommand:%s\n",*command);
#endif
}
else if(count <= MAXARG)
{
parameters[count-1] = pStart;
count++;
}
else
{
break;
}
if(*pEnd == '\0' || *pEnd == '\n')
{
*pEnd = '\0';
isFinished = 1;
}
else
{
*pEnd = '\0';
pEnd++;
pStart = pEnd;
}
}
parameters[count-1] = NULL;
#ifdef DEBUG
/*input analysis*/
printf("input analysis:\n");
printf("pathname:[%s]\ncommand:[%s]\nparameters:\n",*command,parameters[0]);
int i;
for(i=0;i<count-1;i++)
printf("[%s]\n",parameters[i]);
#endif
return count;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/GG_Colin/wshell.git
git@gitee.com:GG_Colin/wshell.git
GG_Colin
wshell
wshell
master

搜索帮助