Skip to content

Commit b536f3f

Browse files
committed
A new substitution special string %(sh {shell-command}) that runs
an external command, captures its output and substitutes it in place of the %-string. Can be used as follows: bind generic T :echo '%(sh printf "Home dir is $HOME")' As it can be seen, the command is run via "sh -c '{command}'", so environment variable substitutions can be used.
1 parent 06f7d75 commit b536f3f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/argv.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,29 @@ format_expand_arg(struct format_context *format, const char *name, const char *e
315315
return false;
316316
return string_format_from(format->buf, &format->bufpos, "%s", value);
317317
}
318+
if (!prefixcmp(name, "%(sh")) {
319+
char cbuf[SIZEOF_STR];
320+
const char *c="";
321+
char value[SIZEOF_STR]={0};
322+
const char *cstart = name + STRING_SIZE("%(sh");
323+
const int clen = end - cstart - 1;
324+
int size;
325+
FILE *cp;
326+
if (end && clen > 0 && string_format(cbuf, "sh -c '%.*s'", clen, cstart)) {
327+
c=cbuf;
328+
while (isspace(*c))
329+
c++;
330+
}
331+
if (!*c || strlen(c)==8)
332+
return false;
333+
cp=popen(c,"r");
334+
size=fread(value,1,SIZEOF_STR-1,cp);
335+
if (value[0]==0 || size==0)
336+
return false;
337+
if (value[size-1]=='\n')
338+
value[size-1]='\0';
339+
return string_format_from(format->buf, &format->bufpos, "%s", value);
340+
}
318341

319342
for (i = 0; i < format->vars_size; i++) {
320343
if (string_enum_compare(name, vars[i].name, vars[i].namelen))

0 commit comments

Comments
 (0)