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

Annotation of OpenXM/src/ox_toolkit/mathcap.c, Revision 1.11

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.11    ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/mathcap.c,v 1.10 2003/09/15 09:31:41 ohara Exp $ */
1.1       ohara       3:
                      4: /* This module includes functions for handling mathcap databases. */
                      5:
                      6: #include <stdlib.h>
1.5       ohara       7: #include <string.h>
1.1       ohara       8: #include "ox_toolkit.h"
                      9:
1.5       ohara      10: #define MATHCAP_FLAG_DENY   0
                     11: #define MATHCAP_FLAG_ALLOW  1
1.3       ohara      12:
1.5       ohara      13: static void table_init(table *m, int key);
                     14: static table *new_table(int *src);
                     15: static table *table_lookup(table *tbl, int tag);
                     16: static void table_ctl(table *tbl, int tag, int flag);
                     17: static void table_ctl_all(table *tbl, int flag);
                     18: static cmo_list *table_get_all(table *tbl);
                     19: static void table_allow(table *tbl, int tag);
                     20: static void table_deny(table *tbl, int tag);
                     21: static void table_update(table *cmotbl, cmo_list* types);
                     22: static int table_allowQ_tag(table *tbl, int tag);
                     23: static int table_allowQ_cmo_list(table *cmotbl, cmo_list *ob);
                     24: static int table_allowQ_cmo_monomial32(table *cmotbl, cmo_monomial32 *ob);
                     25: static int table_allowQ_cmo_mathcap(table *cmotbl, cmo_mathcap *ob);
                     26: static int table_allowQ_cmo(table *cmotbl, cmo *ob);
                     27: static cmo_list *sysinfo_get();
1.3       ohara      28: static char *new_string(char *s);
1.5       ohara      29: static int *new_int_array(int *array);
                     30: static cmo_list *get_messagetypes(cmo_list *ob, int type);
1.3       ohara      31: static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc);
1.2       ohara      32:
1.5       ohara      33: static int cmotbl_a[] = {
1.3       ohara      34:     CMO_NULL,
                     35:     CMO_INT32,
                     36:     CMO_DATUM,
                     37:     CMO_STRING,
                     38:     CMO_MATHCAP,
                     39:     CMO_LIST,
                     40:     CMO_MONOMIAL32,
                     41:     CMO_ZZ,
1.11    ! ohara      42:     CMO_QQ,
        !            43:     CMO_IEEE_DOUBLE_FLOAT,
1.3       ohara      44:     CMO_ZERO,
                     45:     CMO_DMS_GENERIC,
                     46:     CMO_RING_BY_NAME,
                     47:     CMO_INDETERMINATE,
                     48:     CMO_DISTRIBUTED_POLYNOMIAL,
                     49:     CMO_ERROR2,
                     50:     0,
1.1       ohara      51: };
                     52:
1.5       ohara      53: static int smtbl_a[] = {
1.3       ohara      54:     SM_popCMO,
                     55:     SM_popString,
                     56:     SM_mathcap,
                     57:     SM_pops,
                     58:     SM_executeStringByLocalParser,
                     59:     SM_executeFunction,
                     60:     SM_setMathCap,
                     61:     SM_shutdown,
                     62:     SM_control_kill,
                     63:     SM_control_reset_connection,
1.5       ohara      64:     SM_control_spawn_server,
                     65:     SM_control_terminate_server,
1.3       ohara      66:     0,
1.1       ohara      67: };
                     68:
1.5       ohara      69: static struct {
                     70:     int  version;
                     71:     char *version_string;
                     72:     char *sysname;
                     73:     char *hosttype;
                     74:     int  *cmo_tags;
                     75:     int  *sm_cmds;
                     76: } sysinfo = {0, "NO VERSION", "NONAME", "UNKNOWN", cmotbl_a, smtbl_a};
                     77:
1.3       ohara      78: __inline__
1.5       ohara      79: static void table_init(table *m, int key)
1.3       ohara      80: {
                     81:     m->tag  = key;
                     82:     m->flag = MATHCAP_FLAG_ALLOW;
                     83: }
