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