Software Tools in Haskell: unescape
interpret C and ASCII escape codes on stdin
This page is part of a series on Software Tools in Haskell.
This post is literate Haskell; you can load the source into GHCi and play along.
As usual, we start with some imports.
-- sth-unescape: interpret C and ASCII backslash escape codes on stdin
module Main where
import System.Exit (exitSuccess)
See also the Backslash module.
While testing the overstrike
program I ran into an inconvenient problem: I couldn’t find an easy and consistent way to type control characters (namely backspace, but others have the same problem) that works both in my terminal and in my golden test suite. It seems like every program – the terminal, the shell, the test runner – wants to interpret these characters slightly differently. This problem is a good candidate for a filter-style program. unescape
reads lines from stdin and interprets any C-style escape sequences or ASCII abbreviations it finds. (There is a nice wiki page on C-style escape sequences, and the page on ASCII includes a table of abbreviations.)
The main program is simple enough, as it simply munches through the lines on stdin looking for escape codes.
The real work is done by the library function bsUnEsc
.