/*
     Heck, if I'm going to put old imp stuff on the web, I might as
  well do it properly and turn it into colour-highlighted html...

   %alpha<non-alpha> for keywords
   ! at start of a statment for comment until end of line
   %c or a single - at end of line to continue to next line
     (no implicit continuation)
   ; between statements

  That's about it.  May or may not bother with builtin %perm routine names.
  This is a first crude hack but it seems to work well enough.

 */

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

#define QUOTE_COLOR "#ff0000"
#define COMMENT_COLOR "#408080"

void html_header(char *Title) {
  fprintf(stdout, "<HTML>\n<HEAD>\n<TITLE> %s </TITLE>\n</HEAD>\n", Title);
  fprintf(stdout, "<BODY bgcolor=ffffff><pre>\n");
}

void html_footer(void) {
  fprintf(stdout, "\n</pre></BODY></HTML>\n");
}

int main(int argc, char **argv)
{
  int c;
  html_header((argc == 2 ? argv[1] : ""));
  for (;;) {
    /* assume we're at the start of a new statement here. */
    c = fgetc(stdin);
    if (c == EOF) break;
    if (c == ' ' || c == '\t') {
      fputc(c, stdout); /* Don't touch leading indentation */
    } else if (c == '!') {
      /* Comment holds until end of line */
      fprintf(stdout, "<font color=%s>!", COMMENT_COLOR);
      for (;;) {
        c = fgetc(stdin);
        if (c == '\n') break;
        fputc(c, stdout);
      }
      fprintf(stdout, "</font>\n");
    } else if (c == '\n') {
      fprintf(stdout, "\n");
    } else {
      /* rest of statement */
      for (;;) {
        if (c == '%') {
          /* Handle keyword */
          fprintf(stdout, "<B>");
          for (;;) {
            c = fgetc(stdin);
            if (!isalpha(c)) break;
            fputc(c, stdout);
	  }
          fprintf(stdout, "</B> "); ungetc(c, stdin);
	} else if (c == '&') {
          fprintf(stdout, "&amp;");
	} else if (c == '<') {
          fprintf(stdout, "&lt;");
	} else if (c == '>') {
          fprintf(stdout, "&gt;");
	} else if (c == '{') {
          fprintf(stdout, "<font color=%s>{", COMMENT_COLOR);
          for (;;) {
	    c = fgetc(stdin);
	    if (c == '}') break;
	    fputc(c, stdout);
	  }
          fprintf(stdout, "}</font>");
	} else if (c == '"') {
          fprintf(stdout, "<font color=%s>\"", QUOTE_COLOR);
          for (;;) {
	    c = fgetc(stdin);
	    if (c == '"') break;
	    fputc(c, stdout);
	  }
          fprintf(stdout, "\"</font>");
	} else if (c == '\'') {
          fprintf(stdout, "<font color=%s>'", QUOTE_COLOR);
          for (;;) {
	    c = fgetc(stdin);
	    if (c == '\'') break;
	    fputc(c, stdout);
	  }
          fprintf(stdout, "'</font>");
	} else {
          fputc(c, stdout);
	}
        c = fgetc(stdin);
        if (c == EOF) break;
        if (c == '\n') {
          fprintf(stdout, "\n");
          break;
	}
        if (c == ';') {
          fprintf(stdout, ";");
          break;
	}
      }
    }
  }
  html_footer();
  exit(0);
}
