To: vim_dev@googlegroups.com Subject: Patch 8.2.3554 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.3554 Problem: Xxd has various way to exit. Solution: Add function to print error and exit. (closes #9035) Files: src/xxd/xxd.c *** ../vim-8.2.3553/src/xxd/xxd.c 2021-10-22 12:55:38.753954514 +0100 --- src/xxd/xxd.c 2021-10-22 15:54:01.945022434 +0100 *************** *** 188,200 **** # endif #endif - /* Let's collect some prototypes */ - /* CodeWarrior is really picky about missing prototypes */ - static void exit_with_usage __P((void)); - static void die __P((int)); - static int huntype __P((FILE *, FILE *, FILE *, int, int, long)); - static void xxdline __P((FILE *, char *, int)); - #define TRY_SEEK /* attempt to use lseek, or skip forward by reading */ #define COLS 256 /* change here, if you ever need more columns */ #define LLEN ((2*(int)sizeof(unsigned long)) + 4 + (9*COLS-1) + COLS + 2) --- 188,193 ---- *************** *** 245,257 **** } static void ! die(int ret) { fprintf(stderr, "%s: ", pname); perror(NULL); exit(ret); } /* * Max. cols binary characters are decoded from the input stream per line. * Two adjacent garbage characters after evaluated data delimit valid data. --- 238,257 ---- } static void ! perror_exit(int ret) { fprintf(stderr, "%s: ", pname); perror(NULL); exit(ret); } + static void + error_exit(int ret, char *msg) + { + fprintf(stderr, "%s: %s\n", pname, msg); + exit(ret); + } + /* * Max. cols binary characters are decoded from the input stream per line. * Two adjacent garbage characters after evaluated data delimit valid data. *************** *** 263,269 **** huntype( FILE *fpi, FILE *fpo, - FILE *fperr, int cols, int hextype, long base_off) --- 263,268 ---- *************** *** 316,340 **** if (base_off + want_off != have_off) { if (fflush(fpo) != 0) ! die(3); #ifdef TRY_SEEK if (fseek(fpo, base_off + want_off - have_off, 1) >= 0) have_off = base_off + want_off; #endif if (base_off + want_off < have_off) ! { ! fprintf(fperr, "%s: sorry, cannot seek backwards.\n", pname); ! return 5; ! } for (; have_off < base_off + want_off; have_off++) if (putc(0, fpo) == EOF) ! die(3); } if (n2 >= 0 && n1 >= 0) { if (putc((n2 << 4) | n1, fpo) == EOF) ! die(3); have_off++; want_off++; n1 = -1; --- 315,336 ---- if (base_off + want_off != have_off) { if (fflush(fpo) != 0) ! perror_exit(3); #ifdef TRY_SEEK if (fseek(fpo, base_off + want_off - have_off, 1) >= 0) have_off = base_off + want_off; #endif if (base_off + want_off < have_off) ! error_exit(5, "sorry, cannot seek backwards."); for (; have_off < base_off + want_off; have_off++) if (putc(0, fpo) == EOF) ! perror_exit(3); } if (n2 >= 0 && n1 >= 0) { if (putc((n2 << 4) | n1, fpo) == EOF) ! perror_exit(3); have_off++; want_off++; n1 = -1; *************** *** 351,357 **** while (c != '\n' && c != EOF) c = getc(fpi); if (c == EOF && ferror(fpi)) ! die(2); } if (c == '\n') { --- 347,353 ---- while (c != '\n' && c != EOF) c = getc(fpi); if (c == EOF && ferror(fpi)) ! perror_exit(2); } if (c == '\n') { *************** *** 362,375 **** } } if (fflush(fpo) != 0) ! die(3); #ifdef TRY_SEEK fseek(fpo, 0L, 2); #endif if (fclose(fpo) != 0) ! die(3); if (fclose(fpi) != 0) ! die(2); return 0; } --- 358,371 ---- } } if (fflush(fpo) != 0) ! perror_exit(3); #ifdef TRY_SEEK fseek(fpo, 0L, 2); #endif if (fclose(fpo) != 0) ! perror_exit(3); if (fclose(fpi) != 0) ! perror_exit(2); return 0; } *************** *** 402,415 **** zero_seen--; if (zero_seen == 2) if (fputs(z, fp) == EOF) ! die(3); if (zero_seen > 2) if (fputs("*\n", fp) == EOF) ! die(3); } if (nz >= 0 || zero_seen > 0) if (fputs(l, fp) == EOF) ! die(3); if (nz) zero_seen = 0; } --- 398,411 ---- zero_seen--; if (zero_seen == 2) if (fputs(z, fp) == EOF) ! perror_exit(3); if (zero_seen > 2) if (fputs("*\n", fp) == EOF) ! perror_exit(3); } if (nz >= 0 || zero_seen > 0) if (fputs(l, fp) == EOF) ! perror_exit(3); if (nz) zero_seen = 0; } *************** *** 640,651 **** if (octspergrp < 1 || octspergrp > cols) octspergrp = cols; else if (hextype == HEX_LITTLEENDIAN && (octspergrp & (octspergrp-1))) ! { ! fprintf(stderr, ! "%s: number of octets per group must be a power of 2 with -e.\n", ! pname); ! exit(1); ! } if (argc > 3) exit_with_usage(); --- 636,642 ---- if (octspergrp < 1 || octspergrp > cols) octspergrp = cols; else if (hextype == HEX_LITTLEENDIAN && (octspergrp & (octspergrp-1))) ! error_exit(1, "number of octets per group must be a power of 2 with -e."); if (argc > 3) exit_with_usage(); *************** *** 682,692 **** if (revert) { if (hextype && (hextype != HEX_POSTSCRIPT)) ! { ! fprintf(stderr, "%s: sorry, cannot revert this type of hexdump\n", pname); ! return -1; ! } ! return huntype(fp, fpo, stderr, cols, hextype, negseek ? -seekoff : seekoff); } --- 673,680 ---- if (revert) { if (hextype && (hextype != HEX_POSTSCRIPT)) ! error_exit(-1, "sorry, cannot revert this type of hexdump"); ! return huntype(fp, fpo, cols, hextype, negseek ? -seekoff : seekoff); } *************** *** 698,707 **** else e = fseek(fp, negseek ? -seekoff : seekoff, negseek ? 2 : 0); if (e < 0 && negseek) ! { ! fprintf(stderr, "%s: sorry cannot seek.\n", pname); ! return 4; ! } if (e >= 0) seekoff = ftell(fp); else --- 686,692 ---- else e = fseek(fp, negseek ? -seekoff : seekoff, negseek ? 2 : 0); if (e < 0 && negseek) ! error_exit(4, "sorry cannot seek."); if (e >= 0) seekoff = ftell(fp); else *************** *** 714,725 **** { if (ferror(fp)) { ! die(2); } else { ! fprintf(stderr, "%s: sorry cannot seek.\n", pname); ! return 4; } } } --- 699,709 ---- { if (ferror(fp)) { ! perror_exit(2); } else { ! error_exit(4, "sorry cannot seek."); } } } *************** *** 730,741 **** if (fp != stdin) { if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) ! die(3); for (e = 0; (c = argv[1][e]) != 0; e++) if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) ! die(3); if (fputs("[] = {\n", fpo) == EOF) ! die(3); } p = 0; --- 714,725 ---- if (fp != stdin) { if (fprintf(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) ! perror_exit(3); for (e = 0; (c = argv[1][e]) != 0; e++) if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) ! perror_exit(3); if (fputs("[] = {\n", fpo) == EOF) ! perror_exit(3); } p = 0; *************** *** 744,775 **** { if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", (p % cols) ? ", " : &",\n "[2*!p], c) < 0) ! die(3); p++; } if (c == EOF && ferror(fp)) ! die(2); if (p && fputs("\n", fpo) == EOF) ! die(3); if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF) ! die(3); if (fp != stdin) { if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) ! die(3); for (e = 0; (c = argv[1][e]) != 0; e++) if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) ! die(3); if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0) ! die(3); } if (fclose(fp)) ! die(2); if (fclose(fpo)) ! die(3); return 0; } --- 728,759 ---- { if (fprintf(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", (p % cols) ? ", " : &",\n "[2*!p], c) < 0) ! perror_exit(3); p++; } if (c == EOF && ferror(fp)) ! perror_exit(2); if (p && fputs("\n", fpo) == EOF) ! perror_exit(3); if (fputs(&"};\n"[3 * (fp == stdin)], fpo) == EOF) ! perror_exit(3); if (fp != stdin) { if (fprintf(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "") < 0) ! perror_exit(3); for (e = 0; (c = argv[1][e]) != 0; e++) if (putc(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo) == EOF) ! perror_exit(3); if (fprintf(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p) < 0) ! perror_exit(3); } if (fclose(fp)) ! perror_exit(2); if (fclose(fpo)) ! perror_exit(3); return 0; } *************** *** 781,804 **** { if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF || putc(hexx[e & 0xf], fpo) == EOF) ! die(3); n++; if (!--p) { if (putc('\n', fpo) == EOF) ! die(3); p = cols; } } if (e == EOF && ferror(fp)) ! die(2); if (p < cols) if (putc('\n', fpo) == EOF) ! die(3); if (fclose(fp)) ! die(2); if (fclose(fpo)) ! die(3); return 0; } --- 765,788 ---- { if (putc(hexx[(e >> 4) & 0xf], fpo) == EOF || putc(hexx[e & 0xf], fpo) == EOF) ! perror_exit(3); n++; if (!--p) { if (putc('\n', fpo) == EOF) ! perror_exit(3); p = cols; } } if (e == EOF && ferror(fp)) ! perror_exit(2); if (p < cols) if (putc('\n', fpo) == EOF) ! perror_exit(3); if (fclose(fp)) ! perror_exit(2); if (fclose(fpo)) ! perror_exit(3); return 0; } *************** *** 863,869 **** } } if (e == EOF && ferror(fp)) ! die(2); if (p) { l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; --- 847,853 ---- } } if (e == EOF && ferror(fp)) ! perror_exit(2); if (p) { l[c = (addrlen + 3 + (grplen * cols - 1)/octspergrp + p)] = '\n'; l[++c] = '\0'; *************** *** 873,881 **** xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ if (fclose(fp)) ! die(2); if (fclose(fpo)) ! die(3); return 0; } --- 857,865 ---- xxdline(fpo, l, -1); /* last chance to flush out suppressed lines */ if (fclose(fp)) ! perror_exit(2); if (fclose(fpo)) ! perror_exit(3); return 0; } *** ../vim-8.2.3553/src/version.c 2021-10-22 15:11:33.990608190 +0100 --- src/version.c 2021-10-22 15:51:32.455096771 +0100 *************** *** 759,760 **** --- 759,762 ---- { /* Add new patch number below this line */ + /**/ + 3554, /**/ -- How do I set the laser printer to stun? /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///