=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/rs/rs.c,v retrieving revision 1.1 retrieving revision 1.2 diff -c -r1.1 -r1.2 *** src/usr.bin/rs/rs.c 1995/10/18 08:46:03 1.1 --- src/usr.bin/rs/rs.c 1996/05/21 21:37:11 1.2 *************** *** 89,95 **** char isep = ' ', osep = ' '; int owidth = 80, gutter = 2; ! void error __P((char *, char *)); void getargs __P((int, char *[])); void getfile __P((void)); int getline __P((void)); --- 89,95 ---- char isep = ' ', osep = ' '; int owidth = 80, gutter = 2; ! void usage __P((char *, char *)); void getargs __P((int, char *[])); void getfile __P((void)); int getline __P((void)); *************** *** 100,105 **** --- 100,110 ---- void prints __P((char *, int)); void putfile __P((void)); + #define INCR(ep) do { \ + if (++ep >= endelem) \ + ep = getptrs(ep); \ + } while(0) + int main(argc, argv) int argc; *************** *** 148,154 **** p = curline; do { if (flags & ONEPERLINE) { ! *ep++ = curline; if (maxlen < curlen) maxlen = curlen; irows++; --- 153,160 ---- p = curline; do { if (flags & ONEPERLINE) { ! *ep = curline; ! INCR(ep); /* prepare for next entry */ if (maxlen < curlen) maxlen = curlen; irows++; *************** *** 166,181 **** *p = '\0'; /* mark end of entry */ if (maxlen < p - *ep) /* update maxlen */ maxlen = p - *ep; ! ep++; /* prepare for next entry */ } irows++; /* update row count */ if (nullpad) { /* pad missing entries */ padto = elem + irows * icols; ! while (ep < padto) ! *ep++ = ""; } - if (ep > endelem) /* if low on pointers */ - ep = getptrs(ep); /* get some more */ } while (getline() != EOF); *ep = 0; /* mark end of pointers */ nelem = ep - elem; --- 172,187 ---- *p = '\0'; /* mark end of entry */ if (maxlen < p - *ep) /* update maxlen */ maxlen = p - *ep; ! INCR(ep); /* prepare for next entry */ } irows++; /* update row count */ if (nullpad) { /* pad missing entries */ padto = elem + irows * icols; ! while (ep < padto) { ! *ep = ""; ! INCR(ep); ! } } } while (getline() != EOF); *ep = 0; /* mark end of pointers */ nelem = ep - elem; *************** *** 185,205 **** putfile() { register char **ep; ! register int i, j; ep = elem; ! if (flags & TRANSPOSE) for (i = 0; i < orows; i++) { for (j = i; j < nelem; j += orows) prints(ep[j], (j - i) / orows); putchar('\n'); } ! else ! for (i = 0; i < orows; i++) { ! for (j = 0; j < ocols; j++) prints(*ep++, j); putchar('\n'); } } void --- 191,215 ---- putfile() { register char **ep; ! register int i, j, n; ep = elem; ! if (flags & TRANSPOSE) { for (i = 0; i < orows; i++) { for (j = i; j < nelem; j += orows) prints(ep[j], (j - i) / orows); putchar('\n'); } ! } else { ! for (n = 0, i = 0; i < orows && n < nelem; i++) { ! for (j = 0; j < ocols; j++) { ! if (n++ >= nelem) ! break; prints(*ep++, j); + } putchar('\n'); } + } } void *************** *** 223,235 **** } void ! error(msg, s) char *msg, *s; { ! fprintf(stderr, "rs: "); ! fprintf(stderr, msg, s); fprintf(stderr, ! "\nUsage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n"); exit(1); } --- 233,244 ---- } void ! usage(msg, s) char *msg, *s; { ! warnx(msg, s); fprintf(stderr, ! "Usage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n"); exit(1); } *************** *** 254,261 **** } else if (orows == 0 && ocols == 0) { /* decide rows and cols */ ocols = owidth / colw; ! if (ocols == 0) ! fprintf(stderr, "Display width %d is less than column width %d\n", owidth, colw); if (ocols > nelem) ocols = nelem; orows = nelem / ocols + (nelem % ocols ? 1 : 0); --- 263,272 ---- } else if (orows == 0 && ocols == 0) { /* decide rows and cols */ ocols = owidth / colw; ! if (ocols == 0) { ! warnx("Display width %d is less than column width %d\n", owidth, colw); ! ocols = 1; ! } if (ocols > nelem) ocols = nelem; orows = nelem / ocols + (nelem % ocols ? 1 : 0); *************** *** 275,281 **** nelem = lp - elem; } if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) ! error("malloc: No gutter space", ""); if (flags & SQUEEZE) { if (flags & TRANSPOSE) for (ep = elem, i = 0; i < ocols; i++) { --- 286,292 ---- nelem = lp - elem; } if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) ! errx(1, "malloc: No gutter space"); if (flags & SQUEEZE) { if (flags & TRANSPOSE) for (ep = elem, i = 0; i < ocols; i++) { *************** *** 344,350 **** /*ww = endblock-curline; tt += ww;*/ /*printf("#wasted %d total %d\n",ww,tt);*/ if (!(curline = (char *) malloc(BSIZE))) ! error("File too large", ""); endblock = curline + BSIZE; /*printf("#endb %d curline %d\n",endblock,curline);*/ } --- 355,361 ---- /*ww = endblock-curline; tt += ww;*/ /*printf("#wasted %d total %d\n",ww,tt);*/ if (!(curline = (char *) malloc(BSIZE))) ! errx(1, "File too large"); endblock = curline + BSIZE; /*printf("#endb %d curline %d\n",endblock,curline);*/ } *************** *** 360,385 **** getptrs(sp) char **sp; { ! register char **p, **ep; ! for (;;) { ! allocsize += allocsize; ! if (!(p = (char **) malloc(allocsize * sizeof(char *)))) { ! perror("rs"); ! exit(1); ! } ! if ((endelem = p + allocsize - icols) <= p) { ! free(p); ! continue; ! } ! if (elem != 0) ! free(elem); ! ep = elem; ! elem = p; ! while (ep < sp) ! *p++ = *ep++; ! return(p); ! } } void --- 371,386 ---- getptrs(sp) char **sp; { ! register char **p; ! allocsize += allocsize; ! p = (char **)realloc(elem, allocsize * sizeof(char *)); ! if (p == (char **)0) ! err(1, "no memory"); ! ! sp += (p - elem); ! endelem = (elem = p) + allocsize; ! return(sp); } void *************** *** 419,425 **** case 'w': /* window width, default 80 */ p = getnum(&owidth, p, 0); if (owidth <= 0) ! error("Width must be a positive integer", ""); break; case 'K': /* skip N lines */ flags |= SKIPPRINT; --- 420,426 ---- case 'w': /* window width, default 80 */ p = getnum(&owidth, p, 0); if (owidth <= 0) ! usage("Width must be a positive integer", ""); break; case 'K': /* skip N lines */ flags |= SKIPPRINT; *************** *** 475,481 **** p = getlist(&ocbd, p); break; default: ! error("Bad flag: %.1s", p); } /*if (!osep) osep = isep;*/ --- 476,482 ---- p = getlist(&ocbd, p); break; default: ! usage("Bad flag: %.1s", p); } /*if (!osep) osep = isep;*/ *************** *** 489,495 **** case 0: break; default: ! error("Too many arguments. What do you mean by `%s'?", av[3]); } } --- 490,496 ---- case 0: break; default: ! usage("Too many arguments.", ""); } } *************** *** 503,509 **** for (t = p + 1; *t; t++) { if (!isdigit(*t)) ! error("Option %.1s requires a list of unsigned numbers separated by commas", t); count++; while (*t && isdigit(*t)) t++; --- 504,510 ---- for (t = p + 1; *t; t++) { if (!isdigit(*t)) ! usage("Option %.1s requires a list of unsigned numbers separated by commas", t); count++; while (*t && isdigit(*t)) t++; *************** *** 511,517 **** break; } if (!(*list = (short *) malloc(count * sizeof(short)))) ! error("No list space", ""); count = 0; for (t = p + 1; *t; t++) { (*list)[count++] = atoi(t); --- 512,518 ---- break; } if (!(*list = (short *) malloc(count * sizeof(short)))) ! errx(1, "No list space"); count = 0; for (t = p + 1; *t; t++) { (*list)[count++] = atoi(t); *************** *** 535,541 **** if (!isdigit(*++t)) { if (strict || *t == '-' || *t == '+') ! error("Option %.1s requires an unsigned integer", p); *num = 0; return(p); } --- 536,542 ---- if (!isdigit(*++t)) { if (strict || *t == '-' || *t == '+') ! usage("Option %.1s requires an unsigned integer", p); *num = 0; return(p); }