Проблема с функцией в программе оболочки - PullRequest
0 голосов
/ 12 октября 2010

Эта программа отлично работает за исключением одного аспекта smarthistory ().Я не могу понять, почему, когда я ввожу номер команды из массива smarthistory, почему он не выполняется.После ввода команды для выполнения из списка ничего не происходит, даже оператор printf сразу после.Я использую компилятор gcc.

const int MAX_HISTORY=100;

const int MAX_COMMAND_LENGTH=64;

char history[100][64];
int historyCount = 0;
char smarthistory[100][64];
int smarthistoryCount=0;

void chopnl(char *s) { //strip '\n'
    s[strcspn(s, "\n")] = '\0';
}

void printHistory() {
    int i;
    for (i = 0; i < historyCount; i++)
        printf("%d | %s\n", i, history[i]);
}

void printSmartHistory() {
    int i;
    for (i = 0; i < smarthistoryCount; i++)
        printf("%d | %s\n", i, smarthistory[i]);
}

void isPartialMatch(char *commandQuery,char *history, int historyString)
{
    int lengthOfQuery=strlen(commandQuery);
    if(strncmp( history, commandQuery,lengthOfQuery)==0)
    {
        memcpy(smarthistory[smarthistoryCount++], history, strlen(history) + 1);
    }
    else
        return;

}

void smartHistory()
{
    char commandQuery[MAX_COMMAND_LENGTH];
    int commandNumber=0;
    int i=0;
    printHistory();
    printf("enter partial command:> ");
    fgets(commandQuery,MAX_COMMAND_LENGTH,stdin);
    chopnl(commandQuery);
    //printf("%d", strlen(commandQuery));

    for(i=0;i<=historyCount;i++)
    {
            isPartialMatch(commandQuery, history[i], i);
    }
    printf("SmartHistory Search Results\n");
    printSmartHistory();
    printf("enter a command number to execute:> ");
    scanf("%d", commandNumber);
    //chopnl(commandNumber);
    printf("command entered >");
    handleCommand(smarthistory[commandNumber]);
}
void placeInHistory(char *command) {
    // printf("command:> %s |stored in:> %d",command,historyCount );
    memcpy(history[historyCount++], command, strlen(command) + 1);
}

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

    while (1) {
        printf("SHELL:>");
        fgets(command, MAX_COMMAND_LENGTH, stdin);
        chopnl(command);
        if (strcmpi(command, "exit") == 0)
            return EXIT_SUCCESS;
        placeInHistory(command);
        handleCommand(command);
    }
    return (EXIT_SUCCESS);
}

int handleCommand(char *command) {
    pid_t pid;
    int test=0;
    pid = fork();

    if (pid > 0) {
        wait(&test);
    } else if (pid == 0) {
        execCommand(command);
        exit(0);
    } else {
        printf("ERROR");
    }
}

int execCommand(char *command) {
    //system(command);    

    if (strcmpi(command, "history") == 0)
    {
        printHistory();
    }
    else if(strcmpi(command, "smarthistory") == 0)
    {
        smartHistory();
    }
    else if (strcmpi(command, "ls") == 0 || (strcmpi(command, "pwd") == 0)) {
        char *path[] = {"/bin/", NULL};
        strcat(path[0], command);
        execve(path[0], path, NULL);
    }else{system(command);}
}

Ответы [ 3 ]

1 голос
/ 12 октября 2010

Еще раз:

char *path[] = {"/bin/", NULL};
strcat(path[0], command);

path[0] инициализируется с const char*, и вы не можете использовать strcat() для этого.

Еще один:

memcpy(smarthistory[smarthistoryCount++], history, strlen(history) + 1);

не должны ли оба, источник и назначение, быть одного типа, char*?

Кроме того, я бы предложил использовать char* history[MAX_HLEN]

вместо char history[x][y].

0 голосов
/ 13 октября 2010

Проблема решена, проблема была в том, что мне нужно было добавить '&' перед commandNumber в операторе: scanf ("% d", commandNumber);находится в функции SmartHistory.Мне кажется странным то, что я сделал еще одну версию этого, где я переместил «history» и «smarthistory» в командную функцию handle вместо функции execCommand.Когда я сделал это, программа напечатает приглашение оболочки 3 раза ... если кто-нибудь знает почему, пожалуйста, дайте мне знать.Однако в приведенной выше версии просто добавление '&' прекрасно работает.

0 голосов
/ 12 октября 2010

Я не уверен, но похоже, что обработка "history" и "smarthistory" должна быть перемещена с execCommand на handleCommand и должна происходить ВМЕСТО разветвления.Могут быть и другие ошибки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...