Программа трубопроводов на C. Преобразование из пользовательского ввода в аргументы командной строки - PullRequest
0 голосов
/ 17 апреля 2019

Я должен был создать программу, которая должна была реализовать эксперимент с трубопроводом в C. Я сделал это методом получения команды из пользовательского ввода, но после ее завершения мне сказали, что я должен принять ее в качестве начальных аргументов командной строки, чтоабсолютно озадачиваю меня в том, как преобразовать это так, чтобы это взяло входы в этом поместье.Когда я сделаю это и объединю аргументы в строку и отправлю ее только через нее, она выведет длинный список из 0 или ничего для команды ls /usr/bin | wc -l

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/wait.h>

static char* args[512];
pid_t pid;
int command_pipe[2];

#define READ  0
#define WRITE 1

static int command(int input, int first, int last)
{
    int pipettes[2];

    pipe( pipettes );   
    pid = fork();


    if (pid == 0) {
        if (first == 1 && last == 0 && input == 0) {
            // First command
            dup2( pipettes[WRITE], STDOUT_FILENO );
        } else if (first == 0 && last == 0 && input != 0) {
            // Middle command
            dup2(input, STDIN_FILENO);
            dup2(pipettes[WRITE], STDOUT_FILENO);
        } else {
            // Last command
            dup2( input, STDIN_FILENO );
        }

        if (execvp( args[0], args) == -1)
            _exit(EXIT_FAILURE); 
    }

    if (input != 0) 
        close(input);


    close(pipettes[WRITE]);


    if (last == 1)
        close(pipettes[READ]);

    return pipettes[READ];
}


static void cleanup(int n)
{
    int i;
    for (i = 0; i < n; ++i) 
        wait(NULL); 
}

static int run(char* cmd, int input, int first, int last);
static char line[1024];
static int n = 0; /* number of calls to 'command' */

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

        for(int i = 1; i < argc; i++){
            strcat(line, argv[i]);
            strcat(line, " ");
        }

        /* Read a command line */


        int input = 0;
        int first = 1;

        char* cmd = line;
        char* next = strchr(cmd, '@'); /* Find first '|' */

        while (next != NULL) {
            /* 'next' points to '|' */
            *next = '\0';
            input = run(cmd, input, first, 0);

            cmd = next + 1;
            next = strchr(cmd, '@'); /* Find next '|' */
            first = 0;
        }
        input = run(cmd, input, first, 1);
        cleanup(n);
        n = 0;
    }
    return 0;
}

static void split(char* cmd);

static int run(char* cmd, int input, int first, int last)
{
    split(cmd);
    if (args[0] != NULL) {
        if (strcmp(args[0], "exit") == 0) 
            exit(0);
        n += 1;
        return command(input, first, last);
    }
    return 0;
}

static char* skipwhite(char* s)
{
    while (isspace(*s)) ++s;
    return s;
}

static void split(char* cmd)
{
    cmd = skipwhite(cmd);
    char* next = strchr(cmd, ' ');
    int i = 0;

    while(next != NULL) {
        next[0] = '\0';
        args[i] = cmd;
        ++i;
        cmd = skipwhite(next + 1);
        next = strchr(cmd, ' ');
    }

    if (cmd[0] != '\0') {
        args[i] = cmd;
        next = strchr(cmd, '\n');
        next[0] = '\0';
        ++i; 
    }

    args[i] = NULL;
}
...