3

I'm writing an application from which I want to run a user-specified pager. The standard way to do this seems to be to look at the PAGER environment variable.

I'm unclear as to whether this is a program name or a program name together with arguments. I tried to find standards mentioning this, but could not.

My gut feeling is that I should split on spaces and execute. Am I correct? I have come across various programs that assume environment variables like $PAGER and $EDITOR are program names without environment variables.

Perhaps I should execute with sh instead though?

Links

Att Righ
  • 1,196
  • 1
    Run it through the shell. That way your users can't complain. – Petr Skocik Nov 28 '17 at 23:04
  • Probably a good idea. I don't want to spread lies if I shouldn't! The only slight complexity is that that I want to be able to kill it. I guess I could just kill the tree of processes though. – Att Righ Nov 28 '17 at 23:06
  • 1
    git log appears (from its strace output) to try to run it directly if there's no spaces in it and through /bin/sh otherwise. Anyway, you could launch it in a separate process group and then kill the process group. – Petr Skocik Nov 28 '17 at 23:13
  • 1
    Linking in https://unix.stackexchange.com/q/5383/117549 – Jeff Schaller Nov 28 '17 at 23:42
  • 1
    Relating: https://unix.stackexchange.com/a/96231/117549 – Jeff Schaller Nov 28 '17 at 23:45
  • So from my reading these links don't address the quesiton directly but the second indicates that man supports $PAGER with arguments and the first states that "Whether shell metacharacters are expanded in $PAGER is not consistent between applications" – Att Righ Nov 28 '17 at 23:55

1 Answers1

4

My gut feeling is that I should split on spaces and execute.

Good instincts. You're calling exec(3); it's up to you how to interpret the environment variables you support. By supporting options, you save the user the trouble of writing a script to tuck them into a single $PAGER name.

A good example to follow might be man(1). On my system, it supports MANPAGER, which says,

If MANPAGER is set, its value is used as the name of the program to use to display the man page. If not, then PAGER is used. If that has no value either, /usr/bin/less -is is used.

That at least implies that PAGER can contain options. You might experiment with yours to see. I bet it does.

I don't think Posix addresses the question of how environment variables that denote utilities are interpreted. I think the best you have is common examples of prior art.

As to whether or not to pass it though "the" shell, I'd say No, unless you want to take advantage of shell variable expansion, and document it. It's simpler and more predictable to keep the shell out of it.