/* Vortext v1.5 - Michel Laliberté, février 1991 - compilé sur Lattice 5.0
	lc <source>.c
	blink FROM lib:c.o <source>.o TO <source> 
	LIB lib:lc.lib lib:amiga.lib SMALLCODE SMALLDATA NODEBUG VERBOSE
*/
#include <stdio.h>
#include <exec/types.h>
#define LF 10
#define CR 13
#define TAB_DEF 5
#define LIM_AMI 161
#define LIM_PC  128	
unsigned char b = 0;
unsigned char a = 0;
BOOL sl = TRUE;
BOOL ca, ai, ia, am, ma, im, mi,  wp, aw, ascii, nc, Tab_size;
FILE *fp;
FILE *pf;

main(argc,argv)
int argc;
char *argv[];
{
	VOID conv_accents(), Am_C64(), TABtoSPC(), rien_a_foutre();
	char outfile[32];
 	int NextArg; 
                     /* Initialisation des tableaux de conversion */
	static unsigned char tabAI_conv[96] = {
		173,189,156,207,190, 46,245,249,184,166,174,170,240,169,238,248,
		241,253,252,239,230,244,250, 44,251,167,175,171,172,243,168,183,
		181,182,199,142,143,146,128,212,144,210,211,222,214,215,216,209,
		165,227,224,226,229,153,158,157,235,233,234,154,237,231,225,133,
		160,131,198,132,134,145,135,138,130,136,137,141,161,140,139,208,
		164,149,162,147,228,148,246,155,151,163,150,129,236,232,152 };	

	static unsigned char tabAM_conv[96] = {
		193,162,163, 46,180, 46,164,172,169,187,199,194, 46,168, 46,161,
		177, 46, 46,171,181,166,165, 44, 46,188,200, 46, 46, 46,192,203,
		 46, 46,204,128,129,174,130, 46,131, 46, 46, 46, 46, 46, 46, 46,
		132, 46, 46, 46,205,133, 46,175, 46, 46, 46,134, 46, 46,167,136,
		135,137,139,138,140,190,141,143,142,144,145,147,146,148,149, 46,
		150,152,151,153,155,154,214,191,157,156,158,159, 46, 46,216 };
		
	static unsigned char tabIA_conv[129] = {
		199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,
		201,230,198,244,246,242,251,249,255,214,220,248,163,216,215, 46,
		225,237,243,250,241,209,170,186,191,169,172,189,188,161,171,187,
		 46, 46, 46, 46, 46,193,194,192,169, 46, 46, 46, 46,162,165, 46,
		 46, 46, 46, 46, 46, 46,227,195, 46, 46, 46, 46, 46, 46, 46,164,
		240,208,202,203,200, 46,205,206,207, 46, 46, 46, 46, 46,204, 46,
		211,223,212,210,245,213,181,222,254,218,219,217,253,221,175,180,
		173,177, 46,190,182,167,247, 46,176,168,183,185,179,178, 46, 46 };
			
	static unsigned char tabIM_conv[129] = {
		130,159,142,137,138,136,140,141,144,145,143,149,148,147,128,129,
		131,190,174,153,154,152,158,157,216,133,134,191,163,175, 46,196,
		135,146,151,156,150,132,187,188,192,168,194, 46, 46,193,199,200,
		 46, 46, 46, 46, 46, 46, 46,203,169, 46, 46, 46, 46,162,180, 46,
		 46, 46, 46, 46, 46, 46,139,204, 46, 46, 46, 46, 46, 46, 46, 46,
		 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
		 46,167, 46, 46,155,205,181, 46, 46, 46, 46, 46, 46, 46, 46,171,
		209,177, 46, 46,166,164,214, 46,161,172,165, 46, 46, 46,215,202 };
		
	static unsigned char tabMA_conv[90] = {
		196,197,199,201,209,214,220,225,224,226,228,227,229,231,233,232,
		234,235,237,236,238,239,241,243,242,244,246,245,250,249,251,252,
		 46,176,162,163,167,183,182,223,174,169, 46,180,168, 46,198,216,
		 46,177, 46, 46,165,181, 46, 46, 46, 46, 46,170,186, 46,230,248,
		191,161,172, 46, 46, 46, 46,171,187, 46, 46,192,195,213, 46, 46,
		 45, 46, 34, 34, 96, 39,247, 46,255 };
		
	static unsigned char tabMI_conv[90] = {
		142,143,128,144,165,153,154,160,133,131,132,198,134,135,130,138,
		136,137,161,141,140,139,164,162,149,147,148,228,163,151,150,129,
		 46,248,189,156,245,250,244,225,169,184, 46,239,249, 46,146,157,
		 46,241, 46, 46,190,230, 46, 46, 46, 46, 46,166,167, 46,145,155,
		168,173,170, 46,159, 46, 46,174,175, 46,255,183,199,229, 46, 46,
		 45,240, 34, 34, 96, 39,246,254,152  };

	if (argc<2 || argv[1][0] == '?') {  /* Tableau d'aide */
		printf("\f");
		printf("[33m       	              Vortex v1.5 - February 1991[31m\n\n");
		printf("     Converts all accented characters in ASCII or Word Perfect\n");
		printf("     files for Amiga, IBM-PC, MacIntosh and C-64 computers.\n");
		printf("     NOTE: Does not convert the diskette format.\n");
 		printf("           Michel Laliberté, Montréal - tél.: (514) 274-0288\n\n");
		printf("[33m USAGE:[31m 1> %s source [destination] -direction [-options...] \n\n",argv[0]); 
		printf("     Puts in \"destination\" the conversion of file \"source\".\n");
		printf("[33m DIRECTION:[31m     -ai/-ia:  Amiga  -> IBM-PC /or vice versa\n");
		printf(" choose one:    -am/-ma:  Amiga  -> Mac /or vice versa\n");
		printf("                -im/-mi:  IBM-PC -> Mac /or vice versa\n");
		printf("                -ca:      C-64 (Traitex) -> Amiga\n");
		printf("[33m OPTIONS:  [31m     -wp:      Word Perfect 4.x source file\n");
		printf(" [optional]     -aw:      ASCII -> Word Perfect conversion\n");
		printf("                -sl/-sl0: Converts line feeds /or keeps them \n");  
		printf("                -nc:      No accent conversion\n");
		printf("                -tx:      Replaces tabs by x spaces (1-20)\n");
		printf("     ex.: 1> %s  df1:sourcefile  ram:destfile -ai -wp\n\n",argv[0]);
		exit(0);
	}
	else {                        /* Lecture des options  */
		for (NextArg=1; NextArg <argc; NextArg++) {
			if (*argv[NextArg] == '-') {		
				switch (argv[NextArg][1]) {
					case 'a':    {            /* AI, AM et AW */
						if (argv[NextArg][2] == 'i')      ai = TRUE;
						else if (argv[NextArg][2] == 'm') am = TRUE;
						else if (argv[NextArg][2] == 'w') {
							 aw = TRUE; sl = FALSE;
						}
						else rien_a_foutre(); 
						break;
					}
					case 'c': {               /* CA */
						if (argv[NextArg][2] == 'a') ca = TRUE;
						else rien_a_foutre(); 
						break;
					}
					case 'i':   {            /* IA et IM*/
						if (argv[NextArg][2] == 'a')      ia = TRUE; 
						else if (argv[NextArg][2] == 'm') im = TRUE;
						else rien_a_foutre(); 
						break;
					}
					case 'm':    {            /* MA et MI */
						if (argv[NextArg][2] == 'a')      ma = TRUE;
						else if (argv[NextArg][2] == 'i') mi = TRUE;
						else rien_a_foutre();
						break;
					}
					case 'n':    {            /* MA et MI */
						if (argv[NextArg][2] == 'c') {
							nc = TRUE; sl = FALSE;
						}
						else rien_a_foutre();
						break;
					}
					case 's':     {           /* SL */
						if (argv[NextArg][2] == 'l') { 
							sl = TRUE;
							if (argv[NextArg][3] == '0') sl = FALSE;
						}
						else rien_a_foutre(); 
						break;
					}
					case 't': {    /* conversion tabs -> espaces */
						Tab_size = atoi(&argv[NextArg][2]);
						if (Tab_size < 2 || Tab_size > 20) {
							Tab_size = TAB_DEF;
							printf("\tNombre d'espaces invalide; changé pour %d.\n",TAB_DEF);
						}
						break;
					}
					case 'w':     {           /* WP */
						if (argv[NextArg][2] == 'p') { 
							wp = TRUE; sl = FALSE; 
						}
						else rien_a_foutre();
						break;
					}
					default:
						rien_a_foutre();
				}
			}
		}
	}                         /* On ouvre source et destination */

	if (argc >2 && argv[2][0] == '-') { /* Destination = <source>.vx */
		strcpy(outfile,argv[1]);
		strcat(outfile,".vx");
	}
	else if (argv[2][0] != '-')       /* Destination = argv[2] */
		strcpy(outfile,argv[2]);
	
	if (argv[1][0] == '-')     {        /* Source != argv[1]! */            
		printf("USAGE: 1> vortex source [destination] -direction [-options...]\n");
		printf("\tPlease enter source file name first!\n");
    	exit(0);
    }
	if ((argv[2][0] != '-')&&(argv[3][0] != '-')
			||(!ai&&!ia&&!ma&&!am&&!im&&!mi&&!ca)) { /* Pas de direction! */
		printf("USAGE: 1> vortex source [destination] -direction [-options...]\n");
		printf("\tPlease entrer a direction!\n");
    	exit(0);
    }
    if((fp=fopen(argv[1],"r")) == NULL) {
        printf("\tCan't open source file...\n");
        exit(0);
    }
    if((pf=fopen(outfile,"w")) == NULL) {
        printf("\tCan't open destination file...\n");
        fclose(fp);
        exit(0);
    }
    printf("    Destination = %s  [3mWorking...[0m\n",outfile);

	if      (ca) Am_C64();       /* Jump table */
	else if (ai) conv_accents(LIM_AMI, 256, tabAI_conv);
	else if (am) conv_accents(LIM_AMI, 256, tabAM_conv);
	else if (ia) conv_accents(LIM_PC, 256,  tabIA_conv);
	else if (im) conv_accents(LIM_PC, 256,  tabIM_conv);
	else if (ma) conv_accents(LIM_PC, 217,  tabMA_conv);
	else if (mi) conv_accents(LIM_PC, 217,  tabMI_conv);

	fclose(fp);	             /* Sortie */
	fclose(pf);
	exit(0);
}

