cvs-cvs
[Top][All Lists]
Advanced

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

[Cvs-cvs] Changes to ccvs/lib/regcomp.c


From: Derek Robert Price
Subject: [Cvs-cvs] Changes to ccvs/lib/regcomp.c
Date: Sun, 04 Sep 2005 01:59:03 -0400

Index: ccvs/lib/regcomp.c
diff -u ccvs/lib/regcomp.c:1.1 ccvs/lib/regcomp.c:1.2
--- ccvs/lib/regcomp.c:1.1      Fri Aug 12 20:58:10 2005
+++ ccvs/lib/regcomp.c  Sun Sep  4 05:58:56 2005
@@ -18,12 +18,11 @@
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
 
 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
-                                         int length, reg_syntax_t syntax);
+                                         Idx length, reg_syntax_t syntax);
 static void re_compile_fastmap_iter (regex_t *bufp,
                                     const re_dfastate_t *init_state,
                                     char *fastmap);
-static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
-static void init_word_char (re_dfa_t *dfa);
+static reg_errcode_t init_dfa (re_dfa_t *dfa, Idx pat_len);
 #ifdef RE_ENABLE_I18N
 static void free_charset (re_charset_t *cset);
 #endif /* RE_ENABLE_I18N */
@@ -33,7 +32,6 @@
 static void optimize_utf8 (re_dfa_t *dfa);
 #endif
 static reg_errcode_t analyze (regex_t *preg);
-static reg_errcode_t create_initial_state (re_dfa_t *dfa);
 static reg_errcode_t preorder (bin_tree_t *root,
                               reg_errcode_t (fn (void *, bin_tree_t *)),
                               void *extra);
@@ -47,39 +45,31 @@
 static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
 static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
 static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
-static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node,
-                                            int top_clone_node, int root_node,
-                                            unsigned int constraint);
-static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
-                                    unsigned int constraint);
-static int search_duplicated_node (re_dfa_t *dfa, int org_node,
+static Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int 
constraint);
+static Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
                                   unsigned int constraint);
 static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
 static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
-                                        int node, int root);
+                                        Idx node, bool root);
 static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
-static int fetch_number (re_string_t *input, re_token_t *token,
-                        reg_syntax_t syntax);
-static void fetch_token (re_token_t *result, re_string_t *input,
+static Idx fetch_number (re_string_t *input, re_token_t *token,
                         reg_syntax_t syntax);
 static int peek_token (re_token_t *token, re_string_t *input,
                        reg_syntax_t syntax);
-static int peek_token_bracket (re_token_t *token, re_string_t *input,
-                              reg_syntax_t syntax);
 static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
                          reg_syntax_t syntax, reg_errcode_t *err);
 static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
                                  re_token_t *token, reg_syntax_t syntax,
-                                 int nest, reg_errcode_t *err);
+                                 Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
                                 re_token_t *token, reg_syntax_t syntax,
-                                int nest, reg_errcode_t *err);
+                                Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
                                     re_token_t *token, reg_syntax_t syntax,
-                                    int nest, reg_errcode_t *err);
+                                    Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
                                  re_token_t *token, reg_syntax_t syntax,
-                                 int nest, reg_errcode_t *err);
+                                 Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
                                 re_dfa_t *dfa, re_token_t *token,
                                 reg_syntax_t syntax, reg_errcode_t *err);
@@ -91,52 +81,34 @@
                                            re_token_t *token, int token_len,
                                            re_dfa_t *dfa,
                                            reg_syntax_t syntax,
-                                           int accept_hyphen);
+                                           bool accept_hyphen);
 static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
                                          re_string_t *regexp,
                                          re_token_t *token);
-#ifndef _LIBC
-# ifdef RE_ENABLE_I18N
-static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
-                                     re_charset_t *mbcset, int *range_alloc,
-                                     bracket_elem_t *start_elem,
-                                     bracket_elem_t *end_elem);
-static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
-                                            re_charset_t *mbcset,
-                                            int *coll_sym_alloc,
-                                            const unsigned char *name);
-# else /* not RE_ENABLE_I18N */
-static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
-                                     bracket_elem_t *start_elem,
-                                     bracket_elem_t *end_elem);
-static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
-                                            const unsigned char *name);
-# endif /* not RE_ENABLE_I18N */
-#endif /* not _LIBC */
 #ifdef RE_ENABLE_I18N
 static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
                                        re_charset_t *mbcset,
-                                       int *equiv_class_alloc,
+                                       Idx *equiv_class_alloc,
                                        const unsigned char *name);
-static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
+static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
                                      re_bitset_ptr_t sbcset,
                                      re_charset_t *mbcset,
-                                     int *char_class_alloc,
+                                     Idx *char_class_alloc,
                                      const unsigned char *class_name,
                                      reg_syntax_t syntax);
 #else  /* not RE_ENABLE_I18N */
 static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
                                        const unsigned char *name);
-static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
+static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
                                      re_bitset_ptr_t sbcset,
                                      const unsigned char *class_name,
                                      reg_syntax_t syntax);
 #endif /* not RE_ENABLE_I18N */
 static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
-                                      unsigned RE_TRANSLATE_TYPE trans,
+                                      unsigned REG_TRANSLATE_TYPE trans,
                                       const unsigned char *class_name,
                                       const unsigned char *extra,
-                                      int non_match, reg_errcode_t *err);
+                                      bool non_match, reg_errcode_t *err);
 static bin_tree_t *create_tree (re_dfa_t *dfa,
                                bin_tree_t *left, bin_tree_t *right,
                                re_token_type_t type);
@@ -234,24 +206,22 @@
    compiles PATTERN (of length LENGTH) and puts the result in BUFP.
    Returns 0 if the pattern was valid, otherwise an error string.
 
-   Assumes the `allocated' (and perhaps `buffer') and `translate' fields
+   Assumes the `re_allocated' (and perhaps `re_buffer') and `translate' fields
    are set in BUFP on entry.  */
 
 const char *
-re_compile_pattern (pattern, length, bufp)
-    const char *pattern;
-    size_t length;
-    struct re_pattern_buffer *bufp;
+re_compile_pattern (const char *pattern, size_t length,
+                   struct re_pattern_buffer *bufp)
 {
   reg_errcode_t ret;
 
   /* And GNU code determines whether or not to get register information
      by passing null for the REGS argument to re_match, etc., not by
-     setting no_sub, unless RE_NO_SUB is set.  */
-  bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
+     setting re_no_sub, unless REG_NO_SUB is set.  */
+  bufp->re_no_sub = !!(re_syntax_options & REG_NO_SUB);
 
   /* Match anchors at newline.  */
-  bufp->newline_anchor = 1;
+  bufp->re_newline_anchor = 1;
 
   ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
 
@@ -279,8 +249,7 @@
    defined in regex.h.  We return the old syntax.  */
 
 reg_syntax_t
-re_set_syntax (syntax)
-    reg_syntax_t syntax;
+re_set_syntax (reg_syntax_t syntax)
 {
   reg_syntax_t ret = re_syntax_options;
 
@@ -292,11 +261,10 @@
 #endif
 
 int
-re_compile_fastmap (bufp)
-    struct re_pattern_buffer *bufp;
+re_compile_fastmap (struct re_pattern_buffer *bufp)
 {
-  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
-  char *fastmap = bufp->fastmap;
+  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
+  char *fastmap = bufp->re_fastmap;
 
   memset (fastmap, '\0', sizeof (char) * SBC_MAX);
   re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
@@ -306,7 +274,7 @@
     re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
   if (dfa->init_state != dfa->init_state_begbuf)
     re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
-  bufp->fastmap_accurate = 1;
+  bufp->re_fastmap_accurate = 1;
   return 0;
 }
 #ifdef _LIBC
@@ -315,7 +283,7 @@
 
 static inline void
 __attribute ((always_inline))
-re_set_fastmap (char *fastmap, int icase, int ch)
+re_set_fastmap (char *fastmap, bool icase, int ch)
 {
   fastmap[ch] = 1;
   if (icase)
@@ -326,26 +294,25 @@
    Compile fastmap for the initial_state INIT_STATE.  */
 
 static void
-re_compile_fastmap_iter (bufp, init_state, fastmap)
-     regex_t *bufp;
-     const re_dfastate_t *init_state;
-     char *fastmap;
-{
-  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
-  int node_cnt;
-  int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
+re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
+                        char *fastmap)
+{
+  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
+  Idx node_cnt;
+  bool icase = (dfa->mb_cur_max == 1 && (bufp->re_syntax & REG_IGNORE_CASE));
   for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
     {
-      int node = init_state->nodes.elems[node_cnt];
+      Idx node = init_state->nodes.elems[node_cnt];
       re_token_type_t type = dfa->nodes[node].type;
 
       if (type == CHARACTER)
        {
          re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
 #ifdef RE_ENABLE_I18N
-         if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
+         if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
            {
-             unsigned char *buf = alloca (dfa->mb_cur_max), *p;
+             unsigned char buf[MB_LEN_MAX];
+             unsigned char *p;
              wchar_t wc;
              mbstate_t state;
 
@@ -360,7 +327,7 @@
                           &state) == p - buf
                  && (__wcrtomb ((char *) buf, towlower (wc), &state)
                      != (size_t) -1))
-               re_set_fastmap (fastmap, 0, buf[0]);
+               re_set_fastmap (fastmap, false, buf[0]);
            }
 #endif
        }
@@ -369,13 +336,13 @@
          int i, j, ch;
          for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
            for (j = 0; j < UINT_BITS; ++j, ++ch)
-             if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
+             if (dfa->nodes[node].opr.sbcset[i] & (1u << j))
                re_set_fastmap (fastmap, icase, ch);
        }
 #ifdef RE_ENABLE_I18N
       else if (type == COMPLEX_BRACKET)
        {
-         int i;
+         Idx i;
          re_charset_t *cset = dfa->nodes[node].opr.mbcset;
          if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
              || cset->nranges || cset->nchar_classes)
@@ -411,11 +378,11 @@
              memset (&state, '\0', sizeof (state));
              if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
                re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
-             if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
+             if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
                {
                  if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
                      != (size_t) -1)
-                   re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
+                   re_set_fastmap (fastmap, false, *(unsigned char *) buf);
                }
            }
        }
