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

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

1.19    ! millert     1: /*     $OpenBSD: column.c,v 1.18 2014/05/17 20:05:07 espie Exp $       */
1.1       deraadt     2: /*     $NetBSD: column.c,v 1.4 1995/09/02 05:53:03 jtc Exp $   */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993, 1994
                      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.8       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 <sys/types.h>
                     34: #include <sys/ioctl.h>
                     35:
                     36: #include <ctype.h>
                     37: #include <err.h>
                     38: #include <limits.h>
                     39: #include <stdio.h>
                     40: #include <stdlib.h>
                     41: #include <string.h>
                     42: #include <unistd.h>
                     43:
1.7       millert    44: void  c_columnate(void);
1.18      espie      45: void *ereallocarray(void *, size_t, size_t);
                     46: void *ecalloc(size_t, size_t);
1.7       millert    47: void  input(FILE *);
                     48: void  maketbl(void);
                     49: void  print(void);
                     50: void  r_columnate(void);
                     51: void  usage(void);
1.1       deraadt    52:
                     53: int termwidth = 80;            /* default terminal width */
                     54:
                     55: int entries;                   /* number of records */
                     56: int eval;                      /* exit value */
                     57: int maxlength;                 /* longest record */
                     58: char **list;                   /* array of pointers to records */
                     59: char *separator = "\t ";       /* field separator for table option */
                     60:
                     61: int
1.9       deraadt    62: main(int argc, char *argv[])
1.1       deraadt    63: {
                     64:        struct winsize win;
                     65:        FILE *fp;
                     66:        int ch, tflag, xflag;
                     67:        char *p;
1.13      jdixon     68:        const char *errstr;
1.1       deraadt    69:
                     70:        if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
1.13      jdixon     71:                if ((p = getenv("COLUMNS")) && *p != '\0') {
                     72:                        termwidth = strtonum(p, 1, INT_MAX, &errstr);
                     73:                        if (errstr != NULL)
                     74:                                errx(1, "%s: %s", errstr, p);
                     75:                }
1.1       deraadt    76:        } else
                     77:                termwidth = win.ws_col;
                     78:
                     79:        tflag = xflag = 0;
1.3       millert    80:        while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
1.1       deraadt    81:                switch(ch) {
                     82:                case 'c':
1.13      jdixon     83:                        termwidth = strtonum(optarg, 1, INT_MAX, &errstr);
                     84:                        if (errstr != NULL)
                     85:                                errx(1, "%s: %s", errstr, optarg);
1.1       deraadt    86:                        break;
                     87:                case 's':
                     88:                        separator = optarg;
                     89:                        break;
                     90:                case 't':
                     91:                        tflag = 1;
                     92:                        break;
                     93:                case 'x':
                     94:                        xflag = 1;
                     95:                        break;
                     96:                case '?':
                     97:                default:
                     98:                        usage();
                     99:                }
                    100:        argc -= optind;
                    101:        argv += optind;
                    102:
                    103:        if (!*argv)
                    104:                input(stdin);
                    105:        else for (; *argv; ++argv)
1.6       deraadt   106:                if ((fp = fopen(*argv, "r"))) {
1.1       deraadt   107:                        input(fp);
                    108:                        (void)fclose(fp);
                    109:                } else {
                    110:                        warn("%s", *argv);
                    111:                        eval = 1;
                    112:                }
                    113:
                    114:        if (!entries)
                    115:                exit(eval);
                    116:
                    117:        if (tflag)
                    118:                maketbl();
                    119:        else if (maxlength >= termwidth)
                    120:                print();
                    121:        else if (xflag)
                    122:                c_columnate();
                    123:        else
                    124:                r_columnate();
                    125:        exit(eval);
                    126: }
                    127:
                    128: #define        TAB     8
                    129: void
1.9       deraadt   130: c_columnate(void)
1.1       deraadt   131: {
                    132:        int chcnt, col, cnt, endcol, numcols;
                    133:        char **lp;
                    134:
                    135:        maxlength = (maxlength + TAB) & ~(TAB - 1);
                    136:        numcols = termwidth / maxlength;
                    137:        endcol = maxlength;
                    138:        for (chcnt = col = 0, lp = list;; ++lp) {
                    139:                chcnt += printf("%s", *lp);
                    140:                if (!--entries)
                    141:                        break;
                    142:                if (++col == numcols) {
                    143:                        chcnt = col = 0;
                    144:                        endcol = maxlength;
                    145:                        putchar('\n');
                    146:                } else {
1.6       deraadt   147:                        while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1       deraadt   148:                                (void)putchar('\t');
                    149:                                chcnt = cnt;
                    150:                        }
                    151:                        endcol += maxlength;
                    152:                }
                    153:        }
                    154:        if (chcnt)
                    155:                putchar('\n');
                    156: }
                    157:
                    158: void
1.9       deraadt   159: r_columnate(void)
1.1       deraadt   160: {
                    161:        int base, chcnt, cnt, col, endcol, numcols, numrows, row;
                    162:
                    163:        maxlength = (maxlength + TAB) & ~(TAB - 1);
                    164:        numcols = termwidth / maxlength;
1.5       millert   165:        if (numcols == 0)
                    166:                numcols = 1;
1.1       deraadt   167:        numrows = entries / numcols;
                    168:        if (entries % numcols)
                    169:                ++numrows;
                    170:
                    171:        for (row = 0; row < numrows; ++row) {
                    172:                endcol = maxlength;
                    173:                for (base = row, chcnt = col = 0; col < numcols; ++col) {
                    174:                        chcnt += printf("%s", list[base]);
                    175:                        if ((base += numrows) >= entries)
                    176:                                break;
1.6       deraadt   177:                        while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
1.1       deraadt   178:                                (void)putchar('\t');
                    179:                                chcnt = cnt;
                    180:                        }
                    181:                        endcol += maxlength;
                    182:                }
                    183:                putchar('\n');
                    184:        }
                    185: }
                    186:
                    187: void
