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