VOID conv_accents(lim1, lim2, str) /* Routine principale  */
int lim1, lim2;
char *str;
{
	register int i, j;           /* Crée tableau de conversion */
	static unsigned char tab_conv[257];
	for(i=0; i<lim1;i++)     tab_conv[i] = (char)i; 
	for(j=0; i<lim2;i++,j++) tab_conv[i] = (char)str[j]; 
	for(   ; i<256;i++)	     tab_conv[i] = (char)i; 
	if (wp) tab_conv[225] = 225;     /* á */
	b=getc(fp);
	if (wp) {
		if (ai || am) while (b == 128) b=getc(fp); 	
		if (ia) {
			putc(128,pf);
			putc(128,pf);
		}
	}
	while (b!=EOF) {                  /* Boucle principale */
		if (wp && a == 'á') b = tab_conv[b];
		else if (!wp && !nc) b = tab_conv[b];
		a = b;
		if (Tab_size && b == 9) {  /* Tabs */
			TABtoSPC(Tab_size);
			continue;
		}
		if (sl){                         /* Saut de ligne */
			 if (conv_sl()) continue;
		}
		if (aw && ASCtoWP(lim1)) continue;  /* ASCII -> WP */
		putc(b,pf);				
		b=getc(fp); 
	}
}

conv_sl()
{
	switch (b) {                      /* Convertit saut de ligne */
		case LF:
			if     (ai) putc(CR,pf);  /* Amiga -> */
			else if(am) b = CR;
			break;
		case CR:
			if     (ia) {              /* IBM -> */
				b = getc(fp);
				return(TRUE);
			}
			else if(im) getc(fp);
			else if(ma) b = LF;        /* Mac -> */
			else if(mi) { 
				putc(CR,pf);
				b = LF;
			}
			break;
		}
	return(FALSE);
}

