/****************************************************************************** * detab.c * Convert all tabs to n-column spaces. * * Notice * Copyright (c)Jul 1987, David R Tribble. * * 1.00 04-16-1985 drt. * 1.01 07-26-1986 drt. Cleaned program up a bit. * 1.02 04-03-1987 drt. Fixed usage. * 1.03 06-16-1987 drt. Added setvbuf() usage. * * 2.00 07-20-1987 drt. New main(), uses fsearch functions. * 2.01 12-17-1987 drt. Uses fpsearch functions. */ /* Identification */ #define ID_TITLE "DETAB" #define ID_DATE "12-17-1987" #define ID_VERS "2.01" #define ID_FILE __FILE__ #define ID_AUTH "David R. Tribble" #define ID_COMP __DATE__ " " __TIME__ static const char copyright[] = "Copyright (c)1987, " ID_AUTH "."; static const char id[] = "@(#) " ID_DATE " " ID_VERS " " ID_FILE; static const char id_compile[] = "[" ID_TITLE " " ID_VERS ", " ID_DATE "] <" ID_COMP ">"; static const char title[] = ID_TITLE; static const char date[] = ID_DATE; static const char vers[] = ID_VERS; static const char auth[] = ID_AUTH; static const char file[] = ID_FILE; static const char * prog = title; /* System includes */ #define NO_EXT_KEYS #define LINT_ARGS #include #include #include /* Local includes */ #include "optargs.h" #ifndef NFSEARCH #include "fsearch.h" #endif /* Manifest constants */ #define TABSZ 8 /* default tab spacing */ #define HT '\t' #define NL '\n' #define FF '\f' #define SP ' ' /****************************************************************************** * detab * Convert tabs to equivalent whitespace run of spaces, reading * from file in and writing to file out. */ void detab(FILE *in, FILE *out, int tabsz) { int c; int col; col = 0; c = getc(in); while (c != EOF) { while (c != NL && c != EOF) { if (c == HT) { for (; col < tabsz; ++col) putc(SP, out); col = 0; } else { putc(c, out); ++ col; if (col >= tabsz) col = 0; } c = getc(in); } if (c != EOF) { putc(c, out); c = getc(in); } col = 0; } } /****************************************************************************** * main */ static char invbuf[20*1024]; static char outvbuf[20*1024]; int main(int argc, char **argv) { int tablen; FILE * in; int i; /* get -N option */ tablen = TABSZ; /* default */ OPTIONS default: tablen = atoi(OPTSTR-1); NEXTARG; ENDOPTIONS setvbuf(stdout, outvbuf, _IOFBF, sizeof outvbuf); /* get filename, if any */ if (argc > 0) { for (i = 0; i < argc; ++i) { #ifdef NFSEARCH in = fopen(argv[i], "r"); if (in != NULL) { setvbuf(in, invbuf, _IOFBF, sizeof invbuf); fprintf(stderr, "%s\n", argv[i]); detab(in, stdout, tablen); fclose(in); } else fprintf(stderr, "%s: cannot read %s\n", prog, argv[i]); #else if (fpstart(argv[i], NULL, _A_NORMAL)) { while (fpnext(NULL, NULL, 1)) { in = fopen(fps_name, "r"); if (in != NULL) { setvbuf(in, invbuf, _IOFBF, sizeof invbuf); fprintf(stderr, "%s\n", fps_name); detab(in, stdout, tablen); fclose(in); } else fprintf(stderr, "%s: cannot read %s\n", prog, fps_name); } } else fprintf(stderr, "%s: cannot find %s\n", prog, argv[i]); #endif } } else if (! isatty(fileno(stdin))) { setvbuf(stdin, invbuf, _IOFBF, sizeof invbuf); detab(stdin, stdout, tablen); } else goto usage; return EXIT_SUCCESS; usage: fprintf(stderr, "[%s, %s %s]\n" "Convert tabs into spaces.\n\n", prog, vers, date); fprintf(stderr, "usage: %s [-N] file...\n" "or: %s [-N]