Debian Codegraph @

libsrc/readline/tilde.c

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