1 /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
3 /* Copyright (C) 1988,1989 Free Software Foundation, Inc.
5 This file is part of GNU Readline, a library for reading lines
6 of text with interactive input and history editing.
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
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.
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. */
22 #if defined (__GNUC__)
24 # define alloca __builtin_alloca
29 # if defined (HAVE_ALLOCA_H)
31 # endif /* HAVE_ALLOCA_H */
33 #endif /* !__GNUC__ */
35 #if defined (HAVE_STRING_H)
37 #else /* !HAVE_STRING_H */
39 #endif /* !HAVE_STRING_H */
41 #if defined (HAVE_STDLIB_H)
44 # include "ansi_stdlib.h"
45 #endif /* HAVE_STDLIB_H */
47 #include <tilde/tilde.h>
50 #if !defined (sgi) && !defined (isc386)
51 extern struct passwd
*getpwnam (), *getpwuid ();
54 #if !defined (savestring)
55 extern char *xmalloc ();
57 extern char *strcpy ();
59 #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
60 #endif /* !savestring */
63 # if defined (__STDC__)
64 # define NULL ((void *) 0)
67 # endif /* !__STDC__ */
70 #if defined (TEST) || defined (STATIC_MALLOC)
71 static char *xmalloc (), *xrealloc ();
73 extern char *xmalloc (), *xrealloc ();
74 #endif /* TEST || STATIC_MALLOC */
76 /* The default value of tilde_additional_prefixes. This is set to
77 whitespace preceding a tilde so that simple programs which do not
78 perform any word separation get desired behaviour. */
79 static char *default_prefixes
[] =
80 { " ~", "\t~", (char *)NULL
};
82 /* The default value of tilde_additional_suffixes. This is set to
83 whitespace or newline so that simple programs which do not
84 perform any word separation get desired behaviour. */
85 static char *default_suffixes
[] =
86 { " ", "\n", (char *)NULL
};
88 /* If non-null, this contains the address of a function to call if the
89 standard meaning for expanding a tilde fails. The function is called
90 with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
91 which is the expansion, or a NULL pointer if there is no expansion. */
92 Function
*tilde_expansion_failure_hook
= (Function
*)NULL
;
94 /* When non-null, this is a NULL terminated array of strings which
95 are duplicates for a tilde prefix. Bash uses this to expand
97 char **tilde_additional_prefixes
= default_prefixes
;
99 /* When non-null, this is a NULL terminated array of strings which match
100 the end of a username, instead of just "/". Bash sets this to
102 char **tilde_additional_suffixes
= default_suffixes
;
104 /* Find the start of a tilde expansion in STRING, and return the index of
105 the tilde which starts the expansion. Place the length of the text
106 which identified this tilde starter in LEN, excluding the tilde itself. */
108 tilde_find_prefix (string
, len
)
112 register int i
, j
, string_len
;
113 register char **prefixes
= tilde_additional_prefixes
;
115 string_len
= strlen (string
);
118 if (!*string
|| *string
== '~')
123 for (i
= 0; i
< string_len
; i
++)
125 for (j
= 0; prefixes
[j
]; j
++)
127 if (strncmp (string
+ i
, prefixes
[j
], strlen (prefixes
[j
])) == 0)
129 *len
= strlen (prefixes
[j
]) - 1;
138 /* Find the end of a tilde expansion in STRING, and return the index of
139 the character which ends the tilde definition. */
141 tilde_find_suffix (string
)
144 register int i
, j
, string_len
;
145 register char **suffixes
= tilde_additional_suffixes
;
147 string_len
= strlen (string
);
149 for (i
= 0; i
< string_len
; i
++)
151 if (string
[i
] == '/' || !string
[i
])
154 for (j
= 0; suffixes
&& suffixes
[j
]; j
++)
156 if (strncmp (string
+ i
, suffixes
[j
], strlen (suffixes
[j
])) == 0)
163 /* Return a new string which is the result of tilde expanding STRING. */
165 tilde_expand (string
)
168 char *result
, *tilde_expand_word ();
169 int result_size
, result_index
;
171 result_size
= result_index
= 0;
172 result
= (char *)NULL
;
174 /* Scan through STRING expanding tildes as we come to them. */
177 register int start
, end
;
178 char *tilde_word
, *expansion
;
181 /* Make START point to the tilde which starts the expansion. */
182 start
= tilde_find_prefix (string
, &len
);
184 /* Copy the skipped text into the result. */
185 if ((result_index
+ start
+ 1) > result_size
)
186 result
= (char *)xrealloc (result
, 1 + (result_size
+= (start
+ 20)));
188 strncpy (result
+ result_index
, string
, start
);
189 result_index
+= start
;
191 /* Advance STRING to the starting tilde. */
194 /* Make END be the index of one after the last character of the
196 end
= tilde_find_suffix (string
);
198 /* If both START and END are zero, we are all done. */
202 /* Expand the entire tilde word, and copy it into RESULT. */
203 tilde_word
= (char *)xmalloc (1 + end
);
204 strncpy (tilde_word
, string
, end
);
205 tilde_word
[end
] = '\0';
208 expansion
= tilde_expand_word (tilde_word
);
211 len
= strlen (expansion
);
212 if ((result_index
+ len
+ 1) > result_size
)
213 result
= (char *)xrealloc (result
, 1 + (result_size
+= (len
+ 20)));
215 strcpy (result
+ result_index
, expansion
);
220 result
[result_index
] = '\0';
225 /* Do the work of tilde expansion on FILENAME. FILENAME starts with a
226 tilde. If there is no expansion, call tilde_expansion_failure_hook. */
228 tilde_expand_word (filename
)
233 dirname
= filename
? savestring (filename
) : (char *)NULL
;
235 if (dirname
&& *dirname
== '~')
238 if (!dirname
[1] || dirname
[1] == '/')
240 /* Prepend $HOME to the rest of the string. */
241 char *temp_home
= (char *)getenv ("HOME");
243 /* If there is no HOME variable, look up the directory in
244 the password database. */
247 struct passwd
*entry
;
249 entry
= getpwuid (getuid ());
251 temp_home
= entry
->pw_dir
;
254 temp_name
= (char *)alloca (1 + strlen (&dirname
[1])
255 + (temp_home
? strlen (temp_home
) : 0));
258 strcpy (temp_name
, temp_home
);
259 strcat (temp_name
, &dirname
[1]);
261 dirname
= savestring (temp_name
);
265 struct passwd
*user_entry
;
266 char *username
= (char *)alloca (257);
269 for (i
= 1; c
= dirname
[i
]; i
++)
276 username
[i
- 1] = '\0';
278 if (!(user_entry
= getpwnam (username
)))
280 /* If the calling program has a special syntax for
281 expanding tildes, and we couldn't find a standard
282 expansion, then let them try. */
283 if (tilde_expansion_failure_hook
)
288 (char *)(*tilde_expansion_failure_hook
) (username
);
292 temp_name
= (char *)alloca (1 + strlen (expansion
)
293 + strlen (&dirname
[i
]));
294 strcpy (temp_name
, expansion
);
295 strcat (temp_name
, &dirname
[i
]);
300 /* We shouldn't report errors. */
304 temp_name
= (char *)alloca (1 + strlen (user_entry
->pw_dir
)
305 + strlen (&dirname
[i
]));
306 strcpy (temp_name
, user_entry
->pw_dir
);
307 strcat (temp_name
, &dirname
[i
]);
310 dirname
= savestring (temp_name
);
327 char *result
, line
[512];
332 printf ("~expand: ");
336 strcpy (line
, "done");
338 if ((strcmp (line
, "done") == 0) ||
339 (strcmp (line
, "quit") == 0) ||
340 (strcmp (line
, "exit") == 0))
346 result
= tilde_expand (line
);
347 printf (" --> %s\n", result
);
353 static void memory_error_and_abort ();
359 char *temp
= (char *)malloc (bytes
);
362 memory_error_and_abort ();
367 xrealloc (pointer
, bytes
)
374 temp
= (char *)malloc (bytes
);
376 temp
= (char *)realloc (pointer
, bytes
);
379 memory_error_and_abort ();
385 memory_error_and_abort ()
387 fprintf (stderr
, "readline: Out of virtual memory!\n");
393 * compile-command: "gcc -g -DTEST -o tilde tilde.c"