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

Annotation of OpenXM/src/oxc/sm.c, Revision 1.6

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

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