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