home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / tilde.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  8.1 KB  |  308 lines

  1. /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo).  */
  2.  
  3. /* Copyright (C) 1988,1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Readline, a library for reading lines
  6.    of text with interactive input and history editing.
  7.  
  8.    Readline is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by the
  10.    Free Software Foundation; either version 1, or (at your option) any
  11.    later version.
  12.  
  13.    Readline is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with Readline; see the file COPYING.  If not, write to the Free
  20.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #if defined (HAVE_STDLIB_H)
  28. #  include <stdlib.h>
  29. #else
  30. #  include "ansi_stdlib.h"
  31. #endif /* HAVE_STDLIB_H */
  32.  
  33. #include <sys/types.h>
  34.  
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif /* HAVE_UNISTD_H */
  38.  
  39. #include "xmalloc.h"
  40. #include "xstring.h"
  41. #include "tilde.h"
  42.  
  43. #include <pwd.h>
  44.  
  45. #if defined (USG) && !defined (HAVE_GETPW_DECLS)
  46. extern struct passwd *getpwuid (), *getpwnam ();
  47. #endif /* USG && !defined (HAVE_GETPW_DECLS) */
  48.  
  49. #if !defined (savestring)
  50. extern char *xmalloc ();
  51. #  ifndef strcpy
  52. extern char *strcpy ();
  53. #  endif
  54. #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
  55. #endif /* !savestring */
  56.  
  57. #if !defined (NULL)
  58. #  if defined (STDC_HEADERS)
  59. #    define NULL ((void *) 0)
  60. #  else
  61. #    define NULL 0x0
  62. #  endif /* !STDC_HEADERS */
  63. #endif /* !NULL */
  64.  
  65.  
  66. /* The default value of tilde_additional_prefixes.  This is set to
  67.    whitespace preceding a tilde so that simple programs which do not
  68.    perform any word separation get desired behaviour. */
  69. static char *default_prefixes[] =
  70.   { " ~", "\t~", (char *)NULL };
  71.  
  72. /* The default value of tilde_additional_suffixes.  This is set to
  73.    whitespace or newline so that simple programs which do not
  74.    perform any word separation get desired behaviour. */
  75. static char *default_suffixes[] =
  76.   { " ", "\n", (char *)NULL };
  77.  
  78. /* If non-null, this contains the address of a function to call if the
  79.    standard meaning for expanding a tilde fails.  The function is called
  80.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  81.    which is the expansion, or a NULL pointer if there is no expansion. */
  82. CPFunction *tilde_expansion_failure_hook = (CPFunction *)NULL;
  83.  
  84. /* When non-null, this is a NULL terminated array of strings which
  85.    are duplicates for a tilde prefix.  Bash uses this to expand
  86.    `=~' and `:~'. */
  87. char **tilde_additional_prefixes = default_prefixes;
  88.  
  89. /* When non-null, this is a NULL terminated array of strings which match
  90.    the end of a username, instead of just "/".  Bash sets this to
  91.    `:' and `=~'. */
  92. char **tilde_additional_suffixes = default_suffixes;
  93.  
  94. /* Find the start of a tilde expansion in STRING, and return the index of
  95.    the tilde which starts the expansion.  Place the length of the text
  96.    which identified this tilde starter in LEN, excluding the tilde itself. */
  97. static int
  98. tilde_find_prefix (string, len)
  99.      char *string;
  100.      int *len;
  101. {
  102.   register int i, j, string_len;
  103.   register char **prefixes = tilde_additional_prefixes;
  104.  
  105.   string_len = strlen (string);
  106.   *len = 0;
  107.  
  108.   if (!*string || *string == '~')
  109.     return (0);
  110.  
  111.   if (prefixes)
  112.     {
  113.       for (i = 0; i < string_len; i++)
  114.     {
  115.       for (j = 0; prefixes[j]; j++)
  116.         {
  117.           if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
  118.         {
  119.           *len = strlen (prefixes[j]) - 1;
  120.           return (i + *len);
  121.         }
  122.         }
  123.     }
  124.     }
  125.   return (string_len);
  126. }
  127.  
  128. /* Find the end of a tilde expansion in STRING, and return the index of
  129.    the character which ends the tilde definition.  */
  130. static int
  131. tilde_find_suffix (string)
  132.      char *string;
  133. {
  134.   register int i, j, string_len;
  135.   register char **suffixes = tilde_additional_suffixes;
  136.  
  137.   string_len = strlen (string);
  138.  
  139.   for (i = 0; i < string_len; i++)
  140.     {
  141.       if (string[i] == '/' || !string[i])
  142.     break;
  143.  
  144.       for (j = 0; suffixes && suffixes[j]; j++)
  145.     {
  146.       if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
  147.         return (i);
  148.     }
  149.     }
  150.   return (i);
  151. }
  152.  
  153. /* Return a new string which is the result of tilde expanding STRING. */
  154. char *
  155. tilde_expand (string)
  156.      char *string;
  157. {
  158.   char *result, *tilde_expand_word ();
  159.   int result_size, result_index;
  160.  
  161.   result_size = result_index = 0;
  162.   result = (char *)NULL;
  163.  
  164.   /* Scan through STRING expanding tildes as we come to them. */
  165.   while (1)
  166.     {
  167.       register int start, end;
  168.       char *tilde_word, *expansion;
  169.       int len;
  170.  
  171.       /* Make START point to the tilde which starts the expansion. */
  172.       start = tilde_find_prefix (string, &len);
  173.  
  174.       /* Copy the skipped text into the result. */
  175.       if ((result_index + start + 1) > result_size)
  176.     result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
  177.  
  178.       strncpy (result + result_index, string, start);
  179.       result_index += start;
  180.  
  181.       /* Advance STRING to the starting tilde. */
  182.       string += start;
  183.  
  184.       /* Make END be the index of one after the last character of the
  185.      username. */
  186.       end = tilde_find_suffix (string);
  187.  
  188.       /* If both START and END are zero, we are all done. */
  189.       if (!start && !end)
  190.     break;
  191.  
  192.       /* Expand the entire tilde word, and copy it into RESULT. */
  193.       tilde_word = (char *)xmalloc (1 + end);
  194.       strncpy (tilde_word, string, end);
  195.       tilde_word[end] = '\0';
  196.       string += end;
  197.  
  198.       expansion = tilde_expand_word (tilde_word);
  199.       xfree (tilde_word);
  200.  
  201.       len = strlen (expansion);
  202.       if ((result_index + len + 1) > result_size)
  203.     result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
  204.  
  205.       strcpy (result + result_index, expansion);
  206.       result_index += len;
  207.       xfree (expansion);
  208.     }
  209.  
  210.   result[result_index] = '\0';
  211.  
  212.   return (result);
  213. }
  214.  
  215. /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
  216.    tilde.  If there is no expansion, call tilde_expansion_failure_hook. */
  217. char *
  218. tilde_expand_word (filename)
  219.      char *filename;
  220. {
  221.   char *dirname;
  222.  
  223.   dirname = filename ? savestring (filename) : (char *)NULL;
  224.  
  225.   if (dirname && *dirname == '~')
  226.     {
  227.       char *temp_name;
  228.       if (!dirname[1] || dirname[1] == '/')
  229.     {
  230.       /* Prepend $HOME to the rest of the string. */
  231.       char *temp_home = (char *)getenv ("HOME");
  232.  
  233.       /* If there is no HOME variable, look up the directory in
  234.          the password database. */
  235.       if (!temp_home)
  236.         {
  237.           struct passwd *entry;
  238.  
  239.           entry = getpwuid (getuid ());
  240.           if (entry)
  241.         temp_home = entry->pw_dir;
  242.         }
  243.  
  244.       temp_name = xmalloc (1 + strlen (&dirname[1])
  245.                  + (temp_home ? strlen (temp_home) : 0));
  246.       temp_name[0] = '\0';
  247.       if (temp_home)
  248.         strcpy (temp_name, temp_home);
  249.       strcat (temp_name, dirname + 1);
  250.       xfree (dirname);
  251.       dirname = temp_name;
  252.     }
  253.       else
  254.     {
  255.       char u_name[257];
  256.       struct passwd *user_entry;
  257.       char *username;
  258.       int i, c;
  259.  
  260.       username = u_name;
  261.       for (i = 1; (c = dirname[i]); i++)
  262.         {
  263.           if (c == '/')
  264.         break;
  265.           else
  266.         username[i - 1] = c;
  267.         }
  268.       username[i - 1] = '\0';
  269.  
  270.       if (!(user_entry = getpwnam (username)))
  271.         {
  272.           /* If the calling program has a special syntax for
  273.          expanding tildes, and we couldn't find a standard
  274.          expansion, then let them try. */
  275.           if (tilde_expansion_failure_hook)
  276.         {
  277.           char *expansion;
  278.  
  279.           expansion = (*tilde_expansion_failure_hook) (username);
  280.  
  281.           if (expansion)
  282.             {
  283.               temp_name = xmalloc (1 + strlen (expansion)
  284.                           + strlen (&dirname[i]));
  285.               strcpy (temp_name, expansion);
  286.               strcat (temp_name, &dirname[i]);
  287.               xfree (expansion);
  288.               xfree (dirname);
  289.               dirname = temp_name;
  290.             }
  291.         }
  292.           /* We shouldn't report errors. */
  293.         }
  294.       else
  295.         {
  296.           temp_name = xmalloc (1 + strlen (user_entry->pw_dir)
  297.                      + strlen (&dirname[i]));
  298.           strcpy (temp_name, user_entry->pw_dir);
  299.           strcat (temp_name, &dirname[i]);
  300.           xfree (dirname);
  301.           dirname = temp_name;
  302.         }
  303.         endpwent ();
  304.     }
  305.     }
  306.   return (dirname);
  307. }
  308.