home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / BASH_112.ZIP / BASH-112.TAR / bash-1.12 / alias.c < prev    next >
C/C++ Source or Header  |  1991-07-07  |  12KB  |  432 lines

  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'). */
  3.  
  4. /* Copyright (C) 1987,1991 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 it
  9.    under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 1, or (at your option)
  11.    any later version.
  12.  
  13.    Bash is distributed in the hope that it will be useful, but WITHOUT
  14.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15.    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  16.    License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with Bash; see the file COPYING.  If not, write to the Free
  20.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #include <stdio.h>
  23. #include "config.h"
  24. #include "general.h"
  25. #include "alias.h"
  26.  
  27. /* The number of slots to allocate when we need new slots. */
  28. #define alias_list_grow_amount 50
  29.  
  30. /* Non-zero means expand all words on the line.  Otherwise, expand
  31.    after first expansion if the expansion ends in a space. */
  32. int alias_expand_all = 0;
  33.  
  34. /* The list of aliases that we have. */
  35. ASSOC **aliases = (ASSOC **)NULL;
  36.  
  37. /* The number of slots in the above list. */
  38. static int aliases_size = 0;
  39.  
  40. /* The number of aliases that are in existence. */
  41. static int aliases_length = 0;
  42.  
  43. /* The last alias index found with find_alias (). */
  44. static int last_alias_index = 0;
  45.  
  46. /* Scan the list of aliases looking for one with NAME.  Return NULL
  47.    if the alias doesn't exist, else a pointer to the assoc. */
  48. ASSOC *
  49. find_alias (name)
  50.      char *name;
  51. {
  52.   register int i;
  53.  
  54.   for (i = 0; i < aliases_length; i++)
  55.     if (STREQ (name, aliases[i]->name))
  56.       return (aliases[last_alias_index = i]);
  57.  
  58.   return ((ASSOC *)NULL);
  59. }
  60.  
  61. /* Return the value of the alias for NAME, or NULL if there is none. */
  62. char *
  63. get_alias_value (name)
  64.      char *name;
  65. {
  66.   ASSOC *alias = find_alias (name);
  67.   if (alias)
  68.     return (alias->value);
  69.   else
  70.     return ((char *)NULL);
  71. }
  72.  
  73. /* Make a new alias from NAME and VALUE.  If NAME can be found,
  74.    then replace its value. */
  75. void
  76. add_alias (name, value)
  77.      char *name, *value;
  78. {
  79.   ASSOC *temp = find_alias (name);
  80.  
  81.   if (temp)
  82.     {
  83.       free (temp->value);
  84.       temp->value = savestring (value);
  85.     }
  86.   else
  87.     {
  88.       temp = (ASSOC *)xmalloc (sizeof (ASSOC));
  89.       temp->name = savestring (name);
  90.       temp->value = savestring (value);
  91.  
  92.       if ((aliases_length + 1) >= aliases_size)
  93.     {
  94.       aliases =
  95.         (ASSOC **)xrealloc (aliases,
  96.                 (aliases_size += alias_list_grow_amount)
  97.                 * sizeof (ASSOC *));
  98.     }
  99.       aliases[aliases_length++] = temp;
  100.       aliases[aliases_length] = (ASSOC *)NULL;
  101.     }
  102. }
  103.  
  104. /* Remove the alias with name NAME from the alias list.  Returns
  105.    the index of the removed alias, or -1 if the alias didn't exist. */
  106. int
  107. remove_alias (name)
  108.      char *name;
  109. {
  110.   register int i;
  111.  
  112.   if (!find_alias (name))
  113.     return (-1);
  114.  
  115.   i = last_alias_index;
  116.   free (aliases[i]->name);
  117.   free (aliases[i]->value);
  118.   free (aliases[i]);
  119.  
  120.   for (; i < aliases_length; i++)
  121.     aliases[i] = aliases[i + 1];
  122.  
  123.   aliases_length--;
  124.   aliases[aliases_length] = (ASSOC *)NULL;
  125.  
  126.   return (last_alias_index);
  127. }
  128.  
  129. /* Delete all aliases. */
  130. delete_all_aliases ()
  131. {
  132.   register int i;
  133.  
  134.   for (i = 0; i < aliases_length; i++)
  135.     {
  136.       free (aliases[i]->name);
  137.       free (aliases[i]->value);
  138.       free (aliases[i]);
  139.       aliases[i] = (ASSOC *)NULL;
  140.     }
  141.  
  142.   aliases_length = 0;
  143. }
  144.  
  145. /* Return non-zero if CHARACTER is a member of the class of characters
  146.    that are self-delimiting in the shell (this really means that these
  147.    characters delimit tokens). */
  148. #define self_delimiting(character) (member ((character), " \t\n\r;|&()"))
  149.  
  150. /* Return non-zero if CHARACTER is a member of the class of characters
  151.    that delimit commands in the shell. */
  152. #define command_separator(character) (member ((character), "\r\n;|&("))
  153.  
  154. /* If this is 1, we are checking the next token read for alias expansion
  155.    because it is the first word in a command. */
  156. static int command_word;
  157.  
  158. /* This is for skipping quoted strings in alias expansions. */
  159. #define quote_char(c)  (((c) == '\'') || ((c) == '"'))
  160.  
  161. /* Consume a quoted string from STRING, starting at string[START] (so
  162.    string[START] is the opening quote character), and return the index
  163.    of the closing quote character matching the opening quote character.
  164.    This handles single matching pairs of unquoted quotes; it could afford
  165.    to be a little smarter... This skips words between balanced pairs of
  166.    quotes, words where the first character is quoted with a `\', and other
  167.    backslash-escaped characters. */
  168.  
  169. static int
  170. skipquotes (string, start)
  171.      char *string;
  172.      int start;
  173. {
  174.   register int i;
  175.   int delimiter = string[start];
  176.  
  177.   /* i starts at START + 1 because string[START] is the opening quote
  178.      character. */
  179.   for (i = start + 1 ; string[i] ; i++)
  180.     {
  181.       if (string[i] == '\\')
  182.     {
  183.       i++;        /* skip backslash-quoted quote characters, too */
  184.       continue;
  185.     }
  186.  
  187.       if (string[i] == delimiter)
  188.     return i;
  189.     }
  190.   return (i);
  191. }
  192.  
  193. /* Skip the white space and any quoted characters in STRING, starting at
  194.    START.  Return the new index into STRING, after zero or more characters
  195.    have been skipped. */
  196. static int
  197. skipws (string, start)
  198.      char *string;
  199.      int start;
  200. {
  201.   register int i = 0;
  202.   int pass_next, backslash_quoted_word, peekc;
  203.  
  204.   /* skip quoted strings, in ' or ", and words in which a character is quoted
  205.      with a `\'. */
  206.   backslash_quoted_word = pass_next = 0;
  207.  
  208.   /* Skip leading whitespace (or separator characters), and quoted words.
  209.      But save it in the output.  */
  210.  
  211.   for (i = start; string[i]; i++)
  212.     {
  213.       if (pass_next)
  214.     {
  215.       pass_next = 0;
  216.       continue;
  217.     }
  218.  
  219.       if (whitespace (string[i]))
  220.     {
  221.       backslash_quoted_word = 0; /* we are no longer in a backslash-quoted word */
  222.       continue;
  223.     }
  224.  
  225.       if (string[i] == '\\')
  226.     {
  227.       peekc = string[i+1];
  228.       if (isletter (peekc))
  229.         backslash_quoted_word++;    /* this is a backslash-quoted word */
  230.       else
  231.         pass_next++;
  232.       continue;
  233.     }
  234.  
  235.       /* This only handles single pairs of non-escaped quotes.  This
  236.      overloads backslash_quoted_word to also mean that a word like
  237.      ""f is being scanned, so that the quotes will inhibit any expansion
  238.      of the word. */
  239.       if (quote_char(string[i]))
  240.     {
  241.       i = skipquotes (string, i);
  242.       /* This could be a line that contains a single quote character,
  243.          in which case skipquotes () terminates with string[i] == '\0'
  244.          (the end of the string).  Check for that here. */
  245.       if (string[i] == '\0')
  246.         break;
  247.  
  248.       peekc = string[i + 1];
  249.       if (isletter (peekc))
  250.         backslash_quoted_word++;
  251.       continue;
  252.     }
  253.  
  254.       /* If we're in the middle of some kind of quoted word, let it
  255.      pass through. */
  256.       if (backslash_quoted_word)
  257.     continue;
  258.  
  259.       /* If this character is a shell command separator, then set a hint for
  260.      alias_expand that the next token is the first word in a command. */
  261.  
  262.       if (command_separator (string[i]))
  263.     {
  264.       command_word++;
  265.       continue;
  266.     }
  267.       break;
  268.     }
  269.   return (i);
  270. }
  271.  
  272. /* Characters that may appear in a token.  Basically, anything except white
  273.    space and a token separator. */
  274. #define token_char(c)    (!((whitespace (string[i]) || self_delimiting (string[i]))))
  275.  
  276. /* Read from START in STRING until the next separator character, and return
  277.    the index of that separator.  Skip backslash-quoted characters.  Call
  278.    skipquotes () for quoted strings in the middle or at the end of tokens,
  279.    so all characters show up (e.g. foo'' and foo""bar) */
  280. static int
  281. rd_token (string, start)
  282.      char *string; 
  283.      int start;
  284. {
  285.   register int i;
  286.  
  287.   /* From here to next separator character is a token. */
  288.   for (i = start; string[i] && token_char (string[i]); i++)
  289.     {
  290.       if (string[i] == '\\')
  291.     {
  292.       i++;    /* skip backslash-escaped character */
  293.       continue;
  294.     }
  295.  
  296.       /* If this character is a quote character, we want to call skipquotes
  297.      to get the whole quoted portion as part of this word.  That word
  298.      will not generally match an alias, even if te unquoted word would
  299.      have.  The presence of the quotes in the token serves then to 
  300.      inhibit expansion. */
  301.       if (quote_char (string[i]))
  302.     {
  303.       i = skipquotes (string, i);
  304.       /* Now string[i] is the matching quote character, and the
  305.          quoted portion of the token has been scanned. */
  306.       continue;
  307.     }
  308.     }
  309.   return (i);
  310. }
  311.  
  312. /* Return a new line, with any aliases substituted. */
  313. char *
  314. alias_expand (string)
  315.      char *string;
  316. {
  317.   int line_len = 1 + strlen (string);
  318.   char *line = (char *)xmalloc (line_len);
  319.   register int i, j, start;
  320.   char *token = (char *)alloca (line_len);
  321.   int tl, real_start, expand_next, expand_this_token;
  322.   ASSOC *alias;
  323.  
  324.   line[0] = i = 0;
  325.   expand_next = 0;
  326.   command_word = 1; /* initialized to expand the first word on the line */
  327.  
  328.   /* Each time through the loop we find the next word in line.  If it
  329.      has an alias, substitute
  330.      the alias value.  If the value ends in ` ', then try again
  331.      with the next word.  Else, if there is no value, or if
  332.      the value does not end in space, we are done. */
  333.  
  334.   for (;;)
  335.     {
  336.  
  337.       token[0] = 0;
  338.       start = i;
  339.  
  340.       /* Skip white space and quoted characters */
  341.       i = skipws (string, start);
  342.  
  343.       if (start == i && string[i] == '\0')
  344.     return (line);
  345.  
  346.       /* copy the just-skipped characters into the output string,
  347.      expanding it if there is not enough room. */
  348.       j = strlen (line);
  349.       tl = i - start;    /* number of characters just skipped */
  350.       if (1 + j + tl >= line_len)
  351.     line = (char *)xrealloc (line, line_len += (50 + tl));
  352.       strncpy (line + j, string + start, tl);
  353.       line[j + tl] = '\0';
  354.  
  355.       real_start = i;
  356.  
  357.       command_word = command_word || (command_separator (string[i]));
  358.       expand_this_token = (command_word || expand_next);
  359.       expand_next = 0;
  360.  
  361.       /* Read the next token, and copy it into TOKEN. */
  362.       start = i;
  363.       i = rd_token (string, start);
  364.  
  365.       tl = i - start;    /* token length */
  366.  
  367.       /* If tl == 0, but we're not at the end of the string, then we have a
  368.      single-character token, probably a delimiter */
  369.       if (tl == 0 && string[i] != '\0')
  370.     {
  371.       tl = 1;
  372.       i++;        /* move past it */
  373.     }
  374.  
  375.       strncpy (token, string + start, tl);
  376.       token [tl] = '\0';
  377.  
  378.       /* If there is a backslash-escaped character quoted in TOKEN,
  379.      then we don't do alias expansion.  This should check for all
  380.      other quoting characters, too. */
  381.       if (index(token,'\\'))
  382.     expand_this_token = 0;
  383.  
  384.       /* If we should be expanding here, if we are expanding all words, or if
  385.      we are in a location in the string where an expansion is supposed to
  386.      take place, see if this word has a substitution.  If it does, then do
  387.      the expansion.  Note that we defer the alias value lookup until we
  388.      are sure we are expanding this token. */
  389.  
  390.       if ((token[0]) &&
  391.       (expand_this_token || alias_expand_all) &&
  392.       (alias = find_alias (token)))
  393.     {
  394.       char *v = alias->value;
  395.       int l = strlen (v);
  396.       
  397.       /* +3 because we possibly add one more character below. */
  398.       if ((l + 3) > line_len - strlen (line))
  399.         line = (char *)xrealloc (line, line_len += (50 + l));
  400.  
  401.       strcat (line, v);
  402.  
  403.       if ((expand_this_token && l && whitespace (v[l - 1])) || alias_expand_all)
  404.         expand_next = 1;
  405.     }
  406.       else
  407.     {
  408.       int ll = strlen (line);
  409.       int tl = i - real_start; /* tl == strlen(token) */
  410.  
  411.       if (ll + tl + 2 > line_len)
  412.         line = (char *)xrealloc (line, line_len += 50 + ll + tl);
  413.  
  414.       strncpy (line + ll, string + real_start, tl);
  415.       line[ll + tl] = '\0';
  416.     }
  417.       command_word = 0;
  418.     }
  419. }
  420.  
  421. char *
  422. alias_expand_word (s)
  423.      char *s;
  424. {
  425.   ASSOC *r = find_alias (s);
  426.  
  427.   if (r)
  428.     return (savestring (r->value));
  429.   else
  430.     return ((char *)NULL);
  431. }
  432.