Writing to output the same time the user is writing - c

I want to write to the terminal at the same time the user is inputting a command for my program. For example, when the user is trying to print 'help', it could end up looking like this:
heNew packet with length 233
lp
I'd like to be able to have command input just below where everything is outputted, so it would look something like this:
New packet with length 223
Sending x to y..
...
>help
I want to avoid using a library for this. The program is not cross-platform so a linux-only solution would work.

This will require that you manage the raw input and output to the screen. You say that you don't want to use a library but I would strongly encourage you to have a look at ncurses. If you really want to build this from scratch you will be reinventing lots of wheels. :)

Related

Delete current terminal input line

I am trying to code a little console chat-program in C on linux.
So far I coded it in a way that both chatting partners are only able to alternately send/recv, because these function calls are blocking by default.
Now I would like to modify that program, so that both are able to send and receive simultaneously.
The problem that I find is, that once you typed some input to the terminal, I don't know how to output received messages, without messing up the current input line of the terminal.
If there was a way to delete that current input line, you could temporarily save that line, print the new message and put the input line right back.
However, I was not able to find a solution for this problem on the internet.
Is it possible to delete the current input line, and if not, how else could I achieve what I want?
I think you should look into ncurses as Edd said in his comment.
It would allow you to easily manage contents in your terminal window, which sounds like a good idea for your chat program.
All you'd need to do is store your messages in 2 character arrays:
char incoming[MSG_MAX]
and
char outgoing[MSG_MAX]
Then you can output those messages wherever you want in your terminal window, since ncurses allows you to specify x,y coordinates on where to put your text.
Then a simple wrapper for one of ncurses erase() family functions would allow you to delete characters from specify x,y coordinates in your terminal window.
Edit: MSG_MAX is not an actual ncurses macro.

C: open second custom sized terminal and use it for output

I am writing a program in C that is started simply by the terminal. Now I want to make the program itself open another terminal with a custom size and write its output in there.
I found the command
system("gnome-terminal");
which opens another terminal, but I can't find a function that lets me write into this second terminal. I am using Ubuntu.
If you have any idea, that would be great
The easiest thing would probably be to write the output to a file, say /tmp/tmp96888 (tip: mkstemp) and then do something like
system("gnome-terminal --geometry=40x14 --command 'less /tmp/tmp96888'");
Or, to update continuously from the file:
system("gnome-terminal --geometry=40x14 --command 'tail -f /tmp/tmp96888'");
But if you can, I think the best way is to open a new terminal and run the program itself in it, and just print the output. It's only if you actually need to do things in the original window that you have to bother with a separate output window.

How to get started? Simple text editor with C

I have to do a simple text editor using the c language. But I don't know how to get started. The program should begin with a blank screen, waiting for the user to write something. The user is also able to move the cursor using the arrow keys and the user is also able to load text files to the program from his hard drive and also make a text file of something the user has written in the program.
He can load something by pressing CTRL-O or he can press CTRL-S to save something followed by the name of the text file.
Our professor said that we have to use the library screenUtils to make the cursor move.
How should I start? I'm trying to make this work for 3 hours but nothing really works.
Our professor said that we have to use the library screenUtils to make the cursor move. How should I start?
I would start by reading the documentation for this library.

Recording command line input and output on linux with C

