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

Annotation of OpenXM/src/ox_ntl/oxserv.c, Revision 1.3

1.3     ! iwane       1: /* $OpenXM: OpenXM/src/ox_ntl/oxserv.c,v 1.2 2003/11/08 12:34:00 iwane Exp $ */
1.1       iwane       2:
                      3: #include <stdio.h>
                      4: #include <stdlib.h>
                      5: #include <string.h>
                      6: #include <errno.h>
1.2       iwane       7: #include <signal.h>
1.3     ! iwane       8: #include <setjmp.h>
1.1       iwane       9:
                     10: #include "oxserv.h"
1.2       iwane      11: #include "oxstack.h"
1.1       iwane      12:
1.3     ! iwane      13: #include "gc/gc.h"
1.1       iwane      14:
                     15: #define DPRINTF(x)     printf x; fflush(stdout)
                     16:
1.3     ! iwane      17: #define FP     stdout
        !            18: #define EPRINTF(x)     fprintf x; fflush(FP)
        !            19:
        !            20: #if 1
1.1       iwane      21: /*===========================================================================*
                     22:  * for DEBUG
                     23:  *===========================================================================*/
                     24: #include <stdarg.h>
                     25: void
                     26: dprintf(const char *fmt, ...)
                     27: {
                     28:        FILE *fp;
                     29:        va_list ap;
                     30:        va_start(ap, fmt);
                     31:
                     32:        fp = fopen("error.txt", "a");
                     33:
                     34:        vfprintf(fp, fmt, ap);
                     35:
                     36:        fflush(fp);
                     37:        fclose(fp);
                     38:
                     39:        va_end(ap);
                     40: }
                     41: #endif
                     42:
                     43: /*===========================================================================*
1.2       iwane      44:  * MACRO
1.1       iwane      45:  *===========================================================================*/
1.3     ! iwane      46: #define oxserv_get_cmo_tag(m)  ((G_getCmoTag == NULL) ? m->tag : G_getCmoTag(m))
1.1       iwane      47:
                     48:
1.2       iwane      49: #define oxserv_delete_cmo(c)         \
                     50: do {                                 \
1.3     ! iwane      51:         if (c != NULL) {             \
        !            52:             if (G_DeleteCmo != NULL) \
1.2       iwane      53:                 G_DeleteCmo(c);      \
1.3     ! iwane      54:             else                     \
1.2       iwane      55:                 c = NULL;            \
1.3     ! iwane      56:         }                            \
1.2       iwane      57: } while (0)
                     58:
1.1       iwane      59:
                     60: /*===========================================================================*
1.2       iwane      61:  * Global Variables.
1.1       iwane      62:  *===========================================================================*/
1.2       iwane      63: /* ox */
                     64: static OXFILE *G_oxfilep = NULL;
                     65: static cmo_mathcap *G_oxserv_mathcap = NULL;
1.1       iwane      66:
1.3     ! iwane      67: /* signal */
        !            68: sigset_t       G_oxserv_sigusr1;
        !            69: static jmp_buf  G_jmpbuf;
        !            70:
1.2       iwane      71: /* User Function */
                     72: static void (*G_userExecuteFunction)(const char *, cmo **, int) = NULL;
                     73: static void (*G_userExecuteStringParser)(const char *) = NULL;
