BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / vis / vis.c
CommitLineData
07c06a90
MT
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 are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
21 All rights reserved.\n";
22#endif /* not lint */
23
24#ifndef lint
1c15e888 25static char sccsid[] = "@(#)vis.c 1.2 (Berkeley) 6/29/90";
07c06a90
MT
26#endif /* not lint */
27
28#include <stdio.h>
0374ba61 29#include <vis.h>
07c06a90 30
0374ba61 31int eflags, fold, foldwidth=80, none, markeol, debug;
07c06a90
MT
32
33main(argc, argv)
34 char *argv[];
35{
36 extern char *optarg;
37 extern int optind;
38 extern int errno;
39 FILE *fp;
40 int ch;
41
0374ba61 42 while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
07c06a90
MT
43 switch((char)ch) {
44 case 'n':
45 none++;
46 break;
47 case 'w':
0374ba61 48 eflags |= VIS_WHITE;
07c06a90
MT
49 break;
50 case 'c':
0374ba61 51 eflags |= VIS_CSTYLE;
07c06a90 52 break;
0374ba61
MT
53 case 't':
54 eflags |= VIS_TAB;
07c06a90 55 break;
0374ba61
MT
56 case 's':
57 eflags |= VIS_SAFE;
07c06a90 58 break;
0374ba61
MT
59 case 'o':
60 eflags |= VIS_OCTAL;
07c06a90 61 break;
0374ba61
MT
62 case 'b':
63 eflags |= VIS_NOSLASH;
07c06a90
MT
64 break;
65 case 'F':
66 if ((foldwidth = atoi(optarg))<5) {
67 fprintf(stderr,
0374ba61 68 "vis: can't fold lines to less than 5 cols\n");
07c06a90
MT
69 exit(1);
70 }
71 /*FALLTHROUGH*/
72 case 'f':
73 fold++; /* fold output lines to 80 cols */
74 break; /* using hidden newline */
0374ba61
MT
75 case 'l':
76 markeol++; /* mark end of line with \$ */
07c06a90 77 break;
0374ba61
MT
78#ifdef DEBUG
79 case 'd':
80 debug++;
07c06a90 81 break;
0374ba61 82#endif
07c06a90
MT
83 case '?':
84 default:
85 fprintf(stderr,
0374ba61 86 "usage: vis [-nwctsobf] [-F foldwidth]\n");
07c06a90
MT
87 exit(1);
88 }
89 argc -= optind;
90 argv += optind;
91
07c06a90
MT
92 if (*argv)
93 while (*argv) {
94 if ((fp=fopen(*argv, "r")) != NULL)
95 process(fp, *argv);
96 else
97 fprintf(stderr, "vis: %s: %s\n", *argv,
98 (char *)strerror(errno));
99 argv++;
100 }
101 else
102 process(stdin, "<stdin>");
103 exit(0);
104}
105
106process(fp, filename)
107 FILE *fp;
108 char *filename;
109{
110 static int col = 0;
0374ba61 111 register char *cp = "\0"+1; /* so *(cp-1) starts out != '\n' */
07c06a90
MT
112 register int c, rachar;
113 register char nc;
0374ba61 114 char buff[5];
07c06a90 115
0374ba61
MT
116 c = getc(fp);
117 while (c != EOF) {
118 rachar = getc(fp);
119 if (none) {
120 cp = buff;
121 *cp++ = c;
122 if (c == '\\')
123 *cp++ = '\\';
124 *cp = '\0';
125 } else if (markeol && c == '\n') {
126 cp = buff;
127 if ((eflags & VIS_NOSLASH) == 0)
128 *cp++ = '\\';
129 *cp++ = '$';
130 *cp++ = '\n';
131 *cp = '\0';
132 } else
133 (void) vis(buff, (char)c, eflags, (char)rachar);
134
135 cp = buff;
136 if (fold) {
137#ifdef DEBUG
138 if (debug)
139 printf("<%02d,", col);
140#endif
141 col = foldit(cp, col, foldwidth);
142#ifdef DEBUG
143 if (debug)
144 printf("%02d>", col);
145#endif
07c06a90 146 }
0374ba61
MT
147 do {
148 putchar(*cp);
149 } while (*++cp);
150 c = rachar;
151 }
07c06a90 152 /*
0374ba61 153 * terminate partial line with a hidden newline
07c06a90 154 */
0374ba61
MT
155 if (fold && *(cp-1) != '\n')
156 printf("\\\n");
07c06a90 157}