Basically I want to do a program almost like a keylogger. The thing is that I as network admin sometimes I don't remember what I did to a machine on certain case, or same times I make howto's and tutorials for linux. I want to record what have i done.
So basically the idea of this program is:
you type the name of the program, (I call it rat for the moment)
$ rat
Welcome everything from now on will be recorded
recording $ ls
file1 file2 file3
recording $ quit
Bye bye
Everything you do will go out to an xml file. Something like this
<?xml version='1.0' encoding='UTF-8' ?>
<rat>
<command>
<input>ls</input>
<output>file1 file2 file3</output>
<err><err>
</command>
</rat>
i am doing some tests with fp_in = popen( input, "w");
and system, but first with popen i cant change directories and with "system i cant properly manage the input and output.
I was also checking if there is something I can do to bash like a plugin but haven't find any information.
At some points if feels like it I should create another shell (which is way beyond my current abilities) or fork bash sh. But it should been that complicated right.
I am open to suggestion where to start.
I am rusty with C, so I am reading again a lot of basic stuff.
With the xml file, later i was thinking on making a program to store this data and/or editing this data so i can create tutials and howto.
I can think of many ways of expanding this up to using printscreen so all the stored images go to a file you can upload to a server (for the moment i am glad to store the data). It could be a usefull tool.
ps. I do know this can be use for evil things too.
There already exists the script command, which will record all input and output into the terminal, writing it into a transcript. I would recommend just using that, unless you have particular needs that it doesn't meet. Actually, the nicest version of script that I've seen has been the NetBSD version, so you may want to look into that if the Linux version doesn't meet your needs.
If you would like to write it yourself, instead of using system, I would recommend that you use fork/exec to create a single shell process, which you copy all input and output into. To get an idea of how this works, I'd recommend looking at the source code for an existing version of script.
Most shells have a script built-in which will simply record the text in- and out- from the command line. Not quite what you're looking for... To my surprise script is not a built in, which means it is a model for building what you want.
The script command does almost what you want: it simply records the text in- and out- from the command line.
If you make your prompt distinctive (so that you can reliably tell the difference between shell commands and everything else) you can post-process the output of script to achieve your goals. Alternately you can hack script to get it to emit the XML you're looking for.
You can also try approaching this from a different angle. Instead of using a regular shell, connect to the machine using ssh or telnet and run your commands that way. Many ssh/telnet clients (PuTTY, for instance) have an option to log all console input and output during the session. You should be able to post-process this log to generate whatever type of logfile that you need.
Depending on your setup, you might not even have to use a second machine (you should be able to ssh into yourself).

Unable to put a simulation in C to a webpage

I have the following simulation in C for cars in a traffic circle
alt text http://dl.getdropbox.com/u/175564/cars.png
For example,
7 <space> <enter>
gives more cars to the simulation, while
s1 <enter>
puts a top sign to the incoming road #1.
I want to put the simulation to an internet page such that users can try it.
However, I do not know where I should start.
I know Joomla and little about Django. Perhaps, they can be useful.
How can you put a C program to a Webpage such that users can use it?
CGI is what you want; it will let you embed any program you want into a website, it was made for that purpose. Then perhaps embedding a few more options with PHP and HTML will let the user acutally input data into the program via the web. It should not be too hard.
Take a look here for more info: http://www.cs.tut.fi/~jkorpela/perl/cgi.html
I think that is a good pointer in the right direction. I hope that helps.
You could run the C code in a flash app using alchemy:
http://labs.adobe.com/technologies/alchemy/
Here's a good intro to writing CGIs in C: http://www.cs.tut.fi/~jkorpela/forms/cgic.html
However, since you're a beginner, I'd recommend porting your program to PHP. It's a very easy language to pick up, and it's a much easier route than writing a CGI in C.
I assume the C program needs input while it's running and not via command-line arguments? If I'm wrong, you can just use PHP and shell_exec() to run the program. The function returns anything printed to stdout.
Such a page might look like:
$sim = shell_exec("/path/to/binary -a 5 -b 6");
echo $sim;
Where the string passed to shell_exec is exactly what you'd type on the command line.
If you want a dynamic simulation, where the cars moves while you watch , you'll need an applet or flash.
A cgi program renders the page on each http GET/POST (on reload, submit etc)
and that is probably not what you want.
I would start with a flash or java applet wrapper. You can communicate with your application over a tcp connection and display the results in the flash or applet.
I would recommend going with sockets. If your C program could set up and listen on a local or internet socket, you could use the socket facilities in any language to send it arguments and get output.
If that's going to be too much of a pain, have php exec the program while directing the output to some file. Then, have php read that file.
Looking at the output of your program, I think trying to print the results of shell_exec() will result in clobbered output.
So, you could shell_exec("/bin/program -arguments > /tmp/prog-tmp.txt") , then read prog-tmp.txt.

Resources