ast_expr.y 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. * $FreeBSD: src/bin/expr/expr.y,v 1.16 2000/07/22 10:59:36 se Exp $
  8. */
  9. #include <sys/types.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <locale.h>
  14. #include <ctype.h>
  15. #include <err.h>
  16. #include <errno.h>
  17. #include <regex.h>
  18. #include <limits.h>
  19. #include <asterisk/ast_expr.h>
  20. #include <asterisk/logger.h>
  21. #ifdef LONG_LONG_MIN
  22. #define QUAD_MIN LONG_LONG_MIN
  23. #endif
  24. #ifdef LONG_LONG_MAX
  25. #define QUAD_MAX LONG_LONG_MAX
  26. #endif
  27. # if ! defined(QUAD_MIN)
  28. # define QUAD_MIN (-0x7fffffffffffffffL-1)
  29. # endif
  30. # if ! defined(QUAD_MAX)
  31. # define QUAD_MAX (0x7fffffffffffffffL)
  32. # endif
  33. #define YYPARSE_PARAM kota
  34. #define YYLEX_PARAM kota
  35. /* #define ast_log fprintf
  36. #define LOG_WARNING stderr */
  37. enum valtype {
  38. integer, numeric_string, string
  39. } ;
  40. struct val {
  41. enum valtype type;
  42. union {
  43. char *s;
  44. quad_t i;
  45. } u;
  46. } ;
  47. struct parser_control {
  48. struct val *result;
  49. int pipa;
  50. char *arg_orig;
  51. char *argv;
  52. char *ptrptr;
  53. int firsttoken;
  54. } ;
  55. static int chk_div __P((quad_t, quad_t));
  56. static int chk_minus __P((quad_t, quad_t, quad_t));
  57. static int chk_plus __P((quad_t, quad_t, quad_t));
  58. static int chk_times __P((quad_t, quad_t, quad_t));
  59. static void free_value __P((struct val *));
  60. static int is_zero_or_null __P((struct val *));
  61. static int isstring __P((struct val *));
  62. static struct val *make_integer __P((quad_t));
  63. static struct val *make_str __P((const char *));
  64. static struct val *op_and __P((struct val *, struct val *));
  65. static struct val *op_colon __P((struct val *, struct val *));
  66. static struct val *op_div __P((struct val *, struct val *));
  67. static struct val *op_eq __P((struct val *, struct val *));
  68. static struct val *op_ge __P((struct val *, struct val *));
  69. static struct val *op_gt __P((struct val *, struct val *));
  70. static struct val *op_le __P((struct val *, struct val *));
  71. static struct val *op_lt __P((struct val *, struct val *));
  72. static struct val *op_minus __P((struct val *, struct val *));
  73. static struct val *op_ne __P((struct val *, struct val *));
  74. static struct val *op_or __P((struct val *, struct val *));
  75. static struct val *op_plus __P((struct val *, struct val *));
  76. static struct val *op_rem __P((struct val *, struct val *));
  77. static struct val *op_times __P((struct val *, struct val *));
  78. static quad_t to_integer __P((struct val *));
  79. static void to_string __P((struct val *));
  80. /* uh, if I want to predeclare yylex with a YYLTYPE, I have to predeclare the yyltype... sigh */
  81. typedef struct yyltype
  82. {
  83. int first_line;
  84. int first_column;
  85. int last_line;
  86. int last_column;
  87. } yyltype;
  88. # define YYLTYPE yyltype
  89. # define YYLTYPE_IS_TRIVIAL 1
  90. static int ast_yyerror __P((const char *,YYLTYPE *, struct parser_control *));
  91. #define ast_yyerror(x) ast_yyerror(x,&yyloc,kota)
  92. %}
  93. %pure-parser
  94. %locations
  95. /* %debug for when you are having big problems */
  96. /* %name-prefix="ast_yy" */
  97. %union
  98. {
  99. struct val *val;
  100. }
  101. %{
  102. static int ast_yylex __P((YYSTYPE *, YYLTYPE *, struct parser_control *));
  103. %}
  104. %left <val> '|'
  105. %left <val> '&'
  106. %left <val> '=' '>' '<' GE LE NE
  107. %left <val> '+' '-'
  108. %left <val> '*' '/' '%'
  109. %left <val> ':'
  110. %token <val> TOKEN
  111. %type <val> start expr
  112. %%
  113. start: expr { ((struct parser_control *)kota)->result = $$; }
  114. ;
  115. expr: TOKEN
  116. | '(' expr ')' { $$ = $2; @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  117. | expr '|' expr { $$ = op_or ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  118. | expr '&' expr { $$ = op_and ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  119. | expr '=' expr { $$ = op_eq ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  120. | expr '>' expr { $$ = op_gt ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  121. | expr '<' expr { $$ = op_lt ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  122. | expr GE expr { $$ = op_ge ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  123. | expr LE expr { $$ = op_le ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  124. | expr NE expr { $$ = op_ne ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  125. | expr '+' expr { $$ = op_plus ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  126. | expr '-' expr { $$ = op_minus ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  127. | expr '*' expr { $$ = op_times ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  128. | expr '/' expr { $$ = op_div ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  129. | expr '%' expr { $$ = op_rem ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  130. | expr ':' expr { $$ = op_colon ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
  131. ;
  132. %%
  133. static struct val *
  134. make_integer (i)
  135. quad_t i;
  136. {
  137. struct val *vp;
  138. vp = (struct val *) malloc (sizeof (*vp));
  139. if (vp == NULL) {
  140. ast_log(LOG_WARNING, "malloc() failed\n");
  141. return(NULL);
  142. }
  143. vp->type = integer;
  144. vp->u.i = i;
  145. return vp;
  146. }
  147. static struct val *
  148. make_str (s)
  149. const char *s;
  150. {
  151. struct val *vp;
  152. size_t i;
  153. int isint;
  154. vp = (struct val *) malloc (sizeof (*vp));
  155. if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) {
  156. ast_log(LOG_WARNING,"malloc() failed\n");
  157. return(NULL);
  158. }
  159. for(i = 1, isint = isdigit(s[0]) || s[0] == '-';
  160. isint && i < strlen(s);
  161. i++)
  162. {
  163. if(!isdigit(s[i]))
  164. isint = 0;
  165. }
  166. if (isint)
  167. vp->type = numeric_string;
  168. else
  169. vp->type = string;
  170. return vp;
  171. }
  172. static void
  173. free_value (vp)
  174. struct val *vp;
  175. {
  176. if (vp==NULL) {
  177. return;
  178. }
  179. if (vp->type == string || vp->type == numeric_string)
  180. free (vp->u.s);
  181. free (vp);
  182. }
  183. static quad_t
  184. to_integer (vp)
  185. struct val *vp;
  186. {
  187. quad_t i;
  188. if (vp == NULL) {
  189. ast_log(LOG_WARNING,"vp==NULL in to_integer()\n");
  190. return(0);
  191. }
  192. if (vp->type == integer)
  193. return 1;
  194. if (vp->type == string)
  195. return 0;
  196. /* vp->type == numeric_string, make it numeric */
  197. errno = 0;
  198. i = strtoq(vp->u.s, (char**)NULL, 10);
  199. if (errno != 0) {
  200. free(vp->u.s);
  201. ast_log(LOG_WARNING,"overflow\n");
  202. return(0);
  203. }
  204. free (vp->u.s);
  205. vp->u.i = i;
  206. vp->type = integer;
  207. return 1;
  208. }
  209. static void
  210. to_string (vp)
  211. struct val *vp;
  212. {
  213. char *tmp;
  214. if (vp->type == string || vp->type == numeric_string)
  215. return;
  216. tmp = malloc ((size_t)25);
  217. if (tmp == NULL) {
  218. ast_log(LOG_WARNING,"malloc() failed\n");
  219. return;
  220. }
  221. sprintf (tmp, "%lld", (long long)vp->u.i);
  222. vp->type = string;
  223. vp->u.s = tmp;
  224. }
  225. static int
  226. isstring (vp)
  227. struct val *vp;
  228. {
  229. /* only TRUE if this string is not a valid integer */
  230. return (vp->type == string);
  231. }
  232. static int
  233. ast_yylex (YYSTYPE *lvalp, YYLTYPE *yylloc, struct parser_control *karoto)
  234. {
  235. char *p=0;
  236. char *t1=0;
  237. char savep = 0;
  238. char *savepp = 0;
  239. if (karoto->firsttoken==1) {
  240. t1 = karoto->argv;
  241. karoto->firsttoken = 0;
  242. } else {
  243. t1 = karoto->ptrptr;
  244. }
  245. while(*t1 && *t1 == ' ' ) /* we can remove worries about leading/multiple spaces being present */
  246. t1++;
  247. karoto->ptrptr = t1;
  248. yylloc->first_column = t1 - karoto->argv;
  249. while( *t1 && *t1 != ' ' && *t1 != '"') /* find the next space or quote */
  250. t1++;
  251. if( *t1 == ' ' )
  252. {
  253. *t1 = 0;
  254. p = karoto->ptrptr;
  255. karoto->ptrptr = t1+1;
  256. yylloc->last_column = t1 - karoto->argv;
  257. }
  258. else if (*t1 == '"' )
  259. {
  260. /* opening quote. find the closing quote */
  261. char *t2=t1+1;
  262. while( *t2 && *t2 != '"')
  263. t2++;
  264. if( *t2 == '"' )
  265. {
  266. if( *(t2+1) == ' ' || *(t2+1) == 0 )
  267. {
  268. if( *(t2+1) )
  269. {
  270. *(t2+1) = 0;
  271. karoto->ptrptr = t2+2;
  272. }
  273. else
  274. {
  275. karoto->ptrptr = t2+1;
  276. }
  277. }
  278. else
  279. {
  280. /* hmmm. what if another token is here? */
  281. /* maybe we can insert a space? */
  282. savep = *(t2+1);
  283. savepp = t2+1;
  284. *(t2+1) = 0;
  285. karoto->ptrptr = t2+1;
  286. }
  287. p = t1;
  288. }
  289. else
  290. {
  291. /* NOT GOOD -- no closing quote! */
  292. p = t1;
  293. karoto->ptrptr = t2;
  294. }
  295. yylloc->last_column = t2 - karoto->argv;
  296. }
  297. else if( *t1 == 0 )
  298. {
  299. if( t1 != karoto->ptrptr )
  300. {
  301. /* this is the last token */
  302. p = karoto->ptrptr;
  303. karoto->ptrptr = t1;
  304. }
  305. else
  306. {
  307. /* we are done. That was quick */
  308. p = karoto->ptrptr;
  309. yylloc->last_column = t1 - karoto->argv;
  310. }
  311. }
  312. if( *p == 0 )
  313. p = 0;
  314. if (p==NULL) {
  315. return (0);
  316. }
  317. if (strlen (p) == 1) {
  318. if (strchr ("|&=<>+-*/%:()", *p))
  319. return (*p);
  320. } else if (strlen (p) == 2 && p[1] == '=') {
  321. switch (*p) {
  322. case '>': return (GE);
  323. case '<': return (LE);
  324. case '!': return (NE);
  325. }
  326. }
  327. lvalp->val = make_str (p);
  328. if( savep )
  329. {
  330. *savepp = savep; /* restore the null terminated string */
  331. savepp = 0;
  332. savep = 0;
  333. }
  334. return (TOKEN);
  335. }
  336. static int
  337. is_zero_or_null (vp)
  338. struct val *vp;
  339. {
  340. if (vp->type == integer) {
  341. return (vp->u.i == 0);
  342. } else {
  343. return (*vp->u.s == 0 || (to_integer (vp) && vp->u.i == 0));
  344. }
  345. /* NOTREACHED */
  346. }
  347. char *ast_expr (char *arg)
  348. {
  349. struct parser_control karoto;
  350. char *kota;
  351. char *pirouni;
  352. kota=strdup(arg);
  353. karoto.result = NULL;
  354. karoto.firsttoken=1;
  355. karoto.argv=kota;
  356. karoto.arg_orig = arg;
  357. /* ast_yydebug = 1; */
  358. ast_yyparse ((void *)&karoto);
  359. free(kota);
  360. if (karoto.result==NULL) {
  361. pirouni=strdup("0");
  362. return(pirouni);
  363. } else {
  364. if (karoto.result->type == integer) {
  365. pirouni=malloc(256);
  366. sprintf (pirouni,"%lld", (long long)karoto.result->u.i);
  367. }
  368. else {
  369. pirouni=strdup(karoto.result->u.s);
  370. }
  371. free(karoto.result);
  372. }
  373. return(pirouni);
  374. }
  375. #ifdef STANDALONE
  376. int main(int argc,char **argv) {
  377. char *s;
  378. s=ast_expr(argv[1]);
  379. printf("=====%s======\n",s);
  380. }
  381. #endif
  382. #undef ast_yyerror
  383. #define ast_yyerror(x) ast_yyerror(x, YYLTYPE *yylloc, struct parser_control *karoto)
  384. static int
  385. ast_yyerror (const char *s)
  386. {
  387. char spacebuf[8000]; /* best safe than sorry */
  388. char spacebuf2[8000]; /* best safe than sorry */
  389. int i=0;
  390. spacebuf[0] = 0;
  391. if( yylloc->first_column > 7990 ) /* if things get out of whack, why crash? */
  392. yylloc->first_column = 7990;
  393. if( yylloc->last_column > 7990 )
  394. yylloc->last_column = 7990;
  395. for(i=0;i<yylloc->first_column;i++) spacebuf[i] = ' ';
  396. for( ;i<yylloc->last_column;i++) spacebuf[i] = '^';
  397. spacebuf[i] = 0;
  398. for(i=0;i<karoto->ptrptr-karoto->argv;i++) spacebuf2[i] = ' ';
  399. spacebuf2[i++]='^';
  400. spacebuf2[i]= 0;
  401. ast_log(LOG_WARNING,"ast_yyerror(): syntax error: %s; Input:\n%s\n%s\n%s\n",s,
  402. karoto->arg_orig,spacebuf,spacebuf2);
  403. return(0);
  404. }
  405. static struct val *
  406. op_or (a, b)
  407. struct val *a, *b;
  408. {
  409. if (is_zero_or_null (a)) {
  410. free_value (a);
  411. return (b);
  412. } else {
  413. free_value (b);
  414. return (a);
  415. }
  416. }
  417. static struct val *
  418. op_and (a, b)
  419. struct val *a, *b;
  420. {
  421. if (is_zero_or_null (a) || is_zero_or_null (b)) {
  422. free_value (a);
  423. free_value (b);
  424. return (make_integer ((quad_t)0));
  425. } else {
  426. free_value (b);
  427. return (a);
  428. }
  429. }
  430. static struct val *
  431. op_eq (a, b)
  432. struct val *a, *b;
  433. {
  434. struct val *r;
  435. if (isstring (a) || isstring (b)) {
  436. to_string (a);
  437. to_string (b);
  438. r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) == 0));
  439. } else {
  440. (void)to_integer(a);
  441. (void)to_integer(b);
  442. r = make_integer ((quad_t)(a->u.i == b->u.i));
  443. }
  444. free_value (a);
  445. free_value (b);
  446. return r;
  447. }
  448. static struct val *
  449. op_gt (a, b)
  450. struct val *a, *b;
  451. {
  452. struct val *r;
  453. if (isstring (a) || isstring (b)) {
  454. to_string (a);
  455. to_string (b);
  456. r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) > 0));
  457. } else {
  458. (void)to_integer(a);
  459. (void)to_integer(b);
  460. r = make_integer ((quad_t)(a->u.i > b->u.i));
  461. }
  462. free_value (a);
  463. free_value (b);
  464. return r;
  465. }
  466. static struct val *
  467. op_lt (a, b)
  468. struct val *a, *b;
  469. {
  470. struct val *r;
  471. if (isstring (a) || isstring (b)) {
  472. to_string (a);
  473. to_string (b);
  474. r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) < 0));
  475. } else {
  476. (void)to_integer(a);
  477. (void)to_integer(b);
  478. r = make_integer ((quad_t)(a->u.i < b->u.i));
  479. }
  480. free_value (a);
  481. free_value (b);
  482. return r;
  483. }
  484. static struct val *
  485. op_ge (a, b)
  486. struct val *a, *b;
  487. {
  488. struct val *r;
  489. if (isstring (a) || isstring (b)) {
  490. to_string (a);
  491. to_string (b);
  492. r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) >= 0));
  493. } else {
  494. (void)to_integer(a);
  495. (void)to_integer(b);
  496. r = make_integer ((quad_t)(a->u.i >= b->u.i));
  497. }
  498. free_value (a);
  499. free_value (b);
  500. return r;
  501. }
  502. static struct val *
  503. op_le (a, b)
  504. struct val *a, *b;
  505. {
  506. struct val *r;
  507. if (isstring (a) || isstring (b)) {
  508. to_string (a);
  509. to_string (b);
  510. r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) <= 0));
  511. } else {
  512. (void)to_integer(a);
  513. (void)to_integer(b);
  514. r = make_integer ((quad_t)(a->u.i <= b->u.i));
  515. }
  516. free_value (a);
  517. free_value (b);
  518. return r;
  519. }
  520. static struct val *
  521. op_ne (a, b)
  522. struct val *a, *b;
  523. {
  524. struct val *r;
  525. if (isstring (a) || isstring (b)) {
  526. to_string (a);
  527. to_string (b);
  528. r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) != 0));
  529. } else {
  530. (void)to_integer(a);
  531. (void)to_integer(b);
  532. r = make_integer ((quad_t)(a->u.i != b->u.i));
  533. }
  534. free_value (a);
  535. free_value (b);
  536. return r;
  537. }
  538. static int
  539. chk_plus (a, b, r)
  540. quad_t a, b, r;
  541. {
  542. /* sum of two positive numbers must be positive */
  543. if (a > 0 && b > 0 && r <= 0)
  544. return 1;
  545. /* sum of two negative numbers must be negative */
  546. if (a < 0 && b < 0 && r >= 0)
  547. return 1;
  548. /* all other cases are OK */
  549. return 0;
  550. }
  551. static struct val *
  552. op_plus (a, b)
  553. struct val *a, *b;
  554. {
  555. struct val *r;
  556. if (!to_integer (a)) {
  557. ast_log(LOG_WARNING,"non-numeric argument\n");
  558. if (!to_integer (b)) {
  559. free_value(a);
  560. free_value(b);
  561. return make_integer(0);
  562. } else {
  563. free_value(a);
  564. return (b);
  565. }
  566. } else if (!to_integer(b)) {
  567. free_value(b);
  568. return (a);
  569. }
  570. r = make_integer (/*(quad_t)*/(a->u.i + b->u.i));
  571. if (chk_plus (a->u.i, b->u.i, r->u.i)) {
  572. ast_log(LOG_WARNING,"overflow\n");
  573. }
  574. free_value (a);
  575. free_value (b);
  576. return r;
  577. }
  578. static int
  579. chk_minus (a, b, r)
  580. quad_t a, b, r;
  581. {
  582. /* special case subtraction of QUAD_MIN */
  583. if (b == QUAD_MIN) {
  584. if (a >= 0)
  585. return 1;
  586. else
  587. return 0;
  588. }
  589. /* this is allowed for b != QUAD_MIN */
  590. return chk_plus (a, -b, r);
  591. }
  592. static struct val *
  593. op_minus (a, b)
  594. struct val *a, *b;
  595. {
  596. struct val *r;
  597. if (!to_integer (a)) {
  598. ast_log(LOG_WARNING, "non-numeric argument\n");
  599. if (!to_integer (b)) {
  600. free_value(a);
  601. free_value(b);
  602. return make_integer(0);
  603. } else {
  604. r = make_integer(0 - b->u.i);
  605. free_value(a);
  606. free_value(b);
  607. return (r);
  608. }
  609. } else if (!to_integer(b)) {
  610. ast_log(LOG_WARNING, "non-numeric argument\n");
  611. free_value(b);
  612. return (a);
  613. }
  614. r = make_integer (/*(quad_t)*/(a->u.i - b->u.i));
  615. if (chk_minus (a->u.i, b->u.i, r->u.i)) {
  616. ast_log(LOG_WARNING, "overflow\n");
  617. }
  618. free_value (a);
  619. free_value (b);
  620. return r;
  621. }
  622. static int
  623. chk_times (a, b, r)
  624. quad_t a, b, r;
  625. {
  626. /* special case: first operand is 0, no overflow possible */
  627. if (a == 0)
  628. return 0;
  629. /* cerify that result of division matches second operand */
  630. if (r / a != b)
  631. return 1;
  632. return 0;
  633. }
  634. static struct val *
  635. op_times (a, b)
  636. struct val *a, *b;
  637. {
  638. struct val *r;
  639. if (!to_integer (a) || !to_integer (b)) {
  640. free_value(a);
  641. free_value(b);
  642. ast_log(LOG_WARNING, "non-numeric argument\n");
  643. return(make_integer(0));
  644. }
  645. r = make_integer (/*(quad_t)*/(a->u.i * b->u.i));
  646. if (chk_times (a->u.i, b->u.i, r->u.i)) {
  647. ast_log(LOG_WARNING, "overflow\n");
  648. }
  649. free_value (a);
  650. free_value (b);
  651. return (r);
  652. }
  653. static int
  654. chk_div (a, b)
  655. quad_t a, b;
  656. {
  657. /* div by zero has been taken care of before */
  658. /* only QUAD_MIN / -1 causes overflow */
  659. if (a == QUAD_MIN && b == -1)
  660. return 1;
  661. /* everything else is OK */
  662. return 0;
  663. }
  664. static struct val *
  665. op_div (a, b)
  666. struct val *a, *b;
  667. {
  668. struct val *r;
  669. if (!to_integer (a)) {
  670. free_value(a);
  671. free_value(b);
  672. ast_log(LOG_WARNING, "non-numeric argument\n");
  673. return make_integer(0);
  674. } else if (!to_integer (b)) {
  675. free_value(a);
  676. free_value(b);
  677. ast_log(LOG_WARNING, "non-numeric argument\n");
  678. return make_integer(INT_MAX);
  679. }
  680. if (b->u.i == 0) {
  681. ast_log(LOG_WARNING, "division by zero\n");
  682. free_value(a);
  683. free_value(b);
  684. return make_integer(INT_MAX);
  685. }
  686. r = make_integer (/*(quad_t)*/(a->u.i / b->u.i));
  687. if (chk_div (a->u.i, b->u.i)) {
  688. ast_log(LOG_WARNING, "overflow\n");
  689. }
  690. free_value (a);
  691. free_value (b);
  692. return r;
  693. }
  694. static struct val *
  695. op_rem (a, b)
  696. struct val *a, *b;
  697. {
  698. struct val *r;
  699. if (!to_integer (a) || !to_integer (b)) {
  700. ast_log(LOG_WARNING, "non-numeric argument\n");
  701. free_value(a);
  702. free_value(b);
  703. return make_integer(0);
  704. }
  705. if (b->u.i == 0) {
  706. ast_log(LOG_WARNING, "div by zero\n");
  707. free_value(a);
  708. return (b);
  709. }
  710. r = make_integer (/*(quad_t)*/(a->u.i % b->u.i));
  711. /* chk_rem necessary ??? */
  712. free_value (a);
  713. free_value (b);
  714. return r;
  715. }
  716. static struct val *
  717. op_colon (a, b)
  718. struct val *a, *b;
  719. {
  720. regex_t rp;
  721. regmatch_t rm[2];
  722. char errbuf[256];
  723. int eval;
  724. struct val *v;
  725. /* coerce to both arguments to strings */
  726. to_string(a);
  727. to_string(b);
  728. /* compile regular expression */
  729. if ((eval = regcomp (&rp, b->u.s, REG_EXTENDED)) != 0) {
  730. regerror (eval, &rp, errbuf, sizeof(errbuf));
  731. ast_log(LOG_WARNING,"regcomp() error : %s",errbuf);
  732. free_value(a);
  733. free_value(b);
  734. return make_str("");
  735. }
  736. /* compare string against pattern */
  737. /* remember that patterns are anchored to the beginning of the line */
  738. if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) {
  739. if (rm[1].rm_so >= 0) {
  740. *(a->u.s + rm[1].rm_eo) = '\0';
  741. v = make_str (a->u.s + rm[1].rm_so);
  742. } else {
  743. v = make_integer ((quad_t)(rm[0].rm_eo - rm[0].rm_so));
  744. }
  745. } else {
  746. if (rp.re_nsub == 0) {
  747. v = make_integer ((quad_t)0);
  748. } else {
  749. v = make_str ("");
  750. }
  751. }
  752. /* free arguments and pattern buffer */
  753. free_value (a);
  754. free_value (b);
  755. regfree (&rp);
  756. return v;
  757. }