rs.c revision 33648
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#ifndef lint 35static const char copyright[] = 36"@(#) 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 42static char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93"; 43#endif 44static const char rcsid[] = 45 "$Id: rs.c,v 1.3 1997/08/07 06:42:37 charnier Exp $"; 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 <err.h> 55#include <ctype.h> 56#include <stdio.h> 57#include <stdlib.h> 58#include <string.h> 59 60long flags; 61#define TRANSPOSE 000001 62#define MTRANSPOSE 000002 63#define ONEPERLINE 000004 64#define ONEISEPONLY 000010 65#define ONEOSEPONLY 000020 66#define NOTRIMENDCOL 000040 67#define SQUEEZE 000100 68#define SHAPEONLY 000200 69#define DETAILSHAPE 000400 70#define RIGHTADJUST 001000 71#define NULLPAD 002000 72#define RECYCLE 004000 73#define SKIPPRINT 010000 74#define ICOLBOUNDS 020000 75#define OCOLBOUNDS 040000 76#define ONEPERCHAR 0100000 77#define NOARGS 0200000 78 79short *colwidths; 80short *cord; 81short *icbd; 82short *ocbd; 83int nelem; 84char **elem; 85char **endelem; 86char *curline; 87int allocsize = BUFSIZ; 88int curlen; 89int irows, icols; 90int orows, ocols; 91int maxlen; 92int skip; 93int propgutter; 94char isep = ' ', osep = ' '; 95int owidth = 80, gutter = 2; 96 97void getargs __P((int, char *[])); 98void getfile __P((void)); 99int getline __P((void)); 100char *getlist __P((short **, char *)); 101char *getnum __P((int *, char *, int)); 102char **getptrs __P((char **)); 103void prepfile __P((void)); 104void prints __P((char *, int)); 105void putfile __P((void)); 106static void usage __P((void)); 107 108int 109main(argc, argv) 110 int argc; 111 char *argv[]; 112{ 113 getargs(argc, argv); 114 getfile(); 115 if (flags & SHAPEONLY) { 116 printf("%d %d\n", irows, icols); 117 exit(0); 118 } 119 prepfile(); 120 putfile(); 121 exit(0); 122} 123 124void 125getfile() 126{ 127 register char *p; 128 register char *endp; 129 register char **ep = 0; 130 int multisep = (flags & ONEISEPONLY ? 0 : 1); 131 int nullpad = flags & NULLPAD; 132 char **padto; 133 134 while (skip--) { 135 getline(); 136 if (flags & SKIPPRINT) 137 puts(curline); 138 } 139 getline(); 140 if (flags & NOARGS && curlen < owidth) 141 flags |= ONEPERLINE; 142 if (flags & ONEPERLINE) 143 icols = 1; 144 else /* count cols on first line */ 145 for (p = curline, endp = curline + curlen; p < endp; p++) { 146 if (*p == isep && multisep) 147 continue; 148 icols++; 149 while (*p && *p != isep) 150 p++; 151 } 152 ep = getptrs(elem); 153 p = curline; 154 do { 155 if (flags & ONEPERLINE) { 156 *ep++ = curline; 157 if (maxlen < curlen) 158 maxlen = curlen; 159 irows++; 160 continue; 161 } 162 for (p = curline, endp = curline + curlen; p < endp; p++) { 163 if (*p == isep && multisep) 164 continue; /* eat up column separators */ 165 if (*p == isep) /* must be an empty column */ 166 *ep = ""; 167 else /* store column entry */ 168 *ep = p; 169 while (p < endp && *p != isep) 170 p++; /* find end of entry */ 171 *p = '\0'; /* mark end of entry */ 172 if (maxlen < p - *ep) /* update maxlen */ 173 maxlen = p - *ep; 174 ep++; /* prepare for next entry */ 175 } 176 irows++; /* update row count */ 177 if (nullpad) { /* pad missing entries */ 178 padto = elem + irows * icols; 179 while (ep < padto) 180 *ep++ = ""; 181 } 182 if (ep > endelem) /* if low on pointers */ 183 ep = getptrs(ep); /* get some more */ 184 } while (getline() != EOF); 185 *ep = 0; /* mark end of pointers */ 186 nelem = ep - elem; 187} 188 189void 190putfile() 191{ 192 register char **ep; 193 register int i, j, k; 194 195 ep = elem; 196 if (flags & TRANSPOSE) 197 for (i = 0; i < orows; i++) { 198 for (j = i; j < nelem; j += orows) 199 prints(ep[j], (j - i) / orows); 200 putchar('\n'); 201 } 202 else 203 for (i = k = 0; i < orows; i++) { 204 for (j = 0; j < ocols; j++, k++) 205 if (k < nelem) 206 prints(ep[k], j); 207 putchar('\n'); 208 } 209} 210 211void 212prints(s, col) 213 char *s; 214 int col; 215{ 216 register int n; 217 register char *p = s; 218 219 while (*p) 220 p++; 221 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s)); 222 if (flags & RIGHTADJUST) 223 while (n-- > 0) 224 putchar(osep); 225 for (p = s; *p; p++) 226 putchar(*p); 227 while (n-- > 0) 228 putchar(osep); 229} 230 231static void 232usage() 233{ 234 fprintf(stderr, 235 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n"); 236 exit(1); 237} 238 239void 240prepfile() 241{ 242 register char **ep; 243 register int i; 244 register int j; 245 char **lp; 246 int colw; 247 int max = 0; 248 int n; 249 250 if (!nelem) 251 exit(0); 252 gutter += maxlen * propgutter / 100.0; 253 colw = maxlen + gutter; 254 if (flags & MTRANSPOSE) { 255 orows = icols; 256 ocols = irows; 257 } 258 else if (orows == 0 && ocols == 0) { /* decide rows and cols */ 259 ocols = owidth / colw; 260 if (ocols == 0) 261 warnx("display width %d is less than column width %d", 262 owidth, colw); 263 if (ocols > nelem) 264 ocols = nelem; 265 orows = nelem / ocols + (nelem % ocols ? 1 : 0); 266 } 267 else if (orows == 0) /* decide on rows */ 268 orows = nelem / ocols + (nelem % ocols ? 1 : 0); 269 else if (ocols == 0) /* decide on cols */ 270 ocols = nelem / orows + (nelem % orows ? 1 : 0); 271 lp = elem + orows * ocols; 272 while (lp > endelem) { 273 getptrs(elem + nelem); 274 lp = elem + orows * ocols; 275 } 276 if (flags & RECYCLE) { 277 for (ep = elem + nelem; ep < lp; ep++) 278 *ep = *(ep - nelem); 279 nelem = lp - elem; 280 } 281 if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) 282 errx(1, "malloc"); 283 if (flags & SQUEEZE) { 284 if (flags & TRANSPOSE) 285 for (ep = elem, i = 0; i < ocols; i++) { 286 for (j = 0; j < orows; j++) 287 if ((n = strlen(*ep++)) > max) 288 max = n; 289 colwidths[i] = max + gutter; 290 } 291 else 292 for (i = 0; i < ocols; i++) { 293 for (j = i; j < nelem; j += ocols) 294 if ((n = strlen(ep[j])) > max) 295 max = n; 296 colwidths[i] = max + gutter; 297 } 298 } 299 /* for (i = 0; i < orows; i++) { 300 for (j = i; j < nelem; j += orows) 301 prints(ep[j], (j - i) / orows); 302 putchar('\n'); 303 } 304 else 305 for (i = 0; i < orows; i++) { 306 for (j = 0; j < ocols; j++) 307 prints(*ep++, j); 308 putchar('\n'); 309 }*/ 310 else 311 for (i = 0; i < ocols; i++) 312 colwidths[i] = colw; 313 if (!(flags & NOTRIMENDCOL)) { 314 if (flags & RIGHTADJUST) 315 colwidths[0] -= gutter; 316 else 317 colwidths[ocols - 1] = 0; 318 } 319 n = orows * ocols; 320 if (n > nelem && (flags & RECYCLE)) 321 nelem = n; 322 /*for (i = 0; i < ocols; i++) 323 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/ 324} 325 326#define BSIZE 2048 327char ibuf[BSIZE]; /* two screenfuls should do */ 328 329int 330getline() /* get line; maintain curline, curlen; manage storage */ 331{ 332 static int putlength; 333 static char *endblock = ibuf + BSIZE; 334 register char *p; 335 register int c, i; 336 337 if (!irows) { 338 curline = ibuf; 339 putlength = flags & DETAILSHAPE; 340 } 341 else if (skip <= 0) { /* don't waste storage */ 342 curline += curlen + 1; 343 if (putlength) /* print length, recycle storage */ 344 printf(" %d line %d\n", curlen, irows); 345 } 346 if (!putlength && endblock - curline < BUFSIZ) { /* need storage */ 347 /*ww = endblock-curline; tt += ww;*/ 348 /*printf("#wasted %d total %d\n",ww,tt);*/ 349 if (!(curline = (char *) malloc(BSIZE))) 350 errx(1, "file too large"); 351 endblock = curline + BSIZE; 352 /*printf("#endb %d curline %d\n",endblock,curline);*/ 353 } 354 for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++) 355 if ((c = getchar()) == EOF || c == '\n') 356 break; 357 *p = '\0'; 358 curlen = i - 1; 359 return(c); 360} 361 362char ** 363getptrs(sp) 364 char **sp; 365{ 366 register char **p, **ep; 367 368 for (;;) { 369 allocsize += allocsize; 370 if (!(p = (char **) malloc(allocsize * sizeof(char *)))) 371 errx(1, "malloc"); 372 if ((endelem = p + allocsize - icols) <= p) { 373 free(p); 374 continue; 375 } 376 if (elem != 0) 377 free(elem); 378 ep = elem; 379 elem = p; 380 while (ep < sp) 381 *p++ = *ep++; 382 return(p); 383 } 384} 385 386void 387getargs(ac, av) 388 int ac; 389 char *av[]; 390{ 391 register char *p; 392 393 if (ac == 1) { 394 flags |= NOARGS | TRANSPOSE; 395 } 396 while (--ac && **++av == '-') 397 for (p = *av+1; *p; p++) 398 switch (*p) { 399 case 'T': 400 flags |= MTRANSPOSE; 401 case 't': 402 flags |= TRANSPOSE; 403 break; 404 case 'c': /* input col. separator */ 405 flags |= ONEISEPONLY; 406 case 's': /* one or more allowed */ 407 if (p[1]) 408 isep = *++p; 409 else 410 isep = '\t'; /* default is ^I */ 411 break; 412 case 'C': 413 flags |= ONEOSEPONLY; 414 case 'S': 415 if (p[1]) 416 osep = *++p; 417 else 418 osep = '\t'; /* default is ^I */ 419 break; 420 case 'w': /* window width, default 80 */ 421 p = getnum(&owidth, p, 0); 422 if (owidth <= 0) 423 errx(1, "width must be a positive integer"); 424 break; 425 case 'K': /* skip N lines */ 426 flags |= SKIPPRINT; 427 case 'k': /* skip, do not print */ 428 p = getnum(&skip, p, 0); 429 if (!skip) 430 skip = 1; 431 break; 432 case 'm': 433 flags |= NOTRIMENDCOL; 434 break; 435 case 'g': /* gutter space */ 436 p = getnum(&gutter, p, 0); 437 break; 438 case 'G': 439 p = getnum(&propgutter, p, 0); 440 break; 441 case 'e': /* each line is an entry */ 442 flags |= ONEPERLINE; 443 break; 444 case 'E': 445 flags |= ONEPERCHAR; 446 break; 447 case 'j': /* right adjust */ 448 flags |= RIGHTADJUST; 449 break; 450 case 'n': /* null padding for missing values */ 451 flags |= NULLPAD; 452 break; 453 case 'y': 454 flags |= RECYCLE; 455 break; 456 case 'H': /* print shape only */ 457 flags |= DETAILSHAPE; 458 case 'h': 459 flags |= SHAPEONLY; 460 break; 461 case 'z': /* squeeze col width */ 462 flags |= SQUEEZE; 463 break; 464 /*case 'p': 465 ipagespace = atoi(++p); (default is 1) 466 break;*/ 467 case 'o': /* col order */ 468 p = getlist(&cord, p); 469 break; 470 case 'b': 471 flags |= ICOLBOUNDS; 472 p = getlist(&icbd, p); 473 break; 474 case 'B': 475 flags |= OCOLBOUNDS; 476 p = getlist(&ocbd, p); 477 break; 478 default: 479 usage(); 480 } 481 /*if (!osep) 482 osep = isep;*/ 483 switch (ac) { 484 /*case 3: 485 opages = atoi(av[2]);*/ 486 case 2: 487 ocols = atoi(av[1]); 488 case 1: 489 orows = atoi(av[0]); 490 case 0: 491 break; 492 default: 493 errx(1, "too many arguments"); 494 } 495} 496 497char * 498getlist(list, p) 499 short **list; 500 char *p; 501{ 502 register int count = 1; 503 register char *t; 504 505 for (t = p + 1; *t; t++) { 506 if (!isdigit(*t)) 507 errx(1, 508 "option %.1s requires a list of unsigned numbers separated by commas", t); 509 count++; 510 while (*t && isdigit(*t)) 511 t++; 512 if (*t != ',') 513 break; 514 } 515 if (!(*list = (short *) malloc(count * sizeof(short)))) 516 errx(1, "no list space"); 517 count = 0; 518 for (t = p + 1; *t; t++) { 519 (*list)[count++] = atoi(t); 520 printf("++ %d ", (*list)[count-1]); 521 fflush(stdout); 522 while (*t && isdigit(*t)) 523 t++; 524 if (*t != ',') 525 break; 526 } 527 (*list)[count] = 0; 528 return(t - 1); 529} 530 531char * 532getnum(num, p, strict) /* num = number p points to; if (strict) complain */ 533 int *num, strict; /* returns pointer to end of num */ 534 char *p; 535{ 536 register char *t = p; 537 538 if (!isdigit(*++t)) { 539 if (strict || *t == '-' || *t == '+') 540 errx(1, "option %.1s requires an unsigned integer", p); 541 *num = 0; 542 return(p); 543 } 544 *num = atoi(t); 545 while (*++t) 546 if (!isdigit(*t)) 547 break; 548 return(--t); 549} 550