Source-Changes-D archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CVS commit: src/etc/rc.d



    Date:        Tue, 5 Dec 2017 16:00:25 +0000
    From:        David Holland <dholland-sourcechanges%netbsd.org@localhost>
    Message-ID:  <20171205160025.GA22176%netbsd.org@localhost>

  | Test -o isn't well specified? Or is the issue the precedence of ! vs. -o?

-o is a "to be deprecated one day" option, but that is not really the
problem (our test will continue to support it anyway.)

It isn't the precedence of the operators that is at issue, but deciding what is
an operator - all of the args to test are merely strings, and the strings
that are to be operands can have values like "!" or "-o" (or "-f"  "=" or "(")
as easily as those intended to be operators (or syntax.)

That makes it very difficult to work out which args are operators and
which are operands (there is no notion of quoting in test arg strings.)

To handle this, there are a set of rules that specify exactly how to parse
a test expression that has 0 to 4 args (though even there there are undefined
cases) and any expression with more than 4 args is simply undefined (the
trailing ']' when test is called as '[' does not count as an arg for this
purpose.)

The expression in rc.d/sshd would not have caused any problems, but it
sets a bad example - and as it is always trivial to convert a long test
expression (with an obvious intent) into a series of shorter defined
cases connected by sh operators, I do that whenever I see an undefined
use - whether it will work as written or not.

The change from
	test ! -f filename
to
	! test -f filename

(or really the [ form that is/was there) is not strictly necessary right now
- but it would be if some later revision to test were to add a new binary
operator "-f" to accompany the current unary "-f" operator - which might be
unlikely but is not impossible, so, as the change is trivial, it might as
well be done too.

In general, if you are using any of ! ( ) -o -a  as test args, and they
are not intended to be simple strings to be operated upon, it is best to
rewrite the command, using sh operators and syntax to combine sub-expressions.
Just remember when doing it that in test, -a is (was) higher precedence than -o
but in sh && and || are equal precedence, and associate L to R, so sometimes
extra { } need to be added.

kre



Home | Main Index | Thread Index | Old Index