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