@@ -428,7 +395,7 @@
        {
          memset (fastmap, '\1', sizeof (char) * SBC_MAX);
          if (type == END_OF_RE)
-           bufp->can_be_null = 1;
+           bufp->re_can_be_null = 1;
          return;
        }
     }
@@ -440,14 +407,14 @@
    PREG is a regex_t *.  We do not expect any fields to be initialized,
    since POSIX says we shouldn't.  Thus, we set
 
-     `buffer' to the compiled pattern;
-     `used' to the length of the compiled pattern;
-     `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
+     `re_buffer' to the compiled pattern;
+     `re_used' to the length of the compiled pattern;
+     `re_syntax' to REG_SYNTAX_POSIX_EXTENDED if the
        REG_EXTENDED bit in CFLAGS is set; otherwise, to
-       RE_SYNTAX_POSIX_BASIC;
-     `newline_anchor' to REG_NEWLINE being set in CFLAGS;
-     `fastmap' to an allocated space for the fastmap;
-     `fastmap_accurate' to zero;
+       REG_SYNTAX_POSIX_BASIC;
+     `re_newline_anchor' to REG_NEWLINE being set in CFLAGS;
+     `re_fastmap' to an allocated space for the fastmap;
+     `re_fastmap_accurate' to zero;
      `re_nsub' to the number of subexpressions in PATTERN.
 
    PATTERN is the address of the pattern string.
@@ -471,38 +438,35 @@
    the return codes and their meanings.)  */
 
 int
-regcomp (preg, pattern, cflags)
-    regex_t *__restrict preg;
-    const char *__restrict pattern;
-    int cflags;
+regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
 {
   reg_errcode_t ret;
-  reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
-                        : RE_SYNTAX_POSIX_BASIC);
+  reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? REG_SYNTAX_POSIX_EXTENDED
+                       : REG_SYNTAX_POSIX_BASIC);
 
-  preg->buffer = NULL;
-  preg->allocated = 0;
-  preg->used = 0;
+  preg->re_buffer = NULL;
+  preg->re_allocated = 0;
+  preg->re_used = 0;
 
   /* Try to allocate space for the fastmap.  */
-  preg->fastmap = re_malloc (char, SBC_MAX);
-  if (BE (preg->fastmap == NULL, 0))
+  preg->re_fastmap = re_malloc (char, SBC_MAX);
+  if (BE (preg->re_fastmap == NULL, 0))
     return REG_ESPACE;
 
-  syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
+  syntax |= (cflags & REG_ICASE) ? REG_IGNORE_CASE : 0;
 
   /* If REG_NEWLINE is set, newlines are treated differently.  */
   if (cflags & REG_NEWLINE)
     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
-      syntax &= ~RE_DOT_NEWLINE;
-      syntax |= RE_HAT_LISTS_NOT_NEWLINE;
+      syntax &= ~REG_DOT_NEWLINE;
+      syntax |= REG_HAT_LISTS_NOT_NEWLINE;
       /* It also changes the matching behavior.  */
-      preg->newline_anchor = 1;
+      preg->re_newline_anchor = 1;
     }
   else
-    preg->newline_anchor = 0;
-  preg->no_sub = !!(cflags & REG_NOSUB);
-  preg->translate = NULL;
+    preg->re_newline_anchor = 0;
+  preg->re_no_sub = !!(cflags & REG_NOSUB);
+  preg->re_translate = NULL;
 
   ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
 
@@ -511,7 +475,7 @@
   if (ret == REG_ERPAREN)
     ret = REG_EPAREN;
 
-  /* We have already checked preg->fastmap != NULL.  */
+  /* We have already checked preg->re_fastmap != NULL.  */
   if (BE (ret == REG_NOERROR, 1))
     /* Compute the fastmap now, since regexec cannot modify the pattern
        buffer.  This function never fails in this implementation.  */
@@ -519,8 +483,8 @@
   else
     {
       /* Some error occurred while compiling the expression.  */
-      re_free (preg->fastmap);
-      preg->fastmap = NULL;
+      re_free (preg->re_fastmap);
+      preg->re_fastmap = NULL;
     }
 
   return (int) ret;
@@ -533,11 +497,8 @@
    from either regcomp or regexec.   We don't use PREG here.  */
 
 size_t
-regerror (errcode, preg, errbuf, errbuf_size)
-    int errcode;
-    const regex_t *preg;
-    char *errbuf;
-    size_t errbuf_size;
+regerror (int errcode, const regex_t *__restrict preg,
+         char *__restrict errbuf, size_t errbuf_size)
 {
   const char *msg;
   size_t msg_size;
@@ -597,7 +558,7 @@
 static void
 free_dfa_content (re_dfa_t *dfa)
 {
-  int i, j;
+  Idx i, j;
 
   if (dfa->nodes)
     for (i = 0; i < dfa->nodes_len; ++i)
@@ -645,20 +606,19 @@
 /* Free dynamically allocated space used by PREG.  */
 
 void
-regfree (preg)
-    regex_t *preg;
+regfree (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   if (BE (dfa != NULL, 1))
     free_dfa_content (dfa);
-  preg->buffer = NULL;
-  preg->allocated = 0;
+  preg->re_buffer = NULL;
+  preg->re_allocated = 0;
 
-  re_free (preg->fastmap);
-  preg->fastmap = NULL;
+  re_free (preg->re_fastmap);
+  preg->re_fastmap = NULL;
 
-  re_free (preg->translate);
-  preg->translate = NULL;
+  re_free (preg->re_translate);
+  preg->re_translate = NULL;
 }
 #ifdef _LIBC
 weak_alias (__regfree, regfree)
@@ -679,32 +639,31 @@
    regcomp/regexec above without link errors.  */
 weak_function
 # endif
-re_comp (s)
-     const char *s;
+re_comp (const char *s)
 {
   reg_errcode_t ret;
   char *fastmap;
 
   if (!s)
     {
-      if (!re_comp_buf.buffer)
+      if (!re_comp_buf.re_buffer)
        return gettext ("No previous regular expression");
       return 0;
     }
 
-  if (re_comp_buf.buffer)
+  if (re_comp_buf.re_buffer)
     {
-      fastmap = re_comp_buf.fastmap;
-      re_comp_buf.fastmap = NULL;
+      fastmap = re_comp_buf.re_fastmap;
+      re_comp_buf.re_fastmap = NULL;
       __regfree (&re_comp_buf);
       memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
-      re_comp_buf.fastmap = fastmap;
+      re_comp_buf.re_fastmap = fastmap;
     }
 
-  if (re_comp_buf.fastmap == NULL)
+  if (re_comp_buf.re_fastmap == NULL)
     {
-      re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
-      if (re_comp_buf.fastmap == NULL)
+      re_comp_buf.re_fastmap = (char *) malloc (SBC_MAX);
+      if (re_comp_buf.re_fastmap == NULL)
        return (char *) gettext (__re_error_msgid
                                 + __re_error_msgid_idx[(int) REG_ESPACE]);
     }
@@ -713,7 +672,7 @@
      don't need to initialize the pattern buffer fields which affect it.  */
 
   /* Match anchors at newlines.  */
-  re_comp_buf.newline_anchor = 1;
+  re_comp_buf.re_newline_anchor = 1;
 
   ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
 
@@ -738,40 +697,37 @@
    SYNTAX indicate regular expression's syntax.  */
 
 static reg_errcode_t
-re_compile_internal (preg, pattern, length, syntax)
-     regex_t *preg;
-     const char * pattern;
-     int length;
-     reg_syntax_t syntax;
+re_compile_internal (regex_t *preg, const char *pattern, Idx length,
+                    reg_syntax_t syntax)
 {
   reg_errcode_t err = REG_NOERROR;
   re_dfa_t *dfa;
   re_string_t regexp;
 
   /* Initialize the pattern buffer.  */
-  preg->fastmap_accurate = 0;
-  preg->syntax = syntax;
-  preg->not_bol = preg->not_eol = 0;
-  preg->used = 0;
+  preg->re_fastmap_accurate = 0;
+  preg->re_syntax = syntax;
+  preg->re_not_bol = preg->re_not_eol = 0;
+  preg->re_used = 0;
   preg->re_nsub = 0;
-  preg->can_be_null = 0;
-  preg->regs_allocated = REGS_UNALLOCATED;
+  preg->re_can_be_null = 0;
+  preg->re_regs_allocated = REG_UNALLOCATED;
 
   /* Initialize the dfa.  */
-  dfa = (re_dfa_t *) preg->buffer;
-  if (BE (preg->allocated < sizeof (re_dfa_t), 0))
+  dfa = (re_dfa_t *) preg->re_buffer;
+  if (BE (preg->re_allocated < sizeof (re_dfa_t), 0))
     {
       /* If zero allocated, but buffer is non-null, try to realloc
         enough space.  This loses if buffer's address is bogus, but
-        that is the user's responsibility.  If ->buffer is NULL this
+        that is the user's responsibility.  If buffer is null this
         is a simple allocation.  */
-      dfa = re_realloc (preg->buffer, re_dfa_t, 1);
+      dfa = re_realloc (preg->re_buffer, re_dfa_t, 1);
       if (dfa == NULL)
        return REG_ESPACE;
-      preg->allocated = sizeof (re_dfa_t);
-      preg->buffer = (unsigned char *) dfa;
+      preg->re_allocated = sizeof (re_dfa_t);
+      preg->re_buffer = (unsigned char *) dfa;
     }
-  preg->used = sizeof (re_dfa_t);
+  preg->re_used = sizeof (re_dfa_t);
 
   __libc_lock_init (dfa->lock);
 
@@ -779,8 +735,8 @@
   if (BE (err != REG_NOERROR, 0))
     {
       free_dfa_content (dfa);
-      preg->buffer = NULL;
-      preg->allocated = 0;
+      preg->re_buffer = NULL;
+      preg->re_allocated = 0;
       return err;
     }
 #ifdef DEBUG
@@ -788,16 +744,16 @@
   strncpy (dfa->re_str, pattern, length + 1);
 #endif
 
-  err = re_string_construct (&regexp, pattern, length, preg->translate,
-                            syntax & RE_ICASE, dfa);
+  err = re_string_construct (&regexp, pattern, length, preg->re_translate,
+                            syntax & REG_IGNORE_CASE, dfa);
   if (BE (err != REG_NOERROR, 0))
     {
     re_compile_internal_free_return:
       free_workarea_compile (preg);
       re_string_destruct (&regexp);
       free_dfa_content (dfa);
-      preg->buffer = NULL;
-      preg->allocated = 0;
+      preg->re_buffer = NULL;
+      preg->re_allocated = 0;
       return err;
     }
 
@@ -814,7 +770,7 @@
 
 #ifdef RE_ENABLE_I18N
   /* If possible, do searching in single byte encoding to speed things up.  */
-  if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
+  if (dfa->is_utf8 && !(syntax & REG_IGNORE_CASE) && preg->re_translate == 
NULL)
     optimize_utf8 (dfa);
 #endif
 
@@ -828,8 +784,8 @@
   if (BE (err != REG_NOERROR, 0))
     {
       free_dfa_content (dfa);
-      preg->buffer = NULL;
-      preg->allocated = 0;
+      preg->re_buffer = NULL;
+      preg->re_allocated = 0;
     }
 
   return err;
@@ -839,11 +795,9 @@
    as the initial length of some arrays.  */
 
 static reg_errcode_t
-init_dfa (dfa, pat_len)
-     re_dfa_t *dfa;
-     int pat_len;
+init_dfa (re_dfa_t *dfa, Idx pat_len)
 {
-  int table_size;
+  __re_size_t table_size;
 #ifndef _LIBC
   char *codeset_name;
 #endif
@@ -854,16 +808,14 @@
   dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
 
   dfa->nodes_alloc = pat_len + 1;
-  dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
-
-  dfa->states_alloc = pat_len + 1;
+  dfa->nodes = re_xmalloc (re_token_t, dfa->nodes_alloc);
 
   /*  table_size = 2 ^ ceil(log pat_len) */
-  for (table_size = 1; table_size > 0; table_size <<= 1)
-    if (table_size > pat_len)
-      break;
+  for (table_size = 1; table_size <= pat_len; table_size <<= 1)
+    if (0 < (Idx) -1 && table_size == 0)
+      return REG_ESPACE;
 
-  dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
+  dfa->state_table = re_calloc (struct re_state_table_entry, table_size);
   dfa->state_hash_mask = table_size - 1;
 
   dfa->mb_cur_max = MB_CUR_MAX;
@@ -906,7 +858,7 @@
        {
          int i, j, ch;
 
-         dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
+         dfa->sb_char = re_calloc (unsigned int, BITSET_UINTS);
          if (BE (dfa->sb_char == NULL, 0))
            return REG_ESPACE;
 
@@ -919,7 +871,7 @@
              {
                wint_t wch = __btowc (ch);
                if (wch != WEOF)
-                 dfa->sb_char[i] |= 1 << j;
+                 dfa->sb_char[i] |= 1u << j;
 # ifndef _LIBC
                if (isascii (ch) && wch != ch)
                  dfa->map_notascii = 1;
@@ -939,24 +891,22 @@
    character used by some operators like "\<", "\>", etc.  */
 
 static void
-init_word_char (dfa)
-     re_dfa_t *dfa;
+init_word_char (re_dfa_t *dfa)
 {
   int i, j, ch;
   dfa->word_ops_used = 1;
   for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
     for (j = 0; j < UINT_BITS; ++j, ++ch)
       if (isalnum (ch) || ch == '_')
-       dfa->word_char[i] |= 1 << j;
+       dfa->word_char[i] |= 1u << j;
 }
 
 /* Free the work area which are only used while compiling.  */
 
 static void
-free_workarea_compile (preg)
-     regex_t *preg;
+free_workarea_compile (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_storage_t *storage, *next;
   for (storage = dfa->str_tree_storage; storage; storage = next)
     {
@@ -973,10 +923,9 @@
 /* Create initial states for all contexts.  */
 
 static reg_errcode_t
-create_initial_state (dfa)
-     re_dfa_t *dfa;
+create_initial_state (re_dfa_t *dfa)
 {
-  int first, i;
+  Idx first, i;
   reg_errcode_t err;
   re_node_set init_nodes;
 
@@ -995,10 +944,10 @@
   if (dfa->nbackref > 0)
     for (i = 0; i < init_nodes.nelem; ++i)
       {
-       int node_idx = init_nodes.elems[i];
+       Idx node_idx = init_nodes.elems[i];
        re_token_type_t type = dfa->nodes[node_idx].type;
 
-       int clexp_idx;
+       Idx clexp_idx;
        if (type != OP_BACK_REF)
          continue;
        for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
@@ -1014,7 +963,7 @@
 
        if (type == OP_BACK_REF)
          {
-           int dest_idx = dfa->edests[node_idx].elems[0];
+           Idx dest_idx = dfa->edests[node_idx].elems[0];
            if (!re_node_set_contains (&init_nodes, dest_idx))
              {
                re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
@@ -1056,17 +1005,19 @@
    DFA nodes where needed.  */
 
 static void
-optimize_utf8 (dfa)
-     re_dfa_t *dfa;
+optimize_utf8 (re_dfa_t *dfa)
 {
-  int node, i, mb_chars = 0, has_period = 0;
+  Idx node;
+  int i;
+  bool mb_chars = false;
+  bool has_period = false;
 
   for (node = 0; node < dfa->nodes_len; ++node)
     switch (dfa->nodes[node].type)
       {
       case CHARACTER:
        if (dfa->nodes[node].opr.c >= 0x80)
-         mb_chars = 1;
+         mb_chars = true;
        break;
       case ANCHOR:
        switch (dfa->nodes[node].opr.idx)
@@ -1082,7 +1033,7 @@
          }
        break;
       case OP_PERIOD:
-        has_period = 1;
+        has_period = true;
         break;
       case OP_BACK_REF:
       case OP_ALT:
@@ -1124,25 +1075,24 @@
    "eclosure", and "inveclosure".  */
 
 static reg_errcode_t
-analyze (preg)
-     regex_t *preg;
+analyze (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   reg_errcode_t ret;
 
   /* Allocate arrays.  */
-  dfa->nexts = re_malloc (int, dfa->nodes_alloc);
-  dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
-  dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
+  dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
+  dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
+  dfa->edests = re_xmalloc (re_node_set, dfa->nodes_alloc);
   dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
   if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
          || dfa->eclosures == NULL, 0))
     return REG_ESPACE;
 
-  dfa->subexp_map = re_malloc (int, preg->re_nsub);
+  dfa->subexp_map = re_xmalloc (Idx, preg->re_nsub);
   if (dfa->subexp_map != NULL)
     {
-      int i;
+      Idx i;
       for (i = 0; i < preg->re_nsub; i++)
        dfa->subexp_map[i] = i;
       preorder (dfa->str_tree, optimize_subexps, dfa);
@@ -1172,10 +1122,10 @@
 
   /* We only need this during the prune_impossible_nodes pass in regexec.c;
      skip it if p_i_n will not run, as calc_inveclosure can be quadratic.  */
-  if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
+  if ((!preg->re_no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
       || dfa->nbackref)
     {
-      dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
+      dfa->inveclosures = re_xmalloc (re_node_set, dfa->nodes_len);
       if (BE (dfa->inveclosures == NULL, 0))
         return REG_ESPACE;
       ret = calc_inveclosure (dfa);
@@ -1188,10 +1138,8 @@
    implement parse tree visits.  Instead, we use parent pointers and
    some hairy code in these two functions.  */
 static reg_errcode_t
-postorder (root, fn, extra)
-     bin_tree_t *root;
-     reg_errcode_t (fn (void *, bin_tree_t *));
-     void *extra;
+postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
+          void *extra)
 {
   bin_tree_t *node, *prev;
 
@@ -1222,10 +1170,8 @@
 }
 
 static reg_errcode_t
-preorder (root, fn, extra)
-     bin_tree_t *root;
-     reg_errcode_t (fn (void *, bin_tree_t *));
-     void *extra;
+preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
+         void *extra)
 {
   bin_tree_t *node;
 
@@ -1257,9 +1203,7 @@
    re_search_internal to map the inner one's opr.idx to this one's.  Adjust
    backreferences as well.  Requires a preorder visit.  */
 static reg_errcode_t
-optimize_subexps (extra, node)
-     void *extra;
-     bin_tree_t *node;
+optimize_subexps (void *extra, bin_tree_t *node)
 {
   re_dfa_t *dfa = (re_dfa_t *) extra;
 
@@ -1273,15 +1217,15 @@
   else if (node->token.type == SUBEXP
            && node->left && node->left->token.type == SUBEXP)
     {
-      int other_idx = node->left->token.opr.idx;
+      Idx other_idx = node->left->token.opr.idx;
 
       node->left = node->left->left;
       if (node->left)
         node->left->parent = node;
 
       dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
-      if (other_idx < 8 * sizeof (dfa->used_bkref_map))
-       dfa->used_bkref_map &= ~(1 << other_idx);
+      if (other_idx < CHAR_BIT * sizeof dfa->used_bkref_map)
+       dfa->used_bkref_map &= ~(1u << other_idx);
     }
 
   return REG_NOERROR;
@@ -1290,9 +1234,7 @@
 /* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
    of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP.  */
 static reg_errcode_t
-lower_subexps (extra, node)
-     void *extra;
-     bin_tree_t *node;
+lower_subexps (void *extra, bin_tree_t *node)
 {
   regex_t *preg = (regex_t *) extra;
   reg_errcode_t err = REG_NOERROR;
@@ -1314,23 +1256,20 @@
 }
 
 static bin_tree_t *
-lower_subexp (err, preg, node)
-     reg_errcode_t *err;
-     regex_t *preg;
-     bin_tree_t *node;
+lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *body = node->left;
   bin_tree_t *op, *cls, *tree1, *tree;
 
-  if (preg->no_sub
+  if (preg->re_no_sub
       /* We do not optimize empty subexpressions, because otherwise we may
         have bad CONCAT nodes with NULL children.  This is obviously not
         very common, so we do not lose much.  An example that triggers
         this case is the sed "script" /\(\)/x.  */
       && node->left != NULL
-      && (node->token.opr.idx >= 8 * sizeof (dfa->used_bkref_map)
-         || !(dfa->used_bkref_map & (1 << node->token.opr.idx))))
+      && (node->token.opr.idx >= CHAR_BIT * sizeof dfa->used_bkref_map
+         || !(dfa->used_bkref_map & (1u << node->token.opr.idx))))
     return node->left;
 
   /* Convert the SUBEXP node to the concatenation of an
@@ -1353,9 +1292,7 @@
 /* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
    nodes.  Requires a postorder visit.  */
 static reg_errcode_t
-calc_first (extra, node)
-     void *extra;
-     bin_tree_t *node;
+calc_first (void *extra, bin_tree_t *node)
 {
   re_dfa_t *dfa = (re_dfa_t *) extra;
   if (node->token.type == CONCAT)
@@ -1367,7 +1304,7 @@
     {
       node->first = node;
       node->node_idx = re_dfa_add_node (dfa, node->token);
-      if (BE (node->node_idx == -1, 0))
+      if (BE (node->node_idx == REG_MISSING, 0))
         return REG_ESPACE;
     }
   return REG_NOERROR;
@@ -1375,9 +1312,7 @@
 
 /* Pass 2: compute NEXT on the tree.  Preorder visit.  */
 static reg_errcode_t
-calc_next (extra, node)
-     void *extra;
-     bin_tree_t *node;
+calc_next (void *extra, bin_tree_t *node)
 {
   switch (node->token.type)
     {
@@ -1400,12 +1335,10 @@
 
 /* Pass 3: link all DFA nodes to their NEXT node (any order will do).  */
 static reg_errcode_t
-link_nfa_nodes (extra, node)
-     void *extra;
-     bin_tree_t *node;
+link_nfa_nodes (void *extra, bin_tree_t *node)
 {
   re_dfa_t *dfa = (re_dfa_t *) extra;
-  int idx = node->node_idx;
+  Idx idx = node->node_idx;
   reg_errcode_t err = REG_NOERROR;
 
   switch (node->token.type)
@@ -1420,7 +1353,7 @@
     case OP_DUP_ASTERISK:
     case OP_ALT:
       {
-       int left, right;
+       Idx left, right;
        dfa->has_plural_match = 1;
        if (node->left != NULL)
          left = node->left->first->node_idx;
@@ -1430,8 +1363,8 @@
          right = node->right->first->node_idx;
        else
          right = node->next->node_idx;
-       assert (left > -1);
-       assert (right > -1);
+       assert (REG_VALID_INDEX (left));
+       assert (REG_VALID_INDEX (right));
        err = re_node_set_init_2 (dfa->edests + idx, left, right);
       }
       break;
@@ -1462,18 +1395,16 @@
    to their own constraint.  */
 
 static reg_errcode_t
-duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
-                       init_constraint)
-     re_dfa_t *dfa;
-     int top_org_node, top_clone_node, root_node;
-     unsigned int init_constraint;
+duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node,
+                       Idx top_clone_node, Idx root_node,
+                       unsigned int init_constraint)
 {
-  reg_errcode_t err;
-  int org_node, clone_node, ret;
+  Idx org_node, clone_node;
+  bool ok;
   unsigned int constraint = init_constraint;
   for (org_node = top_org_node, clone_node = top_clone_node;;)
     {
-      int org_dest, clone_dest;
+      Idx org_dest, clone_dest;
       if (dfa->nodes[org_node].type == OP_BACK_REF)
        {
          /* If the back reference epsilon-transit, its destination must
@@ -1482,12 +1413,12 @@
             edests of the back reference.  */
          org_dest = dfa->nexts[org_node];
          re_node_set_empty (dfa->edests + clone_node);
-         err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
-         if (BE (err != REG_NOERROR, 0))
-           return err;
+         clone_dest = duplicate_node (dfa, org_dest, constraint);
+         if (BE (clone_dest == REG_MISSING, 0))
+           return REG_ESPACE;
          dfa->nexts[clone_node] = dfa->nexts[org_node];
-         ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-         if (BE (ret < 0, 0))
+         ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
       else if (dfa->edests[org_node].nelem == 0)
@@ -1512,19 +1443,19 @@
                  /* ...but if the node is root_node itself, it means the
                     epsilon closure have a loop, then tie it to the
                     destination of the root_node.  */
-                 ret = re_node_set_insert (dfa->edests + clone_node,
+                 ok = re_node_set_insert (dfa->edests + clone_node,
                                            org_dest);
-                 if (BE (ret < 0, 0))
+                 if (BE (! ok, 0))
                    return REG_ESPACE;
                  break;
                }
              constraint |= dfa->nodes[org_node].opr.ctx_type;
            }
-         err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
-         if (BE (err != REG_NOERROR, 0))
-           return err;
-         ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-         if (BE (ret < 0, 0))
+         clone_dest = duplicate_node (dfa, org_dest, constraint);
+         if (BE (clone_dest == REG_MISSING, 0))
+           return REG_ESPACE;
+         ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
       else /* dfa->edests[org_node].nelem == 2 */
@@ -1535,14 +1466,15 @@
          re_node_set_empty (dfa->edests + clone_node);
          /* Search for a duplicated node which satisfies the constraint.  */
          clone_dest = search_duplicated_node (dfa, org_dest, constraint);
-         if (clone_dest == -1)
+         if (clone_dest == REG_MISSING)
            {
              /* There are no such a duplicated node, create a new one.  */
-             err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
-             if (BE (err != REG_NOERROR, 0))
-               return err;
-             ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-             if (BE (ret < 0, 0))
+             reg_errcode_t err;
+             clone_dest = duplicate_node (dfa, org_dest, constraint);
+             if (BE (clone_dest == REG_MISSING, 0))
+               return REG_ESPACE;
+             ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+             if (BE (! ok, 0))
                return REG_ESPACE;
              err = duplicate_node_closure (dfa, org_dest, clone_dest,
                                            root_node, constraint);
@@ -1553,17 +1485,17 @@
            {
              /* There are a duplicated node which satisfy the constraint,
                 use it to avoid infinite loop.  */
-             ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-             if (BE (ret < 0, 0))
+             ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+             if (BE (! ok, 0))
                return REG_ESPACE;
            }
 
          org_dest = dfa->edests[org_node].elems[1];
-         err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
-         if (BE (err != REG_NOERROR, 0))
-           return err;
-         ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-         if (BE (ret < 0, 0))
+         clone_dest = duplicate_node (dfa, org_dest, constraint);
+         if (BE (clone_dest == REG_MISSING, 0))
+           return REG_ESPACE;
+         ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
       org_node = org_dest;
@@ -1575,61 +1507,56 @@
 /* Search for a node which is duplicated from the node ORG_NODE, and
    satisfies the constraint CONSTRAINT.  */
 
-static int
-search_duplicated_node (dfa, org_node, constraint)
-     re_dfa_t *dfa;
-     int org_node;
-     unsigned int constraint;
+static Idx
+search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
+                       unsigned int constraint)
 {
-  int idx;
+  Idx idx;
   for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
     {
       if (org_node == dfa->org_indices[idx]
          && constraint == dfa->nodes[idx].constraint)
        return idx; /* Found.  */
     }
-  return -1; /* Not found.  */
+  return REG_MISSING; /* Not found.  */
 }
 
 /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
-   The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded,
-   otherwise return the error code.  */
+   Return the index of the new node, or REG_MISSING if insufficient storage is
+   available.  */
 
-static reg_errcode_t
-duplicate_node (new_idx, dfa, org_idx, constraint)
-     re_dfa_t *dfa;
-     int *new_idx, org_idx;
-     unsigned int constraint;
+static Idx
+duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
 {
-  int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
-  if (BE (dup_idx == -1, 0))
-    return REG_ESPACE;
-  dfa->nodes[dup_idx].constraint = constraint;
-  if (dfa->nodes[org_idx].type == ANCHOR)
-    dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
-  dfa->nodes[dup_idx].duplicated = 1;
-
-  /* Store the index of the original node.  */
-  dfa->org_indices[dup_idx] = org_idx;
-  *new_idx = dup_idx;
-  return REG_NOERROR;
+  Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
+  if (BE (dup_idx != REG_MISSING, 1))
+    {
+      dfa->nodes[dup_idx].constraint = constraint;
+      if (dfa->nodes[org_idx].type == ANCHOR)
+       dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
+      dfa->nodes[dup_idx].duplicated = 1;
+
+      /* Store the index of the original node.  */
+      dfa->org_indices[dup_idx] = org_idx;
+    }
+  return dup_idx;
 }
 
 static reg_errcode_t
-calc_inveclosure (dfa)
-     re_dfa_t *dfa;
+calc_inveclosure (re_dfa_t *dfa)
 {
-  int src, idx, ret;
+  Idx src, idx;
+  bool ok;
   for (idx = 0; idx < dfa->nodes_len; ++idx)
     re_node_set_init_empty (dfa->inveclosures + idx);
 
   for (src = 0; src < dfa->nodes_len; ++src)
     {
-      int *elems = dfa->eclosures[src].elems;
+      Idx *elems = dfa->eclosures[src].elems;
       for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
        {
-         ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
-         if (BE (ret == -1, 0))
+         ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
     }
@@ -1640,14 +1567,14 @@
 /* Calculate "eclosure" for all the node in DFA.  */
 
 static reg_errcode_t
-calc_eclosure (dfa)
-     re_dfa_t *dfa;
+calc_eclosure (re_dfa_t *dfa)
 {
-  int node_idx, incomplete;
+  Idx node_idx;
+  bool incomplete;
 #ifdef DEBUG
   assert (dfa->nodes_len > 0);
 #endif
-  incomplete = 0;
+  incomplete = false;
   /* For each nodes, calculate epsilon closure.  */
   for (node_idx = 0; ; ++node_idx)
     {
@@ -1657,25 +1584,25 @@
        {
          if (!incomplete)
            break;
-         incomplete = 0;
+         incomplete = false;
          node_idx = 0;
        }
 
 #ifdef DEBUG
-      assert (dfa->eclosures[node_idx].nelem != -1);
+      assert (dfa->eclosures[node_idx].nelem != REG_MISSING);
 #endif
 
       /* If we have already calculated, skip it.  */
       if (dfa->eclosures[node_idx].nelem != 0)
        continue;
       /* Calculate epsilon closure of `node_idx'.  */
