171
|
1 /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */ |
|
2 |
|
3 /* This file is part of GNU Info, a program for reading online documentation |
|
4 stored in Info format. |
|
5 |
|
6 This file has appeared in prior works by the Free Software Foundation; |
|
7 thus it carries copyright dates from 1988 through 1993. |
|
8 |
|
9 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software |
|
10 Foundation, Inc. |
|
11 |
|
12 This program is free software; you can redistribute it and/or modify |
|
13 it under the terms of the GNU General Public License as published by |
|
14 the Free Software Foundation; either version 2, or (at your option) |
|
15 any later version. |
|
16 |
|
17 This program is distributed in the hope that it will be useful, |
|
18 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 GNU General Public License for more details. |
|
21 |
|
22 You should have received a copy of the GNU General Public License |
|
23 along with this program; if not, write to the Free Software |
|
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
25 |
|
26 Written by Brian Fox (bfox@ai.mit.edu). */ |
|
27 |
1242
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
171
|
32 #if defined (__GNUC__) |
|
33 # define alloca __builtin_alloca |
|
34 #else /* !__GNUC__ */ |
|
35 # if defined (_AIX) |
|
36 #pragma alloca |
|
37 # else /* !_AIX */ |
|
38 # if defined (HAVE_ALLOCA_H) |
|
39 # include <alloca.h> |
|
40 # endif /* HAVE_ALLOCA_H */ |
|
41 # endif /* !AIX */ |
|
42 #endif /* !__GNUC__ */ |
|
43 |
|
44 #include "tilde.h" |
|
45 #include <pwd.h> |
|
46 |
|
47 #if !defined (savestring) |
|
48 #define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x)) |
|
49 #endif /* !savestring */ |
|
50 |
|
51 #if !defined (NULL) |
|
52 # define NULL 0x0 |
|
53 #endif |
|
54 |
|
55 #if defined (TEST) || defined (STATIC_MALLOC) |
|
56 static char *xmalloc (), *xrealloc (); |
|
57 #else |
|
58 extern char *xmalloc (), *xrealloc (); |
|
59 #endif /* TEST || STATIC_MALLOC */ |
|
60 |
|
61 /* The default value of tilde_additional_prefixes. This is set to |
|
62 whitespace preceding a tilde so that simple programs which do not |
|
63 perform any word separation get desired behaviour. */ |
|
64 static char *default_prefixes[] = |
|
65 { " ~", "\t~", (char *)NULL }; |
|
66 |
|
67 /* The default value of tilde_additional_suffixes. This is set to |
|
68 whitespace or newline so that simple programs which do not |
|
69 perform any word separation get desired behaviour. */ |
|
70 static char *default_suffixes[] = |
|
71 { " ", "\n", (char *)NULL }; |
|
72 |
|
73 /* If non-null, this contains the address of a function to call if the |
|
74 standard meaning for expanding a tilde fails. The function is called |
|
75 with the text (sans tilde, as in "foo"), and returns a malloc()'ed string |
|
76 which is the expansion, or a NULL pointer if there is no expansion. */ |
|
77 Function *tilde_expansion_failure_hook = (Function *)NULL; |
|
78 |
|
79 /* When non-null, this is a NULL terminated array of strings which |
|
80 are duplicates for a tilde prefix. Bash uses this to expand |
|
81 `=~' and `:~'. */ |
|
82 char **tilde_additional_prefixes = default_prefixes; |
|
83 |
|
84 /* When non-null, this is a NULL terminated array of strings which match |
|
85 the end of a username, instead of just "/". Bash sets this to |
|
86 `:' and `=~'. */ |
|
87 char **tilde_additional_suffixes = default_suffixes; |
|
88 |
|
89 /* Find the start of a tilde expansion in STRING, and return the index of |
|
90 the tilde which starts the expansion. Place the length of the text |
|
91 which identified this tilde starter in LEN, excluding the tilde itself. */ |
|
92 static int |
|
93 tilde_find_prefix (string, len) |
|
94 char *string; |
|
95 int *len; |
|
96 { |
|
97 register int i, j, string_len; |
|
98 register char **prefixes = tilde_additional_prefixes; |
|
99 |
|
100 string_len = strlen (string); |
|
101 *len = 0; |
|
102 |
|
103 if (!*string || *string == '~') |
|
104 return (0); |
|
105 |
|
106 if (prefixes) |
|
107 { |
|
108 for (i = 0; i < string_len; i++) |
|
109 { |
|
110 for (j = 0; prefixes[j]; j++) |
|
111 { |
|
112 if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0) |
|
113 { |
|
114 *len = strlen (prefixes[j]) - 1; |
|
115 return (i + *len); |
|
116 } |
|
117 } |
|
118 } |
|
119 } |
|
120 return (string_len); |
|
121 } |
|
122 |
|
123 /* Find the end of a tilde expansion in STRING, and return the index of |
|
124 the character which ends the tilde definition. */ |
|
125 static int |
|
126 tilde_find_suffix (string) |
|
127 char *string; |
|
128 { |
|
129 register int i, j, string_len; |
|
130 register char **suffixes = tilde_additional_suffixes; |
|
131 |
|
132 string_len = strlen (string); |
|
133 |
|
134 for (i = 0; i < string_len; i++) |
|
135 { |
|
136 if (string[i] == '/' || !string[i]) |
|
137 break; |
|
138 |
|
139 for (j = 0; suffixes && suffixes[j]; j++) |
|
140 { |
|
141 if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0) |
|
142 return (i); |
|
143 } |
|
144 } |
|
145 return (i); |
|
146 } |
|
147 |
|
148 /* Return a new string which is the result of tilde expanding STRING. */ |
|
149 char * |
|
150 tilde_expand (string) |
|
151 char *string; |
|
152 { |
|
153 char *result, *tilde_expand_word (); |
|
154 int result_size, result_index; |
|
155 |
|
156 result_size = result_index = 0; |
|
157 result = (char *)NULL; |
|
158 |
|
159 /* Scan through STRING expanding tildes as we come to them. */ |
|
160 while (1) |
|
161 { |
|
162 register int start, end; |
|
163 char *tilde_word, *expansion; |
|
164 int len; |
|
165 |
|
166 /* Make START point to the tilde which starts the expansion. */ |
|
167 start = tilde_find_prefix (string, &len); |
|
168 |
|
169 /* Copy the skipped text into the result. */ |
|
170 if ((result_index + start + 1) > result_size) |
|
171 result = (char *)xrealloc (result, 1 + (result_size += (start + 20))); |
|
172 |
|
173 strncpy (result + result_index, string, start); |
|
174 result_index += start; |
|
175 |
|
176 /* Advance STRING to the starting tilde. */ |
|
177 string += start; |
|
178 |
|
179 /* Make END be the index of one after the last character of the |
|
180 username. */ |
|
181 end = tilde_find_suffix (string); |
|
182 |
|
183 /* If both START and END are zero, we are all done. */ |
|
184 if (!start && !end) |
|
185 break; |
|
186 |
|
187 /* Expand the entire tilde word, and copy it into RESULT. */ |
|
188 tilde_word = (char *)xmalloc (1 + end); |
|
189 strncpy (tilde_word, string, end); |
|
190 tilde_word[end] = '\0'; |
|
191 string += end; |
|
192 |
|
193 expansion = tilde_expand_word (tilde_word); |
|
194 free (tilde_word); |
|
195 |
|
196 len = strlen (expansion); |
|
197 if ((result_index + len + 1) > result_size) |
|
198 result = (char *)xrealloc (result, 1 + (result_size += (len + 20))); |
|
199 |
|
200 strcpy (result + result_index, expansion); |
|
201 result_index += len; |
|
202 free (expansion); |
|
203 } |
|
204 |
|
205 result[result_index] = '\0'; |
|
206 |
|
207 return (result); |
|
208 } |
|
209 |
|
210 /* Do the work of tilde expansion on FILENAME. FILENAME starts with a |
|
211 tilde. If there is no expansion, call tilde_expansion_failure_hook. */ |
|
212 char * |
|
213 tilde_expand_word (filename) |
|
214 char *filename; |
|
215 { |
|
216 char *dirname; |
|
217 |
|
218 dirname = filename ? savestring (filename) : (char *)NULL; |
|
219 |
|
220 if (dirname && *dirname == '~') |
|
221 { |
|
222 char *temp_name; |
|
223 if (!dirname[1] || dirname[1] == '/') |
|
224 { |
|
225 /* Prepend $HOME to the rest of the string. */ |
|
226 char *temp_home = (char *)getenv ("HOME"); |
|
227 |
|
228 /* If there is no HOME variable, look up the directory in |
|
229 the password database. */ |
|
230 if (!temp_home) |
|
231 { |
|
232 extern struct passwd *getpwuid (); |
|
233 struct passwd *entry; |
|
234 |
|
235 entry = getpwuid (getuid ()); |
|
236 if (entry) |
|
237 temp_home = entry->pw_dir; |
|
238 } |
|
239 |
|
240 temp_name = (char *)alloca (1 + strlen (&dirname[1]) |
|
241 + (temp_home ? strlen (temp_home) : 0)); |
|
242 temp_name[0] = '\0'; |
|
243 if (temp_home) |
|
244 strcpy (temp_name, temp_home); |
|
245 strcat (temp_name, &dirname[1]); |
|
246 free (dirname); |
|
247 dirname = savestring (temp_name); |
|
248 } |
|
249 else |
|
250 { |
|
251 struct passwd *getpwnam (), *user_entry; |
|
252 char *username = (char *)alloca (257); |
|
253 int i, c; |
|
254 |
|
255 for (i = 1; c = dirname[i]; i++) |
|
256 { |
|
257 if (c == '/') |
|
258 break; |
|
259 else |
|
260 username[i - 1] = c; |
|
261 } |
|
262 username[i - 1] = '\0'; |
|
263 |
|
264 if (!(user_entry = getpwnam (username))) |
|
265 { |
|
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) |
|
270 { |
|
271 char *expansion; |
|
272 |
|
273 expansion = |
|
274 (char *)(*tilde_expansion_failure_hook) (username); |
|
275 |
|
276 if (expansion) |
|
277 { |
|
278 temp_name = (char *)alloca (1 + strlen (expansion) |
|
279 + strlen (&dirname[i])); |
|
280 strcpy (temp_name, expansion); |
|
281 strcat (temp_name, &dirname[i]); |
|
282 free (expansion); |
|
283 goto return_name; |
|
284 } |
|
285 } |
|
286 /* We shouldn't report errors. */ |
|
287 } |
|
288 else |
|
289 { |
|
290 temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir) |
|
291 + strlen (&dirname[i])); |
|
292 strcpy (temp_name, user_entry->pw_dir); |
|
293 strcat (temp_name, &dirname[i]); |
|
294 return_name: |
|
295 free (dirname); |
|
296 dirname = savestring (temp_name); |
|
297 } |
|
298 endpwent (); |
|
299 } |
|
300 } |
|
301 return (dirname); |
|
302 } |
|
303 |
|
304 |
|
305 #if defined (TEST) |
|
306 #undef NULL |
|
307 #include <stdio.h> |
|
308 |
|
309 main (argc, argv) |
|
310 int argc; |
|
311 char **argv; |
|
312 { |
|
313 char *result, line[512]; |
|
314 int done = 0; |
|
315 |
|
316 while (!done) |
|
317 { |
|
318 printf ("~expand: "); |
|
319 fflush (stdout); |
|
320 |
|
321 if (!gets (line)) |
|
322 strcpy (line, "done"); |
|
323 |
|
324 if ((strcmp (line, "done") == 0) || |
|
325 (strcmp (line, "quit") == 0) || |
|
326 (strcmp (line, "exit") == 0)) |
|
327 { |
|
328 done = 1; |
|
329 break; |
|
330 } |
|
331 |
|
332 result = tilde_expand (line); |
|
333 printf (" --> %s\n", result); |
|
334 free (result); |
|
335 } |
|
336 exit (0); |
|
337 } |
|
338 |
|
339 static void memory_error_and_abort (); |
|
340 |
|
341 static char * |
|
342 xmalloc (bytes) |
|
343 int bytes; |
|
344 { |
|
345 char *temp = (char *)malloc (bytes); |
|
346 |
|
347 if (!temp) |
|
348 memory_error_and_abort (); |
|
349 return (temp); |
|
350 } |
|
351 |
|
352 static char * |
|
353 xrealloc (pointer, bytes) |
|
354 char *pointer; |
|
355 int bytes; |
|
356 { |
|
357 char *temp; |
|
358 |
|
359 if (!pointer) |
|
360 temp = (char *)malloc (bytes); |
|
361 else |
|
362 temp = (char *)realloc (pointer, bytes); |
|
363 |
|
364 if (!temp) |
|
365 memory_error_and_abort (); |
|
366 |
|
367 return (temp); |
|
368 } |
|
369 |
|
370 static void |
|
371 memory_error_and_abort () |
|
372 { |
|
373 fprintf (stderr, "readline: Out of virtual memory!\n"); |
|
374 abort (); |
|
375 } |
|
376 |
|
377 /* |
|
378 * Local variables: |
|
379 * compile-command: "gcc -g -DTEST -o tilde tilde.c" |
|
380 * end: |
|
381 */ |
|
382 #endif /* TEST */ |
|
383 |