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
24#include <default-hook.h>
25#include <oprs-rerror_f-pub.h>
26
27/// @cond external
28extern "C" {
29typedef struct op_structure Op_Structure;
30
31typedef enum { IS_ACTIVE, IS_SLEEPING, IS_SUSP_ACTIVE, IS_SUSP_SLEEPING } Intention_Status;
32typedef Slist *Thread_Intention_Block_List;
33typedef Slist *Condition_List;
34struct intention
35{
36 // cppcheck-suppress unusedStructMember
37 Fact *fact;
38 // cppcheck-suppress unusedStructMember
39 Goal *goal;
40 // cppcheck-suppress unusedStructMember
41 Thread_Intention_Block_List fils;
42 // cppcheck-suppress unusedStructMember
43 Thread_Intention_Block_List active_tibs;
44 Op_Instance * top_op;
45 // cppcheck-suppress unusedStructMember
46 short priority;
47 Intention_Status status;
48 Symbol id;
49 // cppcheck-suppress unusedStructMember
50 Thread_Intention_Block *critical_section;
51 PDate creation;
52 // cppcheck-suppress unusedStructMember
53 Condition_List activation_conditions_list;
54 // cppcheck-suppress unusedStructMember
55 Sprinter *failed_goal_sprinter;
56 OPRS_LIST failed_goal_stack;
57};
58}
59/// @endcond
60
61#include <lisp-list_f-pub.h>
62#include <op-instance_f-pub.h>
63#include <op-structure_f-pub.h>
64
65extern "C" Term *
66func_op_name(TermList terms)
67{
68 Term * op_t;
69 Op_Instance *opi;
70
71 op_t = (Term *)get_list_pos(terms, 1);
72 if (!op_t) {
73 report_fatal_external_error((char *)"Cannot retrieve OP instance term");
74 }
75 if (op_t->type != TT_OP_INSTANCE) {
76 report_fatal_external_error((char *)"Term is not of type OP_INSTANCE");
77 }
78
79 opi = (Op_Instance *)(op_t->u.opi);
80 Op_Structure *op_s = op_instance_op(opi);
81 if (!op_s) {
82 report_fatal_external_error((char *)"Failed to get OP structure from OP instance");
83 }
84
85 return build_id(op_name(op_s));
86}
87
88extern "C" Term *
89func_op_names(TermList terms)
90{
91 Term *ops_t;
92
93 ops_t = (Term *)get_list_pos(terms, 1);
94 if (!ops_t) {
95 report_fatal_external_error((char *)"Cannot retrieve OP instance term");
96 }
97 if (ops_t->type != LISP_LIST) {
98 report_fatal_external_error((char *)"Term is not of type LISP_LIST");
99 }
100
101 TermList name_list = sl_make_slist();
102
103 for (L_List p_l = ops_t->u.l_list; p_l; p_l = l_cdr(p_l)) {
104 Term *t = l_car(p_l);
105 if (t->type == TT_INTENTION) {
106 Op_Instance * opi = (Op_Instance *)(t->u.in->top_op);
107 Op_Structure *op_s = op_instance_op(opi);
108 name_list = build_term_list(name_list, build_id(op_name(op_s)));
109 } else if (t->type == TT_OP_INSTANCE) {
110 Op_Instance * opi = (Op_Instance *)(t->u.opi);
111 Op_Structure *op_s = op_instance_op(opi);
112 if (!op_s) {
113 name_list = build_term_list(name_list, build_id(declare_atom("NOT-AN-OP")));
114 } else {
115 name_list = build_term_list(name_list, build_id(op_name(op_s)));
116 }
117 } else {
118 name_list = build_term_list(name_list, build_id(declare_atom("NOT-AN-OP-INSTANCE")));
119 }
120 }
121
122 return build_term_l_list_from_c_list(name_list);
123}
124
125/** Entry function for the OpenPRS module. */
126extern "C" void
127init()
128{
129 printf("*** LOADING mod_utils\n");
130 make_and_declare_eval_funct("op-name", func_op_name, 1);
131 make_and_declare_eval_funct("op-names", func_op_names, 1);
132
133 const char *gdb_delay_env = getenv("FAWKES_OPRS_GDB_DELAY");
134 if (gdb_delay_env && strcmp(gdb_delay_env, "true") == 0) {
135 fprintf(stderr,
136 "\n============================================================================\n\n"
137 "GDB delay enabled. Waiting for 10 seconds. Connect with GDB using:\n\n"
138 "gdb -p %i\n\n"
139 "============================================================================\n\n",
140 getpid());
141 sleep(10);
142 }
143}
Fact representation for JSON transfer.
Definition: Fact.h:30
Goal representation for JSON transfer.
Definition: Goal.h:28