-      err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
+      err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
       if (BE (err != REG_NOERROR, 0))
        return err;
 
       if (dfa->eclosures[node_idx].nelem == 0)
        {
-         incomplete = 1;
+         incomplete = true;
          re_node_set_free (&eclosure_elem);
        }
     }
@@ -1685,23 +1612,22 @@
 /* Calculate epsilon closure of NODE.  */
 
 static reg_errcode_t
-calc_eclosure_iter (new_set, dfa, node, root)
-     re_node_set *new_set;
-     re_dfa_t *dfa;
-     int node, root;
+calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
 {
   reg_errcode_t err;
   unsigned int constraint;
-  int i, incomplete;
+  Idx i;
+  bool incomplete;
+  bool ok;
   re_node_set eclosure;
-  incomplete = 0;
+  incomplete = false;
   err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
   if (BE (err != REG_NOERROR, 0))
     return err;
 
   /* This indicates that we are calculating this node now.
      We reference this value to avoid infinite loop.  */
-  dfa->eclosures[node].nelem = -1;
+  dfa->eclosures[node].nelem = REG_MISSING;
 
   constraint = ((dfa->nodes[node].type == ANCHOR)
                ? dfa->nodes[node].opr.ctx_type : 0);
@@ -1711,7 +1637,7 @@
       && dfa->edests[node].nelem
       && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
     {
-      int org_node, cur_node;
+      Idx org_node, cur_node;
       org_node = cur_node = node;
       err = duplicate_node_closure (dfa, node, node, node, constraint);
       if (BE (err != REG_NOERROR, 0))
@@ -1723,19 +1649,19 @@
     for (i = 0; i < dfa->edests[node].nelem; ++i)
       {
        re_node_set eclosure_elem;
-       int edest = dfa->edests[node].elems[i];
+       Idx edest = dfa->edests[node].elems[i];
        /* If calculating the epsilon closure of `edest' is in progress,
           return intermediate result.  */
-       if (dfa->eclosures[edest].nelem == -1)
+       if (dfa->eclosures[edest].nelem == REG_MISSING)
          {
-           incomplete = 1;
+           incomplete = true;
            continue;
          }
        /* If we haven't calculated the epsilon closure of `edest' yet,
           calculate now. Otherwise use calculated epsilon closure.  */
        if (dfa->eclosures[edest].nelem == 0)
          {
-           err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
+           err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
            if (BE (err != REG_NOERROR, 0))
              return err;
          }
@@ -1747,13 +1673,15 @@
           the epsilon closure of this node is also incomplete.  */
        if (dfa->eclosures[edest].nelem == 0)
          {
-           incomplete = 1;
+           incomplete = true;
            re_node_set_free (&eclosure_elem);
          }
       }
 
   /* Epsilon closures include itself.  */
-  re_node_set_insert (&eclosure, node);
+  ok = re_node_set_insert (&eclosure, node);
+  if (BE (! ok, 0))
+    return REG_ESPACE;
   if (incomplete && !root)
     dfa->eclosures[node].nelem = 0;
   else
@@ -1768,10 +1696,7 @@
    We must not use this function inside bracket expressions.  */
 
 static void
-fetch_token (result, input, syntax)
-     re_token_t *result;
-     re_string_t *input;
-     reg_syntax_t syntax;
+fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
 {
   re_string_skip_bytes (input, peek_token (result, input, syntax));
 }
@@ -1780,10 +1705,7 @@
    We must not use this function inside bracket expressions.  */
 
 static int
-peek_token (token, input, syntax)
-     re_token_t *token;
-     re_string_t *input;
-     reg_syntax_t syntax;
+peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
 {
   unsigned char c;
 
@@ -1833,97 +1755,97 @@
       switch (c2)
        {
        case '|':
-         if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
+         if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_NO_BK_VBAR))
            token->type = OP_ALT;
          break;
        case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
-         if (!(syntax & RE_NO_BK_REFS))
+         if (!(syntax & REG_NO_BK_REFS))
            {
              token->type = OP_BACK_REF;
              token->opr.idx = c2 - '1';
            }
          break;
        case '<':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_FIRST;
            }
          break;
        case '>':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_LAST;
            }
          break;
        case 'b':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_DELIM;
            }
          break;
        case 'B':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = NOT_WORD_DELIM;
            }
          break;
        case 'w':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_WORD;
          break;
        case 'W':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_NOTWORD;
          break;
        case 's':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_SPACE;
          break;
        case 'S':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_NOTSPACE;
          break;
        case '`':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = BUF_FIRST;
            }
          break;
        case '\'':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = BUF_LAST;
            }
          break;
        case '(':
