[英]How to reissue shell prompt if only user input is enter?
我正在尝试制作自己的简单命令行解释器(shell),并且如果唯一的用户输入是空格,我希望提示符重复自己。 本质上,如果用户按回车键,我希望提示重复并等待下一个输入。 我正在使用fgets接受输入,并将其存储在char * commandBuffer中,以通过parse()方法进行解析。 我本来以为要检查argv(argc = 0)中是否有参数,但这只会导致光标移至新行,而无需再次打印提示。 例如,如果我在提示符下输入“ \\ n \\ n \\ ncd”,则cd仍然起作用。 我想尝试解决的另一个问题是,为了将提示符下键入的任何内容发送到Shell,用户必须按两次Enter键。 到目前为止,这是我的代码:
for (;;) {
printf("p2: ");
fflush(stdout);
/*---------FGETS PROMPT----------*/
fgets(commandLine, STORAGE, stdin);
ln = strlen(commandLine)-1;
/* Removes trailing newline */
if(commandLine[ln] == '\n')
commandLine[ln] = '\0';
/* ATTEMPT to repeat the prompt if only user input at prompt is enter*/
if(commandLine[0] == '\0')
continue;
....More shell code....
(由您决定进行调试):我总是这样做,以使命令中出现的任何空白都可以幸免。 如果在输入行的末尾得到\\ r \\ n \\ 0或只是\\ r \\ 0,这也可以避免出现问题,这将导致代码片段问题。
char *cl; for ( cl = commandLine; *cl && isspace(*cl); cp++ ) { if ( cl == 0 ) break; // escape for ( cl = ...) to get back to outer for (;;) // if trimming trailing white space in the same style is important: int ln; // length of non-blank text { // limit scope of cl2 char *cl2; for ( cl = strchr( cl, '\0' ); cl2 >= cl; cl2-- ) if ( isspace(*cl2) ) *cl2 = '\0'; } ln = cl2 - cl + 1; // we can quickly calculate ... do the rest of your loop here using cP as your start of text }
*cl && isspace(*cl)
是在历史时期因残破的isspace()调用遇到而留下的一些偏执狂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.