Nightmares about Fortran70, abusing UNIX and other connected delusions
A friend of mine set EikaShell as his default shell. As in, his actual login shell for his user, which is an important distinction because I had only ever been using it in a terminal emulator and there are things a login shell apparently needs to do which I hadn’t spent much time thinking about.
On my Arch machine with SDDM it worked. On his Fedora machine with GDM it was rather less successful. SELinux complained, we made some unsafe decisions in an effort to get past that, and then GDM refused to let him into his desktop. We ended up on a TTY changing his login shell back. It was fine in the end. I think it was at about this point that I realized the software I’d been making for an operating-systems assignment had become something you could accidentally entrust with rather a lot of your computer.
Anyway, why did I write the shell?
cat file was getting annoying
Our OS class wanted a little shell, read a line, parse a command, fork, exec, redirect some file descriptors, do a pipeline or two. I had a basic thing working in around 300 lines and I probably could have stopped there.
I had to demonstrate it, though, which meant repeatedly typing filenames and paths when I wanted to show that cat or something worked. I hate writing out filenames. Tab completion exists. Surely I could just add Tab completion.
This was the beginning of a process which has since produced several thousand more lines of C++.
At first I just wanted the name to fill in. Then I wanted to see all the candidates. Then I wanted a little window you could scroll through, select a candidate, preview it on the line, cancel it, use history, undo things properly, maybe add some custom keybindings… the amount of trouble you can create by wanting the shell to feel nice is extraordinary.
You can have your terminal back, please
The terminal driver normally does a bunch of convenient things when you’re entering text. You press a key and it appears, you press Backspace and something disappears, Enter gives you a line. As soon as you want custom keybindings and interactive editing you start taking control of parts of that behaviour yourself, which meant learning about canonical input, echo settings, raw and half-raw terminal modes, and the fact that the terminal hands you bytes rather than the abstract concept of a key.
An arrow key arrives as an escape sequence. A paste might be bracketed. There are different sequences for different kinds of keys. You have to decide how long to wait if an Escape might be the start of something else. Then UTF-8. Then drawing. The list just gets longer when you start looking at it.
I remember going down an increasingly strange rabbit hole reading about terminals, column widths and why so many conventions seem to assume a particular number of characters per line. This led to punch cards and the old fixed-form Fortran conventions, which is a very funny place to end up when all you wanted was to make a filename complete when you press Tab. That’s where the Fortran part of the title came from. I think I spent more time reading than was strictly necessary. A recurring problem with me.
I also realized I couldn’t just keep the entire input interaction as one big state machine made of random special cases, because every new feature would need to know about every previous one. I had originally planned reasonably clean abstractions and the outside API barely changed, which was wonderful, but inside that input function I kept discovering some new kind of state that belonged somewhere else.
The line that wasn’t actually the line
Completion was the worst for this. You type part of a path. You press Tab. A menu appears with candidates. You move the highlight and the command line updates to show the candidate you might accept. Then Escape. You want exactly the original buffer back, cursor in the right place, nothing strange on the undo stack. That sounds simple until you realize the previews have been going through the same insertion code as real edits and your undo history now contains things the user never committed to doing.
So a completion session needed an anchor, the original text and cursor position, and each preview had to be calculated from that. Then accepting a candidate makes one edit, and cancellation means throwing the preview away. It took more internal rewrites than I want to count before I was happy with how completion, undo/redo and history search interacted. Most of the rewrites were invisible from the outside. A function kept its name while I moved what it owned somewhere else. Very convenient for scope creep. Very easy to convince myself the next feature would be harmless.
There’s also a scrolling panel to draw, underneath a prompt that can contain ANSI colours and wrap across multiple terminal rows, and after the redraw you have to put the cursor back where it belongs. Terminal resize. Long commands. A menu longer than the available screen space. What if the cursor is on the second line, where is the visual second line, and how much of the prompt exists as bytes which don’t take any space on the screen? I kept thinking I was almost done with the renderer and then finding another case where it had decided to place the cursor somewhere ridiculous.
The input-system documentation describes the layers and the project page gives a cleaner view of the architecture. There is a satisfying system behind it now. Getting there was more like slowly pulling apart something I’d made ad hoc and discovering that each piece had quietly become dependent on three others.
And now?
The shell has a few thousand lines more than the class assignment required, and the feature I still most enjoy showing people is that little completion window. Which is probably fine. I have ideas for a scripting language in the future, but I’m leaving it alone for a bit because I have approximately a million other things to work on and, for once, the input editor mostly behaves.
I still occasionally think about the Fedora login incident. The shell can be fairly well behaved in a terminal and still make a computer seem very dead if you ask the login manager to depend on it. We were lucky that a TTY was there waiting for us.