1.1       iwane      74:
1.2       iwane      75: static cmo *(*G_convertCmo)(cmo *) = NULL; /* convert user object ==> cmo */
1.1       iwane      76:
1.2       iwane      77: static void (*G_DeleteCmo)(cmo *) = NULL;
1.1       iwane      78:
1.2       iwane      79: static int  (*G_getCmoTag)(cmo *) = NULL;
1.1       iwane      80:
1.3     ! iwane      81:
        !            82: /*===========================================================================*
        !            83:  * CMO_ERROR2
        !            84:  *===========================================================================*/
        !            85: #define new_cmo_error2_string(msg) new_cmo_error2((cmo *)new_cmo_string(msg))
        !            86:
        !            87:
        !            88: static void
        !            89: oxserv_push_errormes(char *msg)
        !            90: {
        !            91:        EPRINTF((FP, "%s\n", msg));
        !            92:        oxstack_push((cmo *)new_cmo_error2_string(msg));
        !            93: }
        !            94:
        !            95: /*===========================================================================*
        !            96:  * synchronized malloc
        !            97:  *===========================================================================*/
        !            98: void *
        !            99: oxserv_malloc(size_t size)
        !           100: {
        !           101:        void *ptr;
        !           102:
        !           103:        BLOCK_INPUT();
        !           104:        ptr = GC_malloc(size);
        !           105:        UNBLOCK_INPUT();
        !           106:
        !           107:        return (ptr);
        !           108: }
        !           109:
        !           110: void
        !           111: oxserv_free(void *ptr, size_t size)
        !           112: {
        !           113:        /* nothing */
        !           114: }
        !           115:
        !           116: void *
        !           117: oxserv_realloc(void *org, size_t old, size_t size)
        !           118: {
        !           119:        void *ptr;
        !           120:
        !           121:        BLOCK_INPUT();
        !           122:        ptr = GC_realloc(org, size);
        !           123:        UNBLOCK_INPUT();
        !           124:
        !           125:        return (ptr);
        !           126: }
        !           127:
        !           128:
        !           129:
