home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TOOLS2.ZIP / RETABF.C < prev    next >
Text File  |  1987-08-18  |  5KB  |  183 lines

  1. /*
  2.     HEADER:        CUG000.00;
  3.     TITLE:        Entab/Detab/Strip Combo -- Filter Version;
  4.     DATE:        05/25/1987;
  5.     DESCRIPTION:    "Strips upper-order bits, detabifies, then entabifies a
  6.             text file.  Old and new tab increments can differ.  If
  7.             new increment is 1, no entabification occurs.";
  8.     VERSION:    0.0;
  9.     KEYWORDS:    Tab, Tabs, Tab Expansion, Tab Utility, Tabify, Untab,
  10.             Detab, Detab Utility;
  11.     FILENAME:    RETABF.C;
  12.     COMPILERS:    vanilla;
  13.     AUTHORS:    W. C. Colley III;
  14. */
  15.  
  16. /*
  17.              Tab Size Readjustment Program
  18.  
  19.            Copyright (c) 1987 William C. Colley, III
  20.  
  21. This program combines the functions of an ENTAB program and a DETAB program
  22. in the same program.  Combining the two allows a simple means of retabifying
  23. a program.  For example, let's say you edit a C program with your editor set
  24. for a tab increment of 4.  Now, you TYPE this file on the console and you find
  25. that it laps over the right edge of the screen because your terminal expands
  26. tabs with a tab increment of 8.     What you really would like to do is delete
  27. some of the tabs so that your program will TYPE just like it looked inside the
  28. editor.     The following command will do just that:
  29.  
  30.     RETABF -O4 -N8 <oldfile >newfile
  31.  
  32. In the above command line, the parameters can appear in any order.  The -
  33. options are optional.  Their default values are set by a pair of #defines
  34. below.    The -O option specifies the tab increment that the old file was
  35. created with.  The -N option specifies the desired tab increment of the new
  36. file.
  37.  
  38. Note that or -N1 causes no entabification.  Thus, the following command will
  39. detabify a file:
  40.  
  41.     RETABF -O8 -N1 <oldfile >newfile
  42.  
  43. Also note that if the old file contains no tabs, there will be no
  44. detabification.     Thus, the following command will entabify a detabified file:
  45.  
  46.     RETAB -N8 <oldfile >newfile
  47.  
  48. Blanks and tabs in quoted strings are left alone.  The C conventions regarding
  49. character constants like '"' and character strings like "Here\'s a \"quote\"."
  50. are supported.
  51. */
  52.  
  53. #include <stdio.h>
  54.  
  55. /*  User-adjustable default values:                        */
  56.  
  57. #define OLDTABS        4        /*    Default old tab increment.            */
  58. #define NEWTABS        8        /*    Default new tab increment.            */
  59.  
  60. #define NORMAL        0        /*    Normal state.                    */
  61. #define QUOTE        1        /*    Quoted string skip state.            */
  62. #define BLANK        2        /*    Blank parsing state.                */
  63.  
  64. int main(argc,argv)
  65. int argc;
  66. char **argv;
  67. {
  68.     int del, i, j, atoi();
  69.     static int err = 0, oldtabs = OLDTABS, newtabs = NEWTABS;
  70.     static int blnks = 0, col = 0, lit = 0, state = NORMAL;
  71.  
  72.     while (--argc) {
  73.     if (**++argv == '-') {
  74.         switch (i = *(*argv + 1)) {
  75.         case 'n':
  76.         case 'N':   if ((i = atoi(*argv + 2)) < 1) {
  77.                 fprintf(stderr,
  78.                     "ERROR:  New tab increment < 1\n\n");
  79.                 err = !0;
  80.                 }
  81.                 else newtabs = i;
  82.                 break;
  83.  
  84.         case 'o':
  85.         case 'O':   if ((i = atoi(*argv + 2)) < 1) {
  86.                 fprintf(stderr,
  87.                     "ERROR:  Old tab increment < 1\n\n");
  88.                 err = !0;
  89.                 }
  90.                 else oldtabs = i;
  91.                 break;
  92.  
  93.         default:    fprintf(stderr,
  94.                 "ERROR:  Illegal option %s\n\n",*argv);
  95.                 err = !0;  break;
  96.         }
  97.     }
  98.     else {
  99.         fprintf(stderr,"ERROR:  Illegal parameter %s\n\n",*argv);
  100.         err = !0;
  101.     }
  102.     }
  103.     if (err) {
  104.     fprintf(stderr,"Usage:\tRETABF { -Onn } { -Nnn } oldfile newfile\n\n");
  105.     err = !0;
  106.     }
  107.     else {
  108.     while ((i = getchar()) != EOF) {
  109.         i &= 0177;
  110.         switch (state) {
  111.         case NORMAL:    switch (i) {
  112.                     case '\n':    col = 0;  putchar('\n');
  113.                         break;
  114.  
  115.                     case '\t':    blnks = oldtabs -
  116.                             col % oldtabs;
  117.                         state = BLANK;    break;
  118.  
  119.                     case ' ':    blnks = 1;  state = BLANK;
  120.                         break;
  121.  
  122.                     case '\'':    del = '\'';  goto quote1;
  123.  
  124.                     case '"':    del = '"';
  125. quote1:                        if (!lit) state = QUOTE;
  126.  
  127.                     default:    ++col;    putchar(i);  break;
  128.                 }
  129.                 break;
  130.  
  131.         case QUOTE:    switch (i) {
  132.                     case '\n':    if (!lit) state = NORMAL;
  133.                         col = 0;  break;
  134.  
  135.                     case '\t':    col += newtabs -
  136.                             col % newtabs;
  137.                         break;
  138.  
  139.                     case '\'':
  140.                     case '"':    if (!lit && i == del)
  141.                             state = NORMAL;
  142.  
  143.                     default:    ++col;    break;
  144.                 }
  145.                 putchar(i);  break;
  146.  
  147.         case BLANK:    switch (i) {
  148.                     case '\t':    blnks += oldtabs - (col +
  149.                             blnks) % oldtabs;
  150.                         break;
  151.  
  152.                     case ' ':    ++blnks;  break;
  153.  
  154.                     case '"':    del = '"';  goto quote2;
  155.  
  156.                     case '\'':    del = '\'';
  157. quote2:                        state = QUOTE;    goto entab;
  158.  
  159.                     default:    state = NORMAL;
  160. entab:                        if (blnks > 1 &&
  161.                             newtabs > 1)
  162.                             while ((j = newtabs -
  163.                             col % newtabs)
  164.                             <= blnks) {
  165.                             col += j;
  166.                             blnks -= j;
  167.                             putchar('\t');
  168.                             }
  169.                         for (; blnks; --blnks) {
  170.                             ++col;  putchar(' ');
  171.                         }
  172.                         if (i == '\n') col = 0;
  173.                         else ++col;
  174.                         putchar(i);  break;
  175.                 }
  176.                 break;
  177.         }
  178.         lit = !lit && i == '\\';
  179.     }
  180.     }
  181.     return err;
  182. }
  183.