/** * Converts a table of tab separated values into HTML. * * Eric Blossom 2004 */ #include #include #include /* for getopt */ char *program_name; void filter_file( FILE * in, FILE * out ) { int c; fputs( "\n", out ); c = fgetc( in ); if ( EOF != c ) { fputs( "\n", out ); c = fgetc( in ); if ( EOF != c ) { fputs( "
", out ); } ungetc( c, in ); while ( EOF != ( c = fgetc( in ) ) ) { switch ( c ) { case '\t': fputs( "", out ); break; case '\n': fputs( "
", out ); } ungetc( c, in ); break; default: fputc( c, out ); } } fputs( "
\n", out ); } void filter_named_file( char *fname, FILE * out ) { FILE *f; if ( 0 == strcmp( fname, "-" ) ) { filter_file( stdin, out ); } else { f = fopen( fname, "r" ); if ( NULL == f ) { fprintf( stderr, "%s: could not open %s\n", program_name, fname ); exit( 1 ); } else { filter_file( f, out ); ( void ) fclose( f ); } } } int main( int argc, char *argv[] ) { int option; program_name = *argv; /* Make access to the program name global. */ while ( ( option = getopt( argc, argv, "?" ) ) != EOF ) { switch ( option ) { case '?': fprintf( stderr, "usage: %s [-?] [filename] [filename] ...\n", program_name ); fprintf( stderr, " Converts tables from tab-separated-values to HTML.\n" ); exit( 1 ); break; } } if ( argc <= optind ) { /* No files were specified. */ /* Use stdin like a good filter. */ filter_file( stdin, stdout ); } else { /* Files were specified. */ while ( optind < argc ) { filter_named_file( argv[optind++], stdout ); } } if ( ferror( stdout ) ) { fprintf( stderr, "%s: error writing stdout\n", program_name ); return 2; } return 0; }