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-2009 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash 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.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
26 #if defined (HAVE_UNISTD_H)
28 # include <sys/types.h>
34 #include "chartypes.h"
41 #if defined (PROGRAMMABLE_COMPLETION)
42 # include "pcomplete.h"
45 #define ALIAS_HASH_BUCKETS 16 /* must be power of two */
47 typedef int sh_alias_map_func_t
__P((alias_t
*));
49 static void free_alias_data
__P((PTR_T
));
50 static alias_t
**map_over_aliases
__P((sh_alias_map_func_t
*));
51 static void sort_aliases
__P((alias_t
**));
52 static int qsort_alias_compare
__P((alias_t
**, alias_t
**));
54 #if defined (READLINE)
55 static int skipquotes
__P((char *, int));
56 static int skipws
__P((char *, int));
57 static int rd_token
__P((char *, int));
60 /* Non-zero means expand all words on the line. Otherwise, expand
61 after first expansion if the expansion ends in a space. */
62 int alias_expand_all
= 0;
64 /* The list of aliases that we have. */
65 HASH_TABLE
*aliases
= (HASH_TABLE
*)NULL
;
71 aliases
= hash_create (ALIAS_HASH_BUCKETS
);
74 /* Scan the list of aliases looking for one with NAME. Return NULL
75 if the alias doesn't exist, else a pointer to the alias_t. */
83 return ((alias_t
*)NULL
);
85 al
= hash_search (name
, aliases
, 0);
86 return (al
? (alias_t
*)al
->data
: (alias_t
*)NULL
);
89 /* Return the value of the alias for NAME, or NULL if there is none. */
91 get_alias_value (name
)
97 return ((char *)NULL
);
99 alias
= find_alias (name
);
100 return (alias
? alias
->value
: (char *)NULL
);
103 /* Make a new alias from NAME and VALUE. If NAME can be found,
104 then replace its value. */
106 add_alias (name
, value
)
109 BUCKET_CONTENTS
*elt
;
115 initialize_aliases ();
116 temp
= (alias_t
*)NULL
;
119 temp
= find_alias (name
);
124 temp
->value
= savestring (value
);
125 temp
->flags
&= ~AL_EXPANDNEXT
;
126 n
= value
[strlen (value
) - 1];
127 if (n
== ' ' || n
== '\t')
128 temp
->flags
|= AL_EXPANDNEXT
;
132 temp
= (alias_t
*)xmalloc (sizeof (alias_t
));
133 temp
->name
= savestring (name
);
134 temp
->value
= savestring (value
);
137 n
= value
[strlen (value
) - 1];
138 if (n
== ' ' || n
== '\t')
139 temp
->flags
|= AL_EXPANDNEXT
;
141 elt
= hash_insert (savestring (name
), aliases
, HASH_NOSRCH
);
143 #if defined (PROGRAMMABLE_COMPLETION)
144 set_itemlist_dirty (&it_aliases
);
149 /* Delete a single alias structure. */
151 free_alias_data (data
)
162 /* Remove the alias with name NAME from the alias table. Returns
163 the number of aliases left in the table, or -1 if the alias didn't
169 BUCKET_CONTENTS
*elt
;
174 elt
= hash_remove (name
, aliases
, 0);
177 free_alias_data (elt
->data
);
178 free (elt
->key
); /* alias name */
179 free (elt
); /* XXX */
180 #if defined (PROGRAMMABLE_COMPLETION)
181 set_itemlist_dirty (&it_aliases
);
183 return (aliases
->nentries
);
188 /* Delete all aliases. */
190 delete_all_aliases ()
195 hash_flush (aliases
, free_alias_data
);
196 hash_dispose (aliases
);
197 aliases
= (HASH_TABLE
*)NULL
;
198 #if defined (PROGRAMMABLE_COMPLETION)
199 set_itemlist_dirty (&it_aliases
);
203 /* Return an array of aliases that satisfy the conditions tested by FUNCTION.
204 If FUNCTION is NULL, return all aliases. */
206 map_over_aliases (function
)
207 sh_alias_map_func_t
*function
;
210 register BUCKET_CONTENTS
*tlist
;
211 alias_t
*alias
, **list
;
214 i
= HASH_ENTRIES (aliases
);
216 return ((alias_t
**)NULL
);
218 list
= (alias_t
**)xmalloc ((i
+ 1) * sizeof (alias_t
*));
219 for (i
= list_index
= 0; i
< aliases
->nbuckets
; i
++)
221 for (tlist
= hash_items (i
, aliases
); tlist
; tlist
= tlist
->next
)
223 alias
= (alias_t
*)tlist
->data
;
225 if (!function
|| (*function
) (alias
))
227 list
[list_index
++] = alias
;
228 list
[list_index
] = (alias_t
*)NULL
;
239 qsort (array
, strvec_len ((char **)array
), sizeof (alias_t
*), (QSFUNC
*)qsort_alias_compare
);
243 qsort_alias_compare (as1
, as2
)
244 alias_t
**as1
, **as2
;
248 if ((result
= (*as1
)->name
[0] - (*as2
)->name
[0]) == 0)
249 result
= strcmp ((*as1
)->name
, (*as2
)->name
);
254 /* Return a sorted list of all defined aliases */
260 if (aliases
== 0 || HASH_ENTRIES (aliases
) == 0)
261 return ((alias_t
**)NULL
);
263 list
= map_over_aliases ((sh_alias_map_func_t
*)NULL
);
270 alias_expand_word (s
)
276 return (r
? savestring (r
->value
) : (char *)NULL
);
279 /* Readline support functions -- expand all aliases in a line. */
281 #if defined (READLINE)
283 /* Return non-zero if CHARACTER is a member of the class of characters
284 that are self-delimiting in the shell (this really means that these
285 characters delimit tokens). */
286 #define self_delimiting(character) (member ((character), " \t\n\r;|&()"))
288 /* Return non-zero if CHARACTER is a member of the class of characters
289 that delimit commands in the shell. */
290 #define command_separator(character) (member ((character), "\r\n;|&("))
292 /* If this is 1, we are checking the next token read for alias expansion
293 because it is the first word in a command. */
294 static int command_word
;
296 /* This is for skipping quoted strings in alias expansions. */
297 #define quote_char(c) (((c) == '\'') || ((c) == '"'))
299 /* Consume a quoted string from STRING, starting at string[START] (so
300 string[START] is the opening quote character), and return the index
301 of the closing quote character matching the opening quote character.
302 This handles single matching pairs of unquoted quotes; it could afford
303 to be a little smarter... This skips words between balanced pairs of
304 quotes, words where the first character is quoted with a `\', and other
305 backslash-escaped characters. */
308 skipquotes (string
, start
)
313 int delimiter
= string
[start
];
315 /* i starts at START + 1 because string[START] is the opening quote
317 for (i
= start
+ 1 ; string
[i
] ; i
++)
319 if (string
[i
] == '\\')
321 i
++; /* skip backslash-quoted quote characters, too */
325 if (string
[i
] == delimiter
)
331 /* Skip the white space and any quoted characters in STRING, starting at
332 START. Return the new index into STRING, after zero or more characters
333 have been skipped. */
335 skipws (string
, start
)
340 int pass_next
, backslash_quoted_word
;
343 /* skip quoted strings, in ' or ", and words in which a character is quoted
345 i
= backslash_quoted_word
= pass_next
= 0;
347 /* Skip leading whitespace (or separator characters), and quoted words.
348 But save it in the output. */
350 for (i
= start
; string
[i
]; i
++)
358 if (whitespace (string
[i
]))
360 backslash_quoted_word
= 0; /* we are no longer in a backslash-quoted word */
364 if (string
[i
] == '\\')
367 if (ISLETTER (peekc
))
368 backslash_quoted_word
++; /* this is a backslash-quoted word */
374 /* This only handles single pairs of non-escaped quotes. This
375 overloads backslash_quoted_word to also mean that a word like
376 ""f is being scanned, so that the quotes will inhibit any expansion
378 if (quote_char(string
[i
]))
380 i
= skipquotes (string
, i
);
381 /* This could be a line that contains a single quote character,
382 in which case skipquotes () terminates with string[i] == '\0'
383 (the end of the string). Check for that here. */
384 if (string
[i
] == '\0')
387 peekc
= string
[i
+ 1];
388 if (ISLETTER (peekc
))
389 backslash_quoted_word
++;
393 /* If we're in the middle of some kind of quoted word, let it
395 if (backslash_quoted_word
)
398 /* If this character is a shell command separator, then set a hint for
399 alias_expand that the next token is the first word in a command. */
401 if (command_separator (string
[i
]))
411 /* Characters that may appear in a token. Basically, anything except white
412 space and a token separator. */
413 #define token_char(c) (!((whitespace (string[i]) || self_delimiting (string[i]))))
415 /* Read from START in STRING until the next separator character, and return
416 the index of that separator. Skip backslash-quoted characters. Call
417 skipquotes () for quoted strings in the middle or at the end of tokens,
418 so all characters show up (e.g. foo'' and foo""bar) */
420 rd_token (string
, start
)
426 /* From here to next separator character is a token. */
427 for (i
= start
; string
[i
] && token_char (string
[i
]); i
++)
429 if (string
[i
] == '\\')
431 i
++; /* skip backslash-escaped character */
435 /* If this character is a quote character, we want to call skipquotes
436 to get the whole quoted portion as part of this word. That word
437 will not generally match an alias, even if te unquoted word would
438 have. The presence of the quotes in the token serves then to
439 inhibit expansion. */
440 if (quote_char (string
[i
]))
442 i
= skipquotes (string
, i
);
443 /* This could be a line that contains a single quote character,
444 in which case skipquotes () terminates with string[i] == '\0'
445 (the end of the string). Check for that here. */
446 if (string
[i
] == '\0')
449 /* Now string[i] is the matching quote character, and the
450 quoted portion of the token has been scanned. */
457 /* Return a new line, with any aliases substituted. */
459 alias_expand (string
)
462 register int i
, j
, start
;
464 int line_len
, tl
, real_start
, expand_next
, expand_this_token
;
467 line_len
= strlen (string
) + 1;
468 line
= (char *)xmalloc (line_len
);
469 token
= (char *)xmalloc (line_len
);
473 command_word
= 1; /* initialized to expand the first word on the line */
475 /* Each time through the loop we find the next word in line. If it
476 has an alias, substitute the alias value. If the value ends in ` ',
477 then try again with the next word. Else, if there is no value, or if
478 the value does not end in space, we are done. */
486 /* Skip white space and quoted characters */
487 i
= skipws (string
, start
);
489 if (start
== i
&& string
[i
] == '\0')
495 /* copy the just-skipped characters into the output string,
496 expanding it if there is not enough room. */
498 tl
= i
- start
; /* number of characters just skipped */
499 RESIZE_MALLOCED_BUFFER (line
, j
, (tl
+ 1), line_len
, (tl
+ 50));
500 strncpy (line
+ j
, string
+ start
, tl
);
505 command_word
= command_word
|| (command_separator (string
[i
]));
506 expand_this_token
= (command_word
|| expand_next
);
509 /* Read the next token, and copy it into TOKEN. */
511 i
= rd_token (string
, start
);
513 tl
= i
- start
; /* token length */
515 /* If tl == 0, but we're not at the end of the string, then we have a
516 single-character token, probably a delimiter */
517 if (tl
== 0 && string
[i
] != '\0')
520 i
++; /* move past it */
523 strncpy (token
, string
+ start
, tl
);
526 /* If there is a backslash-escaped character quoted in TOKEN,
527 then we don't do alias expansion. This should check for all
528 other quoting characters, too. */
529 if (xstrchr (token
, '\\'))
530 expand_this_token
= 0;
532 /* If we should be expanding here, if we are expanding all words, or if
533 we are in a location in the string where an expansion is supposed to
534 take place, see if this word has a substitution. If it does, then do
535 the expansion. Note that we defer the alias value lookup until we
536 are sure we are expanding this token. */
539 (expand_this_token
|| alias_expand_all
) &&
540 (alias
= find_alias (token
)))
547 llen
= strlen (line
);
549 /* +3 because we possibly add one more character below. */
550 RESIZE_MALLOCED_BUFFER (line
, llen
, (vlen
+ 3), line_len
, (vlen
+ 50));
552 strcpy (line
+ llen
, v
);
554 if ((expand_this_token
&& vlen
&& whitespace (v
[vlen
- 1])) ||
562 llen
= strlen (line
);
563 tlen
= i
- real_start
; /* tlen == strlen(token) */
565 RESIZE_MALLOCED_BUFFER (line
, llen
, (tlen
+ 1), line_len
, (llen
+ tlen
+ 50));
567 strncpy (line
+ llen
, string
+ real_start
, tlen
);
568 line
[llen
+ tlen
] = '\0';
573 #endif /* READLINE */