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

Annotation of src/usr.bin/rs/rs.c, Revision 1.27

1.27    ! deraadt     1: /*     $OpenBSD: rs.c,v 1.26 2015/10/06 03:26:31 deraadt Exp $ */
1.3       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.8       millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: /*
                     33:  *     rs - reshape a data array
                     34:  *     Author:  John Kunze, Office of Comp. Affairs, UCB
                     35:  *             BEWARE: lots of unfinished edges
                     36:  */
                     37:
                     38: #include <ctype.h>
1.4       millert    39: #include <err.h>
1.14      millert    40: #include <errno.h>
                     41: #include <limits.h>
1.1       deraadt    42: #include <stdio.h>
                     43: #include <stdlib.h>
                     44: #include <string.h>
1.23      millert    45: #include <unistd.h>
1.1       deraadt    46:
                     47: long   flags;
                     48: #define        TRANSPOSE       000001
                     49: #define        MTRANSPOSE      000002
                     50: #define        ONEPERLINE      000004
                     51: #define        ONEISEPONLY     000010
                     52: #define        ONEOSEPONLY     000020
                     53: #define        NOTRIMENDCOL    000040
                     54: #define        SQUEEZE         000100
                     55: #define        SHAPEONLY       000200
                     56: #define        DETAILSHAPE     000400
                     57: #define        RIGHTADJUST     001000
                     58: #define        NULLPAD         002000
                     59: #define        RECYCLE         004000
                     60: #define        SKIPPRINT       010000
                     61: #define ONEPERCHAR     0100000
                     62: #define NOARGS         0200000
                     63:
                     64: short  *colwidths;
                     65: int    nelem;
                     66: char   **elem;
                     67: char   **endelem;
                     68: char   *curline;
                     69: int    allocsize = BUFSIZ;
                     70: int    curlen;
                     71: int    irows, icols;
                     72: int    orows, ocols;
                     73: int    maxlen;
                     74: int    skip;
                     75: int    propgutter;
                     76: char   isep = ' ', osep = ' ';
                     77: int    owidth = 80, gutter = 2;
                     78:
1.12      millert    79: void     usage(void);
1.7       millert    80: void     getargs(int, char *[]);
                     81: void     getfile(void);
1.21      fgsch      82: int      get_line(void);
1.7       millert    83: char   **getptrs(char **);
                     84: void     prepfile(void);
                     85: void     prints(char *, int);
                     86: void     putfile(void);
1.1       deraadt    87:
1.2       deraadt    88: #define INCR(ep) do {                  \
                     89:        if (++ep >= endelem)            \
                     90:                ep = getptrs(ep);       \
                     91: } while(0)
                     92:
1.1       deraadt    93: int
1.9       deraadt    94: main(int argc, char *argv[])
1.1       deraadt    95: {
1.27    ! deraadt    96:        if (pledge("stdio", NULL) == -1)
        !            97:                err(1, "pledge");
1.26      deraadt    98:
1.1       deraadt    99:        getargs(argc, argv);
                    100:        getfile();
                    101:        if (flags & SHAPEONLY) {
                    102:                printf("%d %d\n", irows, icols);
                    103:                exit(0);
                    104:        }
                    105:        prepfile();
                    106:        putfile();
                    107:        exit(0);
                    108: }
                    109:
                    110: void
1.9       deraadt   111: getfile(void)
1.1       deraadt   112: {
1.6       mpech     113:        char *p;
                    114:        char *endp;
1.11      tedu      115:        char **ep = NULL;
1.1       deraadt   116:        int multisep = (flags & ONEISEPONLY ? 0 : 1);
                    117:        int nullpad = flags & NULLPAD;
                    118:        char **padto;
                    119:
                    120:        while (skip--) {
1.21      fgsch     121:                get_line();
1.1       deraadt   122:                if (flags & SKIPPRINT)
                    123:                        puts(curline);
                    124:        }
1.21      fgsch     125:        get_line();
1.1       deraadt   126:        if (flags & NOARGS && curlen < owidth)
                    127:                flags |= ONEPERLINE;
                    128:        if (flags & ONEPERLINE)
                    129:                icols = 1;
                    130:        else                            /* count cols on first line */
                    131:                for (p = curline, endp = curline + curlen; p < endp; p++) {
                    132:                        if (*p == isep && multisep)
                    133:                                continue;
                    134:                        icols++;
                    135:                        while (*p && *p != isep)
                    136:                                p++;
                    137:                }
                    138:        ep = getptrs(elem);
                    139:        p = curline;
                    140:        do {
                    141:                if (flags & ONEPERLINE) {
1.2       deraadt   142:                        *ep = curline;
                    143:                        INCR(ep);               /* prepare for next entry */
1.1       deraadt   144:                        if (maxlen < curlen)
                    145:                                maxlen = curlen;
                    146:                        irows++;
                    147:                        continue;
                    148:                }
                    149:                for (p = curline, endp = curline + curlen; p < endp; p++) {
                    150:                        if (*p == isep && multisep)
                    151:                                continue;       /* eat up column separators */
                    152:                        if (*p == isep)         /* must be an empty column */
                    153:                                *ep = "";
                    154:                        else                    /* store column entry */
                    155:                                *ep = p;
                    156:                        while (p < endp && *p != isep)
                    157:                                p++;            /* find end of entry */
                    158:                        *p = '\0';              /* mark end of entry */
                    159:                        if (maxlen < p - *ep)   /* update maxlen */
                    160:                                maxlen = p - *ep;
1.2       deraadt   161:                        INCR(ep);               /* prepare for next entry */
1.1       deraadt   162:                }
                    163:                irows++;                        /* update row count */
                    164:                if (nullpad) {                  /* pad missing entries */
                    165:                        padto = elem + irows * icols;
1.2       deraadt   166:                        while (ep < padto) {
                    167:                                *ep = "";
                    168:                                INCR(ep);
                    169:                        }
1.1       deraadt   170:                }
1.21      fgsch     171:        } while (get_line() != EOF);
1.11      tedu      172:        *ep = NULL;                             /* mark end of pointers */
1.1       deraadt   173:        nelem = ep - elem;
                    174: }
                    175:
                    176: void
