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