ELI5 - How does a terminal emulator work?
I've tried browsing some source code and I'm lost. Can someone present a basic abstraction that helps me understand how a terminal emulator works?
About this post
- Posted
- Archived
- Score
- 30 · 100% up
- Kind
- Text
- ID
- 3ciful
18 comments
TL;DR a terminal emulator is like web browser that uses an old-fashioned protocol called "VT-100" rather than HTML to communicate, and instead of communicating with a web server, it is communicating with a single shell program like Bash, and rather than communicating over the Internet, it is communicating over a TTY file.
Every UNIX-like app (including Linux apps) are provided 3 channels by the operating system, the "standard input" (a.k.a.
stdin, file descriptor 0) which is used to feed bytes into the program, the "standard output" (a.k.astdout, file descriptor 1), which is used to feed bytes out of the program, and the "standard error" (a.k.a.stderris file descriptor 2), which is a channel that can report problems during computation without disrupting the "standard output".Your command line shell program, usually
bash, is just an ordinary program. Like all other programs, when you launch Bash the OS gives it astdin,stdout, andstderrchannels of communication. Often times you can feed input from a text file directly to Bash'sstdinchannel and it will behave as though a user typed every single character in that file -- with a few caveats, Bash can detect when it is receiving genuine user input or input from a text file and behaves slightly differently in each case.The operating system provides a special kind of file called a TTY (short for "teletype"). These TTY files are located in
/devThe/dev/ptsdirectory contains virtual TTY files that are entirely in-memory, and the TTY files located in/devthat have names like/dev/ttyS0or/dev/ttyUSB0are connected to physical serial ports on the computer (COM ports in Windows-speak). TTY files are not data files, but special files maintained by the operating system, as characters become available a program reads these characters using the same system calls that are used to read ordinary data files. The idea is that programs don't care whether they are being fed data by human users or other programs.So the first thing that a terminal emulator does when it is launched is ask the operating system to create a new virtual TTY file for it. The next thing it does is launch a program, which is by default
bash(you can edit the shell program you use in the terminal emulator settings, so you can have it launchpythonortcshorzshor whataver you like). When the terminal emulator launchesbash, it asks the OS to use the TTY file as thestdin,stdoutandstderr, so the same TTY file is used for all three channels for input and output to Bash.The terminal emulator program is a program designed to work in a graphical user interface, which means X11 (but it could be Wayland or Mir as well). So when you launched your terminal emulator in your GUI environment, your GUI is already catching all of the keyboard events and dispatching these events to various running, windowed programs. Since the terminal emulator is running in the GUI, it is also receiving keyboard events from the graphical user interface.
The terminal emulator is programmed to receive keyboard events from the GUI and translate these events to ASCII characters. The terminal emulator then writes these ASCII characters into the TTY, and
bashsees this as input fromstdinand can respond with output. The terminal emulator simultaneously reads output frombashon the TTY channel and translates these characters to fonts and draws a graphic of what the terminal looks like, and finally sends this graphic to the GUI so you can see it.The next thing you need to know is that there is a protocol called "VT-100" for communicating over TTYs. You aren't required to follow this protocol, but it allows you to do things like draw colored text, move the cursor arbitrarily around the screen, and set the name of the terminal window.
VT-100 is similar in function to what HTML does nowadays. The difference is, VT-100 is much, much simpler, and is designed for live interactive use, unlike HTML which is designed to be transmitted all at once.
The last thing you need to know is about the
termcapdatabase. This is a database of text files and directories that describes what a terminal can do. It allows a program like Bash to modify it's behavior so it can make decisions on how to display information, for example whether or not to use color text. If you typels, Bash will read thetermcapdatabase to see if color is available, and if it is, it will automatically spit-out the special ASCII characters as per the VT-100 protocol required to change colors.Other things the
termcapdatabase can do is tell Bash about whether the terminal window is resizeable and where to lookup the window size (how many rows and columns of text are there), whether or not the cursor can be placed arbitrarily, and whether or not the terminal automatically displays what the user has typed or does bash have to "echo" back the typed input to acknowledge that the input was received.I like most of this bar the ASCII parts.
stdin,stdoutandstderrdon't care what character set you use. It's just bytes to them. Try runningcaton a binary file for example. It will most likely ruin your TTY.I tried. Some random words and symbols are displayed on the terminal. What do you mean by ruin the TTY?
Super helpful, thank you.
I have a question on TTY files though. Is there a file for every program running? Or just files for physical and virtual hardware?
In general there is one TTY file for every terminal emulator window you open, not one for every program. A single Bash session can open multiple programs which will all be connected to the same TTY. (The rule is, when an app is launched, it inherits the same
stdin,stdoutandstderras the app that launched it unless you tell it otherwise.)Also, if you use a program like Tmux or GNUScreen, these programs will also create virtual terminal TTY files for each screen you create, so the terminal emulator will have one TTY communicating with GNUScreen, and then GNUScreen could have three or four windows open (each with it's own TTY) and it will redirect input from it's own TTY to the TTYs of whatever window is currently in focus.
If you want to see what TTY you are using in bash, just enter the "tty" command.
So you open a terminal which creates the virtual TTY file that bash uses for I/O. Is it possible to write to the TTY file using an external program, and cause bash to read it as input? Or is only the terminal emulator allowed to do that?
You can write to the TTY file but none of the input will go go Bash; the system wouldn't be very secure if that were allowed.
The rule for special files that behave as channels (sockets, FIFOs, and TTYs) is that as soon as a process opens the file for reading, it will communicate only with the first process to open the channel for writing. If a process wants input from other processes that write to the TTY, it has to open the TTY file again. Bash does not do this, but some apps, such as a server deamon, may open a channel over and over again as rapidly as it can so that it can serve as many clients as it can.
I guess you can think of the TTY file as a kind of meeting place, and once two procceses have met, they immediately leave, going off somplace together to communicate in private, but the meeting place is still there and can be used by other processes.
I cant get it to work right personally but if I chmod 777 the tty device and echo stuff to it, I can get text to appesr on screen
Same if I go into /proc and plsy around with the shells file descriptors
Can you in simple terms describe some of the best reasons why people should learn to use it and get familiar with it..?
Well, it is always a good idea to learn how your system works if you intend to use it often. The more you learn about it, the more efficiently you will be able to get your work done.
You don't really need to learn about how to program anything, but you should at least be aware that processes can start other processes, and that the processes can be linked together through pipes allowing the output of one program to become the input of another program, and from these building blocks (processes and pipes), you can construct an elaborate circuit of data flow.
The data flow between the GUI, the terminal emulator, the TTYs, and the shell program is just one of these circuits. There are all sorts of circuits of data flow in a typical operating system.
I recommend to absolutely everyone that you should at least learn the Bash scripting language. Bash has a very interesting way of programming these data-flow circuits.
Lets say you want to use the pipe the output of program
aaato the input of programbbb, and the output ofbbbshould pipe to the programccc, all you have to do in Bash is writeaaa | bbb | ccc.Most ordinary computer users have grown up with graphical user interfaces, so they have been trained from an early age to think of things in a computer as stuff that you can manipulate with a mouse. You can click it, you can drag it from here to there, you can activate it by double-clicking.
But learning Bash programming changes your way of thinking about computers. You stop thinking of things as "stuff you can manipulate with a mouse" and you start thinking of data as stuff that can flow from one place to another. You stop thinking of individual chunks of data in files and start thinking of data sets and relationships between data. Thinking like this really makes you an effective computer user.
Nice.
Wow. You should work on the ArchWiki.
If you were reading source code you probably don't need an ELI5 so here's some basic info instead:
Back in the day we had physical terminals which were just a keyboard and a screen but had no brains. You connected this terminal to a big brain (a mainframe) so you could send instructions and receive feedback.
Computers nowadays combine all three pieces together (a keyboard, a display and a brain) so physical terminals are no longer needed.
That's all a terminal (virtual or otherwise) does: receive input, do something with it and present the result somewhere.
But before you start trying to analyze source code you first need to understand some internal Unix concepts like standard streams, file descriptors, pseudoterminals, character devices, escape codes, character sets, shells, system calls, the C standard library, kernel headers, functions, macros, pragmas, etc. So yeah, it gets nasty.
Good explanation. I highly recommend that the OP reads "Advanced Programming in the UNIX Environment" book.
Thanks everyone!
Deleted before we saved it.
with very small magnets