ASCtoWP(lim1)          /* Conversion ASCII -> WP */
int lim1;
{
	if( b >= lim1)	  {   /* Caractère accentué WP à encadrer */
		putc('á',pf);
		putc(b,pf);
		putc('á',pf);
		b = getc(fp);
		return(TRUE);
	}
	else return(FALSE);	
}

VOID TABtoSPC(n)               /* Conversion des TAB en espaces */
int n;
{
	int i;
	b = 32;
	for(i=0; i < n; i++) putc(b,pf);
	b = getc(fp);
}

VOID Am_C64()             /*Conversion Amiga -> C-64 */
{
	while (b == 0) b=getc(fp);
	while (b!=EOF)	{		/* Boucle de lecture du fichier source */
		if (b=='z') {	/* Enlève caractères de contrôle */
			do    {
				b=getc(fp);
			}while (b!=';' && b != 31);
		}
		if (b ==31) b=10;	/* Si b = 31, b = LF   */ 
		else if (b == ';') b = getc(fp);
		else if (b>0 && b<32) b=b+96;	/* Codes d'écran -> ASCII */
		else switch(b) {			/* Convertit autres accents français */
			case 't':		/* é */
				b=233;if (aw) ascii = TRUE; break;
			case 'h':		/* à */		
				b=224;if (aw) ascii = TRUE; break;
			case 'u':		/* è */
				b=232;if (aw) ascii = TRUE; break;
			case 'v':		/* ê */
				b=234;if (aw) ascii = TRUE; break;
			case 'j':		/* â */
				b=226;if (aw) ascii = TRUE; break;
			case '\\':		/* ç */
				b=251;if (aw) ascii = TRUE; break;
			case 'o':		/* î */
				b=231;if (aw) ascii = TRUE; break;
			case 'w':		/* û */
				b=238;if (aw) ascii = TRUE; break;
			case 'x':		/* ô */
				b=244;if (aw) ascii = TRUE; break;
			case 'y':		/* ù */
				b=249;if (aw) ascii = TRUE; break;
			case '^':		/* ë */
				b=235;if (aw) ascii = TRUE; break;
			case '_':		/* ï */
				b=239;if (aw) ascii = TRUE; break;
			case 'i':		/* ü */
				b=252;if (aw) ascii = TRUE; break;         
			case '[':    /* Hard space Traitex -> espace */
				b=32; break;
		}
		if (ascii) putc('á',pf);	/* Imprime á */
		putc(b,pf);				/* Envoie le caractère à l'output */
		if (ascii) putc('á',pf);	/* Imprime á */
		ascii = FALSE;
		b=getc(fp);				/* Va chercher autre car. à l'input*/
	} 
}

VOID rien_a_foutre()            /* Erreur, on sort */
{
	printf("\tIllegal option!\n");
	if (fp) fclose(fp); 
	if (pf) fclose(pf); 
	exit(0);
}
