|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T t
Length: 16067 (0x3ec3) Types: TextFile Names: »texify.c«
└─⟦52210d11f⟧ Bits:30007239 EUUGD2: TeX 3 1992-12 └─⟦87f627614⟧ »utils/texify.tar.Z« └─⟦9880826d9⟧ └─⟦this⟧ »texify.c«
/* $Header: //hufsa/u1/hanche/src/RCS/texify.c,v 1.6 91/05/14 21:39:36 hanche Exp $ */ /* texify.c Copyright (c) 1988, 1990, 1991 Harald Hanche-Olsen The author of this program has retained the copyright only so that others may not claim it. Users are granted the right to do whatever they please with the program under the following restrictions: Do not remove this copyright notice. If you do modify the program, comments to that effect should be included in the source code. Please also note that the program is supplied as is, without any warranty. The author will not accept liability for any loss of data or other direct or indirect damages caused by the program. */ /* This program was called 8to7bit before. */ #define PROGNAME "texify" #define VERSION "1.6, 1991-05-14" /* $Log: texify.c,v $ * Revision 1.6 91/05/14 21:39:36 hanche * Corrected a few bugs in the Macintosh table. Also added a few codes. * * * Revision 1.5.1.2 1991/05/13 08:38:49 anund * Endret s} programmet fungerer med kompilatorer som gj|r strengkonstanter * ikke-skrivbare. * * Revision 1.5.1.1 1991/04/10 07:53:05 anund * Removed a few compilation warnings. * * Revision 1.5 91/01/22 12:23:29 hanche * Two new translation tables: "Norwegian" and ISO8859-1 tables. * Also fixed a minor bug in one of the translation tables. * These changes due to Anund Lie (thanks, Anund). * * * Revision 1.4 90/09/03 16:54:01 hanche * Corrected the Macintosh translation table: Avoid wrong use of {} of * math mode operators. For example, $a{\le}b$ yields the wrong spacing. * * Revision 1.3 90/08/24 19:23:22 hanche * Mac translation tables succesfully added. Now hopefully ANSI compatible. * * Revision 1.2 90/08/24 13:40:11 hanche * This is an attempt at getting the program ready for ANSI, * plus getting it ready to support more built-in translation tables. * */ #include <stdlib.h> #include <stdio.h> #ifdef NOIMPLICIT /* define otherwise implicitly defined functions */ int fprintf(),printf(),fclose(),_filbuf(),_flsbuf(), fscanf(); int unlink(),rename(); int strlen(),strcpy(); #endif #define TRUE 1 #define FALSE 0 #define uchr unsigned char /*********************/ /* Global variables: */ /*********************/ #define CP (uchr *) /* Default translation table */ uchr *dos_trans[] = { CP"\221{\\ae}", CP"\233{\\o}", CP"\206{\\aa}", CP"\222{\\AE}", CP"\235{\\O}", CP"\217{\\AA}", CP"\200\\c C", CP"\201\\\"u", CP"\202\\\'e", CP"\203\\^a", CP"\204\\\"a", CP"\205\\`a", CP"\207\\c c", CP"\210\\^e", CP"\211\\\"e", CP"\212\\`e", CP"\213{\\\"\\i}", CP"\214{\\^\\i}", CP"\216\\\"A", CP"\220\\\'E", CP"\223\\^o", CP"\224\\\"o", CP"\225\\`o", CP"\226\\^u", CP"\227\\`u", CP"\230\\\"y", CP"\231\\\"O", CP"\232\\\"U", CP"\240\\\'a", CP"\241{\\\'\\i}", CP"\242\\\'o", CP"\243\\\'u", CP"\244\\~n", CP"\245\\~N", CP"\250?`", CP"\255!`", CP"" }; uchr *mac_trans[] = { CP"\276{\\ae}", CP"\277{\\o}", CP"\214{\\aa}", CP"\256{\\AE}", CP"\257{\\O}", CP"\201{\\AA}", CP"\317{\\oe}", CP"\316{\\OE}", CP"\210\\`a", CP"\217\\`e", CP"\223{\\`\\i}", CP"\230\\`o", CP"\235\\`u", CP"\203\\`E", CP"\207\\'a", CP"\216\\'e", CP"\222{\\'\\i}", CP"\227\\'o", CP"\234\\'u", CP"\203\\'E", CP"\313\\`A", CP"\314\\^A", CP"\212\\\"a", CP"\221\\\"e", CP"\225{\\\"\\i}",CP"\232\\\"o", CP"\237\\\"u", CP"\330\\\"y", CP"\200\\\"A", CP"\205\\\"O", CP"\206\\\"U", CP"\211\\^a", CP"\220\\^e", CP"\224\\^i", CP"\231\\^o", CP"\236\\^u", CP"\213\\~a", CP"\233\\~o", CP"\226\\~n", CP"\315\\~O", CP"\204\\~N", CP"\300?`", CP"\301!`", CP"\261\\pm ", CP"\255\\ne ", CP"\262\\le ", CP"\263\\ge ", CP"▶ab◀'", /* '\253' looks like a right quote on a mac, while '\047' looks "straight" */ CP"\324`", CP"\325'", /* Variations of left and right quote */ #ifdef NOTDEF CP"\307<<", CP"\310>>",/* left, right <<french quotes>> */ #endif CP"\322``", CP"\323''", CP"\265\\mu ", CP"\271\\pi ", CP"\247{\\ss}", CP"\215\\c c", CP"\311\\ldots ", CP"\321---", CP"\270\\Pi ", CP"\267\\Sigma ", CP"\306\\Delta ", CP"\272\\int ", CP"\245\\bullet", CP"\275\\Omega", CP"\303\\sqrt", CP"\326\\div", CP"\305\\approx", CP"\327\\lozenge", CP"\302\\neg", CP"\243\\pounds", CP"\244\\S", CP"\246\\P", CP"\331\\equiv", CP"" }; /* * Some ISO8859-1 characters have no simple TeX equivalent. * These are translated to a TeX control sequence corresponding to * the character name. * Some of the characters correspond to math symbols, and are * converted under the assumption that they will be used inside math * environments. */ uchr *iso8859_1_trans[] = { CP"\240~", CP"\241?`", CP"\242{\\cent}", CP"\243{\\pounds}", CP"\244{\\currency}", CP"\245{\\yen}", CP"\246{\\brokenbar}", CP"\247{\\S}", CP"\250\\\"\\mbox{0.5em}", CP"\251{\\copyright}", CP"\252{\\ordfeminine}", CP"\253{\\guillemotleft}", CP"\254{\\neg}", CP"\255-", CP"\256{\\registered}", CP"\257{\\macron}", CP"\260{\\degree}", CP"\261{\\pm}", CP"\262{\\twosuperior}", CP"\263{\\threesuperior}", CP"\264\\\'\\mbox{0.5em}", CP"\265{\\mu}", CP"\266{\\P}", CP"\267{\\cdot}", CP"\270{\\c\\mbox{0.5em}}", CP"\271{\\onesuperior}", CP"\272{\\ordmasculine}", CP"\273{\\guillemotright}", CP"\274{\\onequarter}", CP"\275{\\onehalf}", CP"\276{\\threequarters}", CP"\277?`", CP"\300\\`A", CP"\301\\\'A", CP"\302\\^A", CP"\303\\~A", CP"\304\\\"A", CP"\305{\\AA}", CP"\306{\\AE}", CP"\307\\c{C}", CP"\310\\`E", CP"\311\\\'E", CP"\312\\^E", CP"\313\\\"E", CP"\314\\`I", CP"\315\\\'I", CP"\316\\^I", CP"\317\\\"I", CP"\320{\\Eth}", CP"\321\\~N", CP"\322\\`O", CP"\323\\\'O", CP"\324\\^O", CP"\325\\~O", CP"\326\\\"O", CP"\327{\\times}", CP"\330{\\O}", CP"\331\\`U", CP"\332\\\'U", CP"\333\\^U", CP"\334\\\"U", CP"\335\\\'Y", CP"\336{\\Thorn}", CP"\337{\\ss}", CP"\340\\`a", CP"\341\\\'a", CP"\342\\^a", CP"\343\\~a", CP"\344\\\"a", CP"\345{\\aa}", CP"\346{\\ae}", CP"\347\\c{c}", CP"\350\\`e", CP"\351\\\'e", CP"\352\\^e", CP"\353\\\"e", CP"\354\\`i", CP"\355\\\'i", CP"\356\\^i", CP"\357\\\"i", CP"\360{\\eth}", CP"\361\\~n", CP"\362\\`o", CP"\363\\\'o", CP"\364\\^o", CP"\365\\~o", CP"\366\\\"o", CP"\367{\\div}", CP"\370{\\o}", CP"\371\\`u", CP"\372\\\'u", CP"\373\\^u", CP"\374\\\"u", CP"\375\\\'y", CP"\376{\\thorn}", CP"\377\\\"y", CP"" }; uchr *nor_trans[] = { CP"\133{\\AE}", CP"\134{\\O}", CP"\135{\\AA}", CP"\173{\\ae}", CP"\174{\\o}", CP"\175{\\aa}", CP"" }; /* Translation table to be used */ char what_to_do[256]; #define NO_TRANSLATE 0 #define DO_TRANSLATE 1 #define ILLEGAL_CHAR 2 uchr *replacement[256]; int verbose=FALSE, show_tr=FALSE; int illegal_hit[256]; int some_illegal=0; /***********************/ /* Function prototypes */ /***********************/ void do_translate(void); /* Translate from stdin to stdout */ void init_table( char *, uchr ** ); /* Initialize translation table. If called for, read auxiliary translation table from a file */ void illegal_char( int ); /* Take action on reading an illegal character */ void show_table(void); /* Show the translation table if show_tr is set */ void usage( int ); /* Print usage note and exit */ void version(void); /* Print version number */ #define TERSE 0 #define VERBOSE 1 /********/ /* main */ /********/ void main(int argc, char * argv[]) { int i, i0; char *t_filename = (char *)NULL; /* Translation table file name */ char *o_filename = (char *)NULL; /* Output file name */ char *i_filename = (char *)NULL; /* Input file name */ char *m_filename = "$7to8bit.$$$"; /* Intermediate output file name */ uchr **use_trans= NULL; /* What built-in table? */ char *arg, *envflags; for( i=0; i<256; i++ ) illegal_hit[i] = 0; envflags = getenv("TEXIFY"); i0 = (envflags == NULL) ? 1 : 0; /* Process arguments */ for( i = (envflags == NULL) ? 1 : 0; i < argc; i++ ) { arg = (i == 0) ? envflags : argv[i]; if ( *arg == '-' ) while ( *++arg != '\0' ) switch( *arg) { case 'i' : use_trans = (uchr **)iso8859_1_trans; break; case 'n' : use_trans = (uchr **)nor_trans; break; case 'm' : use_trans = (uchr **)mac_trans; break; case 'd' : use_trans = (uchr **)dos_trans; break; case 't' : if ( ++i < argc ) t_filename=argv[i]; else usage(TERSE); break; case 'o' : if ( ++i < argc ) o_filename=argv[i]; else usage(TERSE); break; case '?' : usage(VERBOSE); break; case 'v' : verbose=TRUE; break; case 'V' : version(); break; case 's' : show_tr=TRUE; break; default : usage(TERSE);} else i_filename=arg;} /* Initialize translations table */ init_table(t_filename, use_trans); if ( i_filename == (char *) NULL ) usage(TERSE); /* Open files and check their status */ if (freopen( i_filename, "r", stdin ) == (FILE *) NULL) { fprintf ( stderr, "%s: Could not open file %s for reading\n", PROGNAME, i_filename ); exit(1); } if (o_filename != (char *) NULL) m_filename=o_filename; if (freopen( m_filename, "w", stdout ) == (FILE *) NULL) { fprintf ( stderr, "%s: Could not open file %s for writing\n", PROGNAME, m_filename ); exit(1); } if ( verbose ) fprintf( stderr, "Translating from %s to %s\n", i_filename, m_filename ); /*************/ do_translate(); /*************/ if ( fclose( stdin ) != 0 ) { fprintf( stderr, "%s: Could not close the input file\n", PROGNAME); exit(1); } if ( fclose( stdout ) != 0 ) { fprintf( stderr, "%s: Could not close the output file\n", PROGNAME); exit(1); } if ( some_illegal ) { fprintf( stderr, "%s: WARNING - characters without translation found!\n" "\tEach has been replaced by a \"\\char n \" construct.\n", PROGNAME ); if ( verbose ) for ( i = 0; i < 256; ++i ) if( illegal_hit[i] > 0 ) fprintf( stderr, "%5d occurences of char %3d\n", illegal_hit[i], i ); } if ( verbose ) fprintf( stderr, "Translation done and files closed.\n" ); if (o_filename == (char *)NULL ) { if ( verbose ) fprintf( stderr, "Will now replace %s by the output file:\n", i_filename ); if ( unlink( i_filename ) != 0 ) { fprintf( stderr, "%s: Could not delete %s\n", PROGNAME, i_filename ); exit( 1 ); } if ( rename ( m_filename, i_filename ) != 0 ) { fprintf( stderr, "%s: Could not rename %s to %s\n", PROGNAME, m_filename, i_filename ); exit( 1 ); }} if ( verbose ) fprintf( stderr, "Done.\n" ); } /*********/ /* usage */ /*********/ void usage(int verbosity) { int i; static char *v_usage[] = { "Convert an 8-bit (MultiLingual) TeX file to a 7-bit one.", "The program replaces certain single characters in the given", "file by replacement strings.", " ", "Options include:", "-v\tfor verbose operation", "-V\tto show the version number", "-d\tto use DOS translation table", "-m\tto use Mac translation table", "-n\tto translate Norwegian characters {|}[\\] to their TeX equivalents", "-i\tto use the ISO8859-1 translation table", "-s\tto show the translation table being used", "-o\tto specify the output file", "\t(otherwise, the output file will replace the input)", "-t\tto specify a file containing translations in addition to", "\tor replacing those built into the program", " ", "\tEach line of the file is interpreted in the following way:", "\tThe first character on the line will be replaced by", "\tthe text given by the remainder of the line", "\t(Sorry, no escape conventions are supported).", ""}; if ( verbosity == VERBOSE ) fprintf( stderr, "%s %s\n", PROGNAME, VERSION ); fprintf( stderr, "Usage: %s [ -{d|m|n|i}svV ][ -t tablefilename ] filename [-o outputfilename]\n", PROGNAME); if ( verbosity == VERBOSE ) for ( i = 0; v_usage[i][0] != '\0'; i++ ) fprintf( stderr, "\t%s\n", v_usage[i] ); else fprintf( stderr, "\t%s -? for a longer explanation\n", PROGNAME ); exit(1); } /***********/ /* version */ /***********/ void version(void) { fprintf( stderr, "%s %s\n", PROGNAME, VERSION ); } /****************/ /* do_translate */ /****************/ void do_translate(void) /* Translate from stdin to stdout */ /* Return 1 if successful - 0 if not */ { int ch; while ( (ch = getchar()) != EOF) switch ( what_to_do[ ch ] ) { case NO_TRANSLATE: putchar(ch); break; case DO_TRANSLATE: printf( "%s", replacement[ ch ] ); break; default: printf( "\\char%d ", ch ); some_illegal++; illegal_hit[ch]++; } } /**************/ /* init_table */ /**************/ void init_table(char * t_filename, uchr **use_trans ) { FILE *t_file; uchr t_file_line[200], *str; int i, j, n; if (use_trans == NULL && t_filename == NULL) { fprintf(stderr, "You must specify a translation table!\n"); usage(TERSE); } /* Initialize translation tables */ for( i = 0; i<256; i++ ) what_to_do[ i ] = ILLEGAL_CHAR; /* Everything is illegal, except ... */ /* ... what is permitted : */ for( i = 32; i<127; i++ ) what_to_do[ i ] = NO_TRANSLATE; /* Printable ASCII chars are OK */ what_to_do[ (int) '\n' ] = NO_TRANSLATE; /* Newline is OK */ what_to_do[ (int) '\t' ] = NO_TRANSLATE; /* Tab is OK */ /* Default translation table */ if ( use_trans != NULL ) for ( i = 0; use_trans[i][0] != '\0'; i++ ) { j = (int)use_trans[i][0]; what_to_do [j] = DO_TRANSLATE; replacement[j] = (uchr *)&use_trans[i][1]; } /* If no auxiliary table: */ if ( t_filename == (char *)NULL ) { if ( verbose ) fprintf( stderr, "[Default translation table only]\n" ); show_table(); return; } /* If auxiliary table: */ if ( verbose ) fprintf( stderr, "[Auxiliary translations from file %s]\n", t_filename ); if ( ( t_file = fopen( t_filename, "r" ) ) == (FILE *)NULL ) { fprintf( stderr, "%s: Could not open file %s in mode %s\n", PROGNAME, t_filename, "r" ); exit(1); } while( fscanf( t_file, "%s\n", t_file_line ) != EOF) if ( (n = strlen( t_file_line )) > 0 ) { if ( (str = (uchr *)malloc( n )) == (uchr *)NULL ) { fprintf( stderr, "%s: Insufficient memory!\n", PROGNAME ); exit(1); } n = (int) t_file_line[0]; if ( (t_file_line[1] == n ) && (t_file_line[2] == '\0') ) what_to_do[n] = NO_TRANSLATE; else { strcpy( str, &t_file_line[1] ); what_to_do [ n ] = DO_TRANSLATE; replacement[ n ] = str; }} fclose( t_file ); show_table(); } /**************/ /* show_table */ /**************/ void show_table() { int i, j, k; char literal[]="_", control[]="^_"; if ( ! show_tr ) return; printf( "Dec\tChar\tReplacement\n\n" ); for( i=0; i < 256; i++ ) if ( what_to_do[ i ] == DO_TRANSLATE ) printf( "%3d\t%s\t%s\n", i, ( ( i < 32 || i == 127 ) ? ( control[1] = (char) ( i ^ 0100 ), control ) : ( *literal = (char)i, literal )), replacement[i] ); printf( "\nIllegal characters are" ); j = -1; for( i=0; i < 256; i++ ) if( what_to_do[ i ] == ILLEGAL_CHAR ) { j = i; for( ; i < 256; i++ ) if( what_to_do[ i ] != ILLEGAL_CHAR ) break; k = i-1; if ( k > j ) printf( ", %d-%d", j, k ); else printf( ", %d", j ); } printf( ".\n" ); }