* doc/m4.texi (Indirection): New chapter, containing pre-existing
Indir and Builtin sections.
(Qindir): New section.
(Builtin): Document interaction with qindir.
* src/builtin/c (indir, m4_qindir): New functions.
(builtin_tab): Register new builtin.
* src/input.c (input_type): Add INPUT_FILE_Q.
(push_quote_wrapper): New function.
(push_file, push_string_init, push_string_finish, pop_input)
(pop_wrapup, input_print, next_buffer, consume_buffer, peek_input)
(next_char_1, skip_line): Manage quote injection.
* src/m4.h (push_quote_wrapper): New declaration.
* NEWS: Document the new macro.
---

I'm trying to port remaining patches from the argv_ref branch into
branch-1.6; this one comes from commit 48a3bd03 on that branch.

 NEWS          |   3 +
 doc/m4.texi   | 189 +++++++++++++++++++++++++++++--
 src/builtin.c |  32 +++++-
 src/input.c   | 301 +++++++++++++++++++++++++++++++++++++++-----------
 src/m4.h      |   1 +
 5 files changed, 450 insertions(+), 76 deletions(-)

diff --git a/NEWS b/NEWS
index 355b3fea..89cafe6e 100644
--- a/NEWS
+++ b/NEWS
@@ -77,6 +77,9 @@ GNU M4 NEWS - User visible changes.
    Also, the position of `-d' with respect to files on the command line is
    now significant.

+** A new `qindir' builtin is added, similar to `indir', but where the
+   output is quoted rather than rescanned for further macro expansion.
+
 ** A new predefined text macro, `__m4_version__', expands to the unquoted
    version number of M4, if GNU extensions are enabled.  While you should
    generally favor feature tests over version number checks, this macro can
diff --git a/doc/m4.texi b/doc/m4.texi
index 10314d42..23052e0d 100644
--- a/doc/m4.texi
+++ b/doc/m4.texi
@@ -110,6 +110,7 @@ Top

 * Macros::                      How to invoke macros
 * Definitions::                 How to define new macros
+* Indirection::                 Using inaccessible macros
 * Conditionals::                Conditionals, loops, and recursion

 * Debugging::                   How to debug macros and input
@@ -175,8 +176,11 @@ Top
 * Defn::                        Renaming macros
 * Pushdef::                     Temporarily redefining macros

-* Indir::                       Indirect call of macros
-* Builtin::                     Indirect call of builtins
+Using inaccessible macros
+
+* Indir::                       Unquoted indirect call of macros
+* Qindir::                      Quoted indirect call of macros
+* Builtin::                     Unquoted indirect call of builtins

 Conditionals, loops, and recursion

@@ -1893,9 +1897,6 @@ Definitions
 * Undefine::                    Deleting a macro
 * Defn::                        Renaming macros
 * Pushdef::                     Temporarily redefining macros
-
-* Indir::                       Indirect call of macros
-* Builtin::                     Indirect call of builtins
 @end menu

 @node Define
@@ -2734,10 +2735,30 @@ Pushdef
 @result{}
 @end example

-@node Indir
-@section Indirect call of macros
+@node Indirection
+@chapter Using inaccessible macros

-@cindex indirect call of macros
+@cindex indirection
+@cindex invoking inaccessible macros
+@cindex inaccessible macros, invoking
+@cindex hidden macros, invoking
+GNU M4 places no restrictions on the strings that can be used when
+defining a macro name, even if the name will not be parsed as a word if
+it occurs in isolation.  Additionally, there is nothing wrong with
+hiding the definition of builtin macros that might adversely impact
+output if inadvertently expanded.  Therefore, there are a few builtins
+designed for manipulation of indirect macro names.
+
+@menu
+* Indir::                       Unquoted indirect call of macros
+* Qindir::                      Quoted indirect call of macros
+* Builtin::                     Unquoted indirect call of builtins
+@end menu
+
+@node Indir
+@section Unquoted indirect call of macros
+
+@cindex indirect call of macros, unquoted
 @cindex call of macros, indirect
 @cindex macros, indirect call of
 @cindex GNU extensions
@@ -2828,8 +2849,142 @@ Indir
 @result{}0
 @end example

+@node Qindir
+@section Quoted indirect call of macros
+
+@cindex indirect call of macros, quoted
+@cindex call of macros, indirect
+@cindex macros, indirect call of
+@cindex adding quotes
+@cindex quotes, adding
+@cindex GNU extensions
+Sometimes, it is desirable to avoid rescanning the output of a macro
+invocation.  It is possible to add quotes around any macro with
+@code{qindir}:
+
+@deffn Builtin qindir (@var{name}, @ovar{args@dots{}})
+Results in a call to the macro @var{name}, which is passed the
+rest of the arguments @var{args}.  If @var{name} is not defined, an
+error message is printed, and the expansion is void.  Otherwise, the
+output generated by @var{name} is additionally surrounded by the current
+quotes at the end of the indirect invocation.
+
+This macro was introduced in GNU M4 1.6.
+
+The macro @code{qindir} is recognized only with parameters.
+@end deffn
+
+Many builtin macros return their output unquoted, so that it can be
+rescanned for further macro names.  But in some contexts, it is
+desirable to avoid the rescanning step, particularly when using a
+builtin macro to operate on an opaque block of text and result in
+another block of text.  Observe how @code{qindir} can be used to make
+the output of @code{substr} be quoted (@pxref{Substr}):
+
+@example
+define(`b', `B')
+@result{}
+substr(`abc', `1', `1')
+@result{}B
+qindir(`substr', `abc', `1', `1')
+@result{}b
+@end example
+
+For text macros, @code{qindir} and @code{defn} are somewhat similar
+(@pxref{Defn}); both stop with the definition of the macro rather than
+triggering any recursive expansions.  However, there is a notable
+difference, in that @code{qindir} will expand arguments encountered
+within the macro definition.
+
+@example
+define(`foo', `divnum $1 $#')
+@result{}
+foo
+@result{}0  0
+foo(`one')
+@result{}0 one 1
+foo(`one', `two')
+@result{}0 one 2
+defn(`foo')
+@result{}divnum $1 $#
+qindir(`foo')
+@result{}divnum  0
+qindir(`foo', `one')
+@result{}divnum one 1
+qindir(`foo', `one', `two')
+@result{}divnum one 2
+@end example
+
+This macro is similar to @code{indir} in its ability to call macros with
+computed or ``invalid'' names:
+
+@example
+define(`$$internal$macro', `Internal macro (name `$0')')
+@result{}
+$$internal$macro
+@result{}$$internal$macro
+indir(`$$internal$macro')
+@result{}Internal macro (name $$internal$macro)
+qindir(`$$internal$macro')
+@result{}Internal macro (name `$$internal$macro')
+@end example
+
+It is safe to use @code{qindir} on any macro, including
+@code{changequote} (@pxref{Changequote}), because the indirect macro
+result is surrounded by the quotes that were current at the end of the
+indirection.  Nested invocations can be used to add additional layers of
+quoting.  However, a warning is issued, and no quoting added, when
+@var{name} is not a macro.
+
+@example
+$ @kbd{m4 -d}
+traceon(`qindir')
+@result{}
+qindir(`qindir', `changequote', `[', `]')
+@error{}m4trace: -1- qindir(`changequote', `[', `]') -> [[]]
+@error{}m4trace: -1- qindir(`qindir', `changequote', `[', `]') -> [[[]]]
+@result{}[]
+qindir([changequote])
+@error{}m4trace: -1- qindir([changequote]) -> ``''
+@result{}
+qindir(qindir(defn(`qindir')))
+@error{}m4:stdin:4: warning: qindir: invalid macro name ignored
+@error{}m4trace: -2- qindir(<qindir>) -> `'
+@error{}m4:stdin:4: warning: qindir: undefined macro ''
+@error{}m4trace: -1- qindir(`') -> `'
+@result{}
+@end example
+
+@ignore
+@comment This example is not worth putting in the manual, but it is
+@comment needed for full coverage.
+
+@comment examples
+@example
+$ @kbd{m4 -d -Iexamples}
+define(`bar', `hello')
+@result{}
+include(`foo')dnl
+@result{}hello
+traceon(`qindir')
+@result{}
+qindir(`include', `foo')dnl
+@error{}m4trace: -1- qindir(`include', `foo') -> ``<file: examples/foo>''
+@result{}bar
+qindir(`traceoff', `qindir')
+@error{}m4trace: -1- qindir(`traceoff', `qindir') -> ``''
+@result{}
+m4wrap(`m4wrap(`
+')qindir(`qindir', `dnl')')
+@result{}
+^D
+@error{}m4:stdin:6: warning: dnl: end of file treated as newline
+@result{}`'
+@end example
+@end ignore
+
 @node Builtin
-@section Indirect call of builtins
+@section Unquoted indirect call of builtins

 @cindex indirect call of builtins
 @cindex call of builtins, indirect
@@ -2926,6 +3081,22 @@ Builtin
 @result{}
 @end example

+There is no need for a quoted version of @code{builtin}; it is
+sufficient to use @code{qindir} when extra quoting is needed.
+
+@example
+define(`b', `B')
+@result{}
+substr(`abc', `1', `1')
+@result{}B
+builtin(`substr', `abc', `1', `1')
+@result{}B
+qindir(`builtin', `substr', `abc', `1', `1')
+@result{}b
+builtin(`qindir', `substr', `abc', `1', `1')
+@result{}b
+@end example
+
 @ignore
 @comment This example is not worth putting in the manual, but it is
 @comment needed for full coverage.  Autoconf's m4_include relies heavily
diff --git a/src/builtin.c b/src/builtin.c
index 65e0ff72..ce00e9eb 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -72,6 +72,7 @@ DECLARE (m4_mkstemp);
 DECLARE (m4_patsubst);
 DECLARE (m4_popdef);
 DECLARE (m4_pushdef);
+DECLARE (m4_qindir);
 DECLARE (m4_regexp);
 DECLARE (m4_shift);
 DECLARE (m4_sinclude);
@@ -123,6 +124,7 @@ static builtin const builtin_tab[] = {
   {"patsubst", true, false, true, m4_patsubst},
   {"popdef", false, true, true, m4_popdef},
   {"pushdef", false, true, true, m4_pushdef},
+  {"qindir", true, true, true, m4_qindir},
   {"regexp", true, false, true, m4_regexp},
   {"shift", false, true, true, m4_shift},
   {"sinclude", false, false, true, m4_sinclude},
@@ -947,12 +949,10 @@ m4_builtin (struct obstack *obs, int argc, 
macro_arguments *argv)
     }
 }

-/* The builtin "indir" allows indirect calls to macros, even if their
-   name is not a proper macro name.  It is thus possible to define
-   macros with ill-formed names for internal use in larger macro
-   packages.  This macro is not available in compatibility mode.  */
+/* Common code for indir and qindir.  Surround the output in the
+   current quotes if QUOTE is true, being careful of changequote.  */
 static void
-m4_indir (struct obstack *obs, int argc, macro_arguments *argv)
+indir (struct obstack *obs, int argc, macro_arguments *argv, bool quote)
 {
   const call_info *me = arg_info (argv);
   symbol *s;
@@ -982,9 +982,31 @@ m4_indir (struct obstack *obs, int argc, macro_arguments 
*argv)
                                                  !SYMBOL_MACRO_ARGS (s),
                                                  SYMBOL_TRACED (s));
       call_macro (s, new_argv, obs);
+      if (quote)
+        push_quote_wrapper ();
     }
 }

+/* The builtin "indir" allows indirect calls to macros, even if their
+   name is not a proper macro name.  It is thus possible to define
+   macros with ill-formed names for internal use in larger macro
+   packages.  This macro is not available in compatibility mode.  */
+static void
+m4_indir (struct obstack *obs, int argc, macro_arguments *argv)
+{
+  indir (obs, argc, argv, false);
+}
+
+/* The builtin "qindir" is like "indir", except that the output is
+   surrounded by the quotes in effect at the end of expansion.  This
+   allows wrapping other macros to provide quoted output where their
+   output is normally rescanned.  */
+static void
+m4_qindir (struct obstack *obs, int argc, macro_arguments *argv)
+{
+  indir (obs, argc, argv, true);
+}
+
 /* The macro "defn" returns the quoted definition of the macro named
    by the first argument.  If the macro is builtin, it will push a
    special macro-definition token on the input stack.  */
diff --git a/src/input.c b/src/input.c
index 16317e5f..786a5b39 100644
--- a/src/input.c
+++ b/src/input.c
@@ -74,6 +74,7 @@ enum input_type
 {
   INPUT_STRING,                 /* String resulting from macro expansion.  */
   INPUT_FILE,                   /* File from command line or include.  */
+  INPUT_FILE_Q,                 /* Quotation around a file, via qindir.  */
   INPUT_CHAIN,                  /* FIFO chain of separate strings,
                                    builtins, and $@ refs.  */
   INPUT_EOF                     /* Placeholder at bottom of input stack.  */
@@ -107,6 +108,12 @@ struct input_block
     }
     u_f;                        /* INPUT_FILE */
     struct
+    {
+      string_pair *quotes;      /* Quotes to use when wrapping.  */
+      size_t count;             /* Count of quotes to wrap.  */
+    }
+    u_q;                        /* INPUT_FILE_Q */
+    struct
     {
       token_chain *chain;       /* Current link in chain.  */
       token_chain *end;         /* Last link in chain.  */
@@ -226,15 +233,21 @@ push_file (FILE *fp, const char *title, bool 
close_when_done)

   if (next != NULL)
     {
-      obstack_free (current_input, next);
-      next = NULL;
+      assert (obstack_object_size (current_input) == 0
+              && next->type == INPUT_STRING);
+      i = next;
+    }
+  else
+    {
+      i = (input_block *) obstack_alloc (current_input, sizeof *i);
+      i->prev = isp;
+      isp = i;
     }

   if (debug_level & DEBUG_TRACE_INPUT)
     debug_message ("input read from %s",
                    quotearg_style (locale_quoting_style, title));

-  i = (input_block *) obstack_alloc (current_input, sizeof *i);
   i->type = INPUT_FILE;
   i->file = (char *) obstack_copy0 (&file_names, title, strlen (title));
   i->line = 1;
@@ -245,9 +258,6 @@ push_file (FILE *fp, const char *title, bool 
close_when_done)
   i->u.u_f.close = close_when_done;
   i->u.u_f.advance = start_of_input_line;
   output_current_line = -1;
-
-  i->prev = isp;
-  isp = i;
 }

 /* Given an obstack OBS, capture any unfinished text as a link, then
@@ -302,6 +312,7 @@ push_string_init (const char *file, int line)

   /* Reserve the next location on the obstack.  */
   next = (input_block *) obstack_alloc (current_input, sizeof *next);
+  next->prev = isp;
   next->type = INPUT_STRING;
   next->file = file;
   next->line = line;
@@ -474,6 +485,68 @@ push_token (token_data *token, int level, bool inuse)
   return inuse;
 }

+/* Wrap the current pending expansion in another level of quotes.  */
+void
+push_quote_wrapper (void)
+{
+  token_chain *chain;
+  size_t len = obstack_object_size (current_input);
+
+  assert (next);
+  if (next->type == INPUT_FILE)
+    {
+      input_block *block;
+      string_pair *quotes;
+      assert (!len);
+      block = (input_block *) obstack_alloc (current_input, sizeof *block);
+      quotes = (string_pair *) obstack_alloc (current_input, sizeof *quotes);
+      quotes->str1 = (char *) obstack_copy (current_input, curr_quote.str1,
+                                            curr_quote.len1);
+      quotes->len1 = curr_quote.len1;
+      obstack_grow (current_input, curr_quote.str2, curr_quote.len2);
+      block->type = INPUT_FILE_Q;
+      block->file = current_file;
+      block->line = current_line;
+      block->prev = next;
+      block->u.u_q.quotes = quotes;
+      block->u.u_q.count = 1;
+      next = block;
+    }
+  else if (next->type == INPUT_FILE_Q)
+    {
+      assert (len == curr_quote.len2 * next->u.u_q.count);
+      next->u.u_q.count++;
+      obstack_grow (current_input, curr_quote.str2, curr_quote.len2);
+    }
+  else if (!len && next->type == INPUT_STRING)
+    {
+      obstack_grow (current_input, curr_quote.str1, curr_quote.len1);
+      obstack_grow (current_input, curr_quote.str2, curr_quote.len2);
+    }
+  else
+    {
+      obstack_grow (current_input, curr_quote.str2, curr_quote.len2);
+      if (next->type == INPUT_STRING)
+        {
+          next->type = INPUT_CHAIN;
+          next->u.u_c.chain = next->u.u_c.end = NULL;
+        }
+      assert (next->type == INPUT_CHAIN);
+      make_text_link (current_input, &next->u.u_c.chain, &next->u.u_c.end);
+      assert (obstack_object_size (current_input) == 0 && next->u.u_c.chain);
+      chain = (token_chain *) obstack_alloc (current_input, sizeof *chain);
+      chain->next = next->u.u_c.chain;
+      next->u.u_c.chain = chain;
+      chain->type = CHAIN_STR;
+      chain->quote_age = 0;
+      chain->u.u_s.str =
+        (char *) obstack_copy (current_input, curr_quote.str1,
+                               curr_quote.len1);
+      chain->u.u_s.len = curr_quote.len1;
+      chain->u.u_s.level = -1;
+    }
+}
+
 /* Last half of push_string ().  All remaining unfinished text on
    the obstack returned from push_string_init is collected into the
    input stack.  */
@@ -482,13 +555,20 @@ push_string_finish (void)
 {
   size_t len = obstack_object_size (current_input);

-  if (next == NULL)
+  assert (next);
+  if (next->type == INPUT_FILE)
     {
       assert (!len);
-      return;
+      isp = next;
     }
-
-  if (len || next->type == INPUT_CHAIN)
+  else if (next->type == INPUT_FILE_Q)
+    {
+      assert (len == next->u.u_q.count * curr_quote.len2);
+      next->u.u_q.quotes->str2 = (char *) obstack_finish (current_input);
+      next->u.u_q.quotes->len2 = len;
+      isp = next;
+    }
+  else if (len || next->type == INPUT_CHAIN)
     {
       if (next->type == INPUT_STRING)
         {
@@ -497,7 +577,6 @@ push_string_finish (void)
         }
       else
         make_text_link (current_input, &next->u.u_c.chain, &next->u.u_c.end);
-      next->prev = isp;
       isp = next;
       input_change = true;
     }
@@ -608,6 +687,8 @@ pop_input (bool cleanup)
         }
       break;

+    case INPUT_FILE_Q:
+      return false;
     case INPUT_FILE:
       if (!cleanup)
         return false;
@@ -639,9 +720,15 @@ pop_input (bool cleanup)
       assert (!"pop_input");
       abort ();
     }
+  if (next)
+    {
+      /* Only possible after dnl.  */
+      assert (obstack_object_size (current_input) == 0
+              && next->type == INPUT_STRING);
+      next = NULL;
+    }
   obstack_free (current_input, isp);
   cached_quote = NULL;
-  next = NULL;                  /* might be set in push_string_init () */

   isp = tmp;
   input_change = true;
@@ -655,7 +742,7 @@ pop_input (bool cleanup)
 bool
 pop_wrapup (void)
 {
-  next = NULL;
+  assert (!next);
   obstack_free (current_input, NULL);
   free (current_input);

@@ -689,25 +776,33 @@ input_print (struct obstack *obs)
   size_t len = obstack_object_size (current_input);
   size_t maxlen = max_debug_argument_length;
   token_chain *chain;
+  input_block *block = next ? next : isp;
+  size_t count;

-  if (!next)
-    {
-      assert (!len);
-      return;
-    }
-  switch (next->type)
+  switch (block->type)
     {
     case INPUT_STRING:
-      assert (!next->u.u_s.len);
+      assert (next && !block->u.u_s.len);
+      break;
+    case INPUT_FILE_Q:
+      assert (next && len);
+      count = block->u.u_q.count;
+      while (count--)
+        if (shipout_string_trunc (obs, block->u.u_q.quotes->str1,
+                                  block->u.u_q.quotes->len1, &maxlen))
+          return;
+      obstack_grow (obs, "<file: ", strlen ("<file: "));
+      obstack_grow (obs, block->prev->file, strlen (block->prev->file));
+      obstack_1grow (obs, '>');
       break;
     case INPUT_FILE:
       assert (!len);
       obstack_grow (obs, "<file: ", strlen ("<file: "));
-      obstack_grow (obs, next->file, strlen (next->file));
+      obstack_grow (obs, block->file, strlen (block->file));
       obstack_1grow (obs, '>');
       break;
     case INPUT_CHAIN:
-      chain = next->u.u_c.chain;
+      chain = block->u.u_c.chain;
       while (chain)
         {
           switch (chain->type)
@@ -762,39 +857,63 @@ static const char *
 next_buffer (size_t *len, bool allow_quote)
 {
   token_chain *chain;
+  input_block *block = isp;

   while (1)
     {
-      assert (isp);
+      assert (block);
       if (input_change)
         {
-          current_file = isp->file;
-          current_line = isp->line;
+          current_file = block->file;
+          current_line = block->line;
           input_change = false;
         }

-      switch (isp->type)
+      switch (block->type)
         {
         case INPUT_STRING:
-          if (isp->u.u_s.len)
+          if (block->u.u_s.len)
             {
-              *len = isp->u.u_s.len;
-              return isp->u.u_s.str;
+              *len = block->u.u_s.len;
+              return block->u.u_s.str;
             }
           break;

+        case INPUT_FILE_Q:
+          if (block->u.u_q.count)
+            {
+              push_string_init (block->file, block->line);
+              while (block->u.u_q.count)
+                {
+                  obstack_grow (current_input, block->u.u_q.quotes->str1,
+                                block->u.u_q.quotes->len1);
+                  block->u.u_q.count--;
+                }
+              push_string_finish ();
+              return next_buffer (len, allow_quote);
+            }
+          if (block->prev->u.u_f.end)
+            {
+              string_pair *pair = block->u.u_q.quotes;
+              block->type = INPUT_STRING;
+              block->u.u_s.str = pair->str2;
+              block->u.u_s.len = pair->len2;
+              return next_buffer (len, allow_quote);
+            }
+          block = block->prev;
+          FALLTHROUGH;
         case INPUT_FILE:
           if (start_of_input_line)
             {
               start_of_input_line = false;
-              current_line = ++isp->line;
+              current_line = ++block->line;
             }
-          if (isp->u.u_f.end)
+          if (block->u.u_f.end)
             break;
-          return freadptr (isp->u.u_f.fp, len);
+          return freadptr (block->u.u_f.fp, len);

         case INPUT_CHAIN:
-          chain = isp->u.u_c.chain;
+          chain = block->u.u_c.chain;
           while (chain)
             {
               if (allow_quote && chain->quote_age == current_quote_age)
@@ -822,16 +941,16 @@ next_buffer (size_t *len, bool allow_quote)
                     }
                   return NULL;  /* No buffer to provide.  */
                 case CHAIN_LOC:
-                  isp->file = chain->u.u_l.file;
-                  isp->line = chain->u.u_l.line;
+                  block->file = chain->u.u_l.file;
+                  block->line = chain->u.u_l.line;
                   input_change = true;
-                  isp->u.u_c.chain = chain->next;
+                  block->u.u_c.chain = chain->next;
                   return next_buffer (len, allow_quote);
                 default:
                   assert (!"next_buffer");
                   abort ();
                 }
-              isp->u.u_c.chain = chain = chain->next;
+              block->u.u_c.chain = chain = chain->next;
             }
           break;

@@ -845,6 +964,7 @@ next_buffer (size_t *len, bool allow_quote)

       /* End of input source --- pop one level.  */
       pop_input (true);
+      block = isp;
     }
 }

@@ -858,19 +978,24 @@ consume_buffer (size_t len)
   const char *buf;
   const char *p;
   size_t buf_len;
+  input_block *block = isp;

-  assert (isp && !input_change && len);
-  switch (isp->type)
+  assert (block && !input_change && len);
+  switch (block->type)
     {
     case INPUT_STRING:
-      assert (len <= isp->u.u_s.len);
-      isp->u.u_s.len -= len;
-      isp->u.u_s.str += len;
+      assert (len <= block->u.u_s.len);
+      block->u.u_s.len -= len;
+      block->u.u_s.str += len;
       break;

+    case INPUT_FILE_Q:
+      assert (!block->u.u_q.count);
+      block = block->prev;
+      FALLTHROUGH;
     case INPUT_FILE:
-      assert (!start_of_input_line);
-      buf = freadptr (isp->u.u_f.fp, &buf_len);
+      assert (!start_of_input_line && !block->u.u_f.end);
+      buf = freadptr (block->u.u_f.fp, &buf_len);
       assert (buf && len <= buf_len);
       buf_len = 0;
       while ((p = (char *) memchr (buf + buf_len, '\n', len - buf_len)))
@@ -878,15 +1003,15 @@ consume_buffer (size_t len)
           if (p == buf + len - 1)
             start_of_input_line = true;
           else
-            current_line = ++isp->line;
+            current_line = ++block->line;
           buf_len = p - buf + 1;
         }
-      if (freadseek (isp->u.u_f.fp, len) != 0)
+      if (freadseek (block->u.u_f.fp, len) != 0)
         assert (false);
       break;

     case INPUT_CHAIN:
-      chain = isp->u.u_c.chain;
+      chain = block->u.u_c.chain;
       assert (chain && chain->type == CHAIN_STR && len <= chain->u.u_s.len);
       /* Partial consumption invalidates quote age.  */
       chain->quote_age = 0;
@@ -924,6 +1049,29 @@ peek_input (bool allow_argv)
             break;
           return to_uchar (block->u.u_s.str[0]);

+        case INPUT_FILE_Q:
+          if (block->u.u_q.count)
+            {
+              push_string_init (block->file, block->line);
+              while (block->u.u_q.count)
+                {
+                  obstack_grow (current_input, block->u.u_q.quotes->str1,
+                                block->u.u_q.quotes->len1);
+                  block->u.u_q.count--;
+                }
+              push_string_finish ();
+              return peek_input (allow_argv);
+            }
+          if (block->prev->u.u_f.end)
+            {
+              string_pair *pair = block->u.u_q.quotes;
+              block->type = INPUT_STRING;
+              block->u.u_s.str = pair->str2;
+              block->u.u_s.len = pair->len2;
+              return peek_input (allow_argv);
+            }
+          block = block->prev;
+          FALLTHROUGH;
         case INPUT_FILE:
           ch = getc (block->u.u_f.fp);
           if (ch != EOF)
@@ -1016,46 +1164,71 @@ next_char_1 (bool allow_quote, bool allow_argv)
 {
   int ch;
   token_chain *chain;
+  input_block *block = isp;

   while (1)
     {
-      assert (isp);
+      assert (block);
       if (input_change)
         {
-          current_file = isp->file;
-          current_line = isp->line;
+          current_file = block->file;
+          current_line = block->line;
           input_change = false;
         }

-      switch (isp->type)
+      switch (block->type)
         {
         case INPUT_STRING:
-          if (!isp->u.u_s.len)
+          if (!block->u.u_s.len)
             break;
-          isp->u.u_s.len--;
-          return to_uchar (*isp->u.u_s.str++);
+          block->u.u_s.len--;
+          return to_uchar (*block->u.u_s.str++);

+        case INPUT_FILE_Q:
+          if (block->u.u_q.count)
+            {
+              push_string_init (block->file, block->line);
+              while (block->u.u_q.count)
+                {
+                  obstack_grow (current_input, block->u.u_q.quotes->str1,
+                                block->u.u_q.quotes->len1);
+                  block->u.u_q.count--;
+                }
+              push_string_finish ();
+              return next_char_1 (allow_quote, allow_argv);
+            }
+          if (block->prev->u.u_f.end)
+            {
+              string_pair *pair = block->u.u_q.quotes;
+              block->type = INPUT_STRING;
+              block->u.u_s.str = pair->str2;
+              block->u.u_s.len = pair->len2;
+              return next_char_1 (allow_quote, allow_argv);
+            }
+          block = block->prev;
+          FALLTHROUGH;
         case INPUT_FILE:
           if (start_of_input_line)
             {
               start_of_input_line = false;
-              current_line = ++isp->line;
+              current_line = ++block->line;
             }

           /* If stdin is a terminal, calling getc after peek_input
              already called it would make the user have to hit ^D
              twice to quit.  */
-          ch = isp->u.u_f.end ? EOF : getc (isp->u.u_f.fp);
+          ch = block->u.u_f.end ? EOF : getc (block->u.u_f.fp);
           if (ch != EOF)
             {
               if (ch == '\n')
                 start_of_input_line = true;
               return ch;
             }
+          block->u.u_f.end = true;
           break;

         case INPUT_CHAIN:
-          chain = isp->u.u_c.chain;
+          chain = block->u.u_c.chain;
           while (chain)
             {
               unsigned int argc;
@@ -1099,7 +1272,7 @@ next_char_1 (bool allow_quote, bool allow_argv)
                   /* Rather than directly parse argv here, we push
                      another input block containing the next unparsed
                      argument from argv.  */
-                  push_string_init (isp->file, isp->line);
+                  push_string_init (block->file, block->line);
                   push_arg_quote (current_input, chain->u.u_a.argv,
                                   chain->u.u_a.index,
                                   quote_cache (NULL, chain->quote_age,
@@ -1109,16 +1282,16 @@ next_char_1 (bool allow_quote, bool allow_argv)
                   push_string_finish ();
                   return next_char_1 (allow_quote, allow_argv);
                 case CHAIN_LOC:
-                  isp->file = chain->u.u_l.file;
-                  isp->line = chain->u.u_l.line;
+                  block->file = chain->u.u_l.file;
+                  block->line = chain->u.u_l.line;
                   input_change = true;
-                  isp->u.u_c.chain = chain->next;
+                  block->u.u_c.chain = chain->next;
                   return next_char_1 (allow_quote, allow_argv);
                 default:
                   assert (!"next_char_1");
                   abort ();
                 }
-              isp->u.u_c.chain = chain = chain->next;
+              block->u.u_c.chain = chain = chain->next;
             }
           break;

@@ -1132,6 +1305,7 @@ next_char_1 (bool allow_quote, bool allow_argv)

       /* End of input source --- pop one level.  */
       pop_input (true);
+      block = isp;
     }
 }

@@ -1167,6 +1341,9 @@ skip_line (const call_info *name)
     }
   if (ch == CHAR_EOF)
     m4_warn (0, name, _("end of file treated as newline"));
+  if (!next)
+    /* Possible if dnl popped input looking for newline.  */
+    push_string_init (current_file, current_line);
 }

 /* When next_token() sees a builtin token with peek_input, this
diff --git a/src/m4.h b/src/m4.h
index fa2cdf65..4e5ca5fd 100644
--- a/src/m4.h
+++ b/src/m4.h
@@ -357,6 +357,7 @@ extern void append_macro (struct obstack *, builtin_func *, 
token_chain **,
 extern void push_macro (struct obstack *, builtin_func *);
 extern struct obstack *push_string_init (const char *, int);
 extern bool push_token (token_data *, int, bool);
+extern void push_quote_wrapper (void);
 extern void push_string_finish (void);
 extern struct obstack *push_wrapup_init (const call_info *, token_chain ***);
 extern void push_wrapup_finish (void);

base-commit: ee7d9b7b8563610956d2e47c01c4cbf4d6f8b7b0
prerequisite-patch-id: 11a56ce31af353d3e1a1ab837d4e70ca48780490
prerequisite-patch-id: dd46c21940479d1fd250d632a1ed2e0608239bd4
-- 
2.49.0


_______________________________________________
M4-patches mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/m4-patches

Reply via email to