/[ascend]/trunk/base/disused/compiler/interface.c
ViewVC logotype

Contents of /trunk/base/disused/compiler/interface.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1002 - (show annotations) (download) (as text)
Sat Dec 30 14:27:47 2006 UTC (13 years, 11 months ago) by johnpye
File MIME type: text/x-csrc
File size: 84204 byte(s)
Some work on fixing error with test_ascSignal.
Breaking down into smaller test cases.
Removed some debug output from detection of ASC_RESETNEEDED.
Changed all calls 'signal' to 'SIGNAL' macro that includes optional debug output.
Removed 'libasctest.so' (made part of libasctestsuite.so FWIW)
Fixed big in test.c wrt CUEA_ABORT.
Added 'print_stack' and 'Asc_SignalPrintStack' and 'Asc_SignalStackLength'.

1 /* ex: set ts=2 : */
2 /*
3 * Interface Implementation
4 * by Tom Epperly
5 * Created: 1/17/90
6 * Version: $Revision: 1.53 $
7 * Version control file: $RCSfile: interface.c,v $
8 * Date last modified: $Date: 1998/06/17 15:33:21 $
9 * Last modified by: $Author: mthomas $
10 *
11 * This file is part of the Ascend Language Interpreter.
12 *
13 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
14 *
15 * The Ascend Language Interpreter is free software; you can redistribute
16 * it and/or modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version.
19 *
20 * The Ascend Language Interpreter is distributed in hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with the program; if not, write to the Free Software Foundation,
27 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
28 * COPYING.
29 *
30 */
31
32 #include <signal.h>
33 #include <math.h>
34 #include <time.h>
35 #include <ctype.h>
36 #include <stdarg.h>
37 #if (defined(__alpha) || defined(sun))
38 #include <malloc.h>
39 #endif
40 #include <utilities/ascConfig.h>
41 #include <utilities/ascMalloc.h>
42 #include <utilities/ascPanic.h>
43 #include <utilities/ascSignal.h>
44 #include <general/pool.h>
45 #include <general/list.h>
46 #include <general/dstring.h>
47 #include "compiler.h"
48 #include "symtab.h"
49 #include "notate.h"
50 #include "braced.h"
51 #include "ascCompiler.h"
52 #include "commands.h"
53 #include "termsetup.h"
54 #include "fractions.h"
55 #include "dimen.h"
56 #include "functype.h"
57 #include "types.h"
58 #include "instance_enum.h"
59 #include "cmpfunc.h"
60 #include "visitinst.h"
61 #include "name.h"
62 #include "bit.h"
63 #include "instance_io.h"
64 #include "module.h"
65 #include "library.h"
66 #include "prototype.h"
67 #include "child.h"
68 #include "type_desc.h"
69 #include "type_descio.h"
70 #include "name.h" /* these three included to do */
71 #include "stattypes.h"
72 #include "statement.h" /* Interactive processing */
73 #include "vlist.h"
74 #include "vlistio.h"
75 #include "slist.h"
76 #include "statio.h"
77 #include "setinstval.h"
78 #include "extinst.h"
79 #include "arrayinst.h"
80 #include "instquery.h"
81 #include "copyinst.h"
82 #include "parentchild.h"
83 #include "mergeinst.h"
84 #include "refineinst.h"
85 #include "mathinst.h"
86 #include "atomvalue.h"
87 #include "destroyinst.h"
88 #include "instance_name.h"
89 #include "instantiate.h"
90 #include "value_type.h"
91 #include "watchpt.h"
92 #include "proc.h"
93 #include "initialize.h"
94 #include "check.h"
95 #include "pending.h"
96 #include "license.h"
97 #include "extfunc.h"
98 #include "packages.h"
99 #include "find.h"
100 #include "relation_type.h"
101 #include "relation.h"
102 #include "relation_util.h"
103 #include "relation_io.h"
104 #include "logical_relation.h"
105 #include "logrelation.h"
106 #include "logrel_util.h"
107 #include "logrel_io.h"
108 #include "setinstval.h"
109 #include "syntax.h"
110 #include "anontype.h"
111 #include "actype.h" /* required for isidchar() */
112 #include "simlist.h"
113 #include "tmpnum.h"
114 #include "bintoken.h"
115 #include "interface.h"
116
117 #ifndef lint
118 static CONST char InterfaceRCSid[]="$Id: interface.c,v 1.53 1998/06/17 15:33:21 mthomas Exp $";
119 #endif /* lint */
120
121 extern int zz_parse();
122
123 /* character definitions */
124 #define ASC_FLUSH '\025' /* control-U */
125 #define ASC_BS '\010' /* backspace */
126 #define ASC_DELETE '\177' /* delete */
127 #define ASC_NEWLINE '\n' /* newline */
128 #define ASC_CLEAR '\f' /* formfeed clear screen */
129
130 #define DEFMETHOD "default_self"
131 #define MAXID 256
132 #define MAXCOMMANDLINE 2048
133 #define PROMPT "Ascend>"
134 #define STARTUP "Welcome to ASCEND!\nPress '?' for a command summary.\n"
135 #define SHUTDOWN "So long!\n"
136 #define LEAVEHELP "exit the ASCEND system"
137
138 /* type definitions */
139
140 union argument {
141 CONST char *id;
142 struct Instance *i;
143 struct value_t value;
144 struct TypeDescription *desc;
145 };
146
147 /* global variables */
148 struct gl_list_t *g_def_list = NULL; /* list of definitions sorted */
149 /* alphabetically */
150 struct Instance *g_root = NULL; /* root instance */
151 struct Instance *g_search_inst = NULL; /* used for searching */
152
153 int open_bracket,open_quote;
154
155 /* fwd decls */
156 static void SetupBinTokens(void);
157
158 /*
159 * We don't have a use for this yet. We are being protractive
160 * in that any objects that we are referencing directly get
161 * cleaned up *before* we call any routines that could
162 * potentially move an instance.
163 */
164 static
165 void InterfacePtrDeleteProc(struct Instance *i, char *ptr)
166 {
167 (void)i; /* stop gcc whine about unused parameter */
168 (void)ptr; /* stop gcc whine about unused parameter */
169 }
170
171 /*
172 * No longer necessary as being handled properly in instance.c. This
173 * function was necessary before for the following reasons:
174 * Let us assume that there as a simulation sim, and it had a root instance,
175 * root. If refine was called on sim->root, and the instance was moved in
176 * memory, we would be notified by this function, and so that if:
177 * sim->root == old, then sim->root = new. This was only necessary for
178 * sims, as they used to be treated *NOT* as instances and so sim->root had
179 * 0 parents, with all the associated implications.
180 * Now that simulations are proper SIM_INSTances, their root instance has
181 * at *least* 1 parent, (possibly more if they are universal !*), and the
182 * necessary fixing up is done within the code in instance.c.
183 *
184 * For the time being it is being left to see if it gets invoked.
185 */
186 static
187 void InterfaceNotifyProc(char *ptr, struct Instance *old, struct Instance *new)
188 {
189 register unsigned long c,len;
190 register struct Instance *sptr;
191 (void)ptr;
192 (void)new;
193 len = gl_length(g_simulation_list);
194 for(c=len;c>=1;c--){
195 sptr = (struct Instance *)gl_fetch(g_simulation_list,c);
196 if (GetSimulationRoot(sptr) == old) {
197 FPRINTF(stderr,
198 "Simulation %s has been relocated for your information.\n",
199 GetSimulationName(sptr));
200 FPRINTF(stderr,"If you see this message please report to\n");
201 FPRINTF(stderr,"\tascend+bugs@cs.cmu.edu\n");
202 }
203 }
204 }
205
206 static
207 void Trap(int sig)
208 {
209 RestoreTerminal();
210 putchar('\n');
211 exit(sig);
212 }
213
214 static
215 void ResetTerminal(union argument *args,int argc)
216 {
217 (void) argc;
218 (void) args;
219 TermSetup_ResetTerminal();
220 }
221
222 static
223 void ProtoTypeInstanceCmd(union argument *args, int argc)
224 {
225 struct Instance *target, *result;
226 CONST struct TypeDescription *desc;
227 int start_time;
228
229 if (argc!=1 || args[0].i==NULL) {
230 FPRINTF(ASCERR,"Call is: proto instance\n");
231 return;
232 }
233
234 start_time = clock();
235 target = args[0].i;
236 switch (InstanceKind(target)) {
237 case ARRAY_ENUM_INST:
238 case ARRAY_INT_INST:
239 case SIM_INST:
240 FPRINTF(ASCERR,"Cannot prototype this type of instance\n");
241 return;
242 default:
243 break;
244 }
245
246 desc = InstanceTypeDesc(target);
247 if (LookupPrototype(GetName(desc))) {
248 FPRINTF(ASCERR,"A prototype already exists");
249 return;
250 }
251 result = CopyInstance(target); /* using copy by reference */
252 start_time = clock() - start_time;
253 if (result) {
254 AddPrototype(result);
255 FPRINTF(stderr,"Time to prototype instance = %d\n",start_time);
256 return;
257 }
258 else{
259 FPRINTF(ASCERR,"Error in prototyping instance");
260 return;
261 }
262 }
263
264
265 static
266 void PrintArrayTypes(union argument *args, int argc)
267 {
268 (void) argc;
269 (void) args;
270 WriteArrayTypeList(stderr);
271 }
272
273 static
274 void DumpAT(FILE *fp,struct Instance *root)
275 {
276 int start;
277 struct gl_list_t *atl;
278 start = clock();
279 atl = Asc_DeriveAnonList(root);
280 PRINTF("time to classify = %d\n",clock()-start);
281 Asc_WriteAnonList(fp,atl,root,0);
282 Asc_DestroyAnonList(atl);
283 }
284
285 static
286 void PrintAnonTypes(union argument *args, int argc)
287 {
288 if (argc==0) {
289 if (g_root!=NULL) {
290 DumpAT(stdout,g_root);
291 } else {
292 Bell();
293 PRINTF("No root instance.\n");
294 }
295 } else {
296 if (args[0].i) {
297 FILE *fp;
298 fp = fopen("/tmp/reldump.txt","w+");
299 DumpAT(fp,args[0].i);
300 fclose(fp);
301 } else {
302 PRINTF("Incorrect instance.\n");
303 }
304 }
305 }
306
307 struct twolists {
308 struct gl_list_t *rl, *il;
309 };
310 static
311 void GetRels(struct Instance *i,struct twolists *t)
312 {
313 CONST struct relation *rel;
314 enum Expr_enum reltype;
315 if (i != NULL && t != NULL && InstanceKind(i) == REL_INST) {
316 rel = GetInstanceRelation(i, &reltype);
317 if (rel==NULL || reltype != e_token) {
318 return;
319 }
320 gl_append_ptr(t->rl,(VOIDPTR)rel);
321 gl_append_ptr(t->il,(VOIDPTR)i);
322 }
323 }
324
325 extern void TimeCalcResidual(struct gl_list_t *, int);
326 static
327 void TimeResiduals(union argument *args, int argc)
328 {
329 struct twolists t;
330 struct Instance *i = NULL;
331 clock_t start,stop;
332 if (argc==0) {
333 if (g_root!=NULL) {
334 i = g_root;
335 } else {
336 Bell();
337 PRINTF("No root instance.\n");
338 }
339 } else {
340 if (args[0].i) {
341 i = args[0].i;
342 } else {
343 PRINTF("Incorrect instance.\n");
344 }
345 }
346 if (i==NULL) return;
347 t.il = gl_create(10000L);
348 t.rl = gl_create(10000L);
349 SilentVisitInstanceTreeTwo(i,(VisitTwoProc)GetRels,0,0,(VOIDPTR)&t);
350 FPRINTF(ASCERR,"Relation count: %lu\n",gl_length(t.il));
351
352 start = clock();
353 TimeCalcResidual(t.il,1);
354 stop = clock();
355 stop -= start;
356 FPRINTF(ASCERR,"PostfixSafe Time: %lu\n",(unsigned long)stop);
357
358 #if 0
359 start = clock();
360 TimeCalcResidual(t.rl,0);
361 stop = clock();
362 stop -= start;
363 FPRINTF(ASCERR,"Binary Time: %lu\n",(unsigned long)stop);
364 #endif
365
366 Asc_SignalHandlerPush(SIGFPE,SIG_IGN);
367 if (setjmp(g_fpe_env)==0) {
368 start = clock();
369 TimeCalcResidual(t.rl,0);
370 stop = clock();
371 stop -= start;
372 FPRINTF(ASCERR,"Binary-IGN Time: %lu\n",(unsigned long)stop);
373 } else {
374 FPRINTF(ASCERR,"Binary-IGN Time: SIGFPE -- REALLY_WIERD\n");
375 }
376 Asc_SignalHandlerPop(SIGFPE,SIG_IGN);
377
378 Asc_SignalHandlerPush(SIGFPE,Asc_SignalTrap);
379 if (setjmp(g_fpe_env)==0) {
380 start = clock();
381 TimeCalcResidual(t.il,2);
382 stop = clock();
383 stop -= start;
384 FPRINTF(ASCERR,"Postfix Time: %lu\n",(unsigned long)stop);
385 } else {
386 FPRINTF(ASCERR,"Postfix Time: SIGFPE\n");
387 }
388 Asc_SignalHandlerPop(SIGFPE,Asc_SignalTrap);
389
390 Asc_SignalHandlerPush(SIGFPE,Asc_SignalTrap);
391 if (setjmp(g_fpe_env)==0) {
392 start = clock();
393 TimeCalcResidual(t.il,3);
394 stop = clock();
395 stop -= start;
396 FPRINTF(ASCERR,"Infix Time: %lu\n",(unsigned long)stop);
397 } else {
398 FPRINTF(ASCERR,"Infix Time: SIGFPE\n");
399 }
400 Asc_SignalHandlerPop(SIGFPE,Asc_SignalTrap);
401
402 gl_destroy(t.rl);
403 gl_destroy(t.il);
404 }
405
406 static
407 void PrintInstanceRelations(union argument *args, int argc)
408 {
409 if (argc==0){
410 if (g_root) {
411 FILE *fp;
412 fp = fopen("/tmp/reldump.txt","w+");
413 WriteRelationsInTree(fp,g_root);
414 fclose(fp);
415 } else {
416 Bell();
417 PRINTF("No root instance.\n");
418 }
419 }
420 else{
421 if (args[0].i) {
422 FILE *fp;
423 fp = fopen("/tmp/reldump.txt","w+");
424 WriteRelationsInTree(fp,args[0].i);
425 fclose(fp);
426 } else {
427 PRINTF("Incorrect instance.\n");
428 }
429 }
430 }
431
432 static
433 void PrintUniqueRelations(union argument *args, int argc)
434 {
435 FILE *fp;
436 struct gl_list_t *list;
437 if (argc==0){
438 if (g_root) {
439 fp = fopen("/tmp/reldump.txt","w+");
440 list = CollectTokenRelationsWithUniqueBINlessShares(g_root,400);
441 WriteRelationsInList(fp,list);
442 if (list != NULL) {
443 FPRINTF(ASCERR,"%lu relations written.\n",gl_length(list));
444 gl_destroy(list);
445 }
446 fclose(fp);
447 } else {
448 Bell();
449 PRINTF("No root instance.\n");
450 }
451 } else {
452 if (args[0].i) {
453 fp = fopen("/tmp/reldump.txt","w+");
454 list = CollectTokenRelationsWithUniqueBINlessShares(g_root,400);
455 WriteRelationsInList(fp,list);
456 if (list != NULL) {
457 FPRINTF(ASCERR,"%lu relations written.\n",gl_length(list));
458 gl_destroy(list);
459 }
460 fclose(fp);
461 } else {
462 PRINTF("Incorrect instance.\n");
463 }
464 }
465 }
466
467
468 static
469 void PrintInstanceLogRelations(union argument *args, int argc)
470 {
471 if (argc==0){
472 if (g_root)
473 WriteInstance(stdout,g_root);
474 else{
475 Bell();
476 PRINTF("No root instance.\n");
477 }
478 }
479 else{
480 if (args[0].i) {
481 FILE *fp;
482 fp = fopen("/tmp/reldump.txt","w+");
483 WriteLogRelationsInTree(fp,args[0].i);
484 fclose(fp);
485 } else {
486 PRINTF("Incorrect instance.\n");
487 }
488 }
489 }
490
491 static struct Instance *g_wviinst;
492 static FILE *g_wvifp;
493 static
494 void WriteVisitIndex(struct Instance *i,unsigned long *list,int len,
495 VOIDPTR dummy)
496 {
497 int c;
498 WriteInstanceName(g_wvifp,i,g_wviinst);
499 FPRINTF(g_wvifp,":\n");
500 for ( c = 0; c < len; c++) {
501 FPRINTF(g_wvifp,"%lu,",list[c]);
502 }
503 FPRINTF(g_wvifp,"\n");
504 }
505 static
506 void PrintIndexedVisit(union argument *args, int argc)
507 {
508 if (argc==0){
509 if (g_root) {
510 WriteInstance(stdout,g_root);
511 } else{
512 Bell();
513 PRINTF("No root instance.\n");
514 }
515 } else{
516 if (args[0].i) {
517 unsigned int l = 0;
518 unsigned long *llp=NULL;
519 g_wvifp = fopen("/tmp/reldump.txt","w+");
520 IndexedVisitInstanceTree(args[0].i,WriteVisitIndex,0,0,&llp,&l,NULL);
521 FPRINTF(stderr,"llen became %d\n",l);
522 if (llp!=NULL) {
523 ascfree(llp);
524 }
525 fclose(g_wvifp);
526 } else {
527 PRINTF("Incorrect instance.\n");
528 }
529 }
530 }
531
532 static
533 void NumberTreeDOT(struct Instance *i,unsigned long *num)
534 {
535 struct Instance *ch;
536 unsigned long c,len;
537 (*num)++;
538 SetTmpNum(i,*num);
539 if (IsCompoundInstance(i)) {
540 for (c=1, len = NumberChildren(i); c <= len; c++) {
541 ch = InstanceChild(i,c);
542 if (ch != NULL) {
543 FPRINTF(g_wvifp,"%lu -> %lu\n",*num,GetTmpNum(ch));
544 }
545 }
546 }
547 }
548
549 static
550 void PrintVisitMapDOT(union argument *args, int argc)
551 {
552 if (argc==0){
553 if (g_root) {
554 WriteInstance(stdout,g_root);
555 } else {
556 Bell();
557 PRINTF("No root instance.\n");
558 }
559 } else {
560 if (args[0].i) {
561 unsigned int l;
562 unsigned long num=0;
563 g_wvifp = fopen("/tmp/reldump.txt","w+");
564 SilentVisitInstanceTreeTwo(args[0].i,(VisitTwoProc)NumberTreeDOT,
565 1,0,&num);
566 fclose(g_wvifp);
567 } else {
568 PRINTF("Incorrect instance.\n");
569 }
570 }
571 }
572
573 static
574 void NumberTree(struct Instance *i,unsigned long *num)
575 {
576 (*num)++;
577 SetTmpNum(i,*num);
578 }
579
580 static
581 void PrintVisitMap(union argument *args, int argc)
582 {
583 if (argc==0){
584 if (g_root) {
585 WriteInstance(stdout,g_root);
586 } else{
587 Bell();
588 PRINTF("No root instance.\n");
589 }
590 } else {
591 if (args[0].i) {
592 struct visitmapinfo *map;
593 unsigned int l;
594 char *ds;
595 char *realetos[3] = {"ERR!","DOWN","-UP-"};
596 char **etos;
597 unsigned long maplen=0,num=0;
598 etos = realetos+1;
599 map = MakeVisitMap(args[0].i,&maplen);
600 SilentVisitInstanceTreeTwo(args[0].i,(VisitTwoProc)NumberTree,1,0,&num);
601 FPRINTF(stderr,"maplen = %lu\n",maplen);
602 g_wvifp = fopen("/tmp/reldump.txt","w+");
603 if (map==NULL) {
604 FPRINTF(g_wvifp,"insufficient memory for map\n");
605 fclose(g_wvifp);
606 return;
607 }
608 FPRINTF(g_wvifp,"index\tcontext child\tdir'n\tparent\n");
609 for (l= 0; l <= maplen; l++) {
610 FPRINTF(g_wvifp,"%d\t%lu\t%lu\t",l,
611 (map[l].context != NULL)?GetTmpNum(map[l].context):0L,
612 map[l].child);
613 FPRINTF(g_wvifp,"%s\t%lu\t%d\n",
614 etos[map[l].dir],
615 (map[l].parent!=NULL)?GetTmpNum(map[l].parent):0L,
616 map[l].last);
617 }
618 fclose(g_wvifp);
619 ascfree(map);
620 } else {
621 PRINTF("Incorrect instance.\n");
622 }
623 }
624 }
625
626 static
627 void SystemCmd(union argument *args, int argc)
628 {
629 (void) argc;
630 if (args[0].id==NULL) return;
631 system(args[0].id);
632 ascfree((char *)args[0].id); /* this id was made my strcpy, not addsymbol */
633 }
634
635 static
636 void HideChild(union argument *args, int argc)
637 {
638 unsigned long c;
639 symchar *name;
640 if (argc==1 && g_root!=NULL){
641 name = AddSymbol(args[0].id);
642 c = ChildPos(GetChildList(InstanceTypeDesc(g_root)),name);
643 if (c) {
644 /* note this is a really crappy way to do this */
645 /* should create InstanceHideChildByName,InstanceShowChildByName */
646 /* in parentchild.h */
647 ChildHide(GetChildList(InstanceTypeDesc(g_root)),c);
648 } else {
649 Bell();
650 PRINTF("No child by name %s to hide.\n",args[0].id);
651 }
652 } else {
653 Bell();
654 PRINTF("No instance name to hide.\n");
655 }
656 }
657
658 static
659 void ShowChildren(union argument *args, int argc)
660 {
661 unsigned long c, len;
662 (void) args;
663 (void) argc;
664 if (g_root!=NULL){
665 len = NumberChildren(g_root);
666 for (c = 1; c <=len; c++) {
667 /* note this is a really crappy way to do this. */
668 /* should create InstanceHideChild and InstanceShowChild(i,n) */
669 /* in parentchild.h */
670 ChildShow(GetChildList(InstanceTypeDesc(g_root)),c);
671 }
672 } else {
673 Bell();
674 PRINTF("No instance to unhide children of.\n");
675 }
676 }
677
678 static
679 void PrintInstanceReach(union argument *args, int argc)
680 {
681 struct Instance *i;
682 if (argc==0){
683 if (g_root) {
684 i = g_root;
685 } else {
686 Bell();
687 PRINTF("No root instance.\n");
688 return;
689 }
690 } else {
691 if (args[0].i) {
692 i = args[0].i;
693 } else {
694 PRINTF("Incorrect instance.\n");
695 return;
696 }
697 }
698 /*
699 *AnonWriteMergeReachable(i);
700 */
701 }
702
703 static
704 void TestLeaf(struct Instance *i)
705 {
706 fprintf(ASCERR,"\n");
707 WriteAliases(ASCERR,i);
708 fprintf(ASCERR,"\n");
709 }
710
711 static
712 void LeavesInstance(union argument *args, int argc)
713 {
714 struct Instance *i;
715 if (argc==0){
716 if (g_root) {
717 i = g_root;
718 } else {
719 Bell();
720 PRINTF("No root instance.\n");
721 }
722 } else {
723 if (args[0].i) {
724 i = args[0].i;
725 } else {
726 Bell();
727 PRINTF("Incorrect instance.\n");
728 }
729 }
730 if (i == NULL ||
731 (InstanceKind(i)!= ARRAY_INT_INST &&
732 InstanceKind(i)!= ARRAY_ENUM_INST)) {
733 Bell();
734 PRINTF("Incorrect nonarray instance.\n");
735 }
736 ArrayVisitLocalLeaves(i,TestLeaf);
737 }
738
739 static
740 void PrintInstance(union argument *args, int argc)
741 {
742 if (argc==0){
743 if (g_root) {
744 WriteInstance(stdout,g_root);
745 } else {
746 Bell();
747 PRINTF("No root instance.\n");
748 }
749 } else {
750 if (args[0].i) {
751 WriteInstance(stdout,args[0].i);
752 } else {
753 PRINTF("Incorrect instance.\n");
754 }
755 }
756 }
757
758 static
759 void ListParents(union argument *args, int argc)
760 {
761 unsigned c,length;
762 struct Instance *ptr;
763 if (argc==0){
764 if (g_root){
765 ptr = g_root;
766 }
767 else{
768 Bell();
769 PRINTF("No root instance.\n");
770 return;
771 }
772 }
773 else
774 ptr = args[0].i;
775 if (ptr){
776 length = NumberParents(ptr);
777 for(c=1;c<=length;c++){
778 WriteInstanceName(stdout,InstanceParent(ptr,c),NULL);
779 putchar('\n');
780 }
781 }
782 else{
783 PRINTF("Incorrect instance.\n");
784 }
785 }
786
787 static
788 void AliiInstance(union argument *args, int argc)
789 {
790 if (argc==0){
791 if (g_root) {
792 WriteAliases(stdout,g_root);
793 } else{
794 Bell();
795 PRINTF("No root instance.\n");
796 }
797 } else {
798 if (args[0].i) {
799 WriteAliases(stdout,args[0].i);
800 } else {
801 PRINTF("Incorrect instance.\n");
802 }
803 }
804 }
805
806 static
807 void IsasInstance(union argument *args, int argc)
808 {
809 if (argc==0){
810 if (g_root) {
811 WriteISAs(stdout,g_root);
812 } else{
813 Bell();
814 PRINTF("No root instance.\n");
815 }
816 } else {
817 if (args[0].i) {
818 WriteISAs(stdout,args[0].i);
819 } else {
820 PRINTF("Incorrect instance.\n");
821 }
822 }
823 }
824
825 static
826 void CliqueInstance(union argument *args, int argc)
827 {
828 if (argc==0){
829 if (g_root)
830 WriteClique(stdout,g_root);
831 else{
832 Bell();
833 PRINTF("No root instance.\n");
834 }
835 }
836 else
837 if (args[0].i)
838 WriteClique(stdout,args[0].i);
839 else
840 PRINTF("Incorrect instance.\n");
841 }
842
843 static
844 void InstanceMerge(union argument *args, int argc)
845 {
846 struct Instance *result;
847 if (argc==2){
848 if (args[0].i&&args[1].i){
849 result = MergeInstances(args[0].i,args[1].i);
850 PostMergeCheck(result);
851 }
852 else
853 PRINTF("One of the two instances is incorrect.\n");
854 }
855 else{
856 Bell();
857 PRINTF("Incorrect arguments.\n");
858 }
859 }
860
861 static
862 void RunInitialization(union argument *args, int argc)
863 {
864 struct Name *name=NULL;
865 enum Proc_enum result;
866 if (argc==2){
867 name = CreateIdName(AddSymbol(args[1].id));
868 if (args[0].i){
869 result = Initialize(args[0].i,name,"sacompiler.",ASCERR,
870 (WP_BTUIFSTOP|WP_STOPONERR),NULL,NULL);
871 if (result!=Proc_all_ok) {
872 /* error */
873 Bell();
874 PRINTF("Error executing initialization.\n");
875 }
876 } else{
877 PRINTF("Incorrect instance.\n");
878 }
879 DestroyName(name);
880 } else{
881 Bell();
882 PRINTF("Incorrect arguments. Instname, procname\n");
883 }
884 }
885
886 static
887 void MakeAlike(union argument *args, int argc)
888 {
889 struct TypeDescription *desc,*desc1,*desc2;
890 if (argc==2){
891 if (args[0].i && args[1].i){
892 desc1 = InstanceTypeDesc(args[0].i);
893 desc2 = InstanceTypeDesc(args[1].i);
894 if (desc1==desc2) MergeCliques(args[0].i,args[1].i);
895 else{
896 if ((desc = MoreRefined(desc1,desc2))){
897 if (desc == desc1) RefineClique(args[1].i,desc,NULL);
898 else RefineClique(args[0].i,desc,NULL);
899 MergeCliques(args[0].i,args[1].i);
900 }
901 else{
902 Bell();
903 PRINTF("Instances are unconformable.\n");
904 }
905 }
906 }
907 else
908 PRINTF("Incorrect instance.\n");
909 }
910 else{
911 Bell();
912 PRINTF("Incorrect arguments.\n");
913 }
914 }
915
916 static
917 void CallExternalCmd(union argument *args, int argc)
918 {
919 int result;
920 if (argc==0){
921 Bell();
922 PRINTF("Incorrect number of args to callproc\n");
923 }
924 else{
925 if (args[0].i) {
926 result = CallExternalProcs(args[0].i);
927 if (result)
928 PRINTF("Error in calling external procedure\n");
929 }
930 else
931 PRINTF("Incorrect instance or calculation error.\n");
932 }
933 }
934
935
936 static
937 void Shutdown(union argument *args, int argc)
938 {
939 (void) args;
940 (void) argc;
941 if (g_def_list) gl_destroy(g_def_list);
942 g_def_list = NULL;
943 Asc_DestroySimulations();
944 PRINTF("Done.\n");
945 }
946
947 static
948 void SetRoot(union argument *args, int argc)
949 {
950 if (argc==1){
951 g_root = args[0].i;
952 }
953 else{
954 PRINTF("Reseting root to NULL.\n");
955 g_root = NULL;
956 }
957 }
958
959 static
960 void ListInstances(union argument *args, int argc)
961 {
962 register unsigned long c,length;
963 register struct Instance *ptr;
964 struct Instance *i;
965 struct InstanceName name;
966 if (argc==0){
967 if (g_root){
968 i = g_root;
969 }
970 else{
971 length = gl_length(g_simulation_list);
972 for(c=1;c<=length;c++){
973 ptr = (struct Instance *)gl_fetch(g_simulation_list,c);
974 PRINTF("%s\n",GetSimulationName(ptr));
975 }
976 return;
977 }
978 }
979 else
980 i = args[0].i;
981 length = NumberChildren(i);
982 for(c=1;c<=length;c++){
983 name = ChildName(i,c);
984 switch(InstanceNameType(name)){
985 case IntArrayIndex:
986 PRINTF("[%ld]\n",InstanceIntIndex(name)); break;
987 case StrArrayIndex:
988 PRINTF("['%s']\n",InstanceStrIndex(name)); break;
989 case StrName:
990 PRINTF("%s\n",InstanceNameStr(name)); break;
991 }
992 }
993 }
994
995 static
996 void WriteNotes(struct gl_list_t *nl)
997 {
998 unsigned long c,len;
999 struct bracechar *bc;
1000 struct Note *n;
1001 if (nl==NULL || gl_length(nl)==0) {
1002 printf("No notes.\n");
1003 return;
1004 }
1005 printf("NOTES\n");
1006 len = gl_length(nl);
1007 for (c=1; c <= len; c++) {
1008 n = (struct Note *) gl_fetch(nl,c);
1009 printf("'%s' ",SCP(GetNoteLanguage(n)));
1010 if (GetNoteEnum(n) == nd_vlist) {
1011 WriteVariableList(stdout,GetNoteData(n,nd_vlist));
1012 printf(" \"vlist\" {\n");
1013 } else {
1014 printf("%s {\n",SCP(GetNoteId(n)));
1015 }
1016 bc = GetNoteText(n);
1017 printf("%s\n} ",BraceCharString(bc));
1018 if (GetNoteMethod(n)!=NULL) {
1019 printf("METHOD %s ",SCP(GetNoteMethod(n)));
1020 }
1021 if (GetNoteType(n)!=NULL) {
1022 printf("Type %s ",SCP(GetNoteType(n)));
1023 }
1024 printf("\n");
1025 }
1026 printf("END NOTES;\n");
1027 }
1028
1029 static
1030 void TypeNotes(union argument *args, int argc)
1031 {
1032 unsigned c,length;
1033 struct TypeDescription *desc;
1034 struct gl_list_t *nlist;
1035 if (argc==1) {
1036 nlist =
1037 GetNotes(LibraryNote(),GetName(args[0].desc),NOTESWILD,NOTESWILD,NOTESWILD,nd_empty);
1038 WriteNotes(nlist);
1039 gl_destroy(nlist);
1040 } else {
1041 Bell();
1042 PRINTF("Odd typenotes request.\n");
1043 }
1044 }
1045 static
1046 void DefineType(union argument *args, int argc)
1047 {
1048 unsigned c,length;
1049 struct TypeDescription *desc;
1050 if (argc==1) {
1051 WriteDefinition(stdout,args[0].desc);
1052 } else {
1053 if (g_def_list){
1054 length = gl_length(g_def_list);
1055 PRINTF("ATOM/CONSTANT types:\n");
1056 for(c=1;c<=length;c++){
1057 desc = (struct TypeDescription *)gl_fetch(g_def_list,c);
1058 if (BaseTypeIsAtomic(desc)!=0 || BaseTypeIsConstant(desc)!=0) {
1059 PRINTF("\t%s\n",SCP(GetName(desc)));
1060 }
1061 }
1062 PRINTF("Complex types:\n");
1063 for(c=1;c<=length;c++){
1064 desc = (struct TypeDescription *)gl_fetch(g_def_list,c);
1065 if (BaseTypeIsAtomic(desc)==0 && BaseTypeIsConstant(desc)==0) {
1066 PRINTF("\t%s\n",SCP(GetName(desc)));
1067 }
1068 }
1069 } else {
1070 Bell();
1071 PRINTF("Strange problem.\n");
1072 }
1073 }
1074 }
1075
1076 static
1077 void ListUniversals(union argument *args, int argc)
1078 {
1079 (void) args;
1080 (void) argc;
1081 PRINTF("Not implemented yet.\n");
1082 }
1083
1084 static
1085 void ResumeInstantiation(union argument *args, int argc)
1086 {
1087 if(argc==0){
1088 if (g_root) {
1089 SetupBinTokens();
1090 ReInstantiate(g_root);
1091 } else {
1092 Bell();
1093 PRINTF("No root instance.\n");
1094 }
1095 }
1096 else{
1097 if (args[0].i) {
1098 SetupBinTokens();
1099 ReInstantiate(args[0].i);
1100 } else {
1101 PRINTF("Incorrect instance.\n");
1102 }
1103 }
1104 }
1105
1106 static
1107 void lowerstring(register char *str)
1108 {
1109 while (*str != '\0'){
1110 if ((*str >= 'A')&&(*str <= 'Z'))
1111 *str = *str + ('a' - 'A');
1112 str++;
1113 }
1114 }
1115
1116
1117 static
1118 void Assignment(union argument *args, int argc)
1119 {
1120 char str[MAXID];
1121 int length=0;
1122 if((argc==1)&&(args[0].i)){
1123 switch(InstanceKind(args[0].i)) {
1124 case REAL_ATOM_INST:
1125 case REAL_INST:
1126 case REAL_CONSTANT_INST:
1127 PRINTF("Please enter the real value followed by units:");
1128 ReadString(str,&length);
1129 SetRealAtomValue(args[0].i,atof(str),0);
1130 break;
1131 case BOOLEAN_ATOM_INST:
1132 case BOOLEAN_INST:
1133 case BOOLEAN_CONSTANT_INST:
1134 PRINTF("Please enter boolean value(true or false):");
1135 ReadString(str,&length);
1136 lowerstring(str);
1137 if (strcmp(str,"true")==0)
1138 SetBooleanAtomValue(args[0].i,1,0);
1139 else if (strcmp(str,"false")==0)
1140 SetBooleanAtomValue(args[0].i,0,0);
1141 else{
1142 Bell();
1143 PRINTF("Incorrect boolean value.\n");
1144 }
1145 break;
1146 case INTEGER_ATOM_INST:
1147 case INTEGER_INST:
1148 case INTEGER_CONSTANT_INST:
1149 if (AtomMutable(args[0].i)){
1150 PRINTF("Please enter the integer value:");
1151 ReadString(str,&length);
1152 SetIntegerAtomValue(args[0].i,atol(str),0);
1153 }
1154 else{
1155 Bell();
1156 PRINTF("Attempting to assign to a fixed integer.\n");
1157 }
1158 break;
1159 case SET_ATOM_INST:
1160 case SET_INST:
1161 Bell();
1162 PRINTF("Not implemented yet.\n");
1163 break;
1164 case SYMBOL_ATOM_INST:
1165 case SYMBOL_INST:
1166 case SYMBOL_CONSTANT_INST:
1167 PRINTF("Enter the symbol value enclosed by single quotes('):");
1168 ReadString(str,&length);
1169
1170 break;
1171 default:
1172 Bell();
1173 PRINTF("The argument to assign is not a atom.\n");
1174 }
1175 }
1176 else{
1177 Bell();
1178 PRINTF("Incorrect arguments to assign.\n");
1179 }
1180 }
1181
1182 static
1183 int UniqueName(CONST char *str)
1184 {
1185 unsigned long c;
1186 struct Instance *ptr;
1187 for(c=gl_length(g_simulation_list);c>=1;c--){
1188 ptr = (struct Instance *)gl_fetch(g_simulation_list,c);
1189 if (strcmp(SCP(GetSimulationName(ptr)),str)==0) return 0;
1190 }
1191 return 1;
1192 }
1193
1194 static
1195 int CmpSim(struct Instance *sim1, struct Instance *sim2)
1196 {
1197 assert(sim1&&sim2);
1198 return CmpSymchar(GetSimulationName(sim1),GetSimulationName(sim2));
1199 }
1200
1201 static
1202 void GetDefinitionList(void)
1203 {
1204 if (g_def_list) gl_destroy(g_def_list);
1205 g_def_list = DefinitionList();
1206 }
1207
1208 static
1209 void HLoadDefinitions(union argument *args, int argc)
1210 {
1211 CONST char *name;
1212 int oldflag;
1213 /* Treat everything after the ``hload'' as the module to load
1214 * if (argc != 1) {
1215 * Bell();
1216 * PRINTF("Incorrect arguments\n");
1217 * ascfree((char *)args[0].id);
1218 * return;
1219 * }
1220 */
1221 (void)argc;
1222 name = args[0].id;
1223 if (Asc_OpenModule(name,NULL)){
1224 oldflag = GetParseRelnsFlag();
1225 SetParseRelnsFlag(0);
1226 zz_parse();
1227 SetParseRelnsFlag(oldflag);
1228 PRINTF("Module %s loaded.\n",name);
1229 GetDefinitionList();
1230 }
1231 else{
1232 Bell();
1233 PRINTF("Unable to load module %s\n",name);
1234 }
1235 ascfree((char *)args[0].id); /* this id was made my strcpy, not addsymbol */
1236 }
1237
1238 static
1239 void GlobalDefinitions(union argument *args, int argc)
1240 {
1241 #define SBUFSIZE 1024
1242 CONST char *name;
1243 char data[SBUFSIZE];
1244 struct module_t *m;
1245 struct gl_list_t *msl;
1246 unsigned long c,len;
1247 int status;
1248 FILE *fp;
1249 /* Treat everything after the ``gload'' as the module to load */
1250 (void)argc;
1251 name = args[0].id;
1252 fp = fopen( args[0].id, "r+");
1253 fgets(data,SBUFSIZE,fp);
1254 fclose(fp);
1255 if (strlen(data) == (SBUFSIZE-1)) {
1256 printf("test file may have been longer than %d characters read\n",
1257 SBUFSIZE-1);
1258 }
1259 printf(">>%s<<\n",data);
1260 m = Asc_OpenStringModule(data,&status,NULL);
1261 if (m != NULL ) {
1262 zz_parse();
1263 Asc_CloseCurrentModule();
1264 if (m != NULL) {
1265 PRINTF("Module name %s loaded.\n",Asc_ModuleName(m));
1266 PRINTF("Module best name %s loaded.\n",Asc_ModuleBestName(m));
1267 msl = Asc_ModuleStatementLists(m);
1268 if (msl != NULL) {
1269 PRINTF("Module statement ptr %lu.\n", (unsigned long)msl);
1270 len = gl_length(msl);
1271 PRINTF("Module # slists %lu.\n", len);
1272 for (c= 1; c<= len; c++) {
1273 WriteStatementList(stdout,gl_fetch(msl,c),4);
1274 }
1275 } else {
1276 PRINTF("Module no statements\n");
1277 }
1278 PRINTF("Module filename %s loaded.\n",Asc_ModuleFileName(m));
1279 }
1280 GetDefinitionList();
1281 }
1282 else{
1283 Bell();
1284 PRINTF("Unable to load module %s\n",name);
1285 }
1286 if (args[0].id != NULL) {
1287 ascfree((char *)args[0].id); /* this id was made my strcpy, not addsymbol */
1288 }
1289 }
1290
1291 static
1292 void LoadDefinitions(union argument *args, int argc)
1293 {
1294 CONST char *name;
1295 /* Treat everything after the ``load'' as the module to load
1296 * if (argc != 1) {
1297 * Bell();
1298 * PRINTF("Incorrect arguments\n");
1299 * ascfree((char *)args[0].id);
1300 * return;
1301 * }
1302 */
1303 (void)argc;
1304 name = args[0].id;
1305 if (Asc_OpenModule(name,NULL)){
1306 zz_parse();
1307 PRINTF("Module %s loaded.\n",name);
1308 GetDefinitionList();
1309 }
1310 else{
1311 Bell();
1312 PRINTF("Unable to load module %s\n",name);
1313 }
1314 if (args[0].id != NULL) {
1315 ascfree((char *)args[0].id); /* this id was made my strcpy, not addsymbol */
1316 }
1317 }
1318
1319 static
1320 void RequireModule(union argument *args, int argc)
1321 {
1322 CONST char *name;
1323 int status; /* return value from RequireModule() */
1324
1325 (void)argc; /* unused parameter */
1326
1327 name = args[0].id;
1328 Asc_RequireModule(name, &status );
1329 if( status == 5 ) {
1330 FPRINTF(ASCINF, "REQUIRED module %s already PROVIDED\n", name);
1331 }
1332 else if( status == 4 ) {
1333 FPRINTF(ASCERR, "Recursive REQUIRE for module %s. Ignored\n", name);
1334 }
1335 else if( status != 0 ) {
1336 FPRINTF(ASCERR, "Unable to locate file for module %s\n", name);
1337 }
1338 else {
1339 zz_parse();
1340 PRINTF("Module %s loaded.\n",name);
1341 GetDefinitionList();
1342 }
1343 ascfree((char *)args[0].id); /* this id was made my strcpy, not addsymbol */
1344 }
1345
1346
1347 #define NEWINSTTEST 1
1348 /* 1 means 3 phase compiler, 0 means 3phase, with no relations */
1349 static
1350 void CreateHollowInstance(union argument *args, int argc)
1351 {
1352 CONST char *name;
1353 struct TypeDescription *type;
1354 struct Instance *new;
1355 unsigned int prel;
1356 if (argc == 2){
1357 name = args[0].id;
1358 type = args[1].desc;
1359 if (UniqueName(name)){
1360 time_t start, delta;
1361
1362 g_ExtVariablesTable = NULL; /* defined in extinst.[ch] */
1363 #if (!NEWINSTTEST)
1364 prel = GetInstantiationRelnFlags();
1365 SetInstantiationRelnFlags(NORELS);
1366 #endif
1367 SetupBinTokens();
1368 start = clock();
1369 new = Instantiate(GetName(type),AddSymbol(name),0,AddSymbol(DEFMETHOD));
1370 delta = clock()-start;
1371 PRINTF("New Instantiation time (u-sec): %d\n",delta);
1372 #if (!NEWINSTTEST)
1373 SetInstantiationRelnFlags(prel);
1374 #endif
1375 if (new) { /* instantiate copies name */
1376 gl_insert_sorted(g_simulation_list,new,(CmpFunc)CmpSim);
1377 }
1378 else{
1379 PRINTF("Unable to create simulation %s\n",name);
1380 }
1381 }
1382 else{
1383 Bell();
1384 PRINTF("A simulation of the name %s already exists.\n",name);
1385 PRINTF("Please use a different name.\n");
1386 }
1387 }
1388 else{
1389 Bell();
1390 PRINTF("Incorrect arguments\n");
1391 }
1392 }
1393
1394 static
1395 void CreateInstance(union argument *args, int argc)
1396 {
1397 CONST char *name;
1398 struct TypeDescription *type;
1399 struct Instance *new;
1400 if (argc == 2){
1401 name = args[0].id;
1402 type = args[1].desc;
1403 if (UniqueName(name)){
1404 time_t start, delta;
1405 g_ExtVariablesTable = NULL; /* defined in extinst.[ch] */
1406 SetupBinTokens();
1407 start = clock();
1408 new = Instantiate(GetName(type),AddSymbol(name),0,AddSymbol(DEFMETHOD));
1409 delta = clock()-start;
1410 PRINTF("Instantiation time (u-sec): %d\n",delta);
1411 if (new) { /* instantiate copies name */
1412 gl_insert_sorted(g_simulation_list,new,(CmpFunc)CmpSim);
1413 }
1414 else{
1415 PRINTF("Unable to create simulation %s\n",name);
1416 }
1417 }
1418 else{
1419 Bell();
1420 PRINTF("A simulation of the name %s already exists.\n",name);
1421 PRINTF("Please use a different name.\n");
1422 }
1423 }
1424 else{
1425 Bell();
1426 PRINTF("Incorrect arguments\n");
1427 }
1428 }
1429
1430 static
1431 void Help(union argument *args, int argc)
1432 {
1433 unsigned long int com;
1434 if (argc == 1){
1435 com = FindCommand(args[0].id);
1436 if (com != 0) {
1437 CommandArgsPrint(stdout,com);
1438 } else {
1439 FPRINTF(stdout,"\"%s\" is not a defined command.\n",args[0].id);
1440 }
1441 } else {
1442 PRINTF(
1443 "Syntax: help <command name> to see the arguments of a command. Or,\n");
1444 PRINTF("while entering any command hit <space-?>"
1445 " to see options for next field.\n");
1446 }
1447 }
1448
1449 static
1450 void IntDestroyInstance(union argument *args, int argc)
1451 {
1452 if (argc == 1){
1453 DestroyInstance(args[0].i,NULL);
1454 }
1455 else{
1456 Bell();
1457 PRINTF("Incorrect arguments.\n");
1458 }
1459 }
1460
1461 void InterfaceCheckInstance(union argument *args, int argc)
1462 {
1463 if (argc == 1){
1464 CheckInstance(stdout,args[0].i);
1465 }
1466 else{
1467 Bell();
1468 PRINTF("Incorrect arguments.\n");
1469 }
1470 }
1471
1472 void InterfaceUseAnon(union argument *args, int argc)
1473 {
1474 g_use_copyanon = !g_use_copyanon;
1475 PRINTF("g_use_copyanon = %d\n",g_use_copyanon);
1476 }
1477
1478 static
1479 void InterfaceInstanceMemory(union argument *args, int argc)
1480 {
1481 #if (defined(__alpha) || defined(sun))
1482 struct mallinfo mi;
1483 #endif
1484 (void) args;
1485 (void) argc;
1486
1487 #ifdef MALLOC_DEBUG
1488 ascstatus("ALLOCATION REPORT:");
1489 #else
1490 PRINTF("Compiled without MALLOC_DEBUG\n");
1491 #endif
1492 /*
1493 #ifdef sun
1494 mallocmap();
1495 #endif
1496 */
1497 #if (defined(__alpha) || defined(sun))
1498 mi = mallinfo();
1499 PRINTF("arena:\t%d\n",mi.arena);
1500 PRINTF("ordblks:\t%d\n",mi.ordblks);
1501 PRINTF("smblks:\t%d\n",mi.smblks);
1502 PRINTF("hblks:\t%d\n",mi.hblks);
1503 PRINTF("hblkhd:\t%d\n",mi.hblkhd);
1504 PRINTF("usmblks:\t%d\n",mi.usmblks);
1505 PRINTF("fsmblks:\t%d\n",mi.fsmblks);
1506 PRINTF("uordblks:\t%d\n",mi.uordblks);
1507 PRINTF("fordblks:\t%d\n",mi.fordblks);
1508 PRINTF("keepcost:\t%d\n",mi.keepcost);
1509 #ifdef __SUN_SUNOS__
1510 PRINTF("mxfast:\t%d\n",mi.mxfast);
1511 PRINTF("nlblks:\t%d\n",mi.nlblks);
1512 PRINTF("grain:\t%d\n",mi.grain);
1513 PRINTF("uordbytes:\t%d\n",mi.uordbytes);
1514 PRINTF("allocated:\t%d\n",mi.allocated);
1515 PRINTF("treeoverhead:\t%d\n",mi.treeoverhead);
1516 #endif
1517 #endif
1518
1519 }
1520
1521 static
1522 void InterfaceInstanceStatistics(union argument *args, int argc)
1523 {
1524 if (argc == 1){
1525 InstanceStatistics(stdout,args[0].i);
1526 }
1527 else{
1528 Bell();
1529 PRINTF("Incorrect arguments.\n");
1530 }
1531 }
1532
1533 /* these should go away */
1534 #define NAME_DEBUG 0
1535 #if NAME_DEBUG
1536 extern unsigned long g_num_names_max;
1537 extern unsigned long g_num_names_cur;
1538 #endif
1539 static
1540 void InterfaceTokenStatistics(union argument *args, int argc)
1541 {
1542 if (argc == 1){
1543 InstanceTokenStatistics(stdout,args[0].i);
1544 ReportPendingPool(ASCERR);
1545 ReportValueManager(ASCERR);
1546 ReportSetManager(ASCERR);
1547 ReportRelInstantiator(ASCERR);
1548 ReportLogRelInstantiator(ASCERR);
1549 ReportInstanceNanny(ASCERR);
1550 gl_report_pool(ASCERR);
1551 gl_reportrecycler(ASCERR);
1552 #if NAME_DEBUG
1553 FPRINTF(ASCERR,"name elements %lu\n",g_num_names_cur);
1554 FPRINTF(ASCERR,"name elements max %lu\n",g_num_names_max);
1555 #endif
1556 PrintTab(0);
1557 }
1558 else{
1559 Bell();
1560 PRINTF("Incorrect arguments.\n");
1561 }
1562 }
1563
1564 static
1565 void ShowNoWarranty(union argument *args, int argc)
1566 {
1567 (void) args;
1568 (void) argc;
1569 PRINTF("%s",NO_WARRANTY);
1570 }
1571
1572 static
1573 void ShowGPL(union argument *args, int argc)
1574 {
1575 (void) args;
1576 (void) argc;
1577 PRINTF("%s%s%s%s%s",GPL1,GPL2,GPL3,GPL4,GPL5);
1578 }
1579
1580
1581 static
1582 void ScaleEqns(union argument *args, int argc)
1583 {
1584 double j;
1585 enum Expr_enum reltype;
1586 struct relation *rel;
1587 if (argc == 1 && InstanceKind(args[0].i)==REL_INST){
1588 j = CalcRelationNominal(args[0].i);
1589 if (j > 0.0) {
1590 rel = (struct relation *)GetInstanceRelation(args[0].i,&reltype);
1591 SetRelationNominal(rel,j);
1592 PRINTF(" scale constant = %g\n", RelationNominal(rel));
1593 } else {
1594 PRINTF(" error in scale constant calculation.");
1595 }
1596 }
1597 else{
1598 Bell();
1599 PRINTF("Incorrect relation argument.\n");
1600 }
1601 }
1602
1603 static
1604 void ScaleAllEqns(union argument *args, int argc)
1605 {
1606 if (argc == 1){
1607 PrintRelationNominals(args[0].i);
1608 }
1609 else{
1610 Bell();
1611 PRINTF("Incorrect arguments.\n");
1612 }
1613 }
1614 static
1615 void CalcResiduals(union argument *args, int argc)
1616 {
1617 if (argc == 1){
1618 PrintRelationResiduals(args[0].i);
1619 }
1620 else{
1621 Bell();
1622 PRINTF("Incorrect arguments.\n");
1623 }
1624 }
1625 static
1626 void CalcAllVars(union argument *args, int argc)
1627 {
1628 if (argc == 1){
1629 PrintDirectSolveSolutions(args[0].i);
1630 }
1631 else{
1632 Bell();
1633 PRINTF("Incorrect arguments.\n");
1634 }
1635 }
1636 static
1637 void CalcAllBoolVars(union argument *args, int argc)
1638 {
1639 if (argc == 1){
1640 PrintDirectSolveBooleanSolutions(args[0].i);
1641 }
1642 else{
1643 Bell();
1644 PRINTF("Incorrect arguments.\n");
1645 }
1646 }
1647 static
1648 void CalcGradients(union argument *args, int argc)
1649 {
1650 if (argc == 1){
1651 PrintRelationGradients(args[0].i);
1652 }
1653 else{
1654 Bell();
1655 PRINTF("Incorrect arguments.\n");
1656 }
1657 }
1658
1659 static
1660 void InstanceRefine(union argument *args, int argc)
1661 {
1662 struct TypeDescription *desc, *desc1, *desc2;
1663 struct Instance *i, *top, *inst;
1664 if (argc>1) {
1665 PRINTF("Wrong # args. refine <typename>\n");
1666 return;
1667 }
1668 i = g_root;
1669 if (!i) {
1670 PRINTF("Cannot refine a NULL instance\n");
1671 return;
1672 }
1673 switch(InstanceKind(i)) {
1674 case REAL_INST: case BOOLEAN_INST:
1675 case INTEGER_INST: case SYMBOL_INST:
1676 case SET_INST: case REL_INST: case LREL_INST:
1677 PRINTF("AscendIV does not allow\nrefinement of\nchildren of ATOMs");
1678 return;
1679 default:
1680 break;
1681 }
1682
1683 desc1 = InstanceTypeDesc(i);
1684 desc2 = args[0].desc;
1685 if (!desc2) {
1686 PRINTF("Type %s not found",args[0].id);
1687 return;
1688 }
1689 if (desc1==desc2) {
1690 return;
1691 }
1692 desc = MoreRefined(desc1,desc2);
1693 /* fix me. needs to prohibit refinement to parameterized types
1694 * or figure out how to get proper arguments
1695 */
1696 if(desc !=NULL) {
1697 if (desc == desc1) {
1698 /* desc1 more refined than desc2 */
1699 return;
1700 } else {
1701 inst = i; /* just in case refine moves i*/
1702 top = inst = RefineClique(inst,desc,NULL);
1703 do { /* Reinstatiate the entire clique */
1704 SetupBinTokens();
1705 ReInstantiate(inst);
1706 inst = NextCliqueMember(inst);
1707 } while (inst != top);
1708 g_root = i;
1709 }
1710 } else {
1711 PRINTF("Types are not conformable or the Library is inconsistent");
1712 }
1713 }
1714
1715
1716 static
1717 void AddCommands(void)
1718 {
1719 AddCommand(1,"print",PrintInstance,"print an instance",0,1,instance_arg);
1720 AddCommand(1,"visitlocal",LeavesInstance,"test arrayVisitLocalLeaves",0,1,
1721 instance_arg);
1722 AddCommand(1,"check",InterfaceCheckInstance,"check an instance",0,1,
1723 instance_arg);
1724 AddCommand(1,"memory",InterfaceInstanceMemory,
1725 "report memory data",0,0);
1726 AddCommand(1,"anoncompilation",InterfaceUseAnon,
1727 "toggle anonymous relation sharing optimization",0,0);
1728 AddCommand(1,"statistics",InterfaceInstanceStatistics,
1729 "report instance data",0,1,instance_arg);
1730 AddCommand(1,"tokens",InterfaceTokenStatistics,
1731 "report token relation data",0,1,instance_arg);
1732 AddCommand(1,"aliases",AliiInstance,"list all the aliases of an instance",
1733 0,1,instance_arg);
1734 AddCommand(1,"isas",IsasInstance,"list all the constructions of an instance",
1735 0,1,instance_arg);
1736 AddCommand(1,"cliques",CliqueInstance,
1737 "list all members of an instance's clique",0,1,instance_arg);
1738 AddCommand(1,"merge",InstanceMerge,"ARE_THE_SAME two instances",
1739 0,2,instance_arg,instance_arg);
1740 AddCommand(1,"run",RunInitialization,"execute an initialization procedure",
1741 0,2,instance_arg,id_arg);
1742 AddCommand(1,"make_alike",MakeAlike,"ARE_ALIKE two instances",
1743 0,2,instance_arg,instance_arg);
1744 AddCommand(1,"callproc",CallExternalCmd,
1745 "call an external procedure", 0,1,instance_arg);
1746 AddCommand(1,"quit",Shutdown,LEAVEHELP,1,0);
1747 AddCommand(1,"exit",Shutdown,LEAVEHELP,1,0);
1748 AddCommand(1,"bye",Shutdown,LEAVEHELP,1,0);
1749 AddCommand(1,"stop",Shutdown,LEAVEHELP,1,0);
1750 AddCommand(1,"reset",ResetTerminal,"reset the terminal",0,0);
1751 /* AddCommand(1,"minos",MinosInstance,"solve using MINOS 5.1",
1752 0,1,instance_arg); */
1753 /* AddCommand(1,"optimize",OptimizeInstance,
1754 "run Tom's algorithm on a NLP",0,1,instance_arg); */
1755 /* AddCommand(1,"solve",SolveInstance,"solve the system of equations",
1756 0,1,instance_arg);*/
1757 AddCommand(1,"root",SetRoot,"set the context for all other searches",
1758 0,1,instance_arg);
1759 AddCommand(1,"list",ListInstances,"list all the instances",
1760 0,1,instance_arg);
1761 AddCommand(1,"define",DefineType,"define a type or list all the types",
1762 0,1,definition_arg);
1763 AddCommand(1,"notes",TypeNotes,"list notes on a type",
1764 0,1,definition_arg);
1765 AddCommand(1,"refine",InstanceRefine,"refine instance to new type",
1766 0,1,definition_arg);
1767 AddCommand(1,"universals",ListUniversals,"list all the universal instances",
1768 0,0);
1769 AddCommand(1,"resume",ResumeInstantiation,
1770 "try to complete the instantiation of a partial instance",
1771 0,1,instance_arg);
1772 AddCommand(1,"assign",Assignment,"assign a value to an instance",
1773 0,1,instance_arg);
1774 AddCommand(1,"create",CreateInstance,"create an instance of a type",
1775 0,2,id_arg,definition_arg);
1776 AddCommand(1,"hide",HideChild,"hide a child in a type",
1777 0,1,id_arg);
1778 AddCommand(1,"showchildren",ShowChildren,"unhide children in a type",
1779 0,0);
1780 AddCommand(1,"ncreate",CreateHollowInstance,
1781 "create (2 phase) an instance of a type",
1782 0,2,id_arg,definition_arg);
1783 AddCommand(1,"load",LoadDefinitions,"load a module",
1784 0,1,shell_arg);
1785 AddCommand(1,"hload",HLoadDefinitions,"load a module w/no relss",
1786 0,1,shell_arg);
1787 AddCommand(1,"gload",GlobalDefinitions,"load a small module as a string",
1788 0,1,shell_arg);
1789 AddCommand(1,"require",RequireModule,"load module if not previously loaded",
1790 0,1,shell_arg);
1791 AddCommand(1,"help",Help,"give expected arg list for a given command",
1792 0,1,id_arg);
1793 AddCommand(1,"destroy",IntDestroyInstance,
1794 "deallocate and deconstruct an instance",0,1,instance_arg);
1795 AddCommand(1,"parents",ListParents,"list an instances parents",
1796 0,1,instance_arg);
1797 AddCommand(1,"copying",ShowGPL,"show the terms of your license", 0,0);
1798 AddCommand(1,"arrays",PrintArrayTypes,"list array definitions", 0,0);
1799 AddCommand(1,"anons",PrintAnonTypes,"list anonymous types", 0,1,instance_arg);
1800 AddCommand(1,"warranty",ShowNoWarranty,"show no warranty notice", 0,0);
1801 AddCommand(1,"scale",ScaleEqns,"scales equations by largest additive term",
1802 0,1,instance_arg);
1803 AddCommand(1,"allscale",ScaleAllEqns,
1804 "scales equations by largest additive term",0,1,instance_arg);
1805 AddCommand(1,"timeresid",TimeResiduals,
1806 "times residuals using binary, infix and postfix functions",
1807 0,1,instance_arg);
1808 AddCommand(1,"calcresid",CalcResiduals,
1809 "calculates residuals using infix and postfix functions",
1810 0,1,instance_arg);
1811 AddCommand(1,"calcvars",CalcAllVars,
1812 "direct solves for each var in relation (for testing purposes)",
1813 0,1,instance_arg);
1814 AddCommand(1,"calcbvars",CalcAllBoolVars,
1815 "direct solves for each dvar in logrel(for testing purposes)",
1816 0,1,instance_arg);
1817 AddCommand(1,"calcgrads",CalcGradients,
1818 "calculates gradients using postfix function",0,1,instance_arg);
1819 AddCommand(1,"relprint",PrintInstanceRelations,
1820 "prints relations to /tmp/reldump.txt",0,1,instance_arg);
1821 AddCommand(1,"urelprint",PrintUniqueRelations,
1822 "prints unique relations to /tmp/reldump.txt",0,1,instance_arg);
1823 AddCommand(1,"mapprint",PrintVisitMap,
1824 "prints visit map info to /tmp/reldump.txt",0,1,instance_arg);
1825 AddCommand(1,"dotprint",PrintVisitMapDOT,
1826 "prints visit map DOT to /tmp/reldump.txt",0,1,instance_arg);
1827 AddCommand(1,"lrelprint",PrintInstanceLogRelations,
1828 "prints log relations to /tmp/reldump.txt",0,1,instance_arg);
1829 AddCommand(1,"visitindex",PrintIndexedVisit,
1830 "prints visit indices/names to /tmp/reldump.txt",0,1,instance_arg);
1831 AddCommand(1,"proto",ProtoTypeInstanceCmd,
1832 "prototype an instance",0,1,instance_arg);
1833 AddCommand(1,"shell",SystemCmd, /* don't change 'shell' to anything else */
1834 "send rest of arguments to system shell",0,1,shell_arg);
1835 }
1836
1837 static
1838 void ClearBuffers(char *line, int *gpos, char *piece, int *pos)
1839 {
1840 while((*gpos)>0) line[--(*gpos)] = '\0';
1841 while((*pos)>0) piece[--(*pos)] = '\0';
1842 }
1843
1844 static
1845 void RePrint(char *line)
1846 {
1847 PRINTF("%s%s",PROMPT,line);
1848 }
1849
1850 static
1851 void WritePossible(unsigned long int lower, unsigned long int upper)
1852 {
1853 while(lower<=upper){
1854 PRINTF("%-15s %s\n",CommandName(lower),CommandHelp(lower));
1855 lower++;
1856 }
1857 }
1858
1859 static
1860 void ClearPiece(char *str, int *count)
1861 {
1862 while(*count > 0) str[--(*count)] = '\0';
1863 }
1864
1865 /* c,&lower,&upper,full_line,piece,&gpos,&pos,&state, &arg_count,&done */
1866 static
1867 void AddShellChar(char c,
1868 unsigned long int *l,
1869 unsigned long int *u,
1870 char *line,
1871 char *piece,
1872 int *gpos, int *pos, int *state,
1873 int *argcount, int *done,union argument *args)
1874 {
1875 int len;
1876 CONST char *p;
1877 (void)l;
1878 (void)u;
1879 (void)state;
1880 (void)argcount;
1881 *done = 0;
1882 if (isprint(c)){
1883 putchar(c);
1884 line[(*gpos)++] = c;
1885 piece[(*pos)++] = c;
1886 } else{
1887 switch(c){
1888 case ASC_FLUSH:
1889 putchar('\r');
1890 ClearLine();
1891 ClearBuffers(line,gpos,piece,pos);
1892 RePrint(line);
1893 break;
1894 case ASC_CLEAR:
1895 ResetTerminal(NULL,0);
1896 RePrint(line);
1897 break;
1898 case ASC_BS:
1899 case ASC_DELETE:
1900 if (*gpos > 0){
1901 DeleteBackOne();
1902 line[--(*gpos)] = '\0';
1903 if (*pos > 0){
1904 piece[--(*pos)] = '\0';
1905 }
1906 } else {
1907 Bell();
1908 }
1909 break;
1910 case ASC_NEWLINE:
1911 putchar(ASC_NEWLINE);
1912 *done = 1;
1913 p = line;
1914 while( isspace(*p) && *p++ != '\0' ); /* remove leading whitespace */
1915 while( ! isspace(*p) && *p++ != '\0' ); /* command name */
1916 while( isspace(*p) && *p++ != '\0' ); /* remove whitespace between
1917 * command name are arguments */
1918 len = strlen(p);
1919 if (len > 0) {
1920 char *shellstring;
1921 shellstring = ASC_NEW_ARRAY(char,len+1);
1922 strcpy(shellstring, p);
1923 args[0].id = shellstring;
1924 ClearBuffers(line,gpos,piece,pos);
1925 }
1926 break;
1927 default:
1928 Bell();
1929 break;
1930 }
1931 }
1932 }
1933
1934
1935 static
1936 void AddCommandChar(char c,
1937 unsigned long int *l,
1938 unsigned long int *u,
1939 char *line,
1940 char *piece,
1941 int *gpos, int *pos, int *state,
1942 int *errloc, unsigned long int *com)
1943 {
1944 int oldpos;
1945 *com = 0;
1946 if ((*l==0)&&(*u==0)){
1947 *l = 1;
1948 *u = NumberCommands();
1949 LimitCommand(l,u,piece,0);
1950 }
1951 if (isprint(c)){
1952 switch(c){
1953 case ' ':
1954 if (*pos){
1955 if (strcmp(piece,CommandName(*l))){
1956 oldpos = *pos;
1957 CompleteCommand(*l,*u,piece,pos);
1958 while(oldpos < *pos){
1959 putchar(piece[oldpos]);
1960 line[(*gpos)++] = piece[oldpos++];
1961 }
1962 } else { /* equal */
1963 putchar(' ');
1964 line[(*gpos)++] = ' ';
1965 *com = *l;
1966 ClearPiece(piece,pos);
1967 }
1968 } else {
1969 line[(*gpos)++] = c;
1970 putchar(c);
1971 }
1972 break;
1973 case '?':
1974 putchar('\n');
1975 WritePossible(*l,*u);
1976 RePrint(line);
1977 break;
1978 default:
1979 putchar(c);
1980 line[(*gpos)++] = c;
1981 piece[(*pos)++] = c;
1982 LimitCommand(l,u,piece,*pos - 1);
1983 if (*l > *u){
1984 piece[--(*pos)] = '\0';
1985 *l = *u = 0;
1986 Bell();
1987 *errloc = *gpos - 1;
1988 *state = 5;
1989 }
1990 break;
1991 }
1992 } else{
1993 switch(c){
1994 case ASC_FLUSH:
1995 putchar('\r');
1996 ClearLine();
1997 *l=0;
1998 *u=0;
1999 ClearBuffers(line,gpos,piece,pos);
2000 RePrint(line);
2001 break;
2002 case ASC_CLEAR:
2003 ResetTerminal(NULL,0);
2004 RePrint(line);
2005 break;
2006 case ASC_BS:
2007 case ASC_DELETE:
2008 if (*gpos > 0){
2009 DeleteBackOne();
2010 line[--(*gpos)] = '\0';
2011 if (*pos > 0){
2012 piece[--(*pos)] = '\0';
2013 }
2014 *l = *u = 0;
2015 }
2016 else
2017 Bell();
2018 break;
2019 case ASC_NEWLINE:
2020 putchar(ASC_NEWLINE);
2021 if ((*l <= *u)&&(strcmp(piece,CommandName(*l))==0)){
2022 *com = *l;
2023 }
2024 ClearBuffers(line,gpos,piece,pos);
2025 break;
2026 default:
2027 Bell();
2028 break;
2029 }
2030 }
2031 }
2032
2033 static
2034 void AddErrorChar(char c, char *line, int *gpos, char *piece,
2035 int *pos, int *state, int errloc, int oldstate)
2036 {
2037 if (isprint(c)){
2038 if (c=='?'){
2039 PRINTF("\nNo possible completions.\n");
2040 RePrint(line);
2041 } else {
2042 line[(*gpos)++] = c;
2043 putchar(c);
2044 }
2045 } else {
2046 switch(c){
2047 case ASC_FLUSH:
2048 putchar('\r');
2049 ClearLine();
2050 ClearBuffers(line,gpos,piece,pos);
2051 RePrint(line);
2052 *state = 0;
2053 break;
2054 case ASC_CLEAR:
2055 ResetTerminal(NULL,0);
2056 RePrint(line);
2057 break;
2058 case ASC_BS:
2059 case ASC_DELETE:
2060 if (*gpos > 0){
2061 DeleteBackOne();
2062 line[--(*gpos)] = '\0';
2063 if (*gpos == errloc){
2064 *state = oldstate;
2065 }
2066 }
2067 else
2068 Bell();
2069 break;
2070 case ASC_NEWLINE:
2071 putchar(ASC_NEWLINE);
2072 ClearBuffers(line,gpos,piece,pos);
2073 *state = 0;
2074 break;
2075 default:
2076 Bell();
2077 break;
2078 }
2079
2080 }
2081 }
2082
2083 static
2084 void InitializeArgs(unsigned long int com, union argument *args)
2085 {
2086 int c,num;
2087 num=CommandNumArgs(com);
2088 for(c=0;c<num;c++){
2089 switch(CommandArgument(com,c)){
2090 case instance_arg:
2091 args[c].i = NULL;
2092 break;
2093 case definition_arg:
2094 args[c].desc = NULL;
2095 break;
2096 case id_arg:
2097 args[c].id = NULL;
2098 break;
2099 case shell_arg:
2100 args[c].id = NULL;
2101 break;
2102 }
2103 }
2104 }
2105
2106 static
2107 void GetLastPiece(register char *line,
2108 register int gpos,
2109 register char *piece,
2110 register int *pos)
2111 {
2112 register int c=0;
2113 assert((*piece=='\0')&&(*pos==0));
2114 c = gpos--;
2115 while((gpos >= 0)&&(line[gpos] != ' ')) gpos--;
2116 if (gpos >= 0) gpos++;
2117 else gpos = 0;
2118 while(gpos < c) piece[(*pos)++] = line[gpos++];
2119 piece[*pos] = '\0';
2120 }
2121
2122 static
2123 int StateFromArg(unsigned long int com, int arg)
2124 {
2125 switch(CommandArgument(com,arg)){
2126 case instance_arg:
2127 g_search_inst = g_root;
2128 return 1;
2129 case definition_arg: return 2;
2130 case id_arg: return 3;
2131 case shell_arg: return 7;
2132 }
2133 Asc_Panic(2, NULL, "Unknown argument type in StateFromArg\n");
2134
2135 }
2136
2137 static
2138 void ControlCharacter(char c, char *line, int *gpos, char *piece,
2139 int *pos, int *state, unsigned long int com, int *argc)
2140 {
2141 switch(c){
2142 case ASC_FLUSH:
2143 putchar('\r');
2144 ClearLine();
2145 ClearBuffers(line,gpos,piece,pos);
2146 RePrint(line);
2147 *state = 0;
2148 break;
2149 case ASC_CLEAR:
2150 ResetTerminal(NULL,0);
2151 RePrint(line);
2152 break;
2153 case ASC_BS:
2154 case ASC_DELETE:
2155 if (*gpos > 0){
2156 DeleteBackOne();
2157 line[--(*gpos)] = '\0';
2158 if (*pos > 0)
2159 piece[--(*pos)] = '\0';
2160 else{
2161 if (line[*gpos - 1] != ' '){ /* backed into previous field */
2162 if (*argc)
2163 *state = StateFromArg(com,--(*argc));
2164 else
2165 *state = 0;
2166 GetLastPiece(line,*gpos,piece,pos);
2167 }
2168 }
2169 }
2170 else
2171 Bell();
2172 break;
2173 case ASC_NEWLINE:
2174 putchar(ASC_NEWLINE);
2175 ClearBuffers(line,gpos,piece,pos);
2176 *state = 0;
2177 break;
2178 default:
2179 Bell();
2180 break;
2181 }
2182 }
2183
2184 static
2185 void AddIdChar(char c, char *line, int *gpos, char *piece,
2186 int *pos, int *state, int *errloc,
2187 int *done, union argument *args, int *argc,
2188 unsigned long int com)
2189 {
2190 *done = 0;
2191 if (isprint(c)){
2192 if (c == '?'){
2193 PRINTF("\nPlease enter an identifier.\n");
2194 RePrint(line);
2195 }
2196 else if (c == ' '){
2197 putchar(c);
2198 line[(*gpos)++] = c;
2199 if (*pos > 0){
2200 args[(*argc)++].id = SCP(AddSymbolL(piece,*pos));
2201 ClearPiece(piece,pos);
2202 *done = 1;
2203 }
2204 }
2205 else if (isidchar(c)){
2206 line[(*gpos)++] = c;
2207 piece[(*pos)++] = c;
2208 putchar(c);
2209 }
2210 else{
2211 putchar(c);
2212 line[(*gpos)++] = c;
2213 Bell();
2214 *errloc = *gpos - 1;
2215 *state = 5;
2216 }
2217 }
2218 else{
2219 if (c==ASC_NEWLINE){
2220 if (*pos>0)
2221 args[(*argc)++].id = SCP(AddSymbolL(piece,*pos));
2222 else
2223 args[*argc].id = NULL;
2224 *done = 1;
2225 }
2226 ControlCharacter(c,line,gpos,piece,pos,state,com,argc);
2227 }
2228 }
2229
2230 static
2231 int Defmycmp(register CONST char *str1, register CONST char *str2)
2232 {
2233 while(*str1 != '\0'){
2234 if (*str1 < *str2) return -1;
2235 if (*str1 > *str2) return 1;
2236 str1++;
2237 str2++;
2238 }
2239 return 0;
2240 }
2241
2242 static
2243 int DefNumberMatch(register CONST char *str1,
2244 register CONST char *str2,
2245 int max)
2246 {
2247 register int c=0;
2248 while((c<max)&&(*str1 != '\0')&&(*str1 == *str2)){
2249 str1++;
2250 str2++;
2251 c++;
2252 }
2253 return c;
2254 }
2255
2256 static
2257 unsigned long FindDefGLB(char *str, int place,
2258 register unsigned long int lower,
2259 register unsigned long int upper)
2260 /*********************************************************************\
2261 Find the greatist lower bound for a command string that could match
2262 str.
2263 \*********************************************************************/
2264 {
2265 register struct TypeDescription *ptr;
2266 unsigned long search;
2267 str += place;
2268 while (lower <= upper){
2269 search = (lower+upper)/2;
2270 ptr = (struct TypeDescription *)gl_fetch(g_def_list,search);
2271 switch(Defmycmp(str,SCP(GetName(ptr))+place)){
2272 case 0:
2273 do {
2274 if (--search < lower) return lower;
2275 ptr = (struct TypeDescription *)gl_fetch(g_def_list,search);
2276 } while(Defmycmp(str,SCP(GetName(ptr))+place)==0);
2277 return search+1;
2278 case -1: upper = search-1; break;
2279 case 1: lower = search+1; break;
2280 }
2281 }
2282 return lower;
2283 }
2284
2285 static
2286 unsigned long FindDefLUB(char *str, int place,
2287 register unsigned long int lower,
2288 register unsigned long int upper)
2289 /*********************************************************************\
2290 Find the least upper bound for a command string that could match
2291 str.
2292 \*********************************************************************/
2293 {
2294 register struct TypeDescription *ptr;
2295 unsigned long search;
2296 str += place;
2297 while (lower <= upper){
2298 search = (lower+upper)/2;
2299 ptr = (struct TypeDescription *)gl_fetch(g_def_list,search);
2300 switch(Defmycmp(str,SCP(GetName(ptr))+place)){
2301 case 0:
2302 do {
2303 if (++search > upper) return upper;
2304 ptr = (struct TypeDescription *)gl_fetch(g_def_list,search);
2305 } while(Defmycmp(str,SCP(GetName(ptr))+place)==0);
2306 return search-1;
2307 case -1: upper = search-1; break;
2308 case 1: lower = search+1; break;
2309 }
2310 }
2311 return upper;
2312 }
2313
2314 static
2315 void LimitDefinition(unsigned long int *l,
2316 unsigned long int *u,
2317 char *str, int pos)
2318 {
2319 register struct TypeDescription *ptr;
2320 if (*l > *u) return;
2321 if (*l == *u){
2322 ptr = (struct TypeDescription *)gl_fetch(g_def_list,*l);
2323 if (Defmycmp(str+pos,SCP(GetName(ptr))+pos)) (*u)--;
2324 }
2325 else{
2326 *l = FindDefGLB(str,pos,*l,*u);
2327 *u = FindDefLUB(str,pos,*l,*u);
2328 }
2329 }
2330
2331 static
2332 void CompleteDefinition(unsigned long int l,
2333 unsigned long int u,
2334 char *str, int *place)
2335 {
2336 register CONST char *cpy;
2337 int count;
2338 unsigned long pos;
2339 struct TypeDescription *ptr;
2340 ptr = (struct TypeDescription *)gl_fetch(g_def_list,l);
2341 cpy = SCP(GetName(ptr));
2342 count = (int)strlen(cpy);
2343 for(pos = l+1;pos<=u;pos++){
2344 ptr = (struct TypeDescription *)gl_fetch(g_def_list,pos);
2345 count = DefNumberMatch(cpy,SCP(GetName(ptr)),count);
2346 }
2347 strncpy(str,cpy,count);
2348 str[count] = '\0';
2349 *place = count;
2350 }
2351
2352 static
2353 void WritePossibleDefs(unsigned long int l, unsigned long int u)
2354 {
2355 struct TypeDescription *desc;
2356 while(l <= u){
2357 desc = (struct TypeDescription *)gl_fetch(g_def_list,l++);
2358 PRINTF("%s\n",SCP(GetName(desc)));
2359 }
2360 }
2361
2362 static
2363 void AddDefinitionChar(char c,
2364 unsigned long int *l,
2365 unsigned long int *u,
2366 char *line, int *gpos, char *piece, int *pos,
2367 int *state, int *errloc, int *done,
2368 union argument *args, int *argc,
2369 unsigned long int com)
2370 {
2371 int oldpos;
2372 *done = 0;
2373 if ((*l==0)&&(*u==0)){
2374 *l = 1;
2375 *u = gl_length(g_def_list);
2376 LimitDefinition(l,u,piece,0);
2377 }
2378 if (isprint(c)){
2379 switch(c){
2380 case ' ':
2381 if (*pos){
2382 if (!FindType(AddSymbol(piece))){
2383 oldpos = *pos;
2384 CompleteDefinition(*l,*u,piece,pos);
2385 while (oldpos < *pos){
2386 putchar(piece[oldpos]);
2387 line[(*gpos)++] = piece[oldpos++];
2388 }
2389 }
2390 else{
2391 putchar(' ');
2392 line[(*gpos)++] = ' ';
2393 args[(*argc)++].desc = FindType(AddSymbol(piece));
2394 *done = 1;
2395 ClearPiece(piece,pos);
2396 }
2397 }
2398 else{
2399 line[(*gpos)++] = c;
2400 putchar(c);
2401 }
2402 break;
2403 case '?':
2404 putchar('\n');
2405 WritePossibleDefs(*l,*u);
2406 RePrint(line);
2407 break;
2408 default:
2409 putchar(c);
2410 line[(*gpos)++] = c;
2411 piece[(*pos)++] = c;
2412 LimitDefinition(l,u,piece,*pos - 1);
2413 if (*l > *u){
2414 piece[--(*pos)] = '\0';
2415 *l = *u = 0;
2416 Bell();
2417 *errloc = *gpos - 1;
2418 *state = 5;
2419 }
2420 break;
2421 }
2422 }
2423 else{
2424 switch(c){
2425 case ASC_FLUSH:
2426 putchar('\r');
2427 ClearLine();
2428 *l = *u =0;
2429 *state = 0;
2430 ClearBuffers(line,gpos,piece,pos);
2431 RePrint(line);
2432 break;
2433 case ASC_CLEAR:
2434 ResetTerminal(NULL,0);
2435 RePrint(line);
2436 break;
2437 case ASC_BS:
2438 case ASC_DELETE:
2439 if (*gpos > 0){
2440 DeleteBackOne();
2441 line[--(*gpos)] = '\0';
2442 if (*pos > 0){
2443 piece[--(*pos)] = '\0';
2444 }
2445 else{
2446 if (line[*gpos - 1] != ' '){ /* backed into previous field */
2447 if (*argc)
2448 *state = StateFromArg(com,--(*argc));
2449 else
2450 *state = 0;
2451 GetLastPiece(line,*gpos,piece,pos);
2452 }
2453 }
2454 *l = *u = 0;
2455 }
2456 else
2457 Bell();
2458 break;
2459 case ASC_NEWLINE:
2460 putchar(ASC_NEWLINE);
2461 *done = 1;
2462 if ((*l <= *u)&&(FindType(AddSymbol(piece)))){
2463 args[(*argc)++].desc = FindType(AddSymbol(piece));
2464 }
2465 ClearBuffers(line,gpos,piece,pos);
2466 break;
2467 default:
2468 Bell();
2469 break;
2470 }
2471 }
2472 }
2473
2474 static
2475 void AddDoneChar(char c, char *line, int *gpos, char *piece,
2476 int *pos, int *state, int *errloc,
2477 unsigned long int com, int *argc)
2478 {
2479 if (isprint(c)){
2480 if (c == '?'){
2481 PRINTF("\nThe command and all its arguments"
2482 " are correct press return.\n");
2483 RePrint(line);
2484 }
2485 else {
2486 putchar(c);
2487 line[(*gpos)++] = c;
2488 if (c != ' '){
2489 *errloc = *gpos - 1;
2490 Bell();
2491 *state = 5;
2492 }
2493 }
2494 }
2495 else
2496 ControlCharacter(c,line,gpos,piece,pos,state,com,argc);
2497 }
2498
2499 static
2500 struct Instance *FindSimulationRoot(char *str)
2501 {
2502 unsigned long c,len;
2503 struct Instance *ptr;
2504
2505 len = gl_length(g_simulation_list);
2506 for (c=len;c>=1;c--){
2507 ptr = (struct Instance *)gl_fetch(g_simulation_list,c);
2508 if (strcmp(SCP(GetSimulationName(ptr)),str)==0) {
2509 return GetSimulationRoot(ptr);
2510 }
2511 }
2512 return NULL;
2513 }
2514
2515 static
2516 void SearchStr(char *str, char *temp)
2517 {
2518 register char *ptr;
2519 struct InstanceName name;
2520 open_bracket = 0;
2521 open_quote = 0;
2522 g_search_inst = g_root;
2523 ptr = temp;
2524 while(*str != '\0'){
2525 switch(*str){
2526 case '.':
2527 if (*(str-1) != ']'){
2528 *ptr = '\0';
2529 if (g_search_inst){
2530 SetInstanceNameType(name,StrName);
2531 SetInstanceNameStrPtr(name,AddSymbol(temp));
2532 g_search_inst = InstanceChild(g_search_inst,
2533 ChildSearch(g_search_inst,&name));
2534 }
2535 else
2536 g_search_inst = FindSimulationRoot(temp);
2537 }
2538 str++;
2539 ptr = temp;
2540 break;
2541 case '\'':
2542 str++;
2543 if (open_quote) open_quote--;
2544 else open_quote++;
2545 break;
2546 case '[':
2547 if (*(str-1) != ']'){
2548 *ptr = '\0';
2549 if (g_search_inst){
2550 SetInstanceNameType(name,StrName);
2551 SetInstanceNameStrPtr(name,AddSymbol(temp));
2552 g_search_inst = InstanceChild(g_search_inst,
2553 ChildSearch(g_search_inst,&name));
2554 }
2555 else
2556 g_search_inst = FindSimulationRoot(temp);
2557 }
2558 ptr = temp;
2559 open_bracket++;
2560 str++;
2561 break;
2562 case ']':
2563 open_bracket--;
2564 *ptr = '\0';
2565 str++;
2566 switch(InstanceKind(g_search_inst)){
2567 case ARRAY_INT_INST:
2568 SetInstanceNameType(name,IntArrayIndex);
2569 SetInstanceNameIntIndex(name,atol(temp));
2570 g_search_inst = InstanceChild(g_search_inst,
2571 ChildSearch(g_search_inst,&name));
2572 break;
2573 case ARRAY_ENUM_INST:
2574 SetInstanceNameType(name,StrArrayIndex);
2575 SetInstanceNameStrIndex(name,AddSymbol(temp));
2576 g_search_inst = InstanceChild(g_search_inst,
2577 ChildSearch(g_search_inst,&name));
2578 break;
2579 default:
2580 Bell();
2581 ERROR_REPORTER_HERE(ASC_PROG_ERR,"Unexpected error in FindInstanceLimits.");
2582 break;
2583 }
2584 ptr = temp;
2585 break;
2586 default:
2587 *(ptr++) = *(str++);
2588 break;
2589 }
2590 }
2591 *ptr = '\0';
2592 }
2593
2594 static
2595 void GetLastPart(char *str, char *part)
2596 {
2597 int length;
2598 register char *ptr;
2599 length = strlen(str);
2600 while(length && (str[length] != '[') && (str[length] != '.'))
2601 length--;
2602 if (str[length] == '.') length++;
2603 ptr = part;
2604 while(str[length] != '\0')
2605 *(ptr++) = str[length++];
2606 *ptr = '\0';
2607 }
2608
2609 static
2610 int CmpName(struct InstanceName name1, struct InstanceName name2)
2611 {
2612 assert(InstanceNameType(name1)==InstanceNameType(name2));
2613 switch(InstanceNameType(name1)){
2614 case IntArrayIndex:
2615 if (InstanceIntIndex(name1)<InstanceIntIndex(name2)) {
2616 return -1;
2617 } else {
2618 if (InstanceIntIndex(name1)==InstanceIntIndex(name2)) {
2619 return 0;
2620 } else {
2621 return 1;
2622 }
2623 }
2624 case StrArrayIndex:
2625 return Defmycmp(SCP(InstanceStrIndex(name1)),SCP(InstanceStrIndex(name2)));
2626 case StrName:
2627 return Defmycmp(SCP(InstanceNameStr(name1)),SCP(InstanceNameStr(name2)));
2628 }
2629 /*NOTREACHED*/
2630 return 1;
2631 }
2632
2633 static
2634 unsigned long FindSimLUB(char *str)
2635 {
2636 unsigned long search,lower,upper;
2637 struct Instance *ptr;
2638 lower = 1;
2639 upper = gl_length(g_simulation_list);
2640 while(lower<=upper){
2641 search = (lower+upper)/2;
2642 ptr = (struct Instance *)gl_fetch(g_simulation_list,search);
2643 switch(Defmycmp(str,SCP(GetSimulationName(ptr)))){
2644 case 0:
2645 do {
2646 if (++search > upper) return upper;
2647 ptr = (struct Instance *)gl_fetch(g_simulation_list,search);
2648 } while (Defmycmp(str,SCP(GetSimulationName(ptr)))==0);
2649 return search - 1;
2650 case -1: upper = search - 1; break;
2651 case 1: lower = search + 1; break;
2652 }
2653 }
2654 return upper;
2655 }
2656
2657 static
2658 unsigned long FindSimGLB(char *str)
2659 {
2660 unsigned long search,lower,upper;
2661 struct Instance *ptr;
2662 lower = 1;
2663 upper = gl_length(g_simulation_list);
2664 while(lower<=upper){
2665 search = (lower+upper)/2;
2666 ptr = (struct Instance *)gl_fetch(g_simulation_list,search);
2667 switch(Defmycmp(str,SCP(GetSimulationName(ptr)))){
2668 case 0:
2669 do {
2670 if (--search < lower) return lower;
2671 ptr = (struct Instance *)gl_fetch(g_simulation_list,search);
2672 } while (Defmycmp(str,SCP(GetSimulationName(ptr)))==0);
2673 return search + 1;
2674 case -1: upper = search - 1; break;
2675 case 1: lower = search + 1; break;
2676 }
2677 }
2678 return lower;
2679 }
2680
2681 static
2682 unsigned long InstanceGLB(struct Instance *i, struct InstanceName name)
2683 {
2684 unsigned long search,lower,upper;
2685 struct InstanceName testname;
2686 lower = 1;
2687 upper = NumberChildren(i);
2688 while (lower <= upper){
2689 search = (lower+upper)/2;
2690 testname = ChildName(i,search);
2691 switch(CmpName(name,testname)){
2692 case 0:
2693 do {
2694 if (--search < lower) return lower;
2695 testname = ChildName(i,search);
2696 } while (CmpName(name,testname)==0);
2697 return search + 1;
2698 case -1: upper = search - 1; break;
2699 case 1: lower = search + 1; break;
2700 }
2701 }
2702 return lower;
2703 }
2704
2705 static
2706 unsigned long InstanceLUB(struct Instance *i, struct InstanceName name)
2707 {
2708 unsigned long search,lower,upper;
2709 struct InstanceName testname;
2710 lower = 1;
2711 upper = NumberChildren(i);
2712 while (lower <= upper){
2713 search = (lower+upper)/2;
2714 testname = ChildName(i,search);
2715 switch(CmpName(name,testname)){
2716 case 0:
2717 do {
2718 if (++search > upper) return upper;
2719 testname = ChildName(i,search);
2720 } while (CmpName(name,testname)==0);
2721 return search - 1;
2722 case -1: upper = search - 1; break;
2723 case 1: lower = search + 1; break;
2724 }
2725 }
2726 return upper;
2727
2728 }
2729
2730 static
2731 void LimitInstance(unsigned long int *l, unsigned long int *u, char *str)
2732 {
2733 char part[MAXID];
2734 register char *ptr1,*ptr2;
2735 struct InstanceName name;
2736 GetLastPart(str,part);
2737 if (g_search_inst){
2738 switch(InstanceKind(g_search_inst)){
2739 case MODEL_INST:
2740 case REAL_ATOM_INST:
2741 case BOOLEAN_ATOM_INST:
2742 case INTEGER_ATOM_INST:
2743 case SET_ATOM_INST:
2744 case SYMBOL_ATOM_INST:
2745 case REL_INST:
2746 case LREL_INST:
2747 case WHEN_INST:
2748 assert(*part != '\0');
2749 SetInstanceNameType(name,StrName);
2750 SetInstanceNameStrPtr(name,AddSymbol(part));
2751 *l = InstanceGLB(g_search_inst,name);
2752 *u = InstanceLUB(g_search_inst,name);
2753 break;
2754 case ARRAY_INT_INST:
2755 *l = 1;
2756 *u = NumberChildren(g_search_inst);
2757 break;
2758 case ARRAY_ENUM_INST:
2759 ptr1 = part;
2760 while((*ptr1 != '\0')&&(*ptr1 != '\'')) ptr1++;
2761 if (*ptr1 == '\0'){
2762 *l = 1;
2763 *u = NumberChildren(g_search_inst);
2764 } else{
2765 ptr2 = ptr1++;
2766 while ((*ptr2 != '\0')&&(*ptr2 != '\'')) ptr2++;
2767 if (*ptr2 == '\'') *ptr2 = '\0';
2768 SetInstanceNameType(name,StrArrayIndex);
2769 SetInstanceNameStrIndex(name,AddSymbol(ptr1));
2770 *l = InstanceGLB(g_search_inst,name);
2771 *u = InstanceLUB(g_search_inst,name);
2772 }
2773 break;
2774 case REAL_CONSTANT_INST:
2775 case INTEGER_CONSTANT_INST:
2776 case BOOLEAN_CONSTANT_INST:
2777 case SYMBOL_CONSTANT_INST:
2778 case REAL_INST:
2779 case INTEGER_INST:
2780 case BOOLEAN_INST:
2781 case SET_INST:
2782 case DUMMY_INST:
2783 case SYMBOL_INST:
2784 *l = 1;
2785 *u = 0;
2786 break;
2787 default:
2788 break;
2789 }
2790 } else{ /* search simulation list */
2791 *l = FindSimGLB(part);
2792 *u = FindSimLUB(part);
2793 }
2794 }
2795
2796 static
2797 void FindInstanceLimits(unsigned long int *l,
2798 unsigned long int *u,
2799 char *str, int length)
2800 {
2801 char temp[MAXID];
2802 if (length){
2803 SearchStr(str,temp);
2804 if (*temp != '\0')
2805 LimitInstance(l,u,str);
2806 else{
2807 assert(g_search_inst);
2808 *l = 1;
2809 *u = NumberChildren(g_search_inst);
2810 }
2811 }
2812 else{
2813 open_bracket = 0;
2814 open_quote = 0;
2815 if ((g_search_inst = g_root)){
2816 *l = 1;
2817 *u = NumberChildren(g_search_inst);
2818 }
2819 else{
2820 *l = 1;
2821 *u = gl_length(g_simulation_list);
2822 }
2823 }
2824 }
2825
2826 static
2827 struct Instance *InstanceComplete(char *str)
2828 {
2829 char temp[MAXID];
2830 symchar *symbol;
2831 int length;
2832 unsigned long pos;
2833 struct InstanceName name;
2834 length = strlen(str);
2835 switch(str[length-1]){
2836 case '.': return NULL;
2837 case '[': return NULL;
2838 case '\'': return NULL;
2839 case ']':
2840 return g_search_inst;
2841 default:
2842 GetLastPart(str,temp);
2843 if (*temp == '[') return NULL;
2844 if (g_search_inst){
2845 SetInstanceNameType(name,StrName);
2846 symbol = AddSymbol(temp);
2847 SetInstanceNameStrPtr(name,symbol);
2848 if ((pos = ChildSearch(g_search_inst,&name))) {
2849 return InstanceChild(g_search_inst,pos);
2850 } else {
2851 return NULL;
2852 }
2853 }
2854 else return FindSimulationRoot(temp);
2855 }
2856 }
2857
2858 static
2859 void WritePossibleInstances(unsigned long int l, unsigned long int u)
2860 {
2861 struct InstanceName name;
2862 struct Instance *ptr;
2863 if (g_search_inst){
2864 while(l <= u){
2865 name = ChildName(g_search_inst,l++);
2866 switch(InstanceNameType(name)){
2867 case IntArrayIndex:
2868 PRINTF("[%ld]\n",InstanceIntIndex(name));
2869 break;
2870 case StrArrayIndex:
2871 PRINTF("['%s']\n",InstanceStrIndex(name));
2872 break;
2873 case StrName:
2874 PRINTF("%s\n",InstanceNameStr(name));
2875 break;
2876 }
2877 }
2878 }
2879 else{
2880 while(l<=u){
2881 ptr = (struct Instance *)gl_fetch(g_simulation_list,l++);
2882 PRINTF("%s\n",GetSimulationName(ptr));
2883 }
2884 }
2885 }
2886
2887 static
2888 void CompleteInstance(unsigned long int *l,
2889 unsigned long int *u,
2890 char *str, int *pos)
2891 {
2892 register unsigned long c;
2893 register CONST char *str1;
2894 register int count,oldlen;
2895 if (g_search_inst){
2896 struct InstanceName name;
2897 char part[MAXID];
2898 GetLastPart(str,part);
2899 switch(InstanceKind(g_search_inst)) {
2900 case MODEL_INST:
2901 case REAL_ATOM_INST:
2902 case BOOLEAN_ATOM_INST:
2903 case INTEGER_ATOM_INST:
2904 case SET_ATOM_INST:
2905 case SYMBOL_ATOM_INST:
2906 case REL_INST:
2907 case LREL_INST:
2908 name = ChildName(g_search_inst,*l);
2909 assert(InstanceNameType(name) == StrName);
2910 str1 = SCP(InstanceNameStr(name));
2911 count = strlen(str1);
2912 for(c = *l + 1; c <= *u;c++){
2913 name = ChildName(g_search_inst,c);
2914 assert(InstanceNameType(name) == StrName);
2915 count = DefNumberMatch(str1,SCP(InstanceNameStr(name)),count);
2916 }
2917 oldlen = strlen(part);
2918 while (oldlen < count)
2919 str[(*pos)++] = str1[oldlen++];
2920 str[*pos] = '\0';
2921 break;
2922 case ARRAY_INT_INST:
2923 break;
2924 case ARRAY_ENUM_INST:
2925 name = ChildName(g_search_inst,*l);
2926 assert(InstanceNameType(name) == StrArrayIndex);
2927 str1 = SCP(InstanceStrIndex(name));
2928 count = strlen(str1);
2929 for(c = *l + 1; c <= *u;c++){
2930 name = ChildName(g_search_inst,c);
2931 assert(InstanceNameType(name) == StrArrayIndex);
2932 count = DefNumberMatch(str1,SCP(InstanceStrIndex(name)),count);
2933 }
2934 oldlen = strlen(part);
2935 if (oldlen == 0){
2936 str[(*pos)++] = '[';
2937 str[(*pos)++] = '\'';
2938 } else if (oldlen == 1){
2939 str[(*pos)++] = '\'';
2940 oldlen = 0;
2941 }
2942 else oldlen -= 2;
2943 while (oldlen < count)
2944 str[(*pos)++] = str1[oldlen++];
2945 str[*pos] = '\0';
2946 break;
2947 default:
2948 Asc_Panic(2, NULL, "Bad instance type.\n");
2949 }
2950 }
2951 else{
2952 register struct Instance *ptr;
2953 ptr = (struct Instance *)gl_fetch(g_simulation_list,*l);
2954 str1 = SCP(GetSimulationName(ptr));
2955 count = strlen(str1);
2956 for(c = *l + 1;c <= *u;c++){
2957 ptr = (struct Instance *)gl_fetch(g_simulation_list,c);
2958 count = DefNumberMatch(str1,SCP(GetSimulationName(ptr)),count);
2959 }
2960 if (count > *pos) {
2961 strncpy(str,str1,count);
2962 str[count] = '\0';
2963 *pos = count;
2964 }
2965 }
2966 }
2967
2968 static
2969 int CheckClosingQuote(struct Instance *i, char *str, int pos)
2970 {
2971 char temp[MAXID];
2972 register int open_pos=pos,c=0;
2973 struct InstanceName name;
2974 /* find the str */
2975 while(str[--open_pos] != '\'');
2976 while(open_pos <= pos)
2977 temp[c++] = str[++open_pos];
2978 SetInstanceNameType(name,StrArrayIndex);
2979 SetInstanceNameStrIndex(name,AddSymbol(temp));
2980 if (ChildSearch(i,&name)) return 1;
2981 else return 0;
2982 }
2983
2984 static
2985 int CheckOpeningBracket(char *str, int pos)
2986 {
2987 char temp[MAXID];
2988 register int start=pos-1,c=0;
2989 struct Instance *inst;
2990 register unsigned long childpos;
2991 struct InstanceName name;
2992 if (pos==0){
2993 if (g_search_inst &&
2994 ((InstanceKind(g_search_inst)==ARRAY_INT_INST)||
2995 (InstanceKind(g_search_inst)==ARRAY_ENUM_INST)))
2996 return 1;
2997 return 0;
2998 }
2999 if ((str[start] == ']')&&
3000 ((InstanceKind(g_search_inst)==ARRAY_INT_INST)||
3001 (InstanceKind(g_search_inst)==ARRAY_ENUM_INST)))
3002 return 1;
3003 while((start>=0)&&(str[start] != '.')&&(str[start] != ']')) start--;
3004 while(start <= pos)
3005 temp[c++] = str[++start];
3006 if (g_search_inst){
3007 SetInstanceNameType(name,StrName);
3008 SetInstanceNameStrPtr(name,AddSymbol(temp));
3009 if ((childpos = ChildSearch(g_search_inst,&name))){
3010 inst = InstanceChild(g_search_inst,childpos);
3011 if ((InstanceKind(inst)==ARRAY_INT_INST)||
3012 (InstanceKind(inst)==ARRAY_ENUM_INST)){
3013 g_search_inst = inst;
3014 return 1;
3015 }
3016 }
3017 return 0;
3018 }
3019 else{
3020 register unsigned long search,lower,upper;
3021 register struct Instance *ptr;
3022 register int cmp;
3023 lower = 1;
3024 upper = gl_length(g_simulation_list);
3025 while(lower<=upper){
3026 search = (lower+upper)/2;
3027 ptr = (struct Instance *)gl_fetch(g_simulation_list,search);
3028 if ((cmp = strcmp(temp,SCP(GetSimulationName(ptr))))==0){
3029 inst = GetSimulationRoot(ptr);
3030 if ((InstanceKind(inst)==ARRAY_INT_INST)||
3031 (InstanceKind(inst)==ARRAY_ENUM_INST)){
3032 g_search_inst = inst;
3033 return 1;
3034 }
3035 return 0;
3036 }
3037 else if (cmp > 0) lower = search + 1;
3038 else upper = search - 1;
3039 }
3040 return 0;
3041 }
3042 }
3043
3044 static
3045 int CheckClosingPeriod(char *str, int pos)
3046 {
3047 char temp[MAXID];
3048 register int start=pos-1,c=0;
3049 struct Instance *inst;
3050 register unsigned long childpos;
3051 struct InstanceName name;
3052 if (str[start] == ']'){
3053 switch(InstanceKind(g_search_inst)) {
3054 case MODEL_INST:
3055 case REAL_ATOM_INST:
3056 case BOOLEAN_ATOM_INST:
3057 case INTEGER_ATOM_INST:
3058 case SET_ATOM_INST:
3059 case SYMBOL_ATOM_INST:
3060 case REL_INST:
3061 case LREL_INST:
3062 /* instances w/out children don't go here */
3063 return 1;
3064 default:
3065 break;
3066 }
3067 return 0;
3068 }
3069 while((start>=0)&&(str[start] != '.')&&(str[start] != ']')) start--;
3070 while(start <= pos)
3071 temp[c++] = str[++start];
3072 if (g_search_inst){
3073 SetInstanceNameType(name,StrName);
3074 SetInstanceNameStrPtr(name,AddSymbol(temp));
3075 if ((childpos = ChildSearch(g_search_inst,&name))){
3076 inst = InstanceChild(g_search_inst,childpos);
3077 switch(InstanceKind(inst)) {
3078 case MODEL_INST:
3079 case REAL_ATOM_INST:
3080 case BOOLEAN_ATOM_INST:
3081 case INTEGER_ATOM_INST:
3082 case SET_ATOM_INST:
3083 case SYMBOL_ATOM_INST:
3084 case REL_INST:
3085 case LREL_INST:
3086 /* instances w/out children don't go here */
3087 g_search_inst = inst;
3088 return 1;
3089 default:
3090 break;
3091 }
3092 }
3093 return 0;
3094 }
3095 else{
3096 register int cmp;
3097 register unsigned long search,lower,upper;
3098 register struct Instance *ptr;
3099 lower = 1;
3100 upper = gl_length(g_simulation_list);
3101 while(lower<=upper){
3102 search = (lower+upper)/2;
3103 ptr = (struct Instance *)gl_fetch(g_simulation_list,search);
3104 if ((cmp = strcmp(temp,SCP(GetSimulationName(ptr))))==0){
3105 g_search_inst = GetSimulationRoot(ptr);
3106 return 1;
3107 }
3108 else if (cmp > 0) lower = search + 1;
3109 else upper = search - 1;
3110 }
3111 return 0;
3112 }
3113 }
3114
3115 static
3116 int IsIntegerStr(register char *str)
3117 {
3118 register int started=0;
3119 while(*str != '\0'){
3120 if(isdigit(*str)) started = 1;
3121 else
3122 if ((!started)&&((*str == '+')||(*str == '-'))) started = 1;
3123 else return 0;
3124 str++;
3125 }
3126 return started;
3127 }
3128
3129 static
3130 int CheckClosingBracket(char *str, int pos)
3131 {
3132 char temp[MAXID];
3133 register int start=pos-1,c=0;
3134 register unsigned long childpos;
3135 struct InstanceName name;
3136 if (str[start] == '\''){
3137 pos = --start;
3138 while(str[start] != '\'') start--;
3139 while(start < pos)
3140 temp[c++] = str[++start];
3141 temp[c] = '\0';
3142 SetInstanceNameType(name,StrArrayIndex);
3143 SetInstanceNameStrIndex(name,AddSymbol(temp));
3144 if ((childpos = ChildSearch(g_search_inst,&name))){
3145 g_search_inst = InstanceChild(g_search_inst,childpos);
3146 return 1;
3147 }
3148 }
3149 else{ /* integer index */
3150 while((str[start] != '[')) start--;
3151 while (start <= pos)
3152 temp[c++] = str[++start];
3153 if (IsIntegerStr(temp)){
3154 SetInstanceNameType(name,IntArrayIndex);
3155 SetInstanceNameIntIndex(name,atol(temp));
3156 if ((childpos = ChildSearch(g_search_inst,&name))){
3157 g_search_inst = InstanceChild(g_search_inst,childpos);
3158 return 1;
3159 }
3160 }
3161 }
3162 return 0;
3163 }
3164
3165 static
3166 void AddInstanceChar(char c,
3167 unsigned long int *l,
3168 unsigned long int *u,
3169 char *line, int *gpos, char *piece,
3170 int *pos, int *state,
3171 int *errloc, int *done,
3172 union argument *args, int *argc,
3173 unsigned long int com)
3174 {
3175 int oldpos;
3176 *done = 0;
3177 if ((*l == 0)&&(*u==0)){
3178 FindInstanceLimits(l,u,piece,*pos);
3179 }
3180 if (isprint(c)){
3181 switch(c){
3182 case ' ':
3183 if (*pos){
3184 if (InstanceComplete(piece)){
3185 putchar(' ');
3186 line[(*gpos)++] = ' ';
3187 args[(*argc)++].i = InstanceComplete(piece);
3188 *done = 1;
3189 ClearPiece(piece,pos);
3190 }
3191 else{
3192 oldpos = *pos;
3193 CompleteInstance(l,u,piece,pos);
3194 while(oldpos < *pos){
3195 putchar(piece[oldpos]);
3196 line[(*gpos)++] = piece[oldpos++];
3197 }
3198 }
3199 }
3200 else{
3201 line[(*gpos)++] = c;
3202 putchar(c);
3203 }
3204 break;
3205 case '?':
3206 putchar('\n');
3207 WritePossibleInstances(*l,*u);
3208 RePrint(line);
3209 break;
3210 case '[': /* check opening bracket */
3211 line[(*gpos)++] = c;
3212 putchar(c);
3213 if ((!(open_bracket)) &&
3214 CheckOpeningBracket(piece,*pos)){
3215 open_bracket++;
3216 piece[(*pos)++] = c;
3217 *l = 1;
3218 *u = NumberChildren(g_search_inst);
3219 }
3220 else{
3221 *l = *u = 0;
3222 Bell();
3223 *errloc = *gpos - 1;
3224 *state = 5;
3225 }
3226 break;
3227 case '\'': /* check end or beginning quote */
3228 line[(*gpos)++] = c;
3229 putchar(c);
3230 if (open_quote){
3231 if (CheckClosingQuote(g_search_inst,piece,*pos)){
3232 piece[(*pos)++] = c;
3233 open_quote--;
3234 }
3235 else{
3236 *l = *u = 0;
3237 Bell();
3238 *errloc = *gpos -1;
3239 *state = 5;
3240 }
3241 }
3242 else{
3243 if (g_search_inst && (InstanceKind(g_search_inst) == ARRAY_ENUM_INST)){
3244 open_quote++;
3245 piece[(*pos)++] = c;
3246 }
3247 else{
3248 *l = *u = 0;
3249 Bell();
3250 *errloc = *gpos - 1;
3251 *state = 5;