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