finalize support for sharing scanner/parser between multiple connections

[email protected] Tue, 22 Nov 2005 10:53:02 -0800
Newsgroups gmane.comp.lib.ivtools.patches
Message-ID <[email protected]>
--Apple-Mail-1--240439703
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="ivtools-051122-johnston-014"
Content-Disposition: attachment;
	filename=ivtools-051122-johnston-014

Patch:    ivtools-051122-johnston-014
For:      ivtools-1.2.3
Author:   [email protected]
Subject:  finalize support for sharing scanner/parser between multiple connections
Requires: 

This is an intermediate patch to ivtools-1.2.3.  To apply, cd to the
top-level directory of the ivtools source tree (the directory with src
and config subdirs), and apply like this:

	patch -p0 <ThisFile

Summary of Changes:

- finalize support for sharing scanner/parser between multiple
connections or users of a single process.  Not thread-safe, but the
next best thing for an event-driven program.

Index: ComTerp/comhandler.c
diff -c ComTerp/comhandler.c:1.3 ComTerp/comhandler.c:1.4
*** ComTerp/comhandler.c:1.3	Fri Oct 14 13:58:05 2005
--- src/ComTerp/comhandler.c	Tue Nov 22 10:25:25 2005
***************
*** 212,218 ****
      if (!ComterpHandler::logger_mode()) {
        comterp_->load_string(inbuf);
        if (fd>0) 
! 	cerr << "command via ACE -- " << inbuf << "\n";
        comterp_->_fd = fd;
        comterp_->_outfunc = (outfuncptr)&ComTerpServ::fd_fputs;
  
--- 212,218 ----
      if (!ComterpHandler::logger_mode()) {
        comterp_->load_string(inbuf);
        if (fd>0) 
! 	cerr << "(" << fd << "):  " << inbuf << "\n";
        comterp_->_fd = fd;
        comterp_->_outfunc = (outfuncptr)&ComTerpServ::fd_fputs;
  
Index: ComTerp/comterp.c
diff -c ComTerp/comterp.c:1.3 ComTerp/comterp.c:1.4
*** ComTerp/comterp.c:1.3	Fri Sep 16 11:06:35 2005
--- src/ComTerp/comterp.c	Tue Nov 22 10:25:25 2005
***************
*** 170,181 ****
  }
  
  boolean ComTerp::read_expr() {
!     unsigned int toklen, tokstart;
      int status = parser (_inptr, _infunc, _eoffunc, _errfunc, (FILE*)_outptr, _outfunc,
  			 _buffer, _bufsiz, &_bufptr, _token, _toksiz, &_linenum,
  			 &_pfbuf, &_pfsiz, &_pfnum);
  
      _pfoff = 0;
      return status==0 && _pfbuf[_pfnum-1].type != TOK_EOF && _buffer[0] != '\0';
  }
  
--- 170,182 ----
  }
  
  boolean ComTerp::read_expr() {
!     check_parser_client();
      int status = parser (_inptr, _infunc, _eoffunc, _errfunc, (FILE*)_outptr, _outfunc,
  			 _buffer, _bufsiz, &_bufptr, _token, _toksiz, &_linenum,
  			 &_pfbuf, &_pfsiz, &_pfnum);
  
      _pfoff = 0;
+     save_parser_client();    
      return status==0 && _pfbuf[_pfnum-1].type != TOK_EOF && _buffer[0] != '\0';
  }
  
***************
*** 887,893 ****
    _errbuf[0] = '\0';
    char errbuf_save[BUFSIZ];
    errbuf_save[0] = '\0';
-   
  
  #if __GNUC__<3
    filebuf fbuf;
--- 888,893 ----
Index: ComTerp/comterp.h
diff -c ComTerp/comterp.h:1.1 ComTerp/comterp.h:1.2
*** ComTerp/comterp.h:1.1	Tue Jun 15 08:53:38 2004
--- src/ComTerp/comterp.h	Tue Nov 22 10:25:25 2005
***************
*** 355,359 ****
--- 355,360 ----
    eoffuncptr _eoffunc;
    errfuncptr _errfunc;
    void* _inptr;
+ 
  };
  #endif /* !defined(_comterp_h) */
Index: ComTerp/parser.c
diff -c ComTerp/parser.c:1.1 ComTerp/parser.c:1.2
*** ComTerp/parser.c:1.1	Tue Jun 15 08:53:41 2004
--- src/ComTerp/parser.c	Tue Nov 22 10:25:25 2005
***************
*** 1,4 ****
--- 1,5 ----
  /*
+  * Copyright (c) 2005 Scott E. Johnston
   * Copyright (c) 1994, 1995, 1998 Vectaport Inc.
   *
   * Permission to use, copy, modify, distribute, and sell this software and
***************
*** 29,34 ****
--- 30,61 ----
  
  #define TITLE "Parser"
  
+ extern int _continuation_prompt;
+ extern int _continuation_prompt_disabled;
+ extern int _skip_shell_comments;
+ extern infuncptr _oneshot_infunc;
+ extern int _angle_brackets;
+ extern unsigned _token_state_save;
+ 
+ extern void* parser_client;             /* pointer to current client */
+ extern unsigned expecting;              /* Type of operator expected next */
+ 
+ extern paren_stack *ParenStack;         /* Stack to count args and keywords */
+ extern int TopOfParenStack;             /* Top of ParenStack */
+ extern int SizeOfParenStack;            /* Allocated size of ParenStack */
+ 
+ extern oper_stack *OperStack;          /* Operator stack */
+ extern int TopOfOperStack;             /* Top of OperStack */
+ extern int SizeOfOperStack;            /* Allocated size of OperStack */
+ 
+ extern unsigned NextBufptr;            /* Variables for look-ahead token */
+ extern char *NextToken;
+ extern unsigned NextToklen;    
+ extern unsigned NextToktype;
+ extern unsigned NextTokstart;
+ extern unsigned NextLinenum;
+ extern int NextOp_ids[OPTYPE_NUM];
+ 
  #if __GNUC__>=3
  static char newline;
  #endif
***************
*** 69,74 ****
--- 96,102 ----
      /* Create and load operator table */
      if(opr_tbl_default() != 0) 
  	KANRET("error in creating and loading default operator table");
+ 
  }
  
  
***************
*** 166,168 ****
--- 194,250 ----
    return !in.good();
  }
  
+ void Parser::check_parser_client() {
+   if (parser_client==NULL)
+     parser_client = (void*)this;
+   else if (parser_client != (void*)this) {
+     if (_linenum != 0) {
+       parser_client = (void*)this;
+       _continuation_prompt = __continuation_prompt;
+       _continuation_prompt_disabled = __continuation_prompt_disabled;
+       _skip_shell_comments = __skip_shell_comments;
+       _oneshot_infunc = __oneshot_infunc;
+       _angle_brackets  = __angle_brackets ;
+       _token_state_save = __token_state_save;
+       expecting = _expecting;
+       ParenStack = _ParenStack;
+       TopOfParenStack = _TopOfParenStack;
+       SizeOfParenStack = _SizeOfParenStack;
+       OperStack = _OperStack;
+       TopOfOperStack = _TopOfOperStack;
+       SizeOfOperStack = _SizeOfOperStack;
+       NextBufptr = _NextBufptr;
+       NextToken = _NextToken;
+       NextToklen = _NextToklen;    
+       NextToktype = _NextToktype;
+       NextTokstart = _NextTokstart;
+       NextLinenum = _NextLinenum;
+       for (int i=0; i<OPTYPE_NUM; i++)
+ 	NextOp_ids[i] = _NextOp_ids[i];
+     }
+   }
+ }
+ 
+ void Parser::save_parser_client() {
+   __continuation_prompt = _continuation_prompt;
+   __continuation_prompt_disabled = _continuation_prompt_disabled;
+   __skip_shell_comments = _skip_shell_comments;
+   __oneshot_infunc = _oneshot_infunc;
+   __angle_brackets  = _angle_brackets ;
+   __token_state_save = _token_state_save;
+   _expecting = expecting;
+   _ParenStack = ParenStack;
+   _TopOfParenStack = TopOfParenStack;
+   _SizeOfParenStack = SizeOfParenStack;
+   _OperStack = OperStack;
+   _TopOfOperStack = TopOfOperStack;
+   _SizeOfOperStack = SizeOfOperStack;
+   _NextBufptr = NextBufptr;
+   _NextToken = NextToken;
+   _NextToklen = NextToklen;    
+   _NextToktype = NextToktype;
+   _NextTokstart = NextTokstart;
+   _NextLinenum = NextLinenum;
+   for (int i=0; i<OPTYPE_NUM; i++)
+     _NextOp_ids[i] = NextOp_ids[i];
+ }
Index: ComTerp/parser.h
diff -c ComTerp/parser.h:1.1 ComTerp/parser.h:1.2
*** ComTerp/parser.h:1.1	Tue Jun 15 08:53:41 2004
--- src/ComTerp/parser.h	Tue Nov 22 10:25:25 2005
***************
*** 50,55 ****
--- 50,61 ----
      boolean skip_matched_parens();
      // support for '()', '{}', and '[]'.
  
