Update cmr.c: use finite automaton and go char-by-char