home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / txtutils.lbr / ENTAB.CZ / ENTAB.C
Encoding:
C/C++ Source or Header  |  1993-10-25  |  1.6 KB  |  100 lines

  1. /* copyright (c) 1982    Peter Baker */
  2.  
  3. #include <stdio.h>
  4.  
  5. /*
  6.  *   Replace blanks by tabs using standard input and output.
  7.  * 
  8.  *   Heavily derived from Software Toolworks by Kernighan and Plauger.
  9.  */
  10.  
  11. #define BOOLEAN    int
  12. #define MAXLINE    133
  13. #define BLANK    ' '
  14. #define TAB        0X09
  15. #define NEWLINE '\n'
  16.  
  17. main( argc, argv)
  18. char *argv[];
  19. int argc;
  20. {
  21.     int c;                /* int because it has to be able to hold EOF */
  22.     int col, i, newcol;
  23.     BOOLEAN  tabpos();
  24.     BOOLEAN     tabs[MAXLINE];
  25.     int tabsize;        /* tab at every tabsize'th column */
  26.  
  27.     if( argc > 1 )
  28.     {
  29.         if( *(argv[1]+1) == 'T' )
  30.             tabsize = atoi( argv[1]+2 );
  31.         else
  32.             tabsize = 8;
  33.     }
  34.     else
  35.         tabsize = 8;
  36.  
  37.     settab( tabs, tabsize );
  38.  
  39.     col = 1;
  40.     while( 1 )            /* repeat until break */
  41.     {
  42.         newcol = col;
  43.         while( (c = getchar()) == BLANK )        /* collect blanks */
  44.         {
  45.             newcol++;
  46.             if( tabpos( newcol, tabs ) )
  47.             {
  48.                 putchar( TAB );
  49.                 col = newcol;
  50.             }
  51.         }
  52.         while( col < newcol )
  53.         {
  54.             putchar( BLANK );                /* output leftover blanks */
  55.             col++;
  56.         }
  57.         if( c == EOF )
  58.             break;
  59.         putchar( c );
  60.         if( c == NEWLINE )
  61.             col = 1;
  62.         else
  63.             col++;
  64.     }
  65. }
  66.  
  67. /*
  68.  * example: if tabsize == 8 then tab stops will be at:
  69.  *    1 9 17 25 33 ...
  70.  */
  71. settab( tabs, tabsize )
  72. char *tabs;
  73. int tabsize;
  74. {
  75.     int i;
  76.  
  77.     for( i = 1 ; i < MAXLINE ; i++ )
  78.         if( (i % tabsize) == 0 )
  79.             tabs[i+1] = true;
  80.         else
  81.             tabs[i+1] = false;
  82. }
  83.  
  84. BOOLEAN tabpos( col, tabs )
  85. char *tabs;
  86. int col;
  87. {
  88.     if( col > MAXLINE )
  89.         return( true );
  90.     else
  91.         return( tabs[col] );
  92. }
  93. /
  94.  
  95.     if( argc > 1 )
  96.     {
  97.         if( *(argv[1]+1) == 'T' )
  98.             tabsize = atoi( argv[1]+2 );
  99.         else
  100.