-         if (!(syntax & RE_NO_BK_PARENS))
+         if (!(syntax & REG_NO_BK_PARENS))
            token->type = OP_OPEN_SUBEXP;
          break;
        case ')':
-         if (!(syntax & RE_NO_BK_PARENS))
+         if (!(syntax & REG_NO_BK_PARENS))
            token->type = OP_CLOSE_SUBEXP;
          break;
        case '+':
-         if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
+         if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
            token->type = OP_DUP_PLUS;
          break;
        case '?':
-         if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
+         if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
            token->type = OP_DUP_QUESTION;
          break;
        case '{':
-         if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
+         if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
            token->type = OP_OPEN_DUP_NUM;
          break;
        case '}':
-         if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
+         if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
            token->type = OP_CLOSE_DUP_NUM;
          break;
        default:
@@ -1946,38 +1868,38 @@
   switch (c)
     {
     case '\n':
-      if (syntax & RE_NEWLINE_ALT)
+      if (syntax & REG_NEWLINE_ALT)
        token->type = OP_ALT;
       break;
     case '|':
-      if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
+      if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_NO_BK_VBAR))
        token->type = OP_ALT;
       break;
     case '*':
       token->type = OP_DUP_ASTERISK;
       break;
     case '+':
-      if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
+      if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
        token->type = OP_DUP_PLUS;
       break;
     case '?':
