Dan Pasanen | c6e3786 | 2014-10-02 14:08:59 -0500 | [diff] [blame] | 1 | /* alias.c, created from alias.def. */ |
| 2 | #line 42 "./alias.def" |
| 3 | |
| 4 | #include <config.h> |
| 5 | |
| 6 | #if defined (ALIAS) |
| 7 | |
| 8 | #if defined (HAVE_UNISTD_H) |
| 9 | # ifdef _MINIX |
| 10 | # include <sys/types.h> |
| 11 | # endif |
| 12 | # include <unistd.h> |
| 13 | #endif |
| 14 | |
| 15 | # include "../bashansi.h" |
| 16 | # include "../bashintl.h" |
| 17 | |
| 18 | # include <stdio.h> |
| 19 | # include "../shell.h" |
| 20 | # include "../alias.h" |
| 21 | # include "common.h" |
| 22 | # include "bashgetopt.h" |
| 23 | |
| 24 | /* Flags for print_alias */ |
| 25 | #define AL_REUSABLE 0x01 |
| 26 | |
| 27 | static void print_alias __P((alias_t *, int)); |
| 28 | |
| 29 | extern int posixly_correct; |
| 30 | |
| 31 | /* Hack the alias command in a Korn shell way. */ |
| 32 | int |
| 33 | alias_builtin (list) |
| 34 | WORD_LIST *list; |
| 35 | { |
| 36 | int any_failed, offset, pflag, dflags; |
| 37 | alias_t **alias_list, *t; |
| 38 | char *name, *value; |
| 39 | |
| 40 | dflags = posixly_correct ? 0 : AL_REUSABLE; |
| 41 | pflag = 0; |
| 42 | reset_internal_getopt (); |
| 43 | while ((offset = internal_getopt (list, "p")) != -1) |
| 44 | { |
| 45 | switch (offset) |
| 46 | { |
| 47 | case 'p': |
| 48 | pflag = 1; |
| 49 | dflags |= AL_REUSABLE; |
| 50 | break; |
| 51 | default: |
| 52 | builtin_usage (); |
| 53 | return (EX_USAGE); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | list = loptend; |
| 58 | |
| 59 | if (list == 0 || pflag) |
| 60 | { |
| 61 | if (aliases == 0) |
| 62 | return (EXECUTION_SUCCESS); |
| 63 | |
| 64 | alias_list = all_aliases (); |
| 65 | |
| 66 | if (alias_list == 0) |
| 67 | return (EXECUTION_SUCCESS); |
| 68 | |
| 69 | for (offset = 0; alias_list[offset]; offset++) |
| 70 | print_alias (alias_list[offset], dflags); |
| 71 | |
| 72 | free (alias_list); /* XXX - Do not free the strings. */ |
| 73 | |
| 74 | if (list == 0) |
| 75 | return (sh_chkwrite (EXECUTION_SUCCESS)); |
| 76 | } |
| 77 | |
| 78 | any_failed = 0; |
| 79 | while (list) |
| 80 | { |
| 81 | name = list->word->word; |
| 82 | |
| 83 | for (offset = 0; name[offset] && name[offset] != '='; offset++) |
| 84 | ; |
| 85 | |
| 86 | if (offset && name[offset] == '=') |
| 87 | { |
| 88 | name[offset] = '\0'; |
| 89 | value = name + offset + 1; |
| 90 | |
| 91 | if (legal_alias_name (name, 0) == 0) |
| 92 | { |
| 93 | builtin_error (_("`%s': invalid alias name"), name); |
| 94 | any_failed++; |
| 95 | } |
| 96 | else |
| 97 | add_alias (name, value); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | t = find_alias (name); |
| 102 | if (t) |
| 103 | print_alias (t, dflags); |
| 104 | else |
| 105 | { |
| 106 | sh_notfound (name); |
| 107 | any_failed++; |
| 108 | } |
| 109 | } |
| 110 | list = list->next; |
| 111 | } |
| 112 | |
| 113 | return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS); |
| 114 | } |
| 115 | #endif /* ALIAS */ |
| 116 | |
| 117 | #line 167 "./alias.def" |
| 118 | |
| 119 | #if defined (ALIAS) |
| 120 | /* Remove aliases named in LIST from the aliases database. */ |
| 121 | int |
| 122 | unalias_builtin (list) |
| 123 | register WORD_LIST *list; |
| 124 | { |
| 125 | register alias_t *alias; |
| 126 | int opt, aflag; |
| 127 | |
| 128 | aflag = 0; |
| 129 | reset_internal_getopt (); |
| 130 | while ((opt = internal_getopt (list, "a")) != -1) |
| 131 | { |
| 132 | switch (opt) |
| 133 | { |
| 134 | case 'a': |
| 135 | aflag = 1; |
| 136 | break; |
| 137 | default: |
| 138 | builtin_usage (); |
| 139 | return (EX_USAGE); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | list = loptend; |
| 144 | |
| 145 | if (aflag) |
| 146 | { |
| 147 | delete_all_aliases (); |
| 148 | return (EXECUTION_SUCCESS); |
| 149 | } |
| 150 | |
| 151 | if (list == 0) |
| 152 | { |
| 153 | builtin_usage (); |
| 154 | return (EX_USAGE); |
| 155 | } |
| 156 | |
| 157 | aflag = 0; |
| 158 | while (list) |
| 159 | { |
| 160 | alias = find_alias (list->word->word); |
| 161 | |
| 162 | if (alias) |
| 163 | remove_alias (alias->name); |
| 164 | else |
| 165 | { |
| 166 | sh_notfound (list->word->word); |
| 167 | aflag++; |
| 168 | } |
| 169 | |
| 170 | list = list->next; |
| 171 | } |
| 172 | |
| 173 | return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS); |
| 174 | } |
| 175 | |
| 176 | /* Output ALIAS in such a way as to allow it to be read back in. */ |
| 177 | static void |
| 178 | print_alias (alias, flags) |
| 179 | alias_t *alias; |
| 180 | int flags; |
| 181 | { |
| 182 | char *value; |
| 183 | |
| 184 | value = sh_single_quote (alias->value); |
| 185 | if (flags & AL_REUSABLE) |
| 186 | printf ("alias "); |
| 187 | printf ("%s=%s\n", alias->name, value); |
| 188 | free (value); |
| 189 | |
| 190 | fflush (stdout); |
| 191 | } |
| 192 | #endif /* ALIAS */ |