1
0
forked from aniani/vim

patch 8.2.3670: error checks repeated several times

Problem:    Error checks repeated several times.
Solution:   Move the checks to functions. (closes #9213)
This commit is contained in:
Bram Moolenaar
2021-11-25 11:16:50 +00:00
parent bd228fd097
commit 8af87bd6b1
2 changed files with 70 additions and 69 deletions

View File

@@ -757,6 +757,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
3670,
/**/ /**/
3669, 3669,
/**/ /**/

View File

@@ -252,6 +252,43 @@ error_exit(int ret, char *msg)
exit(ret); exit(ret);
} }
static void
exit_on_ferror(char c, FILE *fpi)
{
if (c == EOF && ferror(fpi))
perror_exit(2);
}
static void
putc_or_die(char c, FILE *fpo)
{
if (putc(c, fpo) == EOF)
perror_exit(3);
}
static void
fputs_or_die(char *s, FILE *fpo)
{
if (fputs(s, fpo) == EOF)
perror_exit(3);
}
static void
fprintf_or_die(FILE *fpo, char *format, char *s, int d)
{
if (fprintf(fpo, format, s, d) < 0)
perror_exit(3);
}
static void
fclose_or_die(FILE *fpi, FILE *fpo)
{
if (fclose(fpo) != 0)
perror_exit(3);
if (fclose(fpi) != 0)
perror_exit(2);
}
/* /*
* If "c" is a hex digit, return the value. * If "c" is a hex digit, return the value.
* Otherwise return -1. * Otherwise return -1.
@@ -275,8 +312,7 @@ skip_to_eol(FILE *fpi, int c)
{ {
while (c != '\n' && c != EOF) while (c != '\n' && c != EOF)
c = getc(fpi); c = getc(fpi);
if (c == EOF && ferror(fpi)) exit_on_ferror(c, fpi);
perror_exit(2);
return c; return c;
} }
@@ -342,14 +378,12 @@ huntype(
if (base_off + want_off < have_off) if (base_off + want_off < have_off)
error_exit(5, "sorry, cannot seek backwards."); error_exit(5, "sorry, cannot seek backwards.");
for (; have_off < base_off + want_off; have_off++) for (; have_off < base_off + want_off; have_off++)
if (putc(0, fpo) == EOF) putc_or_die(0, fpo);
perror_exit(3);
} }
if (n2 >= 0 && n1 >= 0) if (n2 >= 0 && n1 >= 0)
{ {
if (putc((n2 << 4) | n1, fpo) == EOF) putc_or_die((n2 << 4) | n1, fpo);
perror_exit(3);
have_off++; have_off++;
want_off++; want_off++;
n1 = -1; n1 = -1;
@@ -374,10 +408,7 @@ huntype(
#ifdef TRY_SEEK #ifdef TRY_SEEK
fseek(fpo, 0L, SEEK_END); fseek(fpo, 0L, SEEK_END);
#endif #endif
if (fclose(fpo) != 0) fclose_or_die(fpi, fpo);
perror_exit(3);
if (fclose(fpi) != 0)
perror_exit(2);
return 0; return 0;
} }
@@ -409,15 +440,12 @@ xxdline(FILE *fp, char *l, int nz)
if (nz < 0) if (nz < 0)
zero_seen--; zero_seen--;
if (zero_seen == 2) if (zero_seen == 2)
if (fputs(z, fp) == EOF) fputs_or_die(z, fp);
perror_exit(3);
if (zero_seen > 2) if (zero_seen > 2)
if (fputs("*\n", fp) == EOF) fputs_or_die("*\n", fp);
perror_exit(3);
} }
if (nz >= 0 || zero_seen > 0) if (nz >= 0 || zero_seen > 0)
if (fputs(l, fp) == EOF) fputs_or_die(l, fp);
perror_exit(3);
if (nz) if (nz)
zero_seen = 0; zero_seen = 0;
} }
@@ -708,16 +736,10 @@ main(int argc, char *argv[])
long s = seekoff; long s = seekoff;
while (s--) while (s--)
if (getc(fp) == EOF) if ((c = getc(fp)) == EOF)
{ {
if (ferror(fp)) exit_on_ferror(c, fp);
{ error_exit(4, "sorry cannot seek.");
perror_exit(2);
}
else
{
error_exit(4, "sorry cannot seek.");
}
} }
} }
} }
@@ -726,47 +748,35 @@ main(int argc, char *argv[])
{ {
if (fp != stdin) if (fp != stdin)
{ {
if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) fprintf_or_die(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "", 0);
perror_exit(3);
for (e = 0; (c = argv[1][e]) != 0; e++) for (e = 0; (c = argv[1][e]) != 0; e++)
if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
perror_exit(3); fputs_or_die("[] = {\n", fpo);
if (fputs("[] = {\n", fpo) == EOF)
perror_exit(3);
} }
p = 0; p = 0;
c = 0; c = 0;
while ((length < 0 || p < length) && (c = getc(fp)) != EOF) while ((length < 0 || p < length) && (c = getc(fp)) != EOF)
{ {
if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", fprintf_or_die(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
(p % cols) ? ", " : &",\n "[2*!p], c) < 0) (p % cols) ? ", " : &",\n "[2*!p], c);
perror_exit(3);
p++; p++;
} }
if (c == EOF && ferror(fp)) exit_on_ferror(c, fp);
perror_exit(2);
if (p && fputs("\n", fpo) == EOF) if (p)
perror_exit(3); fputs_or_die("\n", fpo);
if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF) fputs_or_die(&"};\n"[3 * (fp == stdin)], fpo);
perror_exit(3);
if (fp != stdin) if (fp != stdin)
{ {
if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) fprintf_or_die(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "", 0);
perror_exit(3);
for (e = 0; (c = argv[1][e]) != 0; e++) for (e = 0; (c = argv[1][e]) != 0; e++)
if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
perror_exit(3); fprintf_or_die(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p);
if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0)
perror_exit(3);
} }
if (fclose(fp)) fclose_or_die(fp, fpo);
perror_exit(2);
if (fclose(fpo))
perror_exit(3);
return 0; return 0;
} }
@@ -776,26 +786,19 @@ main(int argc, char *argv[])
e = 0; e = 0;
while ((length < 0 || n < length) && (e = getc(fp)) != EOF) while ((length < 0 || n < length) && (e = getc(fp)) != EOF)
{ {
if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF putc_or_die(hexx[(e >> 4) & 0xf], fpo);
|| putc(hexx[e & 0xf], fpo) == EOF) putc_or_die(hexx[e & 0xf], fpo);
perror_exit(3);
n++; n++;
if (!--p) if (!--p)
{ {
if (putc('\n', fpo) == EOF) putc_or_die('\n', fpo);
perror_exit(3);
p = cols; p = cols;
} }
} }
if (e == EOF && ferror(fp)) exit_on_ferror(e, fp);
perror_exit(2);
if (p < cols) if (p < cols)
if (putc('\n', fpo) == EOF) putc_or_die('\n', fpo);
perror_exit(3); fclose_or_die(fp, fpo);
if (fclose(fp))
perror_exit(2);
if (fclose(fpo))
perror_exit(3);
return 0; return 0;
} }
@@ -853,8 +856,7 @@ main(int argc, char *argv[])
p = 0; p = 0;
} }
} }
if (e == EOF && ferror(fp)) exit_on_ferror(e, fp);
perror_exit(2);
if (p) if (p)
{ {
l[c] = '\n'; l[c] = '\n';
@@ -864,10 +866,7 @@ main(int argc, char *argv[])
else if (autoskip) else if (autoskip)
xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */
if (fclose(fp)) fclose_or_die(fp, fpo);
perror_exit(2);
if (fclose(fpo))
perror_exit(3);
return 0; return 0;
} }