home *** CD-ROM | disk | FTP | other *** search
- /* tabs.c by Robert A. Larson
- */
-
- #include <stdio.h>
- #define inchar() getc(stdin)
- #define outchar(c) putc(c, stdout)
- #define TRUE 1
- #define FALSE 0
-
- static unsigned otab = 8;
- static unsigned lpos = 0;
- static unsigned nsp = 0;
-
- outspace() {
- register int i;
- if(otab) {
- while(nsp>1 && nsp >= (i=otab-(lpos%otab))) {
- outchar('\t');
- nsp -= i;
- lpos += i;
- }
- }
- lpos += nsp;
- while(nsp--) outchar(' ');
- nsp = 0;
- }
-
- main(argc,argv)
- int argc;
- register char **argv;
- {
- register int c;
- register unsigned itab = 8;
- register int trailsup = FALSE;
-
- while(--argc) {
- switch(**++argv) {
- case '-':
- for(;;) {
- switch(*++*argv) {
- case '\0': goto nextarg;
- case 'i':
- itab = 0;
- while((c = *++*argv)>='0' && c<='9') {
- itab *= 10;
- itab += c - '0';
- }
- (*argv)--;
- break;
- case 'o':
- otab = 0;
- while((c = *++*argv)>='0' && c<='9') {
- otab *= 10;
- otab += c - '0';
- }
- (*argv)--;
- break;
- case 't':
- trailsup = TRUE;
- break;
- default:
- fprintf(stderr, "Unknown switch: %c\n", **argv);
- exit(1);
- }
- }
- nextarg: break;
- default:
- fprintf(stderr, "Illegal agument: %s\n", *argv);
- exit(1);
- }
- }
- for(;;){
- switch(c = inchar()){
- case -1:
- if(nsp && !trailsup) outspace(); /* in case the file ends in space */
- exit(0);
- case ' ':
- nsp++;
- break;
- case '\t':
- if(itab) nsp += itab - ((lpos+nsp)%itab);
- else {
- if(nsp) outspace();
- outchar(c);
- lpos++;
- }
- break;
- case '\n':
- if(nsp && !trailsup) outspace();
- outchar(c);
- lpos = nsp = 0;
- break;
- default:
- if(nsp) outspace();
- outchar(c);
- lpos++;
- break;
- }
- }
- }
-