1.2       ohara      84:
1.5       ohara      85: static table *new_table(int *src)
1.2       ohara      86: {
1.5       ohara      87:     table *new;
1.3       ohara      88:     int len=0;
                     89:     int i;
                     90:     while (src[len++] != 0) {
                     91:     }
1.9       ohara      92:     new = MALLOC(sizeof(table)*len);
1.3       ohara      93:     for(i=0; i<len; i++) {
1.5       ohara      94:         table_init(new+i, src[i]);
1.3       ohara      95:     }
                     96:     return new;
1.2       ohara      97: }
                     98:
1.7       ohara      99: /* looking for an item of the tag */
1.5       ohara     100: static table *table_lookup(table *tbl, int tag)
1.2       ohara     101: {
1.5       ohara     102:     while (tbl->tag != 0) {
                    103:         if (tbl->tag == tag) {
                    104:             return tbl;
1.2       ohara     105:         }
1.5       ohara     106:         tbl++;
1.2       ohara     107:     }
                    108:     return NULL;
                    109: }
                    110:
1.7       ohara     111: /* controller about a cmo identified by the tag */
1.5       ohara     112: static void table_ctl(table *tbl, int tag, int flag)
1.2       ohara     113: {
1.5       ohara     114:     table *e = table_lookup(tbl, tag);
1.2       ohara     115:     if (e != NULL) {
                    116:         e->flag = flag;
                    117:     }
                    118: }
                    119:
1.7       ohara     120: /* controller about all CMObjects */
1.5       ohara     121: static void table_ctl_all(table *tbl, int flag)
1.2       ohara     122: {
1.5       ohara     123:     while (tbl->tag != 0) {
                    124:         tbl->flag = flag;
                    125:         tbl++;
1.2       ohara     126:     }
                    127: }
                    128:
1.7       ohara     129: /* getting the list of tags of all allowed objects */
1.5       ohara     130: static cmo_list *table_get_all(table *tbl)
1.2       ohara     131: {
                    132:     cmo_list *list = new_cmo_list();
1.5       ohara     133:     while (tbl->tag != 0) {
                    134:         if (tbl->flag == MATHCAP_FLAG_ALLOW) {
                    135:             list_append(list, (cmo *)new_cmo_int32(tbl->tag));
1.2       ohara     136:         }
1.5       ohara     137:         tbl++;
1.2       ohara     138:     }
                    139:     return list;
                    140: }
                    141:
1.7       ohara     142: /* giving a permssion to send objects identified by the tag. */
1.5       ohara     143: __inline__
                    144: static void table_allow(table *tbl, int tag)
                    145: {
                    146:     table_ctl(tbl, tag, MATHCAP_FLAG_ALLOW);
                    147: }
                    148:
1.7       ohara     149: /* taking a permssion to send objects identified by the tag. */
1.5       ohara     150: __inline__
                    151: static void table_deny(table *tbl, int tag)
                    152: {
                    153:     table_ctl(tbl, tag, MATHCAP_FLAG_DENY);
                    154: }
                    155:
                    156: static void table_update(table *cmotbl, cmo_list* types)
                    157: {
                    158:     cell *el = list_first(types);
                    159:     cmo_int32 *ob;
                    160:     while(!list_endof(types, el)) {
                    161:         ob = (cmo_int32 *)el->cmo;
                    162:         if (ob->tag == CMO_INT32) {
                    163:             table_allow(cmotbl, ob->i);
                    164:         }
                    165:         el = list_next(el);
                    166:     }
                    167: }
1.1       ohara     168:
1.7       ohara     169: /* getting a permission to send objects identified by the tag. */
1.5       ohara     170: static int table_allowQ_tag(table *tbl, int tag)
1.1       ohara     171: {
1.5       ohara     172:     while (tbl->tag != 0 && tbl->tag != tag) {
                    173:         tbl++;
1.1       ohara     174:     }
1.5       ohara     175:     return tbl->flag;
1.1       ohara     176: }
                    177:
1.5       ohara     178: static int table_allowQ_cmo_list(table *cmotbl, cmo_list *ob)
1.1       ohara     179: {
                    180:     cell *el;
1.5       ohara     181:     if (table_allowQ_tag(cmotbl, ob->tag)) {
1.1       ohara     182:         el = list_first(ob);
                    183:         while (!list_endof(ob, el)) {
1.5       ohara     184:             if (!table_allowQ_cmo(cmotbl, el->cmo)) {
1.1       ohara     185:                 return MATHCAP_FLAG_DENY;
                    186:             }
                    187:             el = list_next(el);
                    188:         }
                    189:         return MATHCAP_FLAG_ALLOW;
                    190:     }
                    191:     return MATHCAP_FLAG_DENY;
                    192: }
                    193:
1.2       ohara     194: __inline__
1.5       ohara     195: static int table_allowQ_cmo_monomial32(table *cmotbl, cmo_monomial32 *ob)
1.1       ohara     196: {
1.5       ohara     197:     return table_allowQ_tag(cmotbl, ob->tag)
                    198:         && table_allowQ_cmo(cmotbl, ob->coef);
1.1       ohara     199: }
                    200:
1.2       ohara     201: __inline__
1.5       ohara     202: static int table_allowQ_cmo_mathcap(table *cmotbl, cmo_mathcap *ob)
1.1       ohara     203: {
1.5       ohara     204:     return table_allowQ_tag(cmotbl, ob->tag)
                    205:         && table_allowQ_cmo(cmotbl, ob->ob);
1.1       ohara     206: }
                    207:
1.7       ohara     208: /* getting a permission to send the following object. */
1.5       ohara     209: static int table_allowQ_cmo(table *cmotbl, cmo *ob)
1.1       ohara     210: {
                    211:     int tag = ob->tag;
                    212:     switch(tag) {
                    213:     case CMO_LIST:
                    214:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.5       ohara     215:         return table_allowQ_cmo_list(cmotbl, (cmo_list *)ob);
1.1       ohara     216:     case CMO_MATHCAP:
                    217:     case CMO_ERROR2:
                    218:     case CMO_RING_BY_NAME:
                    219:     case CMO_INDETERMINATE:
1.5       ohara     220:         return table_allowQ_cmo_mathcap(cmotbl, (cmo_mathcap *)ob);
1.1       ohara     221:     case CMO_MONOMIAL32:
1.5       ohara     222:         return table_allowQ_cmo_monomial32(cmotbl, (cmo_monomial32 *)ob);
1.1       ohara     223:     default:
1.5       ohara     224:         return table_allowQ_tag(cmotbl, tag);
1.1       ohara     225:     }
                    226: }
                    227:
1.7       ohara     228: /* getting the System Information */
1.5       ohara     229: static cmo_list *sysinfo_get()
1.1       ohara     230: {
                    231:     cmo_list *syslist = new_cmo_list();
1.3       ohara     232:     cmo_int32 *ver    = new_cmo_int32(sysinfo.version);
                    233:     cmo_string *vers  = new_cmo_string(sysinfo.version_string);
                    234:     cmo_string *host  = new_cmo_string(sysinfo.hosttype);
                    235:     cmo_string *sname = new_cmo_string(sysinfo.sysname);
                    236:     return list_appendl(syslist, ver, sname, vers, host, NULL);
1.1       ohara     237: }
                    238:
                    239: static char *new_string(char *s)
                    240: {
1.9       ohara     241:     char *t = MALLOC(strlen(s)+1);
1.1       ohara     242:     strcpy(t, s);
                    243:     return t;
                    244: }
                    245:
1.5       ohara     246: static int *new_int_array(int *array)
                    247: {
                    248:     int *new_array;
                    249:     int length = 0;
                    250:     while(array[length++] != 0)
                    251:         ;
1.9       ohara     252:     new_array = MALLOC(sizeof(int)*length);
1.5       ohara     253:     return memcpy(new_array, array, sizeof(int)*length);
                    254: }
                    255:
                    256: void mathcap_init(int ver, char *vstr, char *sysname, int cmos[], int sms[])
