title: terminfo parameterized string %d encoding behavior
body: <p>I'm trying to understand the behavior of <code>%d</code> encoding in terminfo's parameterized string parser. The relevant man page is <a href="https://www.freebsd.org/cgi/man.cgi?query=terminfo&amp;sektion=5" rel="nofollow noreferrer">here</a> and states that -</p>
  
  <pre><code>%[[:]flags][width[.precision]][doxXs]
          as  in  printf, flags are [-+#] and space.  Use a ":" to allow the
          next character to be a "-" flag, avoiding interpreting "%-" as  an
          operator.
  </code></pre>
  
  <p>but says nothing about from where the values are to be printed and what to do with edge cases. Are they from stack or from the arguments being passed to the parameterized string? Also what happens when extra arguments are passed(non equal <code>%d</code> in parameterized string) or extra <code>%d</code> are present(incorrect parameterized string?)? Is that undefined behavior or implementation-defined or defined by definition somewhere?</p>
  
  <p>I tried to check some cases by manually writing some valid and invalid strings and verifying the output but everything till now is bit inconsistent so I cannot see a pattern here -</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;curses.h&gt;
  #include &lt;term.h&gt;
  
  using namespace std;
  
  int main() {
      // single %d prints single argument
      // =&gt; 2
      auto res = tparm("[%d]", 2);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // single %d prints single argument and ignores additional
      // =&gt; 2
      res = tparm("[%d]", 2, 3, 4);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // multiple %d prints 0 for absent additional arguments
      // =&gt; 2-0-0-0
      res = tparm("[%d-%d-%d-%d]", 2);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // multiple %d prints with equal number of arguments prints
      // first two correctly and rest 0
      // =&gt; 2-3-0-0-0
      res = tparm("[%d-%d-%d-%d-%d]", 2,3,4,5,6);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // single value pushed to stack prints from stack
      // =&gt; 2
      res = tparm("[%p1%d]", 2);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // single value pushed to stack prints from stack and ignores extra arguments
      // =&gt; 2
      res = tparm("[%p1%d]", 2,3,4);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // single value pushed to stack prints from stack and additional prints are 0
      // if no arguments are provided
      // =&gt; 2-0-0
      res = tparm("[%p1%d-%d-%d]", 2);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // single value pushed to stack prints from stack and additional prints 0
      // even if equal arguments are provided
      // =&gt; 2-0-0
      res = tparm("[%p1%d-%d-%d]", 2,3,4);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // single value pushed to stack prints from stack after pop()?
      // =&gt; 100-&lt;garbage&gt;
      res = tparm("[%p1%d-%c]", 100);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // pushed to stack via {} and equal arguments provided, prints all
      // =&gt; 2-1-100-200
      res = tparm("[%{1}%{2}%d-%d-%d-%d]", 100, 200);
      cout &lt;&lt; res &lt;&lt; endl;
  
      // pushed to stack via {} and %p1 equal arguments provided
      // prints only stack and rest 0
      // =&gt; 100-2-1-0
      res = tparm("[%{1}%{2}%p1%d-%d-%d-%d]", 100, 200);
      cout &lt;&lt; res &lt;&lt; endl;
  }
  </code></pre>