
/*
 * ww4convert - convert Word Writer 4 documents to HTML
 *
 * This program is a simplistic, brain-dead attempt to convert Word Writer 4
 * documents into HTML.  Word Writer 4 was a popular word processor for the
 * Commodore 64 in the late 1980's.
 *
 * To compile:
 * cc -o ww4convert ww4convert.c
 *
 * To use:
 *     ww4convert < input_file > output_file.html
 *
 * Bash commands to convert every file in a directory:
 * for i in *; do new=`echo $i | sed 's/ /_/g'`; ww4convert < "$i" > /tmp/html/$new.html ; done
 *
 * This program is public domain -- do whatever you want with it.
 *
 * David Simmons
 * March 20, 2007
 *
 */

#include <stdio.h>

/* delimiter ctrl-d */
/*
 mark (bookmark) ctrl-s set mark ... c= n next mark

 outliner 
 	index line - F6
	lower index = ctrl-L
	raise index = ctrl-R
*/

int main(int argc, char **argv) {

	int n;
	unsigned char c;
	unsigned char header[28];
	int checkmark=0;
	int marknum=0;

	/* read the WW4 header */
	fread(header, 28, 1, stdin);

	/* HTML header */
	printf("<html>\n<head>\n<title>exported ww4 document</title>\n</head>\n<body>\n");

	/* process each character in the stream */
	while ((n=getchar())!=EOF) {
		c = (char)n;

		if (checkmark) {
			if (c == 0xFF) {
				printf(" -->\n");
				checkmark=0;
				continue;
			}
		}

		/**** ascii translation ****/
		if (c == 0x00) {
			putchar('@');
		} else if ((c >= 0x01) && (c <= 0x1A)) {
			putchar(c+0x60);
		} else if (c == 0x1E) {
			putchar('^'); /* caret */
		} else if (c == 0x20) {
			/* space */
			putchar(' ');
		} else if ((c > 0x20) && (c <= 0x3F)) {
			if (c == '<') {
				printf("&lt;");
			} else if (c == '>') {
				printf("&gt;");
			} else {
				putchar(c);
			}
		} else if ((c >= 0x1B) && (c<=0x1F)) {
			if (c == 0x1C) {
				printf("&pound;");
			} else {
				putchar(c+0x40);
			}
		} else if (
			((c >= 0x40) && (c <= 0x5A))
		) {
			putchar(c);
		} else if (c == 0x7A) {
			/* "check-mark" directives in the WW4 doc */
			printf("<!-- ");
			checkmark = 1;
		}
		/**** control codes ****/
		else if (c == 0xC2) {
			printf("<b>");
		} else if (c == 0x82) {
			printf("</b>");
		} else if (c == 0xC9) {
			printf("<em>");
		} else if (c == 0x89) {
			printf("</em>");
		} else if (c == 0xD5) {
			printf("<u>");
		} else if (c == 0x95) {
			printf("</u>");
		} else if (c == 0xFF) {
			printf("\n<p />\n");
		} else if (c == 0xFC) {
			/* delimiter */
			printf("_");
		} else if (c == 0x80) {
			/* mark */
			printf("<!-- mark -->");
			printf("<a name=\"mark%d\"></a>",++marknum);
		}
	}
	printf("\n");

	/*
	 * other (unhandled) control codes:
	 * d7/97	code1
	 * d8/98	code2
	 * d9/99	code3
	 */

	/* close the HTML document */
	printf("</body>\n</html>\n");

	return 0;
}

