home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / info / tilde.c < prev    next >
C/C++ Source or Header  |  1997-07-24  |  10KB  |  360 lines

  1. /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo).
  2.    $Id: tilde.c,v 1.8 1997/07/24 21:49:03 karl Exp $
  3.  
  4.    This file is part of GNU Info, a program for reading online documentation
  5.    stored in Info format.
  6.  
  7.    Copyright (C) 1988, 89, 90, 91, 92, 93, 96 Free Software Foundation, Inc.
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2, or (at your option)
  12.    any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23.    Written by Brian Fox (bfox@ai.mit.edu). */
  24.  
  25. /* Include config.h before doing alloca.  */
  26. #include "info.h"
  27.  
  28. #if defined (__GNUC__)
  29. #  define alloca __builtin_alloca
  30. #else /* !__GNUC__ */
  31. #  if defined (_AIX)
  32.  #pragma alloca
  33. #  else /* !_AIX */
  34. #    if defined (HAVE_ALLOCA_H)
  35. #      include <alloca.h>
  36. #    endif /* HAVE_ALLOCA_H */
  37. #  endif /* !AIX */
  38. #endif /* !__GNUC__ */
  39.  
  40. #if defined (TEST) || defined (STATIC_MALLOC)
  41. static void *xmalloc (), *xrealloc ();
  42. #endif /* TEST || STATIC_MALLOC */
  43.  
  44. /* The default value of tilde_additional_prefixes.  This is set to
  45.    whitespace preceding a tilde so that simple programs which do not
  46.    perform any word separation get desired behaviour. */
  47. static char *default_prefixes[] =
  48.   { " ~", "\t~", (char *)NULL };
  49.  
  50. /* The default value of tilde_additional_suffixes.  This is set to
  51.    whitespace or newline so that simple programs which do not
  52.    perform any word separation get desired behaviour. */
  53. static char *default_suffixes[] =
  54.   { " ", "\n", (char *)NULL };
  55.  
  56. /* If non-null, this contains the address of a function to call if the
  57.    standard meaning for expanding a tilde fails.  The function is called
  58.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  59.    which is the expansion, or a NULL pointer if there is no expansion. */
  60. CFunction *tilde_expansion_failure_hook = (CFunction *)NULL;
  61.  
  62. /* When non-null, this is a NULL terminated array of strings which
  63.    are duplicates for a tilde prefix.  Bash uses this to expand
  64.    `=~' and `:~'. */
  65. char **tilde_additional_prefixes = default_prefixes;
  66.  
  67. /* When non-null, this is a NULL terminated array of strings which match
  68.    the end of a username, instead of just "/".  Bash sets this to
  69.    `:' and `=~'. */
  70. char **tilde_additional_suffixes = default_suffixes;
  71.  
  72. /* Find the start of a tilde expansion in STRING, and return the index of
  73.    the tilde which starts the expansion.  Place the length of the text
  74.    which identified this tilde starter in LEN, excluding the tilde itself. */
  75. static int
  76. tilde_find_prefix (string, len)
  77.      char *string;
  78.      int *len;
  79. {
  80.   register int i, j, string_len;
  81.   register char **prefixes = tilde_additional_prefixes;
  82.  
  83.   string_len = strlen (string);
  84.   *len = 0;
  85.  
  86.   if (!*string || *string == '~')
  87.     return (0);
  88.  
  89.   if (prefixes)
  90.     {
  91.       for (i = 0; i < string_len; i++)
  92.         {
  93.           for (j = 0; prefixes[j]; j++)
  94.             {
  95.               if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
  96.                 {
  97.                   *len = strlen (prefixes[j]) - 1;
  98.                   return (i + *len);
  99.                 }
  100.             }
  101.         }
  102.     }
  103.   return (string_len);
  104. }
  105.  
  106. /* Find the end of a tilde expansion in STRING, and return the index of
  107.    the character which ends the tilde definition.  */
  108. static int
  109. tilde_find_suffix (string)
  110.      char *string;
  111. {
  112.   register int i, j, string_len;
  113.   register char **suffixes = tilde_additional_suffixes;
  114.  
  115.   string_len = strlen (string);
  116.  
  117.   for (i = 0; i < string_len; i++)
  118.     {
  119.       if (string[i] == '/' || !string[i])
  120.         break;
  121.  
  122.       for (j = 0; suffixes && suffixes[j]; j++)
  123.         {
  124.           if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
  125.             return (i);
  126.         }
  127.     }
  128.   return (i);
  129. }
  130.  
  131. /* Return a new string which is the result of tilde expanding STRING. */
  132. char *
  133. tilde_expand (string)
  134.      char *string;
  135. {
  136.   char *result, *tilde_expand_word ();
  137.   int result_size, result_index;
  138.  
  139.   result_size = result_index = 0;
  140.   result = (char *)NULL;
  141.  
  142.   /* Scan through STRING expanding tildes as we come to them. */
  143.   while (1)
  144.     {
  145.       register int start, end;
  146.       char *tilde_word, *expansion;
  147.       int len;
  148.  
  149.       /* Make START point to the tilde which starts the expansion. */
  150.       start = tilde_find_prefix (string, &len);
  151.  
  152.       /* Copy the skipped text into the result. */
  153.       if ((result_index + start + 1) > result_size)
  154.         result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
  155.  
  156.       strncpy (result + result_index, string, start);
  157.       result_index += start;
  158.  
  159.       /* Advance STRING to the starting tilde. */
  160.       string += start;
  161.  
  162.       /* Make END be the index of one after the last character of the
  163.          username. */
  164.       end = tilde_find_suffix (string);
  165.  
  166.       /* If both START and END are zero, we are all done. */
  167.       if (!start && !end)
  168.         break;
  169.  
  170.       /* Expand the entire tilde word, and copy it into RESULT. */
  171.       tilde_word = (char *)xmalloc (1 + end);
  172.       strncpy (tilde_word, string, end);
  173.       tilde_word[end] = '\0';
  174.       string += end;
  175.  
  176.       expansion = tilde_expand_word (tilde_word);
  177.       free (tilde_word);
  178.  
  179.       len = strlen (expansion);
  180.       if ((result_index + len + 1) > result_size)
  181.         result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
  182.  
  183.       strcpy (result + result_index, expansion);
  184.       result_index += len;
  185.       free (expansion);
  186.     }
  187.  
  188.   result[result_index] = '\0';
  189.  
  190.   return (result);
  191. }
  192.  
  193. /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
  194.    tilde.  If there is no expansion, call tilde_expansion_failure_hook. */
  195. char *
  196. tilde_expand_word (filename)
  197.      char *filename;
  198. {
  199.   char *dirname;
  200.  
  201.   dirname = filename ? xstrdup (filename) : (char *)NULL;
  202.  
  203.   if (dirname && *dirname == '~')
  204.     {
  205.       char *temp_name;
  206.       if (!dirname[1] || dirname[1] == '/')
  207.         {
  208.           /* Prepend $HOME to the rest of the string. */
  209.           char *temp_home = getenv ("HOME");
  210.  
  211.           /* If there is no HOME variable, look up the directory in
  212.              the password database. */
  213.           if (!temp_home)
  214.             {
  215.               struct passwd *entry;
  216.  
  217.               entry = (struct passwd *) getpwuid (getuid ());
  218.               if (entry)
  219.                 temp_home = entry->pw_dir;
  220.             }
  221.  
  222.           temp_name = (char *)
  223.             alloca (1 + strlen (&dirname[1])
  224.                     + (temp_home ? strlen (temp_home) : 0));
  225.           temp_name[0] = '\0';
  226.           if (temp_home)
  227.             strcpy (temp_name, temp_home);
  228.           strcat (temp_name, &dirname[1]);
  229.           free (dirname);
  230.           dirname = xstrdup (temp_name);
  231.         }
  232.       else
  233.         {
  234.           struct passwd *user_entry;
  235.           char *username = (char *)alloca (257);
  236.           int i, c;
  237.  
  238.           for (i = 1; (c = dirname[i]); i++)
  239.             {
  240.               if (c == '/')
  241.                 break;
  242.               else
  243.                 username[i - 1] = c;
  244.             }
  245.           username[i - 1] = '\0';
  246.  
  247.           if (!(user_entry = (struct passwd *) getpwnam (username)))
  248.             {
  249.               /* If the calling program has a special syntax for
  250.                  expanding tildes, and we couldn't find a standard
  251.                  expansion, then let them try. */
  252.               if (tilde_expansion_failure_hook)
  253.                 {
  254.                   char *expansion;
  255.  
  256.                   expansion = (*tilde_expansion_failure_hook) (username);
  257.  
  258.                   if (expansion)
  259.                     {
  260.                       temp_name = (char *)alloca
  261.                         (1 + strlen (expansion) + strlen (&dirname[i]));
  262.                       strcpy (temp_name, expansion);
  263.                       strcat (temp_name, &dirname[i]);
  264.                       free (expansion);
  265.                       goto return_name;
  266.                     }
  267.                 }
  268.               /* We shouldn't report errors. */
  269.             }
  270.           else
  271.             {
  272.               temp_name = (char *)alloca
  273.                 (1 + strlen (user_entry->pw_dir) + strlen (&dirname[i]));
  274.               strcpy (temp_name, user_entry->pw_dir);
  275.               strcat (temp_name, &dirname[i]);
  276.             return_name:
  277.               free (dirname);
  278.               dirname = xstrdup (temp_name);
  279.             }
  280.             endpwent ();
  281.         }
  282.     }
  283.   return (dirname);
  284. }
  285.  
  286.  
  287. #if defined (TEST)
  288. #undef NULL
  289. #include <stdio.h>
  290.  
  291. main (argc, argv)
  292.      int argc;
  293.      char **argv;
  294. {
  295.   char *result, line[512];
  296.   int done = 0;
  297.  
  298.   while (!done)
  299.     {
  300.       printf ("~expand: ");
  301.       fflush (stdout);
  302.  
  303.       if (!gets (line))
  304.         strcpy (line, "done");
  305.  
  306.       if ((strcmp (line, "done") == 0) ||
  307.           (strcmp (line, "quit") == 0) ||
  308.           (strcmp (line, "exit") == 0))
  309.         {
  310.           done = 1;
  311.           break;
  312.         }
  313.  
  314.       result = tilde_expand (line);
  315.       printf ("  --> %s\n", result);
  316.       free (result);
  317.     }
  318.   exit (0);
  319. }
  320.  
  321. static void memory_error_and_abort ();
  322.  
  323. static void *
  324. xmalloc (bytes)
  325.      int bytes;
  326. {
  327.   void *temp = (void *)malloc (bytes);
  328.  
  329.   if (!temp)
  330.     memory_error_and_abort ();
  331.   return (temp);
  332. }
  333.  
  334. static void *
  335. xrealloc (pointer, bytes)
  336.      void *pointer;
  337.      int bytes;
  338. {
  339.   void *temp;
  340.  
  341.   if (!pointer)
  342.     temp = (char *)malloc (bytes);
  343.   else
  344.     temp = (char *)realloc (pointer, bytes);
  345.  
  346.   if (!temp)
  347.     memory_error_and_abort ();
  348.  
  349.   return (temp);
  350. }
  351.  
  352. static void
  353. memory_error_and_abort ()
  354. {
  355.   fprintf (stderr, _("readline: Out of virtual memory!\n"));
  356.   abort ();
  357. }
  358. #endif /* TEST */
  359.  
  360.