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

  1. /*-
  2.  * Copyright (c) 1989 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) 1989 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[] = "@(#)vis.c    5.1 (Berkeley) 4/18/91";
  42. #endif /* not lint */
  43.  
  44. #include <stdio.h>
  45. #include <vis.h>
  46.  
  47. int eflags, fold, foldwidth=80, none, markeol, debug;
  48.  
  49. main(argc, argv) 
  50.     char *argv[];
  51. {
  52.     extern char *optarg;
  53.     extern int optind;
  54.     extern int errno;
  55.     FILE *fp;
  56.     int ch;
  57.  
  58.     while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
  59.         switch((char)ch) {
  60.         case 'n':
  61.             none++;
  62.             break;
  63.         case 'w':
  64.             eflags |= VIS_WHITE;
  65.             break;
  66.         case 'c':
  67.             eflags |= VIS_CSTYLE;
  68.             break;
  69.         case 't':
  70.             eflags |= VIS_TAB;
  71.             break;
  72.         case 's':
  73.             eflags |= VIS_SAFE;
  74.             break;
  75.         case 'o':
  76.             eflags |= VIS_OCTAL;
  77.             break;
  78.         case 'b':
  79.             eflags |= VIS_NOSLASH;
  80.             break;
  81.         case 'F':
  82.             if ((foldwidth = atoi(optarg))<5) {
  83.                 fprintf(stderr, 
  84.                  "vis: can't fold lines to less than 5 cols\n");
  85.                 exit(1);
  86.             }
  87.             /*FALLTHROUGH*/
  88.         case 'f':
  89.             fold++;        /* fold output lines to 80 cols */
  90.             break;        /* using hidden newline */
  91.         case 'l':
  92.             markeol++;    /* mark end of line with \$ */
  93.             break;
  94. #ifdef DEBUG
  95.         case 'd':
  96.             debug++;
  97.             break;
  98. #endif
  99.         case '?':
  100.         default:
  101.             fprintf(stderr, 
  102.         "usage: vis [-nwctsobf] [-F foldwidth]\n");
  103.             exit(1);
  104.         }
  105.     argc -= optind;
  106.     argv += optind;
  107.  
  108.     if (*argv)
  109.         while (*argv) {
  110.             if ((fp=fopen(*argv, "r")) != NULL)
  111.                 process(fp, *argv);
  112.             else
  113.                 fprintf(stderr, "vis: %s: %s\n", *argv,
  114.                     (char *)strerror(errno));
  115.             argv++;
  116.         }
  117.     else
  118.         process(stdin, "<stdin>");
  119.     exit(0);
  120. }
  121.     
  122. process(fp, filename)
  123.     FILE *fp;
  124.     char *filename;
  125. {
  126.     static int col = 0;
  127.     register char *cp = "\0"+1;    /* so *(cp-1) starts out != '\n' */
  128.     register int c, rachar; 
  129.     register char nc;
  130.     char buff[5];
  131.     
  132.     c = getc(fp);
  133.     while (c != EOF) {
  134.         rachar = getc(fp);
  135.         if (none) {
  136.             cp = buff;
  137.             *cp++ = c;
  138.             if (c == '\\')
  139.                 *cp++ = '\\';
  140.             *cp = '\0';
  141.         } else if (markeol && c == '\n') {
  142.             cp = buff;
  143.             if ((eflags & VIS_NOSLASH) == 0)
  144.                 *cp++ = '\\';
  145.             *cp++ = '$';
  146.             *cp++ = '\n';
  147.             *cp = '\0';
  148.         } else 
  149.             (void) vis(buff, (char)c, eflags, (char)rachar);
  150.  
  151.         cp = buff;
  152.         if (fold) {
  153. #ifdef DEBUG
  154.             if (debug)
  155.                 printf("<%02d,", col);
  156. #endif
  157.             col = foldit(cp, col, foldwidth);
  158. #ifdef DEBUG
  159.             if (debug)
  160.                 printf("%02d>", col);
  161. #endif
  162.         }
  163.         do {
  164.             putchar(*cp);
  165.         } while (*++cp);
  166.         c = rachar;
  167.     }
  168.     /*
  169.      * terminate partial line with a hidden newline
  170.      */
  171.     if (fold && *(cp-1) != '\n')
  172.         printf("\\\n");
  173. }
  174.