Terminal emulator from scratch.


 
Thread Tools Search this Thread
Top Forums Programming Terminal emulator from scratch.
# 15  
Old 09-14-2010
Ok. I'am finally beginning to see where the word emulator comes in now. So apart from isatty(), are there a number of other calls to watch for, is there documentation online for this.
Strange, but I made a console application and redirected the stdout and stderr the way you instructed, but to a file and it worked just fine. How did the isatty() function not pickup on this?
# 16  
Old 09-14-2010
Quote:
Originally Posted by howdini
Strange, but I made a console application and redirected the stdout and stderr the way you instructed, but to a file and it worked just fine. How did the isatty() function not pickup on this?
Most utilities don't care, and shouldn't care. Their output is expected to be piped and redirected in creative ways. Only things like interactive editors and login systems check isatty().
# 17  
Old 03-13-2011
Hi Corona688

Thanks for the help so far. With the time I have got, I managed to put together the app this far. Currently my only problem is I cant seem to send any keyboard input to the child process program. Please take a look at my code and advice.

Code:
       
       #include <sys/wait.h>
       #include <assert.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>
       #include <string.h>
       #include <error.h>

int main(int argc, char *argv[])
{
    int MAXLINE = 2000;
    int fd1[2];
    int fd2[2];
    pid_t pid;
    char line[MAXLINE];
    char *PROGRAM_B_INPUT= "fake input\n";
    char *PROGRAM_B = argv[1];

    error(0,0,"Starting [%s]...", PROGRAM_B);
    if ( (pipe(fd1) < 0) || (pipe(fd2) < 0) )
    {
        error(0,0,"PIPE ERROR");
        return -2;
    }
    if ( (pid = fork()) < 0 )
    {
        error(0,0,"FORK ERROR");
        return -3;
    }
    else  if (pid == 0)     // CHILD PROCESS
    {
        close(fd1[1]);
        close(fd2[0]);

            if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
            {
                error(0,0,"-- CHILD --    dup2 error to stdin");
            }
            close(fd1[0]);


            if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
            {
                error(0,0,"-- CHILD --    dup2 error to stdout");
            }
            close(fd2[1]);

        if ( execl(PROGRAM_B, PROGRAM_B, (char *)0) < 0 )
        {
            error(0,0,"-- CHILD --    system error");
        perror("ERROR DEFN : ");
            return -4;
        }

        return 0;
    }
    else        // PARENT PROCESS
    {
        int rv;
        close(fd1[0]);
        close(fd2[1]);

        if ( write(fd1[1], PROGRAM_B_INPUT, strlen(PROGRAM_B_INPUT) ) != strlen(PROGRAM_B_INPUT) )
        {
            error(0,0,"READ ERROR FROM PIPE");
        }

        if ( (rv = read(fd2[0], line, MAXLINE)) < 0 )
        {
            error(0,0,"READ ERROR FROM PIPE");
        }
        else if (rv == 0)
        {
            error(0,0,"Child Closed Pipe");
            return 0;
        }

        error(0,0,"OUTPUT of PROGRAM B is: \n%s", line);

        return 0;
    }
    return 0;
}


Last edited by pludi; 03-13-2011 at 07:48 PM..
# 18  
Old 03-13-2011
You having put it in quote tags instead of code tags means I can't even quote it to get the program with sane indentation and must do it all by hand.. I'll take a look ati t...

---------- Post updated at 04:53 PM ---------- Previous update was at 04:45 PM ----------

Your program actually worked without modification, first try:
Code:
$ ./a.out /bin/cat 
./a.out: Starting [/bin/cat]...
./a.out: OUTPUT of PROGRAM B is: 
fake input
��
$

...though you forget to null-terminate your read string, read doesn't do that for you!
Code:
...
                line[rv]='\0';
                error(0,0,"OUTPUT of PROGRAM B is: \n%s", line);

Code:
$ ./a.out /bin/cat
$ ./a.out /bin/cat 
./a.out: Starting [/bin/cat]...
./a.out: OUTPUT of PROGRAM B is: 
fake input
$

And depending on your system the pipe may actually buffer and wait forever. You should close() the pipe after you're done writing to it, to force it to send the stuff along before you try and read() from the other pipe.
# 19  
Old 03-14-2011
Sorry about the QUOTE tags, just now seen the CODE one.
I suspected something of the sort, but didn't know whether it was a \n escape that was needed or something else. I guess now my biggest challenge is how to continuously send and receive text from a more interactive program. I was testing it with passwd and was unable to control the input and output correctly. Please help me understand how I can achieve this successfully. I suspect some looping will be required, don't have a clue where to start with that with all the pipe logic.
# 20  
Old 03-14-2011
Quote:
Originally Posted by howdini
I was testing it with passwd and was unable to control the input and output correctly.
Quote:
Originally Posted by Corona688
Only things like interactive editors and login systems check isatty().
It's no mystery why passwd refuses to work with pipes. I also warned that interactive programs would do this.
# 21  
Old 03-14-2011
The only time I received an isatty related error was with a few interactive types of programs like top. It said that the tty check failed, just as you said some of them would. passwd on the other hand launches well and requests for some input. getting it this input and retrieving the next output is proving to be the challenge. I hope you are not saying that this method completely cannot work with interactive programs. If it is possible, I would be grateful for some pointers on how to go about it.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Terminal Emulator

Hi, I was just wondering how to distinguish between the two terms: 1. Terminal emulator (vt100, vt220 and so on) 2. shell command line Then i decided to conclude myself that these 2 are very equivalent. am I right? this actually came to my mind when I was using my HP-UX terminal. I am... (1 Reply)
Discussion started by: messi777
1 Replies

2. Solaris

Tera Terminal Emulator

Hello Expert! :b: Question for you guys, Can anyone tell me how to use terminal emulator on Windows XP to view Solaris config? I have no idea on Solaris and the only thing I could do is to boot it up. Honestly, I have given a tasked to delete all the files and some necessary memory information... (2 Replies)
Discussion started by: katsloko
2 Replies

3. UNIX for Advanced & Expert Users

Filesystem from scratch

Hey, Had anyone tried with writing a new FS - file system ( whether its useful or not, that doesn't matter ) ? I tried one couple of years ago, but that was a fatal failure :( and can't continue working on it since then. :( Anybody got some experience with writing file system from the... (4 Replies)
Discussion started by: matrixmadhan
4 Replies

4. UNIX for Advanced & Expert Users

unix from scratch

hi all, i'm trying to write a unix system from scratch (not re-writing the kernel) does anyone have information about that? tips and stuff...?i would appreciate every help, thnks :) (9 Replies)
Discussion started by: elzalem
9 Replies

5. Linux

how to use terminal emulator???

hello, can any body tell how to use terminal emulator.... i want to check he serial port communication with the help of that terminal emmulator.... also tell me how to open terminal emmulator.....and how to configure it........and how to use it... I am using fedora core 6..... (1 Reply)
Discussion started by: arunchaudhary19
1 Replies
Login or Register to Ask a Question