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 35 static 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 static char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 44 /* 45 * rs - reshape a data array 46 * Author: John Kunze, Office of Comp. Affairs, UCB 47 * BEWARE: lots of unfinished edges 48 */ 49 50 #include <ctype.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 long flags; 56 #define TRANSPOSE 000001 57 #define MTRANSPOSE 000002 58 #define ONEPERLINE 000004 59 #define ONEISEPONLY 000010 60 #define ONEOSEPONLY 000020 61 #define NOTRIMENDCOL 000040 62 #define SQUEEZE 000100 63 #define SHAPEONLY 000200 64 #define DETAILSHAPE 000400 65 #define RIGHTADJUST 001000 66 #define NULLPAD 002000 67 #define RECYCLE 004000 68 #define SKIPPRINT 010000 69 #define ICOLBOUNDS 020000 70 #define OCOLBOUNDS 040000 71 #define ONEPERCHAR 0100000 72 #define NOARGS 0200000 73 74 short *colwidths; 75 short *cord; 76 short *icbd; 77 short *ocbd; 78 int nelem; 79 char **elem; 80 char **endelem; 81 char *curline; 82 int allocsize = BUFSIZ; 83 int curlen; 84 int irows, icols; 85 int orows, ocols; 86 int maxlen; 87 int skip; 88 int propgutter; 89 char isep = ' ', osep = ' '; 90 int owidth = 80, gutter = 2; 91 92 void usage __P((char *, char *)); 93 void getargs __P((int, char *[])); 94 void getfile __P((void)); 95 int getline __P((void)); 96 char *getlist __P((short **, char *)); 97 char *getnum __P((int *, char *, int)); 98 char **getptrs __P((char **)); 99 void prepfile __P((void)); 100 void prints __P((char *, int)); 101 void putfile __P((void)); 102 103 #define INCR(ep) do { \ 104 if (++ep >= endelem) \ 105 ep = getptrs(ep); \ 106 } while(0) 107 108 int 109 main(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 124 void 125 getfile() 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 INCR(ep); /* prepare for next entry */ 158 if (maxlen < curlen) 159 maxlen = curlen; 160 irows++; 161 continue; 162 } 163 for (p = curline, endp = curline + curlen; p < endp; p++) { 164 if (*p == isep && multisep) 165 continue; /* eat up column separators */ 166 if (*p == isep) /* must be an empty column */ 167 *ep = ""; 168 else /* store column entry */ 169 *ep = p; 170 while (p < endp && *p != isep) 171 p++; /* find end of entry */ 172 *p = '\0'; /* mark end of entry */ 173 if (maxlen < p - *ep) /* update maxlen */ 174 maxlen = p - *ep; 175 INCR(ep); /* prepare for next entry */ 176 } 177 irows++; /* update row count */ 178 if (nullpad) { /* pad missing entries */ 179 padto = elem + irows * icols; 180 while (ep < padto) { 181 *ep = ""; 182 INCR(ep); 183 } 184 } 185 } while (getline() != EOF); 186 *ep = 0; /* mark end of pointers */ 187 nelem = ep - elem; 188 } 189 190 void 191 putfile() 192 { 193 register char **ep; 194 register int i, j, n; 195 196 ep = elem; 197 if (flags & TRANSPOSE) { 198 for (i = 0; i < orows; i++) { 199 for (j = i; j < nelem; j += orows) 200 prints(ep[j], (j - i) / orows); 201 putchar('\n'); 202 } 203 } else { 204 for (n = 0, i = 0; i < orows && n < nelem; i++) { 205 for (j = 0; j < ocols; j++) { 206 if (n++ >= nelem) 207 break; 208 prints(*ep++, j); 209 } 210 putchar('\n'); 211 } 212 } 213 } 214 215 void 216 prints(s, col) 217 char *s; 218 int col; 219 { 220 register int n; 221 register char *p = s; 222 223 while (*p) 224 p++; 225 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s)); 226 if (flags & RIGHTADJUST) 227 while (n-- > 0) 228 putchar(osep); 229 for (p = s; *p; p++) 230 putchar(*p); 231 while (n-- > 0) 232 putchar(osep); 233 } 234 235 void 236 usage(msg, s) 237 char *msg, *s; 238 { 239 warnx(msg, s); 240 fprintf(stderr, 241 "Usage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n"); 242 exit(1); 243 } 244 245 void 246 prepfile() 247 { 248 register char **ep; 249 register int i; 250 register int j; 251 char **lp; 252 int colw; 253 int max = 0; 254 int n; 255 256 if (!nelem) 257 exit(0); 258 gutter += maxlen * propgutter / 100.0; 259 colw = maxlen + gutter; 260 if (flags & MTRANSPOSE) { 261 orows = icols; 262 ocols = irows; 263 } 264 else if (orows == 0 && ocols == 0) { /* decide rows and cols */ 265 ocols = owidth / colw; 266 if (ocols == 0) { 267 warnx("Display width %d is less than column width %d\n", owidth, colw); 268 ocols = 1; 269 } 270 if (ocols > nelem) 271 ocols = nelem; 272 orows = nelem / ocols + (nelem % ocols ? 1 : 0); 273 } 274 else if (orows == 0) /* decide on rows */ 275 orows = nelem / ocols + (nelem % ocols ? 1 : 0); 276 else if (ocols == 0) /* decide on cols */ 277 ocols = nelem / orows + (nelem % orows ? 1 : 0); 278 lp = elem + orows * ocols; 279 while (lp > endelem) { 280 getptrs(elem + nelem); 281 lp = elem + orows * ocols; 282 } 283 if (flags & RECYCLE) { 284 for (ep = elem + nelem; ep < lp; ep++) 285 *ep = *(ep - nelem); 286 nelem = lp - elem; 287 } 288 if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) 289 errx(1, "malloc: No gutter space"); 290 if (flags & SQUEEZE) { 291 if (flags & TRANSPOSE) 292 for (ep = elem, i = 0; i < ocols; i++) { 293 for (j = 0; j < orows; j++) 294 if ((n = strlen(*ep++)) > max) 295 max = n; 296 colwidths[i] = max + gutter; 297 } 298 else 299 for (i = 0; i < ocols; i++) { 300 for (j = i; j < nelem; j += ocols) 301 if ((n = strlen(ep[j])) > max) 302 max = n; 303 colwidths[i] = max + gutter; 304 } 305 } 306 /* for (i = 0; i < orows; i++) { 307 for (j = i; j < nelem; j += orows) 308 prints(ep[j], (j - i) / orows); 309 putchar('\n'); 310 } 311 else 312 for (i = 0; i < orows; i++) { 313 for (j = 0; j < ocols; j++) 314 prints(*ep++, j); 315 putchar('\n'); 316 }*/ 317 else 318 for (i = 0; i < ocols; i++) 319 colwidths[i] = colw; 320 if (!(flags & NOTRIMENDCOL)) { 321 if (flags & RIGHTADJUST) 322 colwidths[0] -= gutter; 323 else 324 colwidths[ocols - 1] = 0; 325 } 326 n = orows * ocols; 327 if (n > nelem && (flags & RECYCLE)) 328 nelem = n; 329 /*for (i = 0; i < ocols; i++) 330 fprintf(stderr, "%d ",colwidths[i]); 331 fprintf(stderr, "is colwidths, nelem %d\n", nelem);*/ 332 } 333 334 #define BSIZE 2048 335 char ibuf[BSIZE]; /* two screenfuls should do */ 336 337 int 338 getline() /* get line; maintain curline, curlen; manage storage */ 339 { 340 static int putlength; 341 static char *endblock = ibuf + BSIZE; 342 register char *p; 343 register int c, i; 344 345 if (!irows) { 346 curline = ibuf; 347 putlength = flags & DETAILSHAPE; 348 } 349 else if (skip <= 0) { /* don't waste storage */ 350 curline += curlen + 1; 351 if (putlength) /* print length, recycle storage */ 352 printf(" %d line %d\n", curlen, irows); 353 } 354 if (!putlength && endblock - curline < BUFSIZ) { /* need storage */ 355 /*ww = endblock-curline; tt += ww;*/ 356 /*printf("#wasted %d total %d\n",ww,tt);*/ 357 if (!(curline = (char *) malloc(BSIZE))) 358 errx(1, "File too large"); 359 endblock = curline + BSIZE; 360 /*printf("#endb %d curline %d\n",endblock,curline);*/ 361 } 362 for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++) 363 if ((c = getchar()) == EOF || c == '\n') 364 break; 365 *p = '\0'; 366 curlen = i - 1; 367 return(c); 368 } 369 370 char ** 371 getptrs(sp) 372 char **sp; 373 { 374 register char **p; 375 376 allocsize += allocsize; 377 p = (char **)realloc(elem, allocsize * sizeof(char *)); 378 if (p == (char **)0) 379 err(1, "no memory"); 380 381 sp += (p - elem); 382 endelem = (elem = p) + allocsize; 383 return(sp); 384 } 385 386 void 387 getargs(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 usage("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("Bad flag: %.1s", p); 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 usage("Too many arguments.", ""); 494 } 495 } 496 497 char * 498 getlist(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 usage("Option %.1s requires a list of unsigned numbers separated by commas", t); 508 count++; 509 while (*t && isdigit(*t)) 510 t++; 511 if (*t != ',') 512 break; 513 } 514 if (!(*list = (short *) malloc(count * sizeof(short)))) 515 errx(1, "No list space"); 516 count = 0; 517 for (t = p + 1; *t; t++) { 518 (*list)[count++] = atoi(t); 519 printf("++ %d ", (*list)[count-1]); 520 fflush(stdout); 521 while (*t && isdigit(*t)) 522 t++; 523 if (*t != ',') 524 break; 525 } 526 (*list)[count] = 0; 527 return(t - 1); 528 } 529 530 char * 531 getnum(num, p, strict) /* num = number p points to; if (strict) complain */ 532 int *num, strict; /* returns pointer to end of num */ 533 char *p; 534 { 535 register char *t = p; 536 537 if (!isdigit(*++t)) { 538 if (strict || *t == '-' || *t == '+') 539 usage("Option %.1s requires an unsigned integer", p); 540 *num = 0; 541 return(p); 542 } 543 *num = atoi(t); 544 while (*++t) 545 if (!isdigit(*t)) 546 break; 547 return(--t); 548 } 549