1.1       iwane     130: /*===========================================================================*
                    131:  * OX_SERVER
                    132:  *===========================================================================*/
                    133:
                    134: /*****************************************************************************
1.2       iwane     135:  * -- SM_popCMO --
                    136:  * pop an object from the stack, convert it into a serialized from according
                    137:  * to the standard CMO encoding scheme, and send it to the stream
                    138:  *
1.1       iwane     139:  * PARAM : fd : OXFILE
                    140:  * RETURN: NONE
                    141:  *****************************************************************************/
                    142: static void
                    143: oxserv_sm_popCMO(OXFILE *fd)
                    144: {
1.3     ! iwane     145:        cmo *m, *n;
        !           146:        m = oxstack_pop();
1.1       iwane     147:        if (m == NULL) {
1.3     ! iwane     148:                EPRINTF((FP, "stack underflow in popCMO\n"));
1.1       iwane     149:                m = new_cmo_null();
                    150:        } else if (G_convertCmo) {
1.3     ! iwane     151:                n = G_convertCmo(m);
        !           152:                if (m != n)
        !           153:                        oxserv_delete_cmo(m);
        !           154:                m = n;
1.1       iwane     155:        }
                    156:
                    157:        send_ox_cmo(fd, m);
                    158:
1.2       iwane     159:        oxserv_delete_cmo(m);
1.1       iwane     160: }
                    161:
                    162: /*****************************************************************************
1.2       iwane     163:  * -- SM_popString --
1.1       iwane     164:  * pop an cmo from stack, convert it into a string according to the
                    165:  * output format of the local system, and send the string.
1.2       iwane     166:  *
1.1       iwane     167:  * PARAM : fd : OXFILE
                    168:  * RETURN: NONE
                    169:  *****************************************************************************/
                    170: static void
                    171: oxserv_sm_popString(OXFILE *fd)
                    172: {
                    173:        char *str;
1.2       iwane     174:        cmo *m = oxstack_pop();
1.1       iwane     175:        cmo_string *m_str;
                    176:
1.3     ! iwane     177:        if (m == NULL) {
        !           178:                EPRINTF((FP, "stack underflow in popString\n"));
1.1       iwane     179:                m = new_cmo_null();
1.3     ! iwane     180:        }
1.1       iwane     181:
                    182:        str = new_string_set_cmo(m);
                    183:
                    184:        m_str = new_cmo_string(str);
                    185:
                    186:        send_ox_cmo(fd, (cmo *)m_str);
                    187:
1.2       iwane     188:        oxserv_delete_cmo(m);
                    189:        oxserv_delete_cmo((cmo *)m_str);
1.1       iwane     190:
1.2       iwane     191:        /* free(str); */
1.1       iwane     192: }
                    193:
                    194: /*****************************************************************************
1.2       iwane     195:  * -- SM_pops --
1.1       iwane     196:  * pop n and to discard element from the stack
1.2       iwane     197:  *
                    198:  * PARAM : NONE
1.1       iwane     199:  * RETURN: NONE
                    200:  *****************************************************************************/
                    201: static void
                    202: oxserv_sm_pops()
                    203: {
                    204:        cmo_int32 *c;
1.2       iwane     205:        cmo *m;
                    206:        int n;
                    207:        int i;
                    208:
                    209:        c = (cmo_int32 *)oxstack_pop(); /* suppose */
1.3     ! iwane     210:        if (c == NULL) {
        !           211:                EPRINTF((FP, "stack underflow in pops\n"));
        !           212:                return ;
        !           213:        }
1.1       iwane     214:
1.2       iwane     215:        n = oxstack_get_stack_pointer();
                    216:        if (c->i < n)
                    217:                n = c->i;
1.1       iwane     218:
1.2       iwane     219:        for (i = 0; i < n; i++) {
                    220:                m = oxstack_pop();
                    221:                oxserv_delete_cmo(m);
                    222:        }
                    223:
                    224:        oxserv_delete_cmo((cmo *)c);
1.1       iwane     225:
                    226: }
                    227:
                    228: /*****************************************************************************
1.2       iwane     229:  * -- SM_getsp --
                    230:  * push the current stack pointer onto the stack.
1.1       iwane     231:  *
                    232:  * PARAM : fd : OXFILE
                    233:  * RETURN: NONE
                    234:  *****************************************************************************/
                    235: static void
                    236: oxserv_sm_getsp()
                    237: {
1.2       iwane     238:        cmo_int32 *m = new_cmo_int32(oxstack_get_stack_pointer());
                    239:        oxstack_push((cmo *)m);
1.1       iwane     240: }
                    241:
                    242: /*****************************************************************************
                    243:  *
                    244:  * PARAM : ver    :
                    245:  *       : vstr   :
                    246:  *       : sysname:
                    247:  *       : cmos   :
                    248:  *       : sms    :
                    249:  * RETURN: NONE
                    250:  * SEE   : oxserv_set();
                    251:  *****************************************************************************/
                    252: static void
                    253: oxserv_mathcap_init(int ver, char *vstr, char *sysname, int *cmos, int *sms)
                    254: {
                    255:        int i;
                    256:
                    257:        int local_sms[] = {
                    258:                SM_popCMO,
                    259:                SM_mathcap,
                    260:                SM_setMathCap,
                    261:                SM_pops,
                    262:                SM_getsp,
                    263:                SM_popString,
                    264:                SM_pushCMOtag,
                    265:                0,
                    266:                SM_executeFunction,
                    267:                SM_dupErrors,
                    268:                SM_executeStringByLocalParser,
                    269:                SM_executeStringByLocalParserInBatchMode,
                    270:                SM_shutdown,
                    271:                0,
                    272:        };
                    273:
                    274:        /* depend on ox_toolkit */
                    275:        int local_cmos[] = {
                    276:                CMO_ERROR2,
                    277:                CMO_NULL,
                    278:                CMO_INT32,
                    279:                CMO_STRING,
                    280:                CMO_MATHCAP,
                    281:                CMO_LIST,
                    282:                CMO_MONOMIAL32,
                    283:                CMO_ZZ,
                    284:                CMO_ZERO,
                    285:                CMO_RECURSIVE_POLYNOMIAL,
                    286:                CMO_DISTRIBUTED_POLYNOMIAL,
                    287:                CMO_POLYNOMIAL_IN_ONE_VARIABLE,
                    288:                CMO_DMS_GENERIC,
                    289:                CMO_RING_BY_NAME,
                    290:                CMO_INDETERMINATE,
                    291:                CMO_TREE,
                    292:                CMO_LAMBDA,
                    293:                0,
                    294:                CMO_QQ,
                    295:                CMO_ATTRIBUTE_LIST,
                    296:                CMO_DMS,
                    297:                CMO_DMS_OF_N_VARIABLES,
                    298:                CMO_LIST_R,
                    299:                CMO_INT32COEFF,
                    300:                CMO_RATIONAL,
                    301:                CMO_DATUM,
                    302:                0,
                    303:        };
                    304:
                    305:        if (sms == NULL) {
                    306:                sms = local_sms;
                    307:
                    308:                for (i = 0; sms[i] != 0; i++)
                    309:                        ;
                    310:                if (G_userExecuteFunction != NULL)
                    311:                        sms[i++] = SM_executeFunction;
                    312:
                    313:                if (G_userExecuteStringParser != NULL) {
                    314:                        sms[i++] = SM_executeStringByLocalParser;
                    315:                        sms[i++] = SM_executeStringByLocalParserInBatchMode;
                    316:                }
                    317:
                    318:                sms[i] = 0;
                    319:        }
                    320:        if (cmos == NULL)
                    321:                cmos = local_cmos;
                    322:
                    323:        mathcap_init(ver, vstr, sysname, cmos, sms);
                    324:
1.2       iwane     325:        oxserv_delete_cmo((cmo *)G_oxserv_mathcap);
1.1       iwane     326:
                    327:        G_oxserv_mathcap = mathcap_get(new_mathcap());
                    328: }
                    329:
1.2       iwane     330: /*****************************************************************************
                    331:  * -- SM_mathcap --
                    332:  * push the mathcap of the server.
                    333:  *
                    334:  * PARAM : NONE
                    335:  * RETURN: NONE
                    336:  *****************************************************************************/
1.1       iwane     337: static void
                    338: oxserv_sm_mathcap()
                    339: {
                    340:        if (G_oxserv_mathcap == NULL) {
                    341:                oxserv_mathcap_init(0, "", "oxserv", NULL, NULL);
                    342:        }
                    343:
1.2       iwane     344:        oxstack_push((cmo *)G_oxserv_mathcap);
1.1       iwane     345: }
                    346:
1.2       iwane     347: /*****************************************************************************
                    348:  * -- SM_executeStringByLocalParserInBatchMode --
                    349:  * peek a character string s, parse it by the local parser of the stack machine,
                    350:  * and interpret by the local interpreter.
                    351:  *
                    352:  * PARAM : NONE
                    353:  * RETURN: NONE
                    354:  *****************************************************************************/
1.1       iwane     355: static void
                    356: oxserv_sm_executeStringByLocalParserInBatchMode(void)
                    357: {
1.2       iwane     358:        cmo_string *str = (cmo_string *)oxstack_peek();
1.3     ! iwane     359:        if (str == NULL) {
        !           360:                oxserv_push_errormes("stack underflow in executeStringByLocalParserInBatchMode");
        !           361:                return ;
        !           362:        }
1.2       iwane     363:        G_userExecuteStringParser(str->s);
1.1       iwane     364: }
                    365:
1.2       iwane     366: /*****************************************************************************
                    367:  * -- SM_executeStringByLocalParser --
                    368:  * pop a character string s, parse it by the local parser of the stack machine,
                    369:  * and interpret by the local interpreter.
                    370:  *
                    371:  * PARAM : NONE
                    372:  * RETURN: NONE
                    373:  *****************************************************************************/
1.1       iwane     374: static void
                    375: oxserv_sm_executeStringByLocalParser(void)
                    376: {
1.2       iwane     377:        cmo_string *str = (cmo_string *)oxstack_pop();
1.3     ! iwane     378:        if (str == NULL) {
        !           379:                oxserv_push_errormes("stack underflow in executeStringByLocalParser");
        !           380:                return ;
        !           381:        }
1.2       iwane     382:        G_userExecuteStringParser(str->s);
1.1       iwane     383: }
                    384:
                    385:
                    386:
1.2       iwane     387: /*****************************************************************************
                    388:  * -- SM_executeFunction --
                    389:  * pop s as a function name, pop n as the number of arguments and to execute a
                    390:  * local function s with n arguments poped from the stack.
                    391:  *
                    392:  * PARAM : NONE
                    393:  * RETURN: NONE
                    394:  *****************************************************************************/
