#defines with variable # arguments

Larry Yaffe lgy at pupthy2.PRINCETON.EDU
Mon May 16 10:53:10 AEST 1988


In article <980 at cresswell.quintus.UUCP> ok at quintus.UUCP (Richard A. O'Keefe) writes:
    [Using m4 to process macros with variable # arguments]

-I should substantiate that.
-
-If you just want to make the BSD sprintf() look as though it returned an
-integer, do
-
-cat >sprintflen.m4 <<'EOF'
-define(sprintf,`strlen(`sprintf'($*))')dnl
-EOF

    This was definitely a helpful suggestion - not being overly familiar
with m4, I hadn't considered using it.  However, the suggested m4 scripts
suffer from two small problems:

    Whitespace between the "sprintf" and the following argument list must
first be removed in order for m4 to process the arguments correctly.
This is easily accomplished using 'sed'.

    Argument lists containing unmatched quoted parentheses, such as,
sprintf (str,"= ("),  produce horrible garbage.  This is a real occurence
in some of the source I'm dealing with.  (However, having to edit a
handful of special sprintf's by hand is a big improvement over having
to edit hundreds.)

------------------------------------------------------------------------
Laurence G. Yaffe			Internet: lgy at pupthy.princeton.edu
Department of Physics			Bitnet:   lgy at pucc
Princeton University			UUCP:     ...!princeton!pupthy!lgy
PO Box 708, Princeton NJ 08544



More information about the Comp.unix.wizards mailing list