1 /* $NetBSD: rs.c,v 1.16 2019/02/03 03:19:30 mrg 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.16 2019/02/03 03:19:30 mrg 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 /* FALLTHROUGH */ 404 case 't': 405 flags |= TRANSPOSE; 406 break; 407 case 'c': /* input col. separator */ 408 flags |= ONEISEPONLY; 409 /* FALLTHROUGH */ 410 case 's': /* one or more allowed */ 411 if (p[1]) 412 isep = *++p; 413 else 414 isep = '\t'; /* default is ^I */ 415 break; 416 case 'C': 417 flags |= ONEOSEPONLY; 418 /* FALLTHROUGH */ 419 case 'S': 420 if (p[1]) 421 osep = *++p; 422 else 423 osep = '\t'; /* default is ^I */ 424 break; 425 case 'w': /* window width, default 80 */ 426 p = getnum(&owidth, p, 0); 427 if (owidth <= 0) 428 usage("Width must be a positive integer"); 429 break; 430 case 'K': /* skip N lines */ 431 flags |= SKIPPRINT; 432 /* FALLTHROUGH */ 433 case 'k': /* skip, do not print */ 434 p = getnum(&skip, p, 0); 435 if (!skip) 436 skip = 1; 437 break; 438 case 'm': 439 flags |= NOTRIMENDCOL; 440 break; 441 case 'g': /* gutter space */ 442 p = getnum(&gutter, p, 0); 443 break; 444 case 'G': 445 p = getnum(&propgutter, p, 0); 446 break; 447 case 'e': /* each line is an entry */ 448 flags |= ONEPERLINE; 449 break; 450 case 'E': 451 flags |= ONEPERCHAR; 452 break; 453 case 'j': /* right adjust */ 454 flags |= RIGHTADJUST; 455 break; 456 case 'n': /* null padding for missing values */ 457 flags |= NULLPAD; 458 break; 459 case 'y': 460 flags |= RECYCLE; 461 break; 462 case 'H': /* print shape only */ 463 flags |= DETAILSHAPE; 464 /* FALLTHROUGH */ 465 case 'h': 466 flags |= SHAPEONLY; 467 break; 468 case 'z': /* squeeze col width */ 469 flags |= SQUEEZE; 470 break; 471 /*case 'p': 472 ipagespace = atoi(++p); (default is 1) 473 break;*/ 474 case 'o': /* col order */ 475 p = getlist(&cord, p); 476 break; 477 case 'b': 478 flags |= ICOLBOUNDS; 479 p = getlist(&icbd, p); 480 break; 481 case 'B': 482 flags |= OCOLBOUNDS; 483 p = getlist(&ocbd, p); 484 break; 485 default: 486 usage("Bad flag: %.1s", p); 487 } 488 /*if (!osep) 489 osep = isep;*/ 490 switch (ac) { 491 /*case 3: 492 opages = atoi(av[2]);*/ 493 /* FALLTHROUGH */ 494 case 2: 495 ocols = atoi(av[1]); 496 /* FALLTHROUGH */ 497 case 1: 498 orows = atoi(av[0]); 499 /* FALLTHROUGH */ 500 case 0: 501 break; 502 default: 503 usage("Too many arguments."); 504 } 505 } 506 507 static char * 508 getlist(short **list, char *p) 509 { 510 int count = 1; 511 char *t; 512 513 for (t = p + 1; *t; t++) { 514 if (!isdigit((unsigned char)*t)) 515 usage("Option %.1s requires a list of unsigned numbers separated by commas", t); 516 count++; 517 while (*t && isdigit((unsigned char)*t)) 518 t++; 519 if (*t != ',') 520 break; 521 } 522 if (!(*list = (short *) malloc(count * sizeof(short)))) 523 errx(1, "No list space"); 524 count = 0; 525 for (t = p + 1; *t; t++) { 526 (*list)[count++] = atoi(t); 527 printf("++ %d ", (*list)[count-1]); 528 fflush(stdout); 529 while (*t && isdigit((unsigned char)*t)) 530 t++; 531 if (*t != ',') 532 break; 533 } 534 (*list)[count] = 0; 535 return(t - 1); 536 } 537 538 static char * 539 getnum(int *num, char *p, int strict) /* num = number p points to; if (strict) complain */ 540 /* returns pointer to end of num */ 541 { 542 char *t = p; 543 544 if (!isdigit((unsigned char)*++t)) { 545 if (strict || *t == '-' || *t == '+') 546 usage("Option %.1s requires an unsigned integer", p); 547 *num = 0; 548 return(p); 549 } 550 *num = atoi(t); 551 while (*++t) 552 if (!isdigit((unsigned char)*t)) 553 break; 554 return(--t); 555 } 556