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

Annotation of OpenXM/src/oxc/sm_ext.c, Revision 1.1

1.1     ! ohara       1: /* -*- mode: C; coding: euc-japan -*- */
        !             2: /* $OpenXM$ */
        !             3:
        !             4: #include <stdio.h>
        !             5: #include <stdlib.h>
        !             6: #include <unistd.h>
        !             7: #include <string.h>
        !             8: #include <ox_toolkit.h>
        !             9: #include "sm.h"
        !            10:
        !            11: /* ultra loose data base. */
        !            12: static db db_localfunc[] = {
        !            13:     {lf_oxc_open, "oxc_open"},
        !            14:     {NULL, NULL}
        !            15: };
        !            16:
        !            17: static db db_sm[] = {
        !            18:     {sm_executeFunction, SM_executeFunction},
        !            19:     {sm_mathcap,         SM_mathcap},
        !            20:     {sm_popCMO,          SM_popCMO},
        !            21:     {sm_pops,            SM_pops},
        !            22:     {NULL, NULL}
        !            23: };
        !            24:
        !            25: __inline__
        !            26: static int (*db_search(void *key, db *dbs, int (*cmp)(void *, void *)))()
        !            27: {
        !            28:     while (dbs->key != NULL) {
        !            29:         if (cmp(key, dbs->key) == 0) {
        !            30:             return dbs->func_ptr;
        !            31:         }
        !            32:         dbs++;
        !            33:     }
        !            34:     return NULL;
        !            35: }
        !            36:
        !            37: static int (*lookup_localfunction(char *name))()
        !            38: {
        !            39:     return db_search(name, db_localfunc, strcmp);
        !            40: }
        !            41:
        !            42: /*
        !            43: Normally local functions push a return value to the stack.
        !            44: but, if error occurs, then these return non-positive numbers and
        !            45: the sm_executeFunction push an error object.
        !            46: */
        !            47: void sm_executeFunction(OXFILE *oxfp)
        !            48: {
        !            49:     int (*func)(OXFILE *);
        !            50:        int retcode = 0;
        !            51:     cmo *ob = pop();
        !            52:     if (ob->tag == CMO_STRING) {
        !            53:         func = lookup_localfunction(((cmo_string *)ob)->s);
        !            54:         if (func != NULL) {
        !            55:             if ((retcode = func(oxfp)) > 0) {
        !            56:                                return;
        !            57:                        }
        !            58:         }
        !            59:     }
        !            60:        push_error(retcode, ob);
        !            61: }
        !            62:
        !            63: /* getargs() set number of popped objects to argc. */
        !            64: static int getargs(cmo ***args)
        !            65: {
        !            66:     cmo **argv;
        !            67:     int i;
        !            68:     int argc = -1;
        !            69:     cmo_int32 *m = pop();
        !            70:
        !            71:     if (m->tag != CMO_INT32 || (argc = m->i) < 0) {
        !            72:         fprintf(stderr, "oxc: invalid arguments\n");
        !            73:     }else {
        !            74:         argv = (cmo **)malloc(sizeof(cmo *)*argc);
        !            75:         for(i=0; i<argc; i++) {
        !            76:             argv[i] = pop();
        !            77:         }
        !            78:         *args = argv;
        !            79:     }
        !            80:        return argc;
        !            81: }
        !            82:
        !            83: static int pids[1024] = {0};
        !            84: static int pid_ptr = 0;
        !            85:
        !            86: int regist_pid(int pid)
        !            87: {
        !            88:        if (pid_ptr < 1024) {
        !            89:                pids[pid_ptr++] = pid;
        !            90:                return pid;
        !            91:        }
        !            92:        return 0;
        !            93: }
        !            94:
        !            95: int lf_oxc_open()
        !            96: {
        !            97:     cmo **argv;
        !            98:     char *cmd;
        !            99:     int port;
        !           100:     int pid;
        !           101:
        !           102:     if (getargs(&argv) != 2 || argv[0]->tag != CMO_STRING
        !           103:                || argv[1]->tag != CMO_INT32) {
        !           104:                fprintf(stderr, "oxc: invalid arguments\n");
        !           105:         return -1;
        !           106:     }
        !           107:
        !           108:        cmd  = ((cmo_string *)argv[0])->s;
        !           109:        port = ((cmo_int32 *)argv[1])->i;
        !           110:        pid = lf_oxc_open_main(cmd, port);
        !           111:        if (pid > 0) {
        !           112:                push(new_cmo_int32(pid));
        !           113:                regist_pid(pid);
        !           114:        }
        !           115:        return pid;
        !           116: }
        !           117:
        !           118: void sm_mathcap(OXFILE *oxfp)
        !           119: {
        !           120:        cmo_mathcap *m = pop();
        !           121:        if (m->tag == CMO_MATHCAP) {
        !           122:                mathcap_update(m);
        !           123:        }else {
        !           124:                push_error(-1, m);
        !           125:                /* an error object must be pushed */
        !           126:        }
        !           127: }
        !           128:
        !           129: static int intcmp(int key1, int key2)
        !           130: {
        !           131:     return key1 != key2;
        !           132: }
        !           133:
        !           134:
        !           135: int (*sm_search_f(int code))()
        !           136: {
        !           137:     return db_search(code, db_sm, intcmp);
        !           138: }

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