[BACK]Return to sm.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / oxc

Diff for /OpenXM/src/oxc/sm.c between version 1.1 and 1.7

version 1.1, 2000/10/13 06:05:12 version 1.7, 2016/04/01 18:12:39
Line 1 
Line 1 
 /* -*- mode: C -*- */  /* -*- mode: C -*- */
 /* $OpenXM$ */  /* $OpenXM: OpenXM/src/oxc/sm.c,v 1.6 2003/05/07 04:00:30 ohara Exp $ */
   
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
   #include <string.h>
 #include <unistd.h>  #include <unistd.h>
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/time.h>  #include <sys/time.h>
 #include <ox_toolkit.h>  #include <ox_toolkit.h>
 #include "sm.h"  #include "sm.h"
   
 /* WARNING: you must be use stack[stack_ptr]. */  #define  receive_sm_command(x)   receive_int32((x))
   
   /* WARNING: you must NOT use stack[stack_ptr]. */
   
 static cmo **stack = NULL;  static cmo **stack = NULL;
 static int stack_ptr = 0;  static int stack_ptr = 0;
 static int sizeof_stack = 0;  static int stack_size = 0;
   OXFILE *stack_oxfp = NULL;
   int oxf_error(OXFILE *oxfp);
   
 #define DIFFERENCE_OF_STACK  1024  #define DIFFERENCE_OF_STACK  1024
   
 void extend_stack()  static void stack_extend()
 {  {
     int newsize = sizeof_stack + DIFFERENCE_OF_STACK;      int newsize = stack_size + DIFFERENCE_OF_STACK;
     cmo **newstack = (cmo **)malloc(sizeof(cmo *)*newsize);      cmo **newstack = (cmo **)malloc(sizeof(cmo *)*newsize);
     if (stack != NULL) {      if (stack != NULL) {
         memcpy(newstack, stack, sizeof(cmo *)*sizeof_stack);          memcpy(newstack, stack, sizeof(cmo *)*stack_size);
         free(stack);          free(stack);
     }      }
     sizeof_stack = newsize;      stack_size = newsize;
     stack = newstack;      stack = newstack;
 }  }
   
 void push(cmo *ob)  void push(cmo *ob)
 {  {
     if (stack_ptr >= sizeof_stack) {      if (stack_ptr >= stack_size) {
         extend_stack();          stack_extend();
     }      }
     stack[stack_ptr] = ob;      stack[stack_ptr] = ob;
     stack_ptr++;      stack_ptr++;
Line 55  void pops(int n)
Line 61  void pops(int n)
   
 void push_error(int errcode, cmo* pushback)  void push_error(int errcode, cmo* pushback)
 {  {
         return push((cmo *)make_error_object(errcode, pushback));      return push((cmo *)make_error_object(errcode, pushback));
 }  }
   
 /*  /*
 If error occurs, then  If error occurs, then
 an sm_* function, called by sm_run, pushes an error obect.  an sm_* function, called by sm_run, pushes an error obect.
 */  */
 void sm_popCMO(OXFILE* oxfp)  int sm_popCMO()
 {  {
     cmo* m = pop();      cmo* m = pop();
     send_ox_cmo(oxfp, m);      send_ox_cmo(stack_oxfp, m);
       return 0;
 }  }
   
 void sm_pops(OXFILE* oxfp)  int sm_pops()
 {  {
     cmo* m = pop();      cmo* m = pop();
     if (m->tag == CMO_INT32) {      if (m->tag == CMO_INT32) {
         pops(((cmo_int32 *)m)->i);          pops(((cmo_int32 *)m)->i);
     }else {      }else {
                 push_error(-1, m); /* m is invalid. */          push_error(-1, m); /* m is invalid. */
         }      }
       return 0;
 }  }
   
 int receive_sm_command(OXFILE* oxfp)  void sm_run(int code)
 {  {
     return receive_int32(oxfp);  
 }  
   
 void sm_run(OXFILE* oxfp, int code)  
 {  
     int (*func)(OXFILE *) = sm_search_f(code);      int (*func)(OXFILE *) = sm_search_f(code);
     if (func != NULL) {      if (func != NULL) {
         func(oxfp);          func(stack_oxfp);
     }else {      }else {
         fprintf(stderr, "oxc: unknown SM code(%d).\n", code);          ox_printf("oxc: unknown SM code(%d).\n", code);
     }      }
 }  }
   
 int receive_ox(OXFILE *oxfp)  int sm_receive_ox()
 {  {
     int tag;      int tag;
     int code;      int code;
   
     tag = receive_ox_tag(oxfp);      tag = receive_ox_tag(stack_oxfp);
     if (oxf_error(oxfp)) {      if (oxf_error(stack_oxfp)) {
         return 0;          return 0;
     }      }
     switch(tag) {      switch(tag) {
     case OX_DATA:      case OX_DATA:
         push(receive_cmo(oxfp));          push(receive_cmo(stack_oxfp));
         break;          break;
     case OX_COMMAND:      case OX_COMMAND:
         code = receive_sm_command(oxfp);          code = receive_sm_command(stack_oxfp);
         fprintf(stderr, "oxc: oxfp(%d), code = %d.\n", oxfp->fd, code);          ox_printf("oxc: code = %d.\n", code);
         sm_run(oxfp, code);          sm_run(code);
         break;          break;
     default:      default:
         fprintf(stderr, "illeagal message? ox_tag = (%d)\n", tag);          ox_printf("illeagal message? ox_tag = (%d)\n", tag);
         return 0;          return 0;
         break;          break;
     }      }
Line 130  int oxf_error(OXFILE *oxfp)
Line 133  int oxf_error(OXFILE *oxfp)
   
 int sm(OXFILE *oxfp)  int sm(OXFILE *oxfp)
 {  {
     extend_stack();      stack_oxfp = oxfp;
     while (receive_ox(oxfp)) {      stack_extend();
       while (sm_receive_ox()) {
     }      }
     fprintf(stderr, "oxc: socket(%d) is closed.\n", oxfp->fd);      ox_printf("oxc: socket(%d) is closed.\n", stack_oxfp->fd);
       return 0;
 }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.7

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>