Annotation of OpenXM/src/ruby/testserv1.c, Revision 1.1
1.1 ! ogino 1:
! 2: /* $OpenXM$ */
! 3:
! 4: #include <stdio.h>
! 5: /* #include <pari.h> */
! 6: #include "../ox_toolkit/ox.h"
! 7:
! 8: int fd_rw = 3;
! 9:
! 10: static int stack_p = 0;
! 11: static cmo *stack[100];
! 12:
! 13: int explain_cmo(cmo *);
! 14:
! 15: int push(cmo* m)
! 16: {
! 17: stack[stack_p] = m;
! 18: stack_p++;
! 19: }
! 20:
! 21: cmo* pop()
! 22: {
! 23: if (stack_p > 0) {
! 24: stack_p--;
! 25: return stack[stack_p];
! 26: }
! 27: return new_cmo_null();
! 28: }
! 29:
! 30: void pops(int n)
! 31: {
! 32: stack_p -= n;
! 33: if (stack_p < 0) {
! 34: stack_p = 0;
! 35: }
! 36: }
! 37:
! 38: int sm_popCMO()
! 39: {
! 40: cmo* m = pop();
! 41:
! 42: if (m != NULL) {
! 43: send_ox_cmo(fd_rw, m);
! 44: return 0;
! 45: }
! 46: return SM_popCMO;
! 47: }
! 48:
! 49: cmo_error2 *make_error2(int code)
! 50: {
! 51: return new_cmo_int32(-1);
! 52: }
! 53:
! 54: int get_i()
! 55: {
! 56: cmo *c = pop();
! 57: if (c->tag == CMO_INT32) {
! 58: return ((cmo_int32 *)c)->i;
! 59: }else if (c->tag == CMO_ZZ) {
! 60: return mpz_get_si(((cmo_zz *)c)->mpz);
! 61: }
! 62: make_error2(-1);
! 63: return 0;
! 64: }
! 65:
! 66: /*
! 67: int getdisc()
! 68: {
! 69: GEN d, x;
! 70: int n;
! 71: n = get_i();
! 72: fprintf(stderr, " getdisc of %d ", n);
! 73: x = stoi(n);
! 74: d = quaddisc(x);
! 75: n = itos(d);
! 76: fprintf(stderr, " = %d\n", n);
! 77: push(new_cmo_int32(n));
! 78: }
! 79: */
! 80: int prms[] = {
! 81: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
! 82: };
! 83:
! 84: int sqfr(n)
! 85: {
! 86: int i;
! 87: for (i = 0; prms[i] < sqrt(n); i++) {
! 88: while(n%(prms[i]*prms[i]) == 0)
! 89: n = n / (prms[i]*prms[i]);
! 90: }
! 91: return n;
! 92: }
! 93:
! 94: int getdisc(n)
! 95: {
! 96: int sg = 1;
! 97: if (n < 0) { sg = -1; n = -n;}
! 98: n = sqfr(n);
! 99: if ((n % 4 == 1) && (sg == 1))
! 100: return n;
! 101: else if ((n % 4 == 3) && (sg == -1))
! 102: return -n;
! 103: else
! 104: return 4*n*sg;
! 105: }
! 106:
! 107: int pushdisc()
! 108: {
! 109: push(new_cmo_int32(getdisc(get_i())));
! 110: return 0;
! 111: }
! 112:
! 113: int sm_executeFunction()
! 114: {
! 115: cmo_string *func = (cmo_string *)pop();
! 116: if (func->tag != CMO_STRING) {
! 117: push(make_error2(0));
! 118: return -1;
! 119: }
! 120: if (strcmp(func->s, "disc") == 0) {
! 121: /* fprintf(stderr, "%s\n", " getdisc"); */
! 122: pushdisc();
! 123: }else {
! 124: push(make_error2(0));
! 125: return -1;
! 126: }
! 127: }
! 128:
! 129:
! 130: int receive_and_execute_sm_command()
! 131: {
! 132: int code = receive_int32(fd_rw);
! 133: switch(code) {
! 134: case SM_popCMO:
! 135: fprintf(stderr, "%s\n", " SM_popCMO");
! 136: sm_popCMO();
! 137: break;
! 138: case SM_executeFunction:
! 139: fprintf(stderr, "%s\n", " SM_executeFunction");
! 140: sm_executeFunction();
! 141: break;
! 142: default:
! 143: }
! 144: }
! 145:
! 146: int receive()
! 147: {
! 148: int tag;
! 149:
! 150: tag = receive_ox_tag(fd_rw);
! 151: switch(tag) {
! 152: case OX_DATA:
! 153: push(receive_cmo(fd_rw));
! 154: break;
! 155: case OX_COMMAND:
! 156: receive_and_execute_sm_command();
! 157: break;
! 158: default:
! 159: }
! 160: return 0;
! 161: }
! 162:
! 163: int main()
! 164: {
! 165: decideByteOrderServer(fd_rw, 0);
! 166:
! 167: /* pari_init (200000, 2000); */
! 168: while(1) {
! 169: receive();
! 170: }
! 171: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>