ast_expr2.y 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. %{
  2. /* Written by Pace Willisson (pace@blitz.com)
  3. * and placed in the public domain.
  4. *
  5. * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
  6. *
  7. * And then overhauled twice by Steve Murphy (murf@digium.com)
  8. * to add double-quoted strings, allow mult. spaces, improve
  9. * error messages, and then to fold in a flex scanner for the
  10. * yylex operation.
  11. *
  12. * $FreeBSD: src/bin/expr/expr.y,v 1.16 2000/07/22 10:59:36 se Exp $
  13. */
  14. #include "asterisk.h"
  15. #include <sys/cdefs.h>
  16. #include <sys/types.h>
  17. #include <stdio.h>
  18. #if !defined(STANDALONE) && !defined(STANDALONE2) \
  19. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  20. #else
  21. #ifndef __USE_ISOC99
  22. #define __USE_ISOC99 1
  23. #endif
  24. #endif
  25. #ifdef __USE_ISOC99
  26. #define FP___PRINTF "%.18Lg"
  27. #define FP___TYPE long double
  28. #else
  29. #define FP___PRINTF "%.16g"
  30. #define FP___TYPE double
  31. #endif
  32. #ifdef HAVE_COSL
  33. #define FUNC_COS cosl
  34. #elif defined(HAVE_COS)
  35. #define FUNC_COS (long double)cos
  36. #endif
  37. #ifdef HAVE_SINL
  38. #define FUNC_SIN sinl
  39. #elif defined(HAVE_SIN)
  40. #define FUNC_SIN (long double)sin
  41. #endif
  42. #ifdef HAVE_TANL
  43. #define FUNC_TAN tanl
  44. #elif defined(HAVE_TAN)
  45. #define FUNC_TAN (long double)tan
  46. #endif
  47. #ifdef HAVE_ACOSL
  48. #define FUNC_ACOS acosl
  49. #elif defined(HAVE_ACOS)
  50. #define FUNC_ACOS (long double)acos
  51. #endif
  52. #ifdef HAVE_ASINL
  53. #define FUNC_ASIN asinl
  54. #elif defined(HAVE_ASIN)
  55. #define FUNC_ASIN (long double)asin
  56. #endif
  57. #ifdef HAVE_ATANL
  58. #define FUNC_ATAN atanl
  59. #elif defined(HAVE_ATAN)
  60. #define FUNC_ATAN (long double)atan
  61. #endif
  62. #ifdef HAVE_ATAN2L
  63. #define FUNC_ATAN2 atan2l
  64. #elif defined(HAVE_ATAN2)
  65. #define FUNC_ATAN2 (long double)atan2
  66. #endif
  67. #ifdef HAVE_POWL
  68. #define FUNC_POW powl
  69. #elif defined(HAVE_POW)
  70. #define FUNC_POW (long double)pow
  71. #endif
  72. #ifdef HAVE_SQRTL
  73. #define FUNC_SQRT sqrtl
  74. #elif defined(HAVE_SQRT)
  75. #define FUNC_SQRT (long double)sqrt
  76. #endif
  77. #ifdef HAVE_RINTL
  78. #define FUNC_RINT rintl
  79. #elif defined(HAVE_RINT)
  80. #define FUNC_RINT (long double)rint
  81. #endif
  82. #ifdef HAVE_EXPL
  83. #define FUNC_EXP expl
  84. #elif defined(HAVE_EXP)
  85. #define FUNC_EXP (long double)exp
  86. #endif
  87. #ifdef HAVE_LOGL
  88. #define FUNC_LOG logl
  89. #elif defined(HAVE_LOG)
  90. #define FUNC_LOG (long double)log
  91. #endif
  92. #ifdef HAVE_REMAINDERL
  93. #define FUNC_REMAINDER remainderl
  94. #elif defined(HAVE_REMAINDER)
  95. #define FUNC_REMAINDER (long double)remainder
  96. #endif
  97. #ifdef HAVE_FMODL
  98. #define FUNC_FMOD fmodl
  99. #elif defined(HAVE_FMOD)
  100. #define FUNC_FMOD (long double)fmod
  101. #endif
  102. #ifdef HAVE_STRTOLD
  103. #define FUNC_STRTOD strtold
  104. #elif defined(HAVE_STRTOD)
  105. #define FUNC_STRTOD (long double)strtod
  106. #endif
  107. #ifdef HAVE_FLOORL
  108. #define FUNC_FLOOR floorl
  109. #elif defined(HAVE_FLOOR)
  110. #define FUNC_FLOOR (long double)floor
  111. #endif
  112. #ifdef HAVE_CEILL
  113. #define FUNC_CEIL ceill
  114. #elif defined(HAVE_CEIL)
  115. #define FUNC_CEIL (long double)ceil
  116. #endif
  117. #ifdef HAVE_ROUNDL
  118. #define FUNC_ROUND roundl
  119. #elif defined(HAVE_ROUND)
  120. #define FUNC_ROUND (long double)round
  121. #endif
  122. #ifdef HAVE_TRUNCL
  123. #define FUNC_TRUNC truncl
  124. #elif defined(HAVE_TRUNC)
  125. #define FUNC_TRUNC (long double)trunc
  126. #endif
  127. /*! \note
  128. * Oddly enough, some platforms have some ISO C99 functions, but not others, so
  129. * we define the missing functions in terms of their mathematical identities.
  130. */
  131. #ifdef HAVE_EXP2L
  132. #define FUNC_EXP2 exp2l
  133. #elif (defined(HAVE_EXPL) && defined(HAVE_LOGL))
  134. #define FUNC_EXP2(x) expl((x) * logl(2.0))
  135. #elif (defined(HAVE_EXP) && defined(HAVE_LOG))
  136. #define FUNC_EXP2(x) (long double)exp((x) * log(2.0))
  137. #endif
  138. #ifdef HAVE_EXP10L
  139. #define FUNC_EXP10 exp10l
  140. #elif (defined(HAVE_EXPL) && defined(HAVE_LOGL))
  141. #define FUNC_EXP10(x) expl((x) * logl(10.0))
  142. #elif (defined(HAVE_EXP) && defined(HAVE_LOG))
  143. #define FUNC_EXP10(x) (long double)exp((x) * log(10.0))
  144. #endif
  145. #ifdef HAVE_LOG2L
  146. #define FUNC_LOG2 log2l
  147. #elif defined(HAVE_LOGL)
  148. #define FUNC_LOG2(x) (logl(x) / logl(2.0))
  149. #elif defined(HAVE_LOG10L)
  150. #define FUNC_LOG2(x) (log10l(x) / log10l(2.0))
  151. #elif defined(HAVE_LOG2)
  152. #define FUNC_LOG2 (long double)log2
  153. #elif defined(HAVE_LOG)
  154. #define FUNC_LOG2(x) ((long double)log(x) / log(2.0))
  155. #endif
  156. #ifdef HAVE_LOG10L
  157. #define FUNC_LOG10 log10l
  158. #elif defined(HAVE_LOGL)
  159. #define FUNC_LOG10(x) (logl(x) / logl(10.0))
  160. #elif defined(HAVE_LOG2L)
  161. #define FUNC_LOG10(x) (log2l(x) / log2l(10.0))
  162. #elif defined(HAVE_LOG10)
  163. #define FUNC_LOG10(x) (long double)log10(x)
  164. #elif defined(HAVE_LOG)
  165. #define FUNC_LOG10(x) ((long double)log(x) / log(10.0))
  166. #endif
  167. #include <stdlib.h>
  168. #ifndef _GNU_SOURCE
  169. #define _GNU_SOURCE
  170. #endif
  171. #include <string.h>
  172. #include <math.h>
  173. #include <locale.h>
  174. #include <unistd.h>
  175. #include <ctype.h>
  176. #if !defined(SOLARIS) && !defined(__CYGWIN__)
  177. /* #include <err.h> */
  178. #else
  179. #define quad_t int64_t
  180. #endif
  181. #include <errno.h>
  182. #include <regex.h>
  183. #include <limits.h>
  184. #include "asterisk/ast_expr.h"
  185. #include "asterisk/logger.h"
  186. #if !defined(STANDALONE) && !defined(STANDALONE2)
  187. #include "asterisk/pbx.h"
  188. #endif
  189. #if defined(LONG_LONG_MIN) && !defined(QUAD_MIN)
  190. #define QUAD_MIN LONG_LONG_MIN
  191. #endif
  192. #if defined(LONG_LONG_MAX) && !defined(QUAD_MAX)
  193. #define QUAD_MAX LONG_LONG_MAX
  194. #endif
  195. # if ! defined(QUAD_MIN)
  196. # define QUAD_MIN (-0x7fffffffffffffffLL-1)
  197. # endif
  198. # if ! defined(QUAD_MAX)
  199. # define QUAD_MAX (0x7fffffffffffffffLL)
  200. # endif
  201. #define YYENABLE_NLS 0
  202. #define YYPARSE_PARAM parseio
  203. #define YYLEX_PARAM ((struct parse_io *)parseio)->scanner
  204. #define YYERROR_VERBOSE 1
  205. extern char extra_error_message[4095];
  206. extern int extra_error_message_supplied;
  207. enum valtype {
  208. AST_EXPR_number, AST_EXPR_numeric_string, AST_EXPR_string
  209. } ;
  210. #if defined(STANDALONE) || defined(STANDALONE2)
  211. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
  212. #endif
  213. struct val {
  214. enum valtype type;
  215. union {
  216. char *s;
  217. FP___TYPE i; /* either long double, or just double, on a bad day */
  218. } u;
  219. } ;
  220. enum node_type {
  221. AST_EXPR_NODE_COMMA, AST_EXPR_NODE_STRING, AST_EXPR_NODE_VAL
  222. } ;
  223. struct expr_node
  224. {
  225. enum node_type type;
  226. struct val *val;
  227. struct expr_node *left;
  228. struct expr_node *right;
  229. };
  230. typedef void *yyscan_t;
  231. struct parse_io
  232. {
  233. char *string;
  234. struct val *val;
  235. yyscan_t scanner;
  236. struct ast_channel *chan;
  237. };
  238. static int chk_div __P((FP___TYPE, FP___TYPE));
  239. static int chk_minus __P((FP___TYPE, FP___TYPE, FP___TYPE));
  240. static int chk_plus __P((FP___TYPE, FP___TYPE, FP___TYPE));
  241. static int chk_times __P((FP___TYPE, FP___TYPE, FP___TYPE));
  242. static void free_value __P((struct val *));
  243. static int is_zero_or_null __P((struct val *));
  244. static int isstring __P((struct val *));
  245. static struct val *make_number __P((FP___TYPE));
  246. static struct val *make_str __P((const char *));
  247. static struct val *op_and __P((struct val *, struct val *));
  248. static struct val *op_colon __P((struct val *, struct val *));
  249. static struct val *op_eqtilde __P((struct val *, struct val *));
  250. static struct val *op_tildetilde __P((struct val *, struct val *));
  251. static struct val *op_div __P((struct val *, struct val *));
  252. static struct val *op_eq __P((struct val *, struct val *));
  253. static struct val *op_ge __P((struct val *, struct val *));
  254. static struct val *op_gt __P((struct val *, struct val *));
  255. static struct val *op_le __P((struct val *, struct val *));
  256. static struct val *op_lt __P((struct val *, struct val *));
  257. static struct val *op_cond __P((struct val *, struct val *, struct val *));
  258. static struct val *op_minus __P((struct val *, struct val *));
  259. static struct val *op_negate __P((struct val *));
  260. static struct val *op_compl __P((struct val *));
  261. static struct val *op_ne __P((struct val *, struct val *));
  262. static struct val *op_or __P((struct val *, struct val *));
  263. static struct val *op_plus __P((struct val *, struct val *));
  264. static struct val *op_rem __P((struct val *, struct val *));
  265. static struct val *op_times __P((struct val *, struct val *));
  266. static struct val *op_func(struct val *funcname, struct expr_node *arglist, struct ast_channel *chan);
  267. static int to_number __P((struct val *));
  268. static void to_string __P((struct val *));
  269. static struct expr_node *alloc_expr_node(enum node_type);
  270. static void destroy_arglist(struct expr_node *arglist);
  271. /* uh, if I want to predeclare yylex with a YYLTYPE, I have to predeclare the yyltype... sigh */
  272. typedef struct yyltype
  273. {
  274. int first_line;
  275. int first_column;
  276. int last_line;
  277. int last_column;
  278. } yyltype;
  279. # define YYLTYPE yyltype
  280. # define YYLTYPE_IS_TRIVIAL 1
  281. /* we will get warning about no prototype for yylex! But we can't
  282. define it here, we have no definition yet for YYSTYPE. */
  283. int ast_yyerror(const char *,YYLTYPE *, struct parse_io *);
  284. /* I wanted to add args to the yyerror routine, so I could print out
  285. some useful info about the error. Not as easy as it looks, but it
  286. is possible. */
  287. #define ast_yyerror(x) ast_yyerror(x,&yyloc,parseio)
  288. #define DESTROY(x) {if((x)->type == AST_EXPR_numeric_string || (x)->type == AST_EXPR_string) free((x)->u.s); (x)->u.s = 0; free(x);}
  289. %}
  290. %pure-parser
  291. %locations
  292. /* %debug for when you are having big problems */
  293. /* %name-prefix="ast_yy" */
  294. %union
  295. {
  296. struct val *val;
  297. struct expr_node *arglist;
  298. }
  299. %{
  300. extern int ast_yylex __P((YYSTYPE *, YYLTYPE *, yyscan_t));
  301. %}
  302. %left <val> TOK_COMMA
  303. %left <val> TOK_COND TOK_COLONCOLON
  304. %left <val> TOK_OR
  305. %left <val> TOK_AND
  306. %left <val> TOK_EQ TOK_GT TOK_LT TOK_GE TOK_LE TOK_NE
  307. %left <val> TOK_PLUS TOK_MINUS
  308. %left <val> TOK_MULT TOK_DIV TOK_MOD
  309. %right <val> TOK_COMPL
  310. %left <val> TOK_COLON TOK_EQTILDE TOK_TILDETILDE
  311. %left <val> TOK_RP TOK_LP
  312. %token <val> TOKEN
  313. %type <arglist> arglist
  314. %type <val> start expr
  315. %destructor { free_value($$); } expr TOKEN TOK_COND TOK_COLONCOLON TOK_OR TOK_AND TOK_EQ
  316. TOK_GT TOK_LT TOK_GE TOK_LE TOK_NE TOK_PLUS TOK_MINUS TOK_MULT TOK_DIV TOK_MOD TOK_COMPL TOK_COLON TOK_EQTILDE
  317. TOK_RP TOK_LP TOK_TILDETILDE
  318. %%
  319. start: expr { ((struct parse_io *)parseio)->val = (struct val *)calloc(sizeof(struct val),1);
  320. ((struct parse_io *)parseio)->val->type = $1->type;
  321. if( $1->type == AST_EXPR_number )
  322. ((struct parse_io *)parseio)->val->u.i = $1->u.i;
  323. else
  324. ((struct parse_io *)parseio)->val->u.s = $1->u.s;
  325. free($1);
  326. }
  327. | {/* nothing */ ((struct parse_io *)parseio)->val = (struct val *)calloc(sizeof(struct val),1);
  328. ((struct parse_io *)parseio)->val->type = AST_EXPR_string;
  329. ((struct parse_io *)parseio)->val->u.s = strdup("");
  330. }
  331. ;
  332. arglist: expr { $$ = alloc_expr_node(AST_EXPR_NODE_VAL); $$->val = $1;}
  333. | arglist TOK_COMMA expr %prec TOK_RP {struct expr_node *x = alloc_expr_node(AST_EXPR_NODE_VAL);
  334. struct expr_node *t;
  335. DESTROY($2);
  336. for (t=$1;t->right;t=t->right)
  337. ;
  338. $$ = $1; t->right = x; x->val = $3;}
  339. | arglist TOK_COMMA %prec TOK_RP {struct expr_node *x = alloc_expr_node(AST_EXPR_NODE_VAL);
  340. struct expr_node *t; /* NULL args should OK */
  341. DESTROY($2);
  342. for (t=$1;t->right;t=t->right)
  343. ;
  344. $$ = $1; t->right = x; x->val = make_str("");}
  345. ;
  346. expr:
  347. TOKEN TOK_LP arglist TOK_RP { $$ = op_func($1,$3, ((struct parse_io *)parseio)->chan);
  348. DESTROY($2);
  349. DESTROY($4);
  350. DESTROY($1);
  351. destroy_arglist($3);
  352. }
  353. | TOKEN {$$ = $1;}
  354. | TOK_LP expr TOK_RP { $$ = $2;
  355. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  356. @$.first_line=0; @$.last_line=0;
  357. DESTROY($1); DESTROY($3); }
  358. | expr TOK_OR expr { $$ = op_or ($1, $3);
  359. DESTROY($2);
  360. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  361. @$.first_line=0; @$.last_line=0;}
  362. | expr TOK_AND expr { $$ = op_and ($1, $3);
  363. DESTROY($2);
  364. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  365. @$.first_line=0; @$.last_line=0;}
  366. | expr TOK_EQ expr { $$ = op_eq ($1, $3);
  367. DESTROY($2);
  368. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  369. @$.first_line=0; @$.last_line=0;}
  370. | expr TOK_GT expr { $$ = op_gt ($1, $3);
  371. DESTROY($2);
  372. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  373. @$.first_line=0; @$.last_line=0;}
  374. | expr TOK_LT expr { $$ = op_lt ($1, $3);
  375. DESTROY($2);
  376. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  377. @$.first_line=0; @$.last_line=0;}
  378. | expr TOK_GE expr { $$ = op_ge ($1, $3);
  379. DESTROY($2);
  380. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  381. @$.first_line=0; @$.last_line=0;}
  382. | expr TOK_LE expr { $$ = op_le ($1, $3);
  383. DESTROY($2);
  384. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  385. @$.first_line=0; @$.last_line=0;}
  386. | expr TOK_NE expr { $$ = op_ne ($1, $3);
  387. DESTROY($2);
  388. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  389. @$.first_line=0; @$.last_line=0;}
  390. | expr TOK_PLUS expr { $$ = op_plus ($1, $3);
  391. DESTROY($2);
  392. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  393. @$.first_line=0; @$.last_line=0;}
  394. | expr TOK_MINUS expr { $$ = op_minus ($1, $3);
  395. DESTROY($2);
  396. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  397. @$.first_line=0; @$.last_line=0;}
  398. | TOK_MINUS expr %prec TOK_COMPL { $$ = op_negate ($2);
  399. DESTROY($1);
  400. @$.first_column = @1.first_column; @$.last_column = @2.last_column;
  401. @$.first_line=0; @$.last_line=0;}
  402. | TOK_COMPL expr { $$ = op_compl ($2);
  403. DESTROY($1);
  404. @$.first_column = @1.first_column; @$.last_column = @2.last_column;
  405. @$.first_line=0; @$.last_line=0;}
  406. | expr TOK_MULT expr { $$ = op_times ($1, $3);
  407. DESTROY($2);
  408. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  409. @$.first_line=0; @$.last_line=0;}
  410. | expr TOK_DIV expr { $$ = op_div ($1, $3);
  411. DESTROY($2);
  412. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  413. @$.first_line=0; @$.last_line=0;}
  414. | expr TOK_MOD expr { $$ = op_rem ($1, $3);
  415. DESTROY($2);
  416. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  417. @$.first_line=0; @$.last_line=0;}
  418. | expr TOK_COLON expr { $$ = op_colon ($1, $3);
  419. DESTROY($2);
  420. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  421. @$.first_line=0; @$.last_line=0;}
  422. | expr TOK_EQTILDE expr { $$ = op_eqtilde ($1, $3);
  423. DESTROY($2);
  424. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  425. @$.first_line=0; @$.last_line=0;}
  426. | expr TOK_COND expr TOK_COLONCOLON expr { $$ = op_cond ($1, $3, $5);
  427. DESTROY($2);
  428. DESTROY($4);
  429. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  430. @$.first_line=0; @$.last_line=0;}
  431. | expr TOK_TILDETILDE expr { $$ = op_tildetilde ($1, $3);
  432. DESTROY($2);
  433. @$.first_column = @1.first_column; @$.last_column = @3.last_column;
  434. @$.first_line=0; @$.last_line=0;}
  435. ;
  436. %%
  437. static struct expr_node *alloc_expr_node(enum node_type nt)
  438. {
  439. struct expr_node *x = calloc(1,sizeof(struct expr_node));
  440. if (!x) {
  441. ast_log(LOG_ERROR, "Allocation for expr_node FAILED!!\n");
  442. return 0;
  443. }
  444. x->type = nt;
  445. return x;
  446. }
  447. static struct val *
  448. make_number (FP___TYPE i)
  449. {
  450. struct val *vp;
  451. vp = (struct val *) malloc (sizeof (*vp));
  452. if (vp == NULL) {
  453. ast_log(LOG_WARNING, "malloc() failed\n");
  454. return(NULL);
  455. }
  456. vp->type = AST_EXPR_number;
  457. vp->u.i = i;
  458. return vp;
  459. }
  460. static struct val *
  461. make_str (const char *s)
  462. {
  463. struct val *vp;
  464. size_t i;
  465. int isint; /* this started out being a test for an integer, but then ended up being a test for a float */
  466. vp = (struct val *) malloc (sizeof (*vp));
  467. if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) {
  468. if (vp) {
  469. free(vp);
  470. }
  471. ast_log(LOG_WARNING,"malloc() failed\n");
  472. return(NULL);
  473. }
  474. for (i = 0, isint = (isdigit(s[0]) || s[0] == '-' || s[0]=='.'); isint && i < strlen(s); i++)
  475. {
  476. if (!isdigit(s[i]) && s[i] != '.') {
  477. isint = 0;
  478. break;
  479. }
  480. }
  481. if (isint)
  482. vp->type = AST_EXPR_numeric_string;
  483. else
  484. vp->type = AST_EXPR_string;
  485. return vp;
  486. }
  487. static void
  488. free_value (struct val *vp)
  489. {
  490. if (vp==NULL) {
  491. return;
  492. }
  493. if (vp->type == AST_EXPR_string || vp->type == AST_EXPR_numeric_string)
  494. free (vp->u.s);
  495. free(vp);
  496. }
  497. static int
  498. to_number (struct val *vp)
  499. {
  500. FP___TYPE i;
  501. if (vp == NULL) {
  502. ast_log(LOG_WARNING,"vp==NULL in to_number()\n");
  503. return(0);
  504. }
  505. if (vp->type == AST_EXPR_number)
  506. return 1;
  507. if (vp->type == AST_EXPR_string)
  508. return 0;
  509. /* vp->type == AST_EXPR_numeric_string, make it numeric */
  510. errno = 0;
  511. i = FUNC_STRTOD(vp->u.s, (char**)0); /* either strtod, or strtold on a good day */
  512. if (errno != 0) {
  513. ast_log(LOG_WARNING,"Conversion of %s to number under/overflowed!\n", vp->u.s);
  514. free(vp->u.s);
  515. vp->u.s = 0;
  516. return(0);
  517. }
  518. free (vp->u.s);
  519. vp->u.i = i;
  520. vp->type = AST_EXPR_number;
  521. return 1;
  522. }
  523. static void
  524. strip_quotes(struct val *vp)
  525. {
  526. if (vp->type != AST_EXPR_string && vp->type != AST_EXPR_numeric_string)
  527. return;
  528. if( vp->u.s[0] == '"' && vp->u.s[strlen(vp->u.s)-1] == '"' )
  529. {
  530. char *f, *t;
  531. f = vp->u.s;
  532. t = vp->u.s;
  533. while( *f )
  534. {
  535. if( *f && *f != '"' )
  536. *t++ = *f++;
  537. else
  538. f++;
  539. }
  540. *t = *f;
  541. }
  542. }
  543. static void
  544. to_string (struct val *vp)
  545. {
  546. char *tmp;
  547. if (vp->type == AST_EXPR_string || vp->type == AST_EXPR_numeric_string)
  548. return;
  549. if (asprintf(&tmp, FP___PRINTF, vp->u.i) == -1) {
  550. ast_log(LOG_WARNING, "asprintf() failed\n");
  551. return;
  552. }
  553. vp->type = AST_EXPR_string;
  554. vp->u.s = tmp;
  555. }
  556. static int
  557. isstring (struct val *vp)
  558. {
  559. /* only TRUE if this string is not a valid number */
  560. return (vp->type == AST_EXPR_string);
  561. }
  562. static int
  563. is_zero_or_null (struct val *vp)
  564. {
  565. if (vp->type == AST_EXPR_number) {
  566. return (vp->u.i == 0);
  567. } else {
  568. return (*vp->u.s == 0 || (to_number(vp) && vp->u.i == 0));
  569. }
  570. /* NOTREACHED */
  571. }
  572. #ifdef STANDALONE2
  573. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
  574. {
  575. va_list vars;
  576. va_start(vars,fmt);
  577. printf("LOG: lev:%d file:%s line:%d func: %s ",
  578. level, file, line, function);
  579. vprintf(fmt, vars);
  580. fflush(stdout);
  581. va_end(vars);
  582. }
  583. int main(int argc,char **argv) {
  584. char s[4096];
  585. char out[4096];
  586. FILE *infile;
  587. if( !argv[1] )
  588. exit(20);
  589. if( access(argv[1],F_OK)== 0 )
  590. {
  591. int ret;
  592. infile = fopen(argv[1],"r");
  593. if( !infile )
  594. {
  595. printf("Sorry, couldn't open %s for reading!\n", argv[1]);
  596. exit(10);
  597. }
  598. while( fgets(s,sizeof(s),infile) )
  599. {
  600. if( s[strlen(s)-1] == '\n' )
  601. s[strlen(s)-1] = 0;
  602. ret = ast_expr(s, out, sizeof(out), NULL);
  603. printf("Expression: %s Result: [%d] '%s'\n",
  604. s, ret, out);
  605. }
  606. fclose(infile);
  607. }
  608. else
  609. {
  610. if (ast_expr(argv[1], s, sizeof(s), NULL))
  611. printf("=====%s======\n",s);
  612. else
  613. printf("No result\n");
  614. }
  615. return 0;
  616. }
  617. #endif
  618. #undef ast_yyerror
  619. #define ast_yyerror(x) ast_yyerror(x, YYLTYPE *yylloc, struct parse_io *parseio)
  620. /* I put the ast_yyerror func in the flex input file,
  621. because it refers to the buffer state. Best to
  622. let it access the BUFFER stuff there and not trying
  623. define all the structs, macros etc. in this file! */
  624. static void destroy_arglist(struct expr_node *arglist)
  625. {
  626. struct expr_node *arglist_next;
  627. while (arglist)
  628. {
  629. arglist_next = arglist->right;
  630. if (arglist->val)
  631. free_value(arglist->val);
  632. arglist->val = 0;
  633. arglist->right = 0;
  634. free(arglist);
  635. arglist = arglist_next;
  636. }
  637. }
  638. #if !defined(STANDALONE) && !defined(STANDALONE2)
  639. static char *compose_func_args(struct expr_node *arglist)
  640. {
  641. struct expr_node *t = arglist;
  642. char *argbuf;
  643. int total_len = 0;
  644. while (t) {
  645. if (t != arglist)
  646. total_len += 1; /* for the sep */
  647. if (t->val) {
  648. if (t->val->type == AST_EXPR_number)
  649. total_len += 25; /* worst case */
  650. else
  651. total_len += strlen(t->val->u.s);
  652. }
  653. t = t->right;
  654. }
  655. total_len++; /* for the null */
  656. ast_log(LOG_NOTICE,"argbuf allocated %d bytes;\n", total_len);
  657. argbuf = malloc(total_len);
  658. argbuf[0] = 0;
  659. t = arglist;
  660. while (t) {
  661. char numbuf[30];
  662. if (t != arglist)
  663. strcat(argbuf,",");
  664. if (t->val) {
  665. if (t->val->type == AST_EXPR_number) {
  666. sprintf(numbuf,FP___PRINTF,t->val->u.i);
  667. strcat(argbuf,numbuf);
  668. } else
  669. strcat(argbuf,t->val->u.s);
  670. }
  671. t = t->right;
  672. }
  673. ast_log(LOG_NOTICE,"argbuf uses %d bytes;\n", (int) strlen(argbuf));
  674. return argbuf;
  675. }
  676. static int is_really_num(char *str)
  677. {
  678. if ( strspn(str,"-0123456789. ") == strlen(str))
  679. return 1;
  680. else
  681. return 0;
  682. }
  683. #endif
  684. static struct val *op_func(struct val *funcname, struct expr_node *arglist, struct ast_channel *chan)
  685. {
  686. if (strspn(funcname->u.s,"ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789") == strlen(funcname->u.s))
  687. {
  688. struct val *result;
  689. if (0) {
  690. #ifdef FUNC_COS
  691. } else if (strcmp(funcname->u.s,"COS") == 0) {
  692. if (arglist && !arglist->right && arglist->val){
  693. to_number(arglist->val);
  694. result = make_number(FUNC_COS(arglist->val->u.i));
  695. return result;
  696. } else {
  697. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  698. return make_number(0.0);
  699. }
  700. #endif
  701. #ifdef FUNC_SIN
  702. } else if (strcmp(funcname->u.s,"SIN") == 0) {
  703. if (arglist && !arglist->right && arglist->val){
  704. to_number(arglist->val);
  705. result = make_number(FUNC_SIN(arglist->val->u.i));
  706. return result;
  707. } else {
  708. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  709. return make_number(0.0);
  710. }
  711. #endif
  712. #ifdef FUNC_TAN
  713. } else if (strcmp(funcname->u.s,"TAN") == 0) {
  714. if (arglist && !arglist->right && arglist->val){
  715. to_number(arglist->val);
  716. result = make_number(FUNC_TAN(arglist->val->u.i));
  717. return result;
  718. } else {
  719. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  720. return make_number(0.0);
  721. }
  722. #endif
  723. #ifdef FUNC_ACOS
  724. } else if (strcmp(funcname->u.s,"ACOS") == 0) {
  725. if (arglist && !arglist->right && arglist->val){
  726. to_number(arglist->val);
  727. result = make_number(FUNC_ACOS(arglist->val->u.i));
  728. return result;
  729. } else {
  730. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  731. return make_number(0.0);
  732. }
  733. #endif
  734. #ifdef FUNC_ASIN
  735. } else if (strcmp(funcname->u.s,"ASIN") == 0) {
  736. if (arglist && !arglist->right && arglist->val){
  737. to_number(arglist->val);
  738. result = make_number(FUNC_ASIN(arglist->val->u.i));
  739. return result;
  740. } else {
  741. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  742. return make_number(0.0);
  743. }
  744. #endif
  745. #ifdef FUNC_ATAN
  746. } else if (strcmp(funcname->u.s,"ATAN") == 0) {
  747. if (arglist && !arglist->right && arglist->val){
  748. to_number(arglist->val);
  749. result = make_number(FUNC_ATAN(arglist->val->u.i));
  750. return result;
  751. } else {
  752. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  753. return make_number(0.0);
  754. }
  755. #endif
  756. #ifdef FUNC_ATAN2
  757. } else if (strcmp(funcname->u.s,"ATAN2") == 0) {
  758. if (arglist && arglist->right && !arglist->right->right && arglist->val && arglist->right->val){
  759. to_number(arglist->val);
  760. to_number(arglist->right->val);
  761. result = make_number(FUNC_ATAN2(arglist->val->u.i, arglist->right->val->u.i));
  762. return result;
  763. } else {
  764. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  765. return make_number(0.0);
  766. }
  767. #endif
  768. #ifdef FUNC_POW
  769. } else if (strcmp(funcname->u.s,"POW") == 0) {
  770. if (arglist && arglist->right && !arglist->right->right && arglist->val && arglist->right->val){
  771. to_number(arglist->val);
  772. to_number(arglist->right->val);
  773. result = make_number(FUNC_POW(arglist->val->u.i, arglist->right->val->u.i));
  774. return result;
  775. } else {
  776. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  777. return make_number(0.0);
  778. }
  779. #endif
  780. #ifdef FUNC_SQRT
  781. } else if (strcmp(funcname->u.s,"SQRT") == 0) {
  782. if (arglist && !arglist->right && arglist->val){
  783. to_number(arglist->val);
  784. result = make_number(FUNC_SQRT(arglist->val->u.i));
  785. return result;
  786. } else {
  787. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  788. return make_number(0.0);
  789. }
  790. #endif
  791. #ifdef FUNC_FLOOR
  792. } else if (strcmp(funcname->u.s,"FLOOR") == 0) {
  793. if (arglist && !arglist->right && arglist->val){
  794. to_number(arglist->val);
  795. result = make_number(FUNC_FLOOR(arglist->val->u.i));
  796. return result;
  797. } else {
  798. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  799. return make_number(0.0);
  800. }
  801. #endif
  802. #ifdef FUNC_CEIL
  803. } else if (strcmp(funcname->u.s,"CEIL") == 0) {
  804. if (arglist && !arglist->right && arglist->val){
  805. to_number(arglist->val);
  806. result = make_number(FUNC_CEIL(arglist->val->u.i));
  807. return result;
  808. } else {
  809. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  810. return make_number(0.0);
  811. }
  812. #endif
  813. #ifdef FUNC_ROUND
  814. } else if (strcmp(funcname->u.s,"ROUND") == 0) {
  815. if (arglist && !arglist->right && arglist->val){
  816. to_number(arglist->val);
  817. result = make_number(FUNC_ROUND(arglist->val->u.i));
  818. return result;
  819. } else {
  820. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  821. return make_number(0.0);
  822. }
  823. #endif /* defined(FUNC_ROUND) */
  824. #ifdef FUNC_RINT
  825. } else if (strcmp(funcname->u.s,"RINT") == 0) {
  826. if (arglist && !arglist->right && arglist->val){
  827. to_number(arglist->val);
  828. result = make_number(FUNC_RINT(arglist->val->u.i));
  829. return result;
  830. } else {
  831. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  832. return make_number(0.0);
  833. }
  834. #endif
  835. #ifdef FUNC_TRUNC
  836. } else if (strcmp(funcname->u.s,"TRUNC") == 0) {
  837. if (arglist && !arglist->right && arglist->val){
  838. to_number(arglist->val);
  839. result = make_number(FUNC_TRUNC(arglist->val->u.i));
  840. return result;
  841. } else {
  842. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  843. return make_number(0.0);
  844. }
  845. #endif /* defined(FUNC_TRUNC) */
  846. #ifdef FUNC_EXP
  847. } else if (strcmp(funcname->u.s,"EXP") == 0) {
  848. if (arglist && !arglist->right && arglist->val){
  849. to_number(arglist->val);
  850. result = make_number(FUNC_EXP(arglist->val->u.i));
  851. return result;
  852. } else {
  853. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  854. return make_number(0.0);
  855. }
  856. #endif
  857. #ifdef FUNC_EXP2
  858. } else if (strcmp(funcname->u.s,"EXP2") == 0) {
  859. if (arglist && !arglist->right && arglist->val){
  860. to_number(arglist->val);
  861. result = make_number(FUNC_EXP2(arglist->val->u.i));
  862. return result;
  863. } else {
  864. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  865. return make_number(0.0);
  866. }
  867. #endif
  868. #ifdef FUNC_EXP10
  869. } else if (strcmp(funcname->u.s,"EXP10") == 0) {
  870. if (arglist && !arglist->right && arglist->val){
  871. to_number(arglist->val);
  872. result = make_number(FUNC_EXP10(arglist->val->u.i));
  873. return result;
  874. } else {
  875. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  876. return make_number(0.0);
  877. }
  878. #endif
  879. #ifdef FUNC_LOG
  880. } else if (strcmp(funcname->u.s,"LOG") == 0) {
  881. if (arglist && !arglist->right && arglist->val){
  882. to_number(arglist->val);
  883. result = make_number(FUNC_LOG(arglist->val->u.i));
  884. return result;
  885. } else {
  886. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  887. return make_number(0.0);
  888. }
  889. #endif
  890. #ifdef FUNC_LOG2
  891. } else if (strcmp(funcname->u.s,"LOG2") == 0) {
  892. if (arglist && !arglist->right && arglist->val){
  893. to_number(arglist->val);
  894. result = make_number(FUNC_LOG2(arglist->val->u.i));
  895. return result;
  896. } else {
  897. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  898. return make_number(0.0);
  899. }
  900. #endif
  901. #ifdef FUNC_LOG10
  902. } else if (strcmp(funcname->u.s,"LOG10") == 0) {
  903. if (arglist && !arglist->right && arglist->val){
  904. to_number(arglist->val);
  905. result = make_number(FUNC_LOG10(arglist->val->u.i));
  906. return result;
  907. } else {
  908. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  909. return make_number(0.0);
  910. }
  911. #endif
  912. #ifdef FUNC_REMAINDER
  913. } else if (strcmp(funcname->u.s,"REMAINDER") == 0) {
  914. if (arglist && arglist->right && !arglist->right->right && arglist->val && arglist->right->val){
  915. to_number(arglist->val);
  916. to_number(arglist->right->val);
  917. result = make_number(FUNC_REMAINDER(arglist->val->u.i, arglist->right->val->u.i));
  918. return result;
  919. } else {
  920. ast_log(LOG_WARNING,"Wrong args to %s() function\n",funcname->u.s);
  921. return make_number(0.0);
  922. }
  923. #endif
  924. } else if (strcmp(funcname->u.s, "ABS") == 0) {
  925. if (arglist && !arglist->right && arglist->val) {
  926. to_number(arglist->val);
  927. result = make_number(arglist->val->u.i < 0 ? arglist->val->u.i * -1 : arglist->val->u.i);
  928. return result;
  929. } else {
  930. ast_log(LOG_WARNING, "Wrong args to %s() function\n", funcname->u.s);
  931. return make_number(0.0);
  932. }
  933. } else {
  934. /* is this a custom function we should execute and collect the results of? */
  935. #if !defined(STANDALONE) && !defined(STANDALONE2)
  936. struct ast_custom_function *f = ast_custom_function_find(funcname->u.s);
  937. if (!chan)
  938. ast_log(LOG_WARNING,"Hey! chan is NULL.\n");
  939. if (!f)
  940. ast_log(LOG_WARNING,"Hey! could not find func %s.\n", funcname->u.s);
  941. if (f && chan) {
  942. if (f->read) {
  943. char workspace[512];
  944. char *argbuf = compose_func_args(arglist);
  945. f->read(chan, funcname->u.s, argbuf, workspace, sizeof(workspace));
  946. free(argbuf);
  947. if (is_really_num(workspace))
  948. return make_number(FUNC_STRTOD(workspace,(char **)NULL));
  949. else
  950. return make_str(workspace);
  951. } else {
  952. ast_log(LOG_ERROR,"Error! Function '%s' cannot be read!\n", funcname->u.s);
  953. return (make_number ((FP___TYPE)0.0));
  954. }
  955. } else {
  956. ast_log(LOG_ERROR, "Error! '%s' doesn't appear to be an available function!\n", funcname->u.s);
  957. return (make_number ((FP___TYPE)0.0));
  958. }
  959. #else
  960. ast_log(LOG_ERROR, "Error! '%s' is not available in the standalone version!\n", funcname->u.s);
  961. return (make_number ((FP___TYPE)0.0));
  962. #endif
  963. }
  964. }
  965. else
  966. {
  967. ast_log(LOG_ERROR, "Error! '%s' is not possibly a function name!\n", funcname->u.s);
  968. return (make_number ((FP___TYPE)0.0));
  969. }
  970. return (make_number ((FP___TYPE)0.0));
  971. }
  972. static struct val *
  973. op_or (struct val *a, struct val *b)
  974. {
  975. if (is_zero_or_null (a)) {
  976. free_value (a);
  977. return (b);
  978. } else {
  979. free_value (b);
  980. return (a);
  981. }
  982. }
  983. static struct val *
  984. op_and (struct val *a, struct val *b)
  985. {
  986. if (is_zero_or_null (a) || is_zero_or_null (b)) {
  987. free_value (a);
  988. free_value (b);
  989. return (make_number ((FP___TYPE)0.0));
  990. } else {
  991. free_value (b);
  992. return (a);
  993. }
  994. }
  995. static struct val *
  996. op_eq (struct val *a, struct val *b)
  997. {
  998. struct val *r;
  999. if (isstring (a) || isstring (b)) {
  1000. to_string (a);
  1001. to_string (b);
  1002. r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) == 0));
  1003. } else {
  1004. #ifdef DEBUG_FOR_CONVERSIONS
  1005. char buffer[2000];
  1006. sprintf(buffer,"Converting '%s' and '%s' ", a->u.s, b->u.s);
  1007. #endif
  1008. (void)to_number(a);
  1009. (void)to_number(b);
  1010. #ifdef DEBUG_FOR_CONVERSIONS
  1011. ast_log(LOG_WARNING,"%s to '%lld' and '%lld'\n", buffer, a->u.i, b->u.i);
  1012. #endif
  1013. r = make_number ((FP___TYPE)(a->u.i == b->u.i));
  1014. }
  1015. free_value (a);
  1016. free_value (b);
  1017. return r;
  1018. }
  1019. static struct val *
  1020. op_gt (struct val *a, struct val *b)
  1021. {
  1022. struct val *r;
  1023. if (isstring (a) || isstring (b)) {
  1024. to_string (a);
  1025. to_string (b);
  1026. r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) > 0));
  1027. } else {
  1028. (void)to_number(a);
  1029. (void)to_number(b);
  1030. r = make_number ((FP___TYPE)(a->u.i > b->u.i));
  1031. }
  1032. free_value (a);
  1033. free_value (b);
  1034. return r;
  1035. }
  1036. static struct val *
  1037. op_lt (struct val *a, struct val *b)
  1038. {
  1039. struct val *r;
  1040. if (isstring (a) || isstring (b)) {
  1041. to_string (a);
  1042. to_string (b);
  1043. r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) < 0));
  1044. } else {
  1045. (void)to_number(a);
  1046. (void)to_number(b);
  1047. r = make_number ((FP___TYPE)(a->u.i < b->u.i));
  1048. }
  1049. free_value (a);
  1050. free_value (b);
  1051. return r;
  1052. }
  1053. static struct val *
  1054. op_ge (struct val *a, struct val *b)
  1055. {
  1056. struct val *r;
  1057. if (isstring (a) || isstring (b)) {
  1058. to_string (a);
  1059. to_string (b);
  1060. r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) >= 0));
  1061. } else {
  1062. (void)to_number(a);
  1063. (void)to_number(b);
  1064. r = make_number ((FP___TYPE)(a->u.i >= b->u.i));
  1065. }
  1066. free_value (a);
  1067. free_value (b);
  1068. return r;
  1069. }
  1070. static struct val *
  1071. op_le (struct val *a, struct val *b)
  1072. {
  1073. struct val *r;
  1074. if (isstring (a) || isstring (b)) {
  1075. to_string (a);
  1076. to_string (b);
  1077. r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) <= 0));
  1078. } else {
  1079. (void)to_number(a);
  1080. (void)to_number(b);
  1081. r = make_number ((FP___TYPE)(a->u.i <= b->u.i));
  1082. }
  1083. free_value (a);
  1084. free_value (b);
  1085. return r;
  1086. }
  1087. static struct val *
  1088. op_cond (struct val *a, struct val *b, struct val *c)
  1089. {
  1090. struct val *r;
  1091. if( isstring(a) )
  1092. {
  1093. if( strlen(a->u.s) && strcmp(a->u.s, "\"\"") != 0 && strcmp(a->u.s,"0") != 0 )
  1094. {
  1095. free_value(a);
  1096. free_value(c);
  1097. r = b;
  1098. }
  1099. else
  1100. {
  1101. free_value(a);
  1102. free_value(b);
  1103. r = c;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. (void)to_number(a);
  1109. if( a->u.i )
  1110. {
  1111. free_value(a);
  1112. free_value(c);
  1113. r = b;
  1114. }
  1115. else
  1116. {
  1117. free_value(a);
  1118. free_value(b);
  1119. r = c;
  1120. }
  1121. }
  1122. return r;
  1123. }
  1124. static struct val *
  1125. op_ne (struct val *a, struct val *b)
  1126. {
  1127. struct val *r;
  1128. if (isstring (a) || isstring (b)) {
  1129. to_string (a);
  1130. to_string (b);
  1131. r = make_number ((FP___TYPE)(strcoll (a->u.s, b->u.s) != 0));
  1132. } else {
  1133. (void)to_number(a);
  1134. (void)to_number(b);
  1135. r = make_number ((FP___TYPE)(a->u.i != b->u.i));
  1136. }
  1137. free_value (a);
  1138. free_value (b);
  1139. return r;
  1140. }
  1141. static int
  1142. chk_plus (FP___TYPE a, FP___TYPE b, FP___TYPE r)
  1143. {
  1144. /* sum of two positive numbers must be positive */
  1145. if (a > 0 && b > 0 && r <= 0)
  1146. return 1;
  1147. /* sum of two negative numbers must be negative */
  1148. if (a < 0 && b < 0 && r >= 0)
  1149. return 1;
  1150. /* all other cases are OK */
  1151. return 0;
  1152. }
  1153. static struct val *
  1154. op_plus (struct val *a, struct val *b)
  1155. {
  1156. struct val *r;
  1157. if (!to_number (a)) {
  1158. if( !extra_error_message_supplied )
  1159. ast_log(LOG_WARNING,"non-numeric argument\n");
  1160. if (!to_number (b)) {
  1161. free_value(a);
  1162. free_value(b);
  1163. return make_number(0);
  1164. } else {
  1165. free_value(a);
  1166. return (b);
  1167. }
  1168. } else if (!to_number(b)) {
  1169. free_value(b);
  1170. return (a);
  1171. }
  1172. r = make_number (a->u.i + b->u.i);
  1173. if (chk_plus (a->u.i, b->u.i, r->u.i)) {
  1174. ast_log(LOG_WARNING,"overflow\n");
  1175. }
  1176. free_value (a);
  1177. free_value (b);
  1178. return r;
  1179. }
  1180. static int
  1181. chk_minus (FP___TYPE a, FP___TYPE b, FP___TYPE r)
  1182. {
  1183. /* special case subtraction of QUAD_MIN */
  1184. if (b == QUAD_MIN) {
  1185. if (a >= 0)
  1186. return 1;
  1187. else
  1188. return 0;
  1189. }
  1190. /* this is allowed for b != QUAD_MIN */
  1191. return chk_plus (a, -b, r);
  1192. }
  1193. static struct val *
  1194. op_minus (struct val *a, struct val *b)
  1195. {
  1196. struct val *r;
  1197. if (!to_number (a)) {
  1198. if( !extra_error_message_supplied )
  1199. ast_log(LOG_WARNING, "non-numeric argument\n");
  1200. if (!to_number (b)) {
  1201. free_value(a);
  1202. free_value(b);
  1203. return make_number(0);
  1204. } else {
  1205. r = make_number(0 - b->u.i);
  1206. free_value(a);
  1207. free_value(b);
  1208. return (r);
  1209. }
  1210. } else if (!to_number(b)) {
  1211. if( !extra_error_message_supplied )
  1212. ast_log(LOG_WARNING, "non-numeric argument\n");
  1213. free_value(b);
  1214. return (a);
  1215. }
  1216. r = make_number (a->u.i - b->u.i);
  1217. if (chk_minus (a->u.i, b->u.i, r->u.i)) {
  1218. ast_log(LOG_WARNING, "overflow\n");
  1219. }
  1220. free_value (a);
  1221. free_value (b);
  1222. return r;
  1223. }
  1224. static struct val *
  1225. op_negate (struct val *a)
  1226. {
  1227. struct val *r;
  1228. if (!to_number (a) ) {
  1229. free_value(a);
  1230. if( !extra_error_message_supplied )
  1231. ast_log(LOG_WARNING, "non-numeric argument\n");
  1232. return make_number(0);
  1233. }
  1234. r = make_number (- a->u.i);
  1235. if (chk_minus (0, a->u.i, r->u.i)) {
  1236. ast_log(LOG_WARNING, "overflow\n");
  1237. }
  1238. free_value (a);
  1239. return r;
  1240. }
  1241. static struct val *
  1242. op_compl (struct val *a)
  1243. {
  1244. int v1 = 1;
  1245. struct val *r;
  1246. if( !a )
  1247. {
  1248. v1 = 0;
  1249. }
  1250. else
  1251. {
  1252. switch( a->type )
  1253. {
  1254. case AST_EXPR_number:
  1255. if( a->u.i == 0 )
  1256. v1 = 0;
  1257. break;
  1258. case AST_EXPR_string:
  1259. if( a->u.s == 0 )
  1260. v1 = 0;
  1261. else
  1262. {
  1263. if( a->u.s[0] == 0 )
  1264. v1 = 0;
  1265. else if (strlen(a->u.s) == 1 && a->u.s[0] == '0' )
  1266. v1 = 0;
  1267. else
  1268. v1 = atoi(a->u.s);
  1269. }
  1270. break;
  1271. case AST_EXPR_numeric_string:
  1272. if( a->u.s == 0 )
  1273. v1 = 0;
  1274. else
  1275. {
  1276. if( a->u.s[0] == 0 )
  1277. v1 = 0;
  1278. else if (strlen(a->u.s) == 1 && a->u.s[0] == '0' )
  1279. v1 = 0;
  1280. else
  1281. v1 = atoi(a->u.s);
  1282. }
  1283. break;
  1284. }
  1285. }
  1286. r = make_number (!v1);
  1287. free_value (a);
  1288. return r;
  1289. }
  1290. static int
  1291. chk_times (FP___TYPE a, FP___TYPE b, FP___TYPE r)
  1292. {
  1293. /* special case: first operand is 0, no overflow possible */
  1294. if (a == 0)
  1295. return 0;
  1296. /* cerify that result of division matches second operand */
  1297. if (r / a != b)
  1298. return 1;
  1299. return 0;
  1300. }
  1301. static struct val *
  1302. op_times (struct val *a, struct val *b)
  1303. {
  1304. struct val *r;
  1305. if (!to_number (a) || !to_number (b)) {
  1306. free_value(a);
  1307. free_value(b);
  1308. if( !extra_error_message_supplied )
  1309. ast_log(LOG_WARNING, "non-numeric argument\n");
  1310. return(make_number(0));
  1311. }
  1312. r = make_number (a->u.i * b->u.i);
  1313. if (chk_times (a->u.i, b->u.i, r->u.i)) {
  1314. ast_log(LOG_WARNING, "overflow\n");
  1315. }
  1316. free_value (a);
  1317. free_value (b);
  1318. return (r);
  1319. }
  1320. static int
  1321. chk_div (FP___TYPE a, FP___TYPE b)
  1322. {
  1323. /* div by zero has been taken care of before */
  1324. /* only QUAD_MIN / -1 causes overflow */
  1325. if (a == QUAD_MIN && b == -1)
  1326. return 1;
  1327. /* everything else is OK */
  1328. return 0;
  1329. }
  1330. static struct val *
  1331. op_div (struct val *a, struct val *b)
  1332. {
  1333. struct val *r;
  1334. if (!to_number (a)) {
  1335. free_value(a);
  1336. free_value(b);
  1337. if( !extra_error_message_supplied )
  1338. ast_log(LOG_WARNING, "non-numeric argument\n");
  1339. return make_number(0);
  1340. } else if (!to_number (b)) {
  1341. free_value(a);
  1342. free_value(b);
  1343. if( !extra_error_message_supplied )
  1344. ast_log(LOG_WARNING, "non-numeric argument\n");
  1345. return make_number(INT_MAX);
  1346. }
  1347. if (b->u.i == 0) {
  1348. ast_log(LOG_WARNING, "division by zero\n");
  1349. free_value(a);
  1350. free_value(b);
  1351. return make_number(INT_MAX);
  1352. }
  1353. r = make_number (a->u.i / b->u.i);
  1354. if (chk_div (a->u.i, b->u.i)) {
  1355. ast_log(LOG_WARNING, "overflow\n");
  1356. }
  1357. free_value (a);
  1358. free_value (b);
  1359. return r;
  1360. }
  1361. static struct val *
  1362. op_rem (struct val *a, struct val *b)
  1363. {
  1364. struct val *r;
  1365. if (!to_number (a) || !to_number (b)) {
  1366. if( !extra_error_message_supplied )
  1367. ast_log(LOG_WARNING, "non-numeric argument\n");
  1368. free_value(a);
  1369. free_value(b);
  1370. return make_number(0);
  1371. }
  1372. if (b->u.i == 0) {
  1373. ast_log(LOG_WARNING, "div by zero\n");
  1374. free_value(a);
  1375. return(b);
  1376. }
  1377. r = make_number (FUNC_FMOD(a->u.i, b->u.i)); /* either fmod or fmodl if FP___TYPE is available */
  1378. /* chk_rem necessary ??? */
  1379. free_value (a);
  1380. free_value (b);
  1381. return r;
  1382. }
  1383. static struct val *
  1384. op_colon (struct val *a, struct val *b)
  1385. {
  1386. regex_t rp;
  1387. regmatch_t rm[2];
  1388. char errbuf[256];
  1389. int eval;
  1390. struct val *v;
  1391. /* coerce to both arguments to strings */
  1392. to_string(a);
  1393. to_string(b);
  1394. /* strip double quotes from both -- they'll screw up the pattern, and the search string starting at ^ */
  1395. strip_quotes(a);
  1396. strip_quotes(b);
  1397. /* compile regular expression */
  1398. if ((eval = regcomp (&rp, b->u.s, REG_EXTENDED)) != 0) {
  1399. regerror (eval, &rp, errbuf, sizeof(errbuf));
  1400. ast_log(LOG_WARNING, "regcomp() error : %s\n", errbuf);
  1401. free_value(a);
  1402. free_value(b);
  1403. return make_str("");
  1404. }
  1405. /* compare string against pattern */
  1406. /* remember that patterns are anchored to the beginning of the line */
  1407. if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) {
  1408. if (rm[1].rm_so >= 0) {
  1409. *(a->u.s + rm[1].rm_eo) = '\0';
  1410. v = make_str (a->u.s + rm[1].rm_so);
  1411. } else {
  1412. v = make_number ((FP___TYPE)(rm[0].rm_eo - rm[0].rm_so));
  1413. }
  1414. } else {
  1415. if (rp.re_nsub == 0) {
  1416. v = make_number ((FP___TYPE)0);
  1417. } else {
  1418. v = make_str ("");
  1419. }
  1420. }
  1421. /* free arguments and pattern buffer */
  1422. free_value (a);
  1423. free_value (b);
  1424. regfree (&rp);
  1425. return v;
  1426. }
  1427. static struct val *
  1428. op_eqtilde (struct val *a, struct val *b)
  1429. {
  1430. regex_t rp;
  1431. regmatch_t rm[2];
  1432. char errbuf[256];
  1433. int eval;
  1434. struct val *v;
  1435. /* coerce to both arguments to strings */
  1436. to_string(a);
  1437. to_string(b);
  1438. /* strip double quotes from both -- they'll screw up the pattern, and the search string starting at ^ */
  1439. strip_quotes(a);
  1440. strip_quotes(b);
  1441. /* compile regular expression */
  1442. if ((eval = regcomp (&rp, b->u.s, REG_EXTENDED)) != 0) {
  1443. regerror (eval, &rp, errbuf, sizeof(errbuf));
  1444. ast_log(LOG_WARNING, "regcomp() error : %s\n", errbuf);
  1445. free_value(a);
  1446. free_value(b);
  1447. return make_str("");
  1448. }
  1449. /* compare string against pattern */
  1450. /* remember that patterns are anchored to the beginning of the line */
  1451. if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 ) {
  1452. if (rm[1].rm_so >= 0) {
  1453. *(a->u.s + rm[1].rm_eo) = '\0';
  1454. v = make_str (a->u.s + rm[1].rm_so);
  1455. } else {
  1456. v = make_number ((FP___TYPE)(rm[0].rm_eo - rm[0].rm_so));
  1457. }
  1458. } else {
  1459. if (rp.re_nsub == 0) {
  1460. v = make_number ((FP___TYPE)0.0);
  1461. } else {
  1462. v = make_str ("");
  1463. }
  1464. }
  1465. /* free arguments and pattern buffer */
  1466. free_value (a);
  1467. free_value (b);
  1468. regfree (&rp);
  1469. return v;
  1470. }
  1471. static struct val * /* this is a string concat operator */
  1472. op_tildetilde (struct val *a, struct val *b)
  1473. {
  1474. struct val *v;
  1475. char *vs;
  1476. /* coerce to both arguments to strings */
  1477. to_string(a);
  1478. to_string(b);
  1479. /* strip double quotes from both -- */
  1480. strip_quotes(a);
  1481. strip_quotes(b);
  1482. vs = malloc(strlen(a->u.s)+strlen(b->u.s)+1);
  1483. if (vs == NULL) {
  1484. ast_log(LOG_WARNING, "malloc() failed\n");
  1485. free_value(a);
  1486. free_value(b);
  1487. return NULL;
  1488. }
  1489. strcpy(vs,a->u.s);
  1490. strcat(vs,b->u.s);
  1491. v = make_str(vs);
  1492. free(vs);
  1493. /* free arguments */
  1494. free_value(a);
  1495. free_value(b);
  1496. return v;
  1497. }