commit: 320e446f1ef258dc7295ac149ee56c0845978ba7
parent 14f50813969fd871c4a5d86e369be3714d3433cd
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 20 Sep 2024 02:34:22 +0200
cmd/expr: unify error message formatting
Diffstat:
1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/cmd/expr.y b/cmd/expr.y
@@ -131,7 +131,7 @@ make_integer(intmax_t i)
vp = (struct val *)malloc(sizeof(*vp));
if (vp == NULL)
- errx(ERR_EXIT, "malloc(vp) failed");
+ errx(ERR_EXIT, "error: malloc(vp) failed");
vp->type = integer;
vp->u.i = i;
@@ -145,10 +145,10 @@ make_str(const char *s)
vp = (struct val *)malloc(sizeof(*vp));
if (vp == NULL)
- errx(ERR_EXIT, "malloc(vp) failed");
+ errx(ERR_EXIT, "error: malloc(vp) failed");
if ((vp->u.s = strdup(s)) == NULL)
- errx(ERR_EXIT, "strdup(s) failed");
+ errx(ERR_EXIT, "error: strdup(s) failed");
if (is_integer(s))
vp->type = numeric_string;
@@ -191,9 +191,9 @@ static void
assert_to_integer(struct val *vp)
{
if (vp->type == string)
- errx(ERR_EXIT, "not a decimal number: '%s'", vp->u.s);
+ errx(ERR_EXIT, "error: not a decimal number: '%s'", vp->u.s);
if (!to_integer(vp))
- errx(ERR_EXIT, "operand too large: '%s'", vp->u.s);
+ errx(ERR_EXIT, "error: operand too large: '%s'", vp->u.s);
}
static void
@@ -212,7 +212,7 @@ to_string(struct val *vp)
#define NDIGITS(x) (3 * (sizeof(x) * CHAR_BIT) / 10 + 1 + 1 + 1)
tmp = malloc(NDIGITS(vp->u.i));
if (tmp == NULL)
- errx(ERR_EXIT, "malloc() failed");
+ errx(ERR_EXIT, "error: malloc() failed");
sprintf(tmp, "%jd", vp->u.i);
vp->type = string;
@@ -289,11 +289,15 @@ main(int argc, char *argv[])
av = argv + 1;
nonposix = 1;
} else {
- while ((c = getopt(argc, argv, "e")) != -1) {
+ while ((c = getopt(argc, argv, ":e")) != -1) {
switch (c) {
case 'e':
nonposix = 1;
break;
+ case ':':
+ fprintf(stderr, "expr: error: Missing operand for option: '-%c'\n", optopt);
+ case '?':
+ fprintf(stderr, "expr: error: Unrecognised option: '-%c'\n", optopt);
default:
errx(ERR_EXIT,
"usage: expr [-e] expression\n");
@@ -320,7 +324,7 @@ main(int argc, char *argv[])
static int
yyerror(const char *s)
{
- errx(ERR_EXIT, "syntax error");
+ errx(ERR_EXIT, "error: syntax error: %s", s);
}
static struct val *
@@ -420,7 +424,7 @@ assert_plus(intmax_t a, intmax_t b, intmax_t r)
*/
if ((a > 0 && b > 0 && r <= 0) ||
(a < 0 && b < 0 && r >= 0))
- errx(ERR_EXIT, "overflow");
+ errx(ERR_EXIT, "error: overflow");
}
static struct val *
@@ -443,7 +447,7 @@ assert_minus(intmax_t a, intmax_t b, intmax_t r)
{
if ((a >= 0 && b < 0 && r <= 0) ||
(a < 0 && b > 0 && r >= 0))
- errx(ERR_EXIT, "overflow");
+ errx(ERR_EXIT, "error: overflow");
}
static struct val *
@@ -481,7 +485,7 @@ assert_times(intmax_t a, intmax_t b, volatile intmax_t r)
* instead of a SIGFPE.
*/
if ((a == -1 && b == INTMAX_MIN) || (a != 0 && r / a != b))
- errx(ERR_EXIT, "overflow");
+ errx(ERR_EXIT, "error: overflow");
}
static struct val *
@@ -503,10 +507,10 @@ static void
assert_div(intmax_t a, intmax_t b)
{
if (b == 0)
- errx(ERR_EXIT, "division by zero");
+ errx(ERR_EXIT, "error: division by zero");
/* only INTMAX_MIN / -1 causes overflow */
if (a == INTMAX_MIN && b == -1)
- errx(ERR_EXIT, "overflow");
+ errx(ERR_EXIT, "error: overflow");
}
static struct val *
@@ -557,7 +561,7 @@ op_colon(struct val *a, struct val *b)
/* compile regular expression */
if ((eval = regcomp(&rp, b->u.s, 0)) != 0) {
regerror(eval, &rp, errbuf, sizeof(errbuf));
- errx(ERR_EXIT, "%s", errbuf);
+ errx(ERR_EXIT, "error: %s", errbuf);
}
/* compare string against pattern */