version 1.2, 2000/10/13 08:05:49 |
version 1.6, 2003/05/07 04:00:30 |
|
|
/* -*- mode: C -*- */ |
/* -*- mode: C -*- */ |
/* $OpenXM: OpenXM/src/oxc/sm.c,v 1.1 2000/10/13 06:05:12 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/oxc/sm.c,v 1.5 2000/12/03 14:32:40 ohara Exp $ */ |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
|
#include <ox_toolkit.h> |
#include <ox_toolkit.h> |
#include "sm.h" |
#include "sm.h" |
|
|
|
#define receive_sm_command(x) receive_int32((x)) |
|
|
/* WARNING: you must NOT use stack[stack_ptr]. */ |
/* 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; |
|
|
#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++; |
|
|
|
|
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) |
void sm_popCMO() |
{ |
{ |
cmo* m = pop(); |
cmo* m = pop(); |
send_ox_cmo(oxfp, m); |
send_ox_cmo(stack_oxfp, m); |
} |
} |
|
|
void sm_pops(OXFILE* oxfp) |
void 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. */ |
} |
} |
} |
} |
|
|
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 129 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); |
} |
} |