/** * @file alias.c * @brief The alias table and the `alias` and `unalias` builtins. * * Aliases are substituted by the parser, at the command-name position * only (see alias_expand() in parser.c). While the table is empty the * parser does not look anything up, so a script that defines no aliases * pays nothing for the feature, and because a loop body is parsed once, * an alias used inside a loop is not looked up again on each iteration. */ #include #include #include "exec.h" #include "parser.h" /** @brief Number of hash buckets. Aliases are few, so this is small. */ #define ATAB_SIZE ((size_t)32) /** * @brief The alias hash table. */ static alias *atab[ATAB_SIZE]; /** @brief How many aliases exist. The parser's "anything to do?" test. */ int alias_count; /** * @brief Hash an alias name to a bucket index. * @param s NUL-terminated name. * @return Bucket index in [0, ATAB_SIZE). */ static size_t alias_hash(const char *s) { unsigned h = 5381; while (*s) h = h * 33 + (unsigned char)*s++; return h % ATAB_SIZE; } /** * @brief Look up an alias. * @param name Candidate command word. * @return The alias, or nullptr if `name` is not one. */ alias *alias_find(const char *name) { for (alias *a = atab[alias_hash(name)]; a; a = a->next) { if (!strcmp(a->name, name)) return a; } return nullptr; } /** * @brief Call a function for the name of every alias. * @param fn Callback. * @param arg Passed through to the callback. */ void alias_each(void (*fn)(const char *name, void *arg), void *arg) { for (size_t b = 0; b < ATAB_SIZE; b++) { for (const alias *a = atab[b]; a; a = a->next) fn(a->name, arg); } } /** * @brief Define or redefine an alias. * @param name Alias name. * @param value Replacement text, copied. */ void alias_set(const char *name, const char *value) { alias *a = alias_find(name); if (a) { /* The lexer reads its own copy of the text (input_push_alias()), so * the old one can go even while it is being read. */ free(a->value); a->value = xstrdup(value); return; } a = xmalloc(sizeof *a); alias **bucket = &atab[alias_hash(name)]; *a = (alias){*bucket, xstrdup(name), xstrdup(value)}; *bucket = a; alias_count++; } /** * @brief Remove one alias. * @param name Alias name. * @return false if there was no such alias. */ static bool alias_remove(const char *name) { for (alias **pa = &atab[alias_hash(name)]; *pa; pa = &(*pa)->next) { alias *a = *pa; if (strcmp(a->name, name)) continue; *pa = a->next; alias_count--; /* The lexer reads its own copy of the text (input_push_alias()). */ free(a->value); free(a->name); free(a); return true; } return false; } /** * @brief Print one alias in a form that can be read back. * @param a The alias. */ static void alias_print(const alias *a) { out_puts(&out1, a->name); out_putc(&out1, '='); out_quote(&out1, a->value); out_putc(&out1, '\n'); } /** @brief qsort() comparison of two `struct alias *` by name. */ static int alias_cmp(const void *x, const void *y) { const alias *const *a = x; const alias *const *b = y; return strcmp((*a)->name, (*b)->name); } /** * @brief Is this name acceptable as an alias name? * * POSIX allows letters, digits and `! % , @ _`. Like other shells zash * accepts anything that the lexer would read as one plain word. */ static bool alias_name_ok(const char *s, size_t n) { if (!n) return false; for (size_t i = 0; i < n; i++) { if (strchr(" \t\n;&|<>()$`\\\"'=", s[i])) return false; } return true; } /** * @brief The `alias` builtin: `alias [name[=value] ...]`. * * With no operands, prints every alias in name order. `name=value` * defines an alias. A bare `name` prints it. * * @return 0, or 1 if a bare name was not an alias. */ int builtin_alias(int argc, char **argv) { int i = 1; if (i < argc && !strcmp(argv[i], "--")) i++; if (i >= argc) { alias **list = xmalloc(((size_t)alias_count + 1) * sizeof *list); size_t n = 0; for (size_t b = 0; b < ATAB_SIZE; b++) { for (alias *a = atab[b]; a; a = a->next) list[n++] = a; } qsort(list, n, sizeof *list, alias_cmp); for (size_t k = 0; k < n; k++) alias_print(list[k]); free(list); return 0; } int status = 0; for (; i < argc; i++) { const char *eq = strchr(argv[i], '='); if (eq && eq != argv[i]) { size_t nlen = (size_t)(eq - argv[i]); if (!alias_name_ok(argv[i], nlen)) { sh_warn("alias: %s: bad alias name", argv[i]); status = 1; continue; } alias_set(arena_strndup(&scratch, argv[i], nlen), eq + 1); } else { const alias *a = alias_find(argv[i]); if (a) { alias_print(a); } else { sh_warn("alias: %s: not found", argv[i]); status = 1; } } } return status; } /** * @brief The `unalias` builtin: `unalias name ...` or `unalias -a`. * @return 0, or 1 if a name was not an alias. */ int builtin_unalias(int argc, char **argv) { bool all = false; int i = 1; for (; i < argc && argv[i][0] == '-' && argv[i][1]; i++) { if (!strcmp(argv[i], "--")) { i++; break; } if (!strcmp(argv[i], "-a")) { all = true; } else { sh_warn("unalias: %s: bad option", argv[i]); return 2; } } if (all) { for (size_t b = 0; b < ATAB_SIZE; b++) { while (atab[b]) alias_remove(atab[b]->name); } return 0; } int status = 0; for (; i < argc; i++) { if (!alias_remove(argv[i])) { sh_warn("unalias: %s: not found", argv[i]); status = 1; } } return status; }