1.9       deraadt   177: putfile(void)
1.1       deraadt   178: {
1.6       mpech     179:        char **ep;
                    180:        int i, j, n;
1.1       deraadt   181:
                    182:        ep = elem;
1.2       deraadt   183:        if (flags & TRANSPOSE) {
1.1       deraadt   184:                for (i = 0; i < orows; i++) {
                    185:                        for (j = i; j < nelem; j += orows)
                    186:                                prints(ep[j], (j - i) / orows);
                    187:                        putchar('\n');
                    188:                }
1.2       deraadt   189:        } else {
                    190:                for (n = 0, i = 0; i < orows && n < nelem; i++) {
                    191:                        for (j = 0; j < ocols; j++) {
                    192:                                if (n++ >= nelem)
                    193:                                        break;
1.1       deraadt   194:                                prints(*ep++, j);
1.2       deraadt   195:                        }
1.1       deraadt   196:                        putchar('\n');
                    197:                }
1.2       deraadt   198:        }
1.1       deraadt   199: }
                    200:
                    201: void
1.9       deraadt   202: prints(char *s, int col)
1.1       deraadt   203: {
1.6       mpech     204:        int n;
                    205:        char *p = s;
1.1       deraadt   206:
                    207:        while (*p)
                    208:                p++;
                    209:        n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
                    210:        if (flags & RIGHTADJUST)
                    211:                while (n-- > 0)
                    212:                        putchar(osep);
                    213:        for (p = s; *p; p++)
                    214:                putchar(*p);
                    215:        while (n-- > 0)
                    216:                putchar(osep);
                    217: }
                    218:
                    219: void
1.12      millert   220: usage(void)
1.1       deraadt   221: {
1.12      millert   222:        extern char *__progname;
                    223:
1.1       deraadt   224:        fprintf(stderr,
1.19      sobrado   225:            "usage: %s [-CcSs[x]] [-GgKkw N] [-EeHhjmnTtyz] [rows [cols]]\n",
1.12      millert   226:            __progname);
1.1       deraadt   227:        exit(1);
                    228: }
                    229:
                    230: void
