Debian Codegraph @

lib/tilde.c

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