1.9       deraadt   188: print(void)
1.1       deraadt   189: {
                    190:        int cnt;
                    191:        char **lp;
                    192:
                    193:        for (cnt = entries, lp = list; cnt--; ++lp)
                    194:                (void)printf("%s\n", *lp);
                    195: }
                    196:
                    197: typedef struct _tbl {
                    198:        char **list;
                    199:        int cols, *len;
                    200: } TBL;
                    201: #define        DEFCOLS 25
                    202:
                    203: void
1.9       deraadt   204: maketbl(void)
1.1       deraadt   205: {
                    206:        TBL *t;
                    207:        int coloff, cnt;
                    208:        char *p, **lp;
1.19    ! millert   209:        int *lens, maxcols = DEFCOLS;
1.1       deraadt   210:        TBL *tbl;
1.14      millert   211:        char **cols;
1.1       deraadt   212:
1.18      espie     213:        t = tbl = ecalloc(entries, sizeof(TBL));
1.19    ! millert   214:        cols = ereallocarray(NULL, maxcols, sizeof(char *));
1.18      espie     215:        lens = ecalloc(maxcols, sizeof(int));
1.1       deraadt   216:        for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
1.6       deraadt   217:                for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator));
1.1       deraadt   218:                    p = NULL)
                    219:                        if (++coloff == maxcols) {
                    220:                                maxcols += DEFCOLS;
1.18      espie     221:                                cols = ereallocarray(cols, maxcols,
                    222:                                    sizeof(char *));
                    223:                                lens = ereallocarray(lens, maxcols,
                    224:                                    sizeof(int));
1.14      millert   225:                                memset(lens + coloff, 0, DEFCOLS * sizeof(int));
1.1       deraadt   226:                        }
1.14      millert   227:                if (coloff == 0)
                    228:                        continue;
1.18      espie     229:                t->list = ecalloc(coloff, sizeof(char *));
                    230:                t->len = ecalloc(coloff, sizeof(int));
1.1       deraadt   231:                for (t->cols = coloff; --coloff >= 0;) {
                    232:                        t->list[coloff] = cols[coloff];
                    233:                        t->len[coloff] = strlen(cols[coloff]);
                    234:                        if (t->len[coloff] > lens[coloff])
                    235:                                lens[coloff] = t->len[coloff];
                    236:                }
                    237:        }
                    238:        for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
1.14      millert   239:                if (t->cols > 0) {
                    240:                        for (coloff = 0; coloff < t->cols - 1; ++coloff)
                    241:                                (void)printf("%s%*s", t->list[coloff],
                    242:                                    lens[coloff] - t->len[coloff] + 2, " ");
                    243:                        (void)printf("%s\n", t->list[coloff]);
                    244:                }
1.1       deraadt   245:        }
1.19    ! millert   246:        free(tbl);
1.17      jsg       247:        free(lens);
                    248:        free(cols);
1.1       deraadt   249: }
                    250:
                    251: #define        DEFNUM          1000
                    252: #define        MAXLINELEN      (LINE_MAX + 1)
                    253:
                    254: void
1.9       deraadt   255: input(FILE *fp)
1.1       deraadt   256: {
1.19    ! millert   257:        static size_t maxentry = DEFNUM;
1.1       deraadt   258:        int len;
                    259:        char *p, buf[MAXLINELEN];
                    260:
                    261:        if (!list)
1.19    ! millert   262:                list = ecalloc(maxentry, sizeof(char *));
1.1       deraadt   263:        while (fgets(buf, MAXLINELEN, fp)) {
1.16      deraadt   264:                for (p = buf; isspace((unsigned char)*p); ++p);
1.1       deraadt   265:                if (!*p)
                    266:                        continue;
                    267:                if (!(p = strchr(p, '\n'))) {
                    268:                        warnx("line too long");
                    269:                        eval = 1;
                    270:                        continue;
                    271:                }
                    272:                *p = '\0';
                    273:                len = p - buf;
                    274:                if (maxlength < len)
                    275:                        maxlength = len;
                    276:                if (entries == maxentry) {
1.14      millert   277:                        maxentry += DEFNUM;
1.18      espie     278:                        list = ereallocarray(list, maxentry, sizeof(char *));
1.19    ! millert   279:                        memset(list + entries, 0, DEFNUM * sizeof(char *));
1.1       deraadt   280:                }
1.11      otto      281:                if (!(list[entries++] = strdup(buf)))
                    282:                        err(1, NULL);
1.1       deraadt   283:        }
                    284: }
                    285:
                    286: void *
1.18      espie     287: ereallocarray(void *oldp, size_t sz1, size_t sz2)
1.1       deraadt   288: {
1.11      otto      289:        void *p;
1.1       deraadt   290:
1.18      espie     291:        if (!(p = reallocarray(oldp, sz1, sz2)))
1.1       deraadt   292:                err(1, NULL);
1.14      millert   293:        return (p);
                    294: }
                    295:
                    296: void *
1.18      espie     297: ecalloc(size_t sz1, size_t sz2)
1.14      millert   298: {
                    299:        void *p;
                    300:
1.18      espie     301:        if (!(p = calloc(sz1, sz2)))
1.14      millert   302:                err(1, NULL);
1.1       deraadt   303:        return (p);
                    304: }
                    305:
                    306: void
1.9       deraadt   307: usage(void)
1.1       deraadt   308: {
                    309:
                    310:        (void)fprintf(stderr,
1.4       deraadt   311:            "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
1.1       deraadt   312:        exit(1);
                    313: }