Conversation
Edited 21 hours ago

Reminded of this in conversation recently: possibly the nastiest piece of abuse of the C switch statement rules I know of that _wasn't_ done by either me or Tom Duff. This was contributed to 's terminal code in 2000 as part of a much larger patch (and removed again in a 2001 rewrite).

The switch is processing the final character of one of the terminal escape sequences ESC#3, ESC#4, ESC#5, ESC#6 and ESC#8. Four of them do something very similar: they set an attribute on the current terminal line (indicating whether the text is double-width or double-height). The fifth is the odd one out. So you want a switch statement in which four of the five cases share _most_ of their code: each sets the line attribute to a different value, but the code for actually writing it into the terminal is the same.

You could write four case statements sharing a handler, and have a secondary switch inside the handler to choose the attribute. Or you could have four separate handlers that choose an attribute value, then hold your nose and write a 'goto' from each one to the shared code that writes it into the terminal data. Or you could put the shared code in a subroutine a long way away from here, and have four independent case handlers all call it with a different parameter.

Or you could do this, which complies with the letter of 'don't use goto', but makes you wish it hadn't! Control enters at one of the four case statements, and after setting the attribute variable, it then skips over each of the following case labels because of the if (0) { ... }. Then all of them fall through to the shared handler.

3
3
1

This code was in PuTTY for less than a year. But in that time it managed to expose a bug in a C compiler.

It was a small, independently written compiler (none of the huge code bases everyone's heard of now). We had an email from its maintainer to let us know – and to ask us what the code was _meant_ to do, because fair enough, it isn't obvious to everybody at first glance!

The bug was that the compiler optimised away if (0) { ... } statements, on the theory that they were unreachable. But the code in these if (0) statements _is_ reachable, via the case labels related to the containing switch. So the compiler had thrown away code that was actually needed, and then got confused when it couldn't generate the jump for those switch cases.

(The more usual strategy is to start by turning the whole function into a control-flow graph, i.e. blocks of straight-line code in a directed graph connected by edges that are effectively gotos. Then you can optimise conditional branches based on a constant into unconditional branches, deleting the never-traversed graph edge out of their block. Then you detect unreachable code by a search through the graph from the function entry point. That strategy avoids this problem.)

1
0
0

The fifth escape sequence in the switch, ESC # 8, is one of the most fun ones in the whole terminal. It instantly replaces the entire screen contents with copies of the capital letter E.

In DEC's documentation it goes by the name DECALN, and is described as an alignment test. I guess the point is that it's a quick way to put text in every part of the screen, if you're about to check that your old-fashioned CRT monitor is showing the whole of the terminal contents, and not e.g. missing half a row or column because it's off one edge of the display.

I was fascinated by that sequence as a child, partly because it's amusing, and also because of the way that it _instantly_ updated the screen, which I didn't know how to do any other way (the machines I was using were slow and I was writing in high-level languages). It stuck in my head all my life for that reason.

And I eventually found a real use for it! Once I wanted to train myself out of the bad habit of using backticks in my shell commands (the synonym $(...) is better, because it nests sensibly and doesn't impose extra escaping needs on its interior). So I bound the ` key in bash to a command that echoed ESC#8, which was just the right level of reminder to not do that again – it didn't make a loud noise annoying my colleagues, or destroy any work, but it definitely reminded me to change that habit. After about a month, I'd got into the new habit and removed the binding.

1
0
0

@simontatham i must admit i have written that without the braces before

0
0
0

@simontatham Yosys' Verilog frontend has so many of these.

1
0
0

@simontatham Don't forget that modern compilers with unicode identifiers support camel case

case '🐫':

0
1
0

@whitequark @simontatham Is that what "Claire's device" was? I tried to look for it the other day, but afaict the blog post is gone from the internet.

1
0
0