-      if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
+      if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
        token->type = OP_DUP_QUESTION;
       break;
     case '{':
-      if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
+      if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
        token->type = OP_OPEN_DUP_NUM;
       break;
     case '}':
-      if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
+      if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
        token->type = OP_CLOSE_DUP_NUM;
       break;
     case '(':
-      if (syntax & RE_NO_BK_PARENS)
+      if (syntax & REG_NO_BK_PARENS)
        token->type = OP_OPEN_SUBEXP;
       break;
     case ')':
-      if (syntax & RE_NO_BK_PARENS)
+      if (syntax & REG_NO_BK_PARENS)
        token->type = OP_CLOSE_SUBEXP;
       break;
     case '[':
@@ -1987,18 +1909,18 @@
       token->type = OP_PERIOD;
       break;
     case '^':
-      if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
+      if (!(syntax & (REG_CONTEXT_INDEP_ANCHORS | REG_CARET_ANCHORS_HERE)) &&
          re_string_cur_idx (input) != 0)
        {
          char prev = re_string_peek_byte (input, -1);
-         if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
+         if (!(syntax & REG_NEWLINE_ALT) || prev != '\n')
            break;
        }
       token->type = ANCHOR;
       token->opr.ctx_type = LINE_FIRST;
       break;
     case '$':
