home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / cawf407.zip / src / bsfilt.c < prev    next >
C/C++ Source or Header  |  1993-12-28  |  4KB  |  216 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.  
  33. #ifdef    STDLIB
  34. #include <stdlib.h>
  35. #endif
  36.  
  37. #ifdef UNIX
  38. # ifdef USG
  39. #include <string.h>
  40. # else    /* not USG */
  41. #include <strings.h>
  42. # endif    /* USG */
  43. #else    /* not UNIX */
  44. #include <string.h>
  45. #endif    /* UNIX */
  46.  
  47. #include <sys/types.h>
  48.  
  49. #include "ansi.h"
  50.  
  51. #define MAXLL    2048            /* ridiculous maximum line length */
  52.  
  53. int Dash = 1;                /* underline with dashes */
  54. int Dp = 0;                /* dash pending */
  55. int Lc = 0;                /* line count */
  56. char *Pname;                /* program name */
  57. unsigned char Ulb[MAXLL];        /* underline buffer */
  58. int Ulx = 0;                /* underline buffer index */
  59.  
  60. _PROTOTYPE(void Putchar,(int ch));
  61.  
  62. main(argc, argv)
  63.     int argc;
  64.     char *argv[];
  65. {
  66.     int ax = 1;            /* argument index */
  67.     unsigned char c;        /* character buffer */
  68.     FILE *fs;            /* file stream */
  69.     int nf = 0;            /* number of files processed */
  70.     unsigned char pc;        /* previous character */
  71.     int under = 0;                  /* underline */
  72. /*
  73.  * Save program name.
  74.  */
  75.     if ((Pname = strrchr(argv[0], '/')) != NULL)
  76.         Pname++;
  77.     else if ((Pname = strrchr(argv[0], '\\')) != NULL)
  78.         Pname++;
  79.     else
  80.         Pname = argv[0];
  81. /*
  82.  * Process options.
  83.  */
  84.     if (argc > 1 && argv[1][0] == '-') {
  85.         switch (argv[1][1]) {
  86.     /*
  87.      * "-U" - underline with dashes.
  88.      */
  89.         case 'U':
  90.             Dash = 0;
  91.             under = 1;
  92.             break;
  93.     /*
  94.      * "-" - do no  underlining at all.
  95.      */
  96.         case '\0':
  97.             Dash = under = 0;
  98.             break;
  99.         default:
  100.             (void) fprintf(stderr,
  101.                 "%s usage: [-] [-U] [file]\n", Pname);
  102.             exit(1);
  103.         }
  104.         ax++;
  105.     }
  106. /*
  107.  * Process files.  Read standard input if no files names.
  108.  */
  109.  
  110.     while (ax < argc || nf == 0) {
  111.         if (ax >= argc)
  112.             fs = stdin;
  113.         else {
  114. #ifdef    UNIX
  115.             if ((fs = fopen(argv[ax], "r")) == NULL)
  116. #else
  117.             if ((fs = fopen(argv[ax], "rt")) == NULL)
  118. #endif
  119.             {
  120.                 (void) fprintf(stderr, "%s: can't open %s\n",
  121.                     Pname, argv[ax]);
  122.                 exit(1);
  123.             }
  124.             ax++;
  125.         }
  126.         nf++;
  127.     /*
  128.      * Read input a character at a time.
  129.      */
  130.         for (pc = '\0';;) {
  131.             c = (unsigned char)fgetc(fs);
  132.             if (feof(fs))
  133.                 break;
  134.             switch(c) {
  135.  
  136.             case '\n':
  137.                 if (pc)
  138.                     Putchar((int)pc);
  139.                 Putchar('\n');
  140.                 pc = '\0';
  141.                 break;
  142.  
  143.             case '\b':
  144.                 if (pc == '_') {
  145.                     if (under) {
  146.                         putchar(pc);
  147.                         putchar('\b');
  148.                     } else if (Dash)
  149.                         Dp = 1;
  150.                 }
  151.                 pc = '\0';
  152.                 break;
  153.  
  154.             default:
  155.                 if (pc)
  156.                     Putchar((int)pc);
  157.                 pc = c;
  158.             }
  159.         }
  160.         if (pc) {
  161.             Putchar((int)pc);
  162.             Putchar((int)'\n');
  163.         }
  164.     }
  165.     return(0);
  166. }
  167.  
  168.  
  169. /*
  170.  * Putchar(ch) - put a character with possible underlining
  171.  */
  172.  
  173. void
  174. Putchar(ch)
  175.     int ch;
  176. {
  177.     int i;                    /* temporary index */
  178.  
  179.     if ((unsigned char)ch == '\n') {
  180. /*
  181.  * Handle end of line.
  182.  */
  183.         putchar('\n');
  184.         if (Ulx) {
  185.             while (Ulx && Ulb[Ulx-1] == ' ')
  186.                 Ulx--;
  187.             if (Ulx) {
  188.                 for (i = 0; i < Ulx; i++)
  189.                     putchar(Ulb[i]);
  190.                 putchar('\n');
  191.             }
  192.         }
  193.         Dp = Ulx = 0;
  194.         Lc++;
  195.         return;
  196.     }
  197. /*
  198.  * Put "normal" character.
  199.  */
  200.     putchar((unsigned char)ch);
  201.     if (Dash) {
  202.  
  203.     /*
  204.      * Handle dash-type underlining.
  205.      */
  206.         if (Ulx >= MAXLL) {
  207.             (void) fprintf(stderr,
  208.                 "%s: underline for line %d > %d characters\n",
  209.                 Pname, Lc, MAXLL);
  210.             exit(1);
  211.         }
  212.         Ulb[Ulx++] = Dp ? '-' : ' ';
  213.         Dp = 0;
  214.     }
  215. }
  216.