home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TABS.ZIP / ENTAB.C < prev    next >
C/C++ Source or Header  |  1990-09-20  |  4KB  |  136 lines

  1. /* entab [1.0.2] - 07-Dec-87 16:50                               John Layman */
  2.  
  3. /*****************************************************************************\
  4. * This filter substitutes tabs for blank substrings in a file.                *
  5. *                                                                             *
  6. * Synopsis:                     entab [t1..tn]                                *
  7. *                                                                             *
  8. * where the optional parameter(s) indicate tab positions.                     *
  9. *                                                                             *
  10. * When no parameter is present, the filter presumes the default system tab    *
  11. * settings (i.e. where column MOD 8 = 1).                                     *
  12. *                                                                             *
  13. * When a single parameter is present, it is considered to be the tab gap      *
  14. * (i.e. tabs are where column MOD gap = 1).                                   *
  15. *                                                                             *
  16. * When multiple parameters are present, they explicitly define each tab       *
  17. * position.                                                                   *
  18. *                                                                             *
  19. * To prevent the corruption of string literals in C programs, the character   *
  20. * stream is monitored for double quotes, and tab substitution is suppressed   *
  21. * within a quoted string.  Quotes which are escaped, or which appear within   *
  22. * a remark are ignored.  Nested remarks are supported.                        *
  23. \*****************************************************************************/
  24.  
  25. #include <stdio.h>
  26.  
  27. #define DEFAULT 8
  28. #define MAXLEN  255
  29.  
  30. #define cycle for(;;)
  31.  
  32. void chktab(), settab(), settabs();
  33.  
  34. char pos[MAXLEN];       /* tab bar, where tab stops == '\t'     */
  35. int ch, prevch;
  36.  
  37.  
  38. main(argc,argv)
  39.   int argc;
  40.   char *argv[];
  41. {
  42.   int col, comment, i, literal, mark;
  43.  
  44.   if (argc == 1)
  45.     settabs(DEFAULT);
  46.   else
  47.   if (argc == 2)
  48.     settabs(atoi(argv[1]));
  49.   else
  50.   for (i = 1; i < argc; i++)
  51.     settab(atoi(argv[i]));
  52.  
  53.   comment = literal = 0;
  54.   col = 1;
  55.   cycle
  56.   {
  57.     mark = col;
  58.     while ((ch = getchar()) == ' ' && !literal)
  59.     {
  60.       col++;
  61.       if (pos[col] == '\t')
  62.       {
  63.         if (col-mark > 1)
  64.           put('\t');
  65.         else
  66.           put(' ');
  67.         mark = col;
  68.       }
  69.     }
  70.     for (; mark < col; mark++)
  71.       put(' ');
  72.     if (ch == EOF)
  73.       break;
  74.     if (ch == '\t')
  75.     {
  76.       fputs("Tab encountered\n",stderr);
  77.       exit(1);
  78.     }
  79.     if (!comment)
  80.       if (prevch != '\\' && ch == '\"')
  81.         literal = !literal;
  82.     if (!literal)
  83.       if (prevch == '/' && ch == '*')
  84.         ++comment;
  85.       else
  86.       if (prevch == '*' && ch == '/')
  87.         --comment;
  88.     put(ch);
  89.     if (ch == '\n')
  90.       col = 1;
  91.     else
  92.       col++;
  93.   }
  94. }
  95.  
  96.  
  97. put(ch)
  98.   int ch;
  99. {
  100.   if (prevch == '\\')
  101.     prevch = '\0';
  102.   else
  103.     prevch = ch;
  104.   return(putchar(ch));
  105. }
  106.  
  107.  
  108. void settab(col)
  109.   int col;
  110. {
  111.   chktab(col);
  112.   pos[col] = '\t';
  113. }
  114.  
  115.  
  116. void settabs(gap)
  117.   int gap;
  118. {
  119.   int col;
  120.  
  121.   chktab(gap);
  122.   for (col = gap+1; col <= MAXLEN; col += gap)
  123.     pos[col] = '\t';
  124. }
  125.  
  126.  
  127. void chktab(col)
  128.   int col;
  129. {
  130.   if (col < 1 || col > MAXLEN)
  131.   {
  132.     fputs("Invalid parameter\n",stderr);
  133.     exit(1);
  134.   }
  135. }
  136.