LCOV - code coverage report
Current view: top level - builtins - alias.def (source / functions) Hit Total Coverage
Test: cov-bash.info Lines: 26 76 34.2 %
Date: 2020-10-29 14:49:28 Functions: 1 3 33.3 %

          Line data    Source code
       1             : This file is alias.def, from which is created alias.c
       2             : It implements the builtins "alias" and "unalias" in Bash.
       3             : 
       4             : Copyright (C) 1987-2015 Free Software Foundation, Inc.
       5             : 
       6             : This file is part of GNU Bash, the Bourne Again SHell.
       7             : 
       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.
      12             : 
      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.
      17             : 
      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/>.
      20             : 
      21             : $BUILTIN alias
      22             : $FUNCTION alias_builtin
      23             : $DEPENDS_ON ALIAS
      24             : $PRODUCES alias.c
      25             : $SHORT_DOC alias [-p] [name[=value] ... ]
      26             : Define or display aliases.
      27             : 
      28             : Without arguments, `alias' prints the list of aliases in the reusable
      29             : form `alias NAME=VALUE' on standard output.
      30             : 
      31             : Otherwise, an alias is defined for each NAME whose VALUE is given.
      32             : A trailing space in VALUE causes the next word to be checked for
      33             : alias substitution when the alias is expanded.
      34             : 
      35             : Options:
      36             :   -p    print all defined aliases in a reusable format
      37             : 
      38             : Exit Status:
      39             : alias returns true unless a NAME is supplied for which no alias has been
      40             : defined.
      41             : $END
      42             : 
      43             : #include <config.h>
      44             : 
      45             : #if defined (ALIAS)
      46             : 
      47             : #if defined (HAVE_UNISTD_H)
      48             : #  ifdef _MINIX
      49             : #    include <sys/types.h>
      50             : #  endif
      51             : #  include <unistd.h>
      52             : #endif
      53             : 
      54             : #  include "../bashansi.h"
      55             : #  include "../bashintl.h"
      56             : 
      57             : #  include <stdio.h>
      58             : #  include "../shell.h"
      59             : #  include "../alias.h"
      60             : #  include "common.h"
      61             : #  include "bashgetopt.h"
      62             : 
      63             : /* Flags for print_alias */
      64             : #define AL_REUSABLE     0x01
      65             : 
      66             : static void print_alias __P((alias_t *, int));
      67             : 
      68             : extern int posixly_correct;
      69             : 
      70             : /* Hack the alias command in a Korn shell way. */
      71             : int
      72          98 : alias_builtin (list)
      73             :      WORD_LIST *list;
      74             : {
      75          98 :   int any_failed, offset, pflag, dflags;
      76          98 :   alias_t **alias_list, *t;
      77          98 :   char *name, *value;
      78             : 
      79          98 :   dflags = posixly_correct ? 0 : AL_REUSABLE;
      80          98 :   pflag = 0;
      81          98 :   reset_internal_getopt ();
      82          98 :   while ((offset = internal_getopt (list, "p")) != -1)
      83             :     {
      84           0 :       switch (offset)
      85             :         {
      86             :         case 'p':
      87             :           pflag = 1;
      88             :           dflags |= AL_REUSABLE;
      89             :           break;
      90           0 :         CASE_HELPOPT;
      91           0 :         default:
      92           0 :           builtin_usage ();
      93           0 :           return (EX_USAGE);
      94             :         }
      95             :     }
      96             : 
      97          98 :   list = loptend;
      98             : 
      99          98 :   if (list == 0 || pflag)
     100             :     {
     101          48 :       if (aliases == 0)
     102             :         return (EXECUTION_SUCCESS);
     103             : 
     104           0 :       alias_list = all_aliases ();
     105             : 
     106           0 :       if (alias_list == 0)
     107             :         return (EXECUTION_SUCCESS);
     108             : 
     109           0 :       for (offset = 0; alias_list[offset]; offset++)
     110           0 :         print_alias (alias_list[offset], dflags);
     111             : 
     112           0 :       free (alias_list);        /* XXX - Do not free the strings. */
     113             : 
     114           0 :       if (list == 0)
     115           0 :         return (sh_chkwrite (EXECUTION_SUCCESS));
     116             :     }
     117             : 
     118             :   any_failed = 0;
     119         121 :   while (list)
     120             :     {
     121          71 :       name = list->word->word;
     122             : 
     123        1086 :       for (offset = 0; name[offset] && name[offset] != '='; offset++)
     124        1015 :         ;
     125             : 
     126          71 :       if (offset && name[offset] == '=')
     127             :         {
     128          20 :           name[offset] = '\0';
     129          20 :           value = name + offset + 1;
     130             : 
     131          20 :           if (legal_alias_name (name, 0) == 0)
     132             :             {
     133           0 :               builtin_error (_("`%s': invalid alias name"), name);
     134           0 :               any_failed++;
     135             :             }
     136             :           else
     137          20 :             add_alias (name, value);
     138             :         }
     139             :       else
     140             :         {
     141          51 :           t = find_alias (name);
     142          51 :           if (t)
     143           0 :             print_alias (t, dflags);
     144             :           else
     145             :             {
     146          51 :               sh_notfound (name);
     147          51 :               any_failed++;
     148             :             }
     149             :         }
     150          71 :       list = list->next;
     151             :     }
     152             : 
     153          50 :   return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
     154             : }
     155             : #endif /* ALIAS */
     156             : 
     157             : $BUILTIN unalias
     158             : $FUNCTION unalias_builtin
     159             : $DEPENDS_ON ALIAS
     160             : $SHORT_DOC unalias [-a] name [name ...]
     161             : Remove each NAME from the list of defined aliases.
     162             : 
     163             : Options:
     164             :   -a    remove all alias definitions
     165             : 
     166             : Return success unless a NAME is not an existing alias.
     167             : $END
     168             : 
     169             : #if defined (ALIAS)
     170             : /* Remove aliases named in LIST from the aliases database. */
     171             : int
     172           0 : unalias_builtin (list)
     173             :      register WORD_LIST *list;
     174             : {
     175           0 :   register alias_t *alias;
     176           0 :   int opt, aflag;
     177             : 
     178           0 :   aflag = 0;
     179           0 :   reset_internal_getopt ();
     180           0 :   while ((opt = internal_getopt (list, "a")) != -1)
     181             :     {
     182           0 :       switch (opt)
     183             :         {
     184             :         case 'a':
     185             :           aflag = 1;
     186             :           break;
     187           0 :         CASE_HELPOPT;
     188           0 :         default:
     189           0 :           builtin_usage ();
     190           0 :           return (EX_USAGE);
     191             :         }
     192             :     }
     193             : 
     194           0 :   list = loptend;
     195             : 
     196           0 :   if (aflag)
     197             :     {
     198           0 :       delete_all_aliases ();
     199           0 :       return (EXECUTION_SUCCESS);
     200             :     }
     201             : 
     202           0 :   if (list == 0)
     203             :     {
     204           0 :       builtin_usage ();
     205           0 :       return (EX_USAGE);
     206             :     }
     207             : 
     208             :   aflag = 0;
     209           0 :   while (list)
     210             :     {
     211           0 :       alias = find_alias (list->word->word);
     212             : 
     213           0 :       if (alias)
     214           0 :         remove_alias (alias->name);
     215             :       else
     216             :         {
     217           0 :           sh_notfound (list->word->word);
     218           0 :           aflag++;
     219             :         }
     220             : 
     221           0 :       list = list->next;
     222             :     }
     223             : 
     224           0 :   return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
     225             : }
     226             : 
     227             : /* Output ALIAS in such a way as to allow it to be read back in. */
     228             : static void
     229           0 : print_alias (alias, flags)
     230             :      alias_t *alias;
     231             :      int flags;
     232             : {
     233           0 :   char *value;
     234             : 
     235           0 :   value = sh_single_quote (alias->value);
     236           0 :   if (flags & AL_REUSABLE)
     237           0 :     printf ("alias %s", (alias->name && alias->name[0] == '-') ? "-- " : "");
     238           0 :   printf ("%s=%s\n", alias->name, value);
     239           0 :   free (value);
     240             : 
     241           0 :   fflush (stdout);
     242           0 : }
     243             : #endif /* ALIAS */

Generated by: LCOV version 1.14.0.6.4058