home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / colcrt / colcrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-17  |  5.5 KB  |  252 lines

  1. /*
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)colcrt.c    5.4 (Berkeley) 6/1/90";
  42. #endif /* not lint */
  43.  
  44. #include <stdio.h>
  45. /*
  46.  * colcrt - replaces col for crts with new nroff esp. when using tbl.
  47.  * Bill Joy UCB July 14, 1977
  48.  *
  49.  * This filter uses a screen buffer, 267 half-lines by 132 columns.
  50.  * It interprets the up and down sequences generated by the new
  51.  * nroff when used with tbl and by \u \d and \r.
  52.  * General overstriking doesn't work correctly.
  53.  * Underlining is split onto multiple lines, etc.
  54.  *
  55.  * Option - suppresses all underlining.
  56.  * Option -2 forces printing of all half lines.
  57.  */
  58.  
  59. char    page[267][132];
  60.  
  61. int    outline = 1;
  62. int    outcol;
  63.  
  64. char    suppresul;
  65. char    printall;
  66.  
  67. char    *progname;
  68. FILE    *f;
  69.  
  70. main(argc, argv)
  71.     int argc;
  72.     char *argv[];
  73. {
  74.     register c;
  75.     register char *cp, *dp;
  76.  
  77.     argc--;
  78.     progname = *argv++;
  79.     while (argc > 0 && argv[0][0] == '-') {
  80.         switch (argv[0][1]) {
  81.             case 0:
  82.                 suppresul = 1;
  83.                 break;
  84.             case '2':
  85.                 printall = 1;
  86.                 break;
  87.             default:
  88.                 printf("usage: %s [ - ] [ -2 ] [ file ... ]\n", progname);
  89.                 fflush(stdout);
  90.                 exit(1);
  91.         }
  92.         argc--;
  93.         argv++;
  94.     }
  95.     do {
  96.         if (argc > 0) {
  97.             close(0);
  98.             if (!(f = fopen(argv[0], "r"))) {
  99.                 fflush(stdout);
  100.                 perror(argv[0]);
  101.                 exit (1);
  102.             }
  103.             argc--;
  104.             argv++;
  105.         }
  106.         for (;;) {
  107.             c = getc(stdin);
  108.             if (c == -1) {
  109.                 pflush(outline);
  110.                 fflush(stdout);
  111.                 break;
  112.             }
  113.             switch (c) {
  114.                 case '\n':
  115.                     if (outline >= 265)
  116.                         pflush(62);
  117.                     outline += 2;
  118.                     outcol = 0;
  119.                     continue;
  120.                 case '\016':
  121.                     case '\017':
  122.                     continue;
  123.                 case 033:
  124.                     c = getc(stdin);
  125.                     switch (c) {
  126.                         case '9':
  127.                             if (outline >= 266)
  128.                                 pflush(62);
  129.                             outline++;
  130.                             continue;
  131.                         case '8':
  132.                             if (outline >= 1)
  133.                                 outline--;
  134.                             continue;
  135.                         case '7':
  136.                             outline -= 2;
  137.                             if (outline < 0)
  138.                                 outline = 0;
  139.                             continue;
  140.                         default:
  141.                             continue;
  142.                     }
  143.                 case '\b':
  144.                     if (outcol)
  145.                         outcol--;
  146.                     continue;
  147.                 case '\t':
  148.                     outcol += 8;
  149.                     outcol &= ~7;
  150.                     outcol--;
  151.                     c = ' ';
  152.                 default:
  153.                     if (outcol >= 132) {
  154.                         outcol++;
  155.                         continue;
  156.                     }
  157.                     cp = &page[outline][outcol];
  158.                     outcol++;
  159.                     if (c == '_') {
  160.                         if (suppresul)
  161.                             continue;
  162.                         cp += 132;
  163.                         c = '-';
  164.                     }
  165.                     if (*cp == 0) {
  166.                         *cp = c;
  167.                         dp = cp - outcol;
  168.                         for (cp--; cp >= dp && *cp == 0; cp--)
  169.                             *cp = ' ';
  170.                     } else
  171.                         if (plus(c, *cp) || plus(*cp, c))
  172.                             *cp = '+';
  173.                         else if (*cp == ' ' || *cp == 0)
  174.                             *cp = c;
  175.                     continue;
  176.             }
  177.         }
  178.     } while (argc > 0);
  179.     fflush(stdout);
  180.     exit(0);
  181. }
  182.  
  183. plus(c, d)
  184.     char c, d;
  185. {
  186.  
  187.     return (c == '|' && d == '-' || d == '_');
  188. }
  189.  
  190. int first;
  191.  
  192. pflush(ol)
  193.     int ol;
  194. {
  195.     register int i, j;
  196.     register char *cp;
  197.     char lastomit;
  198.     int l;
  199.  
  200.     l = ol;
  201.     lastomit = 0;
  202.     if (l > 266)
  203.         l = 266;
  204.     else
  205.         l |= 1;
  206.     for (i = first | 1; i < l; i++) {
  207.         move(i, i - 1);
  208.         move(i, i + 1);
  209.     }
  210.     for (i = first; i < l; i++) {
  211.         cp = page[i];
  212.         if (printall == 0 && lastomit == 0 && *cp == 0) {
  213.             lastomit = 1;
  214.             continue;
  215.         }
  216.         lastomit = 0;
  217.         printf("%s\n", cp);
  218.     }
  219.     bcopy(page[ol], page, (267 - ol) * 132);
  220.     bzero(page[267- ol], ol * 132);
  221.     outline -= ol;
  222.     outcol = 0;
  223.     first = 1;
  224. }
  225.  
  226. move(l, m)
  227.     int l, m;
  228. {
  229.     register char *cp, *dp;
  230.  
  231.     for (cp = page[l], dp = page[m]; *cp; cp++, dp++) {
  232.         switch (*cp) {
  233.             case '|':
  234.                 if (*dp != ' ' && *dp != '|' && *dp != 0)
  235.                     return;
  236.                 break;
  237.             case ' ':
  238.                 break;
  239.             default:
  240.                 return;
  241.         }
  242.     }
  243.     if (*cp == 0) {
  244.         for (cp = page[l], dp = page[m]; *cp; cp++, dp++)
  245.             if (*cp == '|')
  246.                 *dp = '|';
  247.             else if (*dp == 0)
  248.                 *dp = ' ';
  249.         page[l][0] = 0;
  250.     }
  251. }
  252.