/* File: showself1.c */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static char S_src[] = "/* File: showself1.c */\n"
  "\n"
  "#include <stdlib.h>\n"
  "#include <stdio.h>\n"
  "#include <string.h>\n"
  "\n"
  "static char S_src[] = \"%ooga%\";\n"
  "\n"
  "static void\n"
  "S_PrintSub (char *from, char *to)\n"
  "{\n"
  "  char *p;\n"
  "\n"
  "  for (p = from; p < to; ++p) printf (\"%c\", *p);\n"
  "}\n"
  "\n"
  "static void\n"
  "S_PrintData (char str[])\n"
  "{\n"
  "  int i = 0;\n"
  "\n"
  "  while (str[i] != '\\0') {\n"
  "    switch (str[i]) {\n"
  "    case '\"': printf (\"\\\\\\\"\"); break;\n"
  "    case '\\\\': printf (\"\\\\\\\\\"); break;\n"
  "    case '\\n': printf (\"\\\\n\\\"\\n  \\\"\"); break;\n"
  "    default: putchar (str[i]);\n"
  "    }\n"
  "    ++i;\n"
  "  }\n"
  "}\n"
  "\n"
  "static void\n"
  "S_PrintRest (char str[])\n"
  "{\n"
  "  char *p;\n"
  "\n"
  "  for (p = str; *p != '\\0'; ++p) {\n"
  "    putchar (*p);\n"
  "  }\n"
  "}\n"
  "\n"
  "int\n"
  "main ()\n"
  "{\n"
  "  char *src, *p;\n"
  "  FILE *fp;\n"
  "  int i;\n"
  "\n"
  "  if (strcmp (S_src, \"%ooga%\") == 0) {\n"
  "    /* Must load the source code from the file. */\n"
  "    fp = fopen (\"showself1.c\", \"r\");\n"
  "    src = (char *) malloc (10 * 1024);\n"
  "    fread (src, 1, 10 * 1024, fp);\n"
  "    fclose (fp);\n"
  "  } else {\n"
  "    src = S_src;\n"
  "  }\n"
  "  p = strstr (src, \"%ooga%\");\n"
  "  i = strlen (\"%ooga%\");\n"
  "  S_PrintSub (src, p);\n"
  "  S_PrintData (src);\n"
  "  S_PrintRest (p + i);\n"
  "  return 0;\n"
  "}\n"
  "";

static void
S_PrintSub (char *from, char *to)
{
  char *p;

  for (p = from; p < to; ++p) printf ("%c", *p);
}

static void
S_PrintData (char str[])
{
  int i = 0;

  while (str[i] != '\0') {
    switch (str[i]) {
    case '"': printf ("\\\""); break;
    case '\\': printf ("\\\\"); break;
    case '\n': printf ("\\n\"\n  \""); break;
    default: putchar (str[i]);
    }
    ++i;
  }
}

static void
S_PrintRest (char str[])
{
  char *p;

  for (p = str; *p != '\0'; ++p) {
    putchar (*p);
  }
}

int
main ()
{
  char *src, *p;
  FILE *fp;
  int i;

  if (strcmp (S_src, "%ooga%") == 0) {
    /* Must load the source code from the file. */
    fp = fopen ("showself1.c", "r");
    src = (char *) malloc (10 * 1024);
    fread (src, 1, 10 * 1024, fp);
    fclose (fp);
  } else {
    src = S_src;
  }
  p = strstr (src, "%ooga%");
  i = strlen ("%ooga%");
  S_PrintSub (src, p);
  S_PrintData (src);
  S_PrintRest (p + i);
  return 0;
}
