home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / gnuinfo / Source / c / tilde < prev    next >
Encoding:
Text File  |  1994-10-01  |  9.4 KB  |  381 lines

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