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

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

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

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