ast_expr2.y 41 KB

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