Homework 1. m4_do

Autoconf 2.57 defines a macro m4_do as follows:

m4_define([m4_do],
	  [m4_if($#, 0, [],
		 $#, 1, [$1],
		 [$1[]m4_do(m4_shift($@))])])

This definition is contained in the Autoconf 2.57 source file m4sugar.m4, which was taken from the Autoconf CVS repository under lib/m4sugar/m4sugar.m4 revision 2.68.

  1. Autoconf's notation differs from that of ordinary m4. Explain how Autoconf arranged for this change of notation, by including each line of m4sugar.m4 needed to accomplish the change of notation, in the same order that they appear in m4sugar.m4. Prefix each such line with an m4 comment explaining the change of notation that the line implements. Omit the lines of m4sugar.m4 that m4_do does not need. Do not change any of the lines of m4sugar.m4 that you do include.

    For example, you might include the following lines and comments:

    # For quotation, m4 uses ` ' but Autoconf uses [ ].
    changequote([,])
    
    # To define a macro, m4 uses "define" but Autoconf uses "m4_define".
    define([m4_define],   defn([define]))
    
  2. m4_do has an if-then-else with three alternatives, but one of those alternatives is not needed. Which alternative can be discarded, and why? Simplify the macro definition by removing the redundant alternative. Explain the simplification in a comment that precedes the definition of m4_do.

Write a text file hw1.m4 with the following contents, in order:

If your hw1.m4 file is constructed correctly and you are using GNU m4, the command:

m4 hw1.m4

should output the following lines:

{}
{1}
{12}
{34dnl not 56}

and so forth for each test case. Only the first four test outputs are given here; the others are left as an exercise for the reader. The curly braces {} merely show you where each test case starts and stops, and are not necessary to invoke the macros themselves.

To turn in your assignment, submit the file hw1.m4.


© 2003 Paul Eggert. See copying rules.
$Id: hw1.html,v 1.4 2003/01/28 06:46:17 eggert Exp $