1: # include "ne.h" 2: # define SIGPIPE 13 /* nroff has stopped reading */ 3: 4: int ESC 033; 5: int HREV '8'; 6: int HFWD '9'; 7: int SI 017; 8: int SO 016; 9: int ESCOUT 033; 10: int HFWDOUT '9'; 11: int HREVOUT '8'; 12: int BKSPOUT '\b'; 13: int FWDOUT '~'; 14: 15: char in[400]; /* input buffer */ 16: int exit(); 17: 18: main(argc,argv) int argc; char *argv[];{ 19: int i, type; 20: flush(); 21: first = 0; 22: lefteq = righteq = '\0'; 23: signal(SIGPIPE, &exit); 24: setfile(argc,argv); 25: while( (type=getline(in)) != '\0' ){ 26: eqline = linect; 27: if( in[0]=='.' && in[1]=='E' && in[2]=='Q' ){ 28: for( i=11; i<100; used[i++]=0 ); 29: printf(".tr ~\n"); 30: printf("%s",in); 31: init(); 32: yyparse(); 33: if( eqnreg>0 ) 34: printf(".ne %d\n.rs\n'sp %d\n\\*(%d\n'sp %d\n", 35: (eqnht+1)/2, (eqnht-eqnbase-2)/2,eqnreg,eqnbase/2); 36: printf(".EN"); 37: if( lastchar == '\0' ){ 38: putchar('\n'); 39: break; 40: } 41: if( putchar(lastchar) != '\n' ) 42: while( putchar(getc()) != '\n' ); 43: flush(); 44: } else if( type != lefteq ) 45: printf("%s",in); 46: else 47: inline(); 48: } 49: putchar('\0'); 50: flush(); 51: exit(); 52: } 53: 54: getline(s) char *s; { 55: char c; 56: while((*s++=c=getc())!='\n' && c!='\0' && c!=lefteq ); 57: if( c==lefteq ) 58: s--; 59: *s++ = '\0'; 60: return(c); 61: } 62: 63: inline() { 64: int i,j,ds[20],t; 65: i = -1; 66: do{ 67: if( i>=17 ){ 68: while((j=getline(in))!='\n' && j!='\0'); 69: error(!FATAL,"missing right delim (?) at %.20s",in); 70: break; 71: } 72: ds[++i] = oalloc(); 73: printf(".ds %d \"%s\n", ds[i], in); 74: init(); 75: yyparse(); 76: if( eqnreg > 0 ) 77: ds[++i] = eqnreg; 78: } while( (t=getline(in)) == lefteq ); 79: ds[++i] = oalloc(); 80: printf(".ds %d \"%s", ds[i], in); 81: for( j=0; j<=i; j++){ 82: printf("\\*(%d", ds[j]); 83: ofree(ds[j]); 84: } 85: putchar('\n'); 86: flush(); 87: } 88: 89: putout(p1) int p1; { 90: if(dbg)printf(".\tanswer <- S%d\n",p1); 91: eqnht = eht[p1]; 92: eqnbase = ebase[p1]; 93: eqnreg = p1; 94: } 95: 96: abs(v) int v; { 97: return( v>0 ? v : -v ); 98: } 99: 100: max(i,j) int i,j; { 101: return( i>j ? i : j ); 102: } 103: 104: oalloc(){ 105: int i; 106: for( i=11; i<100; i++) 107: if( used[i]++ == 0 ) return(i); 108: error( FATAL, "no strings left", i); 109: } 110: 111: ofree(n) int n; { 112: used[n] = 0; 113: } 114: 115: setfile(argc, argv) int argc; char *argv[]; { 116: svargc = --argc; 117: svargv = argv; 118: while( svargc > 0 && svargv[1][0] == '-'){ 119: switch( svargv[1][1] ){ 120: 121: case 'd': lefteq=svargv[1][2]; righteq=svargv[1][3]; break; 122: case 's': break; 123: case 'f': break; 124: default: 125: dbg = 1; 126: ESCOUT = 'E'; 127: HFWDOUT = 'F'; 128: HREVOUT = 'R'; 129: BKSPOUT = 'B'; 130: FWDOUT = 'S'; 131: } 132: svargc--; 133: svargv++; 134: } 135: if( svargc == 0 ) 136: fin = dup(0); 137: else if( (fin = open(svargv[1], 0)) < 0) 138: error( FATAL,"can't open file %s", argv[1]); 139: ptr = 0; 140: fout = dup(1); 141: ifile = 1; 142: linect = 1; 143: } 144: 145: yyerror(){;} 146: 147: int gsize 10; 148: int gfont 'I'; 149: 150: init(){ 151: ct = 0; 152: first++; 153: } 154: 155: error(fatal, s1, s2) int fatal; char *s1, *s2; { 156: int sfout; 157: printf("NEQN ERROR HERE"); 158: flush(fout); 159: sfout = fout; 160: fout = 2; 161: if( fatal>0 ) 162: printf("fatal error: "); 163: printf(s1,s2); 164: printf(" file %s, between lines %d and %d\n", 165: svargv[ifile], eqline, linect); 166: flush(2); 167: fout = sfout; 168: if( fatal > 0 ) 169: exit(1); 170: } 171: 172: down(n) int n; { 173: int c; 174: if( n<= 0 ) 175: c = HREVOUT; 176: else 177: c = HFWDOUT; 178: n = abs(n); 179: while( n-- > 0 ){ 180: putchar(ESCOUT); putchar(c); 181: } 182: } 183: 184: up(n) int n; { 185: int c; 186: if( n<= 0 ) 187: c = HFWDOUT; 188: else 189: c = HREVOUT; 190: n = abs(n); 191: while( n-- > 0 ){ 192: putchar(ESCOUT); putchar(HREVOUT); 193: } 194: } 195: 196: fwd(n) int n; { 197: int c,i; 198: c = n<0 ? BKSPOUT : FWDOUT; 199: n = abs(n); 200: while( n-- > 0 ) 201: putchar(c); 202: } 203: 204: back(n) int n; { 205: int c,i; 206: c = n>0 ? BKSPOUT : FWDOUT; 207: n = abs(n); 208: while( n-- > 0 ) 209: putchar(c); 210: } 211: 212: line(n) int n; { 213: while( n-- > 0 ) 214: putchar('_'); 215: }