jmc@ has pointed out some inconsistencies in how bandwidth parameters
are parsed and printed out. I agree that we can fix or improve this:
1) Bit/B/bit/b are parsed incorrectly. Ditching them is another option.
2) Should we make lowercase K, M and G parseable as well? I'd like that.
3) Avoid an extra space when printing out bandwidth and there's no unit
(K/M/G) to print.
OK?
diff --git sbin/pfctl/parse.y sbin/pfctl/parse.y
index 63aaafeeea5..8b0f1d3182b 100644
--- sbin/pfctl/parse.y
+++ sbin/pfctl/parse.y
@@ -1455,24 +1455,21 @@ bandwidth : STRING {
bps = strtod($1, &cp);
if (cp != NULL) {
if (strlen(cp) > 1) {
char *cu = cp + 1;
- if (!strcmp(cu, "Bit") ||
- !strcmp(cu, "B") ||
- !strcmp(cu, "bit") ||
- !strcmp(cu, "b")) {
+ if (!strcmp(cp, "Bit") ||
+ !strcmp(cp, "bit"))
*cu = 0;
- }
}
- if (!strcmp(cp, "b"))
+ if (!strcasecmp(cp, "B"))
; /* nothing */
- else if (!strcmp(cp, "K"))
+ else if (!strcasecmp(cp, "K"))
bps *= 1000;
- else if (!strcmp(cp, "M"))
+ else if (!strcasecmp(cp, "M"))
bps *= 1000 * 1000;
- else if (!strcmp(cp, "G"))
+ else if (!strcasecmp(cp, "G"))
bps *= 1000 * 1000 * 1000;
else if (!strcmp(cp, "%")) {
if (bps < 0 || bps > 100) {
yyerror("bandwidth spec "
"out of range");
diff --git sbin/pfctl/pfctl_parser.c sbin/pfctl/pfctl_parser.c
index a69acb2e5b2..e468c2cfe1d 100644
--- sbin/pfctl/pfctl_parser.c
+++ sbin/pfctl/pfctl_parser.c
@@ -1168,19 +1168,19 @@ print_tabledef(const char *name, int flags, int addrs,
void
print_bwspec(const char *prefix, struct pf_queue_bwspec *bw)
{
u_int rate;
int i;
- static const char unit[] = " KMG";
+ static const char *unit[] = { "", "K", "M", "G" };
if (bw->percent)
printf("%s%u%%", prefix, bw->percent);
else if (bw->absolute) {
rate = bw->absolute;
for (i = 0; rate >= 1000 && i <= 3 && (rate % 1000 == 0); i++)
rate /= 1000;
- printf("%s%u%c", prefix, rate, unit[i]);
+ printf("%s%u%s", prefix, rate, unit[i]);
}
}
void
print_scspec(const char *prefix, struct pf_queue_scspec *sc)