home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / fileutil / cawf / bsfilt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  4.0 KB  |  203 lines

  1. /*
  2.  *    bsfilt.c - a colcrt-like processor for cawf(1)
  3.  */
  4.  
  5. /*
  6.  *    Copyright (c) 1991 Purdue University Research Foundation,
  7.  *    West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *    Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *    University Computing Center.  Not derived from licensed software;
  11.  *    derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *    Permission is granted to anyone to use this software for any
  14.  *    purpose on any computer system, and to alter it and redistribute
  15.  *    it freely, subject to the following restrictions:
  16.  *
  17.  *    1. The author is not responsible for any consequences of use of
  18.  *       this software, even if they arise from flaws in it.
  19.  *
  20.  *    2. The origin of this software must not be misrepresented, either
  21.  *       by explicit claim or by omission.  Credits must appear in the
  22.  *       documentation.
  23.  *
  24.  *    3. Altered versions must be plainly marked as such, and must not
  25.  *       be misrepresented as being the original software.  Credits must
  26.  *       appear in the documentation.
  27.  *
  28.  *    4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #ifdef UNIX
  33. #include <strings.h>
  34. #else
  35. #include <string.h>
  36. #endif
  37.  
  38. #define MAXLL    2048            /* ridiculous maximum line length */
  39.  
  40. int Dash = 1;                /* underline with dashes */
  41. int Dp = 0;                /* dash pending */
  42. int Lc = 0;                /* line count */
  43. char *Pname;                /* program name */
  44. char Ulb[MAXLL];            /* underline buffer */
  45. int Ulx = 0;                /* underline buffer index */
  46.  
  47. void Putchar();
  48. #ifndef    STDDEF
  49. char *strrchr();
  50. #endif
  51.  
  52. main(argc, argv)
  53.     int argc;
  54.     char *argv[];
  55. {
  56.     int ax = 1;            /* argument index */
  57.     char c;                /* character buffer */
  58.     FILE *fs;            /* file stream */
  59.     int nf = 0;            /* number of files processed */
  60.     char pc;            /* previous character */
  61.     int under = 0;                  /* underline */
  62. /*
  63.  * Save program name.
  64.  */
  65.     if ((Pname = strrchr(argv[0], '/')) != NULL)
  66.         Pname++;
  67.     else if ((Pname = strrchr(argv[0], '\\')) != NULL)
  68.         Pname++;
  69.     else
  70.         Pname = argv[0];
  71. /*
  72.  * Process options.
  73.  */
  74.     if (argc > 1 && argv[1][0] == '-') {
  75.         switch (argv[1][1]) {
  76.     /*
  77.      * "-U" - underline with dashes.
  78.      */
  79.         case 'U':
  80.             Dash = 0;
  81.             under = 1;
  82.             break;
  83.     /*
  84.      * "-" - do no  underlining at all.
  85.      */
  86.         case '\0':
  87.             Dash = under = 0;
  88.             break;
  89.         default:
  90.             (void) fprintf(stderr,
  91.                 "%s usage: [-] [-U] [file]\n", Pname);
  92.             exit(1);
  93.         }
  94.         ax++;
  95.     }
  96. /*
  97.  * Process files.  Read standard input if no files names.
  98.  */
  99.  
  100.     while (ax < argc || nf == 0) {
  101.         if (ax >= argc)
  102.             fs = stdin;
  103.         else {
  104. #ifdef    UNIX
  105.             if ((fs = fopen(argv[ax], "r")) == NULL)
  106. #else
  107.             if ((fs = fopen(argv[ax], "rt")) == NULL)
  108. #endif
  109.             {
  110.                 (void) fprintf(stderr, "%s: can't open %s\n",
  111.                     Pname, argv[ax]);
  112.                 exit(1);
  113.             }
  114.             ax++;
  115.         }
  116.         nf++;
  117.     /*
  118.      * Read input a character at a time.
  119.      */
  120.         for (pc = '\0'; (c = fgetc(fs)) != EOF;) {
  121.             switch(c) {
  122.  
  123.             case '\n':
  124.                 if (pc)
  125.                     Putchar(pc);
  126.                 Putchar('\n');
  127.                 pc = '\0';
  128.                 break;
  129.  
  130.             case '\b':
  131.                 if (pc == '_') {
  132.                     if (under) {
  133.                         putchar(pc);
  134.                         putchar('\b');
  135.                     } else if (Dash)
  136.                         Dp = 1;
  137.                 }
  138.                 pc = '\0';
  139.                 break;
  140.  
  141.             default:
  142.                 if (pc)
  143.                     Putchar(pc);
  144.                 pc = c;
  145.             }
  146.         }
  147.         if (pc) {
  148.             Putchar(pc);
  149.             Putchar('\n');
  150.         }
  151.     }
  152.     exit(0);
  153. }
  154.  
  155.  
  156. /*
  157.  * Putchar(ch) - put a character with possible underlining
  158.  */
  159.  
  160. void
  161. Putchar(ch)
  162.     char ch;
  163. {
  164.     int i;                    /* temporary index */
  165.  
  166.     if (ch == '\n') {
  167. /*
  168.  * Handle end of line.
  169.  */
  170.         putchar('\n');
  171.         if (Ulx) {
  172.             while (Ulx && Ulb[Ulx-1] == ' ')
  173.                 Ulx--;
  174.             if (Ulx) {
  175.                 for (i = 0; i < Ulx; i++)
  176.                     putchar(Ulb[i]);
  177.                 putchar('\n');
  178.             }
  179.         }
  180.         Dp = Ulx = 0;
  181.         Lc++;
  182.         return;
  183.     }
  184. /*
  185.  * Put "normal" character.
  186.  */
  187.     putchar(ch);
  188.     if (Dash) {
  189.  
  190.     /*
  191.      * Handle dash-type underlining.
  192.      */
  193.         if (Ulx >= MAXLL) {
  194.             (void) fprintf(stderr,
  195.                 "%s: underline for line %d > %d characters\n",
  196.                 Pname, Lc, MAXLL);
  197.             exit(1);
  198.         }
  199.         Ulb[Ulx++] = Dp ? '-' : ' ';
  200.         Dp = 0;
  201.     }
  202. }
  203.