简体   繁体   English

当有人通过信号处理输入“ CTRL-C”时,在我的shell的历史记录缓冲区中输出最后10个命令

[英]Outputting of last 10 commands in a history buffer in my shell when someone enters “CTRL-C” with signal handling

I am trying to keep track of the last 10 commands in my shell, and have the ctrl + c signal handler output an array of them. 我试图跟踪外壳中的最后10个命令,并让ctrl + c信号处理程序输出它们的数组。

Obviously this array only contains 10 elements and always outputs 10; 显然,该数组仅包含10个元素,并且始终输出10个元素; this is just temporary until I get the functionality correct. 这只是暂时的,直到我得到正确的功能。

At the moment when I have more than one command in the shell history, it outputs the array multiple times in a weird order with some things output more than others. 当我在shell历史记录中有多个命令时,它以怪异的顺序多次输出数组,其中某些东西比其他东西输出更多。 I am guessing this is caused by child processes also calling the handler as well, but I don't know what to do at this point. 我猜这是由子进程也调用处理程序引起的,但是我目前不知道该怎么办。 .

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>

#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
#define BUFFER_SIZE 10

char buffer[BUFFER_SIZE][MAX_LINE];
//char buffer[BUFFER_SIZE];
int pos = 0;
int handleEvent = 0;

void handle_SIGINT()
{
   int i = 0;
   while (i < BUFFER_SIZE)
   {
      //printf("%d\n", i);
      write(STDOUT_FILENO, buffer[i], strlen(buffer[i]));
      printf("\n");
      i++;
   }
}


void setup(char inputBuffer[], char *args[],int *background)
{
    int length, /* # of characters in the command line */
    i,      /* loop index for accessing inputBuffer array */
    start,  /* index where beginning of next command parameter is */
    ct;     /* index of where to place the next parameter into args[] */

    ct = 0;

    /* read what the user enters on the command line */
    length = read(STDIN_FILENO, inputBuffer, MAX_LINE);  

    start = -1;
    if (length == 0)
        exit(0);            /* ^d was entered, end of user command stream */
    if ((length < 0) && (errno !=EINTR)){
        perror("error reading the command");
    exit(-1);           /* terminate with error code of -1 */
    }

    /* examine every character in the inputBuffer */
    for (i = 0; i < length; i++) { 
        switch (inputBuffer[i]){
        case ' ':
        case '\t' :               /* argument separators */
            if(start != -1){
                args[ct] = &inputBuffer[start];    /* set up pointer */
                ct++;
            }
            inputBuffer[i] = '\0'; /* add a null char; make a C string */
            start = -1;
            break;


        case '\n':                 /* should be the final char examined */
            if (start != -1){
                args[ct] = &inputBuffer[start];     
                ct++;
            }
            inputBuffer[i] = '\0';
            args[ct] = NULL; /* no more arguments to this command */
            break;

        case '&':
            *background = 1;
            inputBuffer[i] = '\0';
            break;

        default :             /* some other character */
            if (start == -1)
                start = i;
        } 
    }    
    args[ct] = NULL; /* just in case the input line was > 80 */
} 

int main(void)
{
    char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
    int background;             /* equals 1 if a command is followed by '&' */
    char *args[MAX_LINE/2+1];/* command line (of 80) has max of 40 arguments */

    /* set up the signal handler */
    struct sigaction handler;
    handler.sa_handler = handle_SIGINT;
    //handler.sa_flags = 0;
    //sigemptyset(&handler.sa_mask);
    sigaction(SIGINT, &handler, NULL);

    while (1){            /* Program terminates normally inside setup */
        background = 0;
    printf("COMMAND->");
        fflush(0);
        setup(inputBuffer, args, &background);       /* get next command */

    strcpy(buffer[pos], inputBuffer);
    pos++;

        int pid;
    int returnValue;

    pid = fork(); /* for a child process */

    if (pid < 0) { /* check if error occurred with child process */
      fprintf(stderr, "Fork Failed");
      return 1;
    } 
    else if (pid == 0) { /* child process */
      execvp(args[0], args);
    } 
    //else {
      if (background == 0){ /* check if the parent should wait */
        //wait(&returnValue);
        waitpid(pid, NULL, 0);
      }
   //}

    /* the steps are:
     (1) fork a child process using fork()
     (2) the child process will invoke execvp()
     (3) if background == 0, the parent will wait, 
    otherwise returns to the setup() function. */
    }
}

I see at least two things: 我至少看到两件事:

  • execvp WILL return if there is an error (eg if you try to run an unknown command), so you need to add a return statement after execvp. 如果有错误(例如,如果您尝试运行未知命令),execvp将会返回,因此您需要在execvp之后添加一个return语句。
  • read will not read the end of string char so you need something like inputBuffer[length] = '\\0'; read不会读取字符串char的结尾,因此您需要输入inputBuffer [length] ='\\ 0';

Moreover when you hit Ctrl+CI think you also return from read() and add the last value of inputBuffer to your history. 此外,当您按Ctrl + CI时,您还应该从read()返回,并将inputBuffer的最后一个值添加到历史记录中。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM