From 8403aae92886de1e0bd379c94b3715b6df6dc0e4 Mon Sep 17 00:00:00 2001
From: ridiculousfish <corydoras@ridiculousfish.com>
Date: Sun, 22 Jan 2012 21:57:30 -0800
Subject: [PATCH] Convert some cases where parsers are created to using
 PARSER_TYPE_GENERAL

---
 env.cpp    |  2 +-
 exec.cpp   |  2 +-
 fish.cpp   |  2 +-
 parser.cpp | 67 ++++++++++++++++--------------------------------------
 parser.h   | 40 ++++++++++++++++++++++++++++++++
 reader.cpp |  2 +-
 6 files changed, 63 insertions(+), 52 deletions(-)

diff --git a/env.cpp b/env.cpp
index d2d1aab0d..c4cc97152 100644
--- a/env.cpp
+++ b/env.cpp
@@ -258,7 +258,7 @@ static void start_fishd()
 	}
 	
     wcstring cmd = format_string(FISHD_CMD, pw->pw_name);	
-    parser_t parser(PARSER_TYPE_GENERAL);
+    parser_t &parser = parser_t::principal_parser();
 	parser.eval( cmd.c_str(), 0, TOP );
 }
 
diff --git a/exec.cpp b/exec.cpp
index dfaf2de74..baf1e14d2 100644
--- a/exec.cpp
+++ b/exec.cpp
@@ -1765,7 +1765,7 @@ int exec_subshell( const wchar_t *cmd,
 	
 	prev_status = proc_get_last_status();
 	
-    parser_t parser(PARSER_TYPE_GENERAL);
+    parser_t &parser = parser_t::principal_parser();
 	if( parser.eval( cmd, io_buffer, SUBST ) )
 	{
 		status = -1;
diff --git a/fish.cpp b/fish.cpp
index 6fe9f7a8f..734993d2c 100644
--- a/fish.cpp
+++ b/fish.cpp
@@ -77,7 +77,7 @@ static int read_init()
 	wchar_t *config_dir_escaped;
 	void *context;
 	string_buffer_t *eval_buff;
-    parser_t parser(PARSER_TYPE_GENERAL);
+    parser_t &parser = parser_t::principal_parser();
 
 	parser.eval( L"builtin . " DATADIR "/fish/config.fish 2>/dev/null", 0, TOP );
 	parser.eval( L"builtin . " SYSCONFDIR L"/fish/config.fish 2>/dev/null", 0, TOP );
diff --git a/parser.cpp b/parser.cpp
index 0969de115..6aa25ec5c 100644
--- a/parser.cpp
+++ b/parser.cpp
@@ -353,51 +353,22 @@ static const struct block_lookup_entry block_lookup[]=
 	{
 		0, 0, 0
 	}
-}
-	;
+};
 
-/** Last error code */
-static int error_code;
 
-event_block_t *global_event_block=0;
-
-io_data_t *block_io;
-
-/** Position of last error */
-
-static int err_pos;
-
-/** Description of last error */
-static string_buffer_t *err_buff=0;
-
-/** Pointer to the current tokenizer */
-static tokenizer *current_tokenizer;
-
-/** String for representing the current line */
-static string_buffer_t  *lineinfo=0;
-
-/** This is the position of the beginning of the currently parsed command */
-static int current_tokenizer_pos;
-
-/** The current innermost block */
-block_t *current_block=0;
-
-/** List of called functions, used to help prevent infinite recursion */
-static std::vector<wcstring> forbidden_function;
-
-/**
-   String index where the current job started.
-*/
-static int job_start_pos;
-
-/**
-   Keeps track of how many recursive eval calls have been made. Eval
-   doesn't call itself directly, recursion happens on blocks and on
-   command substitutions.
-*/
-static int eval_level=-1;
-
-parser_t::parser_t(enum parser_type_t type) : parser_type(type)
+parser_t::parser_t(enum parser_type_t type) :
+    parser_type(type),
+    error_code(0),
+    err_pos(0),
+    err_buff(NULL),
+    current_tokenizer(NULL),
+    lineinfo(NULL),
+    current_tokenizer_pos(0),
+    job_start_pos(0),
+    eval_level(-1),
+    current_block(NULL),
+    global_event_block(NULL),
+    block_io(NULL)
 {
     
 }
@@ -787,7 +758,7 @@ void parser_t::print_errors( string_buffer_t *target, const wchar_t *prefix )
 /**
    Print error message to stderr if an error has occured while parsing
 */
-static void print_errors_stderr(parser_t &parser)
+void parser_t::print_errors_stderr()
 {
 	if( error_code && err_buff )
 	{
@@ -797,7 +768,7 @@ static void print_errors_stderr(parser_t &parser)
 		tmp = current_tokenizer_pos;
 		current_tokenizer_pos = err_pos;
 		
-		fwprintf( stderr, L"%ls", parser.current_line() );
+		fwprintf( stderr, L"%ls", this->current_line() );
 		
 		current_tokenizer_pos=tmp;
 	}
@@ -876,7 +847,7 @@ int parser_t::eval_args( const wchar_t *line, array_list_t *args )
 		}
 	}
 
-	print_errors_stderr(*this);
+	this->print_errors_stderr();
 	
 	tok_destroy( &tok );
 	
@@ -996,7 +967,7 @@ void parser_t::stack_trace( block_t *b, string_buffer_t *buff)
    moving down the block-scope-stack, checking every block if it is of
    type FUNCTION_CALL.
 */
-static const wchar_t *is_function()
+const wchar_t *parser_t::is_function() const
 {
 	block_t *b = current_block;
 	while( 1 )
@@ -2558,7 +2529,7 @@ int parser_t::eval( const wcstring &cmdStr, io_data_t *io, enum block_type_t blo
 		parser_t::pop_block();
 	}
 
-	print_errors_stderr(*this);
+	this->print_errors_stderr();
 
 	tok_destroy( current_tokenizer );
 	free( current_tokenizer );
diff --git a/parser.h b/parser.h
index 6ffd89e28..5ecab5bd7 100644
--- a/parser.h
+++ b/parser.h
@@ -217,16 +217,56 @@ class parser_t {
     enum parser_type_t parser_type;
     std::vector<block_t> blocks;
     
+    /** Last error code */
+    int error_code;
+    
+    /** Position of last error */
+    int err_pos;
+    
+    /** Description of last error */
+    string_buffer_t *err_buff;
+    
+    /** Pointer to the current tokenizer */
+    tokenizer *current_tokenizer;
+    
+    /** String for representing the current line */
+    string_buffer_t  *lineinfo;
+    
+    /** This is the position of the beginning of the currently parsed command */
+    int current_tokenizer_pos;
+    
+    /** List of called functions, used to help prevent infinite recursion */
+    std::vector<wcstring> forbidden_function;
+    
+    /** String index where the current job started. */
+    int job_start_pos;
+    
+    /**
+       Keeps track of how many recursive eval calls have been made. Eval
+       doesn't call itself directly, recursion happens on blocks and on
+       command substitutions.
+    */
+    int eval_level;
+    
     /* No copying allowed */
     parser_t(const parser_t&);
     parser_t& operator=(const parser_t&);
     
+    /**
+       Returns the name of the currently evaluated function if we are
+       currently evaluating a function, null otherwise. This is tested by
+       moving down the block-scope-stack, checking every block if it is of
+       type FUNCTION_CALL.
+    */
+    const wchar_t *is_function() const;
+    
     void parse_job_argument_list( process_t *p, job_t *j, tokenizer *tok, array_list_t *args );
     int parse_job( process_t *p, job_t *j, tokenizer *tok );
     void skipped_exec( job_t * j );
     void eval_job( tokenizer *tok );
     int parser_test_argument( const wchar_t *arg, string_buffer_t *out, const wchar_t *prefix, int offset );
     void print_errors( string_buffer_t *target, const wchar_t *prefix );
+    void print_errors_stderr();
     
     public:
     std::vector<profile_item_t> profile_items;
diff --git a/reader.cpp b/reader.cpp
index 490dce3aa..f4b9c3ac6 100644
--- a/reader.cpp
+++ b/reader.cpp
@@ -1257,7 +1257,7 @@ static void run_pager( wchar_t *prefix, int is_quoted, array_list_t *comp )
 	out->next = in;
 	out->fd = 4;
 	
-    parser_t parser(PARSER_TYPE_GENERAL);
+    parser_t &parser = parser_t::principal_parser();
 	parser.eval( (wchar_t *)cmd.buff, out, TOP);
 	term_steal();