]>
git.ipfire.org Git - thirdparty/bash.git/blob - alias.c
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-2015 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 #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
46 # include <mbstr.h> /* mbschr */
49 #define ALIAS_HASH_BUCKETS 64 /* must be power of two */
51 typedef int sh_alias_map_func_t
__P((alias_t
*));
53 static void free_alias_data
__P((PTR_T
));
54 static alias_t
**map_over_aliases
__P((sh_alias_map_func_t
*));
55 static void sort_aliases
__P((alias_t
**));
56 static int qsort_alias_compare
__P((alias_t
**, alias_t
**));
58 #if defined (READLINE)
59 static int skipquotes
__P((char *, int));
60 static int skipws
__P((char *, int));
61 static int rd_token
__P((char *, int));
64 /* Non-zero means expand all words on the line. Otherwise, expand
65 after first expansion if the expansion ends in a space. */
66 int alias_expand_all
= 0;
68 /* The list of aliases that we have. */
69 HASH_TABLE
*aliases
= (HASH_TABLE
*)NULL
;
75 aliases
= hash_create (ALIAS_HASH_BUCKETS
);
78 /* Scan the list of aliases looking for one with NAME. Return NULL
79 if the alias doesn't exist, else a pointer to the alias_t. */
87 return ((alias_t
*)NULL
);
89 al
= hash_search (name
, aliases
, 0);
90 return (al
? (alias_t
*)al
->data
: (alias_t
*)NULL
);
93 /* Return the value of the alias for NAME, or NULL if there is none. */
95 get_alias_value (name
)
101 return ((char *)NULL
);
103 alias
= find_alias (name
);
104 return (alias
? alias
->value
: (char *)NULL
);
107 /* Make a new alias from NAME and VALUE. If NAME can be found,
108 then replace its value. */
110 add_alias (name
, value
)
113 BUCKET_CONTENTS
*elt
;
119 initialize_aliases ();
120 temp
= (alias_t
*)NULL
;
123 temp
= find_alias (name
);
128 temp
->value
= savestring (value
);
129 temp
->flags
&= ~AL_EXPANDNEXT
;
130 n
= value
[strlen (value
) - 1];
131 if (n
== ' ' || n
== '\t')
132 temp
->flags
|= AL_EXPANDNEXT
;
136 temp
= (alias_t
*)xmalloc (sizeof (alias_t
));
137 temp
->name
= savestring (name
);
138 temp
->value
= savestring (value
);
141 n
= value
[strlen (value
) - 1];
142 if (n
== ' ' || n
== '\t')
143 temp
->flags
|= AL_EXPANDNEXT
;
145 elt
= hash_insert (savestring (name
), aliases
, HASH_NOSRCH
);
147 #if defined (PROGRAMMABLE_COMPLETION)
148 set_itemlist_dirty (&it_aliases
);
153 /* Delete a single alias structure. */
155 free_alias_data (data
)
166 /* Remove the alias with name NAME from the alias table. Returns
167 the number of aliases left in the table, or -1 if the alias didn't
173 BUCKET_CONTENTS
*elt
;
178 elt
= hash_remove (name
, aliases
, 0);
181 free_alias_data (elt
->data
);
182 free (elt
->key
); /* alias name */
183 free (elt
); /* XXX */
184 #if defined (PROGRAMMABLE_COMPLETION)
185 set_itemlist_dirty (&it_aliases
);
187 return (aliases
->nentries
);
192 /* Delete all aliases. */
194 delete_all_aliases ()
199 hash_flush (aliases
, free_alias_data
);
200 hash_dispose (aliases
);
201 aliases
= (HASH_TABLE
*)NULL
;
202 #if defined (PROGRAMMABLE_COMPLETION)
203 set_itemlist_dirty (&it_aliases
);
207 /* Return an array of aliases that satisfy the conditions tested by FUNCTION.
208 If FUNCTION is NULL, return all aliases. */
210 map_over_aliases (function
)
211 sh_alias_map_func_t
*function
;
214 register BUCKET_CONTENTS
*tlist
;
215 alias_t
*alias
, **list
;
218 i
= HASH_ENTRIES (aliases
);
220 return ((alias_t
**)NULL
);
222 list
= (alias_t
**)xmalloc ((i
+ 1) * sizeof (alias_t
*));
223 for (i
= list_index
= 0; i
< aliases
->nbuckets
; i
++)
225 for (tlist
= hash_items (i
, aliases
); tlist
; tlist
= tlist
->next
)
227 alias
= (alias_t
*)tlist
->data
;
229 if (!function
|| (*function
) (alias
))
231 list
[list_index
++] = alias
;
232 list
[list_index
] = (alias_t
*)NULL
;
243 qsort (array
, strvec_len ((char **)array
), sizeof (alias_t
*), (QSFUNC
*)qsort_alias_compare
);
247 qsort_alias_compare (as1
, as2
)
248 alias_t
**as1
, **as2
;
252 if ((result
= (*as1
)->name
[0] - (*as2
)->name
[0]) == 0)
253 result
= strcmp ((*as1
)->name
, (*as2
)->name
);
258 /* Return a sorted list of all defined aliases */
264 if (aliases
== 0 || HASH_ENTRIES (aliases
) == 0)
265 return ((alias_t
**)NULL
);
267 list
= map_over_aliases ((sh_alias_map_func_t
*)NULL
);
274 alias_expand_word (s
)
280 return (r
? savestring (r
->value
) : (char *)NULL
);
283 /* Readline support functions -- expand all aliases in a line. */
285 #if defined (READLINE)
287 /* Return non-zero if CHARACTER is a member of the class of characters
288 that are self-delimiting in the shell (this really means that these
289 characters delimit tokens). */
290 #define self_delimiting(character) (member ((character), " \t\n\r;|&()"))
292 /* Return non-zero if CHARACTER is a member of the class of characters
293 that delimit commands in the shell. */
294 #define command_separator(character) (member ((character), "\r\n;|&("))
296 /* If this is 1, we are checking the next token read for alias expansion
297 because it is the first word in a command. */
298 static int command_word
;
300 /* This is for skipping quoted strings in alias expansions. */
301 #define quote_char(c) (((c) == '\'') || ((c) == '"'))
303 /* Consume a quoted string from STRING, starting at string[START] (so
304 string[START] is the opening quote character), and return the index
305 of the closing quote character matching the opening quote character.
306 This handles single matching pairs of unquoted quotes; it could afford
307 to be a little smarter... This skips words between balanced pairs of
308 quotes, words where the first character is quoted with a `\', and other
309 backslash-escaped characters. */
312 skipquotes (string
, start
)
317 int delimiter
= string
[start
];
319 /* i starts at START + 1 because string[START] is the opening quote
321 for (i
= start
+ 1 ; string
[i
] ; i
++)
323 if (string
[i
] == '\\')
325 i
++; /* skip backslash-quoted quote characters, too */
331 if (string
[i
] == delimiter
)
337 /* Skip the white space and any quoted characters in STRING, starting at
338 START. Return the new index into STRING, after zero or more characters
339 have been skipped. */
341 skipws (string
, start
)
346 int pass_next
, backslash_quoted_word
;
349 /* skip quoted strings, in ' or ", and words in which a character is quoted
351 i
= backslash_quoted_word
= pass_next
= 0;
353 /* Skip leading whitespace (or separator characters), and quoted words.
354 But save it in the output. */
356 for (i
= start
; string
[i
]; i
++)
364 if (whitespace (string
[i
]))
366 backslash_quoted_word
= 0; /* we are no longer in a backslash-quoted word */
370 if (string
[i
] == '\\')
375 if (ISLETTER (peekc
))
376 backslash_quoted_word
++; /* this is a backslash-quoted word */
382 /* This only handles single pairs of non-escaped quotes. This
383 overloads backslash_quoted_word to also mean that a word like
384 ""f is being scanned, so that the quotes will inhibit any expansion
386 if (quote_char(string
[i
]))
388 i
= skipquotes (string
, i
);
389 /* This could be a line that contains a single quote character,
390 in which case skipquotes () terminates with string[i] == '\0'
391 (the end of the string). Check for that here. */
392 if (string
[i
] == '\0')
395 peekc
= string
[i
+ 1];
396 if (ISLETTER (peekc
))
397 backslash_quoted_word
++;
401 /* If we're in the middle of some kind of quoted word, let it
403 if (backslash_quoted_word
)
406 /* If this character is a shell command separator, then set a hint for
407 alias_expand that the next token is the first word in a command. */
409 if (command_separator (string
[i
]))
419 /* Characters that may appear in a token. Basically, anything except white
420 space and a token separator. */
421 #define token_char(c) (!((whitespace (string[i]) || self_delimiting (string[i]))))
423 /* Read from START in STRING until the next separator character, and return
424 the index of that separator. Skip backslash-quoted characters. Call
425 skipquotes () for quoted strings in the middle or at the end of tokens,
426 so all characters show up (e.g. foo'' and foo""bar) */
428 rd_token (string
, start
)
434 /* From here to next separator character is a token. */
435 for (i
= start
; string
[i
] && token_char (string
[i
]); i
++)
437 if (string
[i
] == '\\')
439 i
++; /* skip backslash-escaped character */
445 /* If this character is a quote character, we want to call skipquotes
446 to get the whole quoted portion as part of this word. That word
447 will not generally match an alias, even if te unquoted word would
448 have. The presence of the quotes in the token serves then to
449 inhibit expansion. */
450 if (quote_char (string
[i
]))
452 i
= skipquotes (string
, i
);
453 /* This could be a line that contains a single quote character,
454 in which case skipquotes () terminates with string[i] == '\0'
455 (the end of the string). Check for that here. */
456 if (string
[i
] == '\0')
459 /* Now string[i] is the matching quote character, and the
460 quoted portion of the token has been scanned. */
467 /* Return a new line, with any aliases substituted. */
469 alias_expand (string
)
472 register int i
, j
, start
;
474 int line_len
, tl
, real_start
, expand_next
, expand_this_token
;
477 line_len
= strlen (string
) + 1;
478 line
= (char *)xmalloc (line_len
);
479 token
= (char *)xmalloc (line_len
);
483 command_word
= 1; /* initialized to expand the first word on the line */
485 /* Each time through the loop we find the next word in line. If it
486 has an alias, substitute the alias value. If the value ends in ` ',
487 then try again with the next word. Else, if there is no value, or if
488 the value does not end in space, we are done. */
496 /* Skip white space and quoted characters */
497 i
= skipws (string
, start
);
499 if (start
== i
&& string
[i
] == '\0')
505 /* copy the just-skipped characters into the output string,
506 expanding it if there is not enough room. */
508 tl
= i
- start
; /* number of characters just skipped */
509 RESIZE_MALLOCED_BUFFER (line
, j
, (tl
+ 1), line_len
, (tl
+ 50));
510 strncpy (line
+ j
, string
+ start
, tl
);
515 command_word
= command_word
|| (command_separator (string
[i
]));
516 expand_this_token
= (command_word
|| expand_next
);
519 /* Read the next token, and copy it into TOKEN. */
521 i
= rd_token (string
, start
);
523 tl
= i
- start
; /* token length */
525 /* If tl == 0, but we're not at the end of the string, then we have a
526 single-character token, probably a delimiter */
527 if (tl
== 0 && string
[i
] != '\0')
530 i
++; /* move past it */
533 strncpy (token
, string
+ start
, tl
);
536 /* If there is a backslash-escaped character quoted in TOKEN,
537 then we don't do alias expansion. This should check for all
538 other quoting characters, too. */
539 if (mbschr (token
, '\\'))
540 expand_this_token
= 0;
542 /* If we should be expanding here, if we are expanding all words, or if
543 we are in a location in the string where an expansion is supposed to
544 take place, see if this word has a substitution. If it does, then do
545 the expansion. Note that we defer the alias value lookup until we
546 are sure we are expanding this token. */
549 (expand_this_token
|| alias_expand_all
) &&
550 (alias
= find_alias (token
)))
557 llen
= strlen (line
);
559 /* +3 because we possibly add one more character below. */
560 RESIZE_MALLOCED_BUFFER (line
, llen
, (vlen
+ 3), line_len
, (vlen
+ 50));
562 strcpy (line
+ llen
, v
);
564 if ((expand_this_token
&& vlen
&& whitespace (v
[vlen
- 1])) ||
572 llen
= strlen (line
);
573 tlen
= i
- real_start
; /* tlen == strlen(token) */
575 RESIZE_MALLOCED_BUFFER (line
, llen
, (tlen
+ 1), line_len
, (llen
+ tlen
+ 50));
577 strncpy (line
+ llen
, string
+ real_start
, tlen
);
578 line
[llen
+ tlen
] = '\0';
583 #endif /* READLINE */