Untitled

 avatar
unknown
plain_text
3 years ago
2.3 kB
4
Indexable
char* yytextstr = yytext;
        char* yyTextSubstring = new char[strlen(yytext) - 1];
        int index = 0;
        for(int i = 2; i < strlen(yytext) - 1; i++){
                yyTextSubstring[index++] = yytextstr[i];
        }

        yyTextSubstring[index++] = '\n';
        yyTextSubstring[index++] = 'e';
        yyTextSubstring[index++] = 'x';
        yyTextSubstring[index++] = 'i';
        yyTextSubstring[index++] = 't';
        yyTextSubstring[index++] = '\n';
        yyTextSubstring[index++] = '\0';

        std::string command(yyTextSubstring);

        printf("command is %s\n", command.data());
        int parent[2];
        int child[2];
        if(pipe(parent)){
                perror("parent error");
                exit(1);
        }
        if(pipe(child)){
                perror("child error");
                exit(1);
        }

        int tempIn = dup(0);
        int tempOut = dup(1);
        int tempErr = dup(2);

        dup2(parent[0], 0);
        dup2(child[1], 1);
        close(parent[0]);
        close(child[1]);

        write(parent[1], command.c_str(), command.size());
        close(parent[1]);

        int pid = fork();
        if(pid < 0){
                std::vector<char *> arguments;
                arguments.emplace_back(const_cast<char *>(std::string("/proc/self/exe").c_str()));
                arguments.emplace_back(nullptr);
                execvp(arguments.at(0), arguments.data());
                perror("execvp in subshell");
                exit(1);
        } else if(child < 0){
                perror("forking subshell error");
                exit(1);
        } else {
                waitpid(pid, NULL, 0);
                dup2(tempIn, 0);
                dup2(tempOut, 1);
                close(tempIn);
                close(tempOut);

                char * buffer = new char[1024];
                read(child[0], buffer, 1024);
                close(child[0]);

                for(int i = strlen(buffer); i >= 0; i--){
                        if(buffer[i] == '\n'){
                                myunputc(' ');
                        } else {
                                myunputc(buffer[i]);
                        }
                }

                delete buffer;
        }
Editor is loading...