home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / BASH_112.ZIP / BASH-112.TAR / bash-1.12 / parse.y < prev    next >
Text File  |  1992-01-25  |  70KB  |  2,731 lines

  1. /* Yacc grammar for bash. */
  2.  
  3. /* Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it under
  8.    the terms of the GNU General Public License as published by the Free
  9.    Software Foundation; either version 1, or (at your option) any later
  10.    version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13.    WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15.    for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License along
  18.    with Bash; see the file LICENSE.  If not, write to the Free Software
  19.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. %{
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <signal.h>
  25. #include "shell.h"
  26. #include "flags.h"
  27. #include "input.h"
  28.  
  29. #if defined (READLINE)
  30. #include <readline/readline.h>
  31. #endif /* READLINE */
  32.  
  33. #include <readline/history.h>
  34.  
  35. #if defined (JOB_CONTROL)
  36. #  include "jobs.h"
  37. #endif /* JOB_CONTROL */
  38.  
  39. #define YYDEBUG 1
  40. extern int eof_encountered;
  41. extern int no_line_editing;
  42. extern int interactive, interactive_shell;
  43.  
  44. /* **************************************************************** */
  45. /*                                    */
  46. /*            "Forward" declarations                */
  47. /*                                    */
  48. /* **************************************************************** */
  49.  
  50. /* This is kind of sickening.  In order to let these variables be seen by
  51.    all the functions that need them, I am forced to place their declarations
  52.    far away from the place where they should logically be found. */
  53.  
  54. static int reserved_word_acceptable ();
  55.  
  56. /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
  57. char *ps1_prompt, *ps2_prompt;
  58.  
  59. /* Handle on the current prompt string.  Indirectly points through
  60.    ps1_ or ps2_prompt. */
  61. char **prompt_string_pointer = (char **)NULL;
  62. char *current_prompt_string;
  63.  
  64. /* The number of lines read from input while creating the current command. */
  65. int current_command_line_count = 0;
  66.  
  67. /* Variables to manage the task of reading here documents, because we need to
  68.    defer the reading until after a complete command has been collected. */
  69. REDIRECT *redirection_needing_here_doc = (REDIRECT *)NULL;
  70. int need_here_doc = 0;
  71. %}
  72.  
  73. %union {
  74.   WORD_DESC *word;        /* the word that we read. */
  75.   int number;            /* the number that we read. */
  76.   WORD_LIST *word_list;
  77.   COMMAND *command;
  78.   REDIRECT *redirect;
  79.   ELEMENT element;
  80.   PATTERN_LIST *pattern;
  81. }
  82.  
  83. /* Reserved words.  Members of the first group are only recognized
  84.    in the case that they are preceded by a list_terminator.  Members
  85.    of the second group are recognized only under special circumstances. */
  86. %token IF THEN ELSE ELIF FI CASE ESAC FOR WHILE UNTIL DO DONE FUNCTION
  87. %token IN BANG
  88.  
  89. /* More general tokens. yylex () knows how to make these. */
  90. %token <word> WORD
  91. %token <number> NUMBER
  92. %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
  93. %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
  94. %token GREATER_BAR
  95.  
  96. /* The types that the various syntactical units return. */
  97.  
  98. %type <command> inputunit command pipeline
  99. %type <command> list list0 list1 simple_list simple_list1
  100. %type <command> simple_command shell_command_1 shell_command
  101. %type <command>  group_command if_command elif_clause
  102. %type <redirect> redirection redirections
  103. %type <element> simple_command_element
  104. %type <word_list> words pattern 
  105. %type <pattern> pattern_list case_clause_sequence case_clause_1 pattern_list_1
  106.  
  107. %start inputunit
  108.  
  109. %left '&' ';' '\n' yacc_EOF
  110. %left AND_AND OR_OR
  111. %right '|'
  112. %%
  113.  
  114. inputunit:    simple_list '\n'
  115.             {
  116.               /* Case of regular command.  Discard the error
  117.                  safety net,and return the command just parsed. */
  118.               global_command = $1;
  119.               eof_encountered = 0;
  120.               discard_parser_constructs (0);
  121.               YYACCEPT;
  122.             }
  123.     |    '\n'
  124.             {
  125.               /* Case of regular command, but not a very
  126.                  interesting one.  Return a NULL command. */
  127.               global_command = (COMMAND *)NULL;
  128.               YYACCEPT;
  129.             }
  130.     |
  131.         error '\n'
  132.             {
  133.               /* Error during parsing.  Return NULL command. */
  134.               global_command = (COMMAND *)NULL;
  135.               eof_encountered = 0;
  136.               discard_parser_constructs (1);
  137.               if (interactive)
  138.                 {
  139.                   YYACCEPT;
  140.                 }
  141.               else
  142.                 {
  143.                   YYABORT;
  144.                 }
  145.             }
  146.     |    yacc_EOF
  147.             {
  148.               /* Case of EOF seen by itself.  Do ignoreeof or 
  149.                  not. */
  150.               global_command = (COMMAND *)NULL;
  151.               handle_eof_input_unit ();
  152.               YYACCEPT;
  153.             }
  154.     ;
  155.  
  156. words:    
  157.             { $$ = (WORD_LIST *)NULL; }
  158.     |    words WORD
  159.             { $$ = make_word_list ($2, $1); }
  160.     ;
  161.  
  162. redirection:    '>' WORD
  163.             { $$ = make_redirection ( 1, r_output_direction, $2); }
  164.     |    '<' WORD
  165.             { $$ = make_redirection ( 0, r_input_direction, $2); }
  166.     |    NUMBER '>' WORD
  167.             { $$ = make_redirection ($1, r_output_direction, $3); }
  168.     |    NUMBER '<' WORD
  169.             { $$ = make_redirection ($1, r_input_direction, $3); }
  170.     |    GREATER_GREATER WORD
  171.             { $$ = make_redirection ( 1, r_appending_to, $2); }
  172.     |    NUMBER GREATER_GREATER WORD
  173.             { $$ = make_redirection ($1, r_appending_to, $3); }
  174.     |    LESS_LESS WORD
  175.             {
  176.               $$ = make_redirection ( 0, r_reading_until, $2);
  177.               redirection_needing_here_doc = $$;
  178.               need_here_doc = 1;
  179.             }
  180.     |    NUMBER LESS_LESS WORD
  181.             {
  182.               $$ = make_redirection ($1, r_reading_until, $3);
  183.               redirection_needing_here_doc = $$;
  184.               need_here_doc = 1;
  185.             }
  186.     |    LESS_AND NUMBER
  187.             {
  188.               $$ = make_redirection ( 0, r_duplicating_input, $2);
  189.             }
  190.     |    NUMBER LESS_AND NUMBER
  191.             {
  192.               $$ = make_redirection ($1, r_duplicating_input, $3);
  193.             }
  194.     |    GREATER_AND NUMBER
  195.             {
  196.               $$ = make_redirection ( 1, r_duplicating_output, $2);
  197.             }
  198.     |    NUMBER GREATER_AND NUMBER
  199.             {
  200.               $$ = make_redirection ($1, r_duplicating_output, $3);
  201.             }
  202.     |    LESS_AND WORD
  203.             {
  204.               $$ = make_redirection
  205.                 (0, r_duplicating_input_word, $2);
  206.             }
  207.     |    NUMBER LESS_AND WORD
  208.             {
  209.               $$ = make_redirection
  210.                 ($1, r_duplicating_input_word, $3);
  211.             }
  212.     |    GREATER_AND WORD
  213.             {
  214.               $$ = make_redirection
  215.                 (1, r_duplicating_output_word, $2);
  216.             }
  217.     |    NUMBER GREATER_AND WORD
  218.             {
  219.               $$ = make_redirection
  220.                 ($1, r_duplicating_output_word, $3);
  221.             }
  222.     |    LESS_LESS_MINUS WORD
  223.             {
  224.               $$ = make_redirection
  225.                 (0, r_deblank_reading_until, $2);
  226.               redirection_needing_here_doc = $$;
  227.               need_here_doc = 1;
  228.             }
  229.     |    NUMBER LESS_LESS_MINUS WORD
  230.             {
  231.               $$ = make_redirection
  232.                 ($1, r_deblank_reading_until, $3);
  233.               redirection_needing_here_doc = $$;
  234.               need_here_doc = 1;
  235.             }
  236.     |    GREATER_AND '-'
  237.             { $$ = make_redirection ( 1, r_close_this, 0); }
  238.     |    NUMBER GREATER_AND '-'
  239.             { $$ = make_redirection ($1, r_close_this, 0); }
  240.     |    LESS_AND '-'
  241.             { $$ = make_redirection ( 0, r_close_this, 0); }
  242.     |    NUMBER LESS_AND '-'
  243.             { $$ = make_redirection ($1, r_close_this, 0); }
  244.     |    AND_GREATER WORD
  245.             { $$ = make_redirection ( 1, r_err_and_out, $2); }
  246.     |    NUMBER LESS_GREATER WORD
  247.             { $$ = make_redirection ( $1, r_input_output, $3); }
  248.     |    LESS_GREATER WORD
  249.             {
  250.               REDIRECT *t1, *t2;
  251.               extern WORD_DESC *copy_word ();
  252.  
  253.               t1 = make_redirection ( 0, r_input_direction, $2);
  254.               t2 = make_redirection ( 1, r_output_direction, copy_word ($2));
  255.               t1->next = t2;
  256.               $$ = t1;
  257.             }              
  258.     |    GREATER_BAR WORD
  259.             { $$ = make_redirection ( 1, r_output_force, $2); }
  260.     |    NUMBER GREATER_BAR WORD
  261.             { $$ = make_redirection ( $1, r_output_force, $3); }
  262.     ;
  263.  
  264. simple_command_element: WORD
  265.             { $$.word = $1; $$.redirect = 0; }
  266.     |    redirection
  267.             { $$.redirect = $1; $$.word = 0; }
  268.     ;
  269.  
  270. redirections:    redirection
  271.             {
  272.               $$ = $1;
  273.             }
  274.     |    redirections redirection
  275.             { 
  276.               register REDIRECT *t = $1;
  277.  
  278.               while (t->next)
  279.                 t = t->next;
  280.               t->next = $2; 
  281.               $$ = $1;
  282.             }
  283.     ;
  284.  
  285. simple_command:    simple_command_element
  286.             { $$ = make_simple_command ($1, (COMMAND *)NULL); }
  287.     |    simple_command simple_command_element
  288.             { $$ = make_simple_command ($2, $1); }
  289.     ;
  290.  
  291. command:    simple_command
  292.             { $$ = clean_simple_command ($1); }
  293.     |    shell_command
  294.             { $$ = $1; }
  295.     ;
  296.  
  297. shell_command:    shell_command_1
  298.             { $$ = $1; }
  299.     |    shell_command_1 redirections
  300.             { $1->redirects = $2; $$ = $1; }
  301.  
  302.     |    redirections shell_command_1
  303.             { $2->redirects = $1; $$ = $2; }
  304.     ;
  305.  
  306. shell_command_1: FOR WORD newlines DO list DONE
  307.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
  308.     |    FOR WORD newlines '{' list '}'
  309.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
  310.     |    FOR WORD ';' newlines DO list DONE
  311.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  312.     |    FOR WORD ';' newlines '{' list '}'
  313.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  314.  
  315.     |    FOR WORD newlines IN words list_terminator newlines DO list DONE
  316.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  317.     |    FOR WORD newlines IN words list_terminator newlines '{' list '}'
  318.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  319.  
  320.     |    CASE WORD newlines IN newlines ESAC
  321.             { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
  322.     |    CASE WORD newlines IN case_clause_sequence newlines ESAC
  323.             { $$ = make_case_command ($2, $5); }
  324.     |    CASE WORD newlines IN case_clause_1 ESAC
  325.             { /* Nobody likes this...
  326.                  report_syntax_error ("Inserted `;;'"); */
  327.               $$ = make_case_command ($2, $5); }
  328.  
  329.     |    if_command
  330.             { $$ = $1; }
  331.     |    WHILE list DO list DONE
  332.             { $$ = make_while_command ($2, $4); }
  333.     |    UNTIL list DO list DONE
  334.             { $$ = make_until_command ($2, $4); }
  335.  
  336.     |    '(' list ')'
  337.             { $2->flags |= CMD_WANT_SUBSHELL; $$ = $2; }
  338.  
  339.     |    group_command
  340.             { $$ = $1; }
  341.  
  342.     |    WORD '(' ')' newlines group_command
  343.             { $$ = make_function_def ($1, $5); }
  344.  
  345.     |    FUNCTION WORD '(' ')' newlines group_command
  346.             { $$ = make_function_def ($2, $6); }
  347.  
  348.     |    FUNCTION WORD newlines group_command
  349.             { $$ = make_function_def ($2, $4); }
  350.     ;
  351.  
  352. if_command:    IF list THEN list FI
  353.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  354.     |    IF list THEN list ELSE list FI
  355.             { $$ = make_if_command ($2, $4, $6); }
  356.     |    IF list THEN list elif_clause FI
  357.             { $$ = make_if_command ($2, $4, $5); }
  358.     ;
  359.  
  360.  
  361. group_command:    '{' list '}'
  362.             { $$ = make_group_command ($2); }
  363.     ;
  364.  
  365. elif_clause:    ELIF list THEN list
  366.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  367.     |    ELIF list THEN list ELSE list
  368.             { $$ = make_if_command ($2, $4, $6); }
  369.     |    ELIF list THEN list elif_clause
  370.             { $$ = make_if_command ($2, $4, $5); }
  371.     ;
  372.  
  373. case_clause_1:    pattern_list_1
  374.     |    case_clause_sequence pattern_list_1
  375.             { $2->next = $1; $$ = $2; }
  376.     ;
  377.  
  378. pattern_list_1:    newlines pattern ')' list
  379.             { $$ = make_pattern_list ($2, $4); }
  380.     |    newlines pattern ')' newlines
  381.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  382.     |    newlines '(' pattern ')' list
  383.             { $$ = make_pattern_list ($3, $5); }
  384.     |    newlines '(' pattern ')' newlines
  385.             { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
  386.     ;
  387.  
  388. case_clause_sequence:  pattern_list
  389.  
  390.     |    case_clause_sequence pattern_list
  391.             { $2->next = $1; $$ = $2; }
  392.     ;
  393.  
  394. pattern_list:    newlines pattern ')' list SEMI_SEMI
  395.             { $$ = make_pattern_list ($2, $4); }
  396.     |    newlines pattern ')' newlines SEMI_SEMI
  397.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  398.     |    newlines '(' pattern ')' list SEMI_SEMI
  399.             { $$ = make_pattern_list ($3, $5); }
  400.     |    newlines '(' pattern ')' newlines SEMI_SEMI
  401.             { $$ = make_pattern_list ($3, (COMMAND *)NULL); }
  402.     ;
  403.  
  404. pattern:    WORD
  405.             { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
  406.     |    pattern '|' WORD
  407.             { $$ = make_word_list ($3, $1); }
  408.     ;
  409.  
  410. /* A list allows leading or trailing newlines and
  411.    newlines as operators (equivalent to semicolons).
  412.    It must end with a newline or semicolon.
  413.    Lists are used within commands such as if, for, while.  */
  414.  
  415. list:        newlines list0
  416.             {
  417.               $$ = $2;
  418.               if (need_here_doc)
  419.                 make_here_document (redirection_needing_here_doc);
  420.               need_here_doc = 0;
  421.              }
  422.     ;
  423.  
  424. list0:        list1
  425.     |    list1 '\n' newlines
  426.     |    list1 '&' newlines
  427.             { $$ = command_connect ($1, 0, '&'); }
  428.     |    list1 ';' newlines
  429.  
  430.     ;
  431.  
  432. list1:        list1 AND_AND newlines list1
  433.             { $$ = command_connect ($1, $4, AND_AND); }
  434.     |    list1 OR_OR newlines list1
  435.             { $$ = command_connect ($1, $4, OR_OR); }
  436.     |    list1 '&' newlines list1
  437.             { $$ = command_connect ($1, $4, '&'); }
  438.     |    list1 ';' newlines list1
  439.             { $$ = command_connect ($1, $4, ';'); }
  440.     |    list1 '\n' newlines list1
  441.             { $$ = command_connect ($1, $4, ';'); }
  442.     |    pipeline
  443.             { $$ = $1; }
  444.     |    BANG pipeline
  445.             {
  446.               $2->flags |= CMD_INVERT_RETURN;
  447.               $$ = $2;
  448.             }
  449.     ;
  450.  
  451. list_terminator:'\n'
  452.     |    ';'
  453.     |    yacc_EOF
  454.     ;
  455.  
  456. newlines:
  457.     |    newlines '\n'
  458.     ;
  459.  
  460. /* A simple_list is a list that contains no significant newlines
  461.    and no leading or trailing newlines.  Newlines are allowed
  462.    only following operators, where they are not significant.
  463.  
  464.    This is what an inputunit consists of.  */
  465.  
  466. simple_list:    simple_list1
  467.             {
  468.               $$ = $1;
  469.               if (need_here_doc)
  470.                 make_here_document (redirection_needing_here_doc);
  471.               need_here_doc = 0;
  472.             }
  473.     |    simple_list1 '&'
  474.             {
  475.               $$ = command_connect ($1, (COMMAND *)NULL, '&');
  476.               if (need_here_doc)
  477.                 make_here_document (redirection_needing_here_doc);
  478.               need_here_doc = 0;
  479.             }
  480.     |    simple_list1 ';'
  481.             {
  482.               $$ = $1;
  483.               if (need_here_doc)
  484.                 make_here_document (redirection_needing_here_doc);
  485.               need_here_doc = 0;
  486.             }
  487.     ;
  488.  
  489. simple_list1:    simple_list1 AND_AND newlines simple_list1
  490.             { $$ = command_connect ($1, $4, AND_AND); }
  491.     |    simple_list1 OR_OR newlines simple_list1
  492.             { $$ = command_connect ($1, $4, OR_OR); }
  493.     |    simple_list1 '&' simple_list1
  494.             { $$ = command_connect ($1, $3, '&'); }
  495.     |    simple_list1 ';' simple_list1
  496.             { $$ = command_connect ($1, $3, ';'); }
  497.     |    pipeline
  498.             { $$ = $1; }
  499.     |    BANG pipeline
  500.             {
  501.               $2->flags |= CMD_INVERT_RETURN;
  502.               $$ = $2;
  503.             }
  504.     ;
  505.  
  506. pipeline:
  507.         pipeline '|' newlines pipeline
  508.             { $$ = command_connect ($1, $4, '|'); }
  509.     |    command
  510.             { $$ = $1; }
  511.     ;
  512. %%
  513.  
  514. /* Initial size to allocate for tokens, and the
  515.    amount to grow them by. */
  516. #define TOKEN_DEFAULT_GROW_SIZE 512
  517.  
  518. /* The token currently being read. */
  519. int current_token = 0;
  520.  
  521. /* The last read token, or NULL.  read_token () uses this for context
  522.    checking. */
  523. int last_read_token = 0;
  524.  
  525. /* The token read prior to last_read_token. */
  526. int token_before_that = 0;
  527.  
  528. /* Global var is non-zero when end of file has been reached. */
  529. int EOF_Reached = 0;
  530.  
  531. /* yy_getc () returns the next available character from input or EOF.
  532.    yy_ungetc (c) makes `c' the next character to read.
  533.    init_yy_io (get, unget, type, location) makes the function GET the
  534.    installed function for getting the next character, makes UNGET the
  535.    installed function for un-getting a character, set the type of stream
  536.    (either string or file) from TYPE, and makes LOCATION point to where
  537.    the input is coming from. */
  538.  
  539. /* Unconditionally returns end-of-file. */
  540. return_EOF ()
  541. {
  542.   return (EOF);
  543. }
  544.  
  545. /* Variable containing the current get and unget functions.
  546.    See ./input.h for a clearer description. */
  547. BASH_INPUT bash_input;
  548.  
  549. /* Set all of the fields in BASH_INPUT to NULL. */
  550. void
  551. initialize_bash_input ()
  552. {
  553.   bash_input.type = 0;
  554.   bash_input.name = (char *)NULL;
  555.   bash_input.location.file = (FILE *)NULL;
  556.   bash_input.location.string = (char *)NULL;
  557.   bash_input.getter = (Function *)NULL;
  558.   bash_input.ungetter = (Function *)NULL;
  559. }
  560.  
  561. /* Set the contents of the current bash input stream from
  562.    GET, UNGET, TYPE, NAME, and LOCATION. */
  563. init_yy_io (get, unget, type, name, location)
  564.      Function *get, *unget;
  565.      int type;
  566.      char *name;
  567.      INPUT_STREAM location;
  568. {
  569.   bash_input.type = type;
  570.   if (bash_input.name)
  571.     free (bash_input.name);
  572.  
  573.   if (name != (char *)NULL)
  574.     bash_input.name = savestring (name);
  575.   else
  576.     bash_input.name = (char *)NULL;
  577.  
  578.   bash_input.location = location;
  579.   bash_input.getter = get;
  580.   bash_input.ungetter = unget;
  581. }
  582.  
  583. /* Call this to get the next character of input. */
  584. yy_getc ()
  585. {
  586.   return (*(bash_input.getter)) ();
  587. }
  588.  
  589. /* Call this to unget C.  That is, to make C the next character
  590.    to be read. */
  591. yy_ungetc (c)
  592. {
  593.   return (*(bash_input.ungetter)) (c);
  594. }
  595.  
  596. /* **************************************************************** */
  597. /*                                    */
  598. /*          Let input be read from readline ().            */
  599. /*                                    */
  600. /* **************************************************************** */
  601.  
  602. #if defined (READLINE)
  603. char *current_readline_prompt = (char *)NULL;
  604. char *current_readline_line = (char *)NULL;
  605. int current_readline_line_index = 0;
  606.  
  607. static int readline_initialized_yet = 0;
  608. int
  609. yy_readline_get ()
  610. {
  611.   if (!current_readline_line)
  612.     {
  613.       extern sighandler sigint_sighandler ();
  614.       extern int interrupt_immediately;
  615.       extern char *readline ();
  616.       SigHandler *old_sigint;
  617. #if defined (JOB_CONTROL)
  618.       extern pid_t shell_pgrp;
  619.       extern int job_control;
  620. #endif /* JOB_CONTROL */
  621.  
  622.       if (!readline_initialized_yet)
  623.     {
  624.       initialize_readline ();
  625.       readline_initialized_yet = 1;
  626.     }
  627.  
  628. #if defined (JOB_CONTROL)
  629.       if (job_control)
  630.     give_terminal_to (shell_pgrp);
  631. #endif /* JOB_CONTROL */
  632.  
  633.       old_sigint = (SigHandler *)signal (SIGINT, sigint_sighandler);
  634.       interrupt_immediately++;
  635.  
  636.       if (!current_readline_prompt)
  637.     current_readline_line = readline ("");
  638.       else
  639.     current_readline_line = readline (current_readline_prompt);
  640.  
  641.       interrupt_immediately--;
  642.       signal (SIGINT, old_sigint);
  643.  
  644.       /* Reset the prompt to whatever is in the decoded value of
  645.      prompt_string_pointer. */
  646.       reset_readline_prompt ();
  647.  
  648.       current_readline_line_index = 0;
  649.  
  650.       if (!current_readline_line)
  651.     {
  652.       current_readline_line_index = 0;
  653.       return (EOF);
  654.     }
  655.  
  656.       current_readline_line =
  657.     (char *)xrealloc (current_readline_line,
  658.               2 + strlen (current_readline_line));
  659.       strcat (current_readline_line, "\n");
  660.     }
  661.  
  662.   if (!current_readline_line[current_readline_line_index])
  663.     {
  664.       free (current_readline_line);
  665.       current_readline_line = (char *)NULL;
  666.       return (yy_readline_get ());
  667.     }
  668.   else
  669.     {
  670.       int c = current_readline_line[current_readline_line_index++];
  671.       return (c);
  672.     }
  673. }
  674.  
  675. int
  676. yy_readline_unget (c)
  677. {
  678.   if (current_readline_line_index && current_readline_line)
  679.     current_readline_line[--current_readline_line_index] = c;
  680.   return (c);
  681. }
  682.   
  683. with_input_from_stdin ()
  684. {
  685.   INPUT_STREAM location;
  686.  
  687.   location.string = current_readline_line;
  688.   init_yy_io (yy_readline_get, yy_readline_unget,
  689.           st_string, "readline stdin", location);
  690. }
  691.  
  692. #else  /* !READLINE */
  693.  
  694. with_input_from_stdin ()
  695. {
  696.   with_input_from_stream (stdin, "stdin");
  697. }
  698. #endif    /* !READLINE */
  699.  
  700. /* **************************************************************** */
  701. /*                                    */
  702. /*   Let input come from STRING.  STRING is zero terminated.        */
  703. /*                                    */
  704. /* **************************************************************** */
  705.  
  706. int
  707. yy_string_get ()
  708. {
  709.   register char *string;
  710.   register int c;
  711.  
  712.   string = bash_input.location.string;
  713.   c = EOF;
  714.  
  715.   /* If the string doesn't exist, or is empty, EOF found. */
  716.   if (string && *string)
  717.     {
  718.       c = *string++;
  719.       bash_input.location.string = string;
  720.     }
  721.   return (c);
  722. }
  723.  
  724. int
  725. yy_string_unget (c)
  726.      int c;
  727. {
  728.   *(--bash_input.location.string) = c;
  729.   return (c);
  730. }
  731.  
  732. void
  733. with_input_from_string (string, name)
  734.      char *string;
  735.      char *name;
  736. {
  737.   INPUT_STREAM location;
  738.  
  739.   location.string = string;
  740.  
  741.   init_yy_io (yy_string_get, yy_string_unget, st_string, name, location);
  742. }
  743.  
  744. /* **************************************************************** */
  745. /*                                    */
  746. /*             Let input come from STREAM.            */
  747. /*                                    */
  748. /* **************************************************************** */
  749.  
  750. int
  751. yy_stream_get ()
  752. {
  753.   if (bash_input.location.file)
  754. #if defined (USG) || (defined (_POSIX_VERSION) && defined (Ultrix))
  755.     return (sysv_getc (bash_input.location.file));
  756. #else
  757.     return (getc (bash_input.location.file));
  758. #endif    /* !USG && !(_POSIX_VERSION && Ultrix) */
  759.   else
  760.     return (EOF);
  761. }
  762.  
  763. int
  764. yy_stream_unget (c)
  765.      int c;
  766. {
  767.   return (ungetc (c, bash_input.location.file));
  768. }
  769.  
  770. with_input_from_stream (stream, name)
  771.      FILE *stream;
  772.      char *name;
  773. {
  774.   INPUT_STREAM location;
  775.  
  776.   location.file = stream;
  777.   init_yy_io (yy_stream_get, yy_stream_unget, st_stream, name, location);
  778. }
  779.  
  780. typedef struct stream_saver {
  781.   struct stream_saver *next;
  782.   BASH_INPUT bash_input;
  783.   int line;
  784. } STREAM_SAVER;
  785.  
  786. /* The globally known line number. */
  787. int line_number = 0;
  788.  
  789. STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
  790.  
  791. push_stream ()
  792. {
  793.   STREAM_SAVER *saver = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
  794.  
  795.   bcopy (&bash_input, &(saver->bash_input), sizeof (BASH_INPUT));
  796.   saver->line = line_number;
  797.   bash_input.name = (char *)NULL;
  798.   saver->next = stream_list;
  799.   stream_list = saver;
  800.   EOF_Reached = line_number = 0;
  801. }
  802.  
  803. pop_stream ()
  804. {
  805.   if (!stream_list)
  806.     {
  807.       EOF_Reached = 1;
  808.     }
  809.   else
  810.     {
  811.       STREAM_SAVER *saver = stream_list;
  812.  
  813.       EOF_Reached = 0;
  814.       stream_list = stream_list->next;
  815.  
  816.       init_yy_io (saver->bash_input.getter,
  817.           saver->bash_input.ungetter,
  818.           saver->bash_input.type,
  819.           saver->bash_input.name,
  820.           saver->bash_input.location);
  821.  
  822.       line_number = saver->line;
  823.  
  824.       if (saver->bash_input.name)
  825.     free (saver->bash_input.name);
  826.  
  827.       free (saver);
  828.     }
  829. }
  830.  
  831. /*
  832.  * This is used to inhibit alias expansion and reserved word recognition
  833.  * inside case statement pattern lists.  A `case statement pattern list'
  834.  * is:
  835.  *    everything between the `in' in a `case word in' and the next ')'
  836.  *    or `esac'
  837.  *    everything between a `;;' and the next `)' or `esac'
  838.  */
  839. static int in_case_pattern_list = 0;
  840.  
  841. #if defined (ALIAS)
  842. /*
  843.  * Pseudo-global variables used in implementing token-wise alias expansion.
  844.  */
  845.  
  846. static int expand_next_token = 0;
  847. static char *current_token_being_expanded = (char *)NULL;
  848. static char *pending_token_being_expanded = (char *)NULL;
  849.  
  850. /*
  851.  * Pushing and popping strings.  This works together with shell_getc to 
  852.  * implement alias expansion on a per-token basis.
  853.  */
  854.  
  855. typedef struct string_saver {
  856.   struct string_saver *next;
  857.   int expand_alias;  /* Value to set expand_alias to when string is popped. */
  858.   char *saved_line;
  859.   int saved_line_size, saved_line_index, saved_line_terminator;
  860.   char *saved_token_being_expanded;
  861. } STRING_SAVER;
  862.  
  863. STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
  864.  
  865. static void save_expansion ();
  866.  
  867. /*
  868.  * Push the current shell_input_line onto a stack of such lines and make S
  869.  * the current input.  Used when expanding aliases.  EXPAND is used to set
  870.  * the value of expand_next_token when the string is popped, so that the
  871.  * word after the alias in the original line is handled correctly when the
  872.  * alias expands to multiple words.  TOKEN is the token that was expanded
  873.  * into S; it is saved and used to prevent infinite recursive expansion.
  874.  */
  875. static void
  876. push_string (s, expand, token)
  877.      char *s;
  878.      int expand;
  879.      char *token;
  880. {
  881.   extern char *shell_input_line;
  882.   extern int shell_input_line_size, shell_input_line_index,
  883.          shell_input_line_terminator;
  884.   STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
  885.  
  886.   temp->expand_alias = expand;
  887.   temp->saved_line = shell_input_line;
  888.   temp->saved_line_size = shell_input_line_size;
  889.   temp->saved_line_index = shell_input_line_index;
  890.   temp->saved_line_terminator = shell_input_line_terminator;
  891.   temp->saved_token_being_expanded = current_token_being_expanded;
  892.   temp->next = pushed_string_list;
  893.   pushed_string_list = temp;
  894.  
  895.   save_expansion (token);
  896.  
  897.   current_token_being_expanded = token;
  898.   shell_input_line = s;
  899.   shell_input_line_size = strlen (s);
  900.   shell_input_line_index = 0;
  901.   shell_input_line_terminator = '\0';
  902.   expand_next_token = 0;
  903. }
  904.  
  905. /*
  906.  * Make the top of the pushed_string stack be the current shell input.
  907.  * Only called when there is something on the stack.  Called from shell_getc
  908.  * when it thinks it has consumed the string generated by an alias expansion
  909.  * and needs to return to the original input line.
  910.  */
  911. static void
  912. pop_string ()
  913. {
  914.   extern char *shell_input_line;
  915.   extern int shell_input_line_size, shell_input_line_index,
  916.          shell_input_line_terminator;
  917.   STRING_SAVER *t;
  918.  
  919.   if (shell_input_line)
  920.     free (shell_input_line);
  921.   shell_input_line = pushed_string_list->saved_line;
  922.   shell_input_line_index = pushed_string_list->saved_line_index;
  923.   shell_input_line_size = pushed_string_list->saved_line_size;
  924.   shell_input_line_terminator = pushed_string_list->saved_line_terminator;
  925.   expand_next_token = pushed_string_list->expand_alias;
  926.   pending_token_being_expanded = pushed_string_list->saved_token_being_expanded;
  927.   t = pushed_string_list;
  928.   pushed_string_list = pushed_string_list->next;
  929.   free((char *)t);
  930. }
  931.  
  932. static void
  933. free_string_list ()
  934. {
  935.   register STRING_SAVER *t = pushed_string_list, *t1;
  936.  
  937.   while (t)
  938.     {
  939.       t1 = t->next;
  940.       if (t->saved_line)
  941.     free (t->saved_line);
  942.       if (t->saved_token_being_expanded)
  943.     free (t->saved_token_being_expanded);
  944.       free ((char *)t);
  945.       t = t1;
  946.     }
  947.   pushed_string_list = (STRING_SAVER *)NULL;
  948. }
  949.  
  950. /* This is a stack to save the values of all tokens for which alias
  951.    expansion has been performed during the current call to read_token ().
  952.    It is used to prevent alias expansion loops:
  953.  
  954.       alias foo=bar
  955.       alias bar=baz
  956.       alias baz=foo
  957.  
  958.    Ideally this would be taken care of by push and pop string, but because
  959.    of when strings are popped the stack will not contain the correct
  960.    strings to test against.  (The popping is done in shell_getc, so that when
  961.    the current string is exhausted, shell_getc can simply pop that string off
  962.    the stack, restore the previous string, and continue with the character
  963.    following the token whose expansion was originally pushed on the stack.)
  964.  
  965.    What we really want is a record of all tokens that have been expanded for
  966.    aliases during the `current' call to read_token().  This does that, at the
  967.    cost of being somewhat special-purpose (OK, OK vile and unclean).  Brian,
  968.    you had better rewrite this whole piece of garbage before the next version
  969.    is released.
  970. */
  971.  
  972. typedef struct _exp_saver {
  973.       struct _exp_saver *next;
  974.       char *saved_token;
  975. } EXPANSION_SAVER;
  976.  
  977. EXPANSION_SAVER *expanded_token_stack = (EXPANSION_SAVER *)NULL;
  978.  
  979. static void
  980. save_expansion (s)
  981.      char *s;
  982. {
  983.   EXPANSION_SAVER *t;
  984.  
  985.   t = (EXPANSION_SAVER *) xmalloc (sizeof (EXPANSION_SAVER));
  986.   t->saved_token = savestring (s);
  987.   t->next = expanded_token_stack;
  988.   expanded_token_stack = t;
  989. }
  990.  
  991. /*
  992.  * Return 1 if TOKEN has already been expanded in the current `stack' of
  993.  * expansions.  If it has been expanded already, it will appear as the value
  994.  * of saved_token for some entry in the stack of expansions created for the
  995.  * current token being expanded.
  996.  */
  997. static int
  998. token_has_been_expanded (token)
  999.      char *token;
  1000. {
  1001.   register EXPANSION_SAVER *t = expanded_token_stack;
  1002.  
  1003.   while (t)
  1004.     {
  1005.       if (STREQ (token, t->saved_token))
  1006.     return (1);
  1007.       t = t->next;
  1008.     }
  1009.   return (0);
  1010. }
  1011.  
  1012. static void
  1013. free_expansion_stack ()
  1014. {
  1015.   register EXPANSION_SAVER *t = expanded_token_stack, *t1;
  1016.  
  1017.   while (t)
  1018.     {
  1019.       t1 = t->next;
  1020.       free (t->saved_token);
  1021.       free (t);
  1022.       t = t1;
  1023.     }
  1024.   expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1025. }
  1026.  
  1027. #endif /* ALIAS */
  1028.  
  1029. /* Return a line of text, taken from wherever yylex () reads input.
  1030.    If there is no more input, then we return NULL. */
  1031. char *
  1032. read_a_line ()
  1033. {
  1034.   char *line_buffer = (char *)NULL;
  1035.   int indx = 0, buffer_size = 0;
  1036.   int c;
  1037.  
  1038.   while (1)
  1039.     {
  1040.       c = yy_getc ();
  1041.  
  1042.       if (c == 0)
  1043.     continue;
  1044.  
  1045.       /* If there is no more input, then we return NULL. */
  1046.       if (c == EOF)
  1047.     {
  1048.       c = '\n';
  1049.       if (!line_buffer)
  1050.         return ((char *)NULL);
  1051.     }
  1052.  
  1053.       /* `+2' in case the final (200'th) character in the buffer is a newline;
  1054.      otherwise the code below that NULL-terminates it will write over the
  1055.      201st slot and kill the range checking in free(). */
  1056.       if (indx + 2 > buffer_size)
  1057.     if (!buffer_size)
  1058.       line_buffer = (char *)xmalloc (buffer_size = 200);
  1059.     else
  1060.       line_buffer = (char *)xrealloc (line_buffer, buffer_size += 200);
  1061.  
  1062.       line_buffer[indx++] = c;
  1063.       if (c == '\n')
  1064.     {
  1065.       line_buffer[indx] = '\0';
  1066.       return (line_buffer);
  1067.     }
  1068.     }
  1069. }
  1070.  
  1071. /* Return a line as in read_a_line (), but insure that the prompt is
  1072.    the secondary prompt. */
  1073. char *
  1074. read_secondary_line ()
  1075. {
  1076.   prompt_string_pointer = &ps2_prompt;
  1077.   prompt_again ();
  1078.   return (read_a_line ());
  1079. }
  1080.  
  1081.  
  1082. /* **************************************************************** */
  1083. /*                                    */
  1084. /*                YYLEX ()                */
  1085. /*                                    */
  1086. /* **************************************************************** */
  1087.  
  1088. /* Reserved words.  These are only recognized as the first word of a
  1089.    command.  TOKEN_WORD_ALIST. */
  1090. STRING_INT_ALIST word_token_alist[] = {
  1091.   { "if", IF },
  1092.   { "then", THEN },
  1093.   { "else", ELSE },
  1094.   { "elif", ELIF },
  1095.   { "fi", FI },
  1096.   { "case", CASE },
  1097.   { "esac", ESAC },
  1098.   { "for", FOR },
  1099.   { "while", WHILE },
  1100.   { "until", UNTIL },
  1101.   { "do", DO },
  1102.   { "done", DONE },
  1103.   { "in", IN },
  1104.   { "function", FUNCTION },
  1105.   { "{", '{' },
  1106.   { "}", '}' },
  1107.   { "!", BANG },
  1108.   { (char *)NULL,  0}
  1109. };
  1110.  
  1111. /* Where shell input comes from.  History expansion is performed on each
  1112.    line when the shell is interactive. */
  1113. char *shell_input_line = (char *)NULL;
  1114. int shell_input_line_index = 0;
  1115. int shell_input_line_size = 0;    /* Amount allocated for shell_input_line. */
  1116. int shell_input_line_len = 0;    /* strlen (shell_input_line) */
  1117.  
  1118. /* Either zero, or EOF. */
  1119. int shell_input_line_terminator = 0;
  1120.  
  1121. /* Return the next shell input character.  This always reads characters
  1122.    from shell_input_line; when that line is exhausted, it is time to
  1123.    read the next line. */
  1124. int
  1125. shell_getc (remove_quoted_newline)
  1126.      int remove_quoted_newline;
  1127. {
  1128.   int c;
  1129.  
  1130.   QUIT;
  1131.  
  1132. #if defined (ALIAS)
  1133.   /* If shell_input_line[shell_input_line_index] == 0, but there is
  1134.      something on the pushed list of strings, then we don't want to go
  1135.      off and get another line.  We let the code down below handle it. */
  1136.  
  1137.   if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
  1138.                 (pushed_string_list == (STRING_SAVER *)NULL)))
  1139. #else /* !ALIAS */
  1140.   if (!shell_input_line || !shell_input_line[shell_input_line_index])
  1141. #endif /* !ALIAS */
  1142.     {
  1143.       register int i, l;
  1144.       char *pre_process_line (), *expansions;
  1145.  
  1146.       restart_read_next_line:
  1147.  
  1148.       line_number++;
  1149.  
  1150.     restart_read:
  1151.  
  1152.       QUIT;    /* XXX experimental */
  1153.  
  1154.       i = 0;
  1155.       shell_input_line_terminator = 0;
  1156.  
  1157. #if defined (JOB_CONTROL)
  1158.       notify_and_cleanup ();
  1159. #endif
  1160.  
  1161.       clearerr (stdin);
  1162.       while (c = yy_getc ())
  1163.     {
  1164.       if (i + 2 > shell_input_line_size)
  1165.         shell_input_line = (char *)
  1166.           xrealloc (shell_input_line, shell_input_line_size += 256);
  1167.  
  1168.       if (c == EOF)
  1169.         {
  1170.           clearerr (stdin);
  1171.  
  1172.           if (!i)
  1173.         shell_input_line_terminator = EOF;
  1174.  
  1175.           shell_input_line[i] = '\0';
  1176.           break;
  1177.         }
  1178.  
  1179.       shell_input_line[i++] = c;
  1180.  
  1181.       if (c == '\n')
  1182.         {
  1183.           shell_input_line[--i] = '\0';
  1184.           current_command_line_count++;
  1185.           break;
  1186.         }
  1187.     }
  1188.       shell_input_line_index = 0;
  1189.       shell_input_line_len = i;        /* == strlen (shell_input_line) */
  1190.  
  1191.       if (!shell_input_line || !shell_input_line[0])
  1192.     goto after_pre_process;
  1193.  
  1194.       if (interactive)
  1195.     {
  1196.       expansions = pre_process_line (shell_input_line, 1, 1);
  1197.  
  1198.       free (shell_input_line);
  1199.       shell_input_line = expansions;
  1200.       shell_input_line_len = shell_input_line ?
  1201.                  strlen (shell_input_line) :
  1202.                  0;
  1203.       /* We have to force the xrealloc below because we don't know the
  1204.          true allocated size of shell_input_line anymore. */
  1205.       shell_input_line_size = shell_input_line_len;
  1206.     }
  1207.  
  1208.   after_pre_process:
  1209.       if (shell_input_line)
  1210.     {
  1211.       if (echo_input_at_read)
  1212.         fprintf (stderr, "%s\n", shell_input_line);
  1213.     }
  1214.       else
  1215.     {
  1216.       shell_input_line_size = 0;
  1217.       prompt_string_pointer = ¤t_prompt_string;
  1218.       prompt_again ();
  1219.       goto restart_read;
  1220.     }
  1221.  
  1222.       /* Add the newline to the end of this string, iff the string does
  1223.      not already end in an EOF character.  */
  1224.       if (shell_input_line_terminator != EOF)
  1225.     {
  1226.       l = shell_input_line_len;    /* was a call to strlen */
  1227.  
  1228.       if (l + 3 > shell_input_line_size)
  1229.         shell_input_line = (char *)xrealloc (shell_input_line,
  1230.                     1 + (shell_input_line_size += 2));
  1231.  
  1232.       strcpy (shell_input_line + l, "\n");
  1233.     }
  1234.     }
  1235.   
  1236.   c = shell_input_line[shell_input_line_index];
  1237.  
  1238.   if (c)
  1239.     shell_input_line_index++;
  1240.  
  1241.   if (c == '\\' && remove_quoted_newline &&
  1242.       shell_input_line[shell_input_line_index] == '\n')
  1243.     {
  1244.     prompt_again ();
  1245.     goto restart_read_next_line;
  1246.     }
  1247.  
  1248. #if defined (ALIAS)
  1249.   /*
  1250.    * If c is NULL, we have reached the end of the current input string.  If
  1251.    * pushed_string_list is non-empty, it's time to pop to the previous string
  1252.    * because we have fully consumed the result of the last alias expansion.
  1253.    * Do it transparently; just return the next character of the string popped
  1254.    * to.  We need to hang onto current_token_being_expanded until the token
  1255.    * currently being read has been recognized; we can't restore it in
  1256.    * pop_string () because the token currently being read would be
  1257.    * inappropriately compared with it.  We defer restoration until the next
  1258.    * call to read_token ().
  1259.    */
  1260.  
  1261.   if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
  1262.     {
  1263.       pop_string ();
  1264.       c = shell_input_line[shell_input_line_index];
  1265.       if (c)
  1266.     shell_input_line_index++;
  1267.     }
  1268. #endif /* ALIAS */
  1269.  
  1270.   if (!c && shell_input_line_terminator == EOF)
  1271.     {
  1272.       if (shell_input_line_index != 0)
  1273.     return ('\n');
  1274.       else
  1275.     return (EOF);
  1276.     }
  1277.  
  1278.   return (c);
  1279. }
  1280.  
  1281. /* Put C back into the input for the shell. */
  1282. shell_ungetc (c)
  1283.      int c;
  1284. {
  1285.   if (shell_input_line && shell_input_line_index)
  1286.     shell_input_line[--shell_input_line_index] = c;
  1287. }
  1288.  
  1289. /* Discard input until CHARACTER is seen. */
  1290. discard_until (character)
  1291.      int character;
  1292. {
  1293.   int c;
  1294.  
  1295.   while ((c = shell_getc (0)) != EOF && c != character)
  1296.     ;
  1297.  
  1298.   if (c != EOF)
  1299.     shell_ungetc (c);
  1300. }
  1301.  
  1302. #if defined (HISTORY_REEDITING)
  1303. /* Tell readline () that we have some text for it to edit. */
  1304. re_edit (text)
  1305.      char *text;
  1306. {
  1307. #if defined (READLINE)
  1308.   if (strcmp (bash_input.name, "readline stdin") == 0)
  1309.     bash_re_edit (text);
  1310. #endif /* READLINE */
  1311. }
  1312. #endif /* HISTORY_REEDITING */
  1313.  
  1314. /* Non-zero means do no history expansion on this line, regardless
  1315.    of what history_expansion says. */
  1316. int history_expansion_inhibited = 0;
  1317.  
  1318. /* Do pre-processing on LINE.  If PRINT_CHANGES is non-zero, then
  1319.    print the results of expanding the line if there were any changes.
  1320.    If there is an error, return NULL, otherwise the expanded line is
  1321.    returned.  If ADDIT is non-zero the line is added to the history
  1322.    list after history expansion.  ADDIT is just a suggestion;
  1323.    REMEMBER_ON_HISTORY can veto, and does.
  1324.    Right now this does history expansion. */
  1325. char *
  1326. pre_process_line (line, print_changes, addit)
  1327.      char *line;
  1328.      int print_changes, addit;
  1329. {
  1330.   extern int remember_on_history;
  1331.   extern int history_expansion;
  1332.   extern int history_expand ();
  1333.   char *history_value;
  1334.   char *return_value;
  1335.   int expanded = 0;
  1336.  
  1337.   return_value = line;
  1338.  
  1339.   /* History expand the line.  If this results in no errors, then
  1340.      add that line to the history if ADDIT is non-zero. */
  1341.   if (!history_expansion_inhibited && history_expansion)
  1342.     {
  1343.       expanded = history_expand (line, &history_value);
  1344.  
  1345.       if (expanded)
  1346.     {
  1347.       if (print_changes)
  1348.         fprintf (stderr, "%s\n", history_value);
  1349.  
  1350.       /* If there was an error, return NULL. */
  1351.       if (expanded < 0)
  1352.         {
  1353.           free (history_value);
  1354.  
  1355. #if defined (HISTORY_REEDITING)
  1356.           /* New hack.  We can allow the user to edit the
  1357.          failed history expansion. */
  1358.           re_edit (line);
  1359. #endif /* HISTORY_REEDITING */
  1360.           return ((char *)NULL);
  1361.         }
  1362.     }
  1363.  
  1364.       /* Let other expansions know that return_value can be free'ed,
  1365.      and that a line has been added to the history list.  Note
  1366.      that we only add lines that have something in them. */
  1367.       expanded = 1;
  1368.       return_value = history_value;
  1369.     }
  1370.  
  1371.   if (addit && remember_on_history && *return_value)
  1372.     {
  1373.       extern int history_control;
  1374.  
  1375.       switch (history_control)
  1376.     {
  1377.       case 0:
  1378.         bash_add_history (return_value);
  1379.         break;
  1380.       case 1:
  1381.         if (*return_value != ' ')
  1382.           bash_add_history (return_value);
  1383.         break;
  1384.       case 2:
  1385.         {
  1386.           HIST_ENTRY *temp;
  1387.  
  1388.           using_history ();
  1389.           temp = previous_history ();
  1390.  
  1391.           if (!temp || (strcmp (temp->line, return_value) != 0))
  1392.         bash_add_history (return_value);
  1393.  
  1394.           using_history ();
  1395.         }
  1396.         break;
  1397.     }
  1398.     }
  1399.  
  1400.   if (!expanded)
  1401.     return_value = savestring (line);
  1402.  
  1403.   return (return_value);
  1404. }
  1405.  
  1406.  
  1407. /* Place to remember the token.  We try to keep the buffer
  1408.    at a reasonable size, but it can grow. */
  1409. char *token = (char *)NULL;
  1410.  
  1411. /* Current size of the token buffer. */
  1412. int token_buffer_size = 0;
  1413.  
  1414. /* Command to read_token () explaining what we want it to do. */
  1415. #define READ 0
  1416. #define RESET 1
  1417. #define prompt_is_ps1 \
  1418.       (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
  1419.  
  1420. /* Function for yyparse to call.  yylex keeps track of
  1421.    the last two tokens read, and calls read_token.  */
  1422.  
  1423. yylex ()
  1424. {
  1425.   if (interactive && (!current_token || current_token == '\n'))
  1426.     {
  1427.       /* Before we print a prompt, we might have to check mailboxes.
  1428.      We do this only if it is time to do so. Notice that only here
  1429.      is the mail alarm reset; nothing takes place in check_mail ()
  1430.      except the checking of mail.  Please don't change this. */
  1431.       if (prompt_is_ps1 && time_to_check_mail ())
  1432.     {
  1433.       check_mail ();
  1434.       reset_mail_timer ();
  1435.     }
  1436.  
  1437.       /* Allow the execution of a random command just before the printing
  1438.      of each primary prompt.  If the shell variable PROMPT_COMMAND
  1439.      is set then the value of it is the command to execute. */
  1440.       if (prompt_is_ps1)
  1441.     {
  1442.       char *command_to_execute;
  1443.  
  1444.       command_to_execute = get_string_value ("PROMPT_COMMAND");
  1445.  
  1446.       if (command_to_execute)
  1447.         {
  1448.           extern Function *last_shell_builtin, *this_shell_builtin;
  1449.           extern int last_command_exit_value;
  1450.           Function *temp_last, *temp_this;
  1451.           int temp_exit_value, temp_eof_encountered;
  1452.  
  1453.           temp_last = last_shell_builtin;
  1454.           temp_this = this_shell_builtin;
  1455.           temp_exit_value = last_command_exit_value;
  1456.           temp_eof_encountered = eof_encountered;
  1457.  
  1458.           parse_and_execute
  1459.         (savestring (command_to_execute), "PROMPT_COMMAND");
  1460.  
  1461.           last_shell_builtin = temp_last;
  1462.           this_shell_builtin = temp_this;
  1463.           last_command_exit_value = temp_exit_value;
  1464.           eof_encountered = temp_eof_encountered;
  1465.         }
  1466.     }
  1467.       prompt_again ();
  1468.     }
  1469.  
  1470.   token_before_that = last_read_token;
  1471.   last_read_token = current_token;
  1472.   current_token = read_token (READ);
  1473.   return (current_token);
  1474. }
  1475.  
  1476. /* Called from shell.c when Control-C is typed at top level.  Or
  1477.    by the error rule at top level. */
  1478. reset_parser ()
  1479. {
  1480.   read_token (RESET);
  1481. }
  1482.   
  1483. /* When non-zero, we have read the required tokens
  1484.    which allow ESAC to be the next one read. */
  1485. static int allow_esac_as_next = 0;
  1486.  
  1487. /* When non-zero, accept single '{' as a token itself. */
  1488. static int allow_open_brace = 0;
  1489.  
  1490. /* DELIMITER is the value of the delimiter that is currently
  1491.    enclosing, or zero for none. */
  1492. static int delimiter = 0;
  1493. static int old_delimiter = 0;
  1494.  
  1495. /* When non-zero, an open-brace used to create a group is awaiting a close
  1496.    brace partner. */
  1497. static int open_brace_awaiting_satisfaction = 0;
  1498.  
  1499. /* If non-zero, it is the token that we want read_token to return regardless
  1500.    of what text is (or isn't) present to be read.  read_token resets this. */
  1501. int token_to_read = 0;
  1502.  
  1503. /* Read the next token.  Command can be READ (normal operation) or 
  1504.    RESET (to normalize state). */
  1505. read_token (command)
  1506.      int command;
  1507. {
  1508.   extern int interactive_shell;    /* Whether the current shell is interactive. */
  1509.   int character;        /* Current character. */
  1510.   int peek_char;        /* Temporary look-ahead character. */
  1511.   int result;            /* The thing to return. */
  1512.   WORD_DESC *the_word;        /* The value for YYLVAL when a WORD is read. */
  1513.  
  1514.   if (token_buffer_size < TOKEN_DEFAULT_GROW_SIZE)
  1515.     {
  1516.       if (token)
  1517.     free (token);
  1518.       token = (char *)xmalloc (token_buffer_size = TOKEN_DEFAULT_GROW_SIZE);
  1519.     }
  1520.  
  1521.   if (command == RESET)
  1522.     {
  1523.       delimiter = old_delimiter = 0;
  1524.       open_brace_awaiting_satisfaction = 0;
  1525.       in_case_pattern_list = 0;
  1526.  
  1527. #if defined (ALIAS)
  1528.       if (pushed_string_list)
  1529.     {
  1530.       free_string_list ();
  1531.       pushed_string_list = (STRING_SAVER *)NULL;
  1532.     }
  1533.  
  1534.       if (pending_token_being_expanded)
  1535.     {
  1536.       free (pending_token_being_expanded);
  1537.       pending_token_being_expanded = (char *)NULL;
  1538.     }
  1539.  
  1540.       if (current_token_being_expanded)
  1541.     {
  1542.       free (current_token_being_expanded);
  1543.       current_token_being_expanded = (char *)NULL;
  1544.     }
  1545.  
  1546.       if (expanded_token_stack)
  1547.     {
  1548.       free_expansion_stack ();
  1549.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1550.     }
  1551.  
  1552.       expand_next_token = 0;
  1553. #endif /* ALIAS */
  1554.  
  1555.       if (shell_input_line)
  1556.     {
  1557.       free (shell_input_line);
  1558.       shell_input_line = (char *)NULL;
  1559.       shell_input_line_size = shell_input_line_index = 0;
  1560.     }
  1561.       last_read_token = '\n';
  1562.       token_to_read = '\n';
  1563.       return ('\n');
  1564.     }
  1565.  
  1566.   if (token_to_read)
  1567.     {
  1568.       int rt = token_to_read;
  1569.       token_to_read = 0;
  1570.       return (rt);
  1571.     }
  1572.  
  1573. #if defined (ALIAS)
  1574.   /*
  1575.    * Now we can replace current_token_being_expanded with 
  1576.    * pending_token_being_expanded, since the token that would be 
  1577.    * inappropriately compared has already been returned.
  1578.    *
  1579.    * To see why restoring current_token_being_expanded in pop_string ()
  1580.    * could be a problem, consider "alias foo=foo".  Then try to
  1581.    * expand `foo'.  The initial value of current_token_being_expanded is
  1582.    * NULL, so that is what is pushed onto pushed_string_list as the
  1583.    * value of saved_token_being_expanded.  "foo" then becomes shell_input_line.
  1584.    * read_token calls shell_getc for `f', `o', `o', and then shell_getc
  1585.    * hits the end of shell_input_line.  pushed_string_list is not empty
  1586.    * so it gets popped.  If we were to blindly restore
  1587.    * current_token_being_expanded at this point, `foo' would be compared
  1588.    * with a NULL string in the check for recursive expansion, and would
  1589.    * infinitely recurse.
  1590.    */
  1591.   if (pending_token_being_expanded)
  1592.     {
  1593.       if (current_token_being_expanded)
  1594.     free (current_token_being_expanded);
  1595.       current_token_being_expanded = pending_token_being_expanded;
  1596.       pending_token_being_expanded = (char *)NULL;
  1597.     }
  1598.  
  1599.   /* If we hit read_token () and there are no saved strings on the
  1600.      pushed_string_list, then we are no longer currently expanding a
  1601.      token.  This can't be done in pop_stream, because pop_stream
  1602.      may pop the stream before the current token has finished being
  1603.      completely expanded (consider what happens when we alias foo to foo,
  1604.      and then try to expand it). */
  1605.   if (!pushed_string_list && current_token_being_expanded)
  1606.     {
  1607.       free (current_token_being_expanded);
  1608.       current_token_being_expanded = (char *)NULL;
  1609.  
  1610.       if (expanded_token_stack)
  1611.     {
  1612.       free_expansion_stack ();
  1613.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1614.     }
  1615.     }
  1616.  
  1617.   /* This is a place to jump back to once we have successfully expanded a
  1618.      token with an alias and pushed the string with push_string () */
  1619. re_read_token:
  1620.  
  1621. #endif /* ALIAS */
  1622.  
  1623.   /* Read a single word from input.  Start by skipping blanks. */
  1624.   while ((character = shell_getc (1)) != EOF && whitespace (character));
  1625.  
  1626.   if (character == EOF)
  1627.     return (yacc_EOF);
  1628.  
  1629.   if (character == '#' && !interactive)
  1630.     {
  1631.       /* A comment.  Discard until EOL or EOF, and then return a newline. */
  1632.       discard_until ('\n');
  1633.       shell_getc (0);
  1634.  
  1635.       /* If we're about to return an unquoted newline, we can go and collect
  1636.      the text of any pending here document. */
  1637.       if (need_here_doc)
  1638.     make_here_document (redirection_needing_here_doc);
  1639.       need_here_doc = 0;
  1640.  
  1641. #if defined (ALIAS)
  1642.       expand_next_token = 0;
  1643. #endif /* ALIAS */
  1644.  
  1645.       return ('\n');
  1646.     }
  1647.  
  1648.   if (character == '\n')
  1649.     {
  1650.       /* If we're about to return an unquoted newline, we can go and collect
  1651.      the text of any pending here document. */
  1652.       if (need_here_doc)
  1653.     make_here_document (redirection_needing_here_doc);
  1654.       need_here_doc = 0;
  1655.  
  1656. #if defined (ALIAS)
  1657.       expand_next_token = 0;
  1658. #endif /* ALIAS */
  1659.  
  1660.       return (character);
  1661.     }
  1662.  
  1663.   if (member (character, "()<>;&|"))
  1664.     {
  1665. #if defined (ALIAS)
  1666.       /* Turn off alias tokenization iff this character sequence would
  1667.      not leave us ready to read a command. */
  1668.       if (character == '<' || character == '>')
  1669.     expand_next_token = 0;
  1670. #endif /* ALIAS */
  1671.  
  1672.       /* Please note that the shell does not allow whitespace to
  1673.      appear in between tokens which are character pairs, such as
  1674.      "<<" or ">>".  I believe this is the correct behaviour. */
  1675.       if (character == (peek_char = shell_getc (1)))
  1676.     {
  1677.       switch (character)
  1678.         {
  1679.           /* If '<' then we could be at "<<" or at "<<-".  We have to
  1680.          look ahead one more character. */
  1681.         case '<':
  1682.           peek_char = shell_getc (1);
  1683.           if (peek_char == '-')
  1684.         return (LESS_LESS_MINUS);
  1685.           else
  1686.         {
  1687.           shell_ungetc (peek_char);
  1688.           return (LESS_LESS);
  1689.         }
  1690.  
  1691.         case '>':
  1692.           return (GREATER_GREATER);
  1693.  
  1694.         case ';':
  1695.           in_case_pattern_list = 1;
  1696. #if defined (ALIAS)
  1697.           expand_next_token = 0;
  1698. #endif /* ALIAS */
  1699.           return (SEMI_SEMI);
  1700.  
  1701.         case '&':
  1702.           return (AND_AND);
  1703.  
  1704.         case '|':
  1705.           return (OR_OR);
  1706.         }
  1707.     }
  1708.       else
  1709.     {
  1710.       if (peek_char == '&')
  1711.         {
  1712.           switch (character)
  1713.         {
  1714.         case '<': return (LESS_AND);
  1715.         case '>': return (GREATER_AND);
  1716.         }
  1717.         }
  1718.       if (character == '<' && peek_char == '>')
  1719.         return (LESS_GREATER);
  1720.       if (character == '>' && peek_char == '|')
  1721.         return (GREATER_BAR);
  1722.       if (peek_char == '>' && character == '&')
  1723.         return (AND_GREATER);
  1724.     }
  1725.       shell_ungetc (peek_char);
  1726.  
  1727.       /* If we look like we are reading the start of a function
  1728.      definition, then let the reader know about it so that
  1729.      we will do the right thing with `{'. */
  1730.       if (character == ')' &&
  1731.       last_read_token == '(' && token_before_that == WORD)
  1732.     {
  1733.       allow_open_brace = 1;
  1734. #if defined (ALIAS)
  1735.       expand_next_token = 0;
  1736. #endif /* ALIAS */
  1737.     }
  1738.  
  1739.       if (in_case_pattern_list && (character == ')'))
  1740.     in_case_pattern_list = 0;
  1741.  
  1742.       return (character);
  1743.     }
  1744.  
  1745.   /* Hack <&- (close stdin) case. */
  1746.   if (character == '-')
  1747.     {
  1748.       switch (last_read_token)
  1749.     {
  1750.     case LESS_AND:
  1751.     case GREATER_AND:
  1752.       return (character);
  1753.     }
  1754.     }
  1755.   
  1756.   /* Okay, if we got this far, we have to read a word.  Read one,
  1757.      and then check it against the known ones. */
  1758.   {
  1759.     /* Index into the token that we are building. */
  1760.     int token_index = 0;
  1761.  
  1762.     /* ALL_DIGITS becomes zero when we see a non-digit. */
  1763.     int all_digits = digit (character);
  1764.  
  1765.     /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
  1766.     int dollar_present = 0;
  1767.  
  1768.     /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
  1769.     int quoted = 0;
  1770.  
  1771.     /* Non-zero means to ignore the value of the next character, and just
  1772.        to add it no matter what. */
  1773.     int pass_next_character = 0;
  1774.  
  1775.     /* Non-zero means parsing a dollar-paren construct.  It is the count of
  1776.        un-quoted closes we need to see. */
  1777.     int dollar_paren_level = 0;
  1778.  
  1779.     /* Non-zero means parsing a dollar-bracket construct ($[...]).  It is
  1780.        the count of un-quoted `]' characters we need to see. */
  1781.     int dollar_bracket_level = 0;
  1782.  
  1783.     /* Another level variable.  This one is for dollar_parens inside of
  1784.        double-quotes. */
  1785.     int delimited_paren_level = 0;
  1786.  
  1787.     for (;;)
  1788.       {
  1789.     if (character == EOF)
  1790.       goto got_token;
  1791.  
  1792.     if (pass_next_character)
  1793.       {
  1794.         pass_next_character = 0;
  1795.         goto got_character;
  1796.       }
  1797.  
  1798.       if (delimiter && character == '\\' && delimiter != '\'')
  1799.     {
  1800.       peek_char = shell_getc (0);
  1801.       if (peek_char != '\\')
  1802.         shell_ungetc (peek_char);
  1803.       else
  1804.         {
  1805.           token[token_index++] = character;
  1806.           goto got_character;
  1807.         }
  1808.     }
  1809.  
  1810.     /* Handle backslashes.  Quote lots of things when not inside of
  1811.        double-quotes, quote some things inside of double-quotes. */
  1812.        
  1813.     if (character == '\\' && delimiter != '\'')
  1814.       {
  1815.         peek_char = shell_getc (0);
  1816.  
  1817.         /* Backslash-newline is ignored in all cases excepting
  1818.            when quoted with single quotes. */
  1819.         if (peek_char == '\n')
  1820.           {
  1821.         character = '\n';
  1822.         goto next_character;
  1823.           }
  1824.         else
  1825.           {
  1826.         shell_ungetc (peek_char);
  1827.  
  1828.         /* If the next character is to be quoted, do it now. */
  1829.         if (!delimiter || delimiter == '`' ||
  1830.             ((delimiter == '"' ) &&
  1831.              (member (peek_char, slashify_in_quotes))))
  1832.           {
  1833.             pass_next_character++;
  1834.             quoted = 1;
  1835.             goto got_character;
  1836.           }
  1837.           }
  1838.       }
  1839.  
  1840.     /* This is a hack, in its present form.  If a backquote substitution
  1841.        appears within double quotes, everything within the backquotes
  1842.        should be read as part of a single word.  Jesus.  Now I see why
  1843.        Korn introduced the $() form. */
  1844.     if (delimiter && delimiter == '"' && character == '`')
  1845.       {
  1846.         old_delimiter = delimiter;
  1847.         delimiter = character;
  1848.         goto got_character;
  1849.       }
  1850.  
  1851.     if (delimiter)
  1852.       {
  1853.         if (character == delimiter)
  1854.           {
  1855.         if (delimited_paren_level)
  1856.           {
  1857. #if defined (NOTDEF)
  1858.             report_error ("Expected ')' before %c", character);
  1859.             return ('\n');
  1860. #else
  1861.             goto got_character;
  1862. #endif /* NOTDEF */
  1863.           }
  1864.  
  1865.         delimiter = 0;
  1866.  
  1867.         if (old_delimiter == '"' && character == '`')
  1868.           {
  1869.             delimiter = old_delimiter;
  1870.             old_delimiter = 0;
  1871.           }
  1872.  
  1873.         goto got_character;
  1874.           }
  1875.       }
  1876.  
  1877.     if (!delimiter || delimiter == '`' || delimiter == '"')
  1878.       {
  1879.         if (character == '$')
  1880.           {
  1881.         peek_char = shell_getc (1);
  1882.         shell_ungetc (peek_char);
  1883.         if (peek_char == '(')
  1884.           {
  1885.             if (!delimiter)
  1886.               dollar_paren_level++;
  1887.             else
  1888.               delimited_paren_level++;
  1889.  
  1890.             pass_next_character++;
  1891.             goto got_character;
  1892.           }
  1893.         else if (peek_char == '[')
  1894.           {
  1895.             if (!delimiter)
  1896.               dollar_bracket_level++;
  1897.  
  1898.             pass_next_character++;
  1899.             goto got_character;
  1900.           }
  1901.           }
  1902.  
  1903.         /* If we are parsing a $() or $[] construct, we need to balance
  1904.            parens and brackets inside the construct.  This whole function
  1905.            could use a rewrite. */
  1906.         if (character == '(')
  1907.           {
  1908.         if (delimiter && delimited_paren_level)
  1909.           delimited_paren_level++;
  1910.  
  1911.         if (!delimiter && dollar_paren_level)
  1912.           dollar_paren_level++;
  1913.           }
  1914.  
  1915.         if (character == '[')
  1916.           {
  1917.         if (!delimiter && dollar_bracket_level)
  1918.           dollar_bracket_level++;
  1919.           }
  1920.  
  1921.         /* This code needs to take into account whether we are inside a
  1922.            case statement pattern list, and whether this paren is supposed
  1923.            to terminate it (hey, it could happen).  It's not as simple
  1924.            as just using in_case_pattern_list, because we're not parsing
  1925.            anything while we're reading a $( ) construct.  Maybe we
  1926.            should move that whole mess into the yacc parser. */
  1927.         if (character == ')')
  1928.           {
  1929.         if (delimiter && delimited_paren_level)
  1930.           delimited_paren_level--;
  1931.  
  1932.         if (!delimiter && dollar_paren_level)
  1933.           {
  1934.             dollar_paren_level--;
  1935.             goto got_character;
  1936.           }
  1937.           }
  1938.  
  1939.         if (character == ']')
  1940.           {
  1941.         if (!delimiter && dollar_bracket_level)
  1942.           {
  1943.             dollar_bracket_level--;
  1944.             goto got_character;
  1945.           }
  1946.           }
  1947.       }
  1948.  
  1949.     if (!dollar_paren_level && !dollar_bracket_level && !delimiter &&
  1950.         member (character, " \t\n;&()|<>"))
  1951.       {
  1952.         shell_ungetc (character);
  1953.         goto got_token;
  1954.       }
  1955.     
  1956.     if (!delimiter)
  1957.       {
  1958.         if (character == '"' || character == '`' || character == '\'')
  1959.           {
  1960.         quoted = 1;
  1961.         delimiter = character;
  1962.         goto got_character;
  1963.           }
  1964.       }
  1965.  
  1966.     if (all_digits) all_digits = digit (character);
  1967.     if (character == '$') dollar_present = 1;
  1968.  
  1969.       got_character:
  1970.  
  1971.     token[token_index++] = character;
  1972.  
  1973.     if (token_index == (token_buffer_size - 1))
  1974.       token = (char *)xrealloc (token, (token_buffer_size
  1975.                         += TOKEN_DEFAULT_GROW_SIZE));
  1976.     {
  1977.       char *decode_prompt_string ();
  1978.  
  1979.     next_character:
  1980.       if (character == '\n' && interactive && bash_input.type != st_string)
  1981.         prompt_again ();
  1982.     }
  1983.     /* We want to remove quoted newlines (that is, a \<newline> pair)
  1984.        unless we are within single quotes or pass_next_character is
  1985.        set (the shell equivalent of literal-next). */
  1986.     character = shell_getc ((delimiter != '\'') && (!pass_next_character));
  1987.       }
  1988.  
  1989.   got_token:
  1990.  
  1991.     token[token_index] = '\0';
  1992.     
  1993.     if ((delimiter || dollar_paren_level || dollar_bracket_level) &&
  1994.     character == EOF)
  1995.       {
  1996.     if (dollar_paren_level && !delimiter)
  1997.       delimiter = ')';
  1998.     else if (dollar_bracket_level && !delimiter)
  1999.       delimiter = ']';
  2000.  
  2001.     report_error ("Unexpected EOF.  Looking for `%c'.", delimiter);
  2002.     return (-1);
  2003.       }
  2004.  
  2005.     if (all_digits)
  2006.       {
  2007.     /* Check to see what thing we should return.  If the last_read_token
  2008.        is a `<', or a `&', or the character which ended this token is
  2009.        a '>' or '<', then, and ONLY then, is this input token a NUMBER.
  2010.        Otherwise, it is just a word, and should be returned as such. */
  2011.  
  2012.     if ((character == '<' || character == '>') ||
  2013.         (last_read_token == LESS_AND ||
  2014.          last_read_token == GREATER_AND))
  2015.       {
  2016.         yylval.number = atoi (token); /* was sscanf (token, "%d", &(yylval.number)); */
  2017.         return (NUMBER);
  2018.       }
  2019.       }
  2020.  
  2021.     /* Handle special case.  IN is recognized if the last token
  2022.        was WORD and the token before that was FOR or CASE. */
  2023.     if ((last_read_token == WORD) &&
  2024.     ((token_before_that == FOR) || (token_before_that == CASE)) &&
  2025.     (STREQ (token, "in")))
  2026.       {
  2027.     if (token_before_that == CASE)
  2028.       {
  2029.         in_case_pattern_list = 1;
  2030.         allow_esac_as_next++;
  2031.       }
  2032.     return (IN);
  2033.       }
  2034.  
  2035.     /* Ditto for DO in the FOR case. */
  2036.     if ((last_read_token == WORD) && (token_before_that == FOR) &&
  2037.     (STREQ (token, "do")))
  2038.       return (DO);
  2039.  
  2040.     /* Ditto for ESAC in the CASE case. 
  2041.        Specifically, this handles "case word in esac", which is a legal
  2042.        construct, certainly because someone will pass an empty arg to the
  2043.        case construct, and we don't want it to barf.  Of course, we should
  2044.        insist that the case construct has at least one pattern in it, but
  2045.        the designers disagree. */
  2046.     if (allow_esac_as_next)
  2047.       {
  2048.     allow_esac_as_next--;
  2049.     if (STREQ (token, "esac"))
  2050.       {
  2051.         in_case_pattern_list = 0;
  2052.         return (ESAC);
  2053.       }
  2054.       }
  2055.  
  2056.     /* Ditto for `{' in the FUNCTION case. */
  2057.     if (allow_open_brace)
  2058.       {
  2059.     allow_open_brace = 0;
  2060.     if (STREQ (token, "{"))
  2061.       {
  2062.         open_brace_awaiting_satisfaction++;
  2063.         return ('{');
  2064.       }
  2065.       }
  2066.  
  2067. #if defined (ALIAS)
  2068.  
  2069. #define command_token_position(token) \
  2070.     ((token) != SEMI_SEMI && reserved_word_acceptable (token))
  2071.  
  2072.     /* OK, we have a token.  Let's try to alias expand it, if (and only if)
  2073.        it's eligible. 
  2074.  
  2075.        It is eligible for expansion if the shell is in interactive mode, and
  2076.        the token is unquoted and the last token read was a command
  2077.        separator (or expand_next_token is set), and we are currently
  2078.        processing an alias (pushed_string_list is non-empty) and this
  2079.        token is not the same as the current or any previously
  2080.        processed alias.
  2081.  
  2082.        Special cases that disqualify:
  2083.      In a pattern list in a case statement (in_case_pattern_list). */
  2084.     if (interactive_shell && !quoted && !in_case_pattern_list &&
  2085.     (command_token_position (last_read_token) || expand_next_token))
  2086.       {
  2087.     char *alias_expand_word (), *expanded;
  2088.     if (current_token_being_expanded &&
  2089.          ((STREQ (token, current_token_being_expanded)) ||
  2090.           (token_has_been_expanded (token))))
  2091.       goto no_expansion;
  2092.  
  2093.     expanded = alias_expand_word (token);
  2094.     if (expanded)
  2095.       {
  2096.         int len = strlen (expanded), expand_next;
  2097.         char *temp;
  2098.  
  2099.         /* Erase the current token. */
  2100.         token_index = 0;
  2101.  
  2102.         expand_next = (expanded[len - 1] == ' ') ||
  2103.               (expanded[len - 1] == '\t');
  2104.  
  2105.         temp = savestring (token);
  2106.         push_string (expanded, expand_next, temp);
  2107.         goto re_read_token;
  2108.       }
  2109.     else
  2110.       /* This is an eligible token that does not have an expansion. */
  2111. no_expansion:
  2112.       expand_next_token = 0;
  2113.       }
  2114.     else
  2115.       {
  2116.     expand_next_token = 0;
  2117.       }
  2118. #endif /* ALIAS */
  2119.  
  2120.     /* Check to see if it is a reserved word.  */
  2121.     if (!dollar_present && !quoted &&
  2122.     reserved_word_acceptable (last_read_token))
  2123.       {
  2124.     int i;
  2125.     for (i = 0; word_token_alist[i].word != (char *)NULL; i++)
  2126.       if (STREQ (token, word_token_alist[i].word))
  2127.         {
  2128.           if (in_case_pattern_list && (word_token_alist[i].token != ESAC))
  2129.         break;
  2130.  
  2131.           if (word_token_alist[i].token == ESAC)
  2132.         in_case_pattern_list = 0;
  2133.  
  2134.           if (word_token_alist[i].token == '{')
  2135.         open_brace_awaiting_satisfaction++;
  2136.  
  2137.           return (word_token_alist[i].token);
  2138.         }
  2139.       }
  2140.  
  2141.     /* What if we are attempting to satisfy an open-brace grouper? */
  2142.     if (open_brace_awaiting_satisfaction && strcmp (token, "}") == 0)
  2143.       {
  2144.     open_brace_awaiting_satisfaction--;
  2145.     return ('}');
  2146.       }
  2147.  
  2148.     the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  2149.     the_word->word = (char *)xmalloc (1 + strlen (token));
  2150.     strcpy (the_word->word, token);
  2151.     the_word->dollar_present = dollar_present;
  2152.     the_word->quoted = quoted;
  2153.     the_word->assignment = assignment (token);
  2154.  
  2155.     yylval.word = the_word;
  2156.     result = WORD;
  2157.     if (last_read_token == FUNCTION)
  2158.       allow_open_brace = 1;
  2159.   }
  2160.   return (result);
  2161. }
  2162.  
  2163. #if defined (NOTDEF)        /* Obsoleted function no longer used. */
  2164. /* Return 1 if this token is a legal shell `identifier'; that is, it consists
  2165.    solely of letters, digits, and underscores, and does not begin with a 
  2166.    digit. */
  2167. legal_identifier (name)
  2168.      char *name;
  2169. {
  2170.   register char *s;
  2171.  
  2172.   if (!name || !*name)
  2173.     return (0);
  2174.  
  2175.   if (digit (*name))
  2176.     return (0);
  2177.  
  2178.   for (s = name; s && *s; s++)
  2179.     {
  2180.       if (!isletter (*s) && !digit (*s) && (*s != '_'))
  2181.     return (0);
  2182.     }
  2183.   return (1);
  2184. }
  2185. #endif /* NOTDEF */
  2186.  
  2187. /* Return 1 if TOKEN is a token that after being read would allow
  2188.    a reserved word to be seen, else 0. */
  2189. static int
  2190. reserved_word_acceptable (token)
  2191.      int token;
  2192. {
  2193.   if (member (token, "\n;()|&{") ||
  2194.       token == AND_AND ||
  2195.       token == BANG ||
  2196.       token == DO ||
  2197.       token == ELIF ||
  2198.       token == ELSE ||
  2199.       token == IF ||
  2200.       token == OR_OR ||
  2201.       token == SEMI_SEMI ||
  2202.       token == THEN ||
  2203.       token == UNTIL ||
  2204.       token == WHILE ||
  2205.       token == 0)
  2206.     return (1);
  2207.   else
  2208.     return (0);
  2209. }
  2210.  
  2211. #if defined (READLINE)
  2212. /* Called after each time readline is called.  This insures that whatever
  2213.    the new prompt string is gets propagated to readline's local prompt
  2214.    variable. */
  2215. reset_readline_prompt ()
  2216. {
  2217.   if (prompt_string_pointer && *prompt_string_pointer)
  2218.     {
  2219.       char *temp_prompt, *decode_prompt_string ();
  2220.  
  2221.       temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2222.  
  2223.       if (!temp_prompt)
  2224.     temp_prompt = savestring ("");
  2225.  
  2226.       if (current_readline_prompt)
  2227.     free (current_readline_prompt);
  2228.  
  2229.       current_readline_prompt = temp_prompt;
  2230.     }
  2231. }
  2232. #endif
  2233.  
  2234. /* A list of tokens which can be followed by newlines, but not by
  2235.    semi-colons.  When concatenating multiple lines of history, the
  2236.    newline separator for such tokens is replaced with a space. */
  2237. int no_semi_successors[] = {
  2238.   '\n', '{', '(', ')', ';',
  2239.   CASE, DO, ELSE, IF, IN, SEMI_SEMI, THEN, UNTIL, WHILE,
  2240.   0
  2241. };
  2242.  
  2243. /* Add a line to the history list.
  2244.    The variable COMMAND_ORIENTED_HISTORY controls the style of history
  2245.    remembering;  when non-zero, and LINE is not the first line of a
  2246.    complete parser construct, append LINE to the last history line instead
  2247.    of adding it as a new line. */
  2248. bash_add_history (line)
  2249.      char *line;
  2250. {
  2251.   extern int command_oriented_history;
  2252.   int add_it = 1;
  2253.  
  2254.   if (command_oriented_history && current_command_line_count > 1)
  2255.     {
  2256.       register int offset;
  2257.       register HIST_ENTRY *current, *old;
  2258.       char *chars_to_add, *new_line;
  2259.  
  2260.       /* If we are not within a delimited expression, try to be smart
  2261.      about which separators can be semi-colons and which must be
  2262.      newlines. */
  2263.       if (!delimiter)
  2264.     {
  2265.       register int i;
  2266.  
  2267.       chars_to_add = (char *)NULL;
  2268.  
  2269.       for (i = 0; no_semi_successors[i]; i++)
  2270.         {
  2271.           if (token_before_that == no_semi_successors[i])
  2272.         {
  2273.           chars_to_add = " ";
  2274.           break;
  2275.         }
  2276.         }
  2277.       if (!chars_to_add)
  2278.         chars_to_add = "; ";
  2279.     }
  2280.       else
  2281.     chars_to_add = "\n";
  2282.  
  2283.       using_history ();
  2284.  
  2285.       current = previous_history ();
  2286.  
  2287.       if (current)
  2288.     {
  2289.       offset = where_history ();
  2290.       new_line = (char *) xmalloc (1
  2291.                        + strlen (current->line)
  2292.                        + strlen (line)
  2293.                        + strlen (chars_to_add));
  2294.       sprintf (new_line, "%s%s%s", current->line, chars_to_add, line);
  2295.       old = replace_history_entry (offset, new_line, current->data);
  2296.       free (new_line);
  2297.  
  2298.       if (old)
  2299.         {
  2300.           /* Note that the old data is not freed, since it was simply
  2301.          copied to the new history entry. */
  2302.           if (old->line)
  2303.         free (old->line);
  2304.  
  2305.           free (old);
  2306.         }
  2307.       add_it = 0;
  2308.     }
  2309.     }
  2310.  
  2311.   if (add_it)
  2312.     {
  2313.       extern int history_lines_this_session;
  2314.  
  2315.       add_history (line);
  2316.       history_lines_this_session++;
  2317.     }
  2318.   using_history ();
  2319. }
  2320.  
  2321. /* Issue a prompt, or prepare to issue a prompt when the next character
  2322.    is read. */
  2323. prompt_again ()
  2324. {
  2325.   char *temp_prompt, *decode_prompt_string ();
  2326.  
  2327.   ps1_prompt = get_string_value ("PS1");
  2328.   ps2_prompt = get_string_value ("PS2");
  2329.  
  2330.   if (!prompt_string_pointer)
  2331.     prompt_string_pointer = &ps1_prompt;
  2332.  
  2333.   if (*prompt_string_pointer)
  2334.     temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2335.   else
  2336.     temp_prompt = savestring ("");
  2337.  
  2338.   current_prompt_string = *prompt_string_pointer;
  2339.   prompt_string_pointer = &ps2_prompt;
  2340.  
  2341. #if defined (READLINE)
  2342.   if (!no_line_editing)
  2343.     {
  2344.       if (current_readline_prompt)
  2345.     free (current_readline_prompt);
  2346.       
  2347.       current_readline_prompt = temp_prompt;
  2348.     }
  2349.   else
  2350. #endif    /* READLINE */
  2351.     {
  2352.       if (interactive)
  2353.     {
  2354.       fprintf (stderr, "%s", temp_prompt);
  2355.       fflush (stderr);
  2356.     }
  2357.       free (temp_prompt);
  2358.     }
  2359. }
  2360.  
  2361. #include "maxpath.h"
  2362.  
  2363. /* Return a string which will be printed as a prompt.  The string
  2364.    may contain special characters which are decoded as follows:
  2365.    
  2366.     \t    the time
  2367.     \d    the date
  2368.     \n    CRLF
  2369.     \s    the name of the shell
  2370.     \w    the current working directory
  2371.     \W    the last element of PWD
  2372.     \u    your username
  2373.     \h    the hostname
  2374.     \#    the command number of this command
  2375.     \!    the history number of this command
  2376.     \$    a $ or a # if you are root
  2377.     \<octal> character code in octal
  2378.     \\    a backslash
  2379. */
  2380. #include <sys/param.h>
  2381. #include <time.h>
  2382.  
  2383. #define PROMPT_GROWTH 50
  2384. char *
  2385. decode_prompt_string (string)
  2386.      char *string;
  2387. {
  2388.   int result_size = PROMPT_GROWTH;
  2389.   int result_index = 0;
  2390.   char *result = (char *)xmalloc (PROMPT_GROWTH);
  2391.   int c;
  2392.   char *temp = (char *)NULL;
  2393.  
  2394.   result[0] = 0;
  2395.   while (c = *string++)
  2396.     {
  2397.       if (c == '\\')
  2398.     {
  2399.       c = *string;
  2400.  
  2401.       switch (c)
  2402.         {
  2403.         case '0':
  2404.         case '1':
  2405.         case '2':
  2406.         case '3':
  2407.         case '4':
  2408.         case '5':
  2409.         case '6':
  2410.         case '7':
  2411.           {
  2412.         char octal_string[4];
  2413.         int n;
  2414.  
  2415.         strncpy (octal_string, string, 3);
  2416.         octal_string[3] = '\0';
  2417.  
  2418.         n = read_octal (octal_string);
  2419.  
  2420.         temp = savestring ("\\");
  2421.         if (n != -1)
  2422.           {
  2423.             string += 3;
  2424.             temp[0] = n;
  2425.           }
  2426.  
  2427.         c = 0;
  2428.         goto add_string;
  2429.           }
  2430.       
  2431.         case 't':
  2432.         case 'd':
  2433.           /* Make the current time/date into a string. */
  2434.           {
  2435.         long the_time = time (0);
  2436.         char *ttemp = ctime (&the_time);
  2437.         temp = savestring (ttemp);
  2438.  
  2439.         if (c == 't')
  2440.           {
  2441.             strcpy (temp, temp + 11);
  2442.             temp[8] = '\0';
  2443.           }
  2444.         else
  2445.           temp[10] = '\0';
  2446.  
  2447.         goto add_string;
  2448.           }
  2449.  
  2450.         case 'n':
  2451.           if (!no_line_editing)
  2452.         temp = savestring ("\r\n");
  2453.           else
  2454.         temp = savestring ("\n");
  2455.           goto add_string;
  2456.  
  2457.         case 's':
  2458.           {
  2459.         extern char *shell_name;
  2460.         extern char *base_pathname ();
  2461.  
  2462.         temp = base_pathname (shell_name);
  2463.         temp = savestring (temp);
  2464.         goto add_string;
  2465.           }
  2466.     
  2467.         case 'w':
  2468.         case 'W':
  2469.           {
  2470.         /* Use the value of PWD because it is much more effecient. */
  2471. #define EFFICIENT
  2472. #ifdef EFFICIENT
  2473.         char *polite_directory_format (), t_string[MAXPATHLEN];
  2474.  
  2475.         temp = get_string_value ("PWD");
  2476.  
  2477.         if (!temp)
  2478.           getwd (t_string);
  2479.         else
  2480.           strcpy (t_string, temp);
  2481. #else
  2482.         getwd (t_string);
  2483. #endif    /* EFFICIENT */
  2484.  
  2485.         if (c == 'W')
  2486.           {
  2487.             char *dir = (char *)rindex (t_string, '/');
  2488.             if (dir && dir != t_string)
  2489.               strcpy (t_string, dir + 1);
  2490.             temp = savestring (t_string);
  2491.           }
  2492.         else
  2493.           temp = savestring (polite_directory_format (t_string));
  2494.         goto add_string;
  2495.           }
  2496.       
  2497.         case 'u':
  2498.           {
  2499.         extern char *current_user_name;
  2500.         temp = savestring (current_user_name);
  2501.  
  2502.         goto add_string;
  2503.           }
  2504.  
  2505.         case 'h':
  2506.           {
  2507.         extern char *current_host_name;
  2508.         char *t_string;
  2509.  
  2510.         temp = savestring (current_host_name);
  2511.         if (t_string = (char *)index (temp, '.'))
  2512.           *t_string = '\0';
  2513.         
  2514.         goto add_string;
  2515.           }
  2516.  
  2517.         case '#':
  2518.           {
  2519.         extern int current_command_number;
  2520.         char number_buffer[20];
  2521.         sprintf (number_buffer, "%d", current_command_number);
  2522.         temp = savestring (number_buffer);
  2523.         goto add_string;
  2524.           }
  2525.  
  2526.         case '!':
  2527.           {
  2528.         extern int history_base, where_history ();
  2529.         char number_buffer[20];
  2530.  
  2531.         using_history ();
  2532.         if (get_string_value ("HISTSIZE"))
  2533.           sprintf (number_buffer, "%d",
  2534.                history_base + where_history ());
  2535.         else
  2536.           strcpy (number_buffer, "!");
  2537.         temp = savestring (number_buffer);
  2538.         goto add_string;
  2539.           }
  2540.  
  2541.         case '$':
  2542.           temp = savestring (geteuid () == 0 ? "#" : "$");
  2543.           goto add_string;
  2544.  
  2545.         case '\\':
  2546.           temp = savestring ("\\");
  2547.           goto add_string;
  2548.  
  2549.         default:
  2550.           temp = savestring ("\\ ");
  2551.           temp[1] = c;
  2552.  
  2553.         add_string:
  2554.           if (c)
  2555.         string++;
  2556.           result =
  2557.         (char *)sub_append_string (temp, result,
  2558.                        &result_index, &result_size);
  2559.           temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */
  2560.           result[result_index] = '\0';
  2561.           break;
  2562.         }
  2563.     }
  2564.       else
  2565.     {
  2566.       while (3 + result_index > result_size)
  2567.         result = (char *)xrealloc (result, result_size += PROMPT_GROWTH);
  2568.  
  2569.       result[result_index++] = c;
  2570.       result[result_index] = '\0';
  2571.     }
  2572.     }
  2573.  
  2574.   /* I don't really think that this is a good idea.  Do you? */
  2575.   if (!find_variable ("NO_PROMPT_VARS"))
  2576.     {
  2577.       WORD_LIST *expand_string (), *list;
  2578.       char *string_list ();
  2579.  
  2580.       list = expand_string (result, 1);
  2581.       free (result);
  2582.       result = string_list (list);
  2583.       dispose_words (list);
  2584.     }
  2585.  
  2586.   return (result);
  2587. }
  2588.  
  2589. /* Report a syntax error, and restart the parser.  Call here for fatal
  2590.    errors. */
  2591. yyerror ()
  2592. {
  2593.   report_syntax_error ((char *)NULL);
  2594.   reset_parser ();
  2595. }
  2596.  
  2597. /* Report a syntax error with line numbers, etc.
  2598.    Call here for recoverable errors.  If you have a message to print,
  2599.    then place it in MESSAGE, otherwise pass NULL and this will figure
  2600.    out an appropriate message for you. */
  2601. report_syntax_error (message)
  2602.      char *message;
  2603. {
  2604.   if (message)
  2605.     {
  2606.       if (!interactive)
  2607.     {
  2608.       char *name = bash_input.name ? bash_input.name : "stdin";
  2609.       report_error ("%s:%d: `%s'", name, line_number, message);
  2610.     }
  2611.       else
  2612.     report_error ("%s", message);
  2613.  
  2614.       return;
  2615.     }
  2616.  
  2617.   if (shell_input_line && *shell_input_line)
  2618.     {
  2619.       char *error_token, *t = shell_input_line;
  2620.       register int i = shell_input_line_index;
  2621.       int token_end = 0;
  2622.  
  2623.       if (!t[i] && i)
  2624.     i--;
  2625.  
  2626.       while (i && (t[i] == ' ' || t[i] == '\t' || t[i] == '\n'))
  2627.     i--;
  2628.  
  2629.       if (i)
  2630.     token_end = i + 1;
  2631.  
  2632.       while (i && !member (t[i], " \n\t;|&"))
  2633.     i--;
  2634.  
  2635.       while (i != token_end && member (t[i], " \n\t"))
  2636.     i++;
  2637.  
  2638.       if (token_end)
  2639.     {
  2640.       error_token = (char *)alloca (1 + (token_end - i));
  2641.       strncpy (error_token, t + i, token_end - i);
  2642.       error_token[token_end - i] = '\0';
  2643.  
  2644.       report_error ("syntax error near `%s'", error_token);
  2645.     }
  2646.       else if ((i == 0) && (token_end == 0))    /* a 1-character token */
  2647.     {
  2648.       error_token = (char *) alloca (2);
  2649.       strncpy(error_token, t + i, 1);
  2650.       error_token[1] = '\0';
  2651.  
  2652.       report_error ("syntax error near `%s'", error_token);
  2653.     }
  2654.  
  2655.       if (!interactive)
  2656.     {
  2657.       char *temp = savestring (shell_input_line);
  2658.       char *name = bash_input.name ? bash_input.name : "stdin";
  2659.       int l = strlen (temp);
  2660.  
  2661.       while (l && temp[l - 1] == '\n')
  2662.         temp[--l] = '\0';
  2663.  
  2664.       report_error ("%s:%d: `%s'", name, line_number, temp);
  2665.       free (temp);
  2666.     }
  2667.     }
  2668.   else
  2669.     report_error ("Syntax error");
  2670. }
  2671.  
  2672. /* ??? Needed function. ??? We have to be able to discard the constructs
  2673.    created during parsing.  In the case of error, we want to return
  2674.    allocated objects to the memory pool.  In the case of no error, we want
  2675.    to throw away the information about where the allocated objects live.
  2676.    (dispose_command () will actually free the command. */
  2677. discard_parser_constructs (error_p)
  2678.      int error_p;
  2679. {
  2680. /*   if (error_p) {
  2681.      fprintf (stderr, "*");
  2682.   } */
  2683. }
  2684.    
  2685. /* Do that silly `type "bye" to exit' stuff.  You know, "ignoreeof". */
  2686.  
  2687. /* The number of times that we have encountered an EOF character without
  2688.    another character intervening.  When this gets above the limit, the
  2689.    shell terminates. */
  2690. int eof_encountered = 0;
  2691.  
  2692. /* The limit for eof_encountered. */
  2693. int eof_encountered_limit = 10;
  2694.  
  2695. /* If we have EOF as the only input unit, this user wants to leave
  2696.    the shell.  If the shell is not interactive, then just leave.
  2697.    Otherwise, if ignoreeof is set, and we haven't done this the
  2698.    required number of times in a row, print a message. */
  2699. handle_eof_input_unit ()
  2700. {
  2701.   extern int login_shell, EOF_Reached;
  2702.  
  2703.   if (interactive)
  2704.     {
  2705.       /* If the user wants to "ignore" eof, then let her do so, kind of. */
  2706.       if (find_variable ("ignoreeof") || find_variable ("IGNOREEOF"))
  2707.     {
  2708.       if (eof_encountered < eof_encountered_limit)
  2709.         {
  2710.           fprintf (stderr, "Use \"%s\" to leave the shell.\n",
  2711.                login_shell ? "logout" : "exit");
  2712.           eof_encountered++;
  2713.           /* Reset the prompt string to be $PS1. */
  2714.           prompt_string_pointer = (char **)NULL;
  2715.           prompt_again ();
  2716.           last_read_token = current_token = '\n';
  2717.           return;
  2718.         } 
  2719.     }
  2720.  
  2721.       /* In this case EOF should exit the shell.  Do it now. */
  2722.       reset_parser ();
  2723.       exit_builtin ((WORD_LIST *)NULL);
  2724.     }
  2725.   else
  2726.     {
  2727.       /* We don't write history files, etc., for non-interactive shells. */
  2728.       EOF_Reached = 1;
  2729.     }
  2730. }
  2731.