home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / tabs / tabs.c < prev   
Encoding:
C/C++ Source or Header  |  1987-03-01  |  1.7 KB  |  101 lines

  1. /* tabs.c by Robert A. Larson
  2.  */
  3.  
  4. #include <stdio.h>
  5. #define inchar()    getc(stdin)
  6. #define outchar(c)    putc(c, stdout)
  7. #define TRUE    1
  8. #define FALSE    0
  9.  
  10. static unsigned otab = 8;
  11. static unsigned lpos = 0;
  12. static unsigned nsp  = 0;
  13.  
  14. outspace() {
  15.   register int i;
  16.   if(otab) {
  17.     while(nsp>1 && nsp >= (i=otab-(lpos%otab))) {
  18.       outchar('\t');
  19.       nsp -= i;
  20.       lpos += i;
  21.     }
  22.   }
  23.   lpos += nsp;
  24.   while(nsp--) outchar(' ');
  25.   nsp = 0;
  26. }
  27.  
  28. main(argc,argv)
  29. int argc;
  30. register char **argv;
  31. {
  32.   register int c;
  33.   register unsigned itab = 8;
  34.   register int trailsup     = FALSE;
  35.  
  36.   while(--argc) {
  37.     switch(**++argv) {
  38.       case '-':
  39.     for(;;) {
  40.       switch(*++*argv) {
  41.         case '\0': goto nextarg;
  42.         case 'i':
  43.           itab = 0;
  44.           while((c = *++*argv)>='0' && c<='9') {
  45.         itab *= 10;
  46.         itab += c - '0';
  47.           }
  48.           (*argv)--;
  49.           break;
  50.         case 'o':
  51.           otab = 0;
  52.           while((c = *++*argv)>='0' && c<='9') {
  53.         otab *= 10;
  54.         otab += c - '0';
  55.           }
  56.           (*argv)--;
  57.           break;
  58.         case 't':
  59.           trailsup = TRUE;
  60.           break;
  61.         default:
  62.           fprintf(stderr, "Unknown switch: %c\n", **argv);
  63.           exit(1);
  64.       }
  65.     }
  66. nextarg: break;
  67.       default:
  68.     fprintf(stderr, "Illegal agument: %s\n", *argv);
  69.     exit(1);
  70.     }
  71.   }
  72.   for(;;){
  73.     switch(c = inchar()){
  74.       case -1:
  75.     if(nsp && !trailsup) outspace();  /* in case the file ends in space */
  76.     exit(0);
  77.       case ' ':
  78.     nsp++;
  79.     break;
  80.       case '\t':
  81.     if(itab) nsp += itab - ((lpos+nsp)%itab);
  82.     else {
  83.       if(nsp) outspace();
  84.       outchar(c);
  85.       lpos++;
  86.     }
  87.     break;
  88.       case '\n':
  89.     if(nsp && !trailsup) outspace();
  90.     outchar(c);
  91.     lpos = nsp = 0;
  92.     break;
  93.       default:
  94.     if(nsp) outspace();
  95.     outchar(c);
  96.     lpos++;
  97.     break;
  98.     }
  99.   }
  100. }
  101.