OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [readline/] [tilde.c] - Diff between revs 834 and 842

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 834 Rev 842
/* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
/* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
 
 
/* Copyright (C) 1988,1989 Free Software Foundation, Inc.
/* Copyright (C) 1988,1989 Free Software Foundation, Inc.
 
 
   This file is part of GNU Readline, a library for reading lines
   This file is part of GNU Readline, a library for reading lines
   of text with interactive input and history editing.
   of text with interactive input and history editing.
 
 
   Readline is free software; you can redistribute it and/or modify it
   Readline is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   Free Software Foundation; either version 2, or (at your option) any
   later version.
   later version.
 
 
   Readline is distributed in the hope that it will be useful, but
   Readline is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
   General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with Readline; see the file COPYING.  If not, write to the Free
   along with Readline; see the file COPYING.  If not, write to the Free
   Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
   Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 
#if defined (HAVE_CONFIG_H)
#if defined (HAVE_CONFIG_H)
#  include <config.h>
#  include <config.h>
#endif
#endif
 
 
#if defined (HAVE_UNISTD_H)
#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#  ifdef _MINIX
#    include <sys/types.h>
#    include <sys/types.h>
#  endif
#  endif
#  include <unistd.h>
#  include <unistd.h>
#endif
#endif
 
 
#if defined (HAVE_STRING_H)
#if defined (HAVE_STRING_H)
#  include <string.h>
#  include <string.h>
#else /* !HAVE_STRING_H */
#else /* !HAVE_STRING_H */
#  include <strings.h>
#  include <strings.h>
#endif /* !HAVE_STRING_H */  
#endif /* !HAVE_STRING_H */  
 
 
#if defined (HAVE_STDLIB_H)
#if defined (HAVE_STDLIB_H)
#  include <stdlib.h>
#  include <stdlib.h>
#else
#else
#  include "ansi_stdlib.h"
#  include "ansi_stdlib.h"
#endif /* HAVE_STDLIB_H */
#endif /* HAVE_STDLIB_H */
 
 
#include <sys/types.h>
#include <sys/types.h>
#if defined (HAVE_PWD_H)
#if defined (HAVE_PWD_H)
#include <pwd.h>
#include <pwd.h>
#endif
#endif
 
 
#include "tilde.h"
#include "tilde.h"
 
 
#if defined (TEST) || defined (STATIC_MALLOC)
#if defined (TEST) || defined (STATIC_MALLOC)
static void *xmalloc (), *xrealloc ();
static void *xmalloc (), *xrealloc ();
#else
#else
#  include "xmalloc.h"
#  include "xmalloc.h"
#endif /* TEST || STATIC_MALLOC */
#endif /* TEST || STATIC_MALLOC */
 
 
#if !defined (HAVE_GETPW_DECLS)
#if !defined (HAVE_GETPW_DECLS)
#  if defined (HAVE_GETPWUID)
#  if defined (HAVE_GETPWUID)
extern struct passwd *getpwuid PARAMS((uid_t));
extern struct passwd *getpwuid PARAMS((uid_t));
#  endif
#  endif
#  if defined (HAVE_GETPWNAM)
#  if defined (HAVE_GETPWNAM)
extern struct passwd *getpwnam PARAMS((const char *));
extern struct passwd *getpwnam PARAMS((const char *));
#  endif
#  endif
#endif /* !HAVE_GETPW_DECLS */
#endif /* !HAVE_GETPW_DECLS */
 
 
#if !defined (savestring)
#if !defined (savestring)
#define savestring(x) strcpy ((char *)xmalloc (1 + strlen (x)), (x))
#define savestring(x) strcpy ((char *)xmalloc (1 + strlen (x)), (x))
#endif /* !savestring */
#endif /* !savestring */
 
 
#if !defined (NULL)
#if !defined (NULL)
#  if defined (__STDC__)
#  if defined (__STDC__)
#    define NULL ((void *) 0)
#    define NULL ((void *) 0)
#  else
#  else
#    define NULL 0x0
#    define NULL 0x0
#  endif /* !__STDC__ */
#  endif /* !__STDC__ */
#endif /* !NULL */
#endif /* !NULL */
 
 
/* If being compiled as part of bash, these will be satisfied from
/* If being compiled as part of bash, these will be satisfied from
   variables.o.  If being compiled as part of readline, they will
   variables.o.  If being compiled as part of readline, they will
   be satisfied from shell.o. */
   be satisfied from shell.o. */
