1 /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo).
2 $Id: tilde.c,v 1.1 1997/08/21 22:58:05 jason Exp $
4 This file is part of GNU Info, a program for reading online documentation
7 Copyright (C) 1988, 89, 90, 91, 92, 93, 96 Free Software Foundation, Inc.
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)
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.
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.
23 Written by Brian Fox (bfox@ai.mit.edu). */
25 #if defined (__GNUC__)
26 # define alloca __builtin_alloca
31 # if defined (HAVE_ALLOCA_H)
33 # endif /* HAVE_ALLOCA_H */
35 #endif /* !__GNUC__ */
37 #if defined (HAVE_STDLIB_H)
44 #if defined (HAVE_STRING_H)
54 #if defined (TEST) || defined (STATIC_MALLOC)
55 static void *xmalloc (), *xrealloc ();
57 extern void *xmalloc (), *xrealloc ();
58 #endif /* TEST || STATIC_MALLOC */
60 /* The default value of tilde_additional_prefixes. This is set to
61 whitespace preceding a tilde so that simple programs which do not
62 perform any word separation get desired behaviour. */
63 static char *default_prefixes[] =
64 { " ~", "\t~", (char *)NULL };
66 /* The default value of tilde_additional_suffixes. This is set to
67 whitespace or newline so that simple programs which do not
68 perform any word separation get desired behaviour. */
69 static char *default_suffixes[] =
70 { " ", "\n", (char *)NULL };
72 /* If non-null, this contains the address of a function to call if the
73 standard meaning for expanding a tilde fails. The function is called
74 with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
75 which is the expansion, or a NULL pointer if there is no expansion. */
76 CFunction *tilde_expansion_failure_hook = (CFunction *)NULL;
78 /* When non-null, this is a NULL terminated array of strings which
79 are duplicates for a tilde prefix. Bash uses this to expand
81 char **tilde_additional_prefixes = default_prefixes;
83 /* When non-null, this is a NULL terminated array of strings which match
84 the end of a username, instead of just "/". Bash sets this to
86 char **tilde_additional_suffixes = default_suffixes;
88 /* Find the start of a tilde expansion in STRING, and return the index of
89 the tilde which starts the expansion. Place the length of the text
90 which identified this tilde starter in LEN, excluding the tilde itself. */
92 tilde_find_prefix (string, len)
96 register int i, j, string_len;
97 register char **prefixes = tilde_additional_prefixes;
99 string_len = strlen (string);
102 if (!*string || *string == '~')
107 for (i = 0; i < string_len; i++)
109 for (j = 0; prefixes[j]; j++)
111 if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
113 *len = strlen (prefixes[j]) - 1;
122 /* Find the end of a tilde expansion in STRING, and return the index of
123 the character which ends the tilde definition. */
125 tilde_find_suffix (string)
128 register int i, j, string_len;
129 register char **suffixes = tilde_additional_suffixes;
131 string_len = strlen (string);
133 for (i = 0; i < string_len; i++)
135 if (string[i] == '/' || !string[i])
138 for (j = 0; suffixes && suffixes[j]; j++)
140 if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
147 /* Return a new string which is the result of tilde expanding STRING. */
149 tilde_expand (string)
152 char *result, *tilde_expand_word ();
153 int result_size, result_index;
155 result_size = result_index = 0;
156 result = (char *)NULL;
158 /* Scan through STRING expanding tildes as we come to them. */
161 register int start, end;
162 char *tilde_word, *expansion;
165 /* Make START point to the tilde which starts the expansion. */
166 start = tilde_find_prefix (string, &len);
168 /* Copy the skipped text into the result. */
169 if ((result_index + start + 1) > result_size)
170 result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
172 strncpy (result + result_index, string, start);
173 result_index += start;
175 /* Advance STRING to the starting tilde. */
178 /* Make END be the index of one after the last character of the
180 end = tilde_find_suffix (string);
182 /* If both START and END are zero, we are all done. */
186 /* Expand the entire tilde word, and copy it into RESULT. */
187 tilde_word = (char *)xmalloc (1 + end);
188 strncpy (tilde_word, string, end);
189 tilde_word[end] = '\0';
192 expansion = tilde_expand_word (tilde_word);
195 len = strlen (expansion);
196 if ((result_index + len + 1) > result_size)
197 result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
199 strcpy (result + result_index, expansion);
204 result[result_index] = '\0';
209 /* Do the work of tilde expansion on FILENAME. FILENAME starts with a
210 tilde. If there is no expansion, call tilde_expansion_failure_hook. */
212 tilde_expand_word (filename)
217 dirname = filename ? strdup (filename) : (char *)NULL;
219 if (dirname && *dirname == '~')
222 if (!dirname[1] || dirname[1] == '/')
224 /* Prepend $HOME to the rest of the string. */
225 extern char *getenv ();
226 char *temp_home = getenv ("HOME");
228 /* If there is no HOME variable, look up the directory in
229 the password database. */
232 struct passwd *entry;
234 entry = (struct passwd *) getpwuid (getuid ());
236 temp_home = entry->pw_dir;
240 alloca (1 + strlen (&dirname[1])
241 + (temp_home ? strlen (temp_home) : 0));
244 strcpy (temp_name, temp_home);
245 strcat (temp_name, &dirname[1]);
247 dirname = strdup (temp_name);
251 struct passwd *user_entry;
252 char *username = (char *)alloca (257);
255 for (i = 1; c = dirname[i]; i++)
262 username[i - 1] = '\0';
264 if (!(user_entry = (struct passwd *) getpwnam (username)))
266 /* If the calling program has a special syntax for
267 expanding tildes, and we couldn't find a standard
268 expansion, then let them try. */
269 if (tilde_expansion_failure_hook)
273 expansion = (*tilde_expansion_failure_hook) (username);
277 temp_name = (char *)alloca
278 (1 + strlen (expansion) + strlen (&dirname[i]));
279 strcpy (temp_name, expansion);
280 strcat (temp_name, &dirname[i]);
285 /* We shouldn't report errors. */
289 temp_name = (char *)alloca
290 (1 + strlen (user_entry->pw_dir) + strlen (&dirname[i]));
291 strcpy (temp_name, user_entry->pw_dir);
292 strcat (temp_name, &dirname[i]);
295 dirname = strdup (temp_name);
312 char *result, line[512];
317 printf ("~expand: ");
321 strcpy (line, "done");
323 if ((strcmp (line, "done") == 0) ||
324 (strcmp (line, "quit") == 0) ||
325 (strcmp (line, "exit") == 0))
331 result = tilde_expand (line);
332 printf (" --> %s\n", result);
338 static void memory_error_and_abort ();
344 void *temp = (void *)malloc (bytes);
347 memory_error_and_abort ();
352 xrealloc (pointer, bytes)
359 temp = (char *)malloc (bytes);
361 temp = (char *)realloc (pointer, bytes);
364 memory_error_and_abort ();
370 memory_error_and_abort ()
372 fprintf (stderr, "readline: Out of virtual memory!\n");