1.1       iwane     395: static void
                    396: oxserv_sm_executeFunction(void)
                    397: {
                    398:        int i;
1.2       iwane     399:        cmo_string *name = (cmo_string *)oxstack_pop();
                    400:        cmo_int32 *cnt = (cmo_int32 *)oxstack_pop();
1.3     ! iwane     401:        cmo **arg;
        !           402:
        !           403:        arg = (cmo **)malloc(cnt->i * sizeof(cmo *));
        !           404:
        !           405:        if (name == NULL || cnt == NULL) {
        !           406:                oxserv_push_errormes("stack underflow in executeFunction");
        !           407:                return ;
        !           408:        }
1.1       iwane     409:
                    410:        for (i = 0; i < cnt->i; i++) {
1.2       iwane     411:                arg[i] = oxstack_pop();
1.3     ! iwane     412:                if (arg[i] == NULL) {
        !           413:                        oxserv_push_errormes("stack underflow in executeFunction");
        !           414:                        return ;
        !           415:                }
1.1       iwane     416:        }
                    417:
                    418:        /* user function */
1.2       iwane     419:        G_userExecuteFunction(name->s, arg, cnt->i);
1.1       iwane     420:
                    421:
                    422:        for (i = 0; i < cnt->i; i++) {
1.2       iwane     423:                oxserv_delete_cmo(arg[i]);
1.1       iwane     424:        }
                    425:
1.2       iwane     426:        oxserv_delete_cmo((cmo *)name);
                    427:        oxserv_delete_cmo((cmo *)cnt);
1.1       iwane     428:
                    429:        free(arg);
                    430: }
                    431:
1.2       iwane     432: /*****************************************************************************
                    433:  * -- SM_pushCMOtag --
                    434:  * push the CMO tag of the top object on the stack.
                    435:  *
                    436:  * PARAM : NONE
                    437:  * RETURN: NONE
                    438:  *****************************************************************************/
1.1       iwane     439: static void
                    440: oxserv_sm_pushCMOtag()
                    441: {
1.2       iwane     442:        cmo *c = oxstack_peek();
1.3     ! iwane     443:        cmo_int32 *tag = new_cmo_int32(oxserv_get_cmo_tag(c));
1.2       iwane     444:        oxstack_push((cmo *)tag);
1.1       iwane     445: }
                    446:
                    447:
1.3     ! iwane     448: /*****************************************************************************
        !           449:  * -- SM_dupErrors --
        !           450:  *
        !           451:  * PARAM : NONE
        !           452:  * RETURN: NONE
        !           453:  *****************************************************************************/
1.1       iwane     454: static void
                    455: oxserv_sm_dupErrors()
                    456: {
                    457:        cmo_list *list;
                    458:        cmo *c;
                    459:        int i;
                    460:
                    461:        list = new_cmo_list();
                    462:
1.2       iwane     463:        for (i = 0; i < oxstack_get_stack_pointer(); i++) {
                    464:                c = oxstack_get(i);
1.1       iwane     465:                if (c->tag == CMO_ERROR2) {
                    466:                        list_append(list, c);
                    467:                }
                    468:        }
                    469:
1.2       iwane     470:        oxstack_push((cmo *)list);
1.1       iwane     471: }
                    472:
1.3     ! iwane     473:
        !           474:
        !           475:
        !           476: /*****************************************************************************
        !           477:  * -- SM_control_reset_connection -- signal handler for SIGUSR1 --
        !           478:  *
        !           479:  * PARAM : NONE
        !           480:  * RETURN: NONE
        !           481:  *****************************************************************************/
