1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-11-03 17:27:29 -05:00

Move error handling into vprepstr(), to save doing it over and over

This commit is contained in:
John Zaitseff 2011-08-13 08:17:30 +10:00
parent 06b66716cf
commit c50b8a4649
2 changed files with 54 additions and 95 deletions

View File

@ -390,10 +390,7 @@ void init_title (void)
} }
lines = prepstr(chbuf, BUFSIZE, attr_game_title, 0, 0, 1, COLS, lines = prepstr(chbuf, BUFSIZE, attr_game_title, 0, 0, 1, COLS,
&width, 1, "%s", _("Star Traders")); &width, 1, _("Star Traders"));
if (lines < 0) {
errno_exit("init_title");
}
pr_center(stdscr, 0, 0, chbuf, lines, &width); pr_center(stdscr, 0, 0, chbuf, lines, &width);
attrset(attr_root_window); attrset(attr_root_window);
@ -604,10 +601,6 @@ int txdlgbox (int maxlines, int ncols, int begin_y, int begin_x,
ncols - 4, widthbuf, maxlines, format, args); ncols - 4, widthbuf, maxlines, format, args);
va_end(args); va_end(args);
if (lines < 0) {
errno_exit(_("txdlgbox: `%s'"), format);
}
newtxwin(usetitle ? lines + 6 : lines + 5, ncols, begin_y, begin_x, newtxwin(usetitle ? lines + 6 : lines + 5, ncols, begin_y, begin_x,
true, bkgd_attr); true, bkgd_attr);
@ -621,15 +614,9 @@ int txdlgbox (int maxlines, int ncols, int begin_y, int begin_x,
err_exit_nomem(); err_exit_nomem();
} }
titlelines = prepstr(titlebuf, BUFSIZE, title_attr, bkgd_attr, titlelines = prepstr(titlebuf, BUFSIZE, title_attr, 0, 0, 1,
norm_attr, 1, ncols - 4, &titlewidth, 1, ncols - 4, &titlewidth, 1, boxtitle);
"%s", boxtitle);
if (titlelines < 0) {
errno_exit(_("txdlgbox: `%s'"), boxtitle);
}
pr_center(curwin, 1, 0, titlebuf, titlelines, &titlewidth); pr_center(curwin, 1, 0, titlebuf, titlelines, &titlewidth);
free(titlebuf); free(titlebuf);
} }
@ -790,8 +777,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
case '^': case '^':
// Switch to a different character rendition // Switch to a different character rendition
if (*format == '\0') { if (*format == '\0') {
errno = EINVAL; goto error_inval;
return -1;
} else { } else {
format++; format++;
} }
@ -800,8 +786,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
case '%': case '%':
// Process a conversion specifier // Process a conversion specifier
if (*format == '\0') { if (*format == '\0') {
errno = EINVAL; goto error_inval;
return -1;
} else if (*format == '%') { } else if (*format == '%') {
format++; format++;
} else { } else {
@ -816,10 +801,8 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
switch (c) { switch (c) {
case '0': case '0':
// Zero flag, or part of numeric count // Zero flag, or part of numeric count
if (count == 0) { if (count == 0)
errno = EINVAL; goto error_inval;
return -1;
}
count *= 10; count *= 10;
break; break;
@ -839,14 +822,12 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
case '$': case '$':
// Fixed-position argument // Fixed-position argument
if (flag_posn || count == 0) { if (flag_posn || count == 0)
errno = EINVAL; goto error_inval;
return -1;
}
if (count > MAXFMTARGS) { if (count > MAXFMTARGS) {
errno = E2BIG; errno = E2BIG;
return -1; goto error;
} }
flag_posn = true; flag_posn = true;
@ -860,10 +841,8 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
case 'l': case 'l':
// Long length modifier // Long length modifier
if (flag_long) { if (flag_long)
errno = EINVAL; goto error_inval;
return -1;
}
flag_long = true; flag_long = true;
break; break;
@ -875,34 +854,29 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
case 'N': case 'N':
// Insert a monetary amount (double) // Insert a monetary amount (double)
if (flag_long) { if (flag_long)
errno = EINVAL; goto error_inval;
return -1;
}
arg_type = TYPE_DOUBLE; arg_type = TYPE_DOUBLE;
goto handlefmt; goto handlefmt;
case 's': case 's':
// Insert a string (const char *) // Insert a string (const char *)
if (flag_long) { if (flag_long)
errno = EINVAL; goto error_inval;
return -1;
}
arg_type = TYPE_STRING; arg_type = TYPE_STRING;
handlefmt: handlefmt:
if (arg_num >= MAXFMTARGS) { if (arg_num >= MAXFMTARGS) {
errno = E2BIG; errno = E2BIG;
return -1; goto error;
} }
if (format_arg[arg_num].a_type == TYPE_NONE) { if (format_arg[arg_num].a_type == TYPE_NONE) {
format_arg[arg_num].a_type = arg_type; format_arg[arg_num].a_type = arg_type;
} else if (format_arg[arg_num].a_type != arg_type) { } else if (format_arg[arg_num].a_type != arg_type) {
errno = EINVAL; goto error_inval;
return -1;
} }
arg_num++; arg_num++;
@ -912,13 +886,11 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
break; break;
default: default:
errno = EINVAL; goto error_inval;
return -1;
} }
} }
if (inspec) { if (inspec) {
errno = EINVAL; goto error_inval;
return -1;
} }
} }
break; break;
@ -951,8 +923,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
/* Cannot allow unused arguments, as we have no way of /* Cannot allow unused arguments, as we have no way of
knowing how much space they take (cf. int vs. long long knowing how much space they take (cf. int vs. long long
int). */ int). */
errno = EINVAL; goto error_inval;
return -1;
} }
} }
@ -972,8 +943,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
case '^': case '^':
// Switch to a different character rendition // Switch to a different character rendition
if (*++format == '\0') { if (*++format == '\0') {
errno = EINVAL; goto error_inval;
return -1;
} else { } else {
switch (*format) { switch (*format) {
case '^': case '^':
@ -981,7 +951,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
maxwidth, &line, &width, &lastspc, maxwidth, &line, &width, &lastspc,
&widthspc, widthbuf, widthbufsize, &widthspc, widthbuf, widthbufsize,
&format) < 0) { &format) < 0) {
return -1; goto error;
} }
break; break;
@ -1002,8 +972,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
break; break;
default: default:
errno = EINVAL; goto error_inval;
return -1;
} }
} }
break; break;
@ -1011,13 +980,12 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
case '%': case '%':
// Process a conversion specifier // Process a conversion specifier
if (*++format == '\0') { if (*++format == '\0') {
errno = EINVAL; goto error_inval;
return -1;
} else if (*format == '%') { } else if (*format == '%') {
if (prepstr_addch(&chbuf, &chbufsize, curattr, maxlines, if (prepstr_addch(&chbuf, &chbufsize, curattr, maxlines,
maxwidth, &line, &width, &lastspc, &widthspc, maxwidth, &line, &width, &lastspc, &widthspc,
widthbuf, widthbufsize, &format) < 0) { widthbuf, widthbufsize, &format) < 0) {
return -1; goto error;
} }
} else { } else {
bool inspec = true; bool inspec = true;
@ -1040,8 +1008,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
if (count == 0) { if (count == 0) {
// Zero flag is not supported // Zero flag is not supported
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
count *= 10; count *= 10;
@ -1064,14 +1031,13 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
// Fixed-position argument // Fixed-position argument
if (flag_posn || count == 0) { if (flag_posn || count == 0) {
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
if (count > MAXFMTARGS) { if (count > MAXFMTARGS) {
free(buf); free(buf);
errno = E2BIG; errno = E2BIG;
return -1; goto error;
} }
flag_posn = true; flag_posn = true;
@ -1083,8 +1049,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
// Use locale-specific thousands separator // Use locale-specific thousands separator
if (flag_thou) { if (flag_thou) {
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
flag_thou = true; flag_thou = true;
@ -1094,8 +1059,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
// Long length modifier // Long length modifier
if (flag_long) { if (flag_long) {
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
flag_long = true; flag_long = true;
@ -1105,14 +1069,13 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
// Insert an integer (int or long int) into the output // Insert an integer (int or long int) into the output
if (count != 0) { if (count != 0) {
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
if (arg_num >= MAXFMTARGS) { if (arg_num >= MAXFMTARGS) {
free(buf); free(buf);
errno = E2BIG; errno = E2BIG;
return -1; goto error;
} }
if (flag_long) { if (flag_long) {
@ -1121,7 +1084,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
saved_errno = errno; saved_errno = errno;
free(buf); free(buf);
errno = saved_errno; errno = saved_errno;
return -1; goto error;
} }
} else { } else {
if (snprintf(buf, BUFSIZE, flag_thou ? "%'d" : "%d", if (snprintf(buf, BUFSIZE, flag_thou ? "%'d" : "%d",
@ -1129,7 +1092,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
saved_errno = errno; saved_errno = errno;
free(buf); free(buf);
errno = saved_errno; errno = saved_errno;
return -1; goto error;
} }
} }
@ -1140,14 +1103,13 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
// Insert a monetary amount (double) into the output // Insert a monetary amount (double) into the output
if (count != 0 || flag_thou || flag_long) { if (count != 0 || flag_thou || flag_long) {
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
if (arg_num >= MAXFMTARGS) { if (arg_num >= MAXFMTARGS) {
free(buf); free(buf);
errno = E2BIG; errno = E2BIG;
return -1; goto error;
} }
if (l_strfmon(buf, BUFSIZE, "%n", if (l_strfmon(buf, BUFSIZE, "%n",
@ -1155,7 +1117,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
saved_errno = errno; saved_errno = errno;
free(buf); free(buf);
errno = saved_errno; errno = saved_errno;
return -1; goto error;
} }
str = buf; str = buf;
@ -1165,14 +1127,13 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
// Insert a string (const char *) into the output // Insert a string (const char *) into the output
if (count != 0 || flag_thou || flag_long) { if (count != 0 || flag_thou || flag_long) {
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
if (arg_num >= MAXFMTARGS) { if (arg_num >= MAXFMTARGS) {
free(buf); free(buf);
errno = E2BIG; errno = E2BIG;
return -1; goto error;
} }
str = format_arg[arg_num].a.a_string; str = format_arg[arg_num].a.a_string;
@ -1191,7 +1152,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
saved_errno = errno; saved_errno = errno;
free(buf); free(buf);
errno = saved_errno; errno = saved_errno;
return -1; goto error;
} }
} }
@ -1201,14 +1162,12 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
default: default:
free(buf); free(buf);
errno = EINVAL; goto error_inval;
return -1;
} }
} }
free(buf); free(buf);
if (inspec) { if (inspec) {
errno = EINVAL; goto error_inval;
return -1;
} }
} }
break; break;
@ -1218,7 +1177,7 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
if (prepstr_addch(&chbuf, &chbufsize, curattr, maxlines, maxwidth, if (prepstr_addch(&chbuf, &chbufsize, curattr, maxlines, maxwidth,
&line, &width, &lastspc, &widthspc, widthbuf, &line, &width, &lastspc, &widthspc, widthbuf,
widthbufsize, &format) < 0) { widthbufsize, &format) < 0) {
return -1; goto error;
} }
} }
} }
@ -1232,6 +1191,13 @@ int vprepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
} }
return line + 1; return line + 1;
error_inval:
errno = EINVAL;
error:
errno_exit(_("prepstr: `%s'"), orig_format);
} }
@ -2507,12 +2473,10 @@ void wait_for_key (WINDOW *win, int y, chtype attr)
chtype *chbuf; chtype *chbuf;
int width; int width;
int lines; int lines;
int key; int key;
bool done; bool done;
keypad(win, true); keypad(win, true);
meta(win, true); meta(win, true);
wtimeout(win, -1); wtimeout(win, -1);
@ -2524,10 +2488,6 @@ void wait_for_key (WINDOW *win, int y, chtype attr)
lines = prepstr(chbuf, BUFSIZE, attr, 0, 0, 1, getmaxx(win) - 4, lines = prepstr(chbuf, BUFSIZE, attr, 0, 0, 1, getmaxx(win) - 4,
&width, 1, _("[ Press <SPACE> to continue ] ")); &width, 1, _("[ Press <SPACE> to continue ] "));
if (lines < 0) {
errno_exit("wait_for_key");
}
pr_center(win, y, 0, chbuf, lines, &width); pr_center(win, y, 0, chbuf, lines, &width);
wrefresh(win); wrefresh(win);

