To: vim_dev@googlegroups.com Subject: Patch 9.0.0008 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.0008 Problem: Cannot specify the variable name for "xxd -i". Solution: Add the "-name" argument. (David Gow, closes #10599) Files: runtime/doc/xxd.1, src/xxd/xxd.c, src/testdir/test_xxd.vim *** ../vim-9.0.0007/runtime/doc/xxd.1 2022-01-14 11:54:56.000000000 +0000 --- runtime/doc/xxd.1 2022-06-29 20:00:01.760640771 +0100 *************** *** 113,118 **** --- 113,122 ---- .RI < len > octets. .TP + .I "\-n name " | " \-name name" + Override the variable name output when \-i is used. The array is named + \fIname\fP and the length is named \fIname\fP_len. + .TP .I \-o offset Add .RI < offset > *** ../vim-9.0.0007/src/xxd/xxd.c 2022-01-14 11:57:06.000000000 +0000 --- src/xxd/xxd.c 2022-06-29 20:17:52.839190238 +0100 *************** *** 55,60 **** --- 55,61 ---- * 11.01.2019 Add full 64/32 bit range to -o and output by Christer Jensen. * 04.02.2020 Add -d for decimal offsets by Aapo Rantalainen * 14.01.2022 Disable extra newlines with -c0 -p by Erik Auerswald. + * 20.06.2022 Permit setting the variable names used by -i by David Gow * * (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com) * *************** *** 226,231 **** --- 227,233 ---- fprintf(stderr, " -h print this summary.\n"); fprintf(stderr, " -i output in C include file style.\n"); fprintf(stderr, " -l len stop after octets.\n"); + fprintf(stderr, " -n name set the variable name used in C include output (-i).\n"); fprintf(stderr, " -o off add to the displayed file position.\n"); fprintf(stderr, " -ps output in postscript plain hexdump style.\n"); fprintf(stderr, " -r reverse operation: convert (or patch) hexdump into binary.\n"); *************** *** 497,502 **** --- 499,505 ---- unsigned long displayoff = 0; static char l[LLEN+1]; /* static because it may be too big for stack */ char *pp; + char *varname = NULL; int addrlen = 9; #ifdef AMIGA *************** *** 635,640 **** --- 638,656 ---- argc--; } } + else if (!STRNCMP(pp, "-n", 2)) + { + if (pp[2] && STRNCMP("ame", pp + 2, 3)) + varname = pp + 2; + else + { + if (!argv[2]) + exit_with_usage(); + varname = argv[2]; + argv++; + argc--; + } + } else if (!strcmp(pp, "--")) /* end of options */ { argv++; *************** *** 753,762 **** if (hextype == HEX_CINCLUDE) { ! if (fp != stdin) { ! FPRINTF_OR_DIE((fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "")); ! for (e = 0; (c = argv[1][e]) != 0; e++) putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); fputs_or_die("[] = {\n", fpo); } --- 769,782 ---- if (hextype == HEX_CINCLUDE) { ! /* A user-set variable name overrides fp == stdin */ ! if (varname == NULL && fp != stdin) ! varname = argv[1]; ! ! if (varname != NULL) { ! FPRINTF_OR_DIE((fpo, "unsigned char %s", isdigit((int)varname[0]) ? "__" : "")); ! for (e = 0; (c = varname[e]) != 0; e++) putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); fputs_or_die("[] = {\n", fpo); } *************** *** 773,783 **** if (p) fputs_or_die("\n", fpo); ! if (fp != stdin) { fputs_or_die("};\n", fpo); ! FPRINTF_OR_DIE((fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "")); ! for (e = 0; (c = argv[1][e]) != 0; e++) putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); FPRINTF_OR_DIE((fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p)); } --- 793,803 ---- if (p) fputs_or_die("\n", fpo); ! if (varname != NULL) { fputs_or_die("};\n", fpo); ! FPRINTF_OR_DIE((fpo, "unsigned int %s", isdigit((int)varname[0]) ? "__" : "")); ! for (e = 0; (c = varname[e]) != 0; e++) putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); FPRINTF_OR_DIE((fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p)); } *** ../vim-9.0.0007/src/testdir/test_xxd.vim 2022-01-14 11:51:21.000000000 +0000 --- src/testdir/test_xxd.vim 2022-06-29 20:12:41.747652424 +0100 *************** *** 219,224 **** --- 219,271 ---- call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) endfor + " Test 17: Print C include with custom variable name + let s:test += 1 + call writefile(['TESTabcd09'], 'XXDfile') + for arg in ['-nvarName', '-n varName', '-name varName'] + %d + exe '0r! ' . s:xxd_cmd . ' -i ' . arg . ' XXDfile' + $d + let expected =<< trim [CODE] + unsigned char varName[] = { + 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a + }; + unsigned int varName_len = 11; + [CODE] + + call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) + endfor + + " using "-n name" reading from stdin + %d + exe '0r! ' . s:xxd_cmd . ' -i < XXDfile -n StdIn' + $d + let expected =<< trim [CODE] + unsigned char StdIn[] = { + 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a + }; + unsigned int StdIn_len = 11; + [CODE] + call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) + + + " Test 18: Print C include: custom variable names can be capitalized + let s:test += 1 + for arg in ['-C', '-capitalize'] + call writefile(['TESTabcd09'], 'XXDfile') + %d + exe '0r! ' . s:xxd_cmd . ' -i ' . arg . ' -n varName XXDfile' + $d + let expected =<< trim [CODE] + unsigned char VARNAME[] = { + 0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a + }; + unsigned int VARNAME_LEN = 11; + [CODE] + call assert_equal(expected, getline(1,'$'), s:Mess(s:test)) + endfor + + %d bwipe! call delete('XXDfile') *** ../vim-9.0.0007/src/version.c 2022-06-29 18:39:05.015841419 +0100 --- src/version.c 2022-06-29 20:01:55.704515149 +0100 *************** *** 737,738 **** --- 737,740 ---- { /* Add new patch number below this line */ + /**/ + 8, /**/ -- How To Keep A Healthy Level Of Insanity: 17. When the money comes out the ATM, scream "I won!, I won! 3rd time this week!!!!!" /// 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 ///