forked from cfengine/core
/
fncall.c
341 lines (266 loc) · 8.12 KB
/
fncall.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
/*
Copyright (C) Cfengine AS
This file is part of Cfengine 3 - written and maintained by Cfengine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of Cfengine, the applicable Commerical Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
/*****************************************************************************/
/* */
/* File: fncall.c */
/* */
/* Created: Wed Aug 8 14:45:53 2007 */
/* */
/*****************************************************************************/
#include "cf3.defs.h"
#include "cf3.extern.h"
#include <assert.h>
/*******************************************************************/
int IsBuiltinFnCall(Rval rval)
{
FnCall *fp;
if (rval.rtype != CF_FNCALL)
{
return false;
}
fp = (FnCall *) rval.item;
if (FindFunction(fp->name))
{
CfDebug("%s is a builtin function\n", fp->name);
return true;
}
else
{
return false;
}
}
/*******************************************************************/
FnCall *NewFnCall(char *name, Rlist *args)
{
FnCall *fp;
CfDebug("Installing Function Call %s\n", name);
fp = xmalloc(sizeof(FnCall));
fp->name = xstrdup(name);
fp->args = args;
fp->argc = RlistLen(args);
CfDebug("Installed ");
if (DEBUG)
{
ShowFnCall(stdout, fp);
}
CfDebug("\n\n");
return fp;
}
/*******************************************************************/
FnCall *CopyFnCall(FnCall *f)
{
CfDebug("CopyFnCall()\n");
return NewFnCall(f->name, CopyRlist(f->args));
}
/*******************************************************************/
void DeleteFnCall(FnCall *fp)
{
if (fp->name)
{
free(fp->name);
}
if (fp->args)
{
DeleteRlist(fp->args);
}
free(fp);
}
/*********************************************************************/
FnCall *ExpandFnCall(char *contextid, FnCall *f, int expandnaked)
{
CfDebug("ExpandFnCall()\n");
//return NewFnCall(f->name,ExpandList(contextid,f->args,expandnaked));
return NewFnCall(f->name, ExpandList(contextid, f->args, false));
}
/*******************************************************************/
int PrintFnCall(char *buffer, int bufsize, FnCall *fp)
{
Rlist *rp;
char work[CF_MAXVARSIZE];
snprintf(buffer, bufsize, "%s(", fp->name);
for (rp = fp->args; rp != NULL; rp = rp->next)
{
switch (rp->type)
{
case CF_SCALAR:
Join(buffer, (char *) rp->item, bufsize);
break;
case CF_FNCALL:
PrintFnCall(work, CF_MAXVARSIZE, (FnCall *) rp->item);
Join(buffer, work, bufsize);
break;
default:
break;
}
if (rp->next != NULL)
{
strcat(buffer, ",");
}
}
strcat(buffer, ")");
return strlen(buffer);
}
/*******************************************************************/
void ShowFnCall(FILE *fout, FnCall *fp)
{
Rlist *rp;
if (XML)
{
fprintf(fout, "%s(", fp->name);
}
else
{
fprintf(fout, "%s(", fp->name);
}
for (rp = fp->args; rp != NULL; rp = rp->next)
{
switch (rp->type)
{
case CF_SCALAR:
fprintf(fout, "%s,", (char *) rp->item);
break;
case CF_FNCALL:
ShowFnCall(fout, (FnCall *) rp->item);
break;
default:
fprintf(fout, "(** Unknown argument **)\n");
break;
}
}
if (XML)
{
fprintf(fout, ")");
}
else
{
fprintf(fout, ")");
}
}
/*******************************************************************/
enum cfdatatype FunctionReturnType(const char *name)
{
const FnCallType *fn = FindFunction(name);
return fn ? fn->dtype : cf_notype;
}
/*******************************************************************/
FnCallResult EvaluateFunctionCall(FnCall *fp, Promise *pp)
{
Rlist *expargs;
const FnCallType *this = FindFunction(fp->name);
if (this)
{
if (DEBUG)
{
printf("EVALUATE FN CALL %s\n", fp->name);
ShowFnCall(stdout, fp);
printf("\n");
}
}
else
{
if (pp)
{
CfOut(cf_error, "", "No such FnCall \"%s()\" in promise @ %s near line %zd\n",
fp->name, pp->audit->filename, pp->offset.line);
}
else
{
CfOut(cf_error, "", "No such FnCall \"%s()\" - context info unavailable\n", fp->name);
}
return (FnCallResult) { FNCALL_FAILURE, { CopyFnCall(fp), CF_FNCALL } };
}
/* If the container classes seem not to be defined at this stage, then don't try to expand the function */
if ((pp != NULL) && !IsDefinedClass(pp->classes))
{
return (FnCallResult) { FNCALL_FAILURE, { CopyFnCall(fp), CF_FNCALL } };
}
expargs = NewExpArgs(fp, pp);
if (UnresolvedArgs(expargs))
{
DeleteExpArgs(expargs);
return (FnCallResult) { FNCALL_FAILURE, { CopyFnCall(fp), CF_FNCALL } };
}
FnCallResult result = CallFunction(this, fp, expargs);
if (result.status == FNCALL_FAILURE)
{
/* We do not assign variables to failed function calls */
DeleteExpArgs(expargs);
return (FnCallResult) { FNCALL_FAILURE, { CopyFnCall(fp), CF_FNCALL } };
}
DeleteExpArgs(expargs);
return result;
}
/*******************************************************************/
const FnCallType *FindFunction(const char *name)
{
int i;
for (i = 0; CF_FNCALL_TYPES[i].name != NULL; i++)
{
if (strcmp(CF_FNCALL_TYPES[i].name, name) == 0)
{
return CF_FNCALL_TYPES + i;
}
}
return NULL;
}
/*****************************************************************************/
void FnCallPrint(Writer *writer, FnCall *call)
{
for (Rlist *rp = call->args; rp != NULL; rp = rp->next)
{
switch (rp->type)
{
case CF_SCALAR:
WriterWriteF(writer, "%s,", (const char *) rp->item);
break;
case CF_FNCALL:
FnCallPrint(writer, (FnCall *) rp->item);
break;
default:
WriterWrite(writer, "(** Unknown argument **)\n");
break;
}
}
}
/*****************************************************************************/
JsonElement *FnCallToJson(FnCall *fp)
{
assert(fp);
JsonElement *object = JsonObjectCreate(3);
JsonObjectAppendString(object, "name", fp->name);
JsonObjectAppendString(object, "type", "function-call");
JsonElement *argsArray = JsonArrayCreate(fp->argc);
for (Rlist *rp = fp->args; rp != NULL; rp = rp->next)
{
switch (rp->type)
{
case CF_SCALAR:
JsonArrayAppendString(argsArray, (const char *) rp->item);
break;
case CF_FNCALL:
JsonArrayAppendObject(argsArray, FnCallToJson((FnCall *) rp->item));
break;
default:
assert(false && "Unknown argument type");
break;
}
}
JsonObjectAppendArray(object, "arguments", argsArray);
return object;
}