#include <string.h>
#include <ctype.h>

/* the following macro was difficult to put in a subroutine,
   but I wanted to prevent writing the same code several times. */
#define CPESCAPE(c)	t[i] = '\\', t[++i] = c;

void escape(char s[], char t[])
    /* requires: the copy plus enough backslashes will fit in t */
    /* effect: copy s to t, changing invisible characters and \ to
       escape sequences */
{
  int i;

  for (i = 0; i < strlen(s); ++i) {
    switch (s[i]) {
    case '\a':
      CPESCAPE('a');
      break;
    case '\b':
      CPESCAPE('b');
      break;
    case '\f':
      CPESCAPE('f');
      break;
    case '\r':
      CPESCAPE('r');
      break;
    case '\t':
      CPESCAPE('t');
      break;
    case '\v':
      CPESCAPE('v');
      break;
    }
  }
}
