Fawkes API  Fawkes Development Version
mod_utils.cpp
1 
2 /***************************************************************************
3  * mod_utils.cpp - OpenPRS general utils module
4  *
5  * Created: Fri Aug 29 19:37:19 2014
6  * Copyright 2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "mod_utils.h"
23 #include <default-hook.h>
24 #include <oprs-rerror_f-pub.h>
25 
26 /// @cond external
27 extern "C" {
28  typedef struct op_structure Op_Structure;
29 
30  typedef enum {IS_ACTIVE, IS_SLEEPING, IS_SUSP_ACTIVE, IS_SUSP_SLEEPING} Intention_Status;
31  typedef Slist *Thread_Intention_Block_List;
32  typedef Slist *Condition_List;
33  struct intention {
34  Fact * fact;
35  Goal * goal;
36  Thread_Intention_Block_List fils;
37  Thread_Intention_Block_List active_tibs;
38  Op_Instance *top_op;
39  short priority;
40  Intention_Status status;
41  Symbol id;
42  Thread_Intention_Block *critical_section;
43  PDate creation;
44  Condition_List activation_conditions_list;
45  Sprinter *failed_goal_sprinter;
46  OPRS_LIST failed_goal_stack;
47  };
48 }
49 /// @endcond
50 
51 #include <op-instance_f-pub.h>
52 #include <op-structure_f-pub.h>
53 #include <lisp-list_f-pub.h>
54 
55 extern "C"
56 Term *
57 func_op_name(TermList terms)
58 {
59  Term *op_t;
60  Op_Instance *opi;
61 
62  op_t = (Term *)get_list_pos(terms, 1);
63  if (! op_t ) {
64  report_fatal_external_error((char *)"Cannot retrieve OP instance term");
65  }
66  if (op_t->type != TT_OP_INSTANCE) {
67  report_fatal_external_error((char *)"Term is not of type OP_INSTANCE");
68  }
69 
70  opi = (Op_Instance *)(op_t->u.opi);
71  Op_Structure *op_s = op_instance_op(opi);
72  if (! op_s) {
73  report_fatal_external_error((char *)"Failed to get OP structure from OP instance");
74  }
75 
76  return build_id(op_name(op_s));
77 }
78 
79 
80 extern "C"
81 Term *
82 func_op_names(TermList terms)
83 {
84  Term *ops_t;
85 
86  ops_t = (Term *)get_list_pos(terms, 1);
87  if (! ops_t ) {
88  report_fatal_external_error((char *)"Cannot retrieve OP instance term");
89  }
90  if (ops_t->type != LISP_LIST) {
91  report_fatal_external_error((char *)"Term is not of type LISP_LIST");
92  }
93 
94  TermList name_list = sl_make_slist();
95 
96  for (L_List p_l = ops_t->u.l_list; p_l; p_l = l_cdr(p_l)) {
97  Term *t = l_car(p_l);
98  if (t->type == TT_INTENTION) {
99  Op_Instance *opi = (Op_Instance *)(t->u.in->top_op);
100  Op_Structure *op_s = op_instance_op(opi);
101  name_list = build_term_list(name_list, build_id(op_name(op_s)));
102  } else if (t->type == TT_OP_INSTANCE) {
103  Op_Instance *opi = (Op_Instance *)(t->u.opi);
104  Op_Structure *op_s = op_instance_op(opi);
105  if (! op_s) {
106  name_list = build_term_list(name_list, build_id(declare_atom("NOT-AN-OP")));
107  } else {
108  name_list = build_term_list(name_list, build_id(op_name(op_s)));
109  }
110  } else {
111  name_list = build_term_list(name_list, build_id(declare_atom("NOT-AN-OP-INSTANCE")));
112  }
113  }
114 
115  return build_term_l_list_from_c_list(name_list);
116 }
117 
118 
119 /** Entry function for the OpenPRS module. */
120 extern "C"
121 void init()
122 {
123  printf("*** LOADING mod_utils\n");
124  make_and_declare_eval_funct("op-name", func_op_name, 1);
125  make_and_declare_eval_funct("op-names", func_op_names, 1);
126 
127  const char *gdb_delay_env = getenv("FAWKES_OPRS_GDB_DELAY");
128  if (gdb_delay_env && strcmp(gdb_delay_env, "true") == 0) {
129  fprintf(stderr,
130  "\n============================================================================\n\n"
131  "GDB delay enabled. Waiting for 10 seconds. Connect with GDB using:\n\n"
132  "gdb -p %i\n\n"
133  "============================================================================\n\n",
134  getpid());
135  sleep(10);
136  }
137 }