Annotation of OpenXM_contrib2/asir2000/io/io_win_mini.c, Revision 1.2
1.2 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/io/io_win_mini.c,v 1.1 2006/09/26 05:35:26 noro Exp $ */
1.1 noro 2:
3: #include "ca.h"
4: #include "parse.h"
5: #include "ox.h"
6: #include "wsio.h"
7: #include "signal.h"
8:
9: #define ISIZ sizeof(int)
10:
1.2 ! noro 11: #define _NEWNODE(n) ((n)=(NODE)malloc(sizeof(struct oNODE)))
1.1 noro 12: #define _NEWSTR(l) ((l)=(STRING)malloc(sizeof(struct oSTRING)),OID(l)=O_STR)
13: #define _NEWUSINT(u) ((u)=(USINT)malloc(sizeof(struct oUSINT)),OID(u)=O_USINT)
14: #define _NEWERR(e) ((e)=(ERR)malloc(sizeof(struct oERR)),OID(e)=O_ERR)
1.2 ! noro 15: #define _NEWLIST(l) ((l)=(LIST)malloc(sizeof(struct oLIST)),OID(l)=O_LIST)
1.1 noro 16: #define _MKSTR(u,b) (_NEWSTR(u),(u)->body=(unsigned)(b))
17: #define _MKUSINT(u,b) (_NEWUSINT(u),(u)->body=(unsigned)(b))
18: #define _MKERR(e,b) (_NEWERR(e),(e)->body=(Obj)(b))
1.2 ! noro 19: #define _MKLIST(l,b) (_NEWLIST(l),(l)->body=(Obj)(b))
! 20: #define _MKNODE(a,b,c) \
! 21: (_NEWNODE(a),(a)->body=(pointer)b,NEXT(a)=(NODE)(c))
1.1 noro 22:
23: int ox_usr1_sent, ox_int_received, critical_when_signal;
24: unsigned int ox_serial;
25: int ox_flushing;
26: int ox_batch;
27: int ox_check=1;
28: int ox_exchange_mathcap=0;
29: int little_endian=1;
30: int terminate;
31:
32: static int available_cmo[] = {
1.2 ! noro 33: CMO_NULL, CMO_INT32, CMO_STRING,CMO_LIST,
1.1 noro 34: CMO_ERROR, CMO_ERROR2, CMO_ZERO,
35: 0
36: };
37:
38: int ox_data_is_available(int s)
39: {
40: return FP_DATA_IS_AVAILABLE(iofp[s].in);
41: }
42:
43: void wait_for_data(int s)
44: {
45: fd_set r;
46: int sock;
47:
48: if ( !FP_DATA_IS_AVAILABLE(iofp[s].in) ) {
49: sock = iofp[s].in->fildes;
50: FD_ZERO(&r);
51: FD_SET((unsigned int)sock,&r);
52: select(0,&r,NULL,NULL,NULL);
53: }
54: }
55:
56: void ox_send_data(int s,pointer p)
57: {
58: ERR err;
59:
60: if ( !valid_as_cmo(p) )
61: return;
62: ox_write_int(s,OX_DATA);
63: ox_write_int(s,ox_serial++);
64: ox_write_cmo(s,p);
65: ox_flush_stream(s);
66: }
67:
68: void ox_send_cmd(int s,int id)
69: {
70: ox_write_int(s,OX_COMMAND);
71: ox_write_int(s,ox_serial++);
72: ox_write_int(s,id);
73: ox_flush_stream(s);
74: }
75:
76: void ox_send_sync(int s)
77: {
78: ox_write_int(s,OX_SYNC_BALL);
79: ox_write_int(s,ox_serial++);
80: ox_flush_stream(s);
81: }
82:
83: unsigned int ox_recv(int s, int *id, Obj *p)
84: {
85: unsigned int cmd,serial;
86: USINT ui;
87:
88: wait_for_data(s);
89: if ( terminate ) return -1;
90: ox_read_int(s,id);
91: ox_read_int(s,&serial);
92: switch ( *id ) {
93: case OX_COMMAND:
94: ox_read_int(s,&cmd);
95: _MKUSINT(ui,cmd);
96: *p = (Obj)ui;
97: break;
98: case OX_DATA:
99: ox_read_cmo(s,p);
100: break;
101: default:
102: *p = 0;
103: break;
104: }
105: return serial;
106: }
107:
108: void ox_get_result(int s,Obj *rp)
109: {
110: int id;
111: Obj obj,r;
112: int level;
113:
114: level = 0;
115: r = 0;
116: do {
117: ox_recv(s,&id,&obj);
118: if ( id == OX_COMMAND ) {
119: switch ( ((USINT)obj)->body ) {
120: case SM_beginBlock:
121: level++;
122: break;
123: case SM_endBlock:
124: level--;
125: }
126: } else
127: r = obj;
128: } while ( level );
129: *rp = r;
130: }
131:
132: void ox_read_int(int s, int *n)
133: {
134: read_int((FILE *)iofp[s].in,n);
135: }
136:
137: void ox_read_cmo(int s, Obj *rp)
138: {
139: read_cmo((FILE *)iofp[s].in,rp);
140: }
141:
142: void ox_write_int(int s, int n)
143: {
144: write_int((FILE *)iofp[s].out,&n);
145: }
146:
147: void ox_write_cmo(int s, Obj obj)
148: {
149: write_cmo((FILE *)iofp[s].out,obj);
150: }
151:
152: void ox_flush_stream(int s)
153: {
154: if ( ox_batch )
155: return;
156: if ( _fileno(&iofp[s].out->fp) < 0 )
157: cflush(iofp[s].out);
158: else
159: fflush((FILE *)iofp[s].out);
160: }
161:
162: void ox_flush_stream_force(int s)
163: {
164: if ( _fileno(&iofp[s].out->fp) < 0 )
165: cflush(iofp[s].out);
166: else
167: fflush((FILE *)iofp[s].out);
168: }
169:
170: /* CMO I/O functions */
171:
172: int valid_as_cmo(Obj obj)
173: {
174: NODE m;
175:
176: if ( !obj )
177: return 1;
178: switch ( OID(obj) ) {
179: case O_STR: case O_ERR: case O_USINT:
180: return 1;
181: default:
182: return 0;
183: }
184: }
185:
186: void write_cmo(FILE *s,Obj obj)
187: {
188: int r;
189: char errmsg[BUFSIZ];
190:
191: if ( !obj ) {
192: r = CMO_NULL; write_int(s,&r);
193: return;
194: }
195: switch ( OID(obj) ) {
196: case O_STR:
197: write_cmo_string(s,(STRING)obj);
198: break;
199: case O_USINT:
200: write_cmo_uint(s,(USINT)obj);
201: break;
1.2 ! noro 202: case O_LIST:
! 203: write_cmo_list(s,(LIST)obj);
! 204: break;
1.1 noro 205: case O_ERR:
206: write_cmo_error(s,(ERR)obj);
207: break;
208: }
209: }
210:
211: int cmo_tag(Obj obj,int *tag)
212: {
213: if ( !obj ) {
214: *tag = CMO_NULL;
215: return 1;
216: }
217: switch ( OID(obj) ) {
218: case O_STR:
219: *tag = CMO_STRING; break;
220: case O_USINT:
221: *tag = CMO_INT32; break;
1.2 ! noro 222: case O_LIST:
! 223: *tag = CMO_LIST; break;
1.1 noro 224: case O_ERR:
225: *tag = CMO_ERROR2; break;
226: default:
227: return 0;
228: }
229: return 1;
230: }
231:
232: void write_cmo_uint(FILE *s,USINT ui)
233: {
234: unsigned int r;
235:
236: r = CMO_INT32; write_int(s,&r);
237: r = ui->body; write_int(s,&r);
238: }
239:
240: void write_cmo_string(FILE *s,STRING str)
241: {
242: int r;
243: int size;
244: char *p;
245:
246: r = CMO_STRING; write_int(s,&r);
247: p = BDY(str);
248: size = p ? strlen(p) : 0;
249: write_int(s,&size);
250: if ( size )
251: write_string(s,p,size);
252: }
253:
1.2 ! noro 254: void write_cmo_list(FILE *s,LIST list)
! 255: {
! 256: NODE m;
! 257: int i,n,r;
! 258:
! 259: for ( n = 0, m = BDY(list); m; m = NEXT(m), n++ );
! 260: r = CMO_LIST; write_int(s,&r);
! 261: write_int(s,&n);
! 262: for ( i = 0, m = BDY(list); i < n; i++, m = NEXT(m) )
! 263: write_cmo(s,BDY(m));
! 264: }
! 265:
1.1 noro 266: void write_cmo_error(FILE *s,ERR e)
267: {
268: int r;
269:
270: r = CMO_ERROR2; write_int(s,&r);
271: write_cmo(s,BDY(e));
272: }
273:
274: void read_cmo(FILE *s,Obj *rp)
275: {
276: int id;
277: STRING str;
1.2 ! noro 278: LIST list;
1.1 noro 279: USINT t;
280: Obj obj;
281: ERR e;
282:
283: read_int(s,&id);
284: switch ( id ) {
285: /* level 0 objects */
286: case CMO_NULL:
287: *rp = 0;
288: break;
289: case CMO_INT32:
290: read_cmo_uint(s,&t); *rp = (Obj)t;
291: break;
292: case CMO_STRING:
293: loadstring(s,&str); *rp = (Obj)str;
294: break;
1.2 ! noro 295: case CMO_LIST:
! 296: read_cmo_list(s,&list); *rp = (Obj)list;
! 297: break;
1.1 noro 298: case CMO_ERROR:
299: _MKERR(e,0); *rp = (Obj)e;
300: break;
301: case CMO_ERROR2:
302: read_cmo(s,&obj); _MKERR(e,obj); *rp = (Obj)e;
303: break;
304: case CMO_ZERO:
305: *rp = 0;
306: break;
307: default:
308: _MKUSINT(t,id);
309: t->id = O_VOID;
310: *rp = (Obj)t;
311: break;
312: }
313: }
314:
315: void read_cmo_uint(FILE *s,USINT *rp)
316: {
317: unsigned int body;
318:
319: read_int(s,&body);
320: _MKUSINT(*rp,body);
321: }
322:
1.2 ! noro 323: void read_cmo_list(FILE *s,Obj *rp)
! 324: {
! 325: int len;
! 326: Obj *w;
! 327: int i;
! 328: NODE n0,n1;
! 329: LIST list;
! 330:
! 331: read_int(s,&len);
! 332: w = (Obj *)ALLOCA(len*sizeof(Obj));
! 333: for ( i = 0; i < len; i++ )
! 334: read_cmo(s,&w[i]);
! 335: for ( i = len-1, n0 = 0; i >= 0; i-- ) {
! 336: _MKNODE(n1,w[i],n0); n0 = n1;
! 337: }
! 338: _MKLIST(list,n0);
! 339: *rp = (Obj)list;
! 340: }
! 341:
1.1 noro 342: void loadstring(FILE *s,STRING *p)
343: {
344: char *t;
345:
346: loadstr(s,&t); _MKSTR(*p,t);
347: }
348:
349: void loadstr(FILE *s,char **p)
350: {
351: int len;
352: char *t;
353:
354: read_int(s,&len);
355: if ( len ) {
356: t = (char *)malloc(len+1); read_string(s,t,len); t[len] = 0;
357: } else
358: t = "";
359: *p = t;
360: }
361:
362: /* low level buffered I/O functions */
363:
364: int gen_fread (char *ptr,int size,int nitems,FILE *stream)
365: {
366: int n;
367:
368: if ( _fileno(stream) < 0 )
369: n = cread(ptr,size,nitems,(STREAM *)stream);
370: else
371: n = fread(ptr,size,nitems,stream);
372: if ( !n )
373: return 0;
374: else
375: return n;
376: }
377:
378: int gen_fwrite (char *ptr,int size,int nitems,FILE *stream)
379: {
380: if ( _fileno(stream) < 0 )
381: return cwrite(ptr,size,nitems,(STREAM *)stream);
382: else
383: return fwrite(ptr,size,nitems,stream);
384: }
385:
386: void write_char(FILE *f,unsigned char *p)
387: {
388: gen_fwrite(p,sizeof(unsigned char),1,f);
389: }
390:
391: void write_short(FILE *f,unsigned short *p)
392: {
393: gen_fwrite((char *)p,sizeof(unsigned short),1,f);
394: }
395:
396: void write_int(FILE *f,unsigned int *p)
397: {
398: gen_fwrite((char *)p,sizeof(unsigned int),1,f);
399: }
400:
401: void write_string(FILE *f,unsigned char *p,int l)
402: {
403: gen_fwrite(p,sizeof(unsigned char),l,f);
404: }
405:
406: void read_char(FILE *f,unsigned char *p)
407: {
408: gen_fread((char *)p,sizeof(unsigned char),1,f);
409: }
410:
411: void read_short(FILE *f,unsigned short *p)
412: {
413: gen_fread((char *)p,sizeof(unsigned short),1,f);
414: }
415:
416: void read_int(FILE *f,unsigned int *p)
417: {
418: gen_fread((char *)p,sizeof(unsigned int),1,f);
419: }
420:
421: void read_string(FILE *f,unsigned char *p,int l)
422: {
423: gen_fread((char *)p,sizeof(unsigned char),l,f);
424: }
425:
426: /* dummy functions, alternative functions */
427:
428: double get_current_time()
429: {
430: return GetTickCount()/1000.0;
431: }
432:
433: void shutdown_all(){}
434:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>