[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.4

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.4     ! ohara       2: /* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.3 2000/11/28 04:02:56 ohara Exp $ */
1.1       ohara       3:
                      4: #include <stdio.h>
                      5: #include <stdlib.h>
                      6: #include <unistd.h>
                      7: #include <string.h>
1.3       ohara       8: #include <signal.h>
1.1       ohara       9: #include <ox_toolkit.h>
                     10: #include "sm.h"
                     11:
                     12: /* ultra loose data base. */
                     13: static db db_localfunc[] = {
                     14:     {lf_oxc_open, "oxc_open"},
                     15:     {NULL, NULL}
                     16: };
                     17:
                     18: static db db_sm[] = {
                     19:     {sm_executeFunction, SM_executeFunction},
                     20:     {sm_mathcap,         SM_mathcap},
1.4     ! ohara      21:     {sm_set_mathcap,     SM_setMathCap},
1.1       ohara      22:     {sm_popCMO,          SM_popCMO},
                     23:     {sm_pops,            SM_pops},
                     24:     {NULL, NULL}
                     25: };
                     26:
                     27: __inline__
                     28: static int (*db_search(void *key, db *dbs, int (*cmp)(void *, void *)))()
                     29: {
                     30:     while (dbs->key != NULL) {
                     31:         if (cmp(key, dbs->key) == 0) {
                     32:             return dbs->func_ptr;
                     33:         }
                     34:         dbs++;
                     35:     }
                     36:     return NULL;
                     37: }
                     38:
                     39: static int (*lookup_localfunction(char *name))()
                     40: {
                     41:     return db_search(name, db_localfunc, strcmp);
                     42: }
                     43:
                     44: /*
                     45: Normally local functions push a return value to the stack.
                     46: but, if error occurs, then these return non-positive numbers and
                     47: the sm_executeFunction push an error object.
                     48: */
                     49: void sm_executeFunction(OXFILE *oxfp)
                     50: {
                     51:     int (*func)(OXFILE *);
1.4     ! ohara      52:     int retcode = 0;
1.1       ohara      53:     cmo *ob = pop();
                     54:     if (ob->tag == CMO_STRING) {
                     55:         func = lookup_localfunction(((cmo_string *)ob)->s);
                     56:         if (func != NULL) {
                     57:             if ((retcode = func(oxfp)) > 0) {
1.4     ! ohara      58:                 return;
        !            59:             }
1.1       ohara      60:         }
                     61:     }
1.4     ! ohara      62:     push_error(retcode, ob);
1.1       ohara      63: }
                     64:
                     65: /* getargs() set number of popped objects to argc. */
                     66: static int getargs(cmo ***args)
                     67: {
                     68:     cmo **argv;
                     69:     int i;
                     70:     int argc = -1;
1.4     ! ohara      71:     cmo_int32 *m = (cmo_int32 *)pop();
1.1       ohara      72:
                     73:     if (m->tag != CMO_INT32 || (argc = m->i) < 0) {
                     74:         fprintf(stderr, "oxc: invalid arguments\n");
                     75:     }else {
                     76:         argv = (cmo **)malloc(sizeof(cmo *)*argc);
                     77:         for(i=0; i<argc; i++) {
                     78:             argv[i] = pop();
                     79:         }
                     80:         *args = argv;
                     81:     }
1.4     ! ohara      82:     return argc;
1.1       ohara      83: }
                     84:
                     85: static int pids[1024] = {0};
                     86: static int pid_ptr = 0;
                     87:
1.2       ohara      88: int pid_lookup(int pid)
                     89: {
1.4     ! ohara      90:     int i;
        !            91:     for(i=0; i<pid_ptr; i++) {
        !            92:         if (pids[i] == pid) {
        !            93:             return i;
        !            94:         }
        !            95:     }
        !            96:     return -1;
1.2       ohara      97: }
                     98:
                     99: int pid_registed(int pid)
                    100: {
1.4     ! ohara     101:     return pid_lookup(pid)+1;
1.2       ohara     102: }
                    103:
                    104: int pid_regist(int pid)
1.1       ohara     105: {
1.4     ! ohara     106:     if (pid_ptr < 1024) {
        !           107:         pids[pid_ptr++] = pid;
        !           108:         return pid;
        !           109:     }
        !           110:     return 0;
1.1       ohara     111: }
                    112:
1.2       ohara     113: void pid_delete(int pid)
                    114: {
1.4     ! ohara     115:     int i = pid_lookup(pid);
        !           116:     if (i >= 0 && i != --pid_ptr) {
        !           117:         pids[i] = pids[pid_ptr];
        !           118:     }
1.2       ohara     119: }
                    120:
1.1       ohara     121: int lf_oxc_open()
                    122: {
                    123:     cmo **argv;
                    124:     char *cmd;
                    125:     int port;
                    126:     int pid;
                    127:
                    128:     if (getargs(&argv) != 2 || argv[0]->tag != CMO_STRING
1.4     ! ohara     129:         || argv[1]->tag != CMO_INT32) {
        !           130:         fprintf(stderr, "oxc: invalid arguments\n");
1.1       ohara     131:         return -1;
                    132:     }
                    133:
1.4     ! ohara     134:     cmd  = ((cmo_string *)argv[0])->s;
        !           135:     port = ((cmo_int32 *)argv[1])->i;
        !           136:     pid = lf_oxc_open_main(cmd, port);
        !           137:     if (pid > 0) {
        !           138:         push(new_cmo_int32(pid));
        !           139:         pid_regist(pid);
        !           140:     }
        !           141:     return pid;
1.1       ohara     142: }
                    143:
                    144: void sm_mathcap(OXFILE *oxfp)
                    145: {
1.4     ! ohara     146:     push((cmo *)oxf_cmo_mathcap(oxfp));
        !           147: }
        !           148:
        !           149: void sm_set_mathcap(OXFILE *oxfp)
        !           150: {
        !           151:     cmo_mathcap *m = (cmo_mathcap *)pop();
        !           152:     if (m->tag == CMO_MATHCAP) {
        !           153:         oxf_mathcap_update(oxfp, m);
        !           154:     }else {
        !           155:         push_error(-1, m);
        !           156:         /* an error object must be pushed */
        !           157:     }
1.1       ohara     158: }
1.2       ohara     159:
                    160: void sm_control_kill(OXFILE *oxfp)
                    161: {
1.4     ! ohara     162:     cmo_int32 *m = (cmo_int32 *)pop();
        !           163:     int pid = m->i;
        !           164:     if (m->tag != CMO_INT32 || !pid_registed(pid)) {
        !           165:         push_error(-1, m);
        !           166:     }else {
        !           167:         kill(pid, SIGKILL);
        !           168:         pid_delete(pid);
        !           169:     }
1.2       ohara     170: }
                    171:
                    172: void sm_control_reset(OXFILE *oxfp)
                    173: {
1.4     ! ohara     174:     cmo_int32 *m = (cmo_int32 *)pop();
        !           175:     int pid = m->i;
        !           176:     if (m->tag != CMO_INT32 || !pid_registed(pid)) {
        !           177:         push_error(-1, m);
        !           178:     }else {
        !           179:         kill(pid, SIGUSR1);
        !           180:     }
1.2       ohara     181: }
                    182:
1.4     ! ohara     183: void sm_control_spawn_server(OXFILE *oxfp);
        !           184: void sm_control_terminate_server(OXFILE *oxfp);
1.1       ohara     185:
                    186: static int intcmp(int key1, int key2)
                    187: {
                    188:     return key1 != key2;
                    189: }
                    190:
                    191:
                    192: int (*sm_search_f(int code))()
                    193: {
                    194:     return db_search(code, db_sm, intcmp);
                    195: }

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