+     void check_parser_client();
+     /* set this object as client of underlying C-based scanner/parser */
+ 
+     void save_parser_client();
+     /* save current parser info for this client */
+ 
  protected:
      void init();
  
***************
*** 64,69 ****
--- 70,102 ----
      postfix_token* _pfbuf;
      unsigned int _pfsiz;
      unsigned int _pfnum;
+ 
+     /* copies of scanner/parser internals */
+     int __continuation_prompt;
+     int __continuation_prompt_disabled;
+     int __skip_shell_comments;
+     infuncptr __oneshot_infunc;
+     int __angle_brackets;
+     unsigned __token_state_save;
+ 
+     unsigned _expecting;             /* Type of operator expected next */
+ 
+     paren_stack* _ParenStack;               /* Stack to count args and keywords */
+     int _TopOfParenStack;            /* Top of ParenStack */
+     int _SizeOfParenStack;           /* Allocated size of ParenStack */
+ 
+     oper_stack* _OperStack;                /* Operator stack */
+     int _TopOfOperStack;             /* Top of OperStack */
+     int _SizeOfOperStack;            /* Allocated size of OperStack */
+ 
+     unsigned _NextBufptr;            /* Variables for look-ahead token */
+     char* _NextToken;
+     unsigned _NextToklen;    
+     unsigned _NextToktype;
+     unsigned _NextTokstart;
+     unsigned _NextLinenum;
+     int _NextOp_ids[OPTYPE_NUM];
+ 
  };
  
  #endif /* !defined(_parser_h) */
Index: ComUtil/_lexscan.c
diff -c ComUtil/_lexscan.c:1.2 ComUtil/_lexscan.c:1.3
*** ComUtil/_lexscan.c:1.2	Fri Oct 14 13:58:03 2005
--- src/ComUtil/_lexscan.c	Tue Nov 22 10:25:21 2005
***************
*** 32,40 ****
  History:        Written by Scott E. Johnston, March 1989
  */
  
- extern int _continuation_prompt;
- extern int _continuation_prompt_disabled;
- 
  #include <stdio.h>
  #include <string.h>
  #include <ctype.h>
--- 32,37 ----
***************
*** 43,48 ****
--- 40,50 ----
  
  #include "comutil.ci"
  
+ extern int _continuation_prompt;
+ extern int _continuation_prompt_disabled;
+ unsigned _token_state_save = TOK_WHITESPACE;
+ 				/* variable to save token state between calls */
+ 
  /* MACROS */
  
  /* States for parsing floating point numbers */
***************
*** 152,159 ****
  BOOLEAN long_num = FALSE;       /* Indicates long integer to be used */
  unsigned token_state = TOK_WHITESPACE;
  				/* Internal token state variable */
- static unsigned token_state_save = TOK_WHITESPACE;
- 				/* variable to save token state between calls */
  unsigned begcmt_len =           /* Number of characters in comment beginning */
     (begcmt != NULL ? strlen(begcmt) : 0 );
  unsigned endcmt_len =           /* Number of characters in comment ending */
--- 154,159 ----
***************
*** 177,188 ****
  /* Initialize */
     *toktype = TOK_NONE;
     *tokstart = 0;
!    if (token_state_save != TOK_STRING)
       *toklen = 0;
     else
       *toklen = strlen(token);