extern char *sh_get_home_dir PARAMS((void));
extern char *sh_get_home_dir PARAMS((void));
extern char *sh_get_env_value PARAMS((const char *));
extern char *sh_get_env_value PARAMS((const char *));
 
 
/* The default value of tilde_additional_prefixes.  This is set to
/* The default value of tilde_additional_prefixes.  This is set to
   whitespace preceding a tilde so that simple programs which do not
   whitespace preceding a tilde so that simple programs which do not
   perform any word separation get desired behaviour. */
   perform any word separation get desired behaviour. */
static const char *default_prefixes[] =
static const char *default_prefixes[] =
  { " ~", "\t~", (const char *)NULL };
  { " ~", "\t~", (const char *)NULL };
 
 
/* The default value of tilde_additional_suffixes.  This is set to
/* The default value of tilde_additional_suffixes.  This is set to
   whitespace or newline so that simple programs which do not
   whitespace or newline so that simple programs which do not
   perform any word separation get desired behaviour. */
   perform any word separation get desired behaviour. */
static const char *default_suffixes[] =
static const char *default_suffixes[] =
  { " ", "\n", (const char *)NULL };
  { " ", "\n", (const char *)NULL };
 
 
/* If non-null, this contains the address of a function that the application
/* If non-null, this contains the address of a function that the application
   wants called before trying the standard tilde expansions.  The function
   wants called before trying the standard tilde expansions.  The function
   is called with the text sans tilde, and returns a malloc()'ed string
   is called with the text sans tilde, and returns a malloc()'ed string
   which is the expansion, or a NULL pointer if the expansion fails. */
   which is the expansion, or a NULL pointer if the expansion fails. */
tilde_hook_func_t *tilde_expansion_preexpansion_hook = (tilde_hook_func_t *)NULL;
tilde_hook_func_t *tilde_expansion_preexpansion_hook = (tilde_hook_func_t *)NULL;
 
 
/* If non-null, this contains the address of a function to call if the
/* If non-null, this contains the address of a function to call if the
   standard meaning for expanding a tilde fails.  The function is called
   standard meaning for expanding a tilde fails.  The function is called
   with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
   with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
   which is the expansion, or a NULL pointer if there is no expansion. */
   which is the expansion, or a NULL pointer if there is no expansion. */
tilde_hook_func_t *tilde_expansion_failure_hook = (tilde_hook_func_t *)NULL;
tilde_hook_func_t *tilde_expansion_failure_hook = (tilde_hook_func_t *)NULL;
 
 
/* When non-null, this is a NULL terminated array of strings which
/* When non-null, this is a NULL terminated array of strings which
   are duplicates for a tilde prefix.  Bash uses this to expand
   are duplicates for a tilde prefix.  Bash uses this to expand
   `=~' and `:~'. */
   `=~' and `:~'. */
char **tilde_additional_prefixes = (char **)default_prefixes;
char **tilde_additional_prefixes = (char **)default_prefixes;
 
 
/* When non-null, this is a NULL terminated array of strings which match
/* When non-null, this is a NULL terminated array of strings which match
   the end of a username, instead of just "/".  Bash sets this to
   the end of a username, instead of just "/".  Bash sets this to
   `:' and `=~'. */
   `:' and `=~'. */
