jac@yoko.rutgers.edu (Jonathan A. Chandross) (12/02/90)
Submitted-by: NONE Posting-number: Volume 1, Source:19 Archive-name: util/entab Architecture: ANY_2 Version-number: 1.00 This C utility replaces all spaces in a file with tabs. Enjoy. =entab.c -/* - * - * entab.c - * - * Replace blanks in a file with tabs. File is modified. - * - * Usage: - * entab [file_1] [file_2] [file_3] [...] - * - * If file is not specified, entab reads from standard in and - * writes to standard out. - * - * - * Contributed Anonymously. Written: November 1983 - * - * Version 1.00 - * - */ - -#include "stdio.h" - -#define TRUE 1 -#define FALSE 0 -#define BLANK ' ' -#define TAB '\t' -#define NL '\n' - -#define INTERVAL 4 -#define SQUOTE 0x27 -#define DQUOTE 0x22 - -main(argc, argv) -int argc ; -char *argv[] ; -{ - FILE *input ; - - - argc-- ; argv++ ; - - if( argc == 0 ) - entab( stdin ) ; - - else - for( ; argc>0 ; argc--,argv++) - if( (input=fopen(*argv,"r")) == NULL ) { - fprintf(stderr, "entab: can't open %s\n", *argv) ; - exit(1) ; - } - else { - entab( input ) ; - fclose( input ) ; - } - - exit(0) ; - -} /* end main */ - -/* entab - replace blanks with tabs */ - -entab( in ) -FILE *in ; -{ - int c, i, nextcol, nb, nt, sqstring, dqstring ; - - - nextcol = nb = nt = 0 ; - sqstring = dqstring = FALSE ; - - while( (c=agetc(in)) != EOF ) { - - nextcol++ ; - if( c==BLANK && !sqstring && !dqstring ) { - nb++ ; - if( nextcol % INTERVAL == 0 && nb > 1 ) { - nt++ ; - nb = 0 ; - } - } - else if( c == NL ) { - aputc( NL, stdout ) ; - nextcol = nb = nt = 0 ; - sqstring = dqstring = FALSE ; - } - else { - for( i=1 ; i <= nt ; i++ ) - aputc( TAB, stdout ) ; - for( i=1 ; i <= nb ; i++ ) - aputc( BLANK, stdout ) ; - nb = nt = 0 ; - aputc( c, stdout ) ; - if( c == SQUOTE ) - sqstring = 1 - sqstring ; - else if( c == DQUOTE ) - dqstring = 1 - dqstring ; - } - - } /* end while */ - -} /* end entab */ - - + END OF ARCHIVE