1#!/bin/bash
2# Canonical C source transformation for easy diffing
3# Copyright (C) 2008  Sylvain Beucler
4#
5# Copying and distribution of this file, with or without modification,
6# are permitted in any medium without royalty provided the copyright
7# notice and this notice are preserved.
8
9# Replace all new lines by a single space, and indent the result to
10# get a "canonical" form. Some newlines are significant: preprocessor
11# directives and '//' comments; for those, we first replace newlines
12# by \r and convert them back to \n after all spaces are squeezed.
13#
14# A comment can be on a line by itself (documents the code below) or
15# placed at the right of a code line (documents code on its left). To
16# avoid replacing the former by the later and introducing confusion, I
17# also insert a significant newline before a comment when it's on a
18# line by itself.
19
20cat $1 | sed 's/\r//' | sed 's,\(^#.*\),\1\r,' | sed 's,^[[:space:]]*\(//.*$\),\r\1\r,' \
21  | sed 's,\(//.*$\),\1\r,' \
22  | tr '\t' ' ' | tr -s '\n' ' ' | tr -s ' ' ' ' | tr '\r' '\n' \
23  | indent - > $1_canonical
24