/* P PW2WS.C */
/* September 4, 1983 */

#include "a:bdscio.h"
#define CR 13
#define HCR 141
#define TAB 9
#define LF 10
#define SPACE 32
#define HIGHBIT 128



main(argc, argv)
char **argv;
{
	char  obuf[BUFSIZ * 8], ibuf[BUFSIZ * 8];
	char *bufptr, tempbuf[320];
	char tabstr[2];
	int  a, c;
	
	strcpy(tabstr,"3");
	bufptr= &tempbuf;

	if (argc != 3) {
		printf("\nUsage: A>pw2ws inputfile.typ outputfile.typ <cr> \n\n");
		puts("    This program translates Perfect Writer text for WordStar.  This allows\n");
puts("use of the Word Plus HYPHEN program. It sets the high bit on the last let-\n");
puts("ter of every word.  It changes single OD/OA combinations to 20 8D 0A.  Use\n");
puts("the normal  WordStar format commands with Perfect Writer.  PW format  com-\n");
puts("mands are made into WS comment lines.  If you want  to eliminate PW format\n");
puts("commands, use PF  with the file output option,  rather than sending  it to \n");
puts("the printer.\n\n");

puts("**************************************************************************\n\n");
printf("Tabs are expanded to %s spaces  by default.\n", tabstr);
puts("You may change this default value by entering a 'Y' now. \n");
puts("Do you want to change the tab expansion value (Y/N)? ");

		if ((toupper(getchar())) == 'Y') {
			puts("\n\nEnter '0' for no translation of TAB character.\n");
			puts("Enter a single digit number (1 - 9) to  translate TAB  character that many\n"); 
			printf("spaces in WordStar. Use <CR> for default (%s characters). ", tabstr);
			if ((c= getchar()) > 47 && c < 58) {  /* check digit range */
				poke(0xd0e, c); /* any change in program will affect this addr */ 
			} else {
				printf("\nNo change in tab value.");
				exit();
			}
		}	else exit();
/* write so many sectors to disk under same file */
		writemem(64); 

	}	/* argc != 3 */
	
	if (fopen(argv[1],ibuf) == ERROR) {
		printf("Can't open %s\n", argv[1]);
		exit();
	}
	
	if (fcreat(argv[2],obuf) == ERROR) {
		printf("Can't create %s\n", argv[2]);
		exit();
	}

	while ((c= getc(ibuf)) != CPMEOF) {
		ungetc(c,ibuf);
		while((c= getc(ibuf)) != CR) {
			switch(c) {
				case TAB:
					if (tabstr[0] == '0') {		/* tab is tab */
						*bufptr++ = TAB;
						continue;
					}
											/* tab translated to spaces */
					for (a= 0; a != (atoi(tabstr)); a++) 
						*bufptr++ = ' ';
					continue;
				case '@':   /* PW format commands are made into WS comments */
						*bufptr++ = '.';
						*bufptr++ = '.';
						*bufptr++ = c;
						continue;	
				case CPMEOF:
					*bufptr++ = '\0';
					bufptr= &tempbuf;  
					putlin(&bufptr, obuf);
					goto close;	
				default:
						*bufptr++ = c;
			}  				  /* switch */ 
		}					  	  /* while not CR */
			getc(ibuf);		  /* Case of CR: assumes OA follows OD */
			*bufptr++ = CR;  /* so it doesn't look at this char */
			*bufptr++ = LF;
			if ((c= getc(ibuf)) == CR) {
				getc(ibuf);
				*bufptr++ = CR;
				*bufptr++ = LF;
			} else {
			 	ungetc(c, ibuf);
				*bufptr--;
				*bufptr--;
				*bufptr++ = SPACE;
				*bufptr++ = HCR;
				*bufptr++ = LF;
			}

			*bufptr++ = '\0';
			bufptr= &tempbuf; 
		while ((c= *bufptr++) != '\0') {
			if (c == SPACE) {
				*bufptr--;
				*bufptr--;
				*bufptr++ += HIGHBIT;
				bufptr++;
			}					
		}
		bufptr= &tempbuf;  
		putlin(&bufptr, obuf);
	} 			/* while, != CPMEOF */
close:
	putc(CPMEOF,obuf);
	fflush(obuf);
	fclose(obuf);
}				/* main */

/* takes a null-terminated line and writes it to output buffer w/o '\0' */

putlin(tempbuf, obuf)
char obuf[], tempbuf[];
{
	int c, d;
	d= 2;

	while ((c= tempbuf[d++]) != '\0') {
		putc(c, obuf);
	}
}

writemem(a)
int a;
{
int fd, u;
char *buf;

	if((fd= open("pw2ws.com", 1)) == ERROR) {
		puts("Can't open B:PW2WS.COM");
		exit();
	}
	buf= 0x100;
	if ((u= write(fd, buf, a)) == ERROR) {
		puts("Write Err");
		exit();
	}
	puts("\nNew default in effect.");
	exit();
}
	CR;
				*bufptr++ = LF;
			} else {
			 	ungetc(c, ibuf);
	