rs.c revision 1.8 1 /* $NetBSD: rs.c,v 1.8 2002/06/11 06:06:20 itojun Exp $ */
2
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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93";
45 #else
46 __RCSID("$NetBSD: rs.c,v 1.8 2002/06/11 06:06:20 itojun Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * rs - reshape a data array
52 * Author: John Kunze, Office of Comp. Affairs, UCB
53 * BEWARE: lots of unfinished edges
54 */
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <stdarg.h>
62
63 long flags;
64 #define TRANSPOSE 000001
65 #define MTRANSPOSE 000002
66 #define ONEPERLINE 000004
67 #define ONEISEPONLY 000010
68 #define ONEOSEPONLY 000020
69 #define NOTRIMENDCOL 000040
70 #define SQUEEZE 000100
71 #define SHAPEONLY 000200
72 #define DETAILSHAPE 000400
73 #define RIGHTADJUST 001000
74 #define NULLPAD 002000
75 #define RECYCLE 004000
76 #define SKIPPRINT 010000
77 #define ICOLBOUNDS 020000
78 #define OCOLBOUNDS 040000
79 #define ONEPERCHAR 0100000
80 #define NOARGS 0200000
81
82 short *colwidths;
83 short *cord;
84 short *icbd;
85 short *ocbd;
86 int nelem;
87 char **elem;
88 char **endelem;
89 char *curline;
90 int allocsize = BUFSIZ;
91 int curlen;
92 int irows, icols;
93 int orows, ocols;
94 int maxlen;
95 int skip;
96 int propgutter;
97 char isep = ' ', osep = ' ';
98 int owidth = 80, gutter = 2;
99
100 void usage __P((char *, ...))
101 __attribute__((__format__(__printf__, 1, 2)));
102 void getargs __P((int, char *[]));
103 void getfile __P((void));
104 int getline __P((void));
105 char *getlist __P((short **, char *));
106 char *getnum __P((int *, char *, int));
107 char **getptrs __P((char **));
108 int main __P((int, char **));
109 void prepfile __P((void));
110 void prints __P((char *, int));
111 void putfile __P((void));
112
113 #define INCR(ep) do { \
114 if (++ep >= endelem) \
115 ep = getptrs(ep); \
116 } while(0)
117
118 int
119 main(argc, argv)
120 int argc;
121 char *argv[];
122 {
123 getargs(argc, argv);
124 getfile();
125 if (flags & SHAPEONLY) {
126 printf("%d %d\n", irows, icols);
127 exit(0);
128 }
129 prepfile();
130 putfile();
131 exit(0);
132 }
133
134 void
135 getfile()
136 {
137 char *p;
138 char *endp;
139 char **ep = 0;
140 int multisep = (flags & ONEISEPONLY ? 0 : 1);
141 int nullpad = flags & NULLPAD;
142 char **padto;
143
144 while (skip--) {
145 getline();
146 if (flags & SKIPPRINT)
147 puts(curline);
148 }
149 getline();
150 if (flags & NOARGS && curlen < owidth)
151 flags |= ONEPERLINE;
152 if (flags & ONEPERLINE)
153 icols = 1;
154 else /* count cols on first line */
155 for (p = curline, endp = curline + curlen; p < endp; p++) {
156 if (*p == isep && multisep)
157 continue;
158 icols++;
159 while (*p && *p != isep)
160 p++;
161 }
162 ep = getptrs(elem);
163 p = curline;
164 do {
165 if (flags & ONEPERLINE) {
166 *ep = curline;
167 INCR(ep); /* prepare for next entry */
168 if (maxlen < curlen)
169 maxlen = curlen;
170 irows++;
171 continue;
172 }
173 for (p = curline, endp = curline + curlen; p < endp; p++) {
174 if (*p == isep && multisep)
175 continue; /* eat up column separators */
176 if (*p == isep) /* must be an empty column */
177 *ep = "";
178 else /* store column entry */
179 *ep = p;
180 while (p < endp && *p != isep)
181 p++; /* find end of entry */
182 *p = '\0'; /* mark end of entry */
183 if (maxlen < p - *ep) /* update maxlen */
184 maxlen = p - *ep;
185 INCR(ep); /* prepare for next entry */
186 }
187 irows++; /* update row count */
188 if (nullpad) { /* pad missing entries */
189 padto = elem + irows * icols;
190 while (ep < padto) {
191 *ep = "";
192 INCR(ep);
193 }
194 }
195 } while (getline() != EOF);
196 *ep = 0; /* mark end of pointers */
197 nelem = ep - elem;
198 }
199
200 void
201 putfile()
202 {
203 char **ep;
204 int i, j, n;
205
206 ep = elem;
207 if (flags & TRANSPOSE) {
208 for (i = 0; i < orows; i++) {
209 for (j = i; j < nelem; j += orows)
210 prints(ep[j], (j - i) / orows);
211 putchar('\n');
212 }
213 } else {
214 for (n = 0, i = 0; i < orows && n < nelem; i++) {
215 for (j = 0; j < ocols; j++) {
216 if (n++ >= nelem)
217 break;
218 prints(*ep++, j);
219 }
220 putchar('\n');
221 }
222 }
223 }
224
225 void
226 prints(s, col)
227 char *s;
228 int col;
229 {
230 int n;
231 char *p = s;
232
233 while (*p)
234 p++;
235 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
236 if (flags & RIGHTADJUST)
237 while (n-- > 0)
238 putchar(osep);
239 for (p = s; *p; p++)
240 putchar(*p);
241 while (n-- > 0)
242 putchar(osep);
243 }
244
245 void
246 usage(char *msg, ...)
247 {
248 va_list ap;
249
250 va_start(ap, msg);
251 vwarnx(msg, ap);
252 va_end(ap);
253 fprintf(stderr,
254 "Usage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n");
255 exit(1);
256 }
257
258 void
259 prepfile()
260 {
261 char **ep;
262 int i;
263 int j;
264 char **lp;
265 int colw;
266 int max = 0;
267 int n;
268
269 ep = NULL;
270 if (!nelem)
271 exit(0);
272 gutter += maxlen * propgutter / 100.0;
273 colw = maxlen + gutter;
274 if (flags & MTRANSPOSE) {
275 orows = icols;
276 ocols = irows;
277 }
278 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
279 ocols = owidth / colw;
280 if (ocols == 0) {
281 warnx("Display width %d is less than column width %d", owidth, colw);
282 ocols = 1;
283 }
284 if (ocols > nelem)
285 ocols = nelem;
286 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
287 }
288 else if (orows == 0) /* decide on rows */
289 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
290 else if (ocols == 0) /* decide on cols */
291 ocols = nelem / orows + (nelem % orows ? 1 : 0);
292 lp = elem + orows * ocols;
293 while (lp > endelem) {
294 getptrs(elem + nelem);
295 lp = elem + orows * ocols;
296 }
297 if (flags & RECYCLE) {
298 for (ep = elem + nelem; ep < lp; ep++)
299 *ep = *(ep - nelem);
300 nelem = lp - elem;
301 }
302 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
303 errx(1, "malloc: No gutter space");
304 if (flags & SQUEEZE) {
305 if (flags & TRANSPOSE)
306 for (ep = elem, i = 0; i < ocols; i++) {
307 for (j = 0; j < orows; j++)
308 if ((n = strlen(*ep++)) > max)
309 max = n;
310 colwidths[i] = max + gutter;
311 }
312 else
313 for (ep = elem, i = 0; i < ocols; i++) {
314 for (j = i; j < nelem; j += ocols)
315 if ((n = strlen(ep[j])) > max)
316 max = n;
317 colwidths[i] = max + gutter;
318 }
319 }
320 /* for (i = 0; i < orows; i++) {
321 for (j = i; j < nelem; j += orows)
322 prints(ep[j], (j - i) / orows);
323 putchar('\n');
324 }
325 else
326 for (i = 0; i < orows; i++) {
327 for (j = 0; j < ocols; j++)
328 prints(*ep++, j);
329 putchar('\n');
330 }*/
331 else
332 for (i = 0; i < ocols; i++)
333 colwidths[i] = colw;
334 if (!(flags & NOTRIMENDCOL)) {
335 if (flags & RIGHTADJUST)
336 colwidths[0] -= gutter;
337 else
338 colwidths[ocols - 1] = 0;
339 }
340 n = orows * ocols;
341 if (n > nelem && (flags & RECYCLE))
342 nelem = n;
343 /*for (i = 0; i < ocols; i++)
344 fprintf(stderr, "%d ",colwidths[i]);
345 fprintf(stderr, "is colwidths, nelem %d\n", nelem);*/
346 }
347
348 #define BSIZE 2048
349 char ibuf[BSIZE]; /* two screenfuls should do */
350
351 int
352 getline() /* get line; maintain curline, curlen; manage storage */
353 {
354 static int putlength;
355 static char *endblock = ibuf + BSIZE;
356 char *p;
357 int c, i;
358
359 if (!irows) {
360 curline = ibuf;
361 putlength = flags & DETAILSHAPE;
362 }
363 else if (skip <= 0) { /* don't waste storage */
364 curline += curlen + 1;
365 if (putlength) /* print length, recycle storage */
366 printf(" %d line %d\n", curlen, irows);
367 }
368 if (!putlength && endblock - curline < BUFSIZ) { /* need storage */
369 /*ww = endblock-curline; tt += ww;*/
370 /*printf("#wasted %d total %d\n",ww,tt);*/
371 if (!(curline = (char *) malloc(BSIZE)))
372 errx(1, "File too large");
373 endblock = curline + BSIZE;
374 /*printf("#endb %d curline %d\n",endblock,curline);*/
375 }
376 for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++)
377 if ((c = getchar()) == EOF || c == '\n')
378 break;
379 *p = '\0';
380 curlen = i - 1;
381 return(c);
382 }
383
384 char **
385 getptrs(sp)
386 char **sp;
387 {
388 char **p;
389
390 allocsize += allocsize;
391 p = (char **)realloc(elem, allocsize * sizeof(char *));
392 if (p == (char **)0)
393 err(1, "no memory");
394
395 sp += (p - elem);
396 endelem = (elem = p) + allocsize;
397 return(sp);
398 }
399
400 void
401 getargs(ac, av)
402 int ac;
403 char *av[];
404 {
405 char *p;
406
407 if (ac == 1) {
408 flags |= NOARGS | TRANSPOSE;
409 }
410 while (--ac && **++av == '-')
411 for (p = *av+1; *p; p++)
412 switch (*p) {
413 case 'T':
414 flags |= MTRANSPOSE;
415 case 't':
416 flags |= TRANSPOSE;
417 break;
418 case 'c': /* input col. separator */
419 flags |= ONEISEPONLY;
420 case 's': /* one or more allowed */
421 if (p[1])
422 isep = *++p;
423 else
424 isep = '\t'; /* default is ^I */
425 break;
426 case 'C':
427 flags |= ONEOSEPONLY;
428 case 'S':
429 if (p[1])
430 osep = *++p;
431 else
432 osep = '\t'; /* default is ^I */
433 break;
434 case 'w': /* window width, default 80 */
435 p = getnum(&owidth, p, 0);
436 if (owidth <= 0)
437 usage("Width must be a positive integer");
438 break;
439 case 'K': /* skip N lines */
440 flags |= SKIPPRINT;
441 case 'k': /* skip, do not print */
442 p = getnum(&skip, p, 0);
443 if (!skip)
444 skip = 1;
445 break;
446 case 'm':
447 flags |= NOTRIMENDCOL;
448 break;
449 case 'g': /* gutter space */
450 p = getnum(&gutter, p, 0);
451 break;
452 case 'G':
453 p = getnum(&propgutter, p, 0);
454 break;
455 case 'e': /* each line is an entry */
456 flags |= ONEPERLINE;
457 break;
458 case 'E':
459 flags |= ONEPERCHAR;
460 break;
461 case 'j': /* right adjust */
462 flags |= RIGHTADJUST;
463 break;
464 case 'n': /* null padding for missing values */
465 flags |= NULLPAD;
466 break;
467 case 'y':
468 flags |= RECYCLE;
469 break;
470 case 'H': /* print shape only */
471 flags |= DETAILSHAPE;
472 case 'h':
473 flags |= SHAPEONLY;
474 break;
475 case 'z': /* squeeze col width */
476 flags |= SQUEEZE;
477 break;
478 /*case 'p':
479 ipagespace = atoi(++p); (default is 1)
480 break;*/
481 case 'o': /* col order */
482 p = getlist(&cord, p);
483 break;
484 case 'b':
485 flags |= ICOLBOUNDS;
486 p = getlist(&icbd, p);
487 break;
488 case 'B':
489 flags |= OCOLBOUNDS;
490 p = getlist(&ocbd, p);
491 break;
492 default:
493 usage("Bad flag: %.1s", p);
494 }
495 /*if (!osep)
496 osep = isep;*/
497 switch (ac) {
498 /*case 3:
499 opages = atoi(av[2]);*/
500 case 2:
501 ocols = atoi(av[1]);
502 case 1:
503 orows = atoi(av[0]);
504 case 0:
505 break;
506 default:
507 usage("Too many arguments.");
508 }
509 }
510
511 char *
512 getlist(list, p)
513 short **list;
514 char *p;
515 {
516 int count = 1;
517 char *t;
518
519 for (t = p + 1; *t; t++) {
520 if (!isdigit(*t))
521 usage("Option %.1s requires a list of unsigned numbers separated by commas", t);
522 count++;
523 while (*t && isdigit(*t))
524 t++;
525 if (*t != ',')
526 break;
527 }
528 if (!(*list = (short *) malloc(count * sizeof(short))))
529 errx(1, "No list space");
530 count = 0;
531 for (t = p + 1; *t; t++) {
532 (*list)[count++] = atoi(t);
533 printf("++ %d ", (*list)[count-1]);
534 fflush(stdout);
535 while (*t && isdigit(*t))
536 t++;
537 if (*t != ',')
538 break;
539 }
540 (*list)[count] = 0;
541 return(t - 1);
542 }
543
544 char *
545 getnum(num, p, strict) /* num = number p points to; if (strict) complain */
546 int *num, strict; /* returns pointer to end of num */
547 char *p;
548 {
549 char *t = p;
550
551 if (!isdigit(*++t)) {
552 if (strict || *t == '-' || *t == '+')
553 usage("Option %.1s requires an unsigned integer", p);
554 *num = 0;
555 return(p);
556 }
557 *num = atoi(t);
558 while (*++t)
559 if (!isdigit(*t))
560 break;
561 return(--t);
562 }
563