-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprbltin.c
1841 lines (1594 loc) · 55.9 KB
/
prbltin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* prbltin.c */
/* The builtin predicates are defined here.
* If you want lots of builtins then make several files that
* include prbltin.h.
*/
/* Dec 18 88 HdeF Simplified remove clause so that it expects just one
* argument.
* 12/25/91 HdeF, added repeat,gennum predicates
* 01/01/92 HdeF, added reverse_trace_mode, no_reverse_trace_mode
*/
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include "prtypes.h"
#include "prbltin.h"
#include "prlush.h"
#include "prunify.h"
#include "prcnsult.h"
#include "prmachine.h"
#include "prassert.h"
#include "pralloc.h"
#include "prerror.h"
#include "prprint.h"
#include "prlex.h"
#define ATOMORSTRING "atom or string"
#define CANTOPEN "can't open %s"
#define TOOMANYFILES "Too many open files"
extern subst_ptr_t Subst_mem; /* bottom of (global) variable bindings stack */
extern subst_ptr_t my_Subst_alloc();
extern string_ptr_t get_string();
extern atom_ptr_t Nil;
extern FILE * Curr_infile;
extern FILE * Curr_outfile;
extern node_ptr_t ND_builtin_next_nodeptr;/* from prlush.c */
static int Nbuiltins; /* not used but you could used this to keep track of
the builtins you add */
int Trace_flag; /* used by Ptrace(), Pnotrace(), lush() */
int Tracing_now;
/* This is used to test if an atom is a builtin.
* We rely on the fact that any atom less than LastBuiltin is created by
* a call to make_builtin()
*/
atom_ptr_t LastBuiltin;
CHAR getachar(); // prscan.c
void ini_parse(); // prscan.c
/****************************************************************************
make_builtin()
This associates a name used at the interpreter level with a builtin.
****************************************************************************/
void make_builtin(intfun fun,char *prolog_name)
{
atom_ptr_t atomptr, intern();
atomptr = intern(prolog_name);
ATOMPTR_BUILTIN(atomptr) = fun;
LastBuiltin = atomptr;
record_pred(atomptr);
Nbuiltins++;
}
/*****************************************************************************
nth_arg()
Returns NULL if error .
Otherwise returns the nth argument of current goal's arguments.
The return value is equal to DerefNode
Obviously one could be more efficient than here.
*****************************************************************************/
node_ptr_t nth_arg(int narg)
{
node_ptr_t rest_args;
dereference(Arguments, SubstGoal);
if(NODEPTR_TYPE(DerefNode) != PAIR)
{
return(NULL);
}
rest_args = DerefNode;
--narg;
while(narg)
{
--narg;
dereference(NODEPTR_TAIL(rest_args), DerefSubst);
if(NODEPTR_TYPE(DerefNode) != PAIR)
{
return(NULL);
}
rest_args = DerefNode;
}
dereference(NODEPTR_HEAD(rest_args), DerefSubst);
return(DerefNode);
}
/**********************************************************************
type_first_arg()
Returns true if the type of the first arg to the call is equal
to the argument of the function.
**********************************************************************/
int type_first_arg(objtype_t type)
{
dereference(Arguments, SubstGoal);
if(NODEPTR_TYPE(DerefNode) != PAIR)
return(nargerr(1));
else
dereference(NODEPTR_HEAD(DerefNode), DerefSubst);
return(NODEPTR_TYPE(DerefNode) == type);
}
/*-------------------------------------------------------------------*/
/* unify the nth argument of goal with an int of value val */
int bind_int(int narg,integer val)
{
extern subst_ptr_t Subst_mem;
node_ptr_t nodeptr, get_node();
if(!nth_arg(narg))return(nargerr(narg));
nodeptr = get_node(DYNAMIC);
NODEPTR_TYPE(nodeptr) = INT;
NODEPTR_INT(nodeptr) = val;
return(unify(DerefNode, DerefSubst, nodeptr, Subst_mem));
}
#ifdef CHARACTER
/*-------------------------------------------------------------------*/
/* unify the nth argument of goal with a char of value val */
int bind_character(int narg, uchar_t val)
{
extern subst_ptr_t Subst_mem;
node_ptr_t nodeptr, get_node();
if(!nth_arg(narg))return(nargerr(narg));
nodeptr = get_node(DYNAMIC);
NODEPTR_TYPE(nodeptr) = CHARACTER;
NODEPTR_CHARACTER(nodeptr) = val;
return(unify(DerefNode, DerefSubst, nodeptr, Subst_mem));
}
#endif
#ifdef REAL
/*-------------------------------------------------------------------*/
/* unify the nth argument of goal with a real of value val */
int bind_real(int narg, real val)
{
node_ptr_t nodeptr, get_node();
real_ptr_t realptr, get_real();
if(!nth_arg(narg))return(nargerr(narg));
nodeptr = get_node(DYNAMIC);
NODEPTR_TYPE(nodeptr) = REAL;
realptr = get_real(DYNAMIC);
*realptr = val;
NODEPTR_REALP(nodeptr) = realptr;
return(unify(DerefNode, DerefSubst, nodeptr, Subst_mem));
}
#endif
/*-------------------------------------------------------------------*/
/* unify the nth argument of goal with an int of value val */
int bind_clause(int narg, clause_ptr_t val)
{
node_ptr_t nodeptr, get_node();
extern subst_ptr_t Subst_mem;
if(!nth_arg(narg))return(nargerr(narg));
nodeptr = get_node(DYNAMIC);
NODEPTR_TYPE(nodeptr) = CLAUSE;
NODEPTR_CLAUSE(nodeptr) = val;
return(unify(DerefNode, DerefSubst, nodeptr, Subst_mem));
}
/*-------------------------------------------------------------------*/
/* unify the nth argument of goal with an atom*/
int bind_atom(int narg, atom_ptr_t atomptr)
{
extern subst_ptr_t Subst_mem;
node_ptr_t nodeptr, get_node();
if(!nth_arg(narg))return(nargerr(narg));
nodeptr = get_node(DYNAMIC);
NODEPTR_TYPE(nodeptr) = ATOM;
NODEPTR_ATOM(nodeptr) = atomptr;
return(unify(DerefNode, DerefSubst, nodeptr, Subst_mem));
}
/*-------------------------------------------------------------------*/
/* unify the nth argument of goal with a copy of the string*/
int bind_string(int narg,string_ptr_t stringptr)
{
extern subst_ptr_t Subst_mem;
node_ptr_t nodeptr, get_node();
string_ptr_t s;
if(!nth_arg(narg))return(nargerr(narg));
nodeptr = get_node(DYNAMIC);
NODEPTR_TYPE(nodeptr) = STRING;
s = get_string((my_alloc_size_t)strlen(stringptr)+1 , DYNAMIC);
strcpy(s, stringptr);
NODEPTR_STRING(nodeptr) = s;
return(unify(DerefNode, DerefSubst, nodeptr, Subst_mem));
}
/*----------------------------------------------------------------------------
The functions corresponding to the builtins are as follows.
The correct syntax for the call refers to the syntax in
prmanual.txt.
----------------------------------------------------------------------------*/
/******************************************************************************
(tell <output_file:string>)
Send output to file. Open file if not already open.
As in Edinburgh Prolog.
See Clocksin and Mellish, or Bratko for more details, or read the code!
******************************************************************************/
/* this stores the open output files */
struct named_ofile Open_ofiles[MAXOPEN];/* the value of MAXOPEN depends on the OS */
/* this stores the open input files */
struct named_ifile Open_ifiles[MAXOPEN];/* the value of MAXOPEN depends on the OS */
void ini_named_files()
{
int i;
Open_ofiles[0].o_filename = "user";
Open_ofiles[0].o_fp = stdout;
for(i = 1 ; i < MAXOPEN; i++)
{
Open_ofiles[i].o_filename = "";
Open_ofiles[i].o_fp = NULL;
}
Open_ifiles[0].i_filename = "user";
Open_ifiles[0].i_fp = stdin;
for(i = 1 ; i < MAXOPEN; i++)
{
Open_ifiles[i].i_filename = "";
Open_ifiles[i].i_fp = NULL;
}
}
int open_output(char *filename)
{
int i, unused;
FILE *ofp;
for(i = 0, unused = MAXOPEN; i < MAXOPEN; i++)
{
if(*(Open_ofiles[i].o_filename) == '\0')
unused = i;
if(!strcmp(filename, Open_ofiles[i].o_filename)){
Curr_outfile = Open_ofiles[i].o_fp;
return 1;
}
}
if(unused < MAXOPEN)
{
if((ofp = fopen(filename, "w")) == NULL)
{
sprintf(Print_buffer, CANTOPEN, filename);
errmsg(Print_buffer);
return 0;
}
else
{
Curr_outfile = ofp;
Open_ofiles[unused].o_fp = ofp;
Open_ofiles[unused].o_filename =
get_string((my_alloc_size_t)strlen(filename) + 1,
PERM_STRING);
strcpy(Open_ofiles[unused].o_filename, filename);
return 1;
}
}
else
{
errmsg(TOOMANYFILES);
return 0;
}
}
int Ptell()
{
char *filename;
ARG_STRING(1, filename);
return (open_output(filename));
}
/******************************************************************************
(telling <output_file:string>)
(telling <output_file:variable>)
As in Edinburgh Prolog.
Unifies the argument with the name of the current output_file
******************************************************************************/
char *get_output_name()
{
int i;
for(i = 0; i < MAXOPEN; i++)
{
if(Curr_outfile == Open_ofiles[i].o_fp)
return(Open_ofiles[i].o_filename);
}
INTERNAL_ERROR("telling");
return(NULL);
}
int Ptelling()
{
return(bind_string(1, get_output_name()));
}
/******************************************************************************
(told)
As in Edinburgh Prolog.
Closes current outfile.
******************************************************************************/
int close_output(FILE *ofp)
{
int i;
if (ofp == stdout)
return 1;
for(i = 1; i < MAXOPEN; i++)
{
if(Curr_outfile == Open_ofiles[i].o_fp){
fclose(Open_ofiles[i].o_fp);
Open_ofiles[i].o_fp = NULL;
Open_ofiles[i].o_filename = "";
Curr_outfile = stdout;
return 1;
}
}
INTERNAL_ERROR("close_output");
return(0);/* for lint */
}
int Ptold()
{
return(close_output(Curr_outfile));
}
/**********************************************************************
(display <anything_to_display:argument>)
(display <anything_to_display:argument> <var:output length>)
***********************************************************************/
int Pdisplay() /* display term */
{
int len;
if(!nth_arg(1))return(nargerr(1));
len = out_node(DerefNode, DerefSubst);
if(nth_arg(2)) /* o.k. this could be more efficient */
return(bind_int(2, (integer)len));
else
return(TRUE);
}
/**********************************************************************
(writes <output_string:string>)
***********************************************************************/
int Pwrites() /* write string without quotes */
{
char *s;
ARG_STRING(1, s);
pr_string(s);
return(TRUE);
}
/**********************************************************************
(put <ascii_code:integer>)
As in Edinburgh Prolog.
**********************************************************************/
int Pput()
{
integer c;
ARG_INT(1, c);
*Print_buffer = (char)c;
Print_buffer[1] = '\0';
pr_string(Print_buffer);
return(1);
}
/**********************************************************************
(nl)
As in Edinburgh Prolog.
***********************************************************************/
int Pnl() /* write newline */
{
pr_string("\n");
return(TRUE);
}
/**********************************************************************
(fail)
As in Edinburgh Prolog.
***********************************************************************/
int Pfail() /* use this to fail */
{
return(FAIL);
}
/**********************************************************************
(quit)
***********************************************************************/
int Pquit() /* leave prolog */
{
return(QUIT);
}
/**********************************************************************
(abort)
***********************************************************************/
int Pabort() /* leave prolog */
{
return(ABORT);
}
/**********************************************************************
(repeat)
This predicate succeeds and backtracks indefinitely. It is better to
define it as a builtin rather than as rules because it wont overflow
the stack this way.
*********************************************************************/
int Prepeat()
{
return (ND_SUCCESS);
}
/******************************************************************************
(gennum <variable> <limit:positive integer>)
Backtrack through the numbers from 0 to limit
******************************************************************************/
int Pgennum()
{
extern subst_ptr_t OldSubstTop , Subst_ptr;
extern node_ptr_t **OldTrailTop,**Trail_ptr;
integer limit;
node_ptr_t nodeptr,get_node();
ARG_INT(2, limit);
if(limit < 0)
return 0;
OldSubstTop = Subst_ptr;
OldTrailTop = Trail_ptr;
if(ND_builtin_next_nodeptr == NULL)/* first call */
{
nodeptr = get_node(DYNAMIC);
NODEPTR_TYPE(nodeptr)= INT;
NODEPTR_INT(nodeptr)= 0; /* initial value */
ND_builtin_next_nodeptr = nodeptr;
}
else
{
assert(NODEPTR_TYPE(ND_builtin_next_nodeptr)== INT);
}
if(NODEPTR_INT(ND_builtin_next_nodeptr)== limit)
{
return (bind_int(1, limit)); /* last possible success */
}
else
{
nodeptr = nth_arg(1);
if(NODEPTR_TYPE(nodeptr)== VAR)
{
bind_int(1, NODEPTR_INT(ND_builtin_next_nodeptr)++); /* update value for next time */
return ND_SUCCESS;
}
else
{
return (NODEPTR_INT(DerefNode)<= limit); /* in case the first arg is bound */
}
}
}
/**********************************************************************
(cut)
As in Edinburgh Prolog.
To be honest implementations of cut are never quite the same
because the behaviour of (not(not (cut))) will vary !
***********************************************************************/
int Pcut() /* infamous cut control pred */
{
do_cut();/* see prlush.c */
return(TRUE);
}
/**********************************************************************
(integer <thing_tested:argument>)
As in Edinburgh Prolog.
***********************************************************************/
int Pinteger() /* test if argument is integer */
{
return(type_first_arg(INT));
}
/**********************************************************************
(atom <thing_tested:argument>)
As in Edinburgh Prolog.
***********************************************************************/
int Patom() /* test if argument is atom */
{
return(type_first_arg(ATOM));
}
#ifdef REAL
/**********************************************************************
(real <thing_tested:argument>)
***********************************************************************/
int Preal() /* test if argument is real*/
{
return(type_first_arg(REAL));
}
#endif
/**********************************************************************
(string <thing_tested:argument>)
***********************************************************************/
int Pstring() /* test if argument is string */
{
return(type_first_arg(STRING));
}
/**********************************************************************
(var <thing_tested:argument>)
As in Edinburgh Prolog.
***********************************************************************/
int Pvar() /* test if argument is variable */
{
return(type_first_arg(VAR));
}
/**********************************************************************
(nonvar <thing_tested:argument>)
As in Edinburgh Prolog.
***********************************************************************/
int Pnonvar() /* test if argument is not variable */
{
switch(type_first_arg(VAR))
{
case 0:
return 1;
case 1:
return 0;
default:
return CRASH;
}
}
/**********************************************************************
(atomic <thing_tested:argument>)
As in Edinburgh Prolog.
***********************************************************************/
int Patomic() /* test if argument is atomic */
{
if(!nth_arg(1))return(nargerr(1));
switch(NODEPTR_TYPE(DerefNode))
{
case ATOM:
case INT:
#ifdef REAL
case REAL:
#endif
#ifdef CHARACTER
case CHARACTER:
#endif
case STRING:
return(1);
default:
return(0);
}
}
/**********************************************************************
(iplus <arg1:integer><arg2:integer><sum:argument>)
***********************************************************************/
int Piplus() /* third arg is sum of first two (integers only) */
{
integer i1, i2;
ARG_INT(1, i1);
ARG_INT(2, i2);
return(bind_int(3, i1 + i2));
}
/**********************************************************************
(iminus <arg1:integer><arg2:integer><difference:argument>)
***********************************************************************/
int Piminus() /* third arg is difference of first two (integers only) */
{
integer i1, i2;
ARG_INT(1, i1);
ARG_INT(2, i2);
return(bind_int(3, i1 - i2));
}
/**********************************************************************
(imult <arg1:integer><arg2:integer><argument>)
***********************************************************************/
int Pimult() /* third arg is product of first two (integers only) */
{
integer i1, i2;
ARG_INT(1, i1);
ARG_INT(2, i2);
return(bind_int(3, i1 * i2));
}
#ifdef REAL
/**********************************************************************
(rplus <arg1:integer><arg2:integer><argument>)
***********************************************************************/
int Prplus() /* third arg is sum of first two (reals only) */
{
real r1, r2;
ARG_REAL(1, r1);
ARG_REAL(2, r2);
return(bind_real(3, r1 + r2));
}
#endif
/**********************************************************************
(iless <arg1:integer><arg2:integer>)
***********************************************************************/
/* compares integers - you should generalise this to make it more useful */
int Piless()
{
integer i1, i2;
ARG_INT(1, i1);
ARG_INT(2, i2);
return(i1 < i2);
}
/**********************************************************************
(rless <arg1:real><arg2:real>)
***********************************************************************/
/* compares reals - you should generalise this to make it more useful */
int Prless()
{
real i1, i2;
ARG_REAL(1, i1);
ARG_REAL(2, i2);
return(i1 < i2);
}
/**********************************************************************
(ileq <arg1:integer><arg2:integer>)
***********************************************************************/
/* compares integers - you should generalise this to make it more useful */
int Pileq()
{
integer i1, i2;
ARG_INT(1, i1);
ARG_INT(2, i2);
return((i1 <= i2));
}
/**********************************************************************
imodify(<arg1:integer><arg2:integer>)
Most unlike Prolog!
***********************************************************************/
/* Lets you copy the integer value of the second argument into the first.
* You must use this with extreme restraint. It is better than
* frequently seen code of the kind
* increment_counter:-counter(N), retract(counter(N)), M is N+1, asserta(counter(M)).
* which is not efficient.
*/
int Pimodify()
{
integer i2;
ARG_INT(2, i2);
CHECK_TYPE_ARG(1, INT);/* verify only */
NODEPTR_INT(DerefNode) = i2;
return(TRUE);
}
/**********************************************************************
(see <input_file:string>)
Make <string> the current infile.
As in Edinburgh Prolog except that the argument is a string or variable.
**********************************************************************/
int open_input(char *filename)
{
int i, unused;
FILE *ifp;
for(i = 0, unused = MAXOPEN; i < MAXOPEN; i++)
{
if(*(Open_ifiles[i].i_filename) == '\0')
unused = i;
if(!strcmp(filename, Open_ifiles[i].i_filename)){
Curr_infile = Open_ifiles[i].i_fp;
return 1;
}
}
if(unused < MAXOPEN)
{
if((ifp = fopen(filename, "r")) == NULL)
{
sprintf(Print_buffer, CANTOPEN, filename);
errmsg(Print_buffer);
return 0;
}
else
{
Curr_infile = ifp;
Open_ifiles[unused].i_fp = ifp;
Open_ifiles[unused].i_filename =
get_string((my_alloc_size_t)strlen(filename) + 1,
PERM_STRING);
strcpy(Open_ifiles[unused].i_filename, filename);
return 1;
}
}
else
{
errmsg(TOOMANYFILES);
return 0;
}
}
int Psee()
{
char *filename;
ARG_STRING(1, filename);
return(open_input(filename));
}
/******************************************************************************
(seeing <output_file:string>)
(seeing <output_file:variable>)
As in Edinburgh Prolog.
Unifies the argument with the name of the current input_file
******************************************************************************/
char *get_input_name()
{
int i;
for(i = 0; i < MAXOPEN; i++)
{
if(Curr_infile == Open_ifiles[i].i_fp)
return(Open_ifiles[i].i_filename);
}
INTERNAL_ERROR("seeing");
return(NULL);
}
int Pseeing()
{
return(bind_string(1, get_input_name()));
}
/**********************************************************************
(seen)
Close current infile.
As in Edinburgh Prolog.
**********************************************************************/
int close_input(FILE *ifp)
{
int i;
if (ifp == stdin)
return 1;
for(i = 1; i < MAXOPEN; i++)
{
if(Curr_infile == Open_ifiles[i].i_fp){
fclose(Open_ifiles[i].i_fp);
Open_ifiles[i].i_fp = NULL;
Open_ifiles[i].i_filename = "";
Curr_infile = stdin;
return 1;
}
}
INTERNAL_ERROR("close_input");
return(0);/* for lint */
}
int Pseen()
{
return(close_input(Curr_infile));
}
/**********************************************************************
(get <ascii_code:argument>)
Unifies the argument with the ascii code of the next char on
Curr_infile.
As in Edinburgh Prolog.
**********************************************************************/
int Pget()
{
return(bind_int(1, (integer)getachar()));
}
/**********************************************************************
(consult <filename:atom>)
(consult <filename:string>)
As in Edinburgh Prolog (apart from consult user)
***********************************************************************/
int Pconsult() /* load file */
{
char *filename;
if(!nth_arg(1))return(nargerr(1));
if(NODEPTR_TYPE(DerefNode) == ATOM)
{
filename = NODEPTR_ATOM(DerefNode)->name;
}
else
if(NODEPTR_TYPE(DerefNode) == STRING)
{
filename = NODEPTR_STRING(DerefNode);
}
else
{
argerr(1, ATOMORSTRING);
return(CRASH);
}
if(load(filename)) /* see prconsult.c */
return(TRUE);
else
return(FALSE);
}
/**********************************************************************
(listing)
(listing <predicate:atom>)
As in Edinburgh Prolog.
***********************************************************************/
int Plisting() /* list clauses of predicate */
{
atom_ptr_t atomptr;
if(IS_NIL(Arguments))
{
do_listing();
return(TRUE);
}
else
{
ARG_ATOM(1, atomptr);
pr_packet(ATOMPTR_CLAUSE(atomptr));
return(TRUE);
}
}
#if TRACE_CAPABILITY
/**********************************************************************
(trace)
As in Edinburgh Prolog.
***********************************************************************/
int Ptrace() /* turn trace on */
{
Trace_flag = 1;
Tracing_now = 1; /* added 12/25/91 */
return(TRUE);
}
/**********************************************************************
(notrace)
As in Edinburgh Prolog.
***********************************************************************/
int Pnotrace() /* turn trace off */
{
Trace_flag = 0;
return(TRUE);
}
/**********************************************************************
reverse_trace_mode
Does not switch tracing on per se but when called all new frames
contain enough information so that reverse tracing is possible.
**********************************************************************/
int Preverse_trace()
{
extern int ReverseTraceMode;
ReverseTraceMode = 1;
return TRUE;
}
/**********************************************************************
no_reverse_trace_mode
reverts to normal execution.
**********************************************************************/
int Pno_reverse_trace()
{
extern int ReverseTraceMode;
ReverseTraceMode = 0;
return TRUE;
}
/*******************************************************************************
(suspend_trace)
Unactivate trace.
*******************************************************************************/
int Psuspend_trace()
{
Trace_flag--;
return 1;
}
/******************************************************************************
(resume_trace)
Return to trace state that existed at last call of suspend_trace
You might want to make use of statements of the form
if(Trace_flag > 1)....
******************************************************************************/
int Presume_trace()
{
Trace_flag++;
return 1;
}
/******************************************************************************
(logging <log_file:string>)
Record all screen io on a designated file.
*****************************************************************************/
FILE *Log_file;
int Plogging()
{
char *log_filename;
ARG_STRING(1, log_filename);
if((Log_file = fopen(log_filename, "w")) == NULL)
{
sprintf(Print_buffer, CANTOPEN, log_filename);
errmsg(Print_buffer);
return 0;
}
else
return 1;
}
/******************************************************************************
(nologging)
Closes the logging file, turns logging off.
*****************************************************************************/