/* display.c */ /* ABZ 21/04/1990, 15/05/1990 */ /* */ /* Output file using bold, underline, etc. */ /* Interprets *xxxx* as bold */ /* _xxxx_ as underlined */ /* and bold generated by printing the same */ /* character in the same place several times*/ /* and underline by backspacing an underline*/ /* character, also some LaTeX {\xxxx word} */ /* stuff for bold, etc. */ /* (The programming in this is atrocious */ /* ... but it works. I'll get round to a */ /* proper re-write soon.) */ #include #include #define STDIN 0 #define STDOUT 1 #define STDERR 2 FILE *f_ptr=STDIN; int c; int lastc=0; int under_flg=0; /* Not used */ int bold_flg=0; /* Not used */ int wrap=0; int convert=0; int xclude=0; int column=0; main(ac,av) unsigned ac; char *av[]; { int here=1; if ((av[1][0]=='-' & av[1][1]!='p' & av[1][1]!='o' & av[1][1]!='x') | ac > 3 | av[2][0] == '-') { pr_string(STDERR,"Usage: "); pr_string(STDERR,av[0]); pr_string(STDERR," [-p|x|o] [filename]\n"); exit(1); } if (av[1][1]=='o' & av[1][0]=='-') { effect('0'); exit(0); } if (av[1][0]=='-' & av[1][1]=='x') { xclude++; here++; } if (av[1][0]=='-' & av[1][1]=='p') { convert++; here++; } if (av[here][0] != 0) { f_ptr=fopen(av[here],"r"); if (f_ptr==NULL) { perror(av[here]); /* report error */ exit(1); } } while((c=get_a_char()) != EOF) { do_this_char(); do_last(); lastc=c; if (c<' ') { do_last(); } } do_last(); if (f_ptr != STDIN) { fclose(f_ptr); } } do_this_char() { if (c==8 & convert==0) { do_back(); } if (c=='_') { do_under(); } /* This must be after do_back */ if (c=='*' && !isalpha(lastc) && xclude==0) { do_last(); lastc=c; c=get_a_char(); do_char('*','1'); } if (c=='{' & xclude==0) { do_curly(); } } do_last() { if (lastc != 0) { p_char(lastc); lastc=0; } } p_char(c) int c; { if (c==9) { p_char(' '); while ((column & 7) != 0) { p_char(' '); } } else { if (c!=0) { if (c==13 || c==10) { column=0; } else { column++; } putchar(c); } } } do_tab() { do_last(); while ((column & 7) != 2) { p_char(' '); } c=0; } get_a_char() { int c; if (f_ptr != STDIN) { c=getc(f_ptr); } else { c=getchar(); } return(c); } do_curly() /* This copes with {\em bold} words */ { int c2; if ((c2=get_a_char()) != 92) { do_last(); lastc=c; c=c2; do_this_char(); } else { if ((c2=get_a_char()) != 'e') { do_last(); p_char('{'); lastc=92; c=c2; do_this_char(); } else { if ((c2=get_a_char()) != 'm') { do_last(); p_char('{'); p_char(92); lastc='e'; c=c2; do_this_char(); } else { if ((c2=get_a_char()) != ' ') { do_last(); p_char('{'); p_char(92); p_char('e'); lastc='m'; c=c2; do_this_char(); } else { do_last(); c=0; do_char('}','1'); c=0; } } } } } do_under() /* This copes with words underlined with a backspace */ { int c2,c3; c3=lastc; do_last(); c2=get_a_char(); if (c2 == '_') { lastc=c2; c=c2; do_under(); } else { if (c2 != 8) { lastc=c; c=c2; /* was <'!' */ if (!isalpha(c3) & xclude==0) { do_char('_','4'); /* Try _underline_ */ c3=get_a_char(); if (isalpha(c3)) { /* _word_word_ intersperced '_' */ if (convert==0) { effect('4'); } do_marked(' ','_','4'); do_marked(c3,'_','4'); lastc=0; do_under(); } else { p_char(c); p_char(c3); /* _word_$ - real end of underline */ c=0; } } /* end of yes-it's-a-letter and yes-I'm-converting */ } /* end of underline followed by not-BS */ else { /* Underline,BS */ effect('4'); p_char(get_a_char()); effect('0'); c=0; lastc=0; } } } do_back() /* Words made bold by backspacing */ { int c2,c3; c2=get_a_char(); while((c2=get_a_char()) == 8) { c3=get_a_char(); } effect('1'); do_last(); effect('0'); c=c2; } do_char(marker,fx_code) /* *Bold* and _underline_ */ int marker,fx_code; /* (Also end of {\em bold} */ { int c2; c2=get_a_char(); /* below used to have || c==39 for "'" */ if ((c2==marker && c2!='_') || c2<' ' || c==39 || (c<'!' && c>0) || c=='.' || c==marker) { do_last(); p_char(c); /* the quote, dot or marker */ c=c2; } else { if (convert==0) { effect(fx_code); } if (c2=='_' && marker==c2) c2=' '; do_marked(c,marker,fx_code); do_marked(c2,marker,fx_code); while ((c2=get_a_char()) != marker & c2 != EOF) { do_marked(c2,marker,fx_code); } if (convert==0) { effect('0'); } lastc=0; c=0; } } do_marked(c,marker,fx_code) int c,marker,fx_code; { if (c<' ') { if (convert==0) { effect('0'); } wrap=1; } if (wrap==1 & c>' ') { if (convert==0) { effect(fx_code); } wrap=0; } if (convert==0 | wrap==1) { p_char(c); } else { if (marker=='*' | marker=='}') { /* *Bold* */ putchar(c); putchar(8); putchar(c); putchar(8); putchar(c); putchar(8); p_char(c); } if (marker=='_') { /* _Underline_ */ putchar('_'); putchar(8); p_char(c); } } } effect(a) int a; { putchar(27); putchar('['); putchar(a); putchar('m'); } pr_string(out,s) /* Writes string to specific output */ char *s; int out; { write (out,s,strlen(s)); }