cencode -> vis; rewrite
authorMarc Teitelbaum <marc@ucbvax.Berkeley.EDU>
Fri, 29 Jun 1990 15:41:43 +0000 (07:41 -0800)
committerMarc Teitelbaum <marc@ucbvax.Berkeley.EDU>
Fri, 29 Jun 1990 15:41:43 +0000 (07:41 -0800)
SCCS-vsn: usr.bin/vis/vis.c 1.2
SCCS-vsn: usr.bin/vis/vis.1 1.2

usr/src/usr.bin/vis/vis.1
usr/src/usr.bin/vis/vis.c

index 3e13378..1d39dbb 100644 (file)
 .\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 .\"
 .\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 .\"
-.\"    @(#)vis.1       1.1 (Berkeley) %G%
+.\"    @(#)vis.1       1.2 (Berkeley) %G%
 .\"
 .\"
-.TH <VIS> <1> ""
+.TH VIS 1 ""
 .UC 7
 .SH NAME
 vis \- display non-printable characters in a visual format
 .SH SYNOPSIS
 .B vis
 [
 .UC 7
 .SH NAME
 vis \- display non-printable characters in a visual format
 .SH SYNOPSIS
 .B vis
 [
-.B \-nwcgoifsh
+.B \-nwctsobfl
 ] [
 .B \-F
 foldwidth
 ] [
 .B \-F
 foldwidth
@@ -30,62 +30,50 @@ foldwidth
 .SH DESCRIPTION
 \fIVis\fP is a filter for converting non-printable characters
 into a visual representation.  It differs from cat -v in that
 .SH DESCRIPTION
 \fIVis\fP is a filter for converting non-printable characters
 into a visual representation.  It differs from cat -v in that
-the form is unique and invertible.  A detailed description of the
-various visual formats is given in cencode(3).
+the form is unique and invertible.  By default, all non-graphic
+characters except space, tab, and newline are encoded.
+A detailed description of the
+various visual formats is given in vis(3).
 .SH OPTIONS
 .SH OPTIONS
-The c, g, and o flags select a particular display format.  All
-three can be requested independent of each other, since two
-of them only operate on a subset of the non-printable characters.
 .IP -c
 Request a format which displays a small subset of the
 non-printable characters using C-style backslash sequences.
 .IP -c
 Request a format which displays a small subset of the
 non-printable characters using C-style backslash sequences.
-.IP -g
-Request a format which displays non-printable characters
-using a \\^C notation for control characters, and \\M notation
-for meta characters (chars with the 8th bit set).  All non-printables,
-except space and meta-space when using the \-w flag, can be encoded 
-using this format.
 .IP -o
 Request a format which displays non-printable characters as
 .IP -o
 Request a format which displays non-printable characters as
-an octal number, \\ddd.  All non-printable characters can
-be displayed using this format.
+an octal number, \\ddd.
+.IP -t
+Tabs are also encoded.
 .IP -w
 .IP -w
-Normally
-.B vis
-considers white space (space-tab-newline) as printable.  The -w
-flag causes white space to be encoded in a visual form.
-.IP -i
-Causes
-.B vis
-to invert visually encoded data back to its original form.  When
-using -i, it is unnecessary to select an encoding format, since
-all formats are unique and invertible.
+White space (space-tab-newline) is also encoded.
+.IP -s
+Only characters considered unsafe to send to a terminal are encoded.
+This flag allows backspace, bell, and carriage return in addition
+to the default space, tab and newline.
 .IP -f
 and
 .IP -F
 Causes
 .B vis
 to fold output lines to foldwidth columns (default 80), like fold(1), except
 .IP -f
 and
 .IP -F
 Causes
 .B vis
 to fold output lines to foldwidth columns (default 80), like fold(1), except
-that a hidden newline sequence is used, which is removed
-when inverting the file back to its original form with -i.
+that a hidden newline sequence is used, (which is removed
+when inverting the file back to its original form with unvis(1)).
 If the last character in the encoded file does not end in a newline,
 If the last character in the encoded file does not end in a newline,
-.B vis
-automatically appends a hidden newline to the end.  This makes
-various editors and other utilities happy.
-.IP -s
-Causes vis to strip the 8th bit on all characters.  This obviously
-produces non-invertible output.
+a hidden newline sequence is appended to the output.
+This makes
+the output usuable with various editors and other utilities which
+typically don't work with partial lines.
 .IP -n
 .IP -n
-Normally, if none of -c, -g, or -o are selected, vis turns on
-all three flags.  The -n flag forces vis to disable any encoding,
-except for backslash stuffing and line length folding.
-.IP -h
-When using -i to invert data, this causes
-.B vis
-to treat the hat (^) as a special character all the time (normally, all
-visual sequences are preceded by a backslash).  This option
-is provided for compatibility with the common convention that ^C
-introduces a control character sequence; however, the new invertible
-sequence, \\^C, is recommended as the preferred representation.
+Turns off any encoding, except for the fact that backslashes are
+still doubled and hidden newline sequences inserted if -f or -F
+is selected.  When combined with the -f flag, vis becomes like
+an invertible version of the fold(1) utility.  That is, the output
+can be unfolded by running the output through unvis(1).
+.IP -b
+Turns off prepending of backslash before up-arrow control sequences
+and Meta characters, and disables the doubling of backslashes.  This
+produces output which is neither invertible or precise, but does
+represent a minimum of change to the input.  It is similar to cat -v.
+.IP -l
+Mark newlines with the visable sequence '\\$', followed by the newline.
 .SH "SEE ALSO
 .SH "SEE ALSO
-cencode(3)
+unvis(1) vis(3)
index 401eb77..c72c3a7 100644 (file)
@@ -22,13 +22,13 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)vis.c      1.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)vis.c      1.2 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <stdio.h>
 #endif /* not lint */
 
 #include <stdio.h>