char **tilde_additional_suffixes = (char **)default_suffixes;
char **tilde_additional_suffixes = (char **)default_suffixes;
 
 
static int tilde_find_prefix PARAMS((const char *, int *));
static int tilde_find_prefix PARAMS((const char *, int *));
static int tilde_find_suffix PARAMS((const char *));
static int tilde_find_suffix PARAMS((const char *));
static char *isolate_tilde_prefix PARAMS((const char *, int *));
static char *isolate_tilde_prefix PARAMS((const char *, int *));
static char *glue_prefix_and_suffix PARAMS((char *, const char *, int));
static char *glue_prefix_and_suffix PARAMS((char *, const char *, int));
 
 
/* Find the start of a tilde expansion in STRING, and return the index of
/* Find the start of a tilde expansion in STRING, and return the index of
   the tilde which starts the expansion.  Place the length of the text
   the tilde which starts the expansion.  Place the length of the text
   which identified this tilde starter in LEN, excluding the tilde itself. */
   which identified this tilde starter in LEN, excluding the tilde itself. */
static int
static int
tilde_find_prefix (string, len)
tilde_find_prefix (string, len)
     const char *string;
     const char *string;
     int *len;
     int *len;
{
{
  register int i, j, string_len;
  register int i, j, string_len;
  register char **prefixes;
  register char **prefixes;
 
 
  prefixes = tilde_additional_prefixes;
  prefixes = tilde_additional_prefixes;
 
 
  string_len = strlen (string);
  string_len = strlen (string);
  *len = 0;
  *len = 0;
 
 
  if (*string == '\0' || *string == '~')
  if (*string == '\0' || *string == '~')
    return (0);
    return (0);
 
 
  if (prefixes)
  if (prefixes)
    {
    {
      for (i = 0; i < string_len; i++)
      for (i = 0; i < string_len; i++)
        {
        {
          for (j = 0; prefixes[j]; j++)
          for (j = 0; prefixes[j]; j++)
            {
            {
              if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
              if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
                {
                {
                  *len = strlen (prefixes[j]) - 1;
                  *len = strlen (prefixes[j]) - 1;
                  return (i + *len);
                  return (i + *len);
                }
                }
            }
            }
        }
        }
    }
    }
  return (string_len);
  return (string_len);
}
}
 
 
/* Find the end of a tilde expansion in STRING, and return the index of
/* Find the end of a tilde expansion in STRING, and return the index of
   the character which ends the tilde definition.  */
   the character which ends the tilde definition.  */
