Where to start creating a Terminal Emulator from scratch
Before I get started I need to link you to this great answer on the Unix & Linux Stack Exchange which explains the differences between a Terminal (tty), a Console, and a Shell. That knowledge will be key to writing a terminal emulator.
VT100 (and beyond)
Setting the stage with a little background.
Most terminal emulators claim to be VT100 emulators. VT100 came out of DEC somewhere around 1977. There were a number of subsequent versions (52,102,220,330,420,510,520). The problem is, that VT100 is a rather limited subset of what people expect:
- VT100s have no function keys.
- VT100s do not do color.
But there is some relevant stuff that came out of this. Eventually the geeks of the world said “Hey, we should probably standardize this stuff”, and that resulted in these standards:
- ECMA-35, Character Code Structure and Extension Techniques (ISO/IEC 2022:1994)
- ECMA-43, 8-Bit Coded Character Set Structure and Rules (ISO/IEC 4873)
- ECMA-48, Control Functions for Coded Character Sets, (ISO/IEC 6429)
Today XTerm is the 500lb gorilla in the room. Modern xterm (as of 2013) implements 546 control sequences. Most VT100 emulators (VT102 really) only implement 68. That being said, there are a good number of competitors to xterm which claim to emulate “most” of XTerm’s controls, the reality is that “none of the other terminal emulators emulates “most” of xterm. Instead, they implement the most commonly-used control sequences, and there are differences between them”.
The takeaway is that, XTerm may be bad-ass, but you don’t need “most” of it to create something that’s very useful. Even better, you can use XTerms list of control sequences as a todo list of things to implement. Even better, if you want to create something that is functional enough to use Vim & Emacs in, then all you need is VT100(ish).
Some Example Projects whose source you can explore
black-screen looks like a promising step into the future for terminal emulators / interactive shells. It’s a node.js app with an Electron front end. It doesn’t have full VT100 support yet, but it’s pretty usable at this point. I would note that it’s creator uses ZSH so support for Bash aliases is still coming. Because its UI is html, the possibilities open to it are pretty limitless. The things that TermKit promised us back in 2011 can finally be realized with this, or some clone / descendent of it.
iTerm 2 is arguably the best OS X terminal emulator available, and fortunately for you, it’s GPL Licensed and written in Objective-C.The source code is here on Github
There’s Terminal.py which is a terminal emulator written in pure python. Being Python I suspect its source code would be a bit easier to wrap your head around (less low level stuff to deal with relative to an Obj-c codebase). It claims to support XTerm emulation, but I can’t say how much of xterm it actually emulates.
Terminal.js. This one’s a little different: Node.js sets up a wee server on your desktop and you interact with it via a Terminal.js which you load up in your browser. There’s a demo app for it. Not sure what exactly that gets you.
And of course, there’s XTerm but I suspect that it’s not a great place to start. Too much functionality built up over too many years. I wouldn’t go there until I’d gotten a handle on the basics.
Much of the info here was gleaned from this page on vt100.net, and the XTerm FAQ.