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

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

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