-#include <cencode.h>
+#include <vis.h>
 
 
-int eflags, dflags, invert, strip, fold, foldwidth=80, none;
+int eflags, fold, foldwidth=80, none, markeol, debug;
 
 main(argc, argv) 
        char *argv[];
 
 main(argc, argv) 
        char *argv[];
@@ -39,59 +39,56 @@ main(argc, argv)
        FILE *fp;
        int ch;
 
        FILE *fp;
        int ch;
 
-       while ((ch = getopt(argc, argv, "nwcgovishfF:")) != EOF)
+       while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
                switch((char)ch) {
                case 'n':
                        none++;
                        break;
                case 'w':
                switch((char)ch) {
                case 'n':
                        none++;
                        break;
                case 'w':
-                       eflags |= CENC_WHITE;
+                       eflags |= VIS_WHITE;
                        break;
                case 'c':
                        break;
                case 'c':
-                       eflags |= CENC_CSTYLE;
+                       eflags |= VIS_CSTYLE;
                        break;
                        break;
-               case 'g':
-                       eflags |= CENC_GRAPH;
+               case 't':
+                       eflags |= VIS_TAB;
                        break;
                        break;
-               case 'o':
-                       eflags |= CENC_OCTAL;
+               case 's':
+                       eflags |= VIS_SAFE;
                        break;
                        break;
-               case 'v':       /* vis -v considered harmful */
-                       eflags |= CENC_GRAPH | CENC_OCTAL;
+               case 'o':
+                       eflags |= VIS_OCTAL;
                        break;
                        break;
-               case 'i':
-                       invert++;
+               case 'b':
+                       eflags |= VIS_NOSLASH;
                        break;
                case 'F':
                        if ((foldwidth = atoi(optarg))<5) {
                                fprintf(stderr, 
                        break;
                case 'F':
                        if ((foldwidth = atoi(optarg))<5) {
                                fprintf(stderr, 
-                                "vis: can't fold lines to less than 2 cols\n");
+                                "vis: can't fold lines to less than 5 cols\n");
                                exit(1);
                        }
                        /*FALLTHROUGH*/
                case 'f':
                        fold++;         /* fold output lines to 80 cols */
                        break;          /* using hidden newline */
                                exit(1);
                        }
                        /*FALLTHROUGH*/
                case 'f':
                        fold++;         /* fold output lines to 80 cols */
                        break;          /* using hidden newline */
-               case 's':
-                       strip++;
+               case 'l':
+                       markeol++;      /* mark end of line with \$ */
                        break;
                        break;
-               case 'h':
-                       dflags |= CDEC_HAT;
+#ifdef DEBUG
+               case 'd':
+                       debug++;
                        break;
                        break;
+#endif
                case '?':
                default:
                        fprintf(stderr, 
                case '?':
                default:
                        fprintf(stderr, 
-               "usage: vis [-nwcgovifsh] [-F foldwidth]\n");
+               "usage: vis [-nwctsobf] [-F foldwidth]\n");
                        exit(1);
                }
        argc -= optind;
        argv += optind;
 
                        exit(1);
                }
        argc -= optind;
        argv += optind;
 
