home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / READLINE.0 / TILDE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-20  |  9.4 KB  |  387 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. #include "memalloc.h"
  23.  
  24. #if defined (HAVE_STRING_H)
  25. #  include <string.h>
  26. #else /* !HAVE_STRING_H */
  27. #  include <strings.h>
  28. #endif /* !HAVE_STRING_H */  
  29.  
  30. #if defined (HAVE_STDLIB_H)
  31. #  include <stdlib.h>
  32. #else
  33. #  include "ansi_stdlib.h"
  34. #endif /* HAVE_STDLIB_H */
  35.  
  36. #include "tilde.h"
  37. #include <pwd.h>
  38.  
  39. #if defined (USG) && !defined (HAVE_GETPW_DECLS)
  40. extern struct passwd *getpwuid (), *getpwnam ();
  41. #endif /* USG && !defined (HAVE_GETPW_DECLS) */
  42.  
  43. #if !defined (savestring)
  44. extern char *xmalloc ();
  45. #  ifndef strcpy
  46. extern char *strcpy ();
  47. #  endif
  48. #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
  49. #endif /* !savestring */
  50.  
  51. #if !defined (NULL)
  52. #  if defined (__STDC__)
  53. #    define NULL ((void *) 0)
  54. #  else
  55. #    define NULL 0x0
  56. #  endif /* !__STDC__ */
  57. #endif /* !NULL */
  58.  
  59. #if defined (TEST) || defined (STATIC_MALLOC)
  60. static char *xmalloc (), *xrealloc ();
  61. #else
  62. extern char *xmalloc (), *xrealloc ();
  63. #endif /* TEST || STATIC_MALLOC */
  64.  
  65. /* The default value of tilde_additional_prefixes.  This is set to
  66.    whitespace preceding a tilde so that simple programs which do not
  67.    perform any word separation get desired behaviour. */
  68. static char *default_prefixes[] =
  69.   { " ~", "\t~", (char *)NULL };
  70.  
  71. /* The default value of tilde_additional_suffixes.  This is set to
  72.    whitespace or newline so that simple programs which do not
  73.    perform any word separation get desired behaviour. */
  74. static char *default_suffixes[] =
  75.   { " ", "\n", (char *)NULL };
  76.  
  77. /* If non-null, this contains the address of a function to call if the
  78.    standard meaning for expanding a tilde fails.  The function is called
  79.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  80.    which is the expansion, or a NULL pointer if there is no expansion. */
  81. CPFunction *tilde_expansion_failure_hook = (CPFunction *)NULL;
  82.  
  83. /* When non-null, this is a NULL terminated array of strings which
  84.    are duplicates for a tilde prefix.  Bash uses this to expand
  85.    `=~' and `:~'. */
  86. char **tilde_additional_prefixes = default_prefixes;
  87.  
  88. /* When non-null, this is a NULL terminated array of strings which match
  89.    the end of a username, instead of just "/".  Bash sets this to
  90.    `:' and `=~'. */
  91. char **tilde_additional_suffixes = default_suffixes;
  92.  
  93. /* Find the start of a tilde expansion in STRING, and return the index of
  94.    the tilde which starts the expansion.  Place the length of the text
  95.    which identified this tilde starter in LEN, excluding the tilde itself. */
  96. static int
  97. tilde_find_prefix (string, len)
  98.      char *string;
  99.      int *len;
  100. {
  101.   register int i, j, string_len;
  102.   register char **prefixes = tilde_additional_prefixes;
  103.  
  104.   string_len = strlen (string);
  105.   *len = 0;
  106.  
  107.   if (!*string || *string == '~')
  108.     return (0);
  109.  
  110.   if (prefixes)
  111.     {
  112.       for (i = 0; i < string_len; i++)
  113.     {
  114.       for (j = 0; prefixes[j]; j++)
  115.         {
  116.           if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
  117.         {
  118.           *len = strlen (prefixes[j]) - 1;
  119.           return (i + *len);
  120.         }
  121.         }
  122.     }
  123.     }
  124.   return (string_len);
  125. }
  126.  
  127. /* Find the end of a tilde expansion in STRING, and return the index of
  128.    the character which ends the tilde definition.  */
  129. static int
  130. tilde_find_suffix (string)
  131.      char *string;
  132. {
  133.   register int i, j, string_len;
  134.   register char **suffixes = tilde_additional_suffixes;
  135.  
  136.   string_len = strlen (string);
  137.  
  138.   for (i = 0; i < string_len; i++)
  139.     {
  140.       if (string[i] == '/' || !string[i])
  141.     break;
  142.  
  143.       for (j = 0; suffixes && suffixes[j]; j++)
  144.     {
  145.       if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
  146.         return (i);
  147.     }
  148.     }
  149.   return (i);
  150. }
  151.  
  152. /* Return a new string which is the result of tilde expanding STRING. */
  153. char *
  154. tilde_expand (string)
  155.      char *string;
  156. {
  157.   char *result, *tilde_expand_word ();
  158.   int result_size, result_index;
  159.  
  160.   result_size = result_index = 0;
  161.   result = (char *)NULL;
  162.  
  163.   /* Scan through STRING expanding tildes as we come to them. */
  164.   while (1)
  165.     {
  166.       register int start, end;
  167.       char *tilde_word, *expansion;
  168.       int len;
  169.  
  170.       /* Make START point to the tilde which starts the expansion. */
  171.       start = tilde_find_prefix (string, &len);
  172.  
  173.       /* Copy the skipped text into the result. */
  174.       if ((result_index + start + 1) > result_size)
  175.     result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
  176.  
  177.       strncpy (result + result_index, string, start);
  178.       result_index += start;
  179.  
  180.       /* Advance STRING to the starting tilde. */
  181.       string += start;
  182.  
  183.       /* Make END be the index of one after the last character of the
  184.      username. */
  185.       end = tilde_find_suffix (string);
  186.  
  187.       /* If both START and END are zero, we are all done. */
  188.       if (!start && !end)
  189.     break;
  190.  
  191.       /* Expand the entire tilde word, and copy it into RESULT. */
  192.       tilde_word = (char *)xmalloc (1 + end);
  193.       strncpy (tilde_word, string, end);
  194.       tilde_word[end] = '\0';
  195.       string += end;
  196.  
  197.       expansion = tilde_expand_word (tilde_word);
  198.       free (tilde_word);
  199.  
  200.       len = strlen (expansion);
  201.       if ((result_index + len + 1) > result_size)
  202.     result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
  203.  
  204.       strcpy (result + result_index, expansion);
  205.       result_index += len;
  206.       free (expansion);
  207.     }
  208.  
  209.   result[result_index] = '\0';
  210.  
  211.   return (result);
  212. }
  213.  
  214. /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
  215.    tilde.  If there is no expansion, call tilde_expansion_failure_hook. */
  216. char *
  217. tilde_expand_word (filename)
  218.      char *filename;
  219. {
  220.   char *dirname;
  221.  
  222.   dirname = filename ? savestring (filename) : (char *)NULL;
  223.  
  224.   if (dirname && *dirname == '~')
  225.     {
  226.       char *temp_name;
  227.       if (!dirname[1] || dirname[1] == '/')
  228.     {
  229.       /* Prepend $HOME to the rest of the string. */
  230.       char *temp_home = (char *)getenv ("HOME");
  231.  
  232.       /* If there is no HOME variable, look up the directory in
  233.          the password database. */
  234.       if (!temp_home)
  235.         {
  236.           struct passwd *entry;
  237.  
  238.           entry = getpwuid (getuid ());
  239.           if (entry)
  240.         temp_home = entry->pw_dir;
  241.         }
  242.  
  243.       temp_name = xmalloc (1 + strlen (&dirname[1])
  244.                  + (temp_home ? strlen (temp_home) : 0));
  245.       temp_name[0] = '\0';
  246.       if (temp_home)
  247.         strcpy (temp_name, temp_home);
  248.       strcat (temp_name, dirname + 1);
  249.       free (dirname);
  250.       dirname = temp_name;
  251.     }
  252.       else
  253.     {
  254.       char u_name[257];
  255.       struct passwd *user_entry;
  256.       char *username;
  257.       int i, c;
  258.  
  259.       username = u_name;
  260.       for (i = 1; c = dirname[i]; i++)
  261.         {
  262.           if (c == '/')
  263.         break;
  264.           else
  265.         username[i - 1] = c;
  266.         }
  267.       username[i - 1] = '\0';
  268.  
  269.       if (!(user_entry = getpwnam (username)))
  270.         {
  271.           /* If the calling program has a special syntax for
  272.          expanding tildes, and we couldn't find a standard
  273.          expansion, then let them try. */
  274.           if (tilde_expansion_failure_hook)
  275.         {
  276.           char *expansion;
  277.  
  278.           expansion = (*tilde_expansion_failure_hook) (username);
  279.  
  280.           if (expansion)
  281.             {
  282.               temp_name = xmalloc (1 + strlen (expansion)
  283.                           + strlen (&dirname[i]));
  284.               strcpy (temp_name, expansion);
  285.               strcat (temp_name, &dirname[i]);
  286.               free (expansion);
  287.               free (dirname);
  288.               dirname = temp_name;
  289.             }
  290.         }
  291.           /* We shouldn't report errors. */
  292.         }
  293.       else
  294.         {
  295.           temp_name = xmalloc (1 + strlen (user_entry->pw_dir)
  296.                      + strlen (&dirname[i]));
  297.           strcpy (temp_name, user_entry->pw_dir);
  298.           strcat (temp_name, &dirname[i]);
  299.           free (dirname);
  300.           dirname = temp_name;
  301.         }
  302.         endpwent ();
  303.     }
  304.     }
  305.   return (dirname);
  306. }
  307.  
  308.  
  309. #if defined (TEST)
  310. #undef NULL
  311. #include <stdio.h>
  312.  
  313. main (argc, argv)
  314.      int argc;
  315.      char **argv;
  316. {
  317.   char *result, line[512];
  318.   int done = 0;
  319.  
  320.   while (!done)
  321.     {
  322.       printf ("~expand: ");
  323.       fflush (stdout);
  324.  
  325.       if (!gets (line))
  326.     strcpy (line, "done");
  327.  
  328.       if ((strcmp (line, "done") == 0) ||
  329.       (strcmp (line, "quit") == 0) ||
  330.       (strcmp (line, "exit") == 0))
  331.     {
  332.       done = 1;
  333.       break;
  334.     }
  335.  
  336.       result = tilde_expand (line);
  337.       printf ("  --> %s\n", result);
  338.       free (result);
  339.     }
  340.   exit (0);
  341. }
  342.  
  343. static void memory_error_and_abort ();
  344.  
  345. static char *
  346. xmalloc (bytes)
  347.      int bytes;
  348. {
  349.   char *temp = (char *)malloc (bytes);
  350.  
  351.   if (!temp)
  352.     memory_error_and_abort ();
  353.   return (temp);
  354. }
  355.  
  356. static char *
  357. xrealloc (pointer, bytes)
  358.      char *pointer;
  359.      int bytes;
  360. {
  361.   char *temp;
  362.  
  363.   if (!pointer)
  364.     temp = (char *)malloc (bytes);
  365.   else
  366.     temp = (char *)realloc (pointer, bytes);
  367.  
  368.   if (!temp)
  369.     memory_error_and_abort ();
  370.  
  371.   return (temp);
  372. }
  373.  
  374. static void
  375. memory_error_and_abort ()
  376. {
  377.   fprintf (stderr, "readline: Out of virtual memory!\n");
  378.   abort ();
  379. }
  380.  
  381. /*
  382.  * Local variables:
  383.  * compile-command: "gcc -g -DTEST -o tilde tilde.c"
  384.  * end:
  385.  */
  386. #endif /* TEST */
  387.