[BACK]Return to vis.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / vis

Annotation of src/usr.bin/vis/vis.c, Revision 1.19

1.19    ! deraadt     1: /*     $OpenBSD: vis.c,v 1.18 2015/10/05 06:59:18 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: vis.c,v 1.4 1994/12/20 16:13:03 jtc Exp $      */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.6       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <stdio.h>
                     34: #include <string.h>
                     35: #include <stdlib.h>
                     36: #include <unistd.h>
1.17      deraadt    37: #include <limits.h>
1.1       deraadt    38: #include <err.h>
                     39: #include <vis.h>
                     40:
1.9       deraadt    41: int eflags, fold, foldwidth=80, none, markeol;
                     42:
                     43: #ifdef DEBUG
                     44: int debug;
                     45: #endif
1.1       deraadt    46:
1.5       millert    47: int foldit(char *, int, int);
1.9       deraadt    48: void process(FILE *);
1.8       millert    49: __dead void usage(void);
1.1       deraadt    50:
                     51: int
1.7       deraadt    52: main(int argc, char *argv[])
1.1       deraadt    53: {
1.17      deraadt    54:        const char *errstr;
1.1       deraadt    55:        FILE *fp;
                     56:        int ch;
                     57:
1.19    ! deraadt    58:        if (pledge("stdio rpath", NULL) == -1)
        !            59:                err(1, "pledge");
1.18      deraadt    60:
1.14      djm        61:        while ((ch = getopt(argc, argv, "anwctsobfF:ld")) != -1)
1.15      okan       62:                switch(ch) {
1.13      djm        63:                case 'a':
                     64:                        eflags |= VIS_ALL;
                     65:                        break;
1.1       deraadt    66:                case 'n':
1.16      deraadt    67:                        none = 1;
1.1       deraadt    68:                        break;
                     69:                case 'w':
                     70:                        eflags |= VIS_WHITE;
                     71:                        break;
                     72:                case 'c':
                     73:                        eflags |= VIS_CSTYLE;
                     74:                        break;
                     75:                case 't':
                     76:                        eflags |= VIS_TAB;
                     77:                        break;
                     78:                case 's':
                     79:                        eflags |= VIS_SAFE;
                     80:                        break;
                     81:                case 'o':
                     82:                        eflags |= VIS_OCTAL;
                     83:                        break;
                     84:                case 'b':
                     85:                        eflags |= VIS_NOSLASH;
                     86:                        break;
                     87:                case 'F':
1.17      deraadt    88:                        foldwidth = strtonum(optarg, 1, INT_MAX, &errstr);
                     89:                        if (errstr)
                     90:                                errx(1, "%s: %s", optarg, errstr);
                     91:                        if (foldwidth < 5)
1.1       deraadt    92:                                errx(1, "can't fold lines to less than 5 cols");
                     93:                        /*FALLTHROUGH*/
                     94:                case 'f':
1.16      deraadt    95:                        fold = 1;       /* fold output lines to 80 cols */
1.1       deraadt    96:                        break;          /* using hidden newline */
                     97:                case 'l':
1.16      deraadt    98:                        markeol = 1;    /* mark end of line with \$ */
1.1       deraadt    99:                        break;
                    100: #ifdef DEBUG
                    101:                case 'd':
1.16      deraadt   102:                        debug = 1;
1.1       deraadt   103:                        break;
                    104: #endif
                    105:                case '?':
                    106:                default:
1.8       millert   107:                        usage();
1.1       deraadt   108:                }
                    109:        argc -= optind;
                    110:        argv += optind;
                    111:
                    112:        if (*argv)
                    113:                while (*argv) {
                    114:                        if ((fp=fopen(*argv, "r")) != NULL)
1.9       deraadt   115:                                process(fp);
1.1       deraadt   116:                        else
                    117:                                warn("%s", *argv);
                    118:                        argv++;
                    119:                }
1.18      deraadt   120:        else {
1.19    ! deraadt   121:                if (pledge("stdio", NULL) == -1)
        !           122:                        err(1, "pledge");
1.9       deraadt   123:                process(stdin);
1.18      deraadt   124:        }
1.1       deraadt   125:        exit(0);
                    126: }
                    127:
                    128: void
1.9       deraadt   129: process(FILE *fp)
1.1       deraadt   130: {
                    131:        static int col = 0;
1.4       mpech     132:        char *cp = "\0"+1;      /* so *(cp-1) starts out != '\n' */
                    133:        int c, rachar;
1.1       deraadt   134:        char buff[5];
                    135:
                    136:        c = getc(fp);
                    137:        while (c != EOF) {
                    138:                rachar = getc(fp);
                    139:                if (none) {
                    140:                        cp = buff;
                    141:                        *cp++ = c;
                    142:                        if (c == '\\')
                    143:                                *cp++ = '\\';
                    144:                        *cp = '\0';
                    145:                } else if (markeol && c == '\n') {
                    146:                        cp = buff;
                    147:                        if ((eflags & VIS_NOSLASH) == 0)
                    148:                                *cp++ = '\\';
                    149:                        *cp++ = '$';
                    150:                        *cp++ = '\n';
                    151:                        *cp = '\0';
                    152:                } else
                    153:                        (void) vis(buff, (char)c, eflags, (char)rachar);
                    154:
                    155:                cp = buff;
                    156:                if (fold) {
                    157: #ifdef DEBUG
                    158:                        if (debug)
                    159:                                printf("<%02d,", col);
                    160: #endif
                    161:                        col = foldit(cp, col, foldwidth);
                    162: #ifdef DEBUG
                    163:                        if (debug)
                    164:                                printf("%02d>", col);
                    165: #endif
                    166:                }
                    167:                do {
                    168:                        putchar(*cp);
                    169:                } while (*++cp);
                    170:                c = rachar;
                    171:        }
                    172:        /*
                    173:         * terminate partial line with a hidden newline
                    174:         */
                    175:        if (fold && *(cp-1) != '\n')
                    176:                printf("\\\n");
1.8       millert   177: }
                    178:
                    179: __dead void
                    180: usage(void)
                    181: {
                    182:        extern char *__progname;
                    183:
1.14      djm       184:        fprintf(stderr, "usage: %s [-abcflnostw] [-F foldwidth] [file ...]\n",
1.8       millert   185:            __progname);
                    186:        exit(1);
1.1       deraadt   187: }