current = NEW_STEP (fromset_expand, NULL, NULL); current->next = NEW_STEP (fromset, NULL, NULL); lastp = &current->next->next;
first = NEW_STEP (fromset_expand, NULL, NULL); first->next = NEW_STEP (fromset, NULL, NULL); lastp = &first->next->next;
find_derivation (const char *toset, const char *toset_expand,		 const char *fromset, const char *fromset_expand,		 struct gconv_step **handle, size_t *nsteps){  __libc_lock_define_initialized (static, lock)  struct derivation_step *current, **lastp, *best = NULL;  int best_cost = 0;  int result;  result = derivation_lookup (fromset_expand ?: fromset, toset_expand ?: toset,			      handle, nsteps);  if (result == GCONV_OK)    return result;  __libc_lock_lock (lock);  /* There is a small chance that this derivation is meanwhile found.  This     can happen if in `find_derivation' we look for this derivation, didn't     find it but at the same time another thread looked for this derivation. */  result = derivation_lookup (fromset_expand ?: fromset, toset_expand ?: toset,			      handle, nsteps);  if (result == GCONV_OK)    return result;  /* ### TODO     For now we use a simple algorithm with quadratic runtime behaviour.     The task is to match the `toset' with any of the available.  */  if (fromset_expand != NULL)    {      current = NEW_STEP (fromset_expand, NULL, NULL);      current->next = NEW_STEP (fromset, NULL, NULL);      lastp = &current->next->next;    }  else    {      current = NEW_STEP (fromset, NULL, NULL);      lastp = &current->next;    }  while (current != NULL)    {      /* Now match all the available module specifications against the         current charset name.  If any of them matches check whether         we already have a derivation for this charset.  If yes, use the         one with the lower costs.  Otherwise add the new charset at the         end.  */      size_t cnt;      for (cnt = 0; cnt < __gconv_nmodules; ++cnt)        {	  const char *result_set = NULL;	  if (__gconv_modules_db[cnt]->from_pattern == NULL)	    {	      if (__strcasecmp (current->result_set,				__gconv_modules_db[cnt]->from_constpfx) == 0)		{		  if (strcmp (__gconv_modules_db[cnt]->to_string, "-") == 0)		    result_set = toset_expand ?: toset;		  else		    result_set = __gconv_modules_db[cnt]->to_string;		}	    }	  else	    /* We have a regular expression.  First see if the prefix	       matches.  */	    if (__strncasecmp (current->result_set,			       __gconv_modules_db[cnt]->from_constpfx,			       __gconv_modules_db[cnt]->from_constpfx_len)		== 0)	      {		/* First compile the regex if not already done.  */		if (__gconv_modules_db[cnt]->from_regex == NULL)		  {		    regex_t *newp = (regex_t *) malloc (sizeof (regex_t));		    if (regcomp (newp, __gconv_modules_db[cnt]->from_pattern,				 REG_EXTENDED | REG_ICASE) != 0)		      {			/* Something is wrong.  Remember this.  */			free (newp);			__gconv_modules_db[cnt]->from_regex = (regex_t *) -1L;		      }		    else		      __gconv_modules_db[cnt]->from_regex = newp;		  }		if (__gconv_modules_db[cnt]->from_regex != (regex_t *) -1L)		  {		    /* Try to match the from name.  */		    regmatch_t match[4];		    if (regexec (__gconv_modules_db[cnt]->from_regex,				 current->result_set, 4, match, 0) == 0			&& match[0].rm_so == 0			&& current->result_set[match[0].rm_eo] == '\0')		      {			/* At least the whole <from> string is matched.			   We must now match sed-like possible			   subexpressions from the match to the			   toset expression.  */#define ENSURE_LEN(LEN) \  if (wp + (LEN) >= constr + len - 1)					      \    {									      \      char *newp = alloca (len += 128);					      \      memcpy (newp, constr, wp - constr);				      \      wp = newp + (wp - constr);					      \      constr = newp;							      \    }			size_t len = 128;			char *constr = alloca (len);			char *wp = constr;			const char *cp = __gconv_modules_db[cnt]->to_string;			while (*cp != '\0')			  {			    if (*cp != '\\')			      {				ENSURE_LEN (1);				*wp++ = *cp++;			      }			    else if (cp[1] == '\0')			      /* Backslash at end of string.  */			      break;			    else			      {				++cp;				if (*cp == '\\')				  {				    *wp++ = *cp++;				    ENSURE_LEN (1);				  }				else if (*cp < '1' || *cp > '3')				  break;				else				  {				    int idx = *cp - '0';				    if (match[idx].rm_so == -1)				      /* No match.  */				      break;				    ENSURE_LEN (match[idx].rm_eo						- match[idx].rm_so);				    wp = __mempcpy (wp,						    &current->result_set[match[idx].rm_so],						    match[idx].rm_eo						    - match[idx].rm_so);				    ++cp;				  }			      }			  }			if (*cp == '\0' && wp != constr)			  {                                /* Terminate the constructed string.  */			    *wp = '\0';			    result_set = constr;			  }		      }		  }	      }	  if (result_set != NULL)	    {	      /* We managed to find a derivation.  First see whether		 this is what we are looking for.  */	      if (__strcasecmp (result_set, toset) == 0		  || (toset_expand != NULL		      && __strcasecmp (result_set, toset_expand) == 0))		{		  /* Determine the costs.  If they are lower than the		     previous solution (or this is the first solution)		     remember this solution.  */		  int cost = __gconv_modules_db[cnt]->cost;		  struct derivation_step *runp = current;		  while (runp->code != NULL)		    {		      cost += runp->code->cost;		      runp = runp->last;		    }		  if (best == NULL || cost < best_cost)		    {		      best = NEW_STEP (result_set, __gconv_modules_db[cnt],				       current);		      best_cost = cost;		    }		}	      else		{		  /* Append at the end.  */		  *lastp = NEW_STEP (result_set, __gconv_modules_db[cnt],				     current);		  lastp = &(*lastp)->next;                }	    }	}    }  if (best != NULL)    /* We really found a way to do the transformation.  Now build a data       structure describing the transformation steps.*/    result = gen_steps (best, toset_expand ?: toset, fromset_expand ?: fromset,			handle, nsteps);  else    {      /* We haven't found a transformation.  Clear the result values.  */      *handle = NULL;      *nsteps = 0;    }  /* Add result in any case to list of known derivations.  */  add_derivation (fromset_expand ?: fromset, toset_expand ?: toset,		  *handle, *nsteps);  __libc_lock_unlock (lock);  return result;}