Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

echoti and number of arguments



I've noticed that the number of arguments was checked with
echoti. It's not a good idea.

Mainly because it's hard or even impossible to guess that number
from the terminfo string. For instance, "sgr" accepts 9
parameters, but terminals don't all support the 9 fields (for
instance, xterm-r5 has:
sgr=\E[%?%p1%t;7%;%?%p2%t;4%;%?%p3%t;7%;%?%p4%t;5%;%?%p6%t;1%;m,
only fields 1 to 6 are supported, but it's valid to pass 9
parameters).

If you look at how ncurses' tput works: if no argument is
passed, the string is returned asis; if at least one argument is
provided, % escape sequences are handled, and 9 parameters are
used (if some are used but not provided, they default to 0 or ""
depending on their type).

Then, the way the argument count is checked in zsh is nonsense:

  /* count the number of arguments required */
  for (argct = 0, u = t; *u; u++)
      if (*u == '%') {
          if (u++, (*u == 'd' || *u == '2' || *u == '3' || *u == '.' ||
                    *u == '+'))
              argct++;
      }

Those flags have nothing to do with the argument count. %d may
be provided twice even when there's only one argument. For
instance, setf on xterm-16color has two %d but takes only one
argument, u6 (cursor position report) has two %d but doesn't
take any argument. sgr takes 9 arguments but argct above would
be zero.

>From terminfo(5):

   %%        outputs `%'
   %[[:]flags][width[.precision]][doxXs]
             as in printf, flags are [-+#] and space
   %c        print pop() like %c in printf()
   %s        print pop() like %s in printf()

   %p[1-9]   push i'th parm
   %P[a-z]   set dynamic variable [a-z] to pop()
   %g[a-z]   get dynamic variable [a-z] and push it
   %P[A-Z]   set static variable [a-z] to pop()
   %g[A-Z]   get static variable [a-z] and push it
   %'c'      char constant c
   %{nn}     integer constant nn
   %l        push strlen(pop)

   %+ %- %* %/ %m
             arithmetic (%m is mod): push(pop() op pop())
   %& %| %^  bit operations: push(pop() op pop())
   %= %> %<  logical operations: push(pop() op pop())
   %A, %O    logical and & or operations (for conditionals)
   %! %~     unary operations push(op pop())
   %i        add 1 to first two parameters (for ANSI terminals)

   %? expr %t thenpart %e elsepart %;
             if-then-else, %e elsepart is optional.
             else-if's are possible a la Algol 68:
             %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e %;
             ci are conditions, bi are bodies.

So, to my mind, echoti should only check that there are no more
than 9 parameters.

-- 
Stéphane



Messages sorted by: Reverse Date, Date, Thread, Author