1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rs - reshape a data array
|
|
|
|
* Author: John Kunze, Office of Comp. Affairs, UCB
|
|
|
|
* BEWARE: lots of unfinished edges
|
|
|
|
*/
|
|
|
|
|
1997-08-07 06:42:37 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <ctype.h>
|
2011-02-07 18:10:18 +00:00
|
|
|
#include <limits.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1998-02-20 04:48:37 +00:00
|
|
|
#include <string.h>
|
2022-11-16 05:03:12 +00:00
|
|
|
#include <vector>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static long flags;
|
1994-05-27 12:33:43 +00:00
|
|
|
#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
|
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static short *colwidths;
|
|
|
|
static short *cord;
|
|
|
|
static short *icbd;
|
|
|
|
static short *ocbd;
|
2022-11-16 05:03:12 +00:00
|
|
|
static std::vector<char *> elem;
|
2011-11-06 08:16:35 +00:00
|
|
|
static char *curline;
|
2022-11-16 05:03:12 +00:00
|
|
|
static size_t curlen;
|
|
|
|
static size_t irows, icols;
|
|
|
|
static size_t orows = 0, ocols = 0;
|
|
|
|
static size_t maxlen;
|
2011-11-06 08:16:35 +00:00
|
|
|
static int skip;
|
|
|
|
static int propgutter;
|
|
|
|
static char isep = ' ', osep = ' ';
|
|
|
|
static char blank[] = "";
|
|
|
|
static int owidth = 80, gutter = 2;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static void getargs(int, char *[]);
|
|
|
|
static void getfile(void);
|
2016-05-10 11:17:36 +00:00
|
|
|
static int get_line(void);
|
2011-11-06 08:16:35 +00:00
|
|
|
static char *getlist(short **, char *);
|
|
|
|
static char *getnum(int *, char *, int);
|
|
|
|
static void prepfile(void);
|
|
|
|
static void prints(char *, int);
|
|
|
|
static void putfile(void);
|
2002-03-22 01:33:25 +00:00
|
|
|
static void usage(void);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-04-28 11:00:34 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
getargs(argc, argv);
|
|
|
|
getfile();
|
|
|
|
if (flags & SHAPEONLY) {
|
2022-11-16 05:03:12 +00:00
|
|
|
printf("%zu %zu\n", irows, icols);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
prepfile();
|
|
|
|
putfile();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static void
|
2002-04-28 11:00:34 +00:00
|
|
|
getfile(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2022-10-05 23:48:05 +00:00
|
|
|
char *p, *sp;
|
2002-04-28 11:00:34 +00:00
|
|
|
char *endp;
|
2011-02-07 18:05:56 +00:00
|
|
|
int c;
|
1994-05-27 12:33:43 +00:00
|
|
|
int multisep = (flags & ONEISEPONLY ? 0 : 1);
|
|
|
|
int nullpad = flags & NULLPAD;
|
2022-11-16 05:03:12 +00:00
|
|
|
size_t len, padto;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
while (skip--) {
|
2016-05-10 11:17:36 +00:00
|
|
|
c = get_line();
|
1994-05-27 12:33:43 +00:00
|
|
|
if (flags & SKIPPRINT)
|
|
|
|
puts(curline);
|
2011-02-07 18:05:56 +00:00
|
|
|
if (c == EOF)
|
|
|
|
return;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2016-05-10 11:17:36 +00:00
|
|
|
get_line();
|
2022-11-16 05:03:12 +00:00
|
|
|
if (flags & NOARGS && curlen < (size_t)owidth)
|
1994-05-27 12:33:43 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (flags & ONEPERLINE) {
|
2022-11-16 05:03:12 +00:00
|
|
|
elem.push_back(curline);
|
1994-05-27 12:33:43 +00:00
|
|
|
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 */
|
2022-11-16 05:03:12 +00:00
|
|
|
elem.push_back(blank);
|
1994-05-27 12:33:43 +00:00
|
|
|
else /* store column entry */
|
2022-11-16 05:03:12 +00:00
|
|
|
elem.push_back(p);
|
2022-10-05 23:48:05 +00:00
|
|
|
sp = p;
|
1994-05-27 12:33:43 +00:00
|
|
|
while (p < endp && *p != isep)
|
|
|
|
p++; /* find end of entry */
|
|
|
|
*p = '\0'; /* mark end of entry */
|
2022-10-05 23:48:05 +00:00
|
|
|
len = p - sp;
|
|
|
|
if (maxlen < len) /* update maxlen */
|
|
|
|
maxlen = len;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
irows++; /* update row count */
|
|
|
|
if (nullpad) { /* pad missing entries */
|
2022-11-16 05:03:12 +00:00
|
|
|
padto = irows * icols;
|
|
|
|
elem.resize(padto, blank);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2016-05-10 11:17:36 +00:00
|
|
|
} while (get_line() != EOF);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static void
|
2002-04-28 11:00:34 +00:00
|
|
|
putfile(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2022-11-16 05:03:12 +00:00
|
|
|
size_t i, j, k;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (flags & TRANSPOSE)
|
|
|
|
for (i = 0; i < orows; i++) {
|
2022-11-16 05:03:12 +00:00
|
|
|
for (j = i; j < elem.size(); j += orows)
|
|
|
|
prints(elem[j], (j - i) / orows);
|
1994-05-27 12:33:43 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
else
|
1996-05-26 18:26:55 +00:00
|
|
|
for (i = k = 0; i < orows; i++) {
|
|
|
|
for (j = 0; j < ocols; j++, k++)
|
2022-11-16 05:03:12 +00:00
|
|
|
if (k < elem.size())
|
|
|
|
prints(elem[k], j);
|
1994-05-27 12:33:43 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static void
|
2002-04-28 11:00:34 +00:00
|
|
|
prints(char *s, int col)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-28 11:00:34 +00:00
|
|
|
int n;
|
|
|
|
char *p = s;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1997-08-07 06:42:37 +00:00
|
|
|
static void
|
2002-04-28 11:00:34 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
1997-08-07 06:42:37 +00:00
|
|
|
"usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static void
|
2002-04-28 11:00:34 +00:00
|
|
|
prepfile(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2022-11-16 05:03:12 +00:00
|
|
|
size_t i, j;
|
|
|
|
size_t colw, max, n, orig_size, padto;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2022-11-16 05:03:12 +00:00
|
|
|
if (elem.empty())
|
1994-05-27 12:33:43 +00:00
|
|
|
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;
|
2001-09-10 11:56:07 +00:00
|
|
|
if (ocols == 0) {
|
2022-11-16 05:03:12 +00:00
|
|
|
warnx("display width %d is less than column width %zu",
|
1997-08-07 06:42:37 +00:00
|
|
|
owidth, colw);
|
2001-09-10 11:56:07 +00:00
|
|
|
ocols = 1;
|
|
|
|
}
|
2022-11-16 05:03:12 +00:00
|
|
|
if (ocols > elem.size())
|
|
|
|
ocols = elem.size();
|
|
|
|
orows = elem.size() / ocols + (elem.size() % ocols ? 1 : 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
else if (orows == 0) /* decide on rows */
|
2022-11-16 05:03:12 +00:00
|
|
|
orows = elem.size() / ocols + (elem.size() % ocols ? 1 : 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
else if (ocols == 0) /* decide on cols */
|
2022-11-16 05:03:12 +00:00
|
|
|
ocols = elem.size() / orows + (elem.size() % orows ? 1 : 0);
|
|
|
|
padto = orows * ocols;
|
|
|
|
orig_size = elem.size();
|
1994-05-27 12:33:43 +00:00
|
|
|
if (flags & RECYCLE) {
|
2022-11-16 05:03:12 +00:00
|
|
|
for (i = 0; elem.size() < padto; i++)
|
|
|
|
elem.push_back(elem[i % orig_size]);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
|
1997-08-07 06:42:37 +00:00
|
|
|
errx(1, "malloc");
|
1994-05-27 12:33:43 +00:00
|
|
|
if (flags & SQUEEZE) {
|
2022-11-16 05:03:12 +00:00
|
|
|
if (flags & TRANSPOSE) {
|
|
|
|
auto it = elem.begin();
|
2001-09-10 11:43:40 +00:00
|
|
|
for (i = 0; i < ocols; i++) {
|
|
|
|
max = 0;
|
2022-11-16 05:03:12 +00:00
|
|
|
for (j = 0; it != elem.end() && j < orows; j++)
|
|
|
|
if ((n = strlen(*it++)) > max)
|
1994-05-27 12:33:43 +00:00
|
|
|
max = n;
|
|
|
|
colwidths[i] = max + gutter;
|
|
|
|
}
|
2022-11-16 05:03:12 +00:00
|
|
|
} else {
|
1994-05-27 12:33:43 +00:00
|
|
|
for (i = 0; i < ocols; i++) {
|
2001-09-10 11:43:40 +00:00
|
|
|
max = 0;
|
2022-11-16 05:03:12 +00:00
|
|
|
for (j = i; j < elem.size(); j += ocols)
|
|
|
|
if ((n = strlen(elem[j])) > max)
|
1994-05-27 12:33:43 +00:00
|
|
|
max = n;
|
|
|
|
colwidths[i] = max + gutter;
|
|
|
|
}
|
2022-11-16 05:03:12 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
/* for (i = 0; i < orows; i++) {
|
2022-11-16 05:03:12 +00:00
|
|
|
for (j = i; j < elem.size(); j += orows)
|
|
|
|
prints(elem[j], (j - i) / orows);
|
1994-05-27 12:33:43 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
2022-11-16 05:03:12 +00:00
|
|
|
else {
|
|
|
|
auto it = elem.begin();
|
1994-05-27 12:33:43 +00:00
|
|
|
for (i = 0; i < orows; i++) {
|
|
|
|
for (j = 0; j < ocols; j++)
|
2022-11-16 05:03:12 +00:00
|
|
|
prints(*it++, j);
|
1994-05-27 12:33:43 +00:00
|
|
|
putchar('\n');
|
|
|
|
}*/
|
|
|
|
else
|
|
|
|
for (i = 0; i < ocols; i++)
|
|
|
|
colwidths[i] = colw;
|
|
|
|
if (!(flags & NOTRIMENDCOL)) {
|
|
|
|
if (flags & RIGHTADJUST)
|
|
|
|
colwidths[0] -= gutter;
|
|
|
|
else
|
|
|
|
colwidths[ocols - 1] = 0;
|
|
|
|
}
|
|
|
|
/*for (i = 0; i < ocols; i++)
|
2022-11-16 05:03:12 +00:00
|
|
|
warnx("%d is colwidths, nelem %zu", colwidths[i], elem.size());*/
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2011-02-07 18:10:18 +00:00
|
|
|
#define BSIZE (LINE_MAX * 2)
|
2011-11-06 08:16:35 +00:00
|
|
|
static char ibuf[BSIZE];
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static int
|
2016-05-10 11:17:36 +00:00
|
|
|
get_line(void) /* get line; maintain curline, curlen; manage storage */
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
static int putlength;
|
|
|
|
static char *endblock = ibuf + BSIZE;
|
2002-04-28 11:00:34 +00:00
|
|
|
char *p;
|
|
|
|
int c, i;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2022-11-16 05:03:12 +00:00
|
|
|
if (irows == 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
curline = ibuf;
|
|
|
|
putlength = flags & DETAILSHAPE;
|
|
|
|
}
|
|
|
|
else if (skip <= 0) { /* don't waste storage */
|
|
|
|
curline += curlen + 1;
|
2002-06-07 07:08:06 +00:00
|
|
|
if (putlength) { /* print length, recycle storage */
|
2022-11-16 05:03:12 +00:00
|
|
|
printf(" %zu line %zu\n", curlen, irows);
|
2002-06-07 07:08:06 +00:00
|
|
|
curline = ibuf;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2011-02-07 18:10:18 +00:00
|
|
|
if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
|
1994-05-27 12:33:43 +00:00
|
|
|
/*ww = endblock-curline; tt += ww;*/
|
|
|
|
/*printf("#wasted %d total %d\n",ww,tt);*/
|
|
|
|
if (!(curline = (char *) malloc(BSIZE)))
|
1997-08-07 06:42:37 +00:00
|
|
|
errx(1, "file too large");
|
1994-05-27 12:33:43 +00:00
|
|
|
endblock = curline + BSIZE;
|
|
|
|
/*printf("#endb %d curline %d\n",endblock,curline);*/
|
|
|
|
}
|
2011-02-07 18:10:18 +00:00
|
|
|
for (p = curline, i = 0;; *p++ = c, i++) {
|
|
|
|
if ((c = getchar()) == EOF)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2011-02-07 18:10:18 +00:00
|
|
|
if (i >= LINE_MAX)
|
|
|
|
errx(1, "maximum line length (%d) exceeded", LINE_MAX);
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
*p = '\0';
|
2011-02-07 18:10:18 +00:00
|
|
|
curlen = i;
|
1994-05-27 12:33:43 +00:00
|
|
|
return(c);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static void
|
2002-04-28 11:00:34 +00:00
|
|
|
getargs(int ac, char *av[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2022-11-16 05:03:12 +00:00
|
|
|
long val;
|
2002-04-28 11:00:34 +00:00
|
|
|
char *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (ac == 1) {
|
|
|
|
flags |= NOARGS | TRANSPOSE;
|
|
|
|
}
|
|
|
|
while (--ac && **++av == '-')
|
|
|
|
for (p = *av+1; *p; p++)
|
|
|
|
switch (*p) {
|
|
|
|
case 'T':
|
|
|
|
flags |= MTRANSPOSE;
|
2022-10-05 23:47:21 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 't':
|
|
|
|
flags |= TRANSPOSE;
|
|
|
|
break;
|
|
|
|
case 'c': /* input col. separator */
|
|
|
|
flags |= ONEISEPONLY;
|
2022-10-05 23:47:21 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 's': /* one or more allowed */
|
|
|
|
if (p[1])
|
|
|
|
isep = *++p;
|
|
|
|
else
|
|
|
|
isep = '\t'; /* default is ^I */
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
flags |= ONEOSEPONLY;
|
2022-10-05 23:47:21 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'S':
|
|
|
|
if (p[1])
|
|
|
|
osep = *++p;
|
|
|
|
else
|
|
|
|
osep = '\t'; /* default is ^I */
|
|
|
|
break;
|
|
|
|
case 'w': /* window width, default 80 */
|
|
|
|
p = getnum(&owidth, p, 0);
|
|
|
|
if (owidth <= 0)
|
1997-08-07 06:42:37 +00:00
|
|
|
errx(1, "width must be a positive integer");
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'K': /* skip N lines */
|
|
|
|
flags |= SKIPPRINT;
|
2022-10-05 23:47:21 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'k': /* skip, do not print */
|
|
|
|
p = getnum(&skip, p, 0);
|
|
|
|
if (!skip)
|
|
|
|
skip = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
flags |= NOTRIMENDCOL;
|
|
|
|
break;
|
|
|
|
case 'g': /* gutter space */
|
|
|
|
p = getnum(&gutter, p, 0);
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
p = getnum(&propgutter, p, 0);
|
|
|
|
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;
|
2022-10-05 23:47:21 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'h':
|
|
|
|
flags |= SHAPEONLY;
|
|
|
|
break;
|
|
|
|
case 'z': /* squeeze col width */
|
|
|
|
flags |= SQUEEZE;
|
|
|
|
break;
|
|
|
|
/*case 'p':
|
|
|
|
ipagespace = atoi(++p); (default is 1)
|
|
|
|
break;*/
|
|
|
|
case 'o': /* col order */
|
|
|
|
p = getlist(&cord, p);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
flags |= ICOLBOUNDS;
|
|
|
|
p = getlist(&icbd, p);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
flags |= OCOLBOUNDS;
|
|
|
|
p = getlist(&ocbd, p);
|
|
|
|
break;
|
|
|
|
default:
|
1997-08-07 06:42:37 +00:00
|
|
|
usage();
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
/*if (!osep)
|
|
|
|
osep = isep;*/
|
|
|
|
switch (ac) {
|
2022-10-05 23:47:21 +00:00
|
|
|
#if 0
|
|
|
|
case 3:
|
|
|
|
opages = atoi(av[2]);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
case 2:
|
2022-11-16 05:03:12 +00:00
|
|
|
val = strtol(av[1], NULL, 10);
|
|
|
|
if (val >= 0)
|
|
|
|
ocols = val;
|
2022-10-05 23:47:21 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 1:
|
2022-11-16 05:03:12 +00:00
|
|
|
val = strtol(av[0], NULL, 10);
|
|
|
|
if (val >= 0)
|
|
|
|
orows = val;
|
2022-10-05 23:47:21 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
1997-08-07 06:42:37 +00:00
|
|
|
errx(1, "too many arguments");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:35 +00:00
|
|
|
static char *
|
2002-04-28 11:00:34 +00:00
|
|
|
getlist(short **list, char *p)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-28 11:00:34 +00:00
|
|
|
int count = 1;
|
|
|
|
char *t;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
for (t = p + 1; *t; t++) {
|
2004-07-15 10:26:38 +00:00
|
|
|
if (!isdigit((unsigned char)*t))
|
1997-08-07 06:42:37 +00:00
|
|
|
errx(1,
|
|
|
|
"option %.1s requires a list of unsigned numbers separated by commas", t);
|
1994-05-27 12:33:43 +00:00
|
|
|
count++;
|
2004-07-15 10:26:38 +00:00
|
|
|
while (*t && isdigit((unsigned char)*t))
|
1994-05-27 12:33:43 +00:00
|
|
|
t++;
|
|
|
|
if (*t != ',')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!(*list = (short *) malloc(count * sizeof(short))))
|
1997-08-07 06:42:37 +00:00
|
|
|
errx(1, "no list space");
|
1994-05-27 12:33:43 +00:00
|
|
|
count = 0;
|
|
|
|
for (t = p + 1; *t; t++) {
|
|
|
|
(*list)[count++] = atoi(t);
|
|
|
|
printf("++ %d ", (*list)[count-1]);
|
|
|
|
fflush(stdout);
|
2004-07-15 10:26:38 +00:00
|
|
|
while (*t && isdigit((unsigned char)*t))
|
1994-05-27 12:33:43 +00:00
|
|
|
t++;
|
|
|
|
if (*t != ',')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(*list)[count] = 0;
|
|
|
|
return(t - 1);
|
|
|
|
}
|
|
|
|
|
2002-04-28 11:00:34 +00:00
|
|
|
/*
|
|
|
|
* num = number p points to; if (strict) complain
|
|
|
|
* returns pointer to end of num
|
|
|
|
*/
|
2011-11-06 08:16:35 +00:00
|
|
|
static char *
|
2002-04-28 11:00:34 +00:00
|
|
|
getnum(int *num, char *p, int strict)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-28 11:00:34 +00:00
|
|
|
char *t = p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-15 10:26:38 +00:00
|
|
|
if (!isdigit((unsigned char)*++t)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (strict || *t == '-' || *t == '+')
|
1997-08-07 06:42:37 +00:00
|
|
|
errx(1, "option %.1s requires an unsigned integer", p);
|
1994-05-27 12:33:43 +00:00
|
|
|
*num = 0;
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
*num = atoi(t);
|
|
|
|
while (*++t)
|
2004-07-15 10:26:38 +00:00
|
|
|
if (!isdigit((unsigned char)*t))
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
return(--t);
|
|
|
|
}
|