I've not done much building of a terminal emulator beyond simple fgets(). If I've been in a programming environment where there are graphical entry fields which are pre-defineable by the tools, then I've been able to do "nice" looking stuff. For instance designing a Windows Form using Visual Studio. Or in Qt where you can use a Widget where you get an entry field and it manages things like the cursor or other.
My point there being, what I'm showing here is very basic, limited to the command prompt or terminal in which you are running this program and if you desire something more graphical, then you'd need to use a graphical development environment such as one of the one's I've cited above; obviously a correct one for the target OS you're working in.
The most basic thing you can do is the following:
term.c sample:
Code:
#include <stdio.h>
void main(void)
{
char inStr[256];
printf("Enter your string: ");
if(fgets(inStr, sizeof(inStr), stdin)) {
printf("You entered: %s", inStr);
}
return;
}
Compile and run this via the following example:
Code:
~/testcode$ gcc -o term term.c
~/testcode$ ./term
Enter your string: This is my test text, and I intentionally made a mistake and used backspace. See, it's still correct and includes the carriage return.
You entered: This is my test text, and I intentionally made a mistake and used backspace. See, it's still correct and includes the carriage return.
~/testcode$
Explanation:
gcc is the gnu C compiler, you either have it or you don't, it will compile C and C++ code.
-o means redirect the output to the following name "term" because if you don't do that, then it makes the output result be "a.out"
The result is therefore an executable named "term", I ran it and it did what I expected. It printed my prompt and allowed me to enter a line of text, up to 256 characters.
I did not try to over-run that and see whether or not it worked; you can explore that and make input foolproof. But if you read the text, I intentionally typed wrong. OK I "really" did make a mistake because I always tend to type "test" instead of "text", so I backspaced to correct it, wondering "Hmm ... is this gonna show up weird when it prints, or will it interpret the backspaces correctly?" Well, it got it right, but that may be a function of my terminal's settings. The points there are, you can be totally simple like that example; however you ought to test and see if you can detect and adjust, correct, or warn the user if they do something wrong. Like, typing beyond your limits, you'll likely take only 256 characters here, so let them know they exceeded your max, or consider a higher max.