View File

@ -299,7 +299,7 @@ extern int txdlgbox (int maxlines, int ncols, int begin_y, int begin_x,
widthbufsize - Number of int elements in widthbuf widthbufsize - Number of int elements in widthbuf
format - Format string as described below format - Format string as described below
... - Arguments for the format string ... - Arguments for the format string
Returns: int - Number of lines actually used, or -1 on error Returns: int - Number of lines actually used
This function converts the format string and following arguments into This function converts the format string and following arguments into
chbuf, a chtype buffer that can be used for calls to pr_left(), chbuf, a chtype buffer that can be used for calls to pr_left(),
@ -350,8 +350,7 @@ extern int txdlgbox (int maxlines, int ncols, int begin_y, int begin_x,
multibyte characters may be split over two lines. multibyte characters may be split over two lines.
This function returns the actual number of lines used (from 0 to This function returns the actual number of lines used (from 0 to
maxlines), or -1 on error (with errno set to EINVAL for an invalid maxlines). If an error is detected, the application terminates.
format conversion specifier or argument).
*/ */
extern int prepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm, extern int prepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
chtype attr_alt1, chtype attr_alt2, int maxlines, chtype attr_alt1, chtype attr_alt2, int maxlines,
@ -372,7 +371,7 @@ extern int prepstr (chtype *restrict chbuf, int chbufsize, chtype attr_norm,
widthbufsize - Number of int elements in widthbuf widthbufsize - Number of int elements in widthbuf
format - Format string as described for prepstr() format - Format string as described for prepstr()
args - Variable argument list args - Variable argument list
Returns: int - Number of lines actually used, or -1 on error Returns: int - Number of lines actually used
This function is exactly the same as prepstr(), except that it is This function is exactly the same as prepstr(), except that it is
called with a va_list parameter args instead of a variable number of called with a va_list parameter args instead of a variable number of