• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <string.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <unistd.h>
23 
24 #include "expr.h"
25 
26 // Functions should:
27 //
28 //    - return a malloc()'d string
29 //    - if Evaluate() on any argument returns NULL, return NULL.
30 
BooleanString(const char * s)31 int BooleanString(const char* s) {
32     return s[0] != '\0';
33 }
34 
Evaluate(State * state,Expr * expr)35 char* Evaluate(State* state, Expr* expr) {
36     return expr->fn(expr->name, state, expr->argc, expr->argv);
37 }
38 
ConcatFn(const char * name,State * state,int argc,Expr * argv[])39 char* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
40     if (argc == 0) {
41         return strdup("");
42     }
43     char** strings = malloc(argc * sizeof(char*));
44     int i;
45     for (i = 0; i < argc; ++i) {
46         strings[i] = NULL;
47     }
48     char* result = NULL;
49     int length = 0;
50     for (i = 0; i < argc; ++i) {
51         strings[i] = Evaluate(state, argv[i]);
52         if (strings[i] == NULL) {
53             goto done;
54         }
55         length += strlen(strings[i]);
56     }
57 
58     result = malloc(length+1);
59     int p = 0;
60     for (i = 0; i < argc; ++i) {
61         strcpy(result+p, strings[i]);
62         p += strlen(strings[i]);
63     }
64     result[p] = '\0';
65 
66   done:
67     for (i = 0; i < argc; ++i) {
68         free(strings[i]);
69     }
70     return result;
71 }
72 
IfElseFn(const char * name,State * state,int argc,Expr * argv[])73 char* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
74     if (argc != 2 && argc != 3) {
75         free(state->errmsg);
76         state->errmsg = strdup("ifelse expects 2 or 3 arguments");
77         return NULL;
78     }
79     char* cond = Evaluate(state, argv[0]);
80     if (cond == NULL) {
81         return NULL;
82     }
83 
84     if (BooleanString(cond) == true) {
85         free(cond);
86         return Evaluate(state, argv[1]);
87     } else {
88         if (argc == 3) {
89             free(cond);
90             return Evaluate(state, argv[2]);
91         } else {
92             return cond;
93         }
94     }
95 }
96 
AbortFn(const char * name,State * state,int argc,Expr * argv[])97 char* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
98     char* msg = NULL;
99     if (argc > 0) {
100         msg = Evaluate(state, argv[0]);
101     }
102     free(state->errmsg);
103     if (msg) {
104         state->errmsg = msg;
105     } else {
106         state->errmsg = strdup("called abort()");
107     }
108     return NULL;
109 }
110 
AssertFn(const char * name,State * state,int argc,Expr * argv[])111 char* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
112     int i;
113     for (i = 0; i < argc; ++i) {
114         char* v = Evaluate(state, argv[i]);
115         if (v == NULL) {
116             return NULL;
117         }
118         int b = BooleanString(v);
119         free(v);
120         if (!b) {
121             int prefix_len;
122             int len = argv[i]->end - argv[i]->start;
123             char* err_src = malloc(len + 20);
124             strcpy(err_src, "assert failed: ");
125             prefix_len = strlen(err_src);
126             memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
127             err_src[prefix_len + len] = '\0';
128             free(state->errmsg);
129             state->errmsg = err_src;
130             return NULL;
131         }
132     }
133     return strdup("");
134 }
135 
SleepFn(const char * name,State * state,int argc,Expr * argv[])136 char* SleepFn(const char* name, State* state, int argc, Expr* argv[]) {
137     char* val = Evaluate(state, argv[0]);
138     if (val == NULL) {
139         return NULL;
140     }
141     int v = strtol(val, NULL, 10);
142     sleep(v);
143     return val;
144 }
145 
StdoutFn(const char * name,State * state,int argc,Expr * argv[])146 char* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) {
147     int i;
148     for (i = 0; i < argc; ++i) {
149         char* v = Evaluate(state, argv[i]);
150         if (v == NULL) {
151             return NULL;
152         }
153         fputs(v, stdout);
154         free(v);
155     }
156     return strdup("");
157 }
158 
LogicalAndFn(const char * name,State * state,int argc,Expr * argv[])159 char* LogicalAndFn(const char* name, State* state,
160                    int argc, Expr* argv[]) {
161     char* left = Evaluate(state, argv[0]);
162     if (left == NULL) return NULL;
163     if (BooleanString(left) == true) {
164         free(left);
165         return Evaluate(state, argv[1]);
166     } else {
167         return left;
168     }
169 }
170 
LogicalOrFn(const char * name,State * state,int argc,Expr * argv[])171 char* LogicalOrFn(const char* name, State* state,
172                   int argc, Expr* argv[]) {
173     char* left = Evaluate(state, argv[0]);
174     if (left == NULL) return NULL;
175     if (BooleanString(left) == false) {
176         free(left);
177         return Evaluate(state, argv[1]);
178     } else {
179         return left;
180     }
181 }
182 
LogicalNotFn(const char * name,State * state,int argc,Expr * argv[])183 char* LogicalNotFn(const char* name, State* state,
184                    int argc, Expr* argv[]) {
185     char* val = Evaluate(state, argv[0]);
186     if (val == NULL) return NULL;
187     bool bv = BooleanString(val);
188     free(val);
189     if (bv) {
190         return strdup("");
191     } else {
192         return strdup("t");
193     }
194 }
195 
SubstringFn(const char * name,State * state,int argc,Expr * argv[])196 char* SubstringFn(const char* name, State* state,
197                   int argc, Expr* argv[]) {
198     char* needle = Evaluate(state, argv[0]);
199     if (needle == NULL) return NULL;
200     char* haystack = Evaluate(state, argv[1]);
201     if (haystack == NULL) {
202         free(needle);
203         return NULL;
204     }
205 
206     char* result = strdup(strstr(haystack, needle) ? "t" : "");
207     free(needle);
208     free(haystack);
209     return result;
210 }
211 
EqualityFn(const char * name,State * state,int argc,Expr * argv[])212 char* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) {
213     char* left = Evaluate(state, argv[0]);
214     if (left == NULL) return NULL;
215     char* right = Evaluate(state, argv[1]);
216     if (right == NULL) {
217         free(left);
218         return NULL;
219     }
220 
221     char* result = strdup(strcmp(left, right) == 0 ? "t" : "");
222     free(left);
223     free(right);
224     return result;
225 }
226 
InequalityFn(const char * name,State * state,int argc,Expr * argv[])227 char* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) {
228     char* left = Evaluate(state, argv[0]);
229     if (left == NULL) return NULL;
230     char* right = Evaluate(state, argv[1]);
231     if (right == NULL) {
232         free(left);
233         return NULL;
234     }
235 
236     char* result = strdup(strcmp(left, right) != 0 ? "t" : "");
237     free(left);
238     free(right);
239     return result;
240 }
241 
SequenceFn(const char * name,State * state,int argc,Expr * argv[])242 char* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
243     char* left = Evaluate(state, argv[0]);
244     if (left == NULL) return NULL;
245     free(left);
246     return Evaluate(state, argv[1]);
247 }
248 
LessThanIntFn(const char * name,State * state,int argc,Expr * argv[])249 char* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
250     if (argc != 2) {
251         free(state->errmsg);
252         state->errmsg = strdup("less_than_int expects 2 arguments");
253         return NULL;
254     }
255 
256     char* left;
257     char* right;
258     if (ReadArgs(state, argv, 2, &left, &right) < 0) return NULL;
259 
260     bool result = false;
261     char* end;
262 
263     long l_int = strtol(left, &end, 10);
264     if (left[0] == '\0' || *end != '\0') {
265         fprintf(stderr, "[%s] is not an int\n", left);
266         goto done;
267     }
268 
269     long r_int = strtol(right, &end, 10);
270     if (right[0] == '\0' || *end != '\0') {
271         fprintf(stderr, "[%s] is not an int\n", right);
272         goto done;
273     }
274 
275     result = l_int < r_int;
276 
277   done:
278     free(left);
279     free(right);
280     return strdup(result ? "t" : "");
281 }
282 
GreaterThanIntFn(const char * name,State * state,int argc,Expr * argv[])283 char* GreaterThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
284     if (argc != 2) {
285         free(state->errmsg);
286         state->errmsg = strdup("greater_than_int expects 2 arguments");
287         return NULL;
288     }
289 
290     Expr* temp[2];
291     temp[0] = argv[1];
292     temp[1] = argv[0];
293 
294     return LessThanIntFn(name, state, 2, temp);
295 }
296 
Literal(const char * name,State * state,int argc,Expr * argv[])297 char* Literal(const char* name, State* state, int argc, Expr* argv[]) {
298     return strdup(name);
299 }
300 
Build(Function fn,YYLTYPE loc,int count,...)301 Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
302     va_list v;
303     va_start(v, count);
304     Expr* e = malloc(sizeof(Expr));
305     e->fn = fn;
306     e->name = "(operator)";
307     e->argc = count;
308     e->argv = malloc(count * sizeof(Expr*));
309     int i;
310     for (i = 0; i < count; ++i) {
311         e->argv[i] = va_arg(v, Expr*);
312     }
313     va_end(v);
314     e->start = loc.start;
315     e->end = loc.end;
316     return e;
317 }
318 
319 // -----------------------------------------------------------------
320 //   the function table
321 // -----------------------------------------------------------------
322 
323 static int fn_entries = 0;
324 static int fn_size = 0;
325 NamedFunction* fn_table = NULL;
326 
RegisterFunction(const char * name,Function fn)327 void RegisterFunction(const char* name, Function fn) {
328     if (fn_entries >= fn_size) {
329         fn_size = fn_size*2 + 1;
330         fn_table = realloc(fn_table, fn_size * sizeof(NamedFunction));
331     }
332     fn_table[fn_entries].name = name;
333     fn_table[fn_entries].fn = fn;
334     ++fn_entries;
335 }
336 
fn_entry_compare(const void * a,const void * b)337 static int fn_entry_compare(const void* a, const void* b) {
338     const char* na = ((const NamedFunction*)a)->name;
339     const char* nb = ((const NamedFunction*)b)->name;
340     return strcmp(na, nb);
341 }
342 
FinishRegistration()343 void FinishRegistration() {
344     qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
345 }
346 
FindFunction(const char * name)347 Function FindFunction(const char* name) {
348     NamedFunction key;
349     key.name = name;
350     NamedFunction* nf = bsearch(&key, fn_table, fn_entries,
351                                 sizeof(NamedFunction), fn_entry_compare);
352     if (nf == NULL) {
353         return NULL;
354     }
355     return nf->fn;
356 }
357 
RegisterBuiltins()358 void RegisterBuiltins() {
359     RegisterFunction("ifelse", IfElseFn);
360     RegisterFunction("abort", AbortFn);
361     RegisterFunction("assert", AssertFn);
362     RegisterFunction("concat", ConcatFn);
363     RegisterFunction("is_substring", SubstringFn);
364     RegisterFunction("stdout", StdoutFn);
365     RegisterFunction("sleep", SleepFn);
366 
367     RegisterFunction("less_than_int", LessThanIntFn);
368     RegisterFunction("greater_than_int", GreaterThanIntFn);
369 }
370 
371 
372 // -----------------------------------------------------------------
373 //   convenience methods for functions
374 // -----------------------------------------------------------------
375 
376 // Evaluate the expressions in argv, giving 'count' char* (the ... is
377 // zero or more char** to put them in).  If any expression evaluates
378 // to NULL, free the rest and return -1.  Return 0 on success.
ReadArgs(State * state,Expr * argv[],int count,...)379 int ReadArgs(State* state, Expr* argv[], int count, ...) {
380     char** args = malloc(count * sizeof(char*));
381     va_list v;
382     va_start(v, count);
383     int i;
384     for (i = 0; i < count; ++i) {
385         args[i] = Evaluate(state, argv[i]);
386         if (args[i] == NULL) {
387             va_end(v);
388             int j;
389             for (j = 0; j < i; ++j) {
390                 free(args[j]);
391             }
392             return -1;
393         }
394         *(va_arg(v, char**)) = args[i];
395     }
396     va_end(v);
397     return 0;
398 }
399 
400 // Evaluate the expressions in argv, returning an array of char*
401 // results.  If any evaluate to NULL, free the rest and return NULL.
402 // The caller is responsible for freeing the returned array and the
403 // strings it contains.
ReadVarArgs(State * state,int argc,Expr * argv[])404 char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
405     char** args = (char**)malloc(argc * sizeof(char*));
406     int i = 0;
407     for (i = 0; i < argc; ++i) {
408         args[i] = Evaluate(state, argv[i]);
409         if (args[i] == NULL) {
410             int j;
411             for (j = 0; j < i; ++j) {
412                 free(args[j]);
413             }
414             free(args);
415             return NULL;
416         }
417     }
418     return args;
419 }
420 
421 // Use printf-style arguments to compose an error message to put into
422 // *state.  Returns NULL.
ErrorAbort(State * state,char * format,...)423 char* ErrorAbort(State* state, char* format, ...) {
424     char* buffer = malloc(4096);
425     va_list v;
426     va_start(v, format);
427     vsnprintf(buffer, 4096, format, v);
428     va_end(v);
429     free(state->errmsg);
430     state->errmsg = buffer;
431     return NULL;
432 }
433