gpt4 book ai didi

c - 使用命令行解释器工作,无法获取 && 和 ||正确工作

转载 作者:行者123 更新时间:2023-11-30 15:55:02 27 4
gpt4 key购买 nike

在尝试创建一个非常基本的命令行解释器时,我遇到了一个我似乎无法弄清楚的部分。当我检查 token 中所需的分隔符时,我似乎无法正确启用 && 和 ||功能。下面列出的是插入参数然后创建进程的循环。

我目前只关注 && 并计划使用该实现来帮助 ||功能。你们能看一下并帮助我指出正确的方向吗?

旁注:这也是我第一次用C编写程序,因此代码中可能会出现一些错误。

这是一个家庭作业问题。

谢谢

更新了我迄今为止拥有的完整程序代码。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#define ARG_SIZE 1<<16
#define MAXLINE 100

char *args[ARG_SIZE];
int place = 0;
int return_status;

void insert( char *token ){
args[ place ] = token;
place++;
}

void create() {
size_t nargs = place;
pid_t pid;

if ( nargs == 0 ) return;
if ( !strcmp( args[0], "exit" ) ) exit(0);
pid = fork();
if ( pid ) {
pid = wait( return_status );
} else {
if ( access( args[ 0 ], X_OK ) ){
if( execvp( args[ 0 ], args ) ) {
puts( strerror( errno ) );
exit( 127 );
}
}else{
puts( strerror( errno ) );
exit( 127 );
}
}

int i = 0;
for ( i ; i < place + 1; i++)
args[ i ] = NULL;
place = 0;
}

int main( int argc, char *argv[] ){

char line[MAXLINE+1]; /* an input line */
int c; /* a single input char or EOF */
int n; /* line length */
char *token; /* pointer to an input token */

for(;;) { /* repeat until end of file */
write( 1, "#>", 2 );

if (fgets(line,MAXLINE,stdin) == NULL) /* end of file? */
exit(0);

n = strlen(line); /* get length of line */

if (n == 0) /* if line is empty */
continue;

if (line[n-1] != '\n') { /* does input end with '\n' ? */
fprintf(stderr,"Line too long.\n"); /* no, so line is too long. */

/*--------------------------------------------*/
/* Read and ignore input through end of line. */
/*--------------------------------------------*/
while ((c = fgetc(stdin)) != '\n') {
if (c == EOF) {
fprintf(stderr,"Unexpected end of file\n");
exit(1);
}
}
continue;
}

write( 1, line, strlen( line ) );
line[n-1] = '\0'; /* remove the end of line */
/*-------------------------------------------------------------*/
/* Identify and process each token (i.e. sequence of non-blank */
/* characters delimited by whitespace). For "ordinary" tokens */
/* (i.e. "words") we just display them. For the ||, &&, and ; */
/* items we display them with a textual explanation. */
/*-------------------------------------------------------------*/
token = strtok(line," \t");
while (token != NULL ) {
insert( token );
if (!strcmp(token,"&&")){
place--;
args[ place ] = NULL;
create();
if( return_status == -1)
write( 1, "Wait failed.\n", 12 );
if( return_status & 0xff ){
int i = 0;
for( i; i < place; i++)
args[ i ] = NULL;
place = 0;
while( strcmp( token, "||" ) || strcmp( token, ";" ) ) ;
}
}
else if (!strcmp(token,"||")){
place--;
args[ place ] = NULL;
completed = 0;
create();
}
else if (!strcmp(token,";")){
place--;
args[ place ] = NULL;
create();
}
else{
}
token = strtok(NULL," \t");
}
create();
}
return 0;
}

最佳答案

wait() 接受一个整数指针,而不是整数。应该有一个编译器警告。如果没有,我建议您使用警告标志。

#include <sys.wait.h>
wait(&return_code);

此检查不正确,因为成功时 access() 返回 0。似乎您无法使用访问权限来检查不在当前目录中的命令,除非它们给出了可执行文件的完整路径或者您能够扩展它。例如,“ls”需要扩展为“/bin/ls”。

if ( access( args[ 0 ], X_OK ) ){
if( execvp( args[ 0 ], args ) ) {
puts( strerror( errno ) );
exit( 127 );
}

对 return_status 的检查是查看第一个字节,但 forked() 或 execvp() 的程序在第二个字节中返回其状态。检查返回的内容:

if( return_status & 0xff ){

应该是:

if ( WEXITSTATUS(return_status) != 0 ){

if ( (return_status & 0xff00) >> 8 ){

if 主体:

      for( int i = 0; i < place; i++)
args[ i ] = NULL;
place = 0;
while( strcmp( token, "||" ) || strcmp( token, ";" ) ) ;

可以替换为:

 break;

因为你正在执行 for 循环并将 place 设置为 0 在 create() 中,而 while 循环对我来说是无限的。只要打破封闭的 while 就会结束命令序列,并让您​​返回阅读在命令行中键入的另一个命令。

  //if ( return_status == -1)

可以使用wait()状态宏:

  if ( ! WIFEXITED(return_status) )

关于c - 使用命令行解释器工作,无法获取 && 和 ||正确工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12568395/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com