/*
     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.

  Addendum: addig code so that we do not generate the extra space
  after emboldened keywords which was necessary to align coments  - 
  instead we save them up until there is a comment on the line and
  then insert them.  If no comment, lose them.

 */

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

static int spaceafter = (0!=0);

#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 held_spaces = 0;
  int c;
  if (argc > 1 && strcmp(argv[1], "-s") == 0) {
    spaceafter = (0==0);
    argc -= 1;
    argv += 1;
  }
  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 == '\r') continue;
    if (c == ' ' || c == '\t') {
      fputc(c, stdout); /* Don't touch leading indentation */
    } else if (c == '!') {
      /* Comment holds until end of line */
      if (held_spaces != 0) {
        for (;;) {
          if (!spaceafter) putchar(' ');
          held_spaces -= 1;
          if (held_spaces == 0) break;
	}
      }
      fprintf(stdout, "<font color=%s>!", COMMENT_COLOR);
      for (;;) {
        c = fgetc(stdin);
        if (c == '\n') break;
        if (c == '\r') continue;
        if (c == EOF) break;
	if (c == '&') {
          fprintf(stdout, "&amp;");
	} else if (c == '<') {
          fprintf(stdout, "&lt;");
	} else if (c == '>') {
          fprintf(stdout, "&gt;");
	} else if (c == '\\') {
          fprintf(stdout, "&not;"); /* Olde Style tty output */
	} else {
	  fputc(c, stdout);
	}
      }
      fprintf(stdout, "</font>\n");
    } else if (c == '\n') {
      fprintf(stdout, "\n");
      held_spaces = 0; /* throw them away - no alignment necessary */
    } else {
      /* rest of statement */
      for (;;) {
        if (c == '%') {
          /* Handle keyword */
          fprintf(stdout, "<B>");
          for (;;) {
            c = fgetc(stdin);
            if (c == EOF) break;
	    if (c == '\r') continue;
            if (!isalpha(c)) break;
            fputc(c, stdout);
	  }
          held_spaces += 1;
          fprintf(stdout, "</B>") /* don't emit here any more */;
          if (spaceafter) putchar(' ');
          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, "&not;"); /* Olde Style tty output */
	} else if (c == '{') {
          fprintf(stdout, "<font color=%s>{", COMMENT_COLOR);
          for (;;) {
	    c = fgetc(stdin);
            if (c == EOF) break;
	    if (c == '}') break;
	    if (c == '\n') break;
	    if (c == '\r') continue;
	    if (c == '&') {
               fprintf(stdout, "&amp;");
	    } else if (c == '<') {
               fprintf(stdout, "&lt;");
	     } else if (c == '>') {
               fprintf(stdout, "&gt;");
	     } else if (c == '\\') {
               fprintf(stdout, "&not;"); /* Olde Style tty output */
	     } else {
	       fputc(c, stdout);
	     }
	  }
          if (c != EOF) fprintf(stdout, "%c</font>", c);
          if (c == '\n') break; /* new statement */
	} else if (c == '"') {
          fprintf(stdout, "<font color=%s>\"", QUOTE_COLOR);
          for (;;) {
	    c = fgetc(stdin);
            if (c == EOF) break;
	    if (c == '"') break;
	    if (c == '\r') continue;
	    if (c == '&') {
              fprintf(stdout, "&amp;");
	    } else if (c == '<') {
              fprintf(stdout, "&lt;");
	    } else if (c == '>') {
              fprintf(stdout, "&gt;");
	    } else if (c == '\\') {
              fprintf(stdout, "&not;"); /* Olde Style tty output */
	    } else {
	      fputc(c, stdout);
	    }
	  }
          fprintf(stdout, "\"</font>");
	} else if (c == '\'') {
          fprintf(stdout, "<font color=%s>'", QUOTE_COLOR);
          for (;;) {
	    c = fgetc(stdin);
            if (c == EOF) break;
	    if (c == '\r') continue;
	    if (c == '\'') break;
	    if (c == '&') {
              fprintf(stdout, "&amp;");
	    } else if (c == '<') {
              fprintf(stdout, "&lt;");
	    } else if (c == '>') {
              fprintf(stdout, "&gt;");
	    } else if (c == '\\') {
              fprintf(stdout, "&not;"); /* Olde Style tty output */
	    } else {
	      fputc(c, stdout);
	    }
	  }
          fprintf(stdout, "'</font>");
	} else if (c == ':') {
          fputc(c, stdout); /* Probably a label.  Should fix this broken case:
                               lab(n): ! comment with odd ' in it */
          break;
	} else {
          fputc(c, stdout);
	}
        do {c = fgetc(stdin);} while (c == '\r');
        if (c == EOF) break;
        if (c == '\n') {
          fprintf(stdout, "\n");
          break;
	}
        if (c == ';') {
          fprintf(stdout, ";");
          break;
	}
      }
    }
  }
  html_footer();
  exit(0);
}