Wild Life  2.30
 All Data Structures Files Functions Variables Typedefs Macros
lib.c
Go to the documentation of this file.
1 
7 /* Copyright 1991 Digital Equipment Corporation.
8  ** All Rights Reserved.
9  *****************************************************************/
10 
11 /* VERSION of Wild-LIFE for calling from C */
12 /* RM: Mar 31 1993 */
13 
14 #include "defs.h"
15 
16 #ifdef SOLARIS
17 #include <stdlib.h>
18 static unsigned int libseed;
19 #endif
20 
21 long noisy=TRUE;
22 long file_date=3;
24 
25 float garbage_time=0;
26 
34 char **group_features(char **f,ptr_node n)
35 {
36  *f=NULL;
37  if(n) {
38  if(n->left)
39  f=group_features(f,n->left);
40  *f=n->key;
41  f++;
42  if(n->right)
43  f=group_features(f,n->right);
44  }
45 
46  return f;
47 }
48 
55 void exit_if_true(long exitflag)
56 {
57  if (exitflag) {
58  printf("\n\n*** Execution is not allowed to continue.\n");
59  /*exit_life(TRUE);*/
60  exit(EXIT_FAILURE);
61  }
62 }
63 
69 void init_io()
70 {
74  output_stream=stdout;
75 }
76 
84 {
85 #ifdef X11
87 #endif
91  undo_stack=NULL; /* 7.8 */
92  var_tree=NULL;
93 
94  /* RM: Oct 13 1993 */
96  prompt=PROMPT;
97  else {
100  }
101 
102  resid_aim=NULL;
104 
105 #ifdef X11
106  /* RM: Dec 15 1992 */
108 #endif
109 
110  init_global_vars(); /* RM: Feb 15 1993 */
111 }
112 
122 void WFInit(long argc, char *argv[])
123 {
124  ptr_stack save_undo_stack;
125 
126  int i;
127 #ifdef SOLARIS
128  for(i=0;i<256;i++)
129  rand_array[i]=rand_r(&libseed);
130 #else
131  for(i=0;i<256;i++)
132  rand_array[i]=random();
133 #endif
134 
135  if (argc < 10)
136  {
137  arg_c=argc;
138  for (i = 0; i < argc; i++) {
139  arg_v[i]=argv[i];
140  }
141  }
142  else
143  {
144  Errorline("Too many command line arguments\n");
145  }
146 
147  quietflag = TRUE; /* RM: Mar 31 1993 */
148 
149  init_io();
150  init_memory();
152  assert(stack_pointer==mem_base); /* 8.10 */
153  init_copy();
154  assert(stack_pointer==mem_base); /* 8.10 */
155  init_print();
156  assert(stack_pointer==mem_base); /* 8.10 */
157 
158  /* Timekeeping initialization */
159  tzset();
160  (void)times(&life_start);
161  assert(stack_pointer==mem_base); /* 8.10 */
162 
163  init_modules(); /* RM: Jan 8 1993 */
164 
166  assert(stack_pointer==mem_base); /* 8.10 */
167 #ifdef X11
169  assert(stack_pointer==mem_base); /* 8.10 */
170 #endif
171  init_interrupt();
172  assert(stack_pointer==mem_base); /* 8.10 */
173  title();
174  assert(stack_pointer==mem_base); /* 8.10 */
175  init_trace();
176  noisy=FALSE;
177 
178  assert(stack_pointer==mem_base); /* 8.10 */
179 
180 
181  (void)set_current_module(user_module); /* RM: Jan 27 1993 */
182 
183  /* Read in the .set_up file */
184  init_system();
185 
186 #ifdef ARITY /* RM: Mar 29 1993 */
187  arity_init();
188 #endif
189 
190 
191  (void)open_input_file("+SETUP+");
193  file_date+=2;
194  main_prove();
195 
196 
197  (void)setjmp(env);
198  /* printf("%ld\n",(long)(stack_pointer-mem_base)); */ /* 8.10 */
199  init_system();
200  init_trace();
203  save_undo_stack=undo_stack;
204  stdin_cleareof();
205 
206  c_query_level=0;
207 }
208 
215 int WFInput(char *query)
216 {
217  ptr_psi_term t;
218  long sort;
219  parse_block pb;
220  int result=WFno;
221  ptr_stack save_undo_stack;
222  ptr_choice_point old_choice;
223 
224 
225  save_undo_stack=undo_stack;
226  old_choice=choice_stack;
227 
228 
229  if(!strcmp(query,".")) {
230  reset_stacks();
231  result=WFyes;
232  c_query_level=0;
233  }
234  else {
235  if(!strcmp(query,";")) {
236  sort=QUERY;
238  }
239  else {
240  /* Parse the string in its own state */
241  save_parse_state(&pb);
244  stringinput=query;
245 
246  /* old_var_occurred=var_occurred; */
248  t=stack_copy_psi_term(parse(&sort));
249 
250  /* Main loop of interpreter */
251  if(sort==QUERY) {
253  goal_count=0;
254 
256  c_query_level++;
259  /* reset_step(); */
260  }
261  else if (sort==FACT) {
263  assert_clause(t);
264  if(assert_ok)
265  result=WFyes;
266  undo(save_undo_stack);
268  encode_types();
269  }
270  }
271 
272  if(sort==QUERY) {
273  start_chrono();
274  main_prove();
275 
277 
278  if((long)(goal_stack->aaaa_1)==c_query_level)
279  if(choice_stack==old_choice) {
280  result=WFyes;
281  c_query_level--;
282  }
283  else
284  result=WFmore;
285  else {
286  result=WFno;
287  c_query_level--;
288  }
289 
291  }
292  }
293  }
294 
295  return result;
296 }
297 
305 {
306  ptr_psi_term result=NULL;
307  ptr_node n;
308 
309  n=find(STRCMP,name,var_tree);
310  if(n) {
311  result=(ptr_psi_term)n->data;
312  if(result)
313  deref_ptr(result);
314  }
315 
316  return result;
317 }
318 
326 {
327  int result=0;
328 
329  if(n) {
330  if(n->left)
331  result+=WFfeature_count_loop(n->left);
332  result++;
333  if(n->right)
334  result+=WFfeature_count_loop(n->right);
335  }
336 
337  return result;
338 }
339 
347 {
348  int result=0;
349 
350  if(psi) {
351  deref_ptr(psi);
352  result=WFfeature_count_loop(psi->attr_list);
353  }
354 
355  return result;
356 }
357 
365 {
366  char *result=NULL;
367  if(psi) {
368  deref_ptr(psi);
369  result=psi->type->keyword->combined_name;
370  }
371  return result;
372 }
373 
381 {
382  char **features_loc=NULL;
383  int n;
384 
385  if(psi) {
386  deref_ptr(psi);
387 
389  if(n) {
390  features_loc=(char **)malloc((n+1)*sizeof(char *));
391  (void)group_features(features_loc,psi->attr_list);
392  }
393  }
394 
395  return features_loc;
396 }
397 
405 double WFGetDouble(ptr_psi_term psi,int *ok)
406 {
407  double value=0.0;
408 
409  if(ok)
410  *ok=FALSE;
411 
412  if(psi) {
413  deref_ptr(psi);
414 
415  if(sub_type(psi->type,real) && psi->value_3) {
416  value= *((double *)psi->value_3);
417  if(ok)
418  *ok=TRUE;
419  }
420  }
421  return value;
422 }
423 
431 char *WFGetString(ptr_psi_term psi,int *ok)
432 {
433  char *value=NULL;
434 
435  if(ok)
436  *ok=FALSE;
437 
438  if(psi) {
439  deref_ptr(psi);
440 
441  if(sub_type(psi->type,quoted_string) && psi->value_3) {
442  value=(char *)psi->value_3;
443  if(ok)
444  *ok=TRUE;
445  }
446  }
447  return value;
448 }
449 
458 {
459  ptr_psi_term result=NULL;
460  ptr_node n;
461 
462  if(psi && feature) {
463  deref_ptr(psi);
464  n=find(FEATCMP,feature,psi->attr_list);
465  if(n)
466  result=(PsiTerm)n->data;
467  }
468 
469  return result;
470 }
GENERIC stack_pointer
used to allocate from stack - size allocated added - adj for alignment
Definition: def_glob.h:69
#define prove
was enum (goal) – but must be long for error.c - now typedef
Definition: def_const.h:1051
char * WFType(ptr_psi_term psi)
WFType.
Definition: lib.c:364
ptr_psi_term aaaa_1
Definition: def_struct.h:239
void init_trace()
init_trace
Definition: error.c:651
void undo(ptr_stack limit)
undo
Definition: login.c:691
long assert_first
Definition: def_glob.h:1032
#define FEATCMP
indicates to use featcmp for comparison (in trees.c)
Definition: def_const.h:979
ptr_psi_term stdin_state
Definition: def_glob.h:857
ptr_module user_module
Default module for user input.
Definition: def_glob.h:694
char * combined_name
Definition: def_struct.h:119
ptr_goal goal_stack
Definition: def_glob.h:1025
void push_choice_point(goals t, ptr_psi_term a, ptr_psi_term b, GENERIC c)
push_choice_point
Definition: login.c:638
void push_goal(goals t, ptr_psi_term a, ptr_psi_term b, GENERIC c)
push_goal
Definition: login.c:600
psi_term parse(long *q)
parse
Definition: parser.c:907
void init_system()
init_system
Definition: lib.c:83
char prompt_buffer[PROMPT_BUFFER]
Definition: def_glob.h:878
ptr_psi_term xevent_list
Definition: def_glob.h:1037
void WFInit(long argc, char *argv[])
WFInit.
Definition: lib.c:122
ptr_module current_module
The current module for the tokenizer.
Definition: def_glob.h:729
void save_parse_state(ptr_parse_block pb)
save_parse_state
Definition: token.c:425
long types_done
Definition: lib.c:23
void init_io()
void init_io
Definition: lib.c:69
includes
#define DEFRULES
Must be different from NULL, a built-in index, and a pointer Used to indicate that the rules of the d...
Definition: def_const.h:302
#define WFmore
Input succeeded with possibly more answers.
Definition: def_const.h:47
#define FACT
Fact Kind of user input.
Definition: def_const.h:338
int WFFeatureCount(ptr_psi_term psi)
WFFeatureCount.
Definition: lib.c:346
ptr_keyword keyword
Definition: def_struct.h:147
struct wl_psi_term * ptr_psi_term
quotedStackCopy
Definition: def_struct.h:62
GENERIC data
Definition: def_struct.h:201
long quietflag
Definition: def_glob.h:912
#define NULL
Definition: def_const.h:533
ptr_node var_tree
Definition: def_glob.h:1005
#define WFno
Input failed.
Definition: def_const.h:34
void * PsiTerm
Type for psi-terms, hidden from users.
Definition: def_struct.h:21
ptr_psi_term input_state
Definition: def_glob.h:856
long ignore_eff
Definition: def_glob.h:677
#define PROMPT
Head of prompt.
Definition: def_const.h:218
#define QUERY
Query Kind of user input.
Definition: def_const.h:345
ptr_goal resid_aim
Definition: def_glob.h:865
#define WFyes
Input succeeded.
Definition: def_const.h:41
#define c_what_next
was enum (goal) – but must be long for error.c - now typedef
Definition: def_const.h:1163
long x_window_creation
Definition: def_glob.h:1046
long assert_ok
Definition: def_glob.h:1033
ptr_node left
Definition: def_struct.h:199
long sub_type(ptr_definition t1, ptr_definition t2)
sub_type
Definition: types.c:1642
ptr_definition real
symbol in bi module
Definition: def_glob.h:375
struct tms life_start
time life started - seconds
Definition: def_glob.h:83
ptr_stack undo_stack
Definition: def_glob.h:1027
void Errorline(char *format,...)
Errorline.
Definition: error.c:465
unsigned long * GENERIC
unsigned long *GENERIC
Definition: def_struct.h:35
long goal_count
Definition: def_glob.h:678
#define deref_ptr(P)
Definition: def_macro.h:100
goals type
Definition: def_struct.h:238
char * heap_copy_string(char *s)
heap_copy_string
Definition: trees.c:172
char * key
Definition: def_struct.h:198
void begin_terminal_io()
begin_terminal_io
Definition: token.c:493
int arg_c
set from argc in either life.c or lib.c
Definition: def_glob.h:20
#define TRUE
Standard boolean.
Definition: def_const.h:268
char * name
Definition: def_glob.h:966
#define STRCMP
indicates to use strcmp for comparison (c function)
Definition: def_const.h:963
int WFInput(char *query)
WFInput.
Definition: lib.c:215
GENERIC other_base
mem_size memory allocated in init_memory by malloc
Definition: def_glob.h:98
void exit_if_true(long exitflag)
exit_if_true
Definition: lib.c:55
void start_chrono()
start_chrono
Definition: login.c:349
void init_global_vars()
init_global_vars
Definition: lefun.c:1484
#define FALSE
Standard boolean.
Definition: def_const.h:275
void init_print()
init_print
Definition: print.c:52
void init_memory()
init_memory ()
Definition: memory.c:1671
GENERIC value_3
Definition: def_struct.h:186
#define fail
was enum (goal) – but must be long for error.c - now typedef
Definition: def_const.h:1044
void init_parse_state()
init_parse_state
Definition: token.c:464
ptr_psi_term stack_nil()
stack_nil
Definition: built_ins.c:26
char * module_name
Definition: def_struct.h:106
void reset_stacks()
reset_stacks
Definition: login.c:2047
long file_date
Definition: lib.c:22
ptr_node find(long comp, char *keystr, ptr_node tree)
find
Definition: trees.c:394
char * arg_v[ARGNN]
set from argv in either life.c or lib.c
Definition: def_glob.h:27
ptr_psi_term stack_copy_psi_term(psi_term t)
stack_copy_psi_term
Definition: parser.c:205
#define load
was enum (goal) – but must be long for error.c - now typedef
Definition: def_const.h:1156
char ** group_features(char **f, ptr_node n)
group_features
Definition: lib.c:34
long rand_array[256]
Definition: def_glob.h:920
double WFGetDouble(ptr_psi_term psi, int *ok)
WFGetDouble.
Definition: lib.c:405
#define PROMPT_BUFFER
Size of prompt buffer.
Definition: def_const.h:225
void encode_types()
encode_types
Definition: types.c:1091
jmp_buf env
Definition: def_glob.h:877
long stdin_terminal
set in init_io in lib.c to true - never changed - used in token.c
Definition: def_glob.h:832
void main_prove()
main_prove
Definition: login.c:2333
char * prompt
Definition: def_glob.h:1018
void x_setup_builtins()
x_setup_builtins
Definition: xpred.c:3374
void init_built_in_types()
init_built_in_types
Definition: built_ins.c:6155
long c_query_level
Definition: def_glob.h:930
FILE * output_stream
Definition: def_glob.h:1017
float garbage_time
Definition: lib.c:25
GENERIC mem_base
mem_size memory allocated in init_memory by malloc
Definition: def_glob.h:48
long stringparse
Definition: def_glob.h:859
long noisy
Definition: lib.c:21
char * stringinput
Definition: def_glob.h:860
void init_modules()
init_modules
Definition: modules.c:34
ptr_definition type
Definition: def_struct.h:181
long memory_check()
memory_check
Definition: memory.c:1723
void stdin_cleareof()
stdin_cleareof
Definition: token.c:51
PsiTerm WFGetVar(char *name)
WFGetVar.
Definition: lib.c:304
void title()
TITLE.
Definition: info.c:39
char * WFGetString(ptr_psi_term psi, int *ok)
WFGetString.
Definition: lib.c:431
void init_copy()
init_copy
Definition: copy.c:32
long var_occurred
???
Definition: def_glob.h:839
int WFfeature_count_loop(ptr_node n)
WFfeature_count_loop.
Definition: lib.c:325
ptr_node attr_list
Definition: def_struct.h:187
long open_input_file(char *file)
open_input_file
Definition: token.c:594
ptr_module set_current_module(ptr_module module)
set_current_module
Definition: modules.c:100
void assert_clause(ptr_psi_term t)
assert_clause
Definition: login.c:287
ptr_definition quoted_string
symbol in bi module
Definition: def_glob.h:368
void init_interrupt()
INIT_INTERRUPT.
Definition: interrupt.c:36
ptr_choice_point choice_stack
Definition: def_glob.h:1026
char ** WFFeatures(ptr_psi_term psi)
WFFeatures.
Definition: lib.c:380
#define assert(N)
Definition: memory.c:114
ptr_node right
Definition: def_struct.h:200
ptr_goal next
Definition: def_struct.h:242
PsiTerm WFGetFeature(ptr_psi_term psi, char *feature)
WFGetFeature.
Definition: lib.c:457