*** filter.c	Fri Aug  5 18:44:21 1994
--- filter.c.new	Wed Aug 31 18:31:47 1994
***************
*** 1937,1939 ****
--- 1937,2340 ----
  
      fflush(stdout);
  }
+ 
+ 
+ #ifdef BWC
+ #ifdef NODEF
+ /*-*/
+ #include <stdio.h>
+ #include <sys/types.h>
+ #include <sys/stat.h>
+ #include <sys/file.h>
+ struct stat buf;
+ char *malloc();
+ char *glyph2richtext();
+ int convert2graph();
+ #endif
+ 
+ char *glyph2richtext();
+ 
+ 
+ char *gf_glyph2richtext(f, c, flg)
+      FILTER_S *f;
+      int       c, flg;
+ {
+   char *line, *p;
+   if(flg == GF_RESET){
+     f->linep = f->line = (char *)fs_get(1000 * sizeof(char ));
+ 
+   } else if(flg == GF_DATA) {
+       if(c == '\n' && *(f->linep - 1) == '\r'){
+           *(f->linep)++ = '\n'; /* Tie of line */
+           *(f->linep) = '\0'; /* Tie of line */
+ 	  line = glyph2richtext(f->line);
+ 	  for(p = line; *p; p++)
+ 	    (*f->next->f)(f->next, *p, GF_DATA);
+   	  fs_give((void **)&line);
+ 	  f->linep = f->line;
+       } else {
+ 	*(f->linep)++ = c;
+       }
+   } else if(flg == GF_EOD) {
+       *(f->linep) = '\0'; /* Tie of line */
+       line = glyph2richtext(f->line);
+       for(p = line; *p; p++)
+         (*f->next->f)(f->next, *p, GF_DATA);
+       fs_give((void **)&line);
+       f->linep = f->line;
+       
+       fs_give((void **)&(f->line)); 
+       (*f->next->f)(f->next, 0 , GF_EOD);
+     }
+ }
+ 
+ 
+ char *glyph2richtext(textin)
+   char *textin;
+ {
+     register char *p, *textout; 
+     int          stack=10, underline=0, bold=0, graphics=0,i=0,two_underline=0 ;
+     int          memory_aloc=0;
+     char         stack_str[10][20];
+   
+     memory_aloc = max(strlen(textin)*8, 80);
+     textout     = malloc(memory_aloc+1);
+     *textout    = '\0';
+     dprint(9, (debugfile, "GLYPH2RICH strlen: %d   allocated: %d\n",
+                strlen(textin), memory_aloc + 1));
+   
+     while (stack--) stack_str[stack][0] = '\0';  /*Initialize the array */
+     stack=0;
+   
+     for(p=textout; *textin;){    /*everything is done in the loop */
+      
+         if ((p -textout) >(memory_aloc -200)){  /*allocate more memory before */
+             realloc(textout,(memory_aloc +=1024)); /*we ran out of it */
+             dprint(9, (debugfile, "GLYPH2RICH left: %d   reallocated: %d\n",
+                   p - textout, memory_aloc));
+         }
+     
+         /*======= THE LINE AND PAGE BREAKS =======*/
+         if(*textin == '\r' && *(textin+1)=='\n'){ /*---- LINE BREAK -----*/
+             while (stack--){                         /*End attributes*/
+                 sprintf(p, "</%s>", stack_str[stack]);
+                 p += strlen(p);
+                 stack_str[stack][0] = '\0';
+             }
+             if(two_underline) { /* In case '_' occured near end of line */
+                 strcpy(p, "<\\underline>");
+                 p += strlen(p);
+             }
+                
+             strcpy(p,"\r\n\r\n");                        /*Add richtext newline */
+             p += strlen(p);
+             textin += 2;
+     
+             stack=underline=bold=graphics=two_underline=0;
+             continue;
+         }
+     
+         if( *textin    == '{'  &&         /*-------- PAGE BREAK --------*/
+            *(textin+1) == '~'){ 
+             strcpy(p,"<np>");
+             p      += strlen(p);
+             textin +=2;
+             continue;
+         }
+     
+         /*==================== ATTRIBUTES ===============*/
+     
+         if(*textin == '_'){  /*------next two characters are underlined-----*/
+             if(underline){
+                 *textin++;  /*skip if already underlined*/
+             } else {
+                 two_underline=2;
+                 strcpy(p,"<underline>");
+                 p += strlen(p);
+                 *textin++;
+             }
+             continue;
+         }
+           
+         if( *textin    == '{'  && 
+            *(textin+1) == '\\'){           /*Some type of attribute found*/
+             textin += 2;
+            
+             while (stack--){                         /*End previous attributes*/
+                 sprintf(p, "</%s>", stack_str[stack]);
+                 p += strlen(p);
+                 stack_str[stack][0] = '\0';	
+             }
+     
+             stack=underline=bold=0;
+             if ( (*textin - 0x20)       & 0x01){
+               strcpy(stack_str[stack++],"bold");               /*bold*/
+               bold=1;
+             }
+             if (((*textin - 0x20) >> 1) & 0x01){               /*underline*/
+               strcpy(stack_str[stack++],"underline");
+               underline=1;
+             }
+             if (((*textin - 0x20) >> 2) & 0x01){              /*blinking*/
+               if(bold) strcpy(stack_str[stack++],"superscript");
+               else if (underline) strcpy(stack_str[stack++],"subscript");
+               else strcpy(stack_str[stack++],"no-op"); /*can't do blinking alone!*/
+             }
+             if (((*textin - 0x20) >> 3) & 0x01){              /*reverse video*/
+               strcat(stack_str[stack++],"italic");            /*Italic on paper*/
+             }
+             if (((*textin - 0x20) >> 4) & 0x01){              /*graphics*/
+               graphics=1;
+             } else graphics=0;
+     
+             
+             for(i=0; i<stack; i++){                /*write the staring attributes*/
+                 sprintf(p, "<%s>", stack_str[i]);
+                 p += strlen(p);
+             }
+             textin++;
+             continue;
+         }
+     
+         /*========== SPECIAL CHARACTERS ===========*/
+         if(two_underline){
+             /* Time to turn off underline? */
+             if(--two_underline ==0 && underline == 0){
+                 strcpy(p,"</underline>");
+                 p += strlen(p);
+             }
+         }
+     
+         if(*textin == '<'){
+             /*add richtext smaller sign */
+             strcpy(p,"<lt>");
+             p += strlen(p);
+             *textin++;
+     
+         } else if(graphics){               /*convert  graphics */
+           if    ( *textin == 'j' ||
+                   *textin == 'k' ||
+                   *textin == 'l' ||
+                   *textin == 'm' ||
+                   *textin == 'n' ||
+                   *textin == 't' ||
+                   *textin == 'u' ||
+                   *textin == 'v' ||
+                   *textin == 'w' )   { *p = '+'; *textin++;}
+           else if ( *textin == 'q' ) { *p = '-'; *textin++;}
+           else if ( *textin == 'x' ) { *p = '|'; *textin++;}
+           else *p= *textin++;
+           p++;
+         } else if(*textin == '^'){    /* Accent (similar to \a notation)*/
+             textin++;
+             if     (*textin == 'A' ){*p='\301'; textin++;}
+             else if(*textin == 'E' ){*p='\311'; textin++;}
+             else if(*textin == 'I' ){*p='\315'; textin++;}
+             else if(*textin == 'O' ){*p='\323'; textin++;}
+             else if(*textin == 'U' ){*p='\332'; textin++;}
+             else if(*textin == 'a' ){*p='\341'; textin++;}
+             else if(*textin == 'e' ){*p='\351'; textin++;}
+             else if(*textin == 'i' ){*p='\355'; textin++;}
+             else if(*textin == 'o' ){*p='\363'; textin++;}
+             else if(*textin == 'u' ){*p='\372'; textin++;}
+             else *p= '^';
+             *++p = '\0';
+             
+         } else if(*textin == '|') {   /* Dot under letter */
+             textin++;
+             switch(*textin) {
+               case 'd':
+               case 'D':
+               case 's':
+               case 'S':
+               case 'T':
+               case 't':
+               case 'Z':
+               case 'z':
+                 *p = *textin++; break;
+               default:
+                 *p = '|'; break;
+             }
+             *++p = '\0';
+     
+         } else if( *textin == '\\'){  /* Decode \ notation for 8 bit chars */
+             switch(*(textin+1)){
+               case '@':
+                 *p = '@';   textin+=2; /* *textin++;*textin++; */
+                 break;
+               case '\\':                         /* back slash*/
+                 *p = '\\';  textin+=2;
+                 break;
+               case '!':                          /* reverse exclaimation */
+                 *p = '\241'; textin+=2;
+                 break;
+               case '#':                          /* superscript 1 2 3 */
+                 if     (*(textin+2) == '1' ){*p='\271'; textin += 3;}
+                 else if(*(textin+2) == '2' ){*p='\262'; textin += 3;}
+                 else if(*(textin+2) == '3' ){*p='\263'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case '$':                         /* cent*/
+                 *p = '\242'; textin+=2;
+                 break;
+               case '&':                         /* ligature*/
+                 *p = '\247'; textin+=2;
+                 break;
+               case '*':                         /* crossed o*/
+                 *p = '\250'; textin+=2;
+                 break;
+               case '-':                         /* minus*/
+                 *p = '-'; textin+=2;
+                 break;
+               case '.':                         /* degree*/
+                 *p = '\260'; textin+=2;
+                 break;
+               case '/':                         /* o slashed*/
+                 if     (*(textin+2) == 'O' ){*p='\330'; textin += 3;}
+                 else if(*(textin+2) == 'o' ){*p='\370'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case '<':                         /* double <*/
+                 *p = '\253'; textin+=2;
+                 break;
+               case 'n':                         /* back quote (ayn)*/
+                 *p = '`'; textin+=2;
+                 break;
+               case '>':                         /* double >*/
+                 *p = '\273'; textin+=2;
+                 break;
+               case '=':                         /* ligature*/
+                 if     (*(textin+2) == 'A' &&
+                         *(textin+3) == 'E' )   {*p='\306';  *(textin+=4);}
+                 else if(*(textin+2) == 'O' &&
+                         *(textin+3) == 'E' )   {*p='\327';  *(textin+=4);}
+                 else if(*(textin+2) == 'a' &&
+                         *(textin+3) == 'e' )   {*p='\346';  *(textin+=4);}
+                 else if(*(textin+2) == 'o' &&
+                         *(textin+3) == 'e' )   {*p='\367';  *(textin+=4);}
+                 
+                 else *p= *textin++; 
+                 break;
+               case '?':                         /* reverse question mark*/
+                 *p = '\277'; textin+=2;
+                 break;
+               case 'B':                         /* german SS (B)*/
+                 *p = '\337'; textin+=2;
+                 break;
+               case 'C':                         /* underlined a & o */
+                 if     (*(textin+2) == 'a' ){*p='\252'; textin += 3;}
+                 else if(*(textin+2) == 'o' ){*p='\272'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case 'H':                         /* One half (1/2) */
+                 *p = '\275'; textin+=2;
+                 break;
+               case 'P':                         /* paragrph marker */
+                 *p = '\266'; textin+=2;
+                 break;
+               case 'Q':                         /* One quarter (1/4)*/
+                 *p = '\274'; textin+=2;
+                 break;
+               case 'U':                         /* greek mu */
+                 *p = '\265'; textin+=2;
+                 break;
+               case 'Y':                         /* Yen */
+                 *p = '\245'; textin+=2;
+                 break;
+               case 'a':                         /* Accent*/
+                 if     (*(textin+2) == 'A' ){*p='\301'; textin += 3;}
+                 else if(*(textin+2) == 'E' ){*p='\311'; textin += 3;}
+                 else if(*(textin+2) == 'I' ){*p='\315'; textin += 3;}
+                 else if(*(textin+2) == 'O' ){*p='\323'; textin += 3;}
+                 else if(*(textin+2) == 'U' ){*p='\332'; textin += 3;}
+                 else if(*(textin+2) == 'a' ){*p='\341'; textin += 3;}
+                 else if(*(textin+2) == 'e' ){*p='\351'; textin += 3;}
+                 else if(*(textin+2) == 'i' ){*p='\355'; textin += 3;}
+                 else if(*(textin+2) == 'o' ){*p='\363'; textin += 3;}
+                 else if(*(textin+2) == 'u' ){*p='\372'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case 'c':                         /* C cedilla*/
+                 if     (*(textin+2) == 'C' ){*p='\307'; textin += 3;}
+                 else if(*(textin+2) == 'c' ){*p='\347'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case 'g':                         /* Grave */
+                 if     (*(textin+2) == 'A' ){*p='\300'; textin += 3;}
+                 else if(*(textin+2) == 'E' ){*p='\310'; textin += 3;}
+                 else if(*(textin+2) == 'I' ){*p='\314'; textin += 3;}
+                 else if(*(textin+2) == 'O' ){*p='\322'; textin += 3;}
+                 else if(*(textin+2) == 'U' ){*p='\331'; textin += 3;}
+                 else if(*(textin+2) == 'a' ){*p='\340'; textin += 3;}
+                 else if(*(textin+2) == 'e' ){*p='\350'; textin += 3;}
+                 else if(*(textin+2) == 'i' ){*p='\354'; textin += 3;}
+                 else if(*(textin+2) == 'o' ){*p='\362'; textin += 3;}
+                 else if(*(textin+2) == 'u' ){*p='\371'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case 'm':                         /* A angstrom*/
+                 if     (*(textin+2) == 'A' ){*p='\305'; textin += 3;}
+                 else if(*(textin+2) == 'a' ){*p='\345'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case 'p':                         /* pound sterling */
+                 *p = '\243'; textin+=2;
+                 break;
+               case 's':                         /*  solidus */
+                 *p = '\267'; textin+=2;
+                 break;
+               case 't':                         /* Tilda */
+                 if     (*(textin+2) == 'A' ){*p='\303'; textin += 3;}
+                 else if(*(textin+2) == 'N' ){*p='\321'; textin += 3;}
+                 else if(*(textin+2) == 'O' ){*p='\325'; textin += 3;}
+                 else if(*(textin+2) == 'a' ){*p='\343'; textin += 3;}
+                 else if(*(textin+2) == 'n' ){*p='\361'; textin += 3;}
+                 else if(*(textin+2) == 'o' ){*p='\365'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case 'u':                         /* Umlaut */
+                 if     (*(textin+2) == 'A' ){*p='\304'; textin += 3;}
+                 else if(*(textin+2) == 'E' ){*p='\313'; textin += 3;}
+                 else if(*(textin+2) == 'I' ){*p='\317'; textin += 3;}
+                 else if(*(textin+2) == 'O' ){*p='\326'; textin += 3;}
+                 else if(*(textin+2) == 'U' ){*p='\334'; textin += 3;}
+                 else if(*(textin+2) == 'Y' ){*p='\335'; textin += 3;}
+                 else if(*(textin+2) == 'a' ){*p='\344'; textin += 3;}
+                 else if(*(textin+2) == 'e' ){*p='\353'; textin += 3;}
+                 else if(*(textin+2) == 'i' ){*p='\357'; textin += 3;}
+                 else if(*(textin+2) == 'o' ){*p='\366'; textin += 3;}
+                 else if(*(textin+2) == 'u' ){*p='\374'; textin += 3;}
+                 else if(*(textin+2) == 'y' ){*p='\375'; textin += 3;}
+                 else *p= *textin++; 
+                 break;
+               case 'x':                         /*  Circumflex */
+                 if     (*(textin+2) == 'A' ){*p='\302'; textin += 3;}
+                 else if(*(textin+2) == 'E' ){*p='\312'; textin += 3;}
+                 else if(*(textin+2) == 'I' ){*p='\316'; textin += 3;}
+                 else if(*(textin+2) == 'O' ){*p='\324'; textin += 3;}
+                 else if(*(textin+2) == 'U' ){*p='\333'; textin += 3;}
+                 else if(*(textin+2) == 'a' ){*p='\342'; textin += 3;}
+                 else if(*(textin+2) == 'e' ){*p='\352'; textin += 3;}
+                 else if(*(textin+2) == 'i' ){*p='\356'; textin += 3;}
+                 else if(*(textin+2) == 'o' ){*p='\364'; textin += 3;}
+                 else if(*(textin+2) == 'u' ){*p='\373'; textin += 3;}
+                 else *p= *textin++;
+                 break;
+               default:
+                 textin +=2; /* Skip the \x and ignore it */
+                 *p      = *textin++; /* Copy third character in group */
+                 break;
+             } /*End switch */
+             p++;
+         } else{
+             *p++ = *textin++; /* Finally a plain ordinary ASCII character */
+         }
+     } /* End for loop over string */
+     
+     *p=0;  /*end of string or file */
+   
+     return textout;
+ }
+ 
+ #endif
+ 