1.2       iwane     482: static void
                    483: oxserv_sm_control_reset_connection(int sig)
                    484: {
                    485:        int tag;
                    486:        OXFILE *fd = G_oxfilep;
                    487:
1.3     ! iwane     488:        DPRINTF(("reset -- start ==> "));
1.2       iwane     489:        send_ox_tag(fd, OX_SYNC_BALL);
                    490:
                    491:        oxstack_init_stack();
                    492:
                    493:        for (;;) {
                    494:                tag = receive_ox_tag(fd);
1.3     ! iwane     495:                DPRINTF(("[OX:%d=0x%x]", tag, tag));
1.2       iwane     496:                if (tag == OX_SYNC_BALL)
                    497:                        break;
                    498:                if (tag == OX_DATA)
                    499:                        receive_cmo(fd);
1.3     ! iwane     500:                else
        !           501:                        receive_int32(fd);
1.2       iwane     502:        }
1.3     ! iwane     503:        DPRINTF((" <== end.\n"));
        !           504:
        !           505:        longjmp(G_jmpbuf, sig);
1.2       iwane     506: }
1.1       iwane     507:
1.3     ! iwane     508: /*****************************************************************************
        !           509:  * execute sm command
        !           510:  *
        !           511:  * PARAM : NONE
        !           512:  * RETURN: NONE
        !           513:  *****************************************************************************/
1.1       iwane     514: static int
1.3     ! iwane     515: oxserv_execute_sm_command(OXFILE *fd, int code)
1.1       iwane     516: {
1.3     ! iwane     517:
        !           518:        DPRINTF(("[SM:%d=0x%x]", code, code));
1.1       iwane     519:
                    520:        switch (code) {
1.3     ! iwane     521:        case SM_popCMO: /* 262 */
1.1       iwane     522:                oxserv_sm_popCMO(fd);
                    523:                break;
1.3     ! iwane     524:        case SM_executeStringByLocalParser: /* 268 */
1.1       iwane     525:                if (G_userExecuteStringParser)
                    526:                        oxserv_sm_executeStringByLocalParser();
                    527:                break;
1.3     ! iwane     528:        case SM_executeStringByLocalParserInBatchMode: /* 274 */
1.1       iwane     529:                if (G_userExecuteStringParser)
                    530:                        oxserv_sm_executeStringByLocalParserInBatchMode();
                    531:                break;
1.3     ! iwane     532:        case SM_pops: /* 265 */
1.1       iwane     533:                oxserv_sm_pops();
                    534:                break;
1.3     ! iwane     535:        case SM_popString: /* 263 */
1.1       iwane     536:                oxserv_sm_popString(fd);
                    537:                break;
1.3     ! iwane     538:        case SM_getsp: /* 275 */
1.1       iwane     539:                oxserv_sm_getsp();
                    540:                break;
1.3     ! iwane     541:        case SM_mathcap: /* 264 */
1.1       iwane     542:                oxserv_sm_mathcap();
                    543:                break;
1.3     ! iwane     544:        case SM_setMathCap: /* 273 */
1.1       iwane     545:                /* dont support */
1.2       iwane     546:                oxstack_pop();
1.1       iwane     547:                break;
1.3     ! iwane     548:        case SM_executeFunction: /* 269 */
1.1       iwane     549:                if (G_userExecuteFunction)
                    550:                        oxserv_sm_executeFunction();
                    551:                break;
1.3     ! iwane     552:        case SM_pushCMOtag: /* 277 */
1.1       iwane     553:                oxserv_sm_pushCMOtag();
                    554:                break;
1.3     ! iwane     555:        case SM_dupErrors: /* 276 */
1.1       iwane     556:                oxserv_sm_dupErrors();
                    557:                break;
1.3     ! iwane     558:        case SM_popSerializedLocalObject:
        !           559:        case SM_setName:
        !           560:        case SM_evalName:
        !           561:        case SM_beginBlock:
        !           562:        case SM_endBlock:
        !           563:        case SM_shutdown:
        !           564:        case SM_executeFunctionAndPopCMO:
        !           565:        case SM_executeFunctionAndPopSerializedLocalObject:
1.2       iwane     566:        case SM_control_reset_connection:
                    567:        case SM_control_reset_connection_server:
1.1       iwane     568:        default:
                    569:                break;
                    570:        }
                    571:        return (OXSERV_SUCCESS);
                    572: }
                    573:
                    574:
1.2       iwane     575:
1.1       iwane     576: /*****************************************************************************
                    577:  * reveice ox_data
1.2       iwane     578:  *
1.1       iwane     579:  * PARAM : fd : OXFILE
                    580:  * RETURN: NONE
                    581:  *****************************************************************************/
