Annotation of OpenXM_contrib2/asir2018/parse/util.c, Revision 1.2
1.1 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.2 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/parse/util.c,v 1.1 2018/09/19 05:45:08 noro Exp $
1.1 noro 49: */
50: #include "ca.h"
51: #include "base.h"
52: #include "parse.h"
53: #if defined(__GNUC__) || defined(VISUAL) || defined(__MINGW32__) || (defined(__MACH__) && defined(__ppc__)) || defined(__FreeBSD__)
54: #include <stdarg.h>
55: #else
56: #include <varargs.h>
57: #endif
58: #include <ctype.h>
59:
60: int length(NODE n)
61: {
62: int i;
63:
64: for ( i = 0; n; n = NEXT(n), i++ );
65: return i;
66: }
67:
68: int argc(NODE a)
69: {
70: int i;
71:
72: for ( i = 0; a; i++, a = NEXT(a) );
73: return ( i );
74: }
75:
76: void stoarg(char *s,int *acp,char **av)
77: {
78: int i;
79:
80: for ( i = 0; ; i++ ) {
81: for ( ; *s && isspace((unsigned char)*s); s++ );
82: if ( *s ) {
83: for ( av[i] = s; *s &&
84: !isspace((unsigned char)*s); s++ );
85: if ( *s )
86: *s++ = 0;
87: } else {
88: *acp = i;
89: return;
90: }
91: }
92: }
93:
94: #if 0
95: unsigned int ator(unsigned int addr,Obj *r)
96: {
97: N n;
98: Q q;
99:
100: if ( !addr )
101: q = 0;
102: if ( addr < BASE )
1.2 ! noro 103: STOZ(addr,q);
1.1 noro 104: else {
105: n = NALLOC(2); PL(n) = 2;
106: BD(n)[0] = addr-BASE; BD(n)[1] = addr>>BSH; NTOQ(n,1,q);
107: }
108: *r = (Obj)q;
109: }
110: #endif
111:
112: void getarray(pointer a,NODE ind,pointer *vp)
113: {
114: Obj len,row,col,trg;
115: int i;
116: NODE n,n0;
117: VECT v;
118: Z q;
119:
120: if ( a && OID(a) == O_BYTEARRAY ) {
121: len = (Obj)BDY(ind);
122: if ( !rangecheck(len,((BYTEARRAY)a)->len) )
123: error("getarray : Out of range");
124: else if ( NEXT(ind) )
125: error("getarray : invalid index");
126: else {
1.2 ! noro 127: i = (int)BDY((BYTEARRAY)a)[ZTOS((Q)len)];
! 128: STOZ(i,q);
1.1 noro 129: *vp = (pointer)q;
130: }
131: return;
132: }
133: for ( ; ind; ind = NEXT(ind) ) {
134: if ( !a )
135: error("getarray : array or list expected");
136: switch ( OID(a) ) {
137: case O_VECT:
138: len = (Obj)BDY(ind);
139: if ( !rangecheck(len,((VECT)a)->len) )
140: error("getarray : Out of range");
141: else
1.2 ! noro 142: a = (pointer)(BDY((VECT)a)[ZTOS((Q)len)]);
1.1 noro 143: break;
144: case O_MAT:
145: row = (Obj)BDY(ind);
146: if ( !rangecheck(row,((MAT)a)->row) )
147: error("getarray : Out of range");
148: else if ( NEXT(ind) ) {
149: ind = NEXT(ind);
150: col = (Obj)BDY(ind);
151: if ( !rangecheck(col,((MAT)a)->col) )
152: error("getarray : Out of range");
153: else
1.2 ! noro 154: a = (pointer)(BDY((MAT)a)[ZTOS((Q)row)][ZTOS((Q)col)]);
1.1 noro 155: } else {
156: NEWVECT(v); v->len = ((MAT)a)->col;
1.2 ! noro 157: v->body = (pointer *)BDY((MAT)a)[ZTOS((Q)row)];
1.1 noro 158: a = (pointer)v;
159: }
160: break;
161: case O_IMAT:
162: row = (Obj)BDY(ind);
163: ind = NEXT(ind);
164: col = (Obj)BDY(ind);
1.2 ! noro 165: if ( ((IMAT)a)->row < ZTOS((Q)row) ||
! 166: ((IMAT)a)->col < ZTOS((Q)col) ||
! 167: (ZTOS((Q)row) < 0) || (ZTOS((Q)col) < 0))
1.1 noro 168: error("putarray : Out of range");
1.2 ! noro 169: GetIbody((IMAT)a, ZTOS((Q)row), ZTOS((Q)col), (Obj*)&trg);
1.1 noro 170: a = (pointer)trg;
171: break;
172: case O_LIST:
1.2 ! noro 173: n0 = BDY((LIST)a); i = ZTOS((Q)BDY(ind));
1.1 noro 174: if ( i < 0 )
175: error("getarray : Out of range");
176: for ( n = n0; i > 0 && n; n = NEXT(n), i-- );
177: if ( i || !n )
178: error("getarray : Out of range");
179: else
180: a = (pointer)BDY(n);
181: break;
182: default:
183: error("getarray : array or list expected");
184: break;
185: }
186: }
187: *vp = a;
188: }
189:
190: void putarray(pointer a,NODE ind,pointer b)
191: {
192: Obj len,row,col;
193: int i;
194: NODE n,n0;
195:
196: if ( a && OID(a) == O_BYTEARRAY ) {
197: len = (Obj)BDY(ind);
198: if ( !rangecheck(len,((BYTEARRAY)a)->len) )
199: error("putarray : Out of range");
200: else if ( NEXT(ind) )
201: error("putarray : invalid index");
202: else
1.2 ! noro 203: BDY((BYTEARRAY)a)[ZTOS((Q)len)] = (unsigned char)ZTOS((Q)b);
1.1 noro 204: return;
205: }
206: for ( ; ind; ind = NEXT(ind) ) {
207: if ( !a )
208: error("putarray : array expected");
209: switch ( OID(a) ) {
210: case O_VECT:
211: len = (Obj)BDY(ind);
212: if ( !rangecheck(len,((VECT)a)->len) )
213: error("putarray : Out of range");
214: else if ( NEXT(ind) )
1.2 ! noro 215: a = BDY((VECT)a)[ZTOS((Q)len)];
1.1 noro 216: else
1.2 ! noro 217: BDY((VECT)a)[ZTOS((Q)len)] = b;
1.1 noro 218: break;
219: case O_MAT:
220: row = (Obj)BDY(ind);
221: if ( !rangecheck(row,((MAT)a)->row) )
222: error("putarray : Out of range");
223: else if ( NEXT(ind) ) {
224: ind = NEXT(ind);
225: col = (Obj)BDY(ind);
226: if ( !rangecheck(col,((MAT)a)->col) )
227: error("putarray : Out of range");
228: else if ( NEXT(ind) )
1.2 ! noro 229: a = BDY((MAT)a)[ZTOS((Q)row)][ZTOS((Q)col)];
1.1 noro 230: else
1.2 ! noro 231: BDY((MAT)a)[ZTOS((Q)row)][ZTOS((Q)col)] = b;
1.1 noro 232: } else
233: error("putarray : invalid assignment");
234: break;
235: case O_IMAT:
236: row = (Obj)BDY(ind);
237: ind = NEXT(ind);
238: col = (Obj)BDY(ind);
1.2 ! noro 239: if ( ((IMAT)a)->row < ZTOS((Q)row) ||
! 240: ((IMAT)a)->col < ZTOS((Q)col) ||
! 241: (ZTOS((Q)row) < 0) || (ZTOS((Q)col) < 0))
1.1 noro 242: error("putarray : Out of range");
1.2 ! noro 243: PutIent((IMAT)a, ZTOS((Q)row), ZTOS((Q)col), (Obj)b);
1.1 noro 244: break;
245: case O_LIST:
246: if ( NEXT(ind) ) {
1.2 ! noro 247: n0 = BDY((LIST)a); i = ZTOS((Q)BDY(ind));
1.1 noro 248: if ( i < 0 )
249: error("putarray : Out of range");
250: for ( n = n0; i > 0 && n; n = NEXT(n), i-- );
251: if ( i || !n )
252: error("puarray : Out of range");
253: else
254: a = (pointer)BDY(n);
255: } else
256: error("putarray : invalid assignment");
257: break;
258: default:
259: error("putarray : array expected");
260: break;
261: }
262: }
263: }
264:
265: int rangecheck(Obj a,int n)
266: {
267: Z z;
268:
269: if ( !a )
270: return 1;
271: if ( OID(a) != O_N || !RATN(a) || !INT(a) || sgnq((Q)a) < 0 )
272: return 0;
1.2 ! noro 273: STOZ(n,z);
1.1 noro 274: if ( cmpz((Z)a,z) >= 0 ) return 0;
275: else return 1;
276: }
277:
278: int zp(P p)
279: {
280: int r;
281: DCP dc;
282:
283: if ( !p )
284: r = 1;
285: else if ( NUM(p) )
286: r = INT((Q)p)?1:0;
287: else
288: for ( dc = DC(p), r = 1; dc && r; dc = NEXT(dc) )
289: r &= zp(COEF(dc));
290: return ( r );
291: }
292:
293: #if defined(__GNUC__) || defined(VISUAL) || defined(__MINGW32__) || (defined(__MACH__) && defined(__ppc__)) || defined(__FreeBSD__)
294: NODE mknode(int ac,...)
295: {
296: va_list ap;
297: int i;
298: NODE n0,n;
299:
300: va_start(ap,ac);
301: for ( i = 0, n0 = 0; i < ac; i++ ) {
302: NEXTNODE(n0,n);
303: BDY(n)=va_arg(ap, pointer);
304: }
305: va_end(ap);
306: if ( n0 )
307: NEXT(n)=0;
308: return n0;
309: }
310:
311: FNODE mkfnode(int ac,fid id,...)
312: {
313: va_list ap;
314: int i;
315: FNODE r;
316:
317: va_start(ap,id);
318: NEWFNODE(r,ac); ID(r) = id;
319: for ( i = 0; i < ac; i++ )
320: r->arg[i] = va_arg(ap, pointer);
321: va_end(ap);
322: return r;
323: }
324:
325: SNODE mksnode(int ac,sid id,...)
326: {
327: va_list ap;
328: int i;
329: SNODE r;
330:
331: va_start(ap,id);
332: NEWSNODE(r,ac); ID(r) = id;
333: for ( i = 0; i < ac; i++ )
334: r->arg[i] = va_arg(ap, pointer);
335: va_end(ap);
336: return r;
337: }
338: #else
339: NODE mknode(va_alist)
340: va_dcl
341: {
342: va_list ap;
343: int ac,i;
344: NODE n0,n;
345:
346: va_start(ap);
347: ac = va_arg(ap,int);
348: for ( i = 0, n0 = 0; i < ac; i++ ) {
349: NEXTNODE(n0,n);
350: BDY(n)=va_arg(ap, pointer);
351: }
352: va_end(ap);
353: if ( n0 )
354: NEXT(n)=0;
355: return n0;
356: }
357:
358: FNODE mkfnode(va_alist)
359: va_dcl
360: {
361: va_list ap;
362: int ac,i;
363: FNODE r;
364:
365: va_start(ap);
366: ac = va_arg(ap, int);
367: NEWFNODE(r,ac); ID(r) = va_arg(ap, fid);
368: for ( i = 0; i < ac; i++ )
369: r->arg[i] = va_arg(ap, pointer);
370: va_end(ap);
371: return r;
372: }
373:
374: SNODE mksnode(va_alist)
375: va_dcl
376: {
377: va_list ap;
378: int ac,i;
379: SNODE r;
380:
381: va_start(ap);
382: ac = va_arg(ap, int);
383: NEWSNODE(r,ac); ID(r) = va_arg(ap, sid);
384: for ( i = 0; i < ac; i++ )
385: r->arg[i] = va_arg(ap, pointer);
386: va_end(ap);
387: return r;
388: }
389: #endif
390:
391: V searchvar(char *str)
392: {
393: VL vl;
394:
395: for ( vl = CO; vl ;vl = NEXT(vl) )
396: if ( NAME(VR(vl)) && !strcmp(NAME(VR(vl)),str) )
397: return VR(vl);
398: return 0;
399: }
400:
401: void update_LASTCO()
402: {
403: VL vl;
404:
405: for ( vl = CO; NEXT(vl); vl = NEXT(vl) );
406: LASTCO = vl;
407: }
408:
409: void makevar(char *str,P *p)
410: {
411: VL vl;
412: V v;
413: P t;
414:
415: for ( vl = CO; ; )
416: if ( NAME(VR(vl)) && !strcmp(NAME(VR(vl)),str) ) {
417: MKV(VR(vl),t); *p = t;
418: return;
419: } else if ( !NEXT(vl) ) {
420: NEWV(v); v->attr = (pointer)V_IND;
421: NAME(v) = (char *)CALLOC(strlen(str)+1,sizeof(char));
422: strcpy(NAME(v),str);
423: NEWVL(NEXT(vl)); VR(NEXT(vl)) = v; NEXT(NEXT(vl)) = 0;
424: MKV(v,t); *p = t;
425: LASTCO = NEXT(vl);
426: return;
427: } else
428: vl = NEXT(vl);
429: }
430:
431: void makesrvar(FUNC f,P *p)
432: {
433: VL vl;
434: V v;
435: P t;
436: char *str;
437:
438: for ( vl = CO, str = f->fullname; ; )
439: if ( NAME(VR(vl)) && !strcmp(NAME(VR(vl)),str) ) {
440: VR(vl)->attr = (pointer)V_SR; VR(vl)->priv = (pointer)f;
441: MKV(VR(vl),t); *p = t;
442: return;
443: } else if ( !NEXT(vl) ) {
444: NEWV(v); v->attr = (pointer)V_SR; v->priv = (pointer)f;
445: NAME(v) = (char *)CALLOC(strlen(str)+1,sizeof(char));
446: strcpy(NAME(v),str);
447: NEWVL(NEXT(vl)); VR(NEXT(vl)) = v; NEXT(NEXT(vl)) = 0;
448: LASTCO = NEXT(vl);
449: MKV(v,t); *p = t;
450: return;
451: } else
452: vl = NEXT(vl);
453: }
454:
455: void appendtonode(NODE n,pointer a,NODE *nrp)
456: {
457: NODE tn;
458:
459: if ( !n )
460: MKNODE(*nrp,a,0);
461: else {
462: for ( tn = n; NEXT(tn); tn = NEXT(tn) );
463: MKNODE(NEXT(tn),a,0); *nrp = n;
464: }
465: }
466:
467: void appendtonode2(NODE2 n,pointer a,pointer b,NODE2 *nrp)
468: {
469: NODE2 tn;
470:
471: if ( !n )
472: MKNODE2(*nrp,a,b,0);
473: else {
474: for ( tn = n; NEXT(tn); tn = NEXT(tn) );
475: MKNODE2(NEXT(tn),a,b,0); *nrp = n;
476: }
477: }
478:
479: void appendvar(VL vl,V v)
480: {
481: while (1)
482: if ( vl->v == v )
483: return;
484: else if ( !NEXT(vl) ) {
485: NEWVL(NEXT(vl)); VR(NEXT(vl)) = v; NEXT(NEXT(vl)) = 0;
486: LASTCO = NEXT(vl);
487: return;
488: } else
489: vl = NEXT(vl);
490: }
491:
492: void asir_reallocarray(char **arrayp,int *sizep,int *indexp,int esize)
493: {
494: char *new;
495:
496: if ( *arrayp ) {
497: *sizep *= 2;
498: new = (char *)MALLOC((*sizep)*esize);
499: bcopy(*arrayp,new,*indexp*esize);
500: *arrayp = new;
501: } else {
502: *sizep = DEFSIZE; *indexp = 0;
503: new = (char *)MALLOC((*sizep)*esize);
504: bzero(new,DEFSIZE*esize);
505: *arrayp = new;
506: }
507: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>