Annotation of OpenXM/src/oxc/sm.c, Revision 1.7
1.1 ohara 1: /* -*- mode: C -*- */
1.7 ! ohara 2: /* $OpenXM: OpenXM/src/oxc/sm.c,v 1.6 2003/05/07 04:00:30 ohara Exp $ */
1.1 ohara 3:
4: #include <stdio.h>
5: #include <stdlib.h>
1.7 ! ohara 6: #include <string.h>
1.1 ohara 7: #include <unistd.h>
8: #include <sys/types.h>
9: #include <sys/time.h>
10: #include <ox_toolkit.h>
11: #include "sm.h"
12:
1.4 ohara 13: #define receive_sm_command(x) receive_int32((x))
14:
1.2 ohara 15: /* WARNING: you must NOT use stack[stack_ptr]. */
1.1 ohara 16:
17: static cmo **stack = NULL;
18: static int stack_ptr = 0;
1.3 ohara 19: static int stack_size = 0;
1.4 ohara 20: OXFILE *stack_oxfp = NULL;
1.7 ! ohara 21: int oxf_error(OXFILE *oxfp);
1.4 ohara 22:
1.1 ohara 23: #define DIFFERENCE_OF_STACK 1024
24:
1.4 ohara 25: static void stack_extend()
1.1 ohara 26: {
1.3 ohara 27: int newsize = stack_size + DIFFERENCE_OF_STACK;
1.1 ohara 28: cmo **newstack = (cmo **)malloc(sizeof(cmo *)*newsize);
29: if (stack != NULL) {
1.3 ohara 30: memcpy(newstack, stack, sizeof(cmo *)*stack_size);
1.1 ohara 31: free(stack);
32: }
1.3 ohara 33: stack_size = newsize;
1.1 ohara 34: stack = newstack;
35: }
36:
37: void push(cmo *ob)
38: {
1.3 ohara 39: if (stack_ptr >= stack_size) {
40: stack_extend();
1.1 ohara 41: }
42: stack[stack_ptr] = ob;
43: stack_ptr++;
44: }
45:
46: cmo *pop()
47: {
48: if (stack_ptr > 0) {
49: return stack[--stack_ptr];
50: }
51: return new_cmo_null();
52: }
53:
54: void pops(int n)
55: {
56: stack_ptr -= n;
57: if (stack_ptr < 0) {
58: stack_ptr = 0;
59: }
60: }
61:
62: void push_error(int errcode, cmo* pushback)
63: {
1.5 ohara 64: return push((cmo *)make_error_object(errcode, pushback));
1.1 ohara 65: }
66:
67: /*
68: If error occurs, then
69: an sm_* function, called by sm_run, pushes an error obect.
70: */
1.7 ! ohara 71: int sm_popCMO()
1.1 ohara 72: {
73: cmo* m = pop();
1.4 ohara 74: send_ox_cmo(stack_oxfp, m);
1.7 ! ohara 75: return 0;
1.1 ohara 76: }
77:
1.7 ! ohara 78: int sm_pops()
1.1 ohara 79: {
80: cmo* m = pop();
81: if (m->tag == CMO_INT32) {
82: pops(((cmo_int32 *)m)->i);
83: }else {
1.5 ohara 84: push_error(-1, m); /* m is invalid. */
85: }
1.7 ! ohara 86: return 0;
1.1 ohara 87: }
88:
1.4 ohara 89: void sm_run(int code)
1.1 ohara 90: {
91: int (*func)(OXFILE *) = sm_search_f(code);
92: if (func != NULL) {
1.4 ohara 93: func(stack_oxfp);
1.1 ohara 94: }else {
1.6 ohara 95: ox_printf("oxc: unknown SM code(%d).\n", code);
1.1 ohara 96: }
97: }
98:
1.4 ohara 99: int sm_receive_ox()
1.1 ohara 100: {
101: int tag;
102: int code;
103:
1.4 ohara 104: tag = receive_ox_tag(stack_oxfp);
105: if (oxf_error(stack_oxfp)) {
1.1 ohara 106: return 0;
107: }
108: switch(tag) {
109: case OX_DATA:
1.4 ohara 110: push(receive_cmo(stack_oxfp));
1.1 ohara 111: break;
112: case OX_COMMAND:
1.4 ohara 113: code = receive_sm_command(stack_oxfp);
1.6 ohara 114: ox_printf("oxc: code = %d.\n", code);
1.4 ohara 115: sm_run(code);
1.1 ohara 116: break;
117: default:
1.6 ohara 118: ox_printf("illeagal message? ox_tag = (%d)\n", tag);
1.1 ohara 119: return 0;
120: break;
121: }
122: return 1;
123: }
124:
125: int oxf_error(OXFILE *oxfp)
126: {
127: int e = oxfp->error;
128: if (e != 0) {
129: oxfp->error = 0;
130: }
131: return e;
132: }
133:
134: int sm(OXFILE *oxfp)
135: {
1.5 ohara 136: stack_oxfp = oxfp;
1.3 ohara 137: stack_extend();
1.4 ohara 138: while (sm_receive_ox()) {
1.1 ohara 139: }
1.6 ohara 140: ox_printf("oxc: socket(%d) is closed.\n", stack_oxfp->fd);
1.7 ! ohara 141: return 0;
1.1 ohara 142: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>