1.3     ! iwane     582: static int
        !           583: oxserv_ox_receive(OXFILE *fd)
1.1       iwane     584: {
                    585:        int tag;
                    586:        cmo *c;
                    587:        int ret = OXSERV_SUCCESS;
1.3     ! iwane     588:        int code;
1.1       iwane     589:
                    590:        tag = receive_ox_tag(fd);
                    591:
                    592:        switch (tag) {
                    593:        case OX_DATA:
                    594:                c = receive_cmo(fd);
1.3     ! iwane     595:                DPRINTF(("[CMO:%d=0x%x]", c->tag, c->tag));
1.2       iwane     596:                oxstack_push(c);
1.1       iwane     597:                break;
                    598:
                    599:        case OX_COMMAND:
1.3     ! iwane     600:                code = receive_int32(fd);
        !           601:                ret = oxserv_execute_sm_command(fd, code);
1.1       iwane     602:                break;
                    603:
                    604:        default:
                    605:                DPRINTF(("receive unknown ox_tag: %d=0x%x\n", tag, tag));
                    606:                return (OXSERV_FAILURE);
                    607:        }
                    608:
                    609:        return (ret);
                    610: }
                    611:
1.3     ! iwane     612: int
        !           613: oxserv_receive(OXFILE *fd)
        !           614: {
        !           615:        int i = 0;
        !           616:        int ret;
        !           617:
        !           618:        ret = setjmp(G_jmpbuf);
        !           619:        if (ret == 0) {
        !           620:                DPRINTF(("setjmp first time -- %d\n", ret));
        !           621:        } else {
        !           622:                DPRINTF(("setjmp return from longjmp() -- %d -- \n", ret));
        !           623:        }
        !           624:
        !           625:        for (;; i++) {
        !           626:                ret = oxserv_ox_receive(fd);
        !           627:                if (ret != OXSERV_SUCCESS)
        !           628:                        break;
        !           629:        }
        !           630:        return (ret);
        !           631: }
        !           632:
