]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/rs/rs.c
MFC r362623:
[FreeBSD/stable/8.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 static const 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 <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52
53 #include <err.h>
54 #include <ctype.h>
55 #include <limits.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 = 0, ocols = 0;
91 int     maxlen;
92 int     skip;
93 int     propgutter;
94 char    isep = ' ', osep = ' ';
95 char    blank[] = "";
96 int     owidth = 80, gutter = 2;
97
98 void      getargs(int, char *[]);
99 void      getfile(void);
100 int       getline(void);
101 char     *getlist(short **, char *);
102 char     *getnum(int *, char *, int);
103 char    **getptrs(char **);
104 void      prepfile(void);
105 void      prints(char *, int);
106 void      putfile(void);
107 static void usage(void);
108
109 #define INCR(ep) do {                   \
110         if (++ep >= endelem)            \
111                 ep = getptrs(ep);       \
112 } while(0)
113
114 int
115 main(int argc, char *argv[])
116 {
117         getargs(argc, argv);
118         getfile();
119         if (flags & SHAPEONLY) {
120                 printf("%d %d\n", irows, icols);
121                 exit(0);
122         }
123         prepfile();
124         putfile();
125         exit(0);
126 }
127
128 void
129 getfile(void)
130 {
131         char *p;
132         char *endp;
133         char **ep;
134         int c;
135         int multisep = (flags & ONEISEPONLY ? 0 : 1);
136         int nullpad = flags & NULLPAD;
137         char **padto;
138
139         while (skip--) {
140                 c = getline();
141                 if (flags & SKIPPRINT)
142                         puts(curline);
143                 if (c == EOF)
144                         return;
145         }
146         getline();
147         if (flags & NOARGS && curlen < owidth)
148                 flags |= ONEPERLINE;
149         if (flags & ONEPERLINE)
150                 icols = 1;
151         else                            /* count cols on first line */
152                 for (p = curline, endp = curline + curlen; p < endp; p++) {
153                         if (*p == isep && multisep)
154                                 continue;
155                         icols++;
156                         while (*p && *p != isep)
157                                 p++;
158                 }
159         ep = getptrs(elem);
160         do {
161                 if (flags & ONEPERLINE) {
162                         *ep = curline;
163                         INCR(ep);               /* prepare for next entry */
164                         if (maxlen < curlen)
165                                 maxlen = curlen;
166                         irows++;
167                         continue;
168                 }
169                 for (p = curline, endp = curline + curlen; p < endp; p++) {
170                         if (*p == isep && multisep)
171                                 continue;       /* eat up column separators */
172                         if (*p == isep)         /* must be an empty column */
173                                 *ep = blank;
174                         else                    /* store column entry */
175                                 *ep = p;
176                         while (p < endp && *p != isep)
177                                 p++;            /* find end of entry */
178                         *p = '\0';              /* mark end of entry */
179                         if (maxlen < p - *ep)   /* update maxlen */
180                                 maxlen = p - *ep;
181                         INCR(ep);               /* prepare for next entry */
182                 }
183                 irows++;                        /* update row count */
184                 if (nullpad) {                  /* pad missing entries */
185                         padto = elem + irows * icols;
186                         while (ep < padto) {
187                                 *ep = blank;
188                                 INCR(ep);
189                         }
190                 }
191         } while (getline() != EOF);
192         *ep = 0;                                /* mark end of pointers */
193         nelem = ep - elem;
194 }
195
196 void
197 putfile(void)
198 {
199         char **ep;
200         int i, j, k;
201
202         ep = elem;
203         if (flags & TRANSPOSE)
204                 for (i = 0; i < orows; i++) {
205                         for (j = i; j < nelem; j += orows)
206                                 prints(ep[j], (j - i) / orows);
207                         putchar('\n');
208                 }
209         else
210                 for (i = k = 0; i < orows; i++) {
211                         for (j = 0; j < ocols; j++, k++)
212                                 if (k < nelem)
213                                         prints(ep[k], j);
214                         putchar('\n');
215                 }
216 }
217
218 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(void)
238 {
239         fprintf(stderr,
240                 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
241         exit(1);
242 }
243
244 void
245 prepfile(void)
246 {
247         char **ep;
248         int  i;
249         int  j;
250         char **lp;
251         int colw;
252         int max;
253         int n;
254
255         if (!nelem)
256                 exit(0);
257         gutter += maxlen * propgutter / 100.0;
258         colw = maxlen + gutter;
259         if (flags & MTRANSPOSE) {
260                 orows = icols;
261                 ocols = irows;
262         }
263         else if (orows == 0 && ocols == 0) {    /* decide rows and cols */
264                 ocols = owidth / colw;
265                 if (ocols == 0) {
266                         warnx("display width %d is less than column width %d",
267                                         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");
290         if (flags & SQUEEZE) {
291                 ep = elem;
292                 if (flags & TRANSPOSE)
293                         for (i = 0; i < ocols; i++) {
294                                 max = 0;
295                                 for (j = 0; *ep != NULL && j < orows; j++)
296                                         if ((n = strlen(*ep++)) > max)
297                                                 max = n;
298                                 colwidths[i] = max + gutter;
299                         }
300                 else
301                         for (i = 0; i < ocols; i++) {
302                                 max = 0;
303                                 for (j = i; j < nelem; j += ocols)
304                                         if ((n = strlen(ep[j])) > max)
305                                                 max = n;
306                                 colwidths[i] = max + gutter;
307                         }
308         }
309         /*      for (i = 0; i < orows; i++) {
310                         for (j = i; j < nelem; j += orows)
311                                 prints(ep[j], (j - i) / orows);
312                         putchar('\n');
313                 }
314         else
315                 for (i = 0; i < orows; i++) {
316                         for (j = 0; j < ocols; j++)
317                                 prints(*ep++, j);
318                         putchar('\n');
319                 }*/
320         else
321                 for (i = 0; i < ocols; i++)
322                         colwidths[i] = colw;
323         if (!(flags & NOTRIMENDCOL)) {
324                 if (flags & RIGHTADJUST)
325                         colwidths[0] -= gutter;
326                 else
327                         colwidths[ocols - 1] = 0;
328         }
329         n = orows * ocols;
330         if (n > nelem && (flags & RECYCLE))
331                 nelem = n;
332         /*for (i = 0; i < ocols; i++)
333                 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
334 }
335
336 #define BSIZE   (LINE_MAX * 2)
337 char    ibuf[BSIZE];
338
339 int
340 getline(void)   /* get line; maintain curline, curlen; manage storage */
341 {
342         static  int putlength;
343         static  char *endblock = ibuf + BSIZE;
344         char *p;
345         int c, i;
346
347         if (!irows) {
348                 curline = ibuf;
349                 putlength = flags & DETAILSHAPE;
350         }
351         else if (skip <= 0) {                   /* don't waste storage */
352                 curline += curlen + 1;
353                 if (putlength) {        /* print length, recycle storage */
354                         printf(" %d line %d\n", curlen, irows);
355                         curline = ibuf;
356                 }
357         }
358         if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
359                 /*ww = endblock-curline; tt += ww;*/
360                 /*printf("#wasted %d total %d\n",ww,tt);*/
361                 if (!(curline = (char *) malloc(BSIZE)))
362                         errx(1, "file too large");
363                 endblock = curline + BSIZE;
364                 /*printf("#endb %d curline %d\n",endblock,curline);*/
365         }
366         for (p = curline, i = 0;; *p++ = c, i++) {
367                 if ((c = getchar()) == EOF)
368                         break;
369                 if (i >= LINE_MAX)
370                         errx(1, "maximum line length (%d) exceeded", LINE_MAX);
371                 if (c == '\n')
372                         break;
373         }
374         *p = '\0';
375         curlen = i;
376         return(c);
377 }
378
379 char **
380 getptrs(char **sp)
381 {
382         char **p;
383
384         allocsize += allocsize;
385         p = (char **)realloc(elem, allocsize * sizeof(char *));
386         if (p == NULL)
387                 err(1, "no memory");
388
389         sp += (p - elem);
390         endelem = (elem = p) + allocsize;
391         return(sp);
392 }
393
394 void
395 getargs(int ac, char *av[])
396 {
397         char *p;
398
399         if (ac == 1) {
400                 flags |= NOARGS | TRANSPOSE;
401         }
402         while (--ac && **++av == '-')
403                 for (p = *av+1; *p; p++)
404                         switch (*p) {
405                         case 'T':
406                                 flags |= MTRANSPOSE;
407                         case 't':
408                                 flags |= TRANSPOSE;
409                                 break;
410                         case 'c':               /* input col. separator */
411                                 flags |= ONEISEPONLY;
412                         case 's':               /* one or more allowed */
413                                 if (p[1])
414                                         isep = *++p;
415                                 else
416                                         isep = '\t';    /* default is ^I */
417                                 break;
418                         case 'C':
419                                 flags |= ONEOSEPONLY;
420                         case 'S':
421                                 if (p[1])
422                                         osep = *++p;
423                                 else
424                                         osep = '\t';    /* default is ^I */
425                                 break;
426                         case 'w':               /* window width, default 80 */
427                                 p = getnum(&owidth, p, 0);
428                                 if (owidth <= 0)
429                                         errx(1, "width must be a positive integer");
430                                 break;
431                         case 'K':                       /* skip N lines */
432                                 flags |= SKIPPRINT;
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                         case 'h':
465                                 flags |= SHAPEONLY;
466                                 break;
467                         case 'z':                       /* squeeze col width */
468                                 flags |= SQUEEZE;
469                                 break;
470                         /*case 'p':
471                                 ipagespace = atoi(++p); (default is 1)
472                                 break;*/
473                         case 'o':                       /* col order */
474                                 p = getlist(&cord, p);
475                                 break;
476                         case 'b':
477                                 flags |= ICOLBOUNDS;
478                                 p = getlist(&icbd, p);
479                                 break;
480                         case 'B':
481                                 flags |= OCOLBOUNDS;
482                                 p = getlist(&ocbd, p);
483                                 break;
484                         default:
485                                 usage();
486                         }
487         /*if (!osep)
488                 osep = isep;*/
489         switch (ac) {
490         /*case 3:
491                 opages = atoi(av[2]);*/
492         case 2:
493                 if ((ocols = atoi(av[1])) < 0)
494                         ocols = 0;
495         case 1:
496                 if ((orows = atoi(av[0])) < 0)
497                         orows = 0;
498         case 0:
499                 break;
500         default:
501                 errx(1, "too many arguments");
502         }
503 }
504
505 char *
506 getlist(short **list, char *p)
507 {
508         int count = 1;
509         char *t;
510
511         for (t = p + 1; *t; t++) {
512                 if (!isdigit((unsigned char)*t))
513                         errx(1,
514         "option %.1s requires a list of unsigned numbers separated by commas", t);
515                 count++;
516                 while (*t && isdigit((unsigned char)*t))
517                         t++;
518                 if (*t != ',')
519                         break;
520         }
521         if (!(*list = (short *) malloc(count * sizeof(short))))
522                 errx(1, "no list space");
523         count = 0;
524         for (t = p + 1; *t; t++) {
525                 (*list)[count++] = atoi(t);
526                 printf("++ %d ", (*list)[count-1]);
527                 fflush(stdout);
528                 while (*t && isdigit((unsigned char)*t))
529                         t++;
530                 if (*t != ',')
531                         break;
532         }
533         (*list)[count] = 0;
534         return(t - 1);
535 }
536
537 /*
538  * num = number p points to; if (strict) complain
539  * returns pointer to end of num
540  */
541 char *
542 getnum(int *num, char *p, int strict)
543 {
544         char *t = p;
545
546         if (!isdigit((unsigned char)*++t)) {
547                 if (strict || *t == '-' || *t == '+')
548                         errx(1, "option %.1s requires an unsigned integer", p);
549                 *num = 0;
550                 return(p);
551         }
552         *num = atoi(t);
553         while (*++t)
554                 if (!isdigit((unsigned char)*t))
555                         break;
556         return(--t);
557 }