Annotation of OpenXM/src/ox_toolkit/ox_Xsample.c, Revision 1.2
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.2 ! ohara 2: /* $OpenXM: OpenXM/src/ox_toolkit/ox_Xsample.c,v 1.1 1999/12/16 11:35:48 ohara Exp $ */
1.1 ohara 3:
4: #include <stdio.h>
1.2 ! ohara 5: #include "ox_toolkit.h"
1.1 ohara 6:
7: int fd_rw = 3;
8:
9: #define INIT_S_SIZE 2048
10: #define EXT_S_SIZE 2048
11:
12: static int stack_size = 0;
13: static int stack_pointer = 0;
14: static cmo **stack = NULL;
15:
16: int initialize_stack()
17: {
18: stack_pointer = 0;
19: stack_size = INIT_S_SIZE;
20: stack = malloc(stack_size*sizeof(cmo*));
21: }
22:
23: static int extend_stack()
24: {
25: int size2 = stack_size + EXT_S_SIZE;
26: cmo **stack2 = malloc(size2*sizeof(cmo*));
27: memcpy(stack2, stack, stack_size*sizeof(cmo *));
28: free(stack);
29: stack = stack2;
30: stack_size = size2;
31: }
32:
33: int push(cmo* m)
34: {
35: stack[stack_pointer] = m;
36: stack_pointer++;
37: if (stack_pointer >= stack_size) {
38: extend_stack();
39: }
40: }
41:
42: cmo* pop()
43: {
44: if (stack_pointer > 0) {
45: stack_pointer--;
46: return stack[stack_pointer];
47: }
48: return new_cmo_null();
49: }
50:
51: void pops(int n)
52: {
53: stack_pointer -= n;
54: if (stack_pointer < 0) {
55: stack_pointer = 0;
56: }
57: }
58:
59: #define VERSION 0x11121400
60: #define ID_STRING "ox_math server 1999/12/14 15:25:00"
61:
62: int sm_mathcap()
63: {
64: push(make_mathcap_object(VERSION, ID_STRING));
65: return 0;
66: }
67:
68: int sm_popCMO()
69: {
70: cmo* m = pop();
71:
72: if (m != NULL) {
73: send_ox_cmo(fd_rw, m);
74: return 0;
75: }
76: return SM_popCMO;
77: }
78:
79: cmo_error2 *make_error2(int code)
80: {
81: return new_cmo_int32(-1);
82: }
83:
84: int get_i()
85: {
86: cmo *c = pop();
87: if (c->tag == CMO_INT32) {
88: return ((cmo_int32 *)c)->i;
89: }else if (c->tag == CMO_ZZ) {
90: return mpz_get_si(((cmo_zz *)c)->mpz);
91: }
92: make_error2(-1);
93: return 0;
94: }
95:
96: int get_xy(int *x, int *y)
97: {
98: cmo *c = pop();
99: *x = get_i();
100: *y = get_i();
101: }
102:
103: int my_setpixel()
104: {
105: int x, y;
106: get_xy(&x, &y);
107: setpixel(x, y);
108: push(new_cmo_int32(0));
109: }
110:
111: int my_moveto()
112: {
113: int x, y;
114: get_xy(&x, &y);
115: moveto(x, y);
116: push(new_cmo_int32(0));
117: }
118:
119: int my_lineto()
120: {
121: int x, y;
122: get_xy(&x, &y);
123: lineto(x, y);
124: push(new_cmo_int32(0));
125: }
126:
127: int my_clear()
128: {
129: /* dummy */
130: pop();
131: push(new_cmo_int32(0));
132: }
133:
134: int sm_executeFunction()
135: {
136: cmo_string *func = (cmo_string *)pop();
137: if (func->tag != CMO_STRING) {
138: push(make_error2(0));
139: return -1;
140: }
141: if (strcmp(func->s, "setpixel") == 0) {
142: my_setpixel();
143: }else if (strcmp(func->s, "moveto") == 0) {
144: my_moveto();
145: }else if (strcmp(func->s, "lineto") == 0) {
146: my_lineto();
147: }else if (strcmp(func->s, "clear") == 0) {
148: my_clear();
149: }else {
150: push(make_error2(0));
151: return -1;
152: }
153: }
154:
155:
156: int receive_and_execute_sm_command()
157: {
158: int code = receive_int32(fd_rw);
159: switch(code) {
160: case SM_popCMO:
161: sm_popCMO();
162: break;
163: case SM_executeFunction:
164: sm_executeFunction();
165: break;
166: case SM_mathcap:
167: sm_mathcap();
168: break;
169: case SM_setMathCap:
170: pop();
171: break;
172: default:
173: }
174: }
175:
176: int receive()
177: {
178: int tag;
179:
180: tag = receive_ox_tag(fd_rw);
181: switch(tag) {
182: case OX_DATA:
183: push(receive_cmo(fd_rw));
184: break;
185: case OX_COMMAND:
186: receive_and_execute_sm_command();
187: break;
188: default:
189: }
190: return 0;
191: }
192:
193: int main()
194: {
195: initialize_stack();
196: decideByteOrderServer(fd_rw, 0);
197:
198: gopen();
199: while(1) {
200: receive();
201: gFlush();
202: }
203: gclose();
204: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>