| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
| 31 |
# include <strings.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#if defined (HAVE_STDLIB_H) |
| 35 |
# include <stdlib.h> |
| 36 |
#else |
| 37 |
# include "ansi_stdlib.h" |
| 38 |
#endif |
| 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 |
| 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 |
| 57 |
|
| 58 |
#if !defined (NULL) |
| 59 |
# if defined (__STDC__) |
| 60 |
# define NULL ((void *) 0) |
| 61 |
# else |
| 62 |
# define NULL 0x0 |
| 63 |
# endif |
| 64 |
#endif |
| 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 |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static char *default_prefixes[] = |
| 76 |
{ " ~", "\t~", (char *)NULL }; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static char *default_suffixes[] = |
| 82 |
{ " ", "\n", (char *)NULL }; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
CPFunction *tilde_expansion_failure_hook = (CPFunction *)NULL; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
char **tilde_additional_prefixes = default_prefixes; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
char **tilde_additional_suffixes = default_suffixes; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 133 |
|
| 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 |
|
| 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 |
|
| 167 |
while (1) |
| 168 |
{ |
| 169 |
register int start, end; |
| 170 |
char *tilde_word, *expansion; |
| 171 |
int len; |
| 172 |
|
| 173 |
|
| 174 |
start = tilde_find_prefix (string, &len); |
| 175 |
|
| 176 |
|
| 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 |
|
| 184 |
string += start; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
end = tilde_find_suffix (string); |
| 189 |
|
| 190 |
|
| 191 |
if (!start && !end) |
| 192 |
break; |
| 193 |
|
| 194 |
|
| 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 |
|
| 218 |
|
| 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 |
|
| 232 |
char *temp_home = (char *)getenv ("HOME"); |
| 233 |
|
| 234 |
|
| 235 |
|
| 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 |
|
| 268 |
|
| 269 |
|
| 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 |
|
| 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 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
#endif |