#include <stdio.h>

#define BUFSIZE 255
#define outfile stdout
#define infile stdin

char *skel[] = {
  "#include \"defs.h\"\n\n",
  "/*  The three line banner used here should be replaced with a one line\n",
  " *  #ident directive if the target C compiler supports #ident\n",
  " *  directives.\n *\n",
  " *  If the skeleton is changed, the banner should be changed so that\n",
  " *  the altered version can easily be distinguished from the original.\n",
  " */\n",
  0,
  "\n\nwrite_section(section)\n",
  "     char *section[];\n",
  "{\n",
  "  register int i;\n\n",
  "  for (i = 0; section[i]; ++i) {\n",
  "     ++outline;\n",
  "     fprintf(output_file, \"%s\\n\", section[i]);\n",
  "  }\n",
  "}\n",
  0
};

char *names[] = {
  "banner", "header", "body", "trailer", 0
};

insert(s, c)
    char *s, c;
{
  char t;
  do {
    t = *s;  *s++ = c;  c = t;
  } while (s[-1]);
}

delete(s)
    char *s;
{
  while(*s) *s = s[1], s++;
}

fixup(b)
    char *b;
{
  for(; *b; b++) {
    if (*b == '\"' || *b == '\\' || *b == '\'') insert(b++, '\\');
     else if (*b == '\n') delete(b--);
  }
}

main()
{
  int section;
  char inbuf[BUFSIZE];
  int skelp;

  for(skelp=0; skel[skelp]; skelp++) fputs(skel[skelp], outfile);

  for(section = 0; names[section]; section++) {
    fprintf(outfile, "\nchar *%s[] = {\n", names[section]);
    while (fgets(inbuf, BUFSIZE, infile)) {
      if (! strncmp(inbuf, "/*%%*/", 6)) break;
    }
    if (feof(infile)) goto unex_eof;
    fputs("  \"/*%%*/\",\n", outfile);
    while (fgets(inbuf, BUFSIZE, infile)) {
      fixup(inbuf);
      fprintf(outfile, "  \"%s\",\n", inbuf);
      if (! strncmp(inbuf, "/*%%*/", 6)) break;
    }
    if (feof(infile)) goto unex_eof;
    fputs("  0\n};\n", outfile);
  }

  for(skelp++; skel[skelp]; skelp++) fputs(skel[skelp], outfile);
  exit(0);

unex_eof:
  fprintf(stderr, "Unexpected EOF.\n");
  exit(2);
}