1.9       deraadt   231: prepfile(void)
1.1       deraadt   232: {
1.6       mpech     233:        char **ep;
                    234:        int  i;
                    235:        int  j;
1.1       deraadt   236:        char **lp;
                    237:        int colw;
                    238:        int max = 0;
                    239:        int n;
                    240:
                    241:        if (!nelem)
                    242:                exit(0);
                    243:        gutter += maxlen * propgutter / 100.0;
                    244:        colw = maxlen + gutter;
                    245:        if (flags & MTRANSPOSE) {
                    246:                orows = icols;
                    247:                ocols = irows;
                    248:        }
                    249:        else if (orows == 0 && ocols == 0) {    /* decide rows and cols */
                    250:                ocols = owidth / colw;
1.2       deraadt   251:                if (ocols == 0) {
1.4       millert   252:                        warnx("Display width %d is less than column width %d",
                    253:                            owidth, colw);
1.2       deraadt   254:                        ocols = 1;
                    255:                }
1.1       deraadt   256:                if (ocols > nelem)
                    257:                        ocols = nelem;
                    258:                orows = nelem / ocols + (nelem % ocols ? 1 : 0);
                    259:        }
                    260:        else if (orows == 0)                    /* decide on rows */
                    261:                orows = nelem / ocols + (nelem % ocols ? 1 : 0);
                    262:        else if (ocols == 0)                    /* decide on cols */
                    263:                ocols = nelem / orows + (nelem % orows ? 1 : 0);
                    264:        lp = elem + orows * ocols;
                    265:        while (lp > endelem) {
                    266:                getptrs(elem + nelem);
                    267:                lp = elem + orows * ocols;
                    268:        }
                    269:        if (flags & RECYCLE) {
                    270:                for (ep = elem + nelem; ep < lp; ep++)
                    271:                        *ep = *(ep - nelem);
                    272:                nelem = lp - elem;
                    273:        }
1.25      deraadt   274:        if (!(colwidths = calloc(ocols, sizeof(short))))
1.2       deraadt   275:                errx(1, "malloc:  No gutter space");
1.1       deraadt   276:        if (flags & SQUEEZE) {
                    277:                if (flags & TRANSPOSE)
                    278:                        for (ep = elem, i = 0; i < ocols; i++) {
                    279:                                for (j = 0; j < orows; j++)
                    280:                                        if ((n = strlen(*ep++)) > max)
                    281:                                                max = n;
                    282:                                colwidths[i] = max + gutter;
                    283:                        }
                    284:                else
1.5       deraadt   285:                        for (ep = elem, i = 0; i < ocols; i++) {
1.1       deraadt   286:                                for (j = i; j < nelem; j += ocols)
                    287:                                        if ((n = strlen(ep[j])) > max)
                    288:                                                max = n;
                    289:                                colwidths[i] = max + gutter;
                    290:                        }
1.13      millert   291:        } else {
1.1       deraadt   292:                for (i = 0; i < ocols; i++)
                    293:                        colwidths[i] = colw;
1.13      millert   294:        }
1.1       deraadt   295:        if (!(flags & NOTRIMENDCOL)) {
                    296:                if (flags & RIGHTADJUST)
                    297:                        colwidths[0] -= gutter;
                    298:                else
                    299:                        colwidths[ocols - 1] = 0;
                    300:        }
                    301:        n = orows * ocols;
                    302:        if (n > nelem && (flags & RECYCLE))
                    303:                nelem = n;
                    304: }
                    305:
                    306: #define        BSIZE   2048
                    307: char   ibuf[BSIZE];            /* two screenfuls should do */
                    308:
                    309: int
1.21      fgsch     310: get_line(void) /* get line; maintain curline, curlen; manage storage */
1.1       deraadt   311: {
                    312:        static  int putlength;
                    313:        static  char *endblock = ibuf + BSIZE;
1.6       mpech     314:        char *p;
                    315:        int c, i;
1.1       deraadt   316:
                    317:        if (!irows) {
                    318:                curline = ibuf;
                    319:                putlength = flags & DETAILSHAPE;
                    320:        }
                    321:        else if (skip <= 0) {                   /* don't waste storage */
                    322:                curline += curlen + 1;
                    323:                if (putlength)          /* print length, recycle storage */
                    324:                        printf(" %d line %d\n", curlen, irows);
                    325:        }
                    326:        if (!putlength && endblock - curline < BUFSIZ) {   /* need storage */
1.25      deraadt   327:                if (!(curline = malloc(BSIZE)))
1.2       deraadt   328:                        errx(1, "File too large");
1.1       deraadt   329:                endblock = curline + BSIZE;
                    330:        }
                    331:        for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++)
                    332:                if ((c = getchar()) == EOF || c == '\n')
                    333:                        break;
                    334:        *p = '\0';
                    335:        curlen = i - 1;
                    336:        return(c);
                    337: }
                    338:
                    339: char **
1.9       deraadt   340: getptrs(char **sp)
1.1       deraadt   341: {
1.6       mpech     342:        char **p;
1.22      otto      343:        int newsize;
1.1       deraadt   344:
1.10      tedu      345:        newsize = allocsize * 2;
1.24      doug      346:        p = reallocarray(elem, newsize, sizeof(char *));
1.10      tedu      347:        if (p == NULL)
1.2       deraadt   348:                err(1, "no memory");
                    349:
1.22      otto      350:        allocsize = newsize;
                    351:        sp += p - elem;
1.10      tedu      352:        elem = p;
                    353:        endelem = elem + allocsize;
1.2       deraadt   354:        return(sp);
1.1       deraadt   355: }
                    356:
                    357: void
