Software Tools in Haskell: unescape

interpret C and ASCII escape codes on stdin

Posted on 2016-02-21 by nbloomf
Tags: software-tools-in-haskell, literate-haskell

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.

import Lib.Backslash (bsUnEsc)

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.

main :: IO ()
main = do
  charFilter bsUnEsc
  exitSuccess

The real work is done by the library function bsUnEsc.

Old stuff

-- apply a map to stdin
charFilter :: (String -> String) -> IO ()
charFilter f = do
  xs <- getContents
  putStr $ f xs