0

I have to write a program that intercepts data from terminal and i have to parse it. After processing when the data, i have to parse it before it goes to stdout.

I can't use tee or commands like prog > file 2>&1 as the program is going to be interactive.

For example : If the user types ls in the terminal i have to parse it then it should go operating system and then when I get the result after processing I ll have to again parse it before it's displayed in the terminal.

I did my research and I think I can achieve it through pseudo terminal interfaces ( pty ).

Please let me know if there is a better way to achieve it. I am using cpp and bash and the platform is *nix.

Update: I can also use libexpect from expect.

quick-
  • 1,425
  • 2
  • 9
  • 13

1 Answers1

2

I am not sure what do you mean here - you mean interactive program as "working in another terminal communicating with user" or even displaying GUI?

How does it specify the terminal? It is probably important what is program layout here (which program starts which).

If your application uses GUI to communicate with user, then I would simply do it this way: start bash with sdtin and stdout attached to pipes, your program reads & writes to it's end's of those pipes, parses data, and reads/writes on it's own stdin&stdout - so it appears on it's terminal.

If you mean controlling different terminal than your application's, it gets though since system generally does not expect program operating on multiple terminals. I don't think it's possible to filter communication between terminal and already working application attached to it. Starting another process spawning another terminal might be an option - to have basically two terminals working in sync. But then you will have to synchronize both processes by some other means (named pipes, network connection or some other IPC).

If you provide more detail on your program I might provide more directed help.

PS Don't tell me that you are writing some terminal keylogger ')

EDIT:

Your program is probably GUI based then - what i would recommend would be something similar to answer linked by banuj.

Best option will probably be to create three pipes, then fork, and in child process assign corresponding ends of pipes to stdin, stdout and stderr. Then child process should exec into shell - probably bash, although I am not sure if other shells would sound better if read out loud ;) Main process will be able to read/write other ends of mentioned pipes, parsing both inputs and outputs to bash and programs it runs. You could also exec directly to commands user specifies, but that forces you to take over tedious job of a shell - managing current directory, environment variables, job control and so on.

Using above method might however cause some trouble - some programs (usually in security related contexts - eg. su(do) asking for password) will try to bypass stdin/stdout anyway and read directly from terminal device. I am not sure what can you do in such case - programing your own terminal emulator would be an option, but I don't know if you want to go this deep into system programming for this.

If you want some code snippet's, if you don't know how to do above, just ask ;)

j_kubik
  • 6,062
  • 1
  • 23
  • 42
  • I am not writing a terminal keylogger :) I am writing a program that takes speech from the user and speech is converted into text using a speech to text converter. After that the conversion of speech i have to execute the command(s) based on the input from the user. So after the user gives the command i want to speak the status back to the user using a text to speech converter. I have got the speech to text and text to speech done but I am not able to get the status of the application and/or the output of the application. I am not getting the integration with the terminal. – quick- Apr 19 '13 at 07:09
  • @quick- In your particular case, it's not necessary to do 'tee' 'pty'. You can do a simple fork+exec or some pals. You should check http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c – banuj Apr 19 '13 at 13:09
  • @j_kubik : A code snippet would be great :). But i also need to work on the password as in authentication. Is there any other way to do that ? – quick- May 10 '13 at 19:17