1.9       deraadt   358: getargs(int ac, char *av[])
1.1       deraadt   359: {
1.12      millert   360:        int ch;
1.14      millert   361:        const char *errstr;
1.1       deraadt   362:
1.12      millert   363:        if (ac == 1)
1.1       deraadt   364:                flags |= NOARGS | TRANSPOSE;
1.12      millert   365:        while ((ch = getopt(ac, av, "c::C::s::S::k:K:g:G:w:tTeEnyjhHmz")) != -1) {
                    366:                switch (ch) {
                    367:                case 'T':
                    368:                        flags |= MTRANSPOSE;
                    369:                        /* FALLTHROUGH */
                    370:                case 't':
                    371:                        flags |= TRANSPOSE;
                    372:                        break;
                    373:                case 'c':               /* input col. separator */
                    374:                        flags |= ONEISEPONLY;
                    375:                        /* FALLTHROUGH */
                    376:                case 's':               /* one or more allowed */
                    377:                        if (optarg == NULL)
                    378:                                isep = '\t';    /* default is ^I */
                    379:                        else if (optarg[1] != '\0')
                    380:                                usage();        /* single char only */
                    381:                        else
                    382:                                isep = *optarg;
                    383:                        break;
                    384:                case 'C':
                    385:                        flags |= ONEOSEPONLY;
                    386:                        /* FALLTHROUGH */
                    387:                case 'S':
                    388:                        if (optarg == NULL)
                    389:                                osep = '\t';    /* default is ^I */
                    390:                        else if (optarg[1] != '\0')
                    391:                                usage();        /* single char only */
                    392:                        else
                    393:                                osep = *optarg;
                    394:                        break;
                    395:                case 'w':               /* window width, default 80 */
1.14      millert   396:                        owidth = strtonum(optarg, 1, INT_MAX, &errstr);
                    397:                        if (errstr) {
                    398:                                warnx("width %s", errstr);
1.12      millert   399:                                usage();
                    400:                        }
                    401:                        break;
                    402:                case 'K':                       /* skip N lines */
                    403:                        flags |= SKIPPRINT;
                    404:                        /* FALLTHROUGH */
                    405:                case 'k':                       /* skip, do not print */
1.14      millert   406:                        skip = strtonum(optarg, 0, INT_MAX, &errstr);
                    407:                        if (errstr) {
                    408:                                warnx("skip value %s", errstr);
                    409:                                usage();
                    410:                        }
                    411:                        if (skip == 0)
1.12      millert   412:                                skip = 1;
                    413:                        break;
                    414:                case 'm':
                    415:                        flags |= NOTRIMENDCOL;
                    416:                        break;
1.14      millert   417:                case 'g':               /* gutter width */
                    418:                        gutter = strtonum(optarg, 0, INT_MAX, &errstr);
                    419:                        if (errstr) {
                    420:                                warnx("gutter width %s", errstr);
                    421:                                usage();
                    422:                        }
1.12      millert   423:                        break;
                    424:                case 'G':
1.14      millert   425:                        propgutter = strtonum(optarg, 0, INT_MAX, &errstr);
                    426:                        if (errstr) {
                    427:                                warnx("gutter proportion %s", errstr);
                    428:                                usage();
                    429:                        }
1.12      millert   430:                        break;
                    431:                case 'e':               /* each line is an entry */
                    432:                        flags |= ONEPERLINE;
                    433:                        break;
                    434:                case 'E':
                    435:                        flags |= ONEPERCHAR;
                    436:                        break;
                    437:                case 'j':                       /* right adjust */
                    438:                        flags |= RIGHTADJUST;
                    439:                        break;
                    440:                case 'n':       /* null padding for missing values */
                    441:                        flags |= NULLPAD;
                    442:                        break;
                    443:                case 'y':
                    444:                        flags |= RECYCLE;
                    445:                        break;
                    446:                case 'H':                       /* print shape only */
                    447:                        flags |= DETAILSHAPE;
                    448:                        /* FALLTHROUGH */
                    449:                case 'h':
                    450:                        flags |= SHAPEONLY;
                    451:                        break;
                    452:                case 'z':                       /* squeeze col width */
                    453:                        flags |= SQUEEZE;
                    454:                        break;
                    455:                default:
                    456:                        usage();
                    457:                }
1.1       deraadt   458:        }
1.12      millert   459:        ac -= optind;
                    460:        av += optind;
                    461:
1.1       deraadt   462:        switch (ac) {
                    463:        case 2:
1.14      millert   464:                ocols = strtonum(av[1], 0, INT_MAX, &errstr);
                    465:                if (errstr) {
                    466:                        warnx("columns value %s", errstr);
                    467:                        usage();
                    468:                }
1.17      moritz    469:                /* FALLTHROUGH */
1.1       deraadt   470:        case 1:
1.14      millert   471:                orows = strtonum(av[0], 0, INT_MAX, &errstr);
                    472:                if (errstr) {
                    473:                        warnx("columns value %s", errstr);
                    474:                        usage();
                    475:                }
1.17      moritz    476:                /* FALLTHROUGH */
1.1       deraadt   477:        case 0:
                    478:                break;
                    479:        default:
1.12      millert   480:                usage();
1.1       deraadt   481:        }
                    482: }