-#define ALL    (CENC_CSTYLE | CENC_GRAPH | CENC_OCTAL)
-       if (none)
-               eflags &= ~ALL;
-       else if (!(eflags&ALL))
-               eflags |= ALL;
        if (*argv)
                while (*argv) {
                        if ((fp=fopen(*argv, "r")) != NULL)
        if (*argv)
                while (*argv) {
                        if ((fp=fopen(*argv, "r")) != NULL)
@@ -111,85 +108,50 @@ process(fp, filename)
        char *filename;
 {
        static int col = 0;
        char *filename;
 {
        static int col = 0;
-       register char *cp = "X"+1;      /* so *(cp-1) starts out != '\n' */
-       register int byte = 0;
+       register char *cp = "\0"+1;     /* so *(cp-1) starts out != '\n' */
        register int c, rachar; 
        register char nc;
        register int c, rachar; 
        register char nc;
+       char buff[5];
        
        
-       /*
-        * Encode
-        */
-       if (!invert) {
-               c = getc(fp);
-               while (c != EOF) {
-                       rachar = getc(fp);
-                       if (strip)
-                               c &= 0177;
-                       cp = cencode((char)c, eflags|CENC_RACHAR, (char)rachar);
-                       if (fold) {
-                               /*
-                                * Keep track of printables and
-                                * space chars (like fold(1)).
-                                */
-                               for (;;) {
-                                       switch(*cp) {
-                                       case '\n':
-                                       case '\r':
-                                               col = 0;
-                                               break;
-                                       case '\t':
-                                               col = col + 8 &~ 07;
-                                               break;
-                                       case '\b':
-                                               col = col ? col - 1 : 0;
-                                               break;
-                                       default:
-                                               col += strlen(cp);
-                                       }
-                                       if (col > (foldwidth-2)) {
-                                               printf("\\\n");
-                                               col = 0;
-                                       } else
-                                               break;
-                               }
-                       }
-                       do {
-                               putchar(*cp++);
-                       } while (*cp);
-                       c = rachar;
+       c = getc(fp);
+       while (c != EOF) {
+               rachar = getc(fp);
+               if (none) {
+                       cp = buff;
+                       *cp++ = c;
+                       if (c == '\\')
+                               *cp++ = '\\';
+                       *cp = '\0';
+               } else if (markeol && c == '\n') {
+                       cp = buff;
+                       if ((eflags & VIS_NOSLASH) == 0)
+                               *cp++ = '\\';
+                       *cp++ = '$';
+                       *cp++ = '\n';
+                       *cp = '\0';
+               } else 
+                       (void) vis(buff, (char)c, eflags, (char)rachar);
+
+               cp = buff;
+               if (fold) {
+#ifdef DEBUG
+                       if (debug)
+                               printf("<%02d,", col);
+#endif
+                       col = foldit(cp, col, foldwidth);
+#ifdef DEBUG
+                       if (debug)
+                               printf("%02d>", col);
+#endif
                }
                }
-               if (fold && *(cp-1) != '\n')
-                       printf("\\\n");
+               do {
+                       putchar(*cp);
+               } while (*++cp);
+               c = rachar;
+       }
        /*
        /*
-        * Decode
+        * terminate partial line with a hidden newline
         */
         */
-       } else {
-               while ((c = getc(fp)) != EOF) {
-                       byte++;
-               again:
-                       switch(cdecode((char)c, &nc, dflags)) {
-                       case CDEC_NEEDMORE:
-                       case CDEC_NOCHAR:
-                               break;
-                       case CDEC_OK:
-                               putchar(nc);
-                               break;
-                       case CDEC_OKPUSH:
-                               putchar(nc);
-                               goto again;
-                       case CDEC_SYNBAD:
-                               fprintf(stderr, 
-                                   "vis: %s: offset: %d: can't decode\n", 
-                                   filename, byte);
-                               break;
-                       default:
-                               fprintf(stderr,
-                                   "vis: bad return value (can't happen)\n");
-                               exit(1);
-                       }
-               }
-               if (cdecode((char)0, &nc, CDEC_END) == CDEC_OK)
-                       putchar(nc);
-       }
-       exit(0);
+       if (fold && *(cp-1) != '\n')
+               printf("\\\n");
 }
 }