static int
static int
tilde_find_suffix (string)
tilde_find_suffix (string)
     const char *string;
     const char *string;
{
{
  register int i, j, string_len;
  register int i, j, string_len;
  register char **suffixes;
  register char **suffixes;
 
 
  suffixes = tilde_additional_suffixes;
  suffixes = tilde_additional_suffixes;
  string_len = strlen (string);
  string_len = strlen (string);
 
 
  for (i = 0; i < string_len; i++)
  for (i = 0; i < string_len; i++)
    {
    {
#if defined (__MSDOS__)
#if defined (__MSDOS__)
      if (string[i] == '/' || string[i] == '\\' /* || !string[i] */)
      if (string[i] == '/' || string[i] == '\\' /* || !string[i] */)
#else
#else
      if (string[i] == '/' /* || !string[i] */)
      if (string[i] == '/' /* || !string[i] */)
#endif
#endif
        break;
        break;
 
 
      for (j = 0; suffixes && suffixes[j]; j++)
      for (j = 0; suffixes && suffixes[j]; j++)
        {
        {
          if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
          if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
            return (i);
            return (i);
        }
        }
    }
    }
  return (i);
  return (i);
}
}
 
 
/* Return a new string which is the result of tilde expanding STRING. */
/* Return a new string which is the result of tilde expanding STRING. */
char *
char *
tilde_expand (string)
tilde_expand (string)
     const char *string;
     const char *string;
{
{
  char *result;
  char *result;
  int result_size, result_index;
  int result_size, result_index;
 
 
  result_index = result_size = 0;
  result_index = result_size = 0;
  if (result = strchr (string, '~'))
  if (result = strchr (string, '~'))
    result = (char *)xmalloc (result_size = (strlen (string) + 16));
    result = (char *)xmalloc (result_size = (strlen (string) + 16));
  else
  else
    result = (char *)xmalloc (result_size = (strlen (string) + 1));
    result = (char *)xmalloc (result_size = (strlen (string) + 1));
 
 
  /* Scan through STRING expanding tildes as we come to them. */
  /* Scan through STRING expanding tildes as we come to them. */
  while (1)
  while (1)
    {
    {
      register int start, end;
      register int start, end;
      char *tilde_word, *expansion;
      char *tilde_word, *expansion;
      int len;
      int len;
 
 
      /* Make START point to the tilde which starts the expansion. */
      /* Make START point to the tilde which starts the expansion. */
      start = tilde_find_prefix (string, &len);
      start = tilde_find_prefix (string, &len);
 
 
      /* Copy the skipped text into the result. */
      /* Copy the skipped text into the result. */
      if ((result_index + start + 1) > result_size)
      if ((result_index + start + 1) > result_size)
        result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
        result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
 
 
      strncpy (result + result_index, string, start);
      strncpy (result + result_index, string, start);
      result_index += start;
      result_index += start;
 
 
      /* Advance STRING to the starting tilde. */
      /* Advance STRING to the starting tilde. */
      string += start;
      string += start;
 
 
      /* Make END be the index of one after the last character of the
      /* Make END be the index of one after the last character of the
         username. */
         username. */
      end = tilde_find_suffix (string);
      end = tilde_find_suffix (string);
 
 
      /* If both START and END are zero, we are all done. */
      /* If both START and END are zero, we are all done. */
      if (!start && !end)
      if (!start && !end)
        break;
        break;
 
 
      /* Expand the entire tilde word, and copy it into RESULT. */
      /* Expand the entire tilde word, and copy it into RESULT. */
      tilde_word = (char *)xmalloc (1 + end);
      tilde_word = (char *)xmalloc (1 + end);
      strncpy (tilde_word, string, end);
      strncpy (tilde_word, string, end);
      tilde_word[end] = '\0';
      tilde_word[end] = '\0';
      string += end;
      string += end;
 
 
      expansion = tilde_expand_word (tilde_word);
      expansion = tilde_expand_word (tilde_word);
      free (tilde_word);
      free (tilde_word);
 
 
      len = strlen (expansion);
      len = strlen (expansion);
#ifdef __CYGWIN__
#ifdef __CYGWIN__
      /* Fix for Cygwin to prevent ~user/xxx from expanding to //xxx when
      /* Fix for Cygwin to prevent ~user/xxx from expanding to //xxx when
         $HOME for `user' is /.  On cygwin, // denotes a network drive. */
         $HOME for `user' is /.  On cygwin, // denotes a network drive. */
      if (len > 1 || *expansion != '/' || *string != '/')
      if (len > 1 || *expansion != '/' || *string != '/')
#endif
#endif
        {
        {
          if ((result_index + len + 1) > result_size)
          if ((result_index + len + 1) > result_size)
            result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
            result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
 
 
          strcpy (result + result_index, expansion);
          strcpy (result + result_index, expansion);
          result_index += len;
          result_index += len;
        }
        }
      free (expansion);
      free (expansion);
    }
    }
 
 
  result[result_index] = '\0';
  result[result_index] = '\0';
 
 
  return (result);
  return (result);
}
}
 
 
/* Take FNAME and return the tilde prefix we want expanded.  If LENP is
/* Take FNAME and return the tilde prefix we want expanded.  If LENP is
   non-null, the index of the end of the prefix into FNAME is returned in
   non-null, the index of the end of the prefix into FNAME is returned in
   the location it points to. */
   the location it points to. */
static char *
static char *
isolate_tilde_prefix (fname, lenp)
isolate_tilde_prefix (fname, lenp)
     const char *fname;
     const char *fname;
     int *lenp;
     int *lenp;
{
{
  char *ret;
  char *ret;
  int i;
  int i;
 
 
  ret = (char *)xmalloc (strlen (fname));
  ret = (char *)xmalloc (strlen (fname));
#if defined (__MSDOS__)
#if defined (__MSDOS__)
  for (i = 1; fname[i] && fname[i] != '/' && fname[i] != '\\'; i++)
  for (i = 1; fname[i] && fname[i] != '/' && fname[i] != '\\'; i++)
#else
#else
  for (i = 1; fname[i] && fname[i] != '/'; i++)
  for (i = 1; fname[i] && fname[i] != '/'; i++)
#endif
#endif
    ret[i - 1] = fname[i];
    ret[i - 1] = fname[i];
  ret[i - 1] = '\0';
  ret[i - 1] = '\0';
  if (lenp)
  if (lenp)
    *lenp = i;
    *lenp = i;
  return ret;
  return ret;
}
}
 
 
#if 0
#if 0
/* Public function to scan a string (FNAME) beginning with a tilde and find
/* Public function to scan a string (FNAME) beginning with a tilde and find
   the portion of the string that should be passed to the tilde expansion
   the portion of the string that should be passed to the tilde expansion
   function.  Right now, it just calls tilde_find_suffix and allocates new
   function.  Right now, it just calls tilde_find_suffix and allocates new
   memory, but it can be expanded to do different things later. */
   memory, but it can be expanded to do different things later. */
char *
char *
tilde_find_word (fname, flags, lenp)
tilde_find_word (fname, flags, lenp)
     const char *fname;
     const char *fname;
     int flags, *lenp;
     int flags, *lenp;
{
{
  int x;
  int x;
  char *r;
  char *r;
 
 
  x = tilde_find_suffix (fname);
  x = tilde_find_suffix (fname);
  if (x == 0)
  if (x == 0)
    {
    {
      r = savestring (fname);
      r = savestring (fname);
      if (lenp)
      if (lenp)
        *lenp = 0;
        *lenp = 0;
    }
    }
  else
  else
    {
    {
      r = (char *)xmalloc (1 + x);
      r = (char *)xmalloc (1 + x);
      strncpy (r, fname, x);
      strncpy (r, fname, x);
      r[x] = '\0';
      r[x] = '\0';
      if (lenp)
      if (lenp)
        *lenp = x;
        *lenp = x;
    }
    }
 
 
  return r;
  return r;
}
}
#endif
#endif
 
 
/* Return a string that is PREFIX concatenated with SUFFIX starting at
/* Return a string that is PREFIX concatenated with SUFFIX starting at
   SUFFIND. */
   SUFFIND. */
static char *
static char *
glue_prefix_and_suffix (prefix, suffix, suffind)
glue_prefix_and_suffix (prefix, suffix, suffind)
     char *prefix;
     char *prefix;
     const char *suffix;
     const char *suffix;
     int suffind;
     int suffind;
{
{
  char *ret;
  char *ret;
  int plen, slen;
  int plen, slen;
 
 
  plen = (prefix && *prefix) ? strlen (prefix) : 0;
  plen = (prefix && *prefix) ? strlen (prefix) : 0;
  slen = strlen (suffix + suffind);
  slen = strlen (suffix + suffind);
  ret = (char *)xmalloc (plen + slen + 1);
  ret = (char *)xmalloc (plen + slen + 1);
  if (plen)
  if (plen)
    strcpy (ret, prefix);
    strcpy (ret, prefix);
  strcpy (ret + plen, suffix + suffind);
  strcpy (ret + plen, suffix + suffind);
  return ret;
  return ret;
}
}
 
 
/* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
/* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
   tilde.  If there is no expansion, call tilde_expansion_failure_hook.
   tilde.  If there is no expansion, call tilde_expansion_failure_hook.
   This always returns a newly-allocated string, never static storage. */
   This always returns a newly-allocated string, never static storage. */
char *
char *
tilde_expand_word (filename)
tilde_expand_word (filename)
     const char *filename;
     const char *filename;
{
{
  char *dirname, *expansion, *username;
  char *dirname, *expansion, *username;
  int user_len;
  int user_len;
  struct passwd *user_entry;
  struct passwd *user_entry;
 
 
  if (filename == 0)
  if (filename == 0)
    return ((char *)NULL);
    return ((char *)NULL);
 
 
  if (*filename != '~')
  if (*filename != '~')
    return (savestring (filename));
    return (savestring (filename));
 
 
  /* A leading `~/' or a bare `~' is *always* translated to the value of
  /* A leading `~/' or a bare `~' is *always* translated to the value of
     $HOME or the home directory of the current user, regardless of any
     $HOME or the home directory of the current user, regardless of any
     preexpansion hook. */
     preexpansion hook. */
  if (filename[1] == '\0' || filename[1] == '/')
  if (filename[1] == '\0' || filename[1] == '/')
    {
    {
      /* Prefix $HOME to the rest of the string. */
      /* Prefix $HOME to the rest of the string. */
      expansion = sh_get_env_value ("HOME");
      expansion = sh_get_env_value ("HOME");
 
 
      /* If there is no HOME variable, look up the directory in
      /* If there is no HOME variable, look up the directory in
         the password database. */
         the password database. */
      if (expansion == 0)
      if (expansion == 0)
        expansion = sh_get_home_dir ();
        expansion = sh_get_home_dir ();
 
 
      return (glue_prefix_and_suffix (expansion, filename, 1));
      return (glue_prefix_and_suffix (expansion, filename, 1));
    }
    }
 
 
  username = isolate_tilde_prefix (filename, &user_len);
  username = isolate_tilde_prefix (filename, &user_len);
 
 
  if (tilde_expansion_preexpansion_hook)
  if (tilde_expansion_preexpansion_hook)
    {
    {
      expansion = (*tilde_expansion_preexpansion_hook) (username);
      expansion = (*tilde_expansion_preexpansion_hook) (username);
      if (expansion)
      if (expansion)
        {
        {
          dirname = glue_prefix_and_suffix (expansion, filename, user_len);
          dirname = glue_prefix_and_suffix (expansion, filename, user_len);
          free (username);
          free (username);
          free (expansion);
          free (expansion);
          return (dirname);
          return (dirname);
        }
        }
    }
    }
 
 
  /* No preexpansion hook, or the preexpansion hook failed.  Look in the
  /* No preexpansion hook, or the preexpansion hook failed.  Look in the
     password database. */
     password database. */
  dirname = (char *)NULL;
  dirname = (char *)NULL;
#if defined (HAVE_GETPWNAM)
#if defined (HAVE_GETPWNAM)
  user_entry = getpwnam (username);
  user_entry = getpwnam (username);
#else
#else
  user_entry = 0;
  user_entry = 0;
#endif
#endif
  if (user_entry == 0)
  if (user_entry == 0)
    {
    {
      /* If the calling program has a special syntax for expanding tildes,
      /* If the calling program has a special syntax for expanding tildes,
         and we couldn't find a standard expansion, then let them try. */
         and we couldn't find a standard expansion, then let them try. */
      if (tilde_expansion_failure_hook)
      if (tilde_expansion_failure_hook)
        {
        {
          expansion = (*tilde_expansion_failure_hook) (username);
          expansion = (*tilde_expansion_failure_hook) (username);
          if (expansion)
          if (expansion)
            {
            {
              dirname = glue_prefix_and_suffix (expansion, filename, user_len);
              dirname = glue_prefix_and_suffix (expansion, filename, user_len);
              free (expansion);
              free (expansion);
            }
            }
        }
        }
      free (username);
      free (username);
      /* If we don't have a failure hook, or if the failure hook did not
      /* If we don't have a failure hook, or if the failure hook did not
         expand the tilde, return a copy of what we were passed. */
         expand the tilde, return a copy of what we were passed. */
      if (dirname == 0)
      if (dirname == 0)
        dirname = savestring (filename);
        dirname = savestring (filename);
    }
    }
#if defined (HAVE_GETPWENT)
#if defined (HAVE_GETPWENT)
  else
  else
    {
    {
      free (username);
      free (username);
      dirname = glue_prefix_and_suffix (user_entry->pw_dir, filename, user_len);
      dirname = glue_prefix_and_suffix (user_entry->pw_dir, filename, user_len);
    }
    }
  endpwent ();
  endpwent ();
#endif
#endif
  return (dirname);
  return (dirname);
}
}
 
 


#if defined (TEST)
#if defined (TEST)
#undef NULL
#undef NULL
#include <stdio.h>
#include <stdio.h>
 
 
main (argc, argv)
main (argc, argv)
     int argc;
     int argc;
     char **argv;
     char **argv;
{
{
  char *result, line[512];
  char *result, line[512];
  int done = 0;
  int done = 0;
 
 
  while (!done)
  while (!done)
    {
    {
      printf ("~expand: ");
      printf ("~expand: ");
      fflush (stdout);
      fflush (stdout);
 
 
      if (!gets (line))
      if (!gets (line))
        strcpy (line, "done");
        strcpy (line, "done");
 
 
      if ((strcmp (line, "done") == 0) ||
      if ((strcmp (line, "done") == 0) ||
          (strcmp (line, "quit") == 0) ||
          (strcmp (line, "quit") == 0) ||
          (strcmp (line, "exit") == 0))
          (strcmp (line, "exit") == 0))
        {
        {
          done = 1;
          done = 1;
          break;
          break;
        }
        }
 
 
      result = tilde_expand (line);
      result = tilde_expand (line);
      printf ("  --> %s\n", result);
      printf ("  --> %s\n", result);
      free (result);
      free (result);
    }
    }
  exit (0);
  exit (0);
}
}
 
 
static void memory_error_and_abort ();
static void memory_error_and_abort ();
 
 
static void *
static void *
xmalloc (bytes)
xmalloc (bytes)
     size_t bytes;
     size_t bytes;
{
{
  void *temp = (char *)malloc (bytes);
  void *temp = (char *)malloc (bytes);
 
 
  if (!temp)
  if (!temp)
    memory_error_and_abort ();
    memory_error_and_abort ();
  return (temp);
  return (temp);
}
}
 
 
static void *
static void *
xrealloc (pointer, bytes)
xrealloc (pointer, bytes)
     void *pointer;
     void *pointer;
     int bytes;
     int bytes;
{
{
  void *temp;
  void *temp;
 
 
  if (!pointer)
  if (!pointer)
    temp = malloc (bytes);
    temp = malloc (bytes);
  else
  else
    temp = realloc (pointer, bytes);
    temp = realloc (pointer, bytes);
 
 
  if (!temp)
  if (!temp)
    memory_error_and_abort ();
    memory_error_and_abort ();
 
 
  return (temp);
  return (temp);
}
}
 
 
static void
static void
memory_error_and_abort ()
memory_error_and_abort ()
{
{
  fprintf (stderr, "readline: out of virtual memory\n");
  fprintf (stderr, "readline: out of virtual memory\n");
  abort ();
  abort ();
}
}
 
 
/*
/*
 * Local variables:
 * Local variables:
 * compile-command: "gcc -g -DTEST -o tilde tilde.c"
 * compile-command: "gcc -g -DTEST -o tilde tilde.c"
 * end:
 * end:
 */
 */
#endif /* TEST */
#endif /* TEST */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.