!    token_state = token_state_save;
!    token_state_save = TOK_WHITESPACE;
  
  /* Initialize if linenumber is 0 */
     if( *linenum == 0 ) {
--- 177,188 ----
  /* Initialize */
     *toktype = TOK_NONE;
     *tokstart = 0;
!    if (_token_state_save != TOK_STRING)
       *toklen = 0;
     else
       *toklen = strlen(token);
!    token_state = _token_state_save;
!    _token_state_save = TOK_WHITESPACE;
  
  /* Initialize if linenumber is 0 */
     if( *linenum == 0 ) {
***************
*** 309,315 ****
           CURR_CHAR = buffer[0];
  	 if (CURR_CHAR == '\0') {
  	   if (token_state == TOK_COMMENT || token_state == TOK_STRING) {
! 	     token_state_save = token_state;
  	     token[*toklen] = '\0';
    	    }
  	    *linenum--;
--- 309,315 ----
           CURR_CHAR = buffer[0];
  	 if (CURR_CHAR == '\0') {
  	   if (token_state == TOK_COMMENT || token_state == TOK_STRING) {
! 	     _token_state_save = token_state;
  	     token[*toklen] = '\0';
    	    }
  	    *linenum--;
Index: ComUtil/_parser.c
diff -c ComUtil/_parser.c:1.3 ComUtil/_parser.c:1.4
*** ComUtil/_parser.c:1.3	Fri Oct 14 13:58:03 2005
--- src/ComUtil/_parser.c	Tue Nov 22 10:25:22 2005
***************
*** 56,81 ****
  #define LEFTPAREN 1
  #define KEYWORD   2
  
- /* Structure of stack to count command arguments and keywords */
- typedef struct _paren_stack paren_stack;
- struct _paren_stack 
- {
-   unsigned nids;
-   unsigned narg;
-   unsigned nkey;
-   unsigned paren_type;
-   int comm_id;
- };
- 
- /* Structure of stack to store operators and keywords */
- typedef struct _oper_stack oper_stack;
- struct _oper_stack 
- {
-   int id;
-   int oper_type;
- };
- 
  /* Static Variables */
  static unsigned expecting;              /* Type of operator expected next */
  
  static paren_stack *ParenStack = NULL;  /* Stack to count args and keywords */
--- 56,67 ----
  #define LEFTPAREN 1
  #define KEYWORD   2
  
  /* Static Variables */
+ #define EXTERN_VISIBILITY
+ #ifdef EXTERN_VISIBILITY
+ #define static /**/
+ void* parser_client = NULL;                    /* pointer to current client */
+ #endif
  static unsigned expecting;              /* Type of operator expected next */
  
  static paren_stack *ParenStack = NULL;  /* Stack to count args and keywords */
***************
*** 93,98 ****
--- 79,87 ----
  static unsigned NextTokstart;
  static unsigned NextLinenum;
  static int NextOp_ids[OPTYPE_NUM];
+ #ifdef EXTERN_VISIBILITY
+ #undef static
+ #endif
  
  /* Parenthesis stack macros */
  #define INITIAL_PAREN_STACK_SIZE 32
Index: ComUtil/comterp.h
diff -c ComUtil/comterp.h:1.3 ComUtil/comterp.h:1.4
*** ComUtil/comterp.h:1.3	Fri Oct 14 13:58:03 2005
--- src/ComUtil/comterp.h	Tue Nov 22 10:25:22 2005
***************
*** 140,145 ****
--- 140,164 ----
     int nids;		/* Number of ids in compound command name */
  } postfix_token;
  
+ /* Structure of stack to count command arguments and keywords */
+ typedef struct _paren_stack paren_stack;
+ struct _paren_stack 
+ {
+   unsigned nids;
+   unsigned narg;
+   unsigned nkey;
+   unsigned paren_type;
+   int comm_id;
+ };
+ 
+ /* Structure of stack to store operators and keywords */
+ typedef struct _oper_stack oper_stack;
+ struct _oper_stack 
+ {
+   int id;
+   int oper_type;
+ };
+ 
  /* Package function prototypes */
  #if !defined(OSK)
  #include <ComUtil/comterp.arg>
*** /dev/null	 Tue Nov 22 10:25:53 PST 2005
--- patches/ivtools-051122-johnston-014
*************** patches/ivtools-051122-johnston-014
*** 0 ****
--- 1 ----
+ ivtools-051122-johnston-014

--Apple-Mail-1--240439703--


-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click