定制外壳只带一个参数 [英] Custom shell only taking one argument

查看:117
本文介绍了定制外壳只带一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个code的外壳:

So I have this code for a shell:

#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM_ARGS 256
#define SIZE 256

void orders(char *command[SIZE]);

int main() {

    char buffer[SIZE]= "";
    //char input_args[MAX_NUM_ARGS];
    char **input_args = NULL;
    int i = 0;// counting variable
    int blah = 0;

    printf("Welcome to the AY shell.\n");
  while(1){

    //initialize array of strings
    //first free any prevously allocated memory
    if (input_args != NULL)
    {   //memory has been allocated free it
        for (i = 0; i <MAX_NUM_ARGS; i++)
        {
            free(input_args[i]);
        }
    }   
    //free array of strings
    free(input_args);

    //new allocate memory
    input_args = (char**) malloc(sizeof(char*) * MAX_NUM_ARGS);
    //check return value for error
    if (input_args == NULL)
    {
        printf("We are out of memory. =(\n");
        continue;
        //print error: out of memory, exit with a error code
        exit(0);
    }
    //allocate memory for each string
    for (i = 0; i <MAX_NUM_ARGS; i++)
    { 
        input_args[i]= (char*)malloc(sizeof(char) * MAX_NUM_ARGS);
        if(input_args[i] == NULL)
            {//error
            printf("Error, the input is empty.");
            continue;
            }//end of if statement
    }//end of for loop


    printf("~$: "); //prompts the user for input
    fgets(buffer, sizeof(buffer), stdin);
    //if the user types in exit, quit
    if (strcmp(buffer, "exit\n") == 0){ 
        exit(0);
    } //end of if statement
    //if user types in clear, wipe the screen and repeat the lop
    else if(strcmp(buffer, "clear\n")==0){

        system("clear");    
        continue;   

    }//end of else if
    //should the user punch in nothing, repeat the loop
    else if (strcmp(buffer, "\n") == 0) {
        continue;
    }//end of else if


    input_args[1] = NULL;

    for (i = 0; i < SIZE; i++) {

        if(buffer[i] != '\n' && buffer[i] != ' ' && buffer[i] != '\t'){

            input_args[0][i] = buffer[i];
        }   //end of if statement
        else{
            input_args[0][i] = '\0';

        }//end of else statment

    }//end of for loop

    //if the input doesn't fall under the conditionals above, execute orders.
    orders(input_args);


   } //end of while loop
    return 0;

}//end of main function

void orders(char *command[SIZE]){
//handles the commands of the shell

    int retval = 0; //return value
    int pid = 0;
    int childValue = 0;
    pid = fork();

    if (pid != 0){
    //  printf("I'm the parent, waiting on the child.\n");//debug
        pid = waitpid(-1, &childValue,0);
    //  printf("Child %d returned a value of %x in hex.\n", pid, childValue);
        return;//return backs to the main prompt
    }//end of if statement
    else{
    //  printf("I am the first child.\n");
        retval = execvp(command[0], command);
        exit(2);
        if (retval != -1){
            //print error!
            printf("Invalid command!\n");
            exit(2);
        }
    }//end of else block


}//end of orders function

现在,它执行清晰,退出,单个字的命令刚刚好,像LS或pwd。然而,多行命令,如VIM不工作,也不改变目录。

Now, it executes clear, exit, and single word commands just well, like ls, or pwd. However, multi-line commands such as "vim " don't work, nor changing directories.

我在做什么错了?

我怀疑的RETVAL = execvp(指令[0],命令);是造成问题,但我不太完全肯定。有什么想法吗?我不想直接回答,因为这是功课,只是在正确的方向推。

I'm suspecting the retval = execvp(command[0], command); is causing problems, but I'm not too entirely sure. Any thoughts? I don't want a direct answer, since this is homework, just a push in the right direction.

推荐答案

本节:

input_args[1] = NULL;

for (i = 0; i < SIZE; i++) {

    if(buffer[i] != '\n' && buffer[i] != ' ' && buffer[i] != '\t'){

        input_args[0][i] = buffer[i];
    }   //end of if statement
    else{
        input_args[0][i] = '\0';

    }//end of else statment

}//end of for loop

限制 input_args 只已被使用的第一个索引。我想这是在哪里,你会找到一种方法,有一个 J ++; 中的其他条款,并使用 input_args [J] [I] 或类似的东西...

limits input_args to only have the first index be used. I assume this is where you would find a way to have a j++; inside the else clause and use input_args[j][i] or something similar...

和你最后的评论相符这一点,因为你的 RETVAL = execvp(指令[0],命令); 也只能用从列表中的第一项

And your last comment matches this, since your retval = execvp(command[0], command); is also only using the first item from the list.

这篇关于定制外壳只带一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