1 /* alias.c -- Not a full alias, but just the kind that we use in the
2 shell. Csh style alias is somewhere else (`over there, in a box'). */
4 /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
13 Bash is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash; see the file COPYING. If not, write to the Free
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
30 static int qsort_alias_compare ();
32 /* Non-zero means expand all words on the line. Otherwise, expand
33 after first expansion if the expansion ends in a space. */
34 int alias_expand_all = 0;
36 /* The list of aliases that we have. */
37 HASH_TABLE *aliases = (HASH_TABLE *)NULL;
43 aliases = make_hash_table (0);
46 /* Scan the list of aliases looking for one with NAME. Return NULL
47 if the alias doesn't exist, else a pointer to the assoc. */
55 return ((ASSOC *)NULL);
57 al = find_hash_item (name, aliases);
60 return ((ASSOC *)al->data);
62 return ((ASSOC *)NULL);
65 /* Return the value of the alias for NAME, or NULL if there is none. */
67 get_alias_value (name)
70 ASSOC *alias = find_alias (name);
72 return (alias->value);
74 return ((char *)NULL);
77 /* Make a new alias from NAME and VALUE. If NAME can be found,
78 then replace its value. */
80 add_alias (name, value)
83 ASSOC *temp = (ASSOC *)NULL;
86 initialize_aliases ();
88 temp = find_alias (name);
93 temp->value = savestring (value);
99 temp = (ASSOC *)xmalloc (sizeof (ASSOC));
100 temp->name = savestring (name);
101 temp->value = savestring (value);
103 elt = add_hash_item (savestring (name), aliases);
104 elt->data = (char *)temp;
108 /* Remove the alias with name NAME from the alias table. Returns
109 the number of aliases left in the table, or -1 if the alias didn't
115 BUCKET_CONTENTS *elt;
120 elt = remove_hash_item (name, aliases);
125 t = (ASSOC *)elt->data;
128 free (elt->key); /* alias name */
131 return (aliases->nentries);
136 /* Delete a hash bucket chain of aliases. */
138 delete_alias_list (alias_list)
139 BUCKET_CONTENTS *alias_list;
141 register BUCKET_CONTENTS *bp, *temp;
144 for (bp = alias_list; bp; )
147 a = (ASSOC *)bp->data;
157 /* Delete all aliases. */
159 delete_all_aliases ()
166 for (i = 0; i < aliases->nbuckets; i++)
168 register BUCKET_CONTENTS *bp;
170 bp = get_hash_bucket (i, aliases);
171 delete_alias_list (bp);
174 aliases = (HASH_TABLE *)NULL;
177 /* Return an array of aliases that satisfy the conditions tested by FUNCTION.
178 If FUNCTION is NULL, return all aliases. */
180 map_over_aliases (function)
184 register BUCKET_CONTENTS *tlist;
185 ASSOC *alias, **list = (ASSOC **)NULL;
186 int list_index = 0, list_size = 0;
188 for (i = 0; i < aliases->nbuckets; i++)
190 tlist = get_hash_bucket (i, aliases);
194 alias = (ASSOC *)tlist->data;
196 if (!function || (*function) (alias))
198 if (list_index + 1 >= list_size)
200 xrealloc ((char *)list, (list_size += 20) * sizeof (ASSOC *));
202 list[list_index++] = alias;
203 list[list_index] = (ASSOC *)NULL;
215 qsort (array, array_len ((char **)array), sizeof (ASSOC *), qsort_alias_compare);
219 qsort_alias_compare (as1, as2)
224 if ((result = (*as1)->name[0] - (*as2)->name[0]) == 0)
225 result = strcmp ((*as1)->name, (*as2)->name);
230 /* Return a sorted list of all defined aliases */
237 return ((ASSOC **)NULL);
239 list = map_over_aliases ((Function *)NULL);
246 alias_expand_word (s)
249 ASSOC *r = find_alias (s);
252 return (savestring (r->value));
254 return ((char *)NULL);
257 /* Return non-zero if CHARACTER is a member of the class of characters
258 that are self-delimiting in the shell (this really means that these
259 characters delimit tokens). */
260 #define self_delimiting(character) (member ((character), " \t\n\r;|&()"))
262 /* Return non-zero if CHARACTER is a member of the class of characters
263 that delimit commands in the shell. */
264 #define command_separator(character) (member ((character), "\r\n;|&("))
266 /* If this is 1, we are checking the next token read for alias expansion
267 because it is the first word in a command. */
268 static int command_word;
270 /* This is for skipping quoted strings in alias expansions. */
271 #define quote_char(c) (((c) == '\'') || ((c) == '"'))
273 /* Consume a quoted string from STRING, starting at string[START] (so
274 string[START] is the opening quote character), and return the index
275 of the closing quote character matching the opening quote character.
276 This handles single matching pairs of unquoted quotes; it could afford
277 to be a little smarter... This skips words between balanced pairs of
278 quotes, words where the first character is quoted with a `\', and other
279 backslash-escaped characters. */
282 skipquotes (string, start)
287 int delimiter = string[start];
289 /* i starts at START + 1 because string[START] is the opening quote
291 for (i = start + 1 ; string[i] ; i++)
293 if (string[i] == '\\')
295 i++; /* skip backslash-quoted quote characters, too */
299 if (string[i] == delimiter)
305 /* Skip the white space and any quoted characters in STRING, starting at
306 START. Return the new index into STRING, after zero or more characters
307 have been skipped. */
309 skipws (string, start)
314 int pass_next, backslash_quoted_word, peekc;
316 /* skip quoted strings, in ' or ", and words in which a character is quoted
318 backslash_quoted_word = pass_next = 0;
320 /* Skip leading whitespace (or separator characters), and quoted words.
321 But save it in the output. */
323 for (i = start; string[i]; i++)
331 if (whitespace (string[i]))
333 backslash_quoted_word = 0; /* we are no longer in a backslash-quoted word */
337 if (string[i] == '\\')
340 if (isletter (peekc))
341 backslash_quoted_word++; /* this is a backslash-quoted word */
347 /* This only handles single pairs of non-escaped quotes. This
348 overloads backslash_quoted_word to also mean that a word like
349 ""f is being scanned, so that the quotes will inhibit any expansion
351 if (quote_char(string[i]))
353 i = skipquotes (string, i);
354 /* This could be a line that contains a single quote character,
355 in which case skipquotes () terminates with string[i] == '\0'
356 (the end of the string). Check for that here. */
357 if (string[i] == '\0')
360 peekc = string[i + 1];
361 if (isletter (peekc))
362 backslash_quoted_word++;
366 /* If we're in the middle of some kind of quoted word, let it
368 if (backslash_quoted_word)
371 /* If this character is a shell command separator, then set a hint for
372 alias_expand that the next token is the first word in a command. */
374 if (command_separator (string[i]))
384 /* Characters that may appear in a token. Basically, anything except white
385 space and a token separator. */
386 #define token_char(c) (!((whitespace (string[i]) || self_delimiting (string[i]))))
388 /* Read from START in STRING until the next separator character, and return
389 the index of that separator. Skip backslash-quoted characters. Call
390 skipquotes () for quoted strings in the middle or at the end of tokens,
391 so all characters show up (e.g. foo'' and foo""bar) */
393 rd_token (string, start)
399 /* From here to next separator character is a token. */
400 for (i = start; string[i] && token_char (string[i]); i++)
402 if (string[i] == '\\')
404 i++; /* skip backslash-escaped character */
408 /* If this character is a quote character, we want to call skipquotes
409 to get the whole quoted portion as part of this word. That word
410 will not generally match an alias, even if te unquoted word would
411 have. The presence of the quotes in the token serves then to
412 inhibit expansion. */
413 if (quote_char (string[i]))
415 i = skipquotes (string, i);
416 /* Now string[i] is the matching quote character, and the
417 quoted portion of the token has been scanned. */
424 /* Return a new line, with any aliases substituted. */
426 alias_expand (string)
429 int line_len = 1 + strlen (string);
430 char *line = (char *)xmalloc (line_len);
431 register int i, j, start;
432 char *token = xmalloc (line_len);
433 int tl, real_start, expand_next, expand_this_token;
438 command_word = 1; /* initialized to expand the first word on the line */
440 /* Each time through the loop we find the next word in line. If it
441 has an alias, substitute
442 the alias value. If the value ends in ` ', then try again
443 with the next word. Else, if there is no value, or if
444 the value does not end in space, we are done. */
452 /* Skip white space and quoted characters */
453 i = skipws (string, start);
455 if (start == i && string[i] == '\0')
461 /* copy the just-skipped characters into the output string,
462 expanding it if there is not enough room. */
464 tl = i - start; /* number of characters just skipped */
465 if (1 + j + tl >= line_len)
466 line = (char *)xrealloc (line, line_len += (50 + tl));
467 strncpy (line + j, string + start, tl);
472 command_word = command_word || (command_separator (string[i]));
473 expand_this_token = (command_word || expand_next);
476 /* Read the next token, and copy it into TOKEN. */
478 i = rd_token (string, start);
480 tl = i - start; /* token length */
482 /* If tl == 0, but we're not at the end of the string, then we have a
483 single-character token, probably a delimiter */
484 if (tl == 0 && string[i] != '\0')
487 i++; /* move past it */
490 strncpy (token, string + start, tl);
493 /* If there is a backslash-escaped character quoted in TOKEN,
494 then we don't do alias expansion. This should check for all
495 other quoting characters, too. */
496 if (strchr (token, '\\'))
497 expand_this_token = 0;
499 /* If we should be expanding here, if we are expanding all words, or if
500 we are in a location in the string where an expansion is supposed to
501 take place, see if this word has a substitution. If it does, then do
502 the expansion. Note that we defer the alias value lookup until we
503 are sure we are expanding this token. */
506 (expand_this_token || alias_expand_all) &&
507 (alias = find_alias (token)))
509 char *v = alias->value;
512 /* +3 because we possibly add one more character below. */
513 if ((l + 3) > line_len - (int)strlen (line))
514 line = (char *)xrealloc (line, line_len += (50 + l));
518 if ((expand_this_token && l && whitespace (v[l - 1])) ||
524 int ll = strlen (line);
525 int tlen = i - real_start; /* tlen == strlen(token) */
527 if (ll + tlen + 2 > line_len)
528 line = (char *)xrealloc (line, line_len += 50 + ll + tlen);
530 strncpy (line + ll, string + real_start, tlen);
531 line[ll + tlen] = '\0';