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