Debian Codegraph @

lib/tilde.c

raw symbols ◀ ▶ SCIP
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 (int);
52 # ifndef strcpy
•53 extern char *strcpy (char *, const char *);
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 (int), *xrealloc (char *, int);
68 #else
•69 extern char *xmalloc (int), *xrealloc (char *, int);
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 (char *string, 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 (char *string)
136 {
137 register int i, j, string_len;
138 register char **suffixes = tilde_additional_suffixes;
139
140 string_len = strlen (string);
141
142 for (i = 0; i < string_len; i++)
143 {
144 if (string[i] == '/' || !string[i])
145 break;
146
147 for (j = 0; suffixes && suffixes[j]; j++)
148 {
149 if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
150 return (i);
151 }
152 }
153 return (i);
154 }
155
156 /* Return a new string which is the result of tilde expanding STRING. */
157 char *
•158 tilde_expand (char *string)
159 {
•160 char *result, *tilde_expand_word (char *);
161 int result_size, result_index;
162
163 result_size = result_index = 0;
164 result = (char *)NULL;
165
166 /* Scan through STRING expanding tildes as we come to them. */
167 while (1)
168 {
169 register int start, end;
170 char *tilde_word, *expansion;
171 int len;
172
173 /* Make START point to the tilde which starts the expansion. */
174 start = tilde_find_prefix (string, &len);
175
176 /* Copy the skipped text into the result. */
177 if ((result_index + start + 1) > result_size)
178 result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
179
180 strncpy (result + result_index, string, start);
181 result_index += start;
182
183 /* Advance STRING to the starting tilde. */
184 string += start;
185
186 /* Make END be the index of one after the last character of the
187 username. */
188 end = tilde_find_suffix (string);
189
190 /* If both START and END are zero, we are all done. */
191 if (!start && !end)
192 break;
193
194 /* Expand the entire tilde word, and copy it into RESULT. */
195 tilde_word = (char *)xmalloc (1 + end);
196 strncpy (tilde_word, string, end);
197 tilde_word[end] = '\0';
198 string += end;
199
200 expansion = tilde_expand_word (tilde_word);
201 free (tilde_word);
202
203 len = strlen (expansion);
204 if ((result_index + len + 1) > result_size)
205 result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
206
207 strcpy (result + result_index, expansion);
208 result_index += len;
209 free (expansion);
210 }
211
212 result[result_index] = '\0';
213
214 return (result);
215 }
216
217 /* Do the work of tilde expansion on FILENAME. FILENAME starts with a
218 tilde. If there is no expansion, call tilde_expansion_failure_hook. */
219 char *
•220 tilde_expand_word (char *filename)
221 {
222 char *dirname;
223
224 dirname = filename ? savestring (filename) : (char *)NULL;
225
226 if (dirname && *dirname == '~')
227 {
228 char *temp_name;
229 if (!dirname[1] || dirname[1] == '/')
230 {
231 /* Prepend $HOME to the rest of the string. */
232 char *temp_home = (char *)getenv ("HOME");
233
234 /* If there is no HOME variable, look up the directory in
235 the password database. */
236 if (!temp_home)
237 {
238 struct passwd *entry;
239
240 entry = getpwuid (getuid ());
241 if (entry)
242 temp_home = entry->pw_dir;
243 }
244
245 temp_name = xmalloc (1 + strlen (&dirname[1])
246 + (temp_home ? strlen (temp_home) : 0));
247 temp_name[0] = '\0';
248 if (temp_home)
249 strcpy (temp_name, temp_home);
250 strcat (temp_name, dirname + 1);
251 free (dirname);
252 dirname = temp_name;
253 }
254 else
255 {
256 char *username;
257 struct passwd *user_entry;
258 int i;
259
260 username = xmalloc (strlen (dirname));
261 for (i = 1; dirname[i] && dirname[i] != '/'; i++)
262 username[i - 1] = dirname[i];
263 username[i - 1] = '\0';
264
265 if ((user_entry = getpwnam (username)) == 0)
266 {
267 /* If the calling program has a special syntax for
268 expanding tildes, and we couldn't find a standard
269 expansion, then let them try. */
270 if (tilde_expansion_failure_hook)
271 {
272 char *expansion;
273
274 expansion = (*tilde_expansion_failure_hook) (username);
275
276 if (expansion)
277 {
278 temp_name = xmalloc (1 + strlen (expansion)
279 + strlen (&dirname[i]));
280 strcpy (temp_name, expansion);
281 strcat (temp_name, &dirname[i]);
282 free (expansion);
283 free (dirname);
284 dirname = temp_name;
285 }
286 }
287 /* We shouldn't report errors. */
288 }
289 else
290 {
291 temp_name = xmalloc (1 + strlen (user_entry->pw_dir)
292 + strlen (&dirname[i]));
293 strcpy (temp_name, user_entry->pw_dir);
294 strcat (temp_name, &dirname[i]);
295 free (dirname);
296 dirname = temp_name;
297 }
298 endpwent ();
299 free (username);
300 }
301 }
302 return (dirname);
303 }
304
305
306 #if defined (TEST)
307 #undef NULL
308 #include <stdio.h>
309
•310 main (int argc, char **argv)
311 {
312 char *result, line[512];
313 int done = 0;
314
315 while (!done)
316 {
317 printf ("~expand: ");
318 fflush (stdout);
319
320 if (!gets (line))
321 strcpy (line, "done");
322
323 if ((strcmp (line, "done") == 0) ||
324 (strcmp (line, "quit") == 0) ||
325 (strcmp (line, "exit") == 0))
326 {
327 done = 1;
328 break;
329 }
330
331 result = tilde_expand (line);
332 printf (" --> %s\n", result);
333 free (result);
334 }
335 exit (0);
336 }
337
338 static void memory_error_and_abort ();
339
340 static char *
•341 xmalloc (int bytes)
342 {
343 char *temp = (char *)malloc (bytes);
344
345 if (!temp)
346 memory_error_and_abort ();
347 return (temp);
348 }
349
350 static char *
•351 xrealloc (char *pointer, int bytes)
352 {
353 char *temp;
354
355 if (!pointer)
356 temp = (char *)malloc (bytes);
357 else
358 temp = (char *)realloc (pointer, bytes);
359
360 if (!temp)
361 memory_error_and_abort ();
362
363 return (temp);
364 }
365
366 static void
367 memory_error_and_abort ()
368 {
369 fprintf (stderr, "readline: Out of virtual memory!\n");
370 abort ();
371 }
372
373 /*
374 * Local variables:
375 * compile-command: "gcc -g -DTEST -o tilde tilde.c"
376 * end:
377 */
378 #endif /* TEST */