YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense alongwithReadline;seethefileCOPYING.Ifnot,writetotheFree
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA. */
#ifdefined (HAVE_CONFIG_H) # include "config_readline.h" #endif
#ifdefined (HAVE_UNISTD_H) # ifdef _MINIX # include <sys/types.h> # endif # include <unistd.h> #endif
#ifdefined (HAVE_STRING_H) # include <string.h> #else/* !HAVE_STRING_H */ # include <strings.h> #endif/* !HAVE_STRING_H */
#ifdefined (HAVE_STDLIB_H) # include <stdlib.h> #else # include "ansi_stdlib.h" #endif/* HAVE_STDLIB_H */
/* If being compiled as part of bash, these will be satisfied from variables.o.Ifbeingcompiledaspartofreadline,theywill
be satisfied from shell.o. */ externchar *sh_get_home_dir PARAMS((void)); externchar *sh_get_env_value PARAMS((constchar *));
/* The default value of tilde_additional_prefixes. This is set to whitespaceprecedingatildesothatsimpleprogramswhichdonot
perform any word separation get desired behaviour. */ staticconstchar *default_prefixes[] =
{ " ~", "\t~", (constchar *)NULL };
/* The default value of tilde_additional_suffixes. This is set to whitespaceornewlinesothatsimpleprogramswhichdonot
perform any word separation get desired behaviour. */ staticconstchar *default_suffixes[] =
{ " ", "\n", (constchar *)NULL };
/* If non-null, this contains the address of a function that the application wantscalledbeforetryingthestandardtildeexpansions.Thefunction iscalledwiththetextsanstilde,andreturnsamalloc()'edstring
which is the expansion, or a NULL pointer if the expansion fails. */
tilde_hook_func_t *tilde_expansion_preexpansion_hook = (tilde_hook_func_t *)NULL;
/* If non-null, this contains the address of a function to call if the standardmeaningforexpandingatildefails.Thefunctioniscalled withthetext(sanstilde,asin"foo"),andreturnsamalloc()'edstring
which is the expansion, or a NULL pointer if there is no expansion. */
tilde_hook_func_t *tilde_expansion_failure_hook = (tilde_hook_func_t *)NULL;
/* When non-null, this is a NULL terminated array of strings which areduplicatesforatildeprefix.Bashusesthistoexpand
`=~' and `:~'. */ char **tilde_additional_prefixes = (char **)default_prefixes;
/* When non-null, this is a NULL terminated array of strings which match theendofausername,insteadofjust"/".Bashsetsthisto
`:' and `=~'. */ char **tilde_additional_suffixes = (char **)default_suffixes;
/* Find the start of a tilde expansion in STRING, and return the index of thetildewhichstartstheexpansion.Placethelengthofthetext
which identified this tilde starter in LEN, excluding the tilde itself. */ staticint
tilde_find_prefix (string, len) constchar *string; int *len;
{ registerint i, j, string_len; registerchar **prefixes;
prefixes = tilde_additional_prefixes;
string_len = strlen (string);
*len = 0;
if (*string == '\0' || *string == '~') return (0);
if (prefixes)
{ for (i = 0; i < string_len; i++)
{ for (j = 0; prefixes[j]; j++)
{ if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
{
*len = strlen (prefixes[j]) - 1; return (i + *len);
}
}
}
} return (string_len);
}
/* Find the end of a tilde expansion in STRING, and return the index of
the character which ends the tilde definition. */ staticint
tilde_find_suffix (string) constchar *string;
{ registerint i, j, string_len; registerchar **suffixes;
for (i = 0; i < string_len; i++)
{ #ifdefined (__MSDOS__) if (string[i] == '/' || string[i] == '\\'/* || !string[i] */) #else if (string[i] == '/'/* || !string[i] */) #endif break;
for (j = 0; suffixes && suffixes[j]; j++)
{ if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0) return (i);
}
} return (i);
}
/* Return a new string which is the result of tilde expanding STRING. */ char *
tilde_expand (string) constchar *string;
{ char *result; int result_size, result_index;
/* Scan through STRING expanding tildes as we come to them. */ while (1)
{ registerint start, end; char *tilde_word, *expansion; int len;
/* Make START point to the tilde which starts the expansion. */
start = tilde_find_prefix (string, &len);
/* Copy the skipped text into the result. */ if ((result_index + start + 1) > result_size)
result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
len = strlen (expansion); #ifdef __CYGWIN__ /* Fix for Cygwin to prevent ~user/xxx from expanding to //xxx when
$HOME for `user' is /. On cygwin, // denotes a network drive. */ if (len > 1 || *expansion != '/' || *string != '/') #endif
{ if ((result_index + len + 1) > result_size)
result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
/* Take FNAME and return the tilde prefix we want expanded. If LENP is non-null,theindexoftheendoftheprefixintoFNAMEisreturnedin
the location it points to. */ staticchar *
isolate_tilde_prefix (fname, lenp) constchar *fname; int *lenp;
{ char *ret; int i;
ret = (char *)xmalloc (strlen (fname)); #ifdefined (__MSDOS__) for (i = 1; fname[i] && fname[i] != '/' && fname[i] != '\\'; i++) #else for (i = 1; fname[i] && fname[i] != '/'; i++) #endif
ret[i - 1] = fname[i];
ret[i - 1] = '\0'; if (lenp)
*lenp = i; return ret;
}
#if0 /* Public function to scan a string (FNAME) beginning with a tilde and find theportionofthestringthatshouldbepassedtothetildeexpansion function.Rightnow,itjustcallstilde_find_suffixandallocatesnew
memory, but it can be expanded to do different things later. */ char *
tilde_find_word (fname, flags, lenp) constchar *fname; int flags, *lenp;
{ int x; char *r;
x = tilde_find_suffix (fname); if (x == 0)
{
r = savestring (fname); if (lenp)
*lenp = 0;
} else
{
r = (char *)xmalloc (1 + x);
strncpy (r, fname, x);
r[x] = '\0'; if (lenp)
*lenp = x;
}
return r;
} #endif
/* Return a string that is PREFIX concatenated with SUFFIX starting at
SUFFIND. */ staticchar *
glue_prefix_and_suffix (prefix, suffix, suffind) char *prefix; constchar *suffix; int suffind;
{ char *ret; int plen, slen;
/* Do the work of tilde expansion on FILENAME. FILENAME starts with a tilde.Ifthereisnoexpansion,calltilde_expansion_failure_hook.
This always returns a newly-allocated string, never static storage. */ char *
tilde_expand_word (filename) constchar *filename;
{ char *dirname, *expansion, *username; int user_len; struct passwd *user_entry;
if (filename == 0) return ((char *)NULL);
if (*filename != '~') return (savestring (filename));
/* A leading `~/' or a bare `~' is *always* translated to the value of $HOMEorthehomedirectoryofthecurrentuser,regardlessofany
preexpansion hook. */ if (filename[1] == '\0' || filename[1] == '/')
{ /* Prefix $HOME to the rest of the string. */
expansion = sh_get_env_value ("HOME");
/* If there is no HOME variable, look up the directory in
the password database. */ if (expansion == 0)
expansion = sh_get_home_dir ();
/* No preexpansion hook, or the preexpansion hook failed. Look in the
password database. */
dirname = (char *)NULL; #ifdefined (HAVE_GETPWNAM)
user_entry = getpwnam (username); #else
user_entry = 0; #endif if (user_entry == 0)
{ /* If the calling program has a special syntax for expanding tildes,
and we couldn't find a standard expansion, then let them try. */ if (tilde_expansion_failure_hook)
{
expansion = (*tilde_expansion_failure_hook) (username); if (expansion)
{
dirname = glue_prefix_and_suffix (expansion, filename, user_len);
free (expansion);
}
} /* If we don't have a failure hook, or if the failure hook did not
expand the tilde, return a copy of what we were passed. */ if (dirname == 0)
dirname = savestring (filename);
} #ifdefined (HAVE_GETPWENT) else
dirname = glue_prefix_and_suffix (user_entry->pw_dir, filename, user_len); #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.