8.1.5 changequote is Evil

The temptation is often high to bypass proper quotation, in particular when it’s late at night. Then, many experienced Autoconf hackers finally surrender to the dark side of the force and use the ultimate weapon: changequote.

The M4 builtin changequote belongs to a set of primitives that allow one to adjust the syntax of the language to adjust it to one’s needs. For instance, by default M4 uses ‘`’ and ‘'’ as quotes, but in the context of shell programming (and actually of most programming languages), that’s about the worst choice one can make: because of strings and back-quoted expressions in shell code (such as ‘'this'’ and ‘`that`’), and because of literal characters in usual programming languages (as in ‘'0'’), there are many unbalanced ‘`’ and ‘'’. Proper M4 quotation then becomes a nightmare, if not impossible. In order to make M4 useful in such a context, its designers have equipped it with changequote, which makes it possible to choose another pair of quotes. M4sugar, M4sh, Autoconf, and Autotest all have chosen to use ‘[’ and ‘]’. Not especially because they are unlikely characters, but because they are characters unlikely to be unbalanced.

There are other magic primitives, such as changecom to specify what syntactic forms are comments (it is common to see ‘changecom(<!--, -->)’ when M4 is used to produce HTML pages), changeword and changesyntax to change other syntactic details (such as the character to denote the nth argument, ‘$’ by default, the parentheses around arguments, etc.).

These primitives are really meant to make M4 more useful for specific domains: they should be considered like command line options: --quotes, --comments, --words, and --syntax. Nevertheless, they are implemented as M4 builtins, as it makes M4 libraries self contained (no need for additional options).

There lies the problem...


The problem is that it is then tempting to use them in the middle of an M4 script, as opposed to its initialization. This, if not carefully thought out, can lead to disastrous effects: you are changing the language in the middle of the execution. Changing and restoring the syntax is often not enough: if you happened to invoke macros in between, these macros are lost, as the current syntax is probably not the one they were implemented with.