Annotation of src/usr.bin/rs/rs.c, Revision 1.22
1.22 ! otto 1: /* $OpenBSD: rs.c,v 1.21 2012/03/04 04:05:15 fgsch 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>
45:
46: long flags;
47: #define TRANSPOSE 000001
48: #define MTRANSPOSE 000002
49: #define ONEPERLINE 000004
50: #define ONEISEPONLY 000010
51: #define ONEOSEPONLY 000020
52: #define NOTRIMENDCOL 000040
53: #define SQUEEZE 000100
54: #define SHAPEONLY 000200
55: #define DETAILSHAPE 000400
56: #define RIGHTADJUST 001000
57: #define NULLPAD 002000
58: #define RECYCLE 004000
59: #define SKIPPRINT 010000
60: #define ONEPERCHAR 0100000
61: #define NOARGS 0200000
62:
63: short *colwidths;
64: int nelem;
65: char **elem;
66: char **endelem;
67: char *curline;
68: int allocsize = BUFSIZ;
69: int curlen;
70: int irows, icols;
71: int orows, ocols;
72: int maxlen;
73: int skip;
74: int propgutter;
75: char isep = ' ', osep = ' ';
76: int owidth = 80, gutter = 2;
77:
1.12 millert 78: void usage(void);
1.7 millert 79: void getargs(int, char *[]);
80: void getfile(void);
1.21 fgsch 81: int get_line(void);
1.7 millert 82: char **getptrs(char **);
83: void prepfile(void);
84: void prints(char *, int);
85: void putfile(void);
1.1 deraadt 86:
1.2 deraadt 87: #define INCR(ep) do { \
88: if (++ep >= endelem) \
89: ep = getptrs(ep); \
90: } while(0)
91:
1.1 deraadt 92: int
1.9 deraadt 93: main(int argc, char *argv[])
1.1 deraadt 94: {
95: getargs(argc, argv);
96: getfile();
97: if (flags & SHAPEONLY) {
98: printf("%d %d\n", irows, icols);
99: exit(0);
100: }
101: prepfile();
102: putfile();
103: exit(0);
104: }
105:
106: void
1.9 deraadt 107: getfile(void)
1.1 deraadt 108: {
1.6 mpech 109: char *p;
110: char *endp;
1.11 tedu 111: char **ep = NULL;
1.1 deraadt 112: int multisep = (flags & ONEISEPONLY ? 0 : 1);
113: int nullpad = flags & NULLPAD;
114: char **padto;
115:
116: while (skip--) {
1.21 fgsch 117: get_line();
1.1 deraadt 118: if (flags & SKIPPRINT)
119: puts(curline);
120: }
1.21 fgsch 121: get_line();
1.1 deraadt 122: if (flags & NOARGS && curlen < owidth)
123: flags |= ONEPERLINE;
124: if (flags & ONEPERLINE)
125: icols = 1;
126: else /* count cols on first line */
127: for (p = curline, endp = curline + curlen; p < endp; p++) {
128: if (*p == isep && multisep)
129: continue;
130: icols++;
131: while (*p && *p != isep)
132: p++;
133: }
134: ep = getptrs(elem);
135: p = curline;
136: do {
137: if (flags & ONEPERLINE) {
1.2 deraadt 138: *ep = curline;
139: INCR(ep); /* prepare for next entry */
1.1 deraadt 140: if (maxlen < curlen)
141: maxlen = curlen;
142: irows++;
143: continue;
144: }
145: for (p = curline, endp = curline + curlen; p < endp; p++) {
146: if (*p == isep && multisep)
147: continue; /* eat up column separators */
148: if (*p == isep) /* must be an empty column */
149: *ep = "";
150: else /* store column entry */
151: *ep = p;
152: while (p < endp && *p != isep)
153: p++; /* find end of entry */
154: *p = '\0'; /* mark end of entry */
155: if (maxlen < p - *ep) /* update maxlen */
156: maxlen = p - *ep;
1.2 deraadt 157: INCR(ep); /* prepare for next entry */
1.1 deraadt 158: }
159: irows++; /* update row count */
160: if (nullpad) { /* pad missing entries */
161: padto = elem + irows * icols;
1.2 deraadt 162: while (ep < padto) {
163: *ep = "";
164: INCR(ep);
165: }
1.1 deraadt 166: }
1.21 fgsch 167: } while (get_line() != EOF);
1.11 tedu 168: *ep = NULL; /* mark end of pointers */
1.1 deraadt 169: nelem = ep - elem;
170: }
171:
172: void
1.9 deraadt 173: putfile(void)
1.1 deraadt 174: {
1.6 mpech 175: char **ep;
176: int i, j, n;
1.1 deraadt 177:
178: ep = elem;
1.2 deraadt 179: if (flags & TRANSPOSE) {
1.1 deraadt 180: for (i = 0; i < orows; i++) {
181: for (j = i; j < nelem; j += orows)
182: prints(ep[j], (j - i) / orows);
183: putchar('\n');
184: }
1.2 deraadt 185: } else {
186: for (n = 0, i = 0; i < orows && n < nelem; i++) {
187: for (j = 0; j < ocols; j++) {
188: if (n++ >= nelem)
189: break;
1.1 deraadt 190: prints(*ep++, j);
1.2 deraadt 191: }
1.1 deraadt 192: putchar('\n');
193: }
1.2 deraadt 194: }
1.1 deraadt 195: }
196:
197: void
1.9 deraadt 198: prints(char *s, int col)
1.1 deraadt 199: {
1.6 mpech 200: int n;
201: char *p = s;
1.1 deraadt 202:
203: while (*p)
204: p++;
205: n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
206: if (flags & RIGHTADJUST)
207: while (n-- > 0)
208: putchar(osep);
209: for (p = s; *p; p++)
210: putchar(*p);
211: while (n-- > 0)
212: putchar(osep);
213: }
214:
215: void
1.12 millert 216: usage(void)
1.1 deraadt 217: {
1.12 millert 218: extern char *__progname;
219:
1.1 deraadt 220: fprintf(stderr,
1.19 sobrado 221: "usage: %s [-CcSs[x]] [-GgKkw N] [-EeHhjmnTtyz] [rows [cols]]\n",
1.12 millert 222: __progname);
1.1 deraadt 223: exit(1);
224: }
225:
226: void
1.9 deraadt 227: prepfile(void)
1.1 deraadt 228: {
1.6 mpech 229: char **ep;
230: int i;
231: int j;
1.1 deraadt 232: char **lp;
233: int colw;
234: int max = 0;
235: int n;
236:
237: if (!nelem)
238: exit(0);
239: gutter += maxlen * propgutter / 100.0;
240: colw = maxlen + gutter;
241: if (flags & MTRANSPOSE) {
242: orows = icols;
243: ocols = irows;
244: }
245: else if (orows == 0 && ocols == 0) { /* decide rows and cols */
246: ocols = owidth / colw;
1.2 deraadt 247: if (ocols == 0) {
1.4 millert 248: warnx("Display width %d is less than column width %d",
249: owidth, colw);
1.2 deraadt 250: ocols = 1;
251: }
1.1 deraadt 252: if (ocols > nelem)
253: ocols = nelem;
254: orows = nelem / ocols + (nelem % ocols ? 1 : 0);
255: }
256: else if (orows == 0) /* decide on rows */
257: orows = nelem / ocols + (nelem % ocols ? 1 : 0);
258: else if (ocols == 0) /* decide on cols */
259: ocols = nelem / orows + (nelem % orows ? 1 : 0);
260: lp = elem + orows * ocols;
261: while (lp > endelem) {
262: getptrs(elem + nelem);
263: lp = elem + orows * ocols;
264: }
265: if (flags & RECYCLE) {
266: for (ep = elem + nelem; ep < lp; ep++)
267: *ep = *(ep - nelem);
268: nelem = lp - elem;
269: }
1.18 deraadt 270: if (!(colwidths = (short *) calloc(ocols, sizeof(short))))
1.2 deraadt 271: errx(1, "malloc: No gutter space");
1.1 deraadt 272: if (flags & SQUEEZE) {
273: if (flags & TRANSPOSE)
274: for (ep = elem, i = 0; i < ocols; i++) {
275: for (j = 0; j < orows; j++)
276: if ((n = strlen(*ep++)) > max)
277: max = n;
278: colwidths[i] = max + gutter;
279: }
280: else
1.5 deraadt 281: for (ep = elem, i = 0; i < ocols; i++) {
1.1 deraadt 282: for (j = i; j < nelem; j += ocols)
283: if ((n = strlen(ep[j])) > max)
284: max = n;
285: colwidths[i] = max + gutter;
286: }
1.13 millert 287: } else {
1.1 deraadt 288: for (i = 0; i < ocols; i++)
289: colwidths[i] = colw;
1.13 millert 290: }
1.1 deraadt 291: if (!(flags & NOTRIMENDCOL)) {
292: if (flags & RIGHTADJUST)
293: colwidths[0] -= gutter;
294: else
295: colwidths[ocols - 1] = 0;
296: }
297: n = orows * ocols;
298: if (n > nelem && (flags & RECYCLE))
299: nelem = n;
300: }
301:
302: #define BSIZE 2048
303: char ibuf[BSIZE]; /* two screenfuls should do */
304:
305: int
1.21 fgsch 306: get_line(void) /* get line; maintain curline, curlen; manage storage */
1.1 deraadt 307: {
308: static int putlength;
309: static char *endblock = ibuf + BSIZE;
1.6 mpech 310: char *p;
311: int c, i;
1.1 deraadt 312:
313: if (!irows) {
314: curline = ibuf;
315: putlength = flags & DETAILSHAPE;
316: }
317: else if (skip <= 0) { /* don't waste storage */
318: curline += curlen + 1;
319: if (putlength) /* print length, recycle storage */
320: printf(" %d line %d\n", curlen, irows);
321: }
322: if (!putlength && endblock - curline < BUFSIZ) { /* need storage */
323: if (!(curline = (char *) malloc(BSIZE)))
1.2 deraadt 324: errx(1, "File too large");
1.1 deraadt 325: endblock = curline + BSIZE;
326: }
327: for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++)
328: if ((c = getchar()) == EOF || c == '\n')
329: break;
330: *p = '\0';
331: curlen = i - 1;
332: return(c);
333: }
334:
335: char **
1.9 deraadt 336: getptrs(char **sp)
1.1 deraadt 337: {
1.6 mpech 338: char **p;
1.22 ! otto 339: int newsize;
1.1 deraadt 340:
1.10 tedu 341: newsize = allocsize * 2;
342: p = realloc(elem, newsize * sizeof(char *));
343: if (p == NULL)
1.2 deraadt 344: err(1, "no memory");
345:
1.22 ! otto 346: allocsize = newsize;
! 347: sp += p - elem;
1.10 tedu 348: elem = p;
349: endelem = elem + allocsize;
1.2 deraadt 350: return(sp);
1.1 deraadt 351: }
352:
353: void
1.9 deraadt 354: getargs(int ac, char *av[])
1.1 deraadt 355: {
1.12 millert 356: int ch;
1.14 millert 357: const char *errstr;
1.1 deraadt 358:
1.12 millert 359: if (ac == 1)
1.1 deraadt 360: flags |= NOARGS | TRANSPOSE;
1.12 millert 361: while ((ch = getopt(ac, av, "c::C::s::S::k:K:g:G:w:tTeEnyjhHmz")) != -1) {
362: switch (ch) {
363: case 'T':
364: flags |= MTRANSPOSE;
365: /* FALLTHROUGH */
366: case 't':
367: flags |= TRANSPOSE;
368: break;
369: case 'c': /* input col. separator */
370: flags |= ONEISEPONLY;
371: /* FALLTHROUGH */
372: case 's': /* one or more allowed */
373: if (optarg == NULL)
374: isep = '\t'; /* default is ^I */
375: else if (optarg[1] != '\0')
376: usage(); /* single char only */
377: else
378: isep = *optarg;
379: break;
380: case 'C':
381: flags |= ONEOSEPONLY;
382: /* FALLTHROUGH */
383: case 'S':
384: if (optarg == NULL)
385: osep = '\t'; /* default is ^I */
386: else if (optarg[1] != '\0')
387: usage(); /* single char only */
388: else
389: osep = *optarg;
390: break;
391: case 'w': /* window width, default 80 */
1.14 millert 392: owidth = strtonum(optarg, 1, INT_MAX, &errstr);
393: if (errstr) {
394: warnx("width %s", errstr);
1.12 millert 395: usage();
396: }
397: break;
398: case 'K': /* skip N lines */
399: flags |= SKIPPRINT;
400: /* FALLTHROUGH */
401: case 'k': /* skip, do not print */
1.14 millert 402: skip = strtonum(optarg, 0, INT_MAX, &errstr);
403: if (errstr) {
404: warnx("skip value %s", errstr);
405: usage();
406: }
407: if (skip == 0)
1.12 millert 408: skip = 1;
409: break;
410: case 'm':
411: flags |= NOTRIMENDCOL;
412: break;
1.14 millert 413: case 'g': /* gutter width */
414: gutter = strtonum(optarg, 0, INT_MAX, &errstr);
415: if (errstr) {
416: warnx("gutter width %s", errstr);
417: usage();
418: }
1.12 millert 419: break;
420: case 'G':
1.14 millert 421: propgutter = strtonum(optarg, 0, INT_MAX, &errstr);
422: if (errstr) {
423: warnx("gutter proportion %s", errstr);
424: usage();
425: }
1.12 millert 426: break;
427: case 'e': /* each line is an entry */
428: flags |= ONEPERLINE;
429: break;
430: case 'E':
431: flags |= ONEPERCHAR;
432: break;
433: case 'j': /* right adjust */
434: flags |= RIGHTADJUST;
435: break;
436: case 'n': /* null padding for missing values */
437: flags |= NULLPAD;
438: break;
439: case 'y':
440: flags |= RECYCLE;
441: break;
442: case 'H': /* print shape only */
443: flags |= DETAILSHAPE;
444: /* FALLTHROUGH */
445: case 'h':
446: flags |= SHAPEONLY;
447: break;
448: case 'z': /* squeeze col width */
449: flags |= SQUEEZE;
450: break;
451: default:
452: usage();
453: }
1.1 deraadt 454: }
1.12 millert 455: ac -= optind;
456: av += optind;
457:
1.1 deraadt 458: switch (ac) {
459: case 2:
1.14 millert 460: ocols = strtonum(av[1], 0, INT_MAX, &errstr);
461: if (errstr) {
462: warnx("columns value %s", errstr);
463: usage();
464: }
1.17 moritz 465: /* FALLTHROUGH */
1.1 deraadt 466: case 1:
1.14 millert 467: orows = strtonum(av[0], 0, INT_MAX, &errstr);
468: if (errstr) {
469: warnx("columns value %s", errstr);
470: usage();
471: }
1.17 moritz 472: /* FALLTHROUGH */
1.1 deraadt 473: case 0:
474: break;
475: default:
1.12 millert 476: usage();
1.1 deraadt 477: }
478: }