Subject: Re: printing expanded variables?
To: Simon J. Gerraty <sjg@crufty.net>
From: Greg A. Woods <woods@weird.com>
List: tech-toolchain
Date: 09/04/2003 11:35:34
[ On Wednesday, September 3, 2003 at 21:35:52 (-0700), Simon J. Gerraty wrote: ]
> Subject: Re: printing expanded variables? 
>
> Ok, since there's no unanimous favourite for new command line switch,
> the following would work just as well, and still use -V.
> 
> $ make -V FUBAR
> ${FU} ${BAR}
> $ make -V '$FUBAR'
> fu bar
> $
> 
> Note the distinct lack of {} after the $, it is just a clue to make
> that we want Var_Subst rather than Var_Value.

I don't think that's a very good idea.  You're going 1% of the way to
implementing a full and proper expression evaluator for command-line
use, but you're doing it in a way incompatible with how things work on
the "inside" of a makefile.

I.e. What you're doing is effectively what I suggested with eval(), but
you're doing it in a very non-intuitive (for make experts) and
incomplete and incompatible (with makefiles) way.

I know that eval() looks like a bit of a hack too, but it was intended
to look and feel like the existing empty() and defined() operators.

I also fully expected it would be implemented internally so that it
could potentially be used in a makefile and that it would simply be
exposed to the command-line and thus make much more of the power of make
usable on the command line.

I was going to suggest simply adding the full macro evaluator to the
'-V' parameter such that "make -v '${FUBAR}'" would work as you suggest
above but I was concerned about confusion with how variables are
expanded within makefiles and I wanted to add another operator separate
from "${}".  I also fully expected that "make -v "${FUBAR}'" would
``just work'' in the end anyway since that's the natural way to do it.

Why not go all the way and make it work "properly"?

Note that I've implemented a very similar syntax for smail's command-
line variable expansion and it turns out to be very intuitive.
Appologies for the length but here's the full section describing the
feature from the manual page.  Note also the way the proposed '-v' makes
the "verbose" output work intuitively too:

       -bP    Take the addresses given on the command line as
              config file item names (see smailconf(5) for more
              information) and write the values for each item on
              the standard output.  Also will write out recon-
              structions of all the configuration files - see
              below.  References to item names, such as hostnames
              or uucp_name which may be computed at run time,
              will yield the values that smail would compute nor-
              mally.  For example, on my workstation, the com-
              mand:

                  smail -bP hostnames max_message_size

              produces the output:

                  futatsu.uts.amdahl.com:futatsu.amdahl.com
                  102400

              With the -v or -d flag the configuration item names
              are displayed as well and the value is given in a
              format suitable for including in a configuration
              file.  So for example the command:

                  smail -v -bP hostnames max_message_size

              produces the output:

                  hostnames=futatsu.uts.amdahl.com:futatsu.amdahl.com
                  max_message_size=102400

              In addition to other configuration file items, the
              name primary_name will output the primary (or
              canonical) name for the local host which will be
              used by smail, and config_file will output the name
              of the primary configuration file.

              If the name begins with a `$' then the internal
              variable and/or meta expansion is performed and the
              result is displayed (with the name if -v or -d is
              given, as with configuration items).  Remember to
              quote or escape the `$' character from the shell.
              Note this value is shown without quoting or escap-
              ing of special characters.

              Configuration items which are permitted to contain
              variable and/or meta expansions can be often be
              fully expanded to reveal their run-time values by
              using the ${eval: meta expansion modifier.  See
              smail(5) for more information.  For example these
              two commands:

                  smail -bP '${smtp_hello_reject_hostnames}'
                  smail -bP '${eval:smtp_hello_reject_hostnames}'

              might produce the following output on a machine
              called ``proven'' in the domain ``example.com'',
              and with ``mail.example.net'' also handled locally:

                  localhost\.;I must be talking to myself!
                  :.*\.localdomain;There's no such domain 'localdomain'!
                  :${rxquote:hostname}:${rxquote:more_hostnames}

                  localhost\.;I must be talking to myself!
                  :.*\.localdomain;There's no such domain 'localdomain'!
                  :proven\.example\.com:mail\.example.net

              Also, the name help will produce a verbose listing
              of all variables associated with their type, one
              variable per line, and the name all will produce a
              verbose listing of all variables along with their
              values.  It is equivalent to smail -v -bP followed
              by a list of the names of all configuration
              variables.



-- 
						Greg A. Woods

+1 416 218-0098                  VE3TCP            RoboHack <woods@robohack.ca>
Planix, Inc. <woods@planix.com>          Secrets of the Weird <woods@weird.com>