148326Sbostic /*-
2*62427Sbostic * Copyright (c) 1989, 1993
3*62427Sbostic * The Regents of the University of California. All rights reserved.
441959Smarc *
548326Sbostic * %sccs.include.redist.c%
641959Smarc */
741959Smarc
841959Smarc #ifndef lint
9*62427Sbostic static char copyright[] =
10*62427Sbostic "@(#) Copyright (c) 1989, 1993\n\
11*62427Sbostic The Regents of the University of California. All rights reserved.\n";
1241959Smarc #endif /* not lint */
1341959Smarc
1441959Smarc #ifndef lint
15*62427Sbostic static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 06/06/93";
1641959Smarc #endif /* not lint */
1741959Smarc
1841959Smarc #include <stdio.h>
1944588Smarc #include <vis.h>
2041959Smarc
2144588Smarc int eflags, fold, foldwidth=80, none, markeol, debug;
2241959Smarc
main(argc,argv)2341959Smarc main(argc, argv)
2441959Smarc char *argv[];
2541959Smarc {
2641959Smarc extern char *optarg;
2741959Smarc extern int optind;
2841959Smarc extern int errno;
2941959Smarc FILE *fp;
3041959Smarc int ch;
3141959Smarc
3244588Smarc while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
3341959Smarc switch((char)ch) {
3441959Smarc case 'n':
3541959Smarc none++;
3641959Smarc break;
3741959Smarc case 'w':
3844588Smarc eflags |= VIS_WHITE;
3941959Smarc break;
4041959Smarc case 'c':
4144588Smarc eflags |= VIS_CSTYLE;
4241959Smarc break;
4344588Smarc case 't':
4444588Smarc eflags |= VIS_TAB;
4541959Smarc break;
4644588Smarc case 's':
4744588Smarc eflags |= VIS_SAFE;
4844588Smarc break;
4941959Smarc case 'o':
5044588Smarc eflags |= VIS_OCTAL;
5141959Smarc break;
5244588Smarc case 'b':
5344588Smarc eflags |= VIS_NOSLASH;
5441959Smarc break;
5541959Smarc case 'F':
5641959Smarc if ((foldwidth = atoi(optarg))<5) {
5741959Smarc fprintf(stderr,
5844588Smarc "vis: can't fold lines to less than 5 cols\n");
5941959Smarc exit(1);
6041959Smarc }
6141959Smarc /*FALLTHROUGH*/
6241959Smarc case 'f':
6341959Smarc fold++; /* fold output lines to 80 cols */
6441959Smarc break; /* using hidden newline */
6544588Smarc case 'l':
6644588Smarc markeol++; /* mark end of line with \$ */
6741959Smarc break;
6844588Smarc #ifdef DEBUG
6944588Smarc case 'd':
7044588Smarc debug++;
7141959Smarc break;
7244588Smarc #endif
7341959Smarc case '?':
7441959Smarc default:
7541959Smarc fprintf(stderr,
7644588Smarc "usage: vis [-nwctsobf] [-F foldwidth]\n");
7741959Smarc exit(1);
7841959Smarc }
7941959Smarc argc -= optind;
8041959Smarc argv += optind;
8141959Smarc
8241959Smarc if (*argv)
8341959Smarc while (*argv) {
8441959Smarc if ((fp=fopen(*argv, "r")) != NULL)
8541959Smarc process(fp, *argv);
8641959Smarc else
8741959Smarc fprintf(stderr, "vis: %s: %s\n", *argv,
8841959Smarc (char *)strerror(errno));
8941959Smarc argv++;
9041959Smarc }
9141959Smarc else
9241959Smarc process(stdin, "<stdin>");
9341959Smarc exit(0);
9441959Smarc }
9541959Smarc
process(fp,filename)9641959Smarc process(fp, filename)
9741959Smarc FILE *fp;
9841959Smarc char *filename;
9941959Smarc {
10041959Smarc static int col = 0;
10144588Smarc register char *cp = "\0"+1; /* so *(cp-1) starts out != '\n' */
10241959Smarc register int c, rachar;
10341959Smarc register char nc;
10444588Smarc char buff[5];
10541959Smarc
10644588Smarc c = getc(fp);
10744588Smarc while (c != EOF) {
10844588Smarc rachar = getc(fp);
10944588Smarc if (none) {
11044588Smarc cp = buff;
11144588Smarc *cp++ = c;
11244588Smarc if (c == '\\')
11344588Smarc *cp++ = '\\';
11444588Smarc *cp = '\0';
11544588Smarc } else if (markeol && c == '\n') {
11644588Smarc cp = buff;
11744588Smarc if ((eflags & VIS_NOSLASH) == 0)
11844588Smarc *cp++ = '\\';
11944588Smarc *cp++ = '$';
12044588Smarc *cp++ = '\n';
12144588Smarc *cp = '\0';
12244588Smarc } else
12344588Smarc (void) vis(buff, (char)c, eflags, (char)rachar);
12444588Smarc
12544588Smarc cp = buff;
12644588Smarc if (fold) {
12744588Smarc #ifdef DEBUG
12844588Smarc if (debug)
12944588Smarc printf("<%02d,", col);
13044588Smarc #endif
13144588Smarc col = foldit(cp, col, foldwidth);
13244588Smarc #ifdef DEBUG
13344588Smarc if (debug)
13444588Smarc printf("%02d>", col);
13544588Smarc #endif
13641959Smarc }
13744588Smarc do {
13844588Smarc putchar(*cp);
13944588Smarc } while (*++cp);
14044588Smarc c = rachar;
14144588Smarc }
14241959Smarc /*
14344588Smarc * terminate partial line with a hidden newline
14441959Smarc */
14544588Smarc if (fold && *(cp-1) != '\n')
14644588Smarc printf("\\\n");
14741959Smarc }
148