Debian Codegraph @

lib/tilde.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* $Id: tilde.c,v 1.3 1999/03/10 07:57:53 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27
28 #if defined (HAVE_STRING_H)
29 # include <string.h>
30 #else /* !HAVE_STRING_H */
31 # include <strings.h>
32 #endif /* !HAVE_STRING_H */
33
34 #if defined (HAVE_STDLIB_H)
35 # include <stdlib.h>
36 #else
37 # include "ansi_stdlib.h"
38 #endif /* HAVE_STDLIB_H */
39
40 #include "tilde.h"
41 #include <sys/types.h>
42 #include <pwd.h>
43 #include <unistd.h>
44 #include <sys/types.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 *username;
262 struct passwd *user_entry;
263 int i;
264
265 username = xmalloc (strlen (dirname));
266 for (i = 1; dirname[i] && dirname[i] != '/'; i++)
267 username[i - 1] = dirname[i];
268 username[i - 1] = '\0';
269
270 if ((user_entry = getpwnam (username)) == 0)
271 {
272 /* If the calling program has a special syntax for
273 expanding tildes, and we couldn't find a standard
274 expansion, then let them try. */
275 if (tilde_expansion_failure_hook)
276 {
277 char *expansion;
278
279 expansion = (*tilde_expansion_failure_hook) (username);
280
281 if (expansion)
282 {
283 temp_name = xmalloc (1 + strlen (expansion)
284 + strlen (&dirname[i]));
285 strcpy (temp_name, expansion);
286 strcat (temp_name, &dirname[i]);
287 free (expansion);
288 free (dirname);
289 dirname = temp_name;
290 }
291 }
292 /* We shouldn't report errors. */
293 }
294 else
295 {
296 temp_name = xmalloc (1 + strlen (user_entry->pw_dir)
297 + strlen (&dirname[i]));
298 strcpy (temp_name, user_entry->pw_dir);
299 strcat (temp_name, &dirname[i]);
300 free (dirname);
301 dirname = temp_name;
302 }
303 endpwent ();
304 free (username);
305 }
306 }
307 return (dirname);
308 }
309
310
311 #if defined (TEST)
312 #undef NULL
313 #include <stdio.h>
314
315 main (argc, argv)
316 int argc;
317 char **argv;
318 {
319 char *result, line[512];
320 int done = 0;
321
322 while (!done)
323 {
324 printf ("~expand: ");
325 fflush (stdout);
326
327 if (!gets (line))
328 strcpy (line, "done");
329
330 if ((strcmp (line, "done") == 0) ||
331 (strcmp (line, "quit") == 0) ||
332 (strcmp (line, "exit") == 0))
333 {
334 done = 1;
335 break;
336 }
337
338 result = tilde_expand (line);
339 printf (" --> %s\n", result);
340 free (result);
341 }
342 exit (0);
343 }
344
345 static void memory_error_and_abort ();
346
347 static char *
348 xmalloc (bytes)
349 int bytes;
350 {
351 char *temp = (char *)malloc (bytes);
352
353 if (!temp)
354 memory_error_and_abort ();
355 return (temp);
356 }
357
358 static char *
359 xrealloc (pointer, bytes)
360 char *pointer;
361 int bytes;
362 {
363 char *temp;
364
365 if (!pointer)
366 temp = (char *)malloc (bytes);
367 else
368 temp = (char *)realloc (pointer, bytes);
369
370 if (!temp)
371 memory_error_and_abort ();
372
373 return (temp);
374 }
375
376 static void
377 memory_error_and_abort ()
378 {
379 fprintf (stderr, "readline: Out of virtual memory!\n");
380 abort ();
381 }
382
383 /*
384 * Local variables:
385 * compile-command: "gcc -g -DTEST -o tilde tilde.c"
386 * end:
387 */
388 #endif /* TEST */