Skip to content

Commit

Permalink
A new substitution special string %(sh {shell-command}) that runs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
psprint committed Aug 31, 2022
1 parent 06f7d75 commit b536f3f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,29 @@ format_expand_arg(struct format_context *format, const char *name, const char *e
return false;
return string_format_from(format->buf, &format->bufpos, "%s", value);
}
if (!prefixcmp(name, "%(sh")) {
char cbuf[SIZEOF_STR];
const char *c="";
char value[SIZEOF_STR]={0};
const char *cstart = name + STRING_SIZE("%(sh");
const int clen = end - cstart - 1;
int size;
FILE *cp;
if (end && clen > 0 && string_format(cbuf, "sh -c '%.*s'", clen, cstart)) {
c=cbuf;
while (isspace(*c))
c++;
}
if (!*c || strlen(c)==8)
return false;
cp=popen(c,"r");
size=fread(value,1,SIZEOF_STR-1,cp);
if (value[0]==0 || size==0)
return false;
if (value[size-1]=='\n')
value[size-1]='\0';
return string_format_from(format->buf, &format->bufpos, "%s", value);
}

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

0 comments on commit b536f3f

Please sign in to comment.