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