Skip to content

Commit 83e1180

Browse files
sulixbrammool
authored andcommitted
patch 9.0.0008: cannot specify the variable name for "xxd -i"
Problem: Cannot specify the variable name for "xxd -i". Solution: Add the "-name" argument. (David Gow, closes #10599)
1 parent 84f5463 commit 83e1180

4 files changed

Lines changed: 79 additions & 6 deletions

File tree

runtime/doc/xxd.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ Stop after writing
113113
.RI < len >
114114
octets.
115115
.TP
116+
.I "\-n name " | " \-name name"
117+
Override the variable name output when \-i is used. The array is named
118+
\fIname\fP and the length is named \fIname\fP_len.
119+
.TP
116120
.I \-o offset
117121
Add
118122
.RI < offset >

src/testdir/test_xxd.vim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,53 @@ func Test_xxd()
219219
call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
220220
endfor
221221

222+
" Test 17: Print C include with custom variable name
223+
let s:test += 1
224+
call writefile(['TESTabcd09'], 'XXDfile')
225+
for arg in ['-nvarName', '-n varName', '-name varName']
226+
%d
227+
exe '0r! ' . s:xxd_cmd . ' -i ' . arg . ' XXDfile'
228+
$d
229+
let expected =<< trim [CODE]
230+
unsigned char varName[] = {
231+
0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a
232+
};
233+
unsigned int varName_len = 11;
234+
[CODE]
235+
236+
call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
237+
endfor
238+
239+
" using "-n name" reading from stdin
240+
%d
241+
exe '0r! ' . s:xxd_cmd . ' -i < XXDfile -n StdIn'
242+
$d
243+
let expected =<< trim [CODE]
244+
unsigned char StdIn[] = {
245+
0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a
246+
};
247+
unsigned int StdIn_len = 11;
248+
[CODE]
249+
call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
250+
251+
252+
" Test 18: Print C include: custom variable names can be capitalized
253+
let s:test += 1
254+
for arg in ['-C', '-capitalize']
255+
call writefile(['TESTabcd09'], 'XXDfile')
256+
%d
257+
exe '0r! ' . s:xxd_cmd . ' -i ' . arg . ' -n varName XXDfile'
258+
$d
259+
let expected =<< trim [CODE]
260+
unsigned char VARNAME[] = {
261+
0x54, 0x45, 0x53, 0x54, 0x61, 0x62, 0x63, 0x64, 0x30, 0x39, 0x0a
262+
};
263+
unsigned int VARNAME_LEN = 11;
264+
[CODE]
265+
call assert_equal(expected, getline(1,'$'), s:Mess(s:test))
266+
endfor
267+
268+
222269
%d
223270
bwipe!
224271
call delete('XXDfile')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ static char *(features[]) =
735735

736736
static int included_patches[] =
737737
{ /* Add new patch number below this line */
738+
/**/
739+
8,
738740
/**/
739741
7,
740742
/**/

src/xxd/xxd.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* 11.01.2019 Add full 64/32 bit range to -o and output by Christer Jensen.
5656
* 04.02.2020 Add -d for decimal offsets by Aapo Rantalainen
5757
* 14.01.2022 Disable extra newlines with -c0 -p by Erik Auerswald.
58+
* 20.06.2022 Permit setting the variable names used by -i by David Gow
5859
*
5960
* (c) 1990-1998 by Juergen Weigert ([email protected])
6061
*
@@ -226,6 +227,7 @@ exit_with_usage(void)
226227
fprintf(stderr, " -h print this summary.\n");
227228
fprintf(stderr, " -i output in C include file style.\n");
228229
fprintf(stderr, " -l len stop after <len> octets.\n");
230+
fprintf(stderr, " -n name set the variable name used in C include output (-i).\n");
229231
fprintf(stderr, " -o off add <off> to the displayed file position.\n");
230232
fprintf(stderr, " -ps output in postscript plain hexdump style.\n");
231233
fprintf(stderr, " -r reverse operation: convert (or patch) hexdump into binary.\n");
@@ -497,6 +499,7 @@ main(int argc, char *argv[])
497499
unsigned long displayoff = 0;
498500
static char l[LLEN+1]; /* static because it may be too big for stack */
499501
char *pp;
502+
char *varname = NULL;
500503
int addrlen = 9;
501504

502505
#ifdef AMIGA
@@ -635,6 +638,19 @@ main(int argc, char *argv[])
635638
argc--;
636639
}
637640
}
641+
else if (!STRNCMP(pp, "-n", 2))
642+
{
643+
if (pp[2] && STRNCMP("ame", pp + 2, 3))
644+
varname = pp + 2;
645+
else
646+
{
647+
if (!argv[2])
648+
exit_with_usage();
649+
varname = argv[2];
650+
argv++;
651+
argc--;
652+
}
653+
}
638654
else if (!strcmp(pp, "--")) /* end of options */
639655
{
640656
argv++;
@@ -753,10 +769,14 @@ main(int argc, char *argv[])
753769

754770
if (hextype == HEX_CINCLUDE)
755771
{
756-
if (fp != stdin)
772+
/* A user-set variable name overrides fp == stdin */
773+
if (varname == NULL && fp != stdin)
774+
varname = argv[1];
775+
776+
if (varname != NULL)
757777
{
758-
FPRINTF_OR_DIE((fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : ""));
759-
for (e = 0; (c = argv[1][e]) != 0; e++)
778+
FPRINTF_OR_DIE((fpo, "unsigned char %s", isdigit((int)varname[0]) ? "__" : ""));
779+
for (e = 0; (c = varname[e]) != 0; e++)
760780
putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
761781
fputs_or_die("[] = {\n", fpo);
762782
}
@@ -773,11 +793,11 @@ main(int argc, char *argv[])
773793
if (p)
774794
fputs_or_die("\n", fpo);
775795

776-
if (fp != stdin)
796+
if (varname != NULL)
777797
{
778798
fputs_or_die("};\n", fpo);
779-
FPRINTF_OR_DIE((fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : ""));
780-
for (e = 0; (c = argv[1][e]) != 0; e++)
799+
FPRINTF_OR_DIE((fpo, "unsigned int %s", isdigit((int)varname[0]) ? "__" : ""));
800+
for (e = 0; (c = varname[e]) != 0; e++)
781801
putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
782802
FPRINTF_OR_DIE((fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p));
783803
}

0 commit comments

Comments
 (0)