1.1       ohara     257: {
                    258:     char *host = getenv("HOSTTYPE");
1.3       ohara     259:     sysinfo.hosttype = (host != NULL)? new_string(host): "UNKNOWN";
                    260:     sysinfo.sysname  = new_string(sysname);
                    261:     sysinfo.version_string = new_string(vstr);
1.5       ohara     262:     sysinfo.version  = ver;
                    263:     if (cmos != NULL) {
                    264:         sysinfo.cmo_tags = new_int_array(cmos);
                    265:     }
                    266:     if (sms != NULL) {
                    267:         sysinfo.sm_cmds = new_int_array(sms);
                    268:     }
1.3       ohara     269: }
1.1       ohara     270:
1.5       ohara     271: mathcap *new_mathcap()
1.3       ohara     272: {
1.9       ohara     273:     mathcap *new = MALLOC(sizeof(mathcap));
1.5       ohara     274:     new->cmotbl = new_table(sysinfo.cmo_tags);
                    275:     new->smtbl  = new_table(sysinfo.sm_cmds);
                    276:     return new;
1.1       ohara     277: }
                    278:
1.7       ohara     279: /* generating a cmo_mathcap by a local database. */
1.3       ohara     280: cmo_mathcap* mathcap_get(mathcap *this)
1.1       ohara     281: {
                    282:     cmo_list *mc = new_cmo_list();
                    283:     cmo_list *l3 = new_cmo_list();
1.10      ohara     284:     list_append(l3, (cmo *)list_appendl(new_cmo_list(),
1.3       ohara     285:                                  new_cmo_int32(OX_DATA),
1.5       ohara     286:                                  table_get_all(this->cmotbl), NULL));
                    287:     list_appendl(mc, (cmo *)sysinfo_get(),
                    288:                  (cmo *)table_get_all(this->smtbl), (cmo *)l3, NULL);
1.1       ohara     289:     return new_cmo_mathcap((cmo *)mc);
                    290: }
                    291:
                    292: /* ( ..., ( type, (...) ), (cmo_int32, (...) ), ... )  */
                    293: /*                ^^^^^ Here!                          */
                    294: static cmo_list *get_messagetypes(cmo_list *ob, int type)
                    295: {
                    296:     cmo_list  *c;
                    297:     cell *el;
                    298:
                    299:     for (el = list_first(ob); !list_endof(ob, el); el = list_next(el)) {
                    300:         c = (cmo_list *)el->cmo;
                    301:         if (((cmo_int32 *)list_nth(c, 0))->i == type) {
                    302:             return (cmo_list *)list_nth(c, 1);
                    303:         }
                    304:     }
                    305:     return NULL;
                    306: }
                    307:
                    308: /* cmo_mathcap->ob = ( (...), (...), ( ( cmo_int32, (...) ), ...), ...) */
                    309: /*                                              ^^^^^ Here!         */
1.2       ohara     310: __inline__
1.1       ohara     311: static cmo_list *cmo_mathcap_get_cmotypes(cmo_mathcap *mc)
                    312: {
                    313:     cmo_list *ob = (cmo_list *)list_nth((cmo_list *)mc->ob, 2);
                    314:     return get_messagetypes(ob, OX_DATA);
                    315: }
                    316:
1.7       ohara     317: /* The mathcap_update integrates received cmo_mathcap into the mathcap
                    318:    database. If this == NULL, then an instance of mathcap is generated. */
1.3       ohara     319: mathcap *mathcap_update(mathcap *this, cmo_mathcap *mc)
1.1       ohara     320: {
                    321:     cmo_list *types;
1.5       ohara     322:     types = cmo_mathcap_get_cmotypes(mc);
                    323:     if (types != NULL) {
                    324:         table_ctl_all(this->cmotbl, MATHCAP_FLAG_DENY);
                    325:         table_update(this->cmotbl, types);
1.1       ohara     326:     }
1.5       ohara     327:
                    328:     return this;
                    329: }
                    330:
                    331: int mathcap_allowQ_cmo(mathcap *this, cmo *ob)
                    332: {
                    333:     return table_allowQ_cmo(this->cmotbl, ob);
1.1       ohara     334: }

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