-      if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
+      if (!(syntax & REG_CONTEXT_INDEP_ANCHORS) &&
          re_string_cur_idx (input) + 1 != re_string_length (input))
        {
          re_token_t next;
@@ -2021,10 +1943,7 @@
    We must not use this function out of bracket expressions.  */
 
 static int
-peek_token_bracket (token, input, syntax)
-     re_token_t *token;
-     re_string_t *input;
-     reg_syntax_t syntax;
+peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
 {
   unsigned char c;
   if (re_string_eoi (input))
@@ -2044,7 +1963,7 @@
     }
 #endif /* RE_ENABLE_I18N */
 
-  if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
+  if (c == '\\' && (syntax & REG_BACKSLASH_ESCAPE_IN_LISTS)
       && re_string_cur_idx (input) + 1 < re_string_length (input))
     {
       /* In this case, '\' escape a character.  */
@@ -2074,7 +1993,7 @@
          token->type = OP_OPEN_EQUIV_CLASS;
          break;
        case ':':
-         if (syntax & RE_CHAR_CLASSES)
+         if (syntax & REG_CHAR_CLASSES)
            {
              token->type = OP_OPEN_CHAR_CLASS;
              break;
@@ -2120,17 +2039,14 @@
    EOR means end of regular expression.  */
 
 static bin_tree_t *
-parse (regexp, preg, syntax, err)
-     re_string_t *regexp;
-     regex_t *preg;
-     reg_syntax_t syntax;
-     reg_errcode_t *err;
+parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
+       reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree, *eor, *root;
   re_token_t current_token;
   dfa->syntax = syntax;
-  fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
+  fetch_token (&current_token, regexp, syntax | REG_CARET_ANCHORS_HERE);
   tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     return NULL;
@@ -2157,15 +2073,10 @@
    ALT means alternative, which represents the operator `|'.  */
 
 static bin_tree_t *
-parse_reg_exp (regexp, preg, token, syntax, nest, err)
-     re_string_t *regexp;
-     regex_t *preg;
-     re_token_t *token;
-     reg_syntax_t syntax;
-     int nest;
-     reg_errcode_t *err;
+parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
+              reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree, *branch = NULL;
   tree = parse_branch (regexp, preg, token, syntax, nest, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
@@ -2173,7 +2084,7 @@
 
   while (token->type == OP_ALT)
     {
-      fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
+      fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
       if (token->type != OP_ALT && token->type != END_OF_RE
          && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
        {
@@ -2203,16 +2114,11 @@
    CAT means concatenation.  */
 
 static bin_tree_t *
-parse_branch (regexp, preg, token, syntax, nest, err)
-     re_string_t *regexp;
-     regex_t *preg;
-     re_token_t *token;
-     reg_syntax_t syntax;
-     int nest;
-     reg_errcode_t *err;
+parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
+             reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
   bin_tree_t *tree, *exp;
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   tree = parse_expression (regexp, preg, token, syntax, nest, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     return NULL;
@@ -2248,15 +2154,10 @@
 */
 
 static bin_tree_t *
-parse_expression (regexp, preg, token, syntax, nest, err)
-     re_string_t *regexp;
-     regex_t *preg;
-     re_token_t *token;
-     reg_syntax_t syntax;
-     int nest;
-     reg_errcode_t *err;
+parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
+                 reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree;
   switch (token->type)
     {
@@ -2313,7 +2214,7 @@
       dfa->has_mb_node = 1;
       break;
     case OP_OPEN_DUP_NUM:
-      if (syntax & RE_CONTEXT_INVALID_DUP)
+      if (syntax & REG_CONTEXT_INVALID_DUP)
        {
          *err = REG_BADRPT;
          return NULL;
@@ -2322,12 +2223,12 @@
     case OP_DUP_ASTERISK:
     case OP_DUP_PLUS:
     case OP_DUP_QUESTION:
-      if (syntax & RE_CONTEXT_INVALID_OPS)
+      if (syntax & REG_CONTEXT_INVALID_OPS)
        {
          *err = REG_BADRPT;
          return NULL;
        }
-      else if (syntax & RE_CONTEXT_INDEP_OPS)
+      else if (syntax & REG_CONTEXT_INDEP_OPS)
        {
          fetch_token (token, regexp, syntax);
          return parse_expression (regexp, preg, token, syntax, nest, err);
@@ -2335,7 +2236,7 @@
       /* else fall through  */
     case OP_CLOSE_SUBEXP:
       if ((token->type == OP_CLOSE_SUBEXP) &&
-         !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
+         !(syntax & REG_UNMATCHED_RIGHT_PAREN_ORD))
        {
          *err = REG_ERPAREN;
          return NULL;
@@ -2449,7 +2350,7 @@
       if (BE (*err != REG_NOERROR && tree == NULL, 0))
        return NULL;
       /* In BRE consecutive duplications are not allowed.  */
-      if ((syntax & RE_CONTEXT_INVALID_DUP)
+      if ((syntax & REG_CONTEXT_INVALID_DUP)
          && (token->type == OP_DUP_ASTERISK
              || token->type == OP_OPEN_DUP_NUM))
        {
@@ -2469,20 +2370,15 @@
 */
 
 static bin_tree_t *
-parse_sub_exp (regexp, preg, token, syntax, nest, err)
-     re_string_t *regexp;
-     regex_t *preg;
-     re_token_t *token;
-     reg_syntax_t syntax;
-     int nest;
-     reg_errcode_t *err;
+parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
+              reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree;
   size_t cur_nsub;
   cur_nsub = preg->re_nsub++;
 
-  fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
+  fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
 
   /* The subexpression may be a null string.  */
   if (token->type == OP_CLOSE_SUBEXP)
@@ -2495,7 +2391,9 @@
       if (BE (*err != REG_NOERROR, 0))
        return NULL;
     }
-  dfa->completed_bkref_map |= 1 << cur_nsub;
+
+  if (cur_nsub <= '9' - '1')
+    dfa->completed_bkref_map |= 1 << cur_nsub;
 
   tree = create_tree (dfa, tree, NULL, SUBEXP);
   if (BE (tree == NULL, 0))
@@ -2510,23 +2408,18 @@
 /* This function parse repetition operators like "*", "+", "{1,3}" etc.  */
 
 static bin_tree_t *
-parse_dup_op (elem, regexp, dfa, token, syntax, err)
-     bin_tree_t *elem;
-     re_string_t *regexp;
-     re_dfa_t *dfa;
-     re_token_t *token;
-     reg_syntax_t syntax;
-     reg_errcode_t *err;
+parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
+             re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
 {
   bin_tree_t *tree = NULL, *old_tree = NULL;
-  int i, start, end, start_idx = re_string_cur_idx (regexp);
+  Idx i, start, end, start_idx = re_string_cur_idx (regexp);
   re_token_t start_token = *token;
 
   if (token->type == OP_OPEN_DUP_NUM)
     {
       end = 0;
       start = fetch_number (regexp, token, syntax);
-      if (start == -1)
+      if (start == REG_MISSING)
        {
          if (token->type == CHARACTER && token->opr.c == ',')
            start = 0; /* We treat "{,m}" as "{0,m}".  */
@@ -2536,17 +2429,17 @@
              return NULL;
            }
        }
-      if (BE (start != -2, 1))
+      if (BE (start != REG_ERROR, 1))
        {
          /* We treat "{n}" as "{n,n}".  */
          end = ((token->type == OP_CLOSE_DUP_NUM) ? start
                 : ((token->type == CHARACTER && token->opr.c == ',')
-                   ? fetch_number (regexp, token, syntax) : -2));
+                   ? fetch_number (regexp, token, syntax) : REG_ERROR));
        }
-      if (BE (start == -2 || end == -2, 0))
+      if (BE (start == REG_ERROR || end == REG_ERROR, 0))
        {
          /* Invalid sequence.  */
-         if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
+         if (BE (!(syntax & REG_INVALID_INTERVAL_ORD), 0))
            {
              if (token->type == END_OF_RE)
                *err = REG_EBRACE;
@@ -2565,7 +2458,7 @@
          return elem;
        }
 
-      if (BE (end != -1 && start > end, 0))
+      if (BE (end != REG_MISSING && start > end, 0))
        {
          /* First number greater than second.  */
          *err = REG_BADBR;
@@ -2575,7 +2468,7 @@
   else
     {
       start = (token->type == OP_DUP_PLUS) ? 1 : 0;
-      end = (token->type == OP_DUP_QUESTION) ? 1 : -1;
+      end = (token->type == OP_DUP_QUESTION) ? 1 : REG_MISSING;
     }
 
   fetch_token (token, regexp, syntax);
@@ -2613,24 +2506,26 @@
   if (elem->token.type == SUBEXP)
     postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
 
-  tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
+  tree = create_tree (dfa, elem, NULL,
+                     (end == REG_MISSING ? OP_DUP_ASTERISK : OP_ALT));
   if (BE (tree == NULL, 0))
     goto parse_dup_op_espace;
 
-  /* This loop is actually executed only when end != -1,
+  /* This loop is actually executed only when end != REG_MISSING,
      to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?...  We have
      already created the start+1-th copy.  */
-  for (i = start + 2; i <= end; ++i)
-    {
-      elem = duplicate_tree (elem, dfa);
-      tree = create_tree (dfa, tree, elem, CONCAT);
-      if (BE (elem == NULL || tree == NULL, 0))
-        goto parse_dup_op_espace;
-
-      tree = create_tree (dfa, tree, NULL, OP_ALT);
-      if (BE (tree == NULL, 0))
-        goto parse_dup_op_espace;
-    }
+  if ((Idx) -1 < 0 || end != REG_MISSING)
+    for (i = start + 2; i <= end; ++i)
+      {
+       elem = duplicate_tree (elem, dfa);
+       tree = create_tree (dfa, tree, elem, CONCAT);
+       if (BE (elem == NULL || tree == NULL, 0))
+         goto parse_dup_op_espace;
+
+       tree = create_tree (dfa, tree, NULL, OP_ALT);
+       if (BE (tree == NULL, 0))
+         goto parse_dup_op_espace;
+      }
 
   if (old_tree)
     tree = create_tree (dfa, old_tree, tree, CONCAT);
@@ -2655,15 +2550,11 @@
      update it.  */
 
 static reg_errcode_t
+build_range_exp (re_bitset_ptr_t sbcset,
 # ifdef RE_ENABLE_I18N
-build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
-     re_charset_t *mbcset;
-     int *range_alloc;
-# else /* not RE_ENABLE_I18N */
-build_range_exp (sbcset, start_elem, end_elem)
-# endif /* not RE_ENABLE_I18N */
-     re_bitset_ptr_t sbcset;
-     bracket_elem_t *start_elem, *end_elem;
+                re_charset_t *mbcset, Idx *range_alloc,
+# endif
+                bracket_elem_t *start_elem, bracket_elem_t *end_elem)
 {
   unsigned int start_ch, end_ch;
   /* Equivalence Classes and Character Classes can't be a range start/end.  */
@@ -2715,14 +2606,13 @@
           {
            /* There is not enough space, need realloc.  */
            wchar_t *new_array_start, *new_array_end;
-           int new_nranges;
+           Idx new_nranges;
 
-           /* +1 in case of mbcset->nranges is 0.  */
-           new_nranges = 2 * mbcset->nranges + 1;
+           new_nranges = mbcset->nranges;
            /* Use realloc since mbcset->range_starts and mbcset->range_ends
               are NULL if *range_alloc == 0.  */
-           new_array_start = re_realloc (mbcset->range_starts, wchar_t,
-                                         new_nranges);
+           new_array_start = re_x2realloc (mbcset->range_starts, wchar_t,
+                                           &new_nranges);
            new_array_end = re_realloc (mbcset->range_ends, wchar_t,
                                        new_nranges);
 
@@ -2776,15 +2666,11 @@
    pointer argument since we may update it.  */
 
 static reg_errcode_t
+build_collating_symbol (re_bitset_ptr_t sbcset,
 # ifdef RE_ENABLE_I18N
-build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
-     re_charset_t *mbcset;
-     int *coll_sym_alloc;
-# else /* not RE_ENABLE_I18N */
-build_collating_symbol (sbcset, name)
-# endif /* not RE_ENABLE_I18N */
-     re_bitset_ptr_t sbcset;
-     const unsigned char *name;
+                       re_charset_t *mbcset, Idx *coll_sym_alloc,
+# endif
+                       const unsigned char *name)
 {
   size_t name_len = strlen ((const char *) name);
   if (BE (name_len != 1, 0))
@@ -2801,12 +2687,8 @@
    "[[.a-a.]]" etc.  */
 
 static bin_tree_t *
-parse_bracket_exp (regexp, dfa, token, syntax, err)
-     re_string_t *regexp;
-     re_dfa_t *dfa;
-     re_token_t *token;
-     reg_syntax_t syntax;
-     reg_errcode_t *err;
+parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
+                  reg_syntax_t syntax, reg_errcode_t *err)
 {
 #ifdef _LIBC
   const unsigned char *collseqmb;
@@ -2822,9 +2704,7 @@
 
   auto inline int32_t
   __attribute ((always_inline))
-  seek_collating_symbol_entry (name, name_len)
-        const unsigned char *name;
-        size_t name_len;
+  seek_collating_symbol_entry (const unsigned char *name, size_t name_len)
     {
       int32_t hash = elem_hash ((const char *) name, name_len);
       int32_t elem = hash % table_size;
@@ -2855,8 +2735,7 @@
 
   auto inline unsigned int
   __attribute ((always_inline))
-  lookup_collation_sequence_value (br_elem)
-        bracket_elem_t *br_elem;
+  lookup_collation_sequence_value (bracket_elem_t *br_elem)
     {
       if (br_elem->type == SB_CHAR)
        {
@@ -2923,11 +2802,9 @@
 
   auto inline reg_errcode_t
   __attribute ((always_inline))
-  build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
-        re_charset_t *mbcset;
-        int *range_alloc;
-        re_bitset_ptr_t sbcset;
-        bracket_elem_t *start_elem, *end_elem;
+  build_range_exp (re_bitset_ptr_t sbcset, re_charset_t *mbcset,
+                  Idx *range_alloc,
+                  bracket_elem_t *start_elem, bracket_elem_t *end_elem)
     {
       unsigned int ch;
       uint32_t start_collseq;
@@ -2945,7 +2822,7 @@
       /* Check start/end collation sequence values.  */
       if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
        return REG_ECOLLATE;
-      if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
+      if (BE ((syntax & REG_NO_EMPTY_RANGES) && start_collseq > end_collseq, 
0))
        return REG_ERANGE;
 
       /* Got valid collation sequence values, add them as a new entry.
@@ -2960,12 +2837,11 @@
              /* There is not enough space, need realloc.  */
              uint32_t *new_array_start;
              uint32_t *new_array_end;
-             int new_nranges;
+             Idx new_nranges;
 
-             /* +1 in case of mbcset->nranges is 0.  */
-             new_nranges = 2 * mbcset->nranges + 1;
-             new_array_start = re_realloc (mbcset->range_starts, uint32_t,
-                                           new_nranges);
+             new_nranges = mbcset->nranges;
+             new_array_start = re_x2realloc (mbcset->range_starts, uint32_t,
+                                             &new_nranges);
              new_array_end = re_realloc (mbcset->range_ends, uint32_t,
                                          new_nranges);
 
@@ -3006,11 +2882,8 @@
 
   auto inline reg_errcode_t
   __attribute ((always_inline))
-  build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
-        re_charset_t *mbcset;
-        int *coll_sym_alloc;
-        re_bitset_ptr_t sbcset;
-        const unsigned char *name;
+  build_collating_symbol (re_bitset_ptr_t sbcset, re_charset_t *mbcset,
+                         Idx *coll_sym_alloc, const unsigned char *name)
     {
       int32_t elem, idx;
       size_t name_len = strlen ((const char *) name);
@@ -3039,12 +2912,11 @@
          if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
            {
              /* Not enough, realloc it.  */
-             /* +1 in case of mbcset->ncoll_syms is 0.  */
-             int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
+             Idx new_coll_sym_alloc = mbcset->ncoll_syms;
              /* Use realloc since mbcset->coll_syms is NULL
                 if *alloc == 0.  */
-             int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
-                                                  new_coll_sym_alloc);
+             int32_t *new_coll_syms = re_x2realloc (mbcset->coll_syms, int32_t,
+                                                    &new_coll_sym_alloc);
              if (BE (new_coll_syms == NULL, 0))
                return REG_ESPACE;
              mbcset->coll_syms = new_coll_syms;
@@ -3070,13 +2942,13 @@
   re_bitset_ptr_t sbcset;
 #ifdef RE_ENABLE_I18N
   re_charset_t *mbcset;
-  int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
-  int equiv_class_alloc = 0, char_class_alloc = 0;
+  Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
+  Idx equiv_class_alloc = 0, char_class_alloc = 0;
 #endif /* not RE_ENABLE_I18N */
-  int non_match = 0;
+  bool non_match = false;
   bin_tree_t *work_tree;
   int token_len;
-  int first_round = 1;
+  bool first_round = true;
 #ifdef _LIBC
   collseqmb = (const unsigned char *)
     _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
@@ -3094,9 +2966,9 @@
                                                   _NL_COLLATE_SYMB_EXTRAMB);
     }
 #endif
-  sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
+  sbcset = re_calloc (unsigned int, BITSET_UINTS);
 #ifdef RE_ENABLE_I18N
-  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
+  mbcset = re_calloc (re_charset_t, 1);
 #endif /* RE_ENABLE_I18N */
 #ifdef RE_ENABLE_I18N
   if (BE (sbcset == NULL || mbcset == NULL, 0))
@@ -3119,8 +2991,8 @@
 #ifdef RE_ENABLE_I18N
       mbcset->non_match = 1;
 #endif /* not RE_ENABLE_I18N */
-      non_match = 1;
-      if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
+      non_match = true;
+      if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
        bitset_set (sbcset, '\0');
       re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
       token_len = peek_token_bracket (token, regexp, syntax);
@@ -3141,7 +3013,8 @@
       unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
       unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
       reg_errcode_t ret;
-      int token_len2 = 0, is_range_exp = 0;
+      int token_len2 = 0;
+      bool is_range_exp = false;
       re_token_t token2;
 
       start_elem.opr.name = start_name_buf;
@@ -3152,7 +3025,7 @@
          *err = ret;
          goto parse_bracket_exp_free_return;
        }
-      first_round = 0;
+      first_round = false;
 
       /* Get information about the next token.  We need it in any case.  */
       token_len = peek_token_bracket (token, regexp, syntax);
@@ -3181,15 +3054,15 @@
                  token->type = CHARACTER;
                }
              else
-               is_range_exp = 1;
+               is_range_exp = true;
            }
        }
 
-      if (is_range_exp == 1)
+      if (is_range_exp == true)
        {
          end_elem.opr.name = end_name_buf;
          ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
-                                      dfa, syntax, 1);
+                                      dfa, syntax, true);
          if (BE (ret != REG_NOERROR, 0))
            {
              *err = ret;
@@ -3227,11 +3100,10 @@
                {
                  wchar_t *new_mbchars;
                  /* Not enough, realloc it.  */
-                 /* +1 in case of mbcset->nmbchars is 0.  */
-                 mbchar_alloc = 2 * mbcset->nmbchars + 1;
+                 mbchar_alloc = mbcset->nmbchars;
                  /* Use realloc since array is NULL if *alloc == 0.  */
-                 new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
-                                           mbchar_alloc);
+                 new_mbchars = re_x2realloc (mbcset->mbchars, wchar_t,
+                                             &mbchar_alloc);
                  if (BE (new_mbchars == NULL, 0))
                    goto parse_bracket_exp_espace;
                  mbcset->mbchars = new_mbchars;
@@ -3357,15 +3229,9 @@
 /* Parse an element in the bracket expression.  */
 
 static reg_errcode_t
-parse_bracket_element (elem, regexp, token, token_len, dfa, syntax,
-                      accept_hyphen)
-     bracket_elem_t *elem;
-     re_string_t *regexp;
-     re_token_t *token;
-     int token_len;
-     re_dfa_t *dfa;
-     reg_syntax_t syntax;
-     int accept_hyphen;
+parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
+                      re_token_t *token, int token_len, re_dfa_t *dfa,
+                      reg_syntax_t syntax, bool accept_hyphen)
 {
 #ifdef RE_ENABLE_I18N
   int cur_char_size;
@@ -3403,10 +3269,8 @@
    [=<equivalent_class>=].  */
 
 static reg_errcode_t
-parse_bracket_symbol (elem, regexp, token)
-     bracket_elem_t *elem;
-     re_string_t *regexp;
-     re_token_t *token;
+parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
+                     re_token_t *token)
 {
   unsigned char ch, delim = token->opr.c;
   int i = 0;
@@ -3452,15 +3316,11 @@
      is a pointer argument sinse we may update it.  */
 
 static reg_errcode_t
+build_equiv_class (re_bitset_ptr_t sbcset,
 #ifdef RE_ENABLE_I18N
-build_equiv_class (sbcset, mbcset, equiv_class_alloc, name)
-     re_charset_t *mbcset;
-     int *equiv_class_alloc;
-#else /* not RE_ENABLE_I18N */
-build_equiv_class (sbcset, name)
-#endif /* not RE_ENABLE_I18N */
-     re_bitset_ptr_t sbcset;
-     const unsigned char *name;
+                  re_charset_t *mbcset, Idx *equiv_class_alloc,
+#endif
+                  const unsigned char *name)
 {
 #if defined _LIBC
   uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
@@ -3517,12 +3377,11 @@
       if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
        {
          /* Not enough, realloc it.  */
-         /* +1 in case of mbcset->nequiv_classes is 0.  */
-         int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
+         Idx new_equiv_class_alloc = mbcset->nequiv_classes;
          /* Use realloc since the array is NULL if *alloc == 0.  */
-         int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
-                                                  int32_t,
-                                                  new_equiv_class_alloc);
+         int32_t *new_equiv_classes = re_x2realloc (mbcset->equiv_classes,
+                                                    int32_t,
+                                                    &new_equiv_class_alloc);
          if (BE (new_equiv_classes == NULL, 0))
            return REG_ESPACE;
          mbcset->equiv_classes = new_equiv_classes;
@@ -3547,24 +3406,18 @@
      is a pointer argument sinse we may update it.  */
 
 static reg_errcode_t
+build_charclass (unsigned REG_TRANSLATE_TYPE trans, re_bitset_ptr_t sbcset,
 #ifdef RE_ENABLE_I18N
-build_charclass (trans, sbcset, mbcset, char_class_alloc, class_name, syntax)
-     re_charset_t *mbcset;
-     int *char_class_alloc;
-#else /* not RE_ENABLE_I18N */
-build_charclass (trans, sbcset, class_name, syntax)
-#endif /* not RE_ENABLE_I18N */
-     unsigned RE_TRANSLATE_TYPE trans;
-     re_bitset_ptr_t sbcset;
-     const unsigned char *class_name;
-     reg_syntax_t syntax;
+                re_charset_t *mbcset, Idx *char_class_alloc,
+#endif
+                const unsigned char *class_name, reg_syntax_t syntax)
 {
   int i;
   const char *name = (const char *) class_name;
 
   /* In case of REG_ICASE "upper" and "lower" match the both of
      upper and lower cases.  */
-  if ((syntax & RE_ICASE)
+  if ((syntax & REG_IGNORE_CASE)
       && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
     name = "alpha";
 
@@ -3573,11 +3426,10 @@
   if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
     {
       /* Not enough, realloc it.  */
-      /* +1 in case of mbcset->nchar_classes is 0.  */
-      int new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
+      Idx new_char_class_alloc = mbcset->nchar_classes;
       /* Use realloc since array is NULL if *alloc == 0.  */
-      wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
-                                              new_char_class_alloc);
+      wctype_t *new_char_classes = re_x2realloc (mbcset->char_classes, 
wctype_t,
+                                                &new_char_class_alloc);
       if (BE (new_char_classes == NULL, 0))
        return REG_ESPACE;
       mbcset->char_classes = new_char_classes;
@@ -3627,26 +3479,23 @@
 }
 
 static bin_tree_t *
-build_charclass_op (dfa, trans, class_name, extra, non_match, err)
-     re_dfa_t *dfa;
-     unsigned RE_TRANSLATE_TYPE trans;
-     const unsigned char *class_name;
-     const unsigned char *extra;
-     int non_match;
-     reg_errcode_t *err;
+build_charclass_op (re_dfa_t *dfa, unsigned REG_TRANSLATE_TYPE trans,
+                   const unsigned char *class_name,
+                   const unsigned char *extra,
+                   bool non_match, reg_errcode_t *err)
 {
   re_bitset_ptr_t sbcset;
 #ifdef RE_ENABLE_I18N
   re_charset_t *mbcset;
-  int alloc = 0;
+  Idx alloc = 0;
 #endif /* not RE_ENABLE_I18N */
   reg_errcode_t ret;
   re_token_t br_token;
   bin_tree_t *tree;
 
-  sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
+  sbcset = re_calloc (unsigned int, BITSET_UINTS);
 #ifdef RE_ENABLE_I18N
-  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
+  mbcset = re_calloc (re_charset_t, 1);
 #endif /* RE_ENABLE_I18N */
 
 #ifdef RE_ENABLE_I18N
@@ -3663,7 +3512,7 @@
     {
 #ifdef RE_ENABLE_I18N
       /*
-      if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
+      if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
        bitset_set(cset->sbcset, '\0');
       */
       mbcset->non_match = 1;
@@ -3743,28 +3592,27 @@
 
 /* This is intended for the expressions like "a{1,3}".
    Fetch a number from `input', and return the number.
-   Return -1, if the number field is empty like "{,1}".
-   Return -2, If an error is occured.  */
+   Return REG_MISSING if the number field is empty like "{,1}".
+   Return REG_ERROR if an error occurred.  */
 
-static int
-fetch_number (input, token, syntax)
-     re_string_t *input;
-     re_token_t *token;
-     reg_syntax_t syntax;
+static Idx
+fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
 {
-  int num = -1;
+  Idx num = REG_MISSING;
   unsigned char c;
   while (1)
     {
       fetch_token (token, input, syntax);
       c = token->opr.c;
       if (BE (token->type == END_OF_RE, 0))
-       return -2;
+       return REG_ERROR;
       if (token->type == OP_CLOSE_DUP_NUM || c == ',')
        break;
-      num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2)
-            ? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0'));
-      num = (num > RE_DUP_MAX) ? -2 : num;
+      num = ((token->type != CHARACTER || c < '0' || '9' < c
+             || num == REG_ERROR)
+            ? REG_ERROR
+            : ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
+      num = (num > REG_DUP_MAX) ? REG_ERROR : num;
     }
   return num;
 }
@@ -3790,11 +3638,8 @@
 /* Create a tree node.  */
 
 static bin_tree_t *
-create_tree (dfa, left, right, type)
-     re_dfa_t *dfa;
-     bin_tree_t *left;
-     bin_tree_t *right;
-     re_token_type_t type;
+create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
+            re_token_type_t type)
 {
   re_token_t t;
   t.type = type;
@@ -3802,11 +3647,8 @@
 }
 
 static bin_tree_t *
-create_token_tree (dfa, left, right, token)
-     re_dfa_t *dfa;
-     bin_tree_t *left;
-     bin_tree_t *right;
-     const re_token_t *token;
+create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
+                  const re_token_t *token)
 {
   bin_tree_t *tree;
   if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
@@ -3829,7 +3671,7 @@
   tree->token.opt_subexp = 0;
   tree->first = NULL;
   tree->next = NULL;
-  tree->node_idx = -1;
+  tree->node_idx = REG_MISSING;
 
   if (left != NULL)
     left->parent = tree;
@@ -3842,11 +3684,9 @@
    To be called from preorder or postorder.  */
 
 static reg_errcode_t
-mark_opt_subexp (extra, node)
-     void *extra;
-     bin_tree_t *node;
+mark_opt_subexp (void *extra, bin_tree_t *node)
 {
-  int idx = (int) (long) extra;
+  Idx idx = (Idx) (long) extra;
   if (node->token.type == SUBEXP && node->token.opr.idx == idx)
     node->token.opt_subexp = 1;
 
@@ -3884,9 +3724,7 @@
    it's easier to duplicate.  */
 
 static bin_tree_t *
-duplicate_tree (root, dfa)
-     const bin_tree_t *root;
-     re_dfa_t *dfa;
+duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
 {
   const bin_tree_t *node;
   bin_tree_t *dup_root;




reply via email to

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