Mercurial > vim
comparison src/xxd/xxd.c @ 26278:3061c07855ce v8.2.3670
patch 8.2.3670: error checks repeated several times
Commit: https://github.com/vim/vim/commit/8af87bd6b1808ffcafdd73b1d7501dd50b9d15bc
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Nov 25 11:16:50 2021 +0000
patch 8.2.3670: error checks repeated several times
Problem: Error checks repeated several times.
Solution: Move the checks to functions. (closes https://github.com/vim/vim/issues/9213)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 25 Nov 2021 12:30:04 +0100 |
parents | 3a13efec0016 |
children | 71bdede8afd8 |
comparison
equal
deleted
inserted
replaced
26277:12a43aac9c6b | 26278:3061c07855ce |
---|---|
250 { | 250 { |
251 fprintf(stderr, "%s: %s\n", pname, msg); | 251 fprintf(stderr, "%s: %s\n", pname, msg); |
252 exit(ret); | 252 exit(ret); |
253 } | 253 } |
254 | 254 |
255 static void | |
256 exit_on_ferror(char c, FILE *fpi) | |
257 { | |
258 if (c == EOF && ferror(fpi)) | |
259 perror_exit(2); | |
260 } | |
261 | |
262 static void | |
263 putc_or_die(char c, FILE *fpo) | |
264 { | |
265 if (putc(c, fpo) == EOF) | |
266 perror_exit(3); | |
267 } | |
268 | |
269 static void | |
270 fputs_or_die(char *s, FILE *fpo) | |
271 { | |
272 if (fputs(s, fpo) == EOF) | |
273 perror_exit(3); | |
274 } | |
275 | |
276 static void | |
277 fprintf_or_die(FILE *fpo, char *format, char *s, int d) | |
278 { | |
279 if (fprintf(fpo, format, s, d) < 0) | |
280 perror_exit(3); | |
281 } | |
282 | |
283 static void | |
284 fclose_or_die(FILE *fpi, FILE *fpo) | |
285 { | |
286 if (fclose(fpo) != 0) | |
287 perror_exit(3); | |
288 if (fclose(fpi) != 0) | |
289 perror_exit(2); | |
290 } | |
291 | |
255 /* | 292 /* |
256 * If "c" is a hex digit, return the value. | 293 * If "c" is a hex digit, return the value. |
257 * Otherwise return -1. | 294 * Otherwise return -1. |
258 */ | 295 */ |
259 static int | 296 static int |
273 static int | 310 static int |
274 skip_to_eol(FILE *fpi, int c) | 311 skip_to_eol(FILE *fpi, int c) |
275 { | 312 { |
276 while (c != '\n' && c != EOF) | 313 while (c != '\n' && c != EOF) |
277 c = getc(fpi); | 314 c = getc(fpi); |
278 if (c == EOF && ferror(fpi)) | 315 exit_on_ferror(c, fpi); |
279 perror_exit(2); | |
280 return c; | 316 return c; |
281 } | 317 } |
282 | 318 |
283 /* | 319 /* |
284 * Max. cols binary characters are decoded from the input stream per line. | 320 * Max. cols binary characters are decoded from the input stream per line. |
340 have_off = base_off + want_off; | 376 have_off = base_off + want_off; |
341 #endif | 377 #endif |
342 if (base_off + want_off < have_off) | 378 if (base_off + want_off < have_off) |
343 error_exit(5, "sorry, cannot seek backwards."); | 379 error_exit(5, "sorry, cannot seek backwards."); |
344 for (; have_off < base_off + want_off; have_off++) | 380 for (; have_off < base_off + want_off; have_off++) |
345 if (putc(0, fpo) == EOF) | 381 putc_or_die(0, fpo); |
346 perror_exit(3); | |
347 } | 382 } |
348 | 383 |
349 if (n2 >= 0 && n1 >= 0) | 384 if (n2 >= 0 && n1 >= 0) |
350 { | 385 { |
351 if (putc((n2 << 4) | n1, fpo) == EOF) | 386 putc_or_die((n2 << 4) | n1, fpo); |
352 perror_exit(3); | |
353 have_off++; | 387 have_off++; |
354 want_off++; | 388 want_off++; |
355 n1 = -1; | 389 n1 = -1; |
356 if (!hextype && (++p >= cols)) | 390 if (!hextype && (++p >= cols)) |
357 /* skip the rest of the line as garbage */ | 391 /* skip the rest of the line as garbage */ |
372 if (fflush(fpo) != 0) | 406 if (fflush(fpo) != 0) |
373 perror_exit(3); | 407 perror_exit(3); |
374 #ifdef TRY_SEEK | 408 #ifdef TRY_SEEK |
375 fseek(fpo, 0L, SEEK_END); | 409 fseek(fpo, 0L, SEEK_END); |
376 #endif | 410 #endif |
377 if (fclose(fpo) != 0) | 411 fclose_or_die(fpi, fpo); |
378 perror_exit(3); | |
379 if (fclose(fpi) != 0) | |
380 perror_exit(2); | |
381 return 0; | 412 return 0; |
382 } | 413 } |
383 | 414 |
384 /* | 415 /* |
385 * Print line l. If nz is false, xxdline regards the line a line of | 416 * Print line l. If nz is false, xxdline regards the line a line of |
407 if (nz) | 438 if (nz) |
408 { | 439 { |
409 if (nz < 0) | 440 if (nz < 0) |
410 zero_seen--; | 441 zero_seen--; |
411 if (zero_seen == 2) | 442 if (zero_seen == 2) |
412 if (fputs(z, fp) == EOF) | 443 fputs_or_die(z, fp); |
413 perror_exit(3); | |
414 if (zero_seen > 2) | 444 if (zero_seen > 2) |
415 if (fputs("*\n", fp) == EOF) | 445 fputs_or_die("*\n", fp); |
416 perror_exit(3); | |
417 } | 446 } |
418 if (nz >= 0 || zero_seen > 0) | 447 if (nz >= 0 || zero_seen > 0) |
419 if (fputs(l, fp) == EOF) | 448 fputs_or_die(l, fp); |
420 perror_exit(3); | |
421 if (nz) | 449 if (nz) |
422 zero_seen = 0; | 450 zero_seen = 0; |
423 } | 451 } |
424 } | 452 } |
425 | 453 |
706 #endif | 734 #endif |
707 { | 735 { |
708 long s = seekoff; | 736 long s = seekoff; |
709 | 737 |
710 while (s--) | 738 while (s--) |
711 if (getc(fp) == EOF) | 739 if ((c = getc(fp)) == EOF) |
712 { | 740 { |
713 if (ferror(fp)) | 741 exit_on_ferror(c, fp); |
714 { | 742 error_exit(4, "sorry cannot seek."); |
715 perror_exit(2); | |
716 } | |
717 else | |
718 { | |
719 error_exit(4, "sorry cannot seek."); | |
720 } | |
721 } | 743 } |
722 } | 744 } |
723 } | 745 } |
724 | 746 |
725 if (hextype == HEX_CINCLUDE) | 747 if (hextype == HEX_CINCLUDE) |
726 { | 748 { |
727 if (fp != stdin) | 749 if (fp != stdin) |
728 { | 750 { |
729 if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) | 751 fprintf_or_die(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "", 0); |
730 perror_exit(3); | |
731 for (e = 0; (c = argv[1][e]) != 0; e++) | 752 for (e = 0; (c = argv[1][e]) != 0; e++) |
732 if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) | 753 putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); |
733 perror_exit(3); | 754 fputs_or_die("[] = {\n", fpo); |
734 if (fputs("[] = {\n", fpo) == EOF) | |
735 perror_exit(3); | |
736 } | 755 } |
737 | 756 |
738 p = 0; | 757 p = 0; |
739 c = 0; | 758 c = 0; |
740 while ((length < 0 || p < length) && (c = getc(fp)) != EOF) | 759 while ((length < 0 || p < length) && (c = getc(fp)) != EOF) |
741 { | 760 { |
742 if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", | 761 fprintf_or_die(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", |
743 (p % cols) ? ", " : &",\n "[2*!p], c) < 0) | 762 (p % cols) ? ", " : &",\n "[2*!p], c); |
744 perror_exit(3); | |
745 p++; | 763 p++; |
746 } | 764 } |
747 if (c == EOF && ferror(fp)) | 765 exit_on_ferror(c, fp); |
748 perror_exit(2); | 766 |
749 | 767 if (p) |
750 if (p && fputs("\n", fpo) == EOF) | 768 fputs_or_die("\n", fpo); |
751 perror_exit(3); | 769 fputs_or_die(&"};\n"[3 * (fp == stdin)], fpo); |
752 if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF) | |
753 perror_exit(3); | |
754 | 770 |
755 if (fp != stdin) | 771 if (fp != stdin) |
756 { | 772 { |
757 if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) | 773 fprintf_or_die(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "", 0); |
758 perror_exit(3); | |
759 for (e = 0; (c = argv[1][e]) != 0; e++) | 774 for (e = 0; (c = argv[1][e]) != 0; e++) |
760 if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) | 775 putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); |
761 perror_exit(3); | 776 fprintf_or_die(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p); |
762 if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0) | 777 } |
763 perror_exit(3); | 778 |
764 } | 779 fclose_or_die(fp, fpo); |
765 | |
766 if (fclose(fp)) | |
767 perror_exit(2); | |
768 if (fclose(fpo)) | |
769 perror_exit(3); | |
770 return 0; | 780 return 0; |
771 } | 781 } |
772 | 782 |
773 if (hextype == HEX_POSTSCRIPT) | 783 if (hextype == HEX_POSTSCRIPT) |
774 { | 784 { |
775 p = cols; | 785 p = cols; |
776 e = 0; | 786 e = 0; |
777 while ((length < 0 || n < length) && (e = getc(fp)) != EOF) | 787 while ((length < 0 || n < length) && (e = getc(fp)) != EOF) |
778 { | 788 { |
779 if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF | 789 putc_or_die(hexx[(e >> 4) & 0xf], fpo); |
780 || putc(hexx[e & 0xf], fpo) == EOF) | 790 putc_or_die(hexx[e & 0xf], fpo); |
781 perror_exit(3); | |
782 n++; | 791 n++; |
783 if (!--p) | 792 if (!--p) |
784 { | 793 { |
785 if (putc('\n', fpo) == EOF) | 794 putc_or_die('\n', fpo); |
786 perror_exit(3); | |
787 p = cols; | 795 p = cols; |
788 } | 796 } |
789 } | 797 } |
790 if (e == EOF && ferror(fp)) | 798 exit_on_ferror(e, fp); |
791 perror_exit(2); | |
792 if (p < cols) | 799 if (p < cols) |
793 if (putc('\n', fpo) == EOF) | 800 putc_or_die('\n', fpo); |
794 perror_exit(3); | 801 fclose_or_die(fp, fpo); |
795 if (fclose(fp)) | |
796 perror_exit(2); | |
797 if (fclose(fpo)) | |
798 perror_exit(3); | |
799 return 0; | 802 return 0; |
800 } | 803 } |
801 | 804 |
802 /* hextype: HEX_NORMAL or HEX_BITS or HEX_LITTLEENDIAN */ | 805 /* hextype: HEX_NORMAL or HEX_BITS or HEX_LITTLEENDIAN */ |
803 | 806 |
851 xxdline(fpo, l, autoskip ? nonzero : 1); | 854 xxdline(fpo, l, autoskip ? nonzero : 1); |
852 nonzero = 0; | 855 nonzero = 0; |
853 p = 0; | 856 p = 0; |
854 } | 857 } |
855 } | 858 } |
856 if (e == EOF && ferror(fp)) | 859 exit_on_ferror(e, fp); |
857 perror_exit(2); | |
858 if (p) | 860 if (p) |
859 { | 861 { |
860 l[c] = '\n'; | 862 l[c] = '\n'; |
861 l[++c] = '\0'; | 863 l[++c] = '\0'; |
862 xxdline(fpo, l, 1); | 864 xxdline(fpo, l, 1); |
863 } | 865 } |
864 else if (autoskip) | 866 else if (autoskip) |
865 xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ | 867 xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ |
866 | 868 |
867 if (fclose(fp)) | 869 fclose_or_die(fp, fpo); |
868 perror_exit(2); | |
869 if (fclose(fpo)) | |
870 perror_exit(3); | |
871 return 0; | 870 return 0; |
872 } | 871 } |
873 | 872 |
874 /* vi:set ts=8 sw=4 sts=2 cino+={2 cino+=n-2 : */ | 873 /* vi:set ts=8 sw=4 sts=2 cino+={2 cino+=n-2 : */ |