1.1       iwane     633:
                    634: /*****************************************************************************
                    635:  * initialize oxserver
                    636:  *
                    637:  * PARAM : see oxserv_mathcap_init()
1.2       iwane     638:  * RETURN: success : OXSERV_SUCCESS
                    639:  *       : failure : OXSERV_FAILURE
1.1       iwane     640:  * SEE   : oxserv_mathcap_init()
                    641:  *       : oxserv_set();
                    642:  *****************************************************************************/
                    643: int
                    644: oxserv_init(OXFILE *oxfp, int ver, char *vstr, char *sysname, int *cmos, int *sms)
                    645: {
                    646:        int ret;
                    647:
1.3     ! iwane     648:        DPRINTF(("init start\n"));
        !           649:
1.2       iwane     650:        ret = oxstack_init_stack();
1.1       iwane     651:        if (ret != OXSERV_SUCCESS)
                    652:                return (ret);
                    653:
1.2       iwane     654:        G_oxfilep = oxfp;
                    655:
1.1       iwane     656:        oxserv_mathcap_init(ver, vstr, sysname, cmos, sms);
                    657:
1.3     ! iwane     658:        /* signal */
        !           659:        sigemptyset(&G_oxserv_sigusr1);
        !           660:        sigaddset(&G_oxserv_sigusr1, SIGUSR1);
1.2       iwane     661:        signal(SIGUSR1, oxserv_sm_control_reset_connection);
                    662:
1.3     ! iwane     663:        /* initialize GMP memory functions. */
        !           664:        mp_set_memory_functions(oxserv_malloc, oxserv_realloc, oxserv_free);
        !           665:
        !           666:        /* session start */
1.1       iwane     667:        oxf_determine_byteorder_server(oxfp);
                    668:
                    669:        return (OXSERV_SUCCESS);
                    670: }
                    671:
                    672:
                    673: /*****************************************************************************
1.3     ! iwane     674:  * set oxserver behavior
1.2       iwane     675:  *
1.1       iwane     676:  * PARAM : mode : mode
                    677:  *              :
                    678:  *       : ptr  :
                    679:  *       : rsv  : reserve space.
1.2       iwane     680:  * RETURN: success : OXSERV_SUCCESS
                    681:  *       : failure : OXSERV_FAILURE
1.1       iwane     682:  * SEE   :
                    683:  *****************************************************************************/
                    684: int
                    685: oxserv_set(int mode, void *ptr, void *rsv)
                    686: {
                    687:        switch (mode) {
                    688:        case OXSERV_SET_EXECUTE_FUNCTION:
1.2       iwane     689:                G_userExecuteFunction = (void (*)(const char *, cmo **, int))ptr;
1.1       iwane     690:                break;
                    691:        case OXSERV_SET_EXECUTE_STRING_PARSER:
1.2       iwane     692:                G_userExecuteStringParser = (void (*)(const char *))ptr;
1.1       iwane     693:                break;
                    694:        case OXSERV_SET_CONVERT_CMO:
                    695:                G_convertCmo = (cmo *(*)(cmo *))ptr;
                    696:                break;
1.2       iwane     697:        case OXSERV_SET_DELETE_CMO:
                    698:                G_DeleteCmo = (void (*)(cmo *))ptr;
                    699:                break;
                    700:        case OXSERV_SET_GET_CMOTAG:
                    701:                G_getCmoTag = (int (*)(cmo *))ptr;
                    702:                break;
1.1       iwane     703:        default:
                    704:                return (OXSERV_FAILURE);
                    705:        }
                    706:
                    707:
                    708:        return (OXSERV_SUCCESS);
                    709: }
                    710:
                    711:
                    712: /*****************************************************************************
                    713:  * destroy
1.2       iwane     714:  *
                    715:  * PARAM : NONE
1.1       iwane     716:  * RETURN: NONE
                    717:  *****************************************************************************/
                    718: void
                    719: oxserv_dest()
                    720: {
1.2       iwane     721:        oxstack_dest();
1.1       iwane     722: }
                    723:
                    724:
1.2       iwane     725: #if __OXSERV_DEBUG
                    726: /*===========================================================================*
                    727:  * DEBUG
                    728:  *===========================================================================*/
                    729:
1.1       iwane     730:
                    731: cmo *
                    732: oxserv_executeFunction(const char *func, cmo **arg, int argc)
                    733: {
                    734:        int i;
                    735:
                    736:        printf("%s()\n", func);
                    737:
                    738:        for (i = 0; i < argc; i++) {
                    739:                printf("\t%2d: %s\n", i, new_string_set_cmo(arg[i]));
                    740:        }
                    741:
                    742:        return ((cmo *)new_cmo_int32(0));
                    743: }
                    744:
1.2       iwane     745: /*****************************************************************************
                    746:  * main
                    747:  *
                    748:  * PARAM : NONE
                    749:  * RETURN: NONE
                    750:  *****************************************************************************/
1.1       iwane     751: int
                    752: main(int argc, char *argv[])
                    753: {
                    754:        int fd = 3;
                    755:        int i;
                    756:        int ret;
                    757:
                    758:        OXFILE *oxfp = oxf_open(fd);
                    759:
                    760:        ox_stderr_init(stderr);
                    761:
1.2       iwane     762:        oxserv_init(oxfp, 0, "$Date: 2003/11/02 16:39:16 $", "oxserv", NULL, NULL);
1.1       iwane     763:
                    764:        DPRINTF(("main - start\n"));
                    765:        for (i = 0;; i++) {
                    766:                DPRINTF(("@"));
                    767:                ret = oxserv_receive(oxfp);
                    768:                if (ret != OXSERV_SUCCESS)
                    769:                        break;
                    770:        }
                    771:
1.2       iwane     772:        oxf_close(oxfp);
                    773:        oxserv_delete_cmo((cmo *)G_oxserv_mathcap);
1.1       iwane     774:
                    775:        return (0);
                    776: }
                    777:
                    778: #endif
1.2       iwane     779:
                    780:

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