bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Officially document that we allow other characters in function names


From: konsolebox
Subject: Officially document that we allow other characters in function names
Date: Mon, 27 Jun 2016 15:11:09 +0800

Hi, I think it's time that we officially specify in the manual of Bash
that we allow other characters besides [[:alnum:]_] when declaring
function names in non-POSIX mode.

In the Bash manual, the only thing we specify about the definition of
shell functions under POSIX mode is "When in posix mode, name may not
be the name of one of the POSIX special builtins."  It doesn't say
anything about the characters we allow when not in POSIX mode.

Reference code:

    static int
    execute_intern_function (name, funcdef)
         WORD_DESC *name;
         FUNCTION_DEF *funcdef;
    {
      SHELL_VAR *var;

      if (check_identifier (name, posixly_correct) == 0)
        {
          if (posixly_correct && interactive_shell == 0)
            {
              last_command_exit_value = EX_BADUSAGE;
              jump_to_top_level (ERREXIT);
            }
          return (EXECUTION_FAILURE);
        }

      ...

      bind_function (name->word, funcdef->command);
      return (EXECUTION_SUCCESS);
    }

    /* Make sure that WORD is a valid shell identifier, i.e.
       does not contain a dollar sign, nor is quoted in any way.  Nor
       does it consist of all digits.  If CHECK_WORD is non-zero,
       the word is checked to ensure that it consists of only letters,
       digits, and underscores. */
    int
    check_identifier (word, check_word)
         WORD_DESC *word;
         int check_word;
    {
      if ((word->flags & (W_HASDOLLAR|W_QUOTED)) || all_digits (word->word))
        {
          internal_error (_("`%s': not a valid identifier"), word->word);
          return (0);
        }
      else if (check_word && legal_identifier (word->word) == 0)
        {
          internal_error (_("`%s': not a valid identifier"), word->word);
          return (0);
        }
      else
        return (1);
    }

    /* Return 1 if this token is a legal shell `identifier'; that is,
it consists
       solely of letters, digits, and underscores, and does not begin with a
       digit. */
    int
    legal_identifier (name)
         char *name;
    {
      register char *s;
      unsigned char c;

      if (!name || !(c = *name) || (legal_variable_starter (c) == 0))
        return (0);

      for (s = name + 1; (c = *s) != 0; s++)
        {
          if (legal_variable_char (c) == 0)
            return (0);
        }
      return (1);
    }

    #define legal_variable_starter(c) (ISALPHA(c) || (c == '_'))
    #define legal_variable_char(c)        (ISALNUM(c) || c == '_')

-- 
konsolebox



reply via email to

[Prev in Thread] Current Thread [Next in Thread]