rs: Use getopt() and strtol() instead of mannual parsing.

This uses the "::" extension to getopt() to handle options which take
an optional argument.

The updated flag tests were all wrong before and only passed because
the manual parser failed to raise errors when a required argument was
missing.  The invalid argument test now gets a better error message.

Reviewed by:	brooks, imp, emaste
Differential Revision:	https://reviews.freebsd.org/D36834
This commit is contained in:
John Baldwin 2022-11-15 21:19:35 -08:00
parent 838a061417
commit afb4998dd4
2 changed files with 104 additions and 154 deletions

View File

@ -41,6 +41,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <vector> #include <vector>
static long flags; static long flags;
@ -63,9 +64,6 @@ static long flags;
#define NOARGS 0200000 #define NOARGS 0200000
static short *colwidths; static short *colwidths;
static short *cord;
static short *icbd;
static short *ocbd;
static std::vector<char *> elem; static std::vector<char *> elem;
static char *curline; static char *curline;
static size_t curlen; static size_t curlen;
@ -76,13 +74,12 @@ static int skip;
static int propgutter; static int propgutter;
static char isep = ' ', osep = ' '; static char isep = ' ', osep = ' ';
static char blank[] = ""; static char blank[] = "";
static int owidth = 80, gutter = 2; static size_t owidth = 80, gutter = 2;
static void getargs(int, char *[]); static void getargs(int, char *[]);
static void getfile(void); static void getfile(void);
static int get_line(void); static int get_line(void);
static char *getlist(short **, char *); static long getnum(const char *);
static char *getnum(int *, char *, int);
static void prepfile(void); static void prepfile(void);
static void prints(char *, int); static void prints(char *, int);
static void putfile(void); static void putfile(void);
@ -120,7 +117,7 @@ getfile(void)
return; return;
} }
get_line(); get_line();
if (flags & NOARGS && curlen < (size_t)owidth) if (flags & NOARGS && curlen < owidth)
flags |= ONEPERLINE; flags |= ONEPERLINE;
if (flags & ONEPERLINE) if (flags & ONEPERLINE)
icols = 1; icols = 1;
@ -226,7 +223,7 @@ prepfile(void)
else if (orows == 0 && ocols == 0) { /* decide rows and cols */ else if (orows == 0 && ocols == 0) { /* decide rows and cols */
ocols = owidth / colw; ocols = owidth / colw;
if (ocols == 0) { if (ocols == 0) {
warnx("display width %d is less than column width %zu", warnx("display width %zu is less than column width %zu",
owidth, colw); owidth, colw);
ocols = 1; ocols = 1;
} }
@ -338,101 +335,95 @@ static void
getargs(int ac, char *av[]) getargs(int ac, char *av[])
{ {
long val; long val;
char *p; int ch;
if (ac == 1) { if (ac == 1) {
flags |= NOARGS | TRANSPOSE; flags |= NOARGS | TRANSPOSE;
} }
while (--ac && **++av == '-')
for (p = *av+1; *p; p++) while ((ch = getopt(ac, av, "C::EG:HK:S::Tc::eg:hjk:mns::tw:yz")) != -1)
switch (*p) { switch (ch) {
case 'T': case 'T':
flags |= MTRANSPOSE; flags |= MTRANSPOSE;
/* FALLTHROUGH */ /* FALLTHROUGH */
case 't': case 't':
flags |= TRANSPOSE; flags |= TRANSPOSE;
break; break;
case 'c': /* input col. separator */ case 'c': /* input col. separator */
flags |= ONEISEPONLY; flags |= ONEISEPONLY;
/* FALLTHROUGH */ /* FALLTHROUGH */
case 's': /* one or more allowed */ case 's': /* one or more allowed */
if (p[1]) if (optarg != NULL)
isep = *++p; isep = *optarg;
else else
isep = '\t'; /* default is ^I */ isep = '\t'; /* default is ^I */
break; break;
case 'C': case 'C':
flags |= ONEOSEPONLY; flags |= ONEOSEPONLY;
/* FALLTHROUGH */ /* FALLTHROUGH */
case 'S': case 'S':
if (p[1]) if (optarg != NULL)
osep = *++p; osep = *optarg;
else else
osep = '\t'; /* default is ^I */ osep = '\t'; /* default is ^I */
break; break;
case 'w': /* window width, default 80 */ case 'w': /* window width, default 80 */
p = getnum(&owidth, p, 0); val = getnum(optarg);
if (owidth <= 0) if (val <= 0)
errx(1, "width must be a positive integer"); errx(1, "width must be a positive integer");
break; owidth = val;
case 'K': /* skip N lines */ break;
flags |= SKIPPRINT; case 'K': /* skip N lines */
/* FALLTHROUGH */ flags |= SKIPPRINT;
case 'k': /* skip, do not print */ /* FALLTHROUGH */
p = getnum(&skip, p, 0); case 'k': /* skip, do not print */
if (!skip) skip = getnum(optarg);
skip = 1; if (skip < 1)
break; skip = 1;
case 'm': break;
flags |= NOTRIMENDCOL; case 'm':
break; flags |= NOTRIMENDCOL;
case 'g': /* gutter space */ break;
p = getnum(&gutter, p, 0); case 'g': /* gutter space */
break; gutter = getnum(optarg);
case 'G': break;
p = getnum(&propgutter, p, 0); case 'G':
break; propgutter = getnum(optarg);
case 'e': /* each line is an entry */ break;
flags |= ONEPERLINE; case 'e': /* each line is an entry */
break; flags |= ONEPERLINE;
case 'E': break;
flags |= ONEPERCHAR; case 'E':
break; flags |= ONEPERCHAR;
case 'j': /* right adjust */ break;
flags |= RIGHTADJUST; case 'j': /* right adjust */
break; flags |= RIGHTADJUST;
case 'n': /* null padding for missing values */ break;
flags |= NULLPAD; case 'n': /* null padding for missing values */
break; flags |= NULLPAD;
case 'y': break;
flags |= RECYCLE; case 'y':
break; flags |= RECYCLE;
case 'H': /* print shape only */ break;
flags |= DETAILSHAPE; case 'H': /* print shape only */
/* FALLTHROUGH */ flags |= DETAILSHAPE;
case 'h': /* FALLTHROUGH */
flags |= SHAPEONLY; case 'h':
break; flags |= SHAPEONLY;
case 'z': /* squeeze col width */ break;
flags |= SQUEEZE; case 'z': /* squeeze col width */
break; flags |= SQUEEZE;
/*case 'p': break;
ipagespace = atoi(++p); (default is 1) /*case 'p':
break;*/ ipagespace = atoi(optarg); (default is 1)
case 'o': /* col order */ break;*/
p = getlist(&cord, p); default:
break; usage();
case 'b': }
flags |= ICOLBOUNDS;
p = getlist(&icbd, p); av += optind;
break; ac -= optind;
case 'B':
flags |= OCOLBOUNDS;
p = getlist(&ocbd, p);
break;
default:
usage();
}
/*if (!osep) /*if (!osep)
osep = isep;*/ osep = isep;*/
switch (ac) { switch (ac) {
@ -458,56 +449,14 @@ getargs(int ac, char *av[])
} }
} }
static char * static long
getlist(short **list, char *p) getnum(const char *p)
{ {
int count = 1; char *ep;
char *t; long val;
for (t = p + 1; *t; t++) { val = strtol(p, &ep, 10);
if (!isdigit((unsigned char)*t)) if (*ep != '\0')
errx(1, errx(1, "invalid integer %s", p);
"option %.1s requires a list of unsigned numbers separated by commas", t); return (val);
count++;
while (*t && isdigit((unsigned char)*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++) {
(*list)[count++] = atoi(t);
printf("++ %d ", (*list)[count-1]);
fflush(stdout);
while (*t && isdigit((unsigned char)*t))
t++;
if (*t != ',')
break;
}
(*list)[count] = 0;
return(t - 1);
}
/*
* num = number p points to; if (strict) complain
* returns pointer to end of num
*/
static char *
getnum(int *num, char *p, int strict)
{
char *t = p;
if (!isdigit((unsigned char)*++t)) {
if (strict || *t == '-' || *t == '+')
errx(1, "option %.1s requires an unsigned integer", p);
*num = 0;
return(p);
}
*num = atoi(t);
while (*++t)
if (!isdigit((unsigned char)*t))
break;
return(--t);
} }

View File

@ -100,7 +100,7 @@ k_flag_head()
k_flag_body() k_flag_body()
{ {
atf_check -s exit:0 -o empty rs -k < /dev/null atf_check -s exit:0 -o empty rs -k 1 < /dev/null
} }
atf_test_case K_flag atf_test_case K_flag
@ -112,7 +112,7 @@ K_flag_head()
K_flag_body() K_flag_body()
{ {
atf_check -s exit:0 -o inline:" atf_check -s exit:0 -o inline:"
" rs -K < /dev/null " rs -K 1 < /dev/null
} }
atf_test_case g_flag atf_test_case g_flag
@ -123,7 +123,7 @@ g_flag_head()
g_flag_body() g_flag_body()
{ {
atf_check -s exit:0 -o empty rs -g < /dev/null atf_check -s exit:0 -o empty rs -g 1 < /dev/null
} }
atf_test_case G_flag atf_test_case G_flag
@ -134,7 +134,7 @@ G_flag_head()
G_flag_body() G_flag_body()
{ {
atf_check -s exit:0 -o empty rs -G < /dev/null atf_check -s exit:0 -o empty rs -G 10 < /dev/null
} }
atf_test_case e_flag atf_test_case e_flag
@ -237,7 +237,8 @@ invalid_usage_head()
invalid_usage_body() invalid_usage_body()
{ {
atf_check -s not-exit:0 -e inline:"rs: width must be a positive integer atf_check -s not-exit:0 -e inline:"rs: option requires an argument -- w
usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]
" rs -w " rs -w
} }