func_strings.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005-2006, Digium, Inc.
  5. * Portions Copyright (C) 2005, Tilghman Lesher. All rights reserved.
  6. * Portions Copyright (C) 2005, Anthony Minessale II
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief String manipulation dialplan functions
  21. *
  22. * \author Tilghman Lesher
  23. * \author Anothony Minessale II
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <regex.h>
  32. #include <ctype.h>
  33. #include "asterisk/module.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/localtime.h"
  39. #include "asterisk/test.h"
  40. AST_THREADSTORAGE(result_buf);
  41. AST_THREADSTORAGE(tmp_buf);
  42. /*** DOCUMENTATION
  43. <function name="FIELDQTY" language="en_US">
  44. <synopsis>
  45. Count the fields with an arbitrary delimiter
  46. </synopsis>
  47. <syntax>
  48. <parameter name="varname" required="true" />
  49. <parameter name="delim" required="true" />
  50. </syntax>
  51. <description>
  52. <para>The delimiter may be specified as a special or extended ASCII character, by encoding it. The characters
  53. <literal>\n</literal>, <literal>\r</literal>, and <literal>\t</literal> are all recognized as the newline,
  54. carriage return, and tab characters, respectively. Also, octal and hexadecimal specifications are recognized
  55. by the patterns <literal>\0nnn</literal> and <literal>\xHH</literal>, respectively. For example, if you wanted
  56. to encode a comma as the delimiter, you could use either <literal>\054</literal> or <literal>\x2C</literal>.</para>
  57. <para>Example: If ${example} contains <literal>ex-amp-le</literal>, then ${FIELDQTY(example,-)} returns 3.</para>
  58. </description>
  59. </function>
  60. <function name="FIELDNUM" language="en_US">
  61. <synopsis>
  62. Return the 1-based offset of a field in a list
  63. </synopsis>
  64. <syntax>
  65. <parameter name="varname" required="true" />
  66. <parameter name="delim" required="true" />
  67. <parameter name="value" required="true" />
  68. </syntax>
  69. <description>
  70. <para>Search the variable named <replaceable>varname</replaceable> for the string <replaceable>value</replaceable>
  71. delimited by <replaceable>delim</replaceable> and return a 1-based offset as to its location. If not found
  72. or an error occured, return <literal>0</literal>.</para>
  73. <para>The delimiter may be specified as a special or extended ASCII character, by encoding it. The characters
  74. <literal>\n</literal>, <literal>\r</literal>, and <literal>\t</literal> are all recognized as the newline,
  75. carriage return, and tab characters, respectively. Also, octal and hexadecimal specifications are recognized
  76. by the patterns <literal>\0nnn</literal> and <literal>\xHH</literal>, respectively. For example, if you wanted
  77. to encode a comma as the delimiter, you could use either <literal>\054</literal> or <literal>\x2C</literal>.</para>
  78. <para>Example: If ${example} contains <literal>ex-amp-le</literal>, then ${FIELDNUM(example,-,amp)} returns 2.</para>
  79. </description>
  80. </function>
  81. <function name="LISTFILTER" language="en_US">
  82. <synopsis>Remove an item from a list, by name.</synopsis>
  83. <syntax>
  84. <parameter name="varname" required="true" />
  85. <parameter name="delim" required="true" default="," />
  86. <parameter name="value" required="true" />
  87. </syntax>
  88. <description>
  89. <para>Remove <replaceable>value</replaceable> from the list contained in the <replaceable>varname</replaceable>
  90. variable, where the list delimiter is specified by the <replaceable>delim</replaceable> parameter. This is
  91. very useful for removing a single channel name from a list of channels, for example.</para>
  92. </description>
  93. </function>
  94. <function name="FILTER" language="en_US">
  95. <synopsis>
  96. Filter the string to include only the allowed characters
  97. </synopsis>
  98. <syntax>
  99. <parameter name="allowed-chars" required="true" />
  100. <parameter name="string" required="true" />
  101. </syntax>
  102. <description>
  103. <para>Permits all characters listed in <replaceable>allowed-chars</replaceable>,
  104. filtering all others outs. In addition to literally listing the characters,
  105. you may also use ranges of characters (delimited by a <literal>-</literal></para>
  106. <para>Hexadecimal characters started with a <literal>\x</literal>(i.e. \x20)</para>
  107. <para>Octal characters started with a <literal>\0</literal> (i.e. \040)</para>
  108. <para>Also <literal>\t</literal>,<literal>\n</literal> and <literal>\r</literal> are recognized.</para>
  109. <note><para>If you want the <literal>-</literal> character it needs to be prefixed with a
  110. <literal>\</literal></para></note>
  111. </description>
  112. </function>
  113. <function name="REPLACE" language="en_US">
  114. <synopsis>
  115. Replace a set of characters in a given string with another character.
  116. </synopsis>
  117. <syntax>
  118. <parameter name="varname" required="true" />
  119. <parameter name="find-chars" required="true" />
  120. <parameter name="replace-char" required="false" />
  121. </syntax>
  122. <description>
  123. <para>Iterates through a string replacing all the <replaceable>find-chars</replaceable> with
  124. <replaceable>replace-char</replaceable>. <replaceable>replace-char</replaceable> may be either
  125. empty or contain one character. If empty, all <replaceable>find-chars</replaceable> will be
  126. deleted from the output.</para>
  127. <note><para>The replacement only occurs in the output. The original variable is not
  128. altered.</para></note>
  129. </description>
  130. </function>
  131. <function name="STRREPLACE" language="en_US">
  132. <synopsis>
  133. Replace instances of a substring within a string with another string.
  134. </synopsis>
  135. <syntax>
  136. <parameter name="varname" required="true" />
  137. <parameter name="find-string" required="true" />
  138. <parameter name="replace-string" required="false" />
  139. <parameter name="max-replacements" required="false" />
  140. </syntax>
  141. <description>
  142. <para>Searches for all instances of the <replaceable>find-string</replaceable> in provided variable and
  143. replaces them with <replaceable>replace-string</replaceable>. If <replaceable>replace-string</replaceable>
  144. is an empty string, this will effecively delete that substring. If <replaceable>max-replacements</replaceable>
  145. is specified, this function will stop after performing replacements <replaceable>max-replacements</replaceable> times.</para>
  146. <note><para>The replacement only occurs in the output. The original variable is not altered.</para></note>
  147. </description>
  148. </function>
  149. <function name="PASSTHRU" language="en_US">
  150. <synopsis>
  151. Pass the given argument back as a value.
  152. </synopsis>
  153. <syntax>
  154. <parameter name="string" required="false" />
  155. </syntax>
  156. <description>
  157. <para>Literally returns the given <replaceable>string</replaceable>. The intent is to permit
  158. other dialplan functions which take a variable name as an argument to be able to take a literal
  159. string, instead.</para>
  160. </description>
  161. </function>
  162. <function name="REGEX" language="en_US">
  163. <synopsis>
  164. Check string against a regular expression.
  165. </synopsis>
  166. <syntax argsep=" ">
  167. <parameter name="&quot;regular expression&quot;" required="true" />
  168. <parameter name="string" required="true" />
  169. </syntax>
  170. <description>
  171. <para>Return <literal>1</literal> on regular expression match or <literal>0</literal> otherwise</para>
  172. <para>Please note that the space following the double quotes separating the
  173. regex from the data is optional and if present, is skipped. If a space is
  174. desired at the beginning of the data, then put two spaces there; the second
  175. will not be skipped.</para>
  176. </description>
  177. </function>
  178. <application name="ClearHash" language="en_US">
  179. <synopsis>
  180. Clear the keys from a specified hashname.
  181. </synopsis>
  182. <syntax>
  183. <parameter name="hashname" required="true" />
  184. </syntax>
  185. <description>
  186. <para>Clears all keys out of the specified <replaceable>hashname</replaceable>.</para>
  187. </description>
  188. </application>
  189. <function name="HASH" language="en_US">
  190. <synopsis>
  191. Implementation of a dialplan associative array
  192. </synopsis>
  193. <syntax>
  194. <parameter name="hashname" required="true" />
  195. <parameter name="hashkey" />
  196. </syntax>
  197. <description>
  198. <para>In two arguments mode, gets and sets values to corresponding keys within
  199. a named associative array. The single-argument mode will only work when assigned
  200. to from a function defined by func_odbc</para>
  201. </description>
  202. </function>
  203. <function name="HASHKEYS" language="en_US">
  204. <synopsis>
  205. Retrieve the keys of the HASH() function.
  206. </synopsis>
  207. <syntax>
  208. <parameter name="hashname" required="true" />
  209. </syntax>
  210. <description>
  211. <para>Returns a comma-delimited list of the current keys of the associative array
  212. defined by the HASH() function. Note that if you iterate over the keys of
  213. the result, adding keys during iteration will cause the result of the HASHKEYS()
  214. function to change.</para>
  215. </description>
  216. </function>
  217. <function name="KEYPADHASH" language="en_US">
  218. <synopsis>
  219. Hash the letters in string into equivalent keypad numbers.
  220. </synopsis>
  221. <syntax>
  222. <parameter name="string" required="true" />
  223. </syntax>
  224. <description>
  225. <para>Example: ${KEYPADHASH(Les)} returns "537"</para>
  226. </description>
  227. </function>
  228. <function name="ARRAY" language="en_US">
  229. <synopsis>
  230. Allows setting multiple variables at once.
  231. </synopsis>
  232. <syntax>
  233. <parameter name="var1" required="true" />
  234. <parameter name="var2" required="false" multiple="true" />
  235. <parameter name="varN" required="false" />
  236. </syntax>
  237. <description>
  238. <para>The comma-delimited list passed as a value to which the function is set will
  239. be interpreted as a set of values to which the comma-delimited list of
  240. variable names in the argument should be set.</para>
  241. <para>Example: Set(ARRAY(var1,var2)=1,2) will set var1 to 1 and var2 to 2</para>
  242. </description>
  243. </function>
  244. <function name="STRPTIME" language="en_US">
  245. <synopsis>
  246. Returns the epoch of the arbitrary date/time string structured as described by the format.
  247. </synopsis>
  248. <syntax>
  249. <parameter name="datetime" required="true" />
  250. <parameter name="timezone" required="true" />
  251. <parameter name="format" required="true" />
  252. </syntax>
  253. <description>
  254. <para>This is useful for converting a date into <literal>EPOCH</literal> time,
  255. possibly to pass to an application like SayUnixTime or to calculate the difference
  256. between the two date strings</para>
  257. <para>Example: ${STRPTIME(2006-03-01 07:30:35,America/Chicago,%Y-%m-%d %H:%M:%S)} returns 1141219835</para>
  258. </description>
  259. </function>
  260. <function name="STRFTIME" language="en_US">
  261. <synopsis>
  262. Returns the current date/time in the specified format.
  263. </synopsis>
  264. <syntax>
  265. <parameter name="epoch" />
  266. <parameter name="timezone" />
  267. <parameter name="format" />
  268. </syntax>
  269. <description>
  270. <para>STRFTIME supports all of the same formats as the underlying C function
  271. <emphasis>strftime(3)</emphasis>.
  272. It also supports the following format: <literal>%[n]q</literal> - fractions of a second,
  273. with leading zeros.</para>
  274. <para>Example: <literal>%3q</literal> will give milliseconds and <literal>%1q</literal>
  275. will give tenths of a second. The default is set at milliseconds (n=3).
  276. The common case is to use it in combination with %S, as in <literal>%S.%3q</literal>.</para>
  277. </description>
  278. <see-also>
  279. <ref type="manpage">strftime(3)</ref>
  280. </see-also>
  281. </function>
  282. <function name="EVAL" language="en_US">
  283. <synopsis>
  284. Evaluate stored variables
  285. </synopsis>
  286. <syntax>
  287. <parameter name="variable" required="true" />
  288. </syntax>
  289. <description>
  290. <para>Using EVAL basically causes a string to be evaluated twice.
  291. When a variable or expression is in the dialplan, it will be
  292. evaluated at runtime. However, if the results of the evaluation
  293. is in fact another variable or expression, using EVAL will have it
  294. evaluated a second time.</para>
  295. <para>Example: If the <variable>MYVAR</variable> contains
  296. <variable>OTHERVAR</variable>, then the result of ${EVAL(
  297. <variable>MYVAR</variable>)} in the dialplan will be the
  298. contents of <variable>OTHERVAR</variable>. Normally just
  299. putting <variable>MYVAR</variable> in the dialplan the result
  300. would be <variable>OTHERVAR</variable>.</para>
  301. </description>
  302. </function>
  303. <function name="TOUPPER" language="en_US">
  304. <synopsis>
  305. Convert string to all uppercase letters.
  306. </synopsis>
  307. <syntax>
  308. <parameter name="string" required="true" />
  309. </syntax>
  310. <description>
  311. <para>Example: ${TOUPPER(Example)} returns "EXAMPLE"</para>
  312. </description>
  313. </function>
  314. <function name="TOLOWER" language="en_US">
  315. <synopsis>
  316. Convert string to all lowercase letters.
  317. </synopsis>
  318. <syntax>
  319. <parameter name="string" required="true" />
  320. </syntax>
  321. <description>
  322. <para>Example: ${TOLOWER(Example)} returns "example"</para>
  323. </description>
  324. </function>
  325. <function name="LEN" language="en_US">
  326. <synopsis>
  327. Return the length of the string given.
  328. </synopsis>
  329. <syntax>
  330. <parameter name="string" required="true" />
  331. </syntax>
  332. <description>
  333. <para>Example: ${LEN(example)} returns 7</para>
  334. </description>
  335. </function>
  336. <function name="QUOTE" language="en_US">
  337. <synopsis>
  338. Quotes a given string, escaping embedded quotes as necessary
  339. </synopsis>
  340. <syntax>
  341. <parameter name="string" required="true" />
  342. </syntax>
  343. <description>
  344. <para>Example: ${QUOTE(ab"c"de)} will return "abcde"</para>
  345. </description>
  346. </function>
  347. <function name="CSV_QUOTE" language="en_US">
  348. <synopsis>
  349. Quotes a given string for use in a CSV file, escaping embedded quotes as necessary
  350. </synopsis>
  351. <syntax>
  352. <parameter name="string" required="true" />
  353. </syntax>
  354. <description>
  355. <para>Example: ${CSV_QUOTE("a,b" 123)} will return """a,b"" 123"</para>
  356. </description>
  357. </function>
  358. <function name="SHIFT" language="en_US">
  359. <synopsis>
  360. Removes and returns the first item off of a variable containing delimited text
  361. </synopsis>
  362. <syntax>
  363. <parameter name="varname" required="true" />
  364. <parameter name="delimiter" required="false" default="," />
  365. </syntax>
  366. <description>
  367. <para>Example:</para>
  368. <para>exten => s,1,Set(array=one,two,three)</para>
  369. <para>exten => s,n,While($["${SET(var=${SHIFT(array)})}" != ""])</para>
  370. <para>exten => s,n,NoOp(var is ${var})</para>
  371. <para>exten => s,n,EndWhile</para>
  372. <para>This would iterate over each value in array, left to right, and
  373. would result in NoOp(var is one), NoOp(var is two), and
  374. NoOp(var is three) being executed.
  375. </para>
  376. </description>
  377. </function>
  378. <function name="POP" language="en_US">
  379. <synopsis>
  380. Removes and returns the last item off of a variable containing delimited text
  381. </synopsis>
  382. <syntax>
  383. <parameter name="varname" required="true" />
  384. <parameter name="delimiter" required="false" default="," />
  385. </syntax>
  386. <description>
  387. <para>Example:</para>
  388. <para>exten => s,1,Set(array=one,two,three)</para>
  389. <para>exten => s,n,While($["${SET(var=${POP(array)})}" != ""])</para>
  390. <para>exten => s,n,NoOp(var is ${var})</para>
  391. <para>exten => s,n,EndWhile</para>
  392. <para>This would iterate over each value in array, right to left, and
  393. would result in NoOp(var is three), NoOp(var is two), and
  394. NoOp(var is one) being executed.
  395. </para>
  396. </description>
  397. </function>
  398. <function name="PUSH" language="en_US">
  399. <synopsis>
  400. Appends one or more values to the end of a variable containing delimited text
  401. </synopsis>
  402. <syntax>
  403. <parameter name="varname" required="true" />
  404. <parameter name="delimiter" required="false" default="," />
  405. </syntax>
  406. <description>
  407. <para>Example: Set(PUSH(array)=one,two,three) would append one,
  408. two, and three to the end of the values stored in the variable
  409. "array".
  410. </para>
  411. </description>
  412. </function>
  413. <function name="UNSHIFT" language="en_US">
  414. <synopsis>
  415. Inserts one or more values to the beginning of a variable containing delimited text
  416. </synopsis>
  417. <syntax>
  418. <parameter name="varname" required="true" />
  419. <parameter name="delimiter" required="false" default="," />
  420. </syntax>
  421. <description>
  422. <para>Example: Set(UNSHIFT(array)=one,two,three) would insert one,
  423. two, and three before the values stored in the variable
  424. "array".
  425. </para>
  426. </description>
  427. </function>
  428. ***/
  429. static int function_fieldqty_helper(struct ast_channel *chan, const char *cmd,
  430. char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
  431. {
  432. char *varsubst;
  433. struct ast_str *str = ast_str_thread_get(&result_buf, 16);
  434. int fieldcount = 0;
  435. AST_DECLARE_APP_ARGS(args,
  436. AST_APP_ARG(varname);
  437. AST_APP_ARG(delim);
  438. );
  439. char delim[2] = "";
  440. size_t delim_used;
  441. if (!str) {
  442. return -1;
  443. }
  444. AST_STANDARD_APP_ARGS(args, parse);
  445. if (args.delim) {
  446. ast_get_encoded_char(args.delim, delim, &delim_used);
  447. varsubst = ast_alloca(strlen(args.varname) + 4);
  448. sprintf(varsubst, "${%s}", args.varname);
  449. ast_str_substitute_variables(&str, 0, chan, varsubst);
  450. if (ast_str_strlen(str) == 0) {
  451. fieldcount = 0;
  452. } else {
  453. char *varval = ast_str_buffer(str);
  454. while (strsep(&varval, delim)) {
  455. fieldcount++;
  456. }
  457. }
  458. } else {
  459. fieldcount = 1;
  460. }
  461. if (sbuf) {
  462. ast_str_set(sbuf, len, "%d", fieldcount);
  463. } else {
  464. snprintf(buf, len, "%d", fieldcount);
  465. }
  466. return 0;
  467. }
  468. static int function_fieldqty(struct ast_channel *chan, const char *cmd,
  469. char *parse, char *buf, size_t len)
  470. {
  471. return function_fieldqty_helper(chan, cmd, parse, buf, NULL, len);
  472. }
  473. static int function_fieldqty_str(struct ast_channel *chan, const char *cmd,
  474. char *parse, struct ast_str **buf, ssize_t len)
  475. {
  476. return function_fieldqty_helper(chan, cmd, parse, NULL, buf, len);
  477. }
  478. static struct ast_custom_function fieldqty_function = {
  479. .name = "FIELDQTY",
  480. .read = function_fieldqty,
  481. .read2 = function_fieldqty_str,
  482. };
  483. static int function_fieldnum_helper(struct ast_channel *chan, const char *cmd,
  484. char *parse, char *buf, struct ast_str **sbuf, ssize_t len)
  485. {
  486. char *varsubst, *field;
  487. struct ast_str *str = ast_str_thread_get(&result_buf, 16);
  488. int fieldindex = 0, res = 0;
  489. AST_DECLARE_APP_ARGS(args,
  490. AST_APP_ARG(varname);
  491. AST_APP_ARG(delim);
  492. AST_APP_ARG(field);
  493. );
  494. char delim[2] = "";
  495. size_t delim_used;
  496. if (!str) {
  497. return -1;
  498. }
  499. AST_STANDARD_APP_ARGS(args, parse);
  500. if (args.argc < 3) {
  501. ast_log(LOG_ERROR, "Usage: FIELDNUM(<listname>,<delimiter>,<fieldvalue>)\n");
  502. res = -1;
  503. } else {
  504. varsubst = ast_alloca(strlen(args.varname) + 4);
  505. sprintf(varsubst, "${%s}", args.varname);
  506. ast_str_substitute_variables(&str, 0, chan, varsubst);
  507. if (ast_str_strlen(str) == 0 || ast_strlen_zero(args.delim)) {
  508. fieldindex = 0;
  509. } else if (ast_get_encoded_char(args.delim, delim, &delim_used) == -1) {
  510. res = -1;
  511. } else {
  512. char *varval = ast_str_buffer(str);
  513. while ((field = strsep(&varval, delim)) != NULL) {
  514. fieldindex++;
  515. if (!strcasecmp(field, args.field)) {
  516. break;
  517. }
  518. }
  519. if (!field) {
  520. fieldindex = 0;
  521. }
  522. res = 0;
  523. }
  524. }
  525. if (sbuf) {
  526. ast_str_set(sbuf, len, "%d", fieldindex);
  527. } else {
  528. snprintf(buf, len, "%d", fieldindex);
  529. }
  530. return res;
  531. }
  532. static int function_fieldnum(struct ast_channel *chan, const char *cmd,
  533. char *parse, char *buf, size_t len)
  534. {
  535. return function_fieldnum_helper(chan, cmd, parse, buf, NULL, len);
  536. }
  537. static int function_fieldnum_str(struct ast_channel *chan, const char *cmd,
  538. char *parse, struct ast_str **buf, ssize_t len)
  539. {
  540. return function_fieldnum_helper(chan, cmd, parse, NULL, buf, len);
  541. }
  542. static struct ast_custom_function fieldnum_function = {
  543. .name = "FIELDNUM",
  544. .read = function_fieldnum,
  545. .read2 = function_fieldnum_str,
  546. };
  547. static int listfilter(struct ast_channel *chan, const char *cmd, char *parse, char *buf, struct ast_str **bufstr, ssize_t len)
  548. {
  549. AST_DECLARE_APP_ARGS(args,
  550. AST_APP_ARG(listname);
  551. AST_APP_ARG(delimiter);
  552. AST_APP_ARG(fieldvalue);
  553. );
  554. struct ast_str *orig_list = ast_str_thread_get(&tmp_buf, 16);
  555. const char *begin, *cur, *next;
  556. int dlen, flen, first = 1;
  557. struct ast_str *result, **result_ptr = &result;
  558. char *delim, *varsubst;
  559. AST_STANDARD_APP_ARGS(args, parse);
  560. if (buf) {
  561. if (!(result = ast_str_thread_get(&result_buf, 16))) {
  562. return -1;
  563. }
  564. } else {
  565. /* Place the result directly into the output buffer */
  566. result_ptr = bufstr;
  567. }
  568. if (args.argc < 3) {
  569. ast_log(LOG_ERROR, "Usage: LISTFILTER(<listname>,<delimiter>,<fieldvalue>)\n");
  570. return -1;
  571. }
  572. varsubst = ast_alloca(strlen(args.listname) + 4);
  573. sprintf(varsubst, "${%s}", args.listname);
  574. /* If we don't lock the channel, the variable could disappear out from underneath us. */
  575. if (chan) {
  576. ast_channel_lock(chan);
  577. }
  578. ast_str_substitute_variables(&orig_list, 0, chan, varsubst);
  579. if (!ast_str_strlen(orig_list)) {
  580. ast_log(LOG_ERROR, "List variable '%s' not found\n", args.listname);
  581. if (chan) {
  582. ast_channel_unlock(chan);
  583. }
  584. return -1;
  585. }
  586. /* If the string isn't there, just copy out the string and be done with it. */
  587. if (!strstr(ast_str_buffer(orig_list), args.fieldvalue)) {
  588. if (buf) {
  589. ast_copy_string(buf, ast_str_buffer(orig_list), len);
  590. } else {
  591. ast_str_set(result_ptr, len, "%s", ast_str_buffer(orig_list));
  592. }
  593. if (chan) {
  594. ast_channel_unlock(chan);
  595. }
  596. return 0;
  597. }
  598. dlen = strlen(args.delimiter);
  599. delim = ast_alloca(dlen + 1);
  600. ast_get_encoded_str(args.delimiter, delim, dlen + 1);
  601. if ((dlen = strlen(delim)) == 0) {
  602. delim = ",";
  603. dlen = 1;
  604. }
  605. flen = strlen(args.fieldvalue);
  606. ast_str_reset(*result_ptr);
  607. /* Enough space for any result */
  608. if (len > -1) {
  609. ast_str_make_space(result_ptr, len ? len : ast_str_strlen(orig_list) + 1);
  610. }
  611. begin = ast_str_buffer(orig_list);
  612. next = strstr(begin, delim);
  613. do {
  614. /* Find next boundary */
  615. if (next) {
  616. cur = next;
  617. next = strstr(cur + dlen, delim);
  618. } else {
  619. cur = strchr(begin + dlen, '\0');
  620. }
  621. if (flen == cur - begin && !strncmp(begin, args.fieldvalue, flen)) {
  622. /* Skip field */
  623. begin += flen + dlen;
  624. } else {
  625. /* Copy field to output */
  626. if (!first) {
  627. ast_str_append(result_ptr, len, "%s", delim);
  628. }
  629. ast_str_append_substr(result_ptr, len, begin, cur - begin);
  630. first = 0;
  631. begin = cur + dlen;
  632. }
  633. } while (*cur != '\0');
  634. if (chan) {
  635. ast_channel_unlock(chan);
  636. }
  637. if (buf) {
  638. ast_copy_string(buf, ast_str_buffer(result), len);
  639. }
  640. return 0;
  641. }
  642. static int listfilter_read(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
  643. {
  644. return listfilter(chan, cmd, parse, buf, NULL, len);
  645. }
  646. static int listfilter_read2(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **buf, ssize_t len)
  647. {
  648. return listfilter(chan, cmd, parse, NULL, buf, len);
  649. }
  650. static struct ast_custom_function listfilter_function = {
  651. .name = "LISTFILTER",
  652. .read = listfilter_read,
  653. .read2 = listfilter_read2,
  654. };
  655. static int filter(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
  656. size_t len)
  657. {
  658. AST_DECLARE_APP_ARGS(args,
  659. AST_APP_ARG(allowed);
  660. AST_APP_ARG(string);
  661. );
  662. char *outbuf = buf;
  663. unsigned char ac;
  664. char allowed[256] = "";
  665. size_t allowedlen = 0;
  666. int32_t bitfield[8] = { 0, }; /* 256 bits */
  667. AST_STANDARD_RAW_ARGS(args, parse);
  668. if (!args.string) {
  669. ast_log(LOG_ERROR, "Usage: FILTER(<allowed-chars>,<string>)\n");
  670. return -1;
  671. }
  672. if (args.allowed[0] == '"' && !ast_opt_dont_warn) {
  673. ast_log(LOG_WARNING, "FILTER allowed characters includes the quote (\") character. This may not be what you want.\n");
  674. }
  675. /* Expand ranges */
  676. for (; *(args.allowed);) {
  677. char c1 = 0, c2 = 0;
  678. size_t consumed = 0;
  679. if (ast_get_encoded_char(args.allowed, &c1, &consumed))
  680. return -1;
  681. args.allowed += consumed;
  682. if (*(args.allowed) == '-') {
  683. if (ast_get_encoded_char(args.allowed + 1, &c2, &consumed))
  684. c2 = c1;
  685. args.allowed += consumed + 1;
  686. if ((unsigned char) c2 < (unsigned char) c1 && !ast_opt_dont_warn) {
  687. ast_log(LOG_WARNING, "Range wrapping in FILTER(%s,%s). This may not be what you want.\n", parse, args.string);
  688. }
  689. /*!\note
  690. * Looks a little strange, until you realize that we can overflow
  691. * the size of a char.
  692. */
  693. for (ac = (unsigned char) c1; ac != (unsigned char) c2; ac++) {
  694. bitfield[ac / 32] |= 1 << (ac % 32);
  695. }
  696. bitfield[ac / 32] |= 1 << (ac % 32);
  697. ast_debug(4, "c1=%d, c2=%d\n", c1, c2);
  698. } else {
  699. ac = (unsigned char) c1;
  700. ast_debug(4, "c1=%d, consumed=%d, args.allowed=%s\n", c1, (int) consumed, args.allowed - consumed);
  701. bitfield[ac / 32] |= 1 << (ac % 32);
  702. }
  703. }
  704. for (ac = 1; ac != 0; ac++) {
  705. if (bitfield[ac / 32] & (1 << (ac % 32))) {
  706. allowed[allowedlen++] = ac;
  707. }
  708. }
  709. ast_debug(1, "Allowed: %s\n", allowed);
  710. for (; *(args.string) && (buf + len - 1 > outbuf); (args.string)++) {
  711. if (strchr(allowed, *(args.string)))
  712. *outbuf++ = *(args.string);
  713. }
  714. *outbuf = '\0';
  715. return 0;
  716. }
  717. static struct ast_custom_function filter_function = {
  718. .name = "FILTER",
  719. .read = filter,
  720. };
  721. static int replace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  722. {
  723. AST_DECLARE_APP_ARGS(args,
  724. AST_APP_ARG(varname);
  725. AST_APP_ARG(find);
  726. AST_APP_ARG(replace);
  727. );
  728. char *strptr, *varsubst;
  729. struct ast_str *str = ast_str_thread_get(&result_buf, 16);
  730. char find[256]; /* Only 256 characters possible */
  731. char replace[2] = "";
  732. size_t unused;
  733. AST_STANDARD_APP_ARGS(args, data);
  734. if (!str) {
  735. return -1;
  736. }
  737. if (args.argc < 2) {
  738. ast_log(LOG_ERROR, "Usage: %s(<varname>,<search-chars>[,<replace-char>])\n", cmd);
  739. return -1;
  740. }
  741. /* Decode escapes */
  742. ast_get_encoded_str(args.find, find, sizeof(find));
  743. ast_get_encoded_char(args.replace, replace, &unused);
  744. if (ast_strlen_zero(find) || ast_strlen_zero(args.varname)) {
  745. ast_log(LOG_ERROR, "The characters to search for and the variable name must not be empty.\n");
  746. return -1;
  747. }
  748. varsubst = ast_alloca(strlen(args.varname) + 4);
  749. sprintf(varsubst, "${%s}", args.varname);
  750. ast_str_substitute_variables(&str, 0, chan, varsubst);
  751. if (!ast_str_strlen(str)) {
  752. /* Blank, nothing to replace */
  753. return -1;
  754. }
  755. ast_debug(3, "String to search: (%s)\n", ast_str_buffer(str));
  756. ast_debug(3, "Characters to find: (%s)\n", find);
  757. ast_debug(3, "Character to replace with: (%s)\n", replace);
  758. for (strptr = ast_str_buffer(str); *strptr; strptr++) {
  759. /* buf is already a mutable buffer, so we construct the result
  760. * directly there */
  761. if (strchr(find, *strptr)) {
  762. if (ast_strlen_zero(replace)) {
  763. /* Remove character */
  764. strcpy(strptr, strptr + 1); /* SAFE */
  765. strptr--;
  766. } else {
  767. /* Replace character */
  768. *strptr = *replace;
  769. }
  770. }
  771. }
  772. ast_str_set(buf, len, "%s", ast_str_buffer(str));
  773. return 0;
  774. }
  775. static struct ast_custom_function replace_function = {
  776. .name = "REPLACE",
  777. .read2 = replace,
  778. };
  779. static int strreplace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  780. {
  781. char *varsubstr; /* substring for input var */
  782. char *start; /* Starting pos of substring search. */
  783. char *end; /* Ending pos of substring search. */
  784. int find_size; /* length of given find-string */
  785. unsigned max_matches; /* number of matches we find before terminating search */
  786. unsigned count; /* loop counter */
  787. struct ast_str *str = ast_str_thread_get(&result_buf, 16); /* Holds the data obtained from varname */
  788. AST_DECLARE_APP_ARGS(args,
  789. AST_APP_ARG(varname);
  790. AST_APP_ARG(find_string);
  791. AST_APP_ARG(replace_string);
  792. AST_APP_ARG(max_replacements);
  793. AST_APP_ARG(other); /* Any remining unused arguments */
  794. );
  795. /* Guarantee output string is empty to start with. */
  796. ast_str_reset(*buf);
  797. if (!str) {
  798. /* We failed to allocate str, forget it. We failed. */
  799. return -1;
  800. }
  801. /* Parse the arguments. */
  802. AST_STANDARD_APP_ARGS(args, data);
  803. if (args.argc < 2) {
  804. /* Didn't receive enough arguments to do anything */
  805. ast_log(LOG_ERROR,
  806. "Usage: %s(<varname>,<find-string>[,<replace-string>,[<max-replacements>]])\n",
  807. cmd);
  808. return -1;
  809. }
  810. /* No var name specified. Return failure, string is already empty. */
  811. if (ast_strlen_zero(args.varname)) {
  812. return -1;
  813. }
  814. /* Zero length find strings are a no-no. Kill the function if we run into one. */
  815. if (ast_strlen_zero(args.find_string)) {
  816. ast_log(LOG_ERROR, "No <find-string> specified\n");
  817. return -1;
  818. }
  819. find_size = strlen(args.find_string);
  820. /* set varsubstr to the matching variable */
  821. varsubstr = ast_alloca(strlen(args.varname) + 4);
  822. sprintf(varsubstr, "${%s}", args.varname);
  823. ast_str_substitute_variables(&str, 0, chan, varsubstr);
  824. /* Determine how many replacements are allowed. */
  825. if (!args.max_replacements
  826. || (max_matches = atoi(args.max_replacements)) <= 0) {
  827. /* Unlimited replacements are allowed. */
  828. max_matches = -1;
  829. }
  830. /* Generate the search and replaced string. */
  831. start = ast_str_buffer(str);
  832. for (count = 0; count < max_matches; ++count) {
  833. end = strstr(start, args.find_string);
  834. if (!end) {
  835. /* Did not find a matching substring in the remainder. */
  836. break;
  837. }
  838. /* Replace the found substring. */
  839. *end = '\0';
  840. ast_str_append(buf, len, "%s", start);
  841. if (args.replace_string) {
  842. /* Append the replacement string */
  843. ast_str_append(buf, len, "%s", args.replace_string);
  844. }
  845. start = end + find_size;
  846. }
  847. ast_str_append(buf, len, "%s", start);
  848. return 0;
  849. }
  850. static struct ast_custom_function strreplace_function = {
  851. .name = "STRREPLACE",
  852. .read2 = strreplace,
  853. };
  854. static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf,
  855. size_t len)
  856. {
  857. AST_DECLARE_APP_ARGS(args,
  858. AST_APP_ARG(null);
  859. AST_APP_ARG(reg);
  860. AST_APP_ARG(str);
  861. );
  862. int errcode;
  863. regex_t regexbuf;
  864. buf[0] = '\0';
  865. AST_NONSTANDARD_APP_ARGS(args, parse, '"');
  866. if (args.argc != 3) {
  867. ast_log(LOG_ERROR, "Unexpected arguments: should have been in the form '\"<regex>\" <string>'\n");
  868. return -1;
  869. }
  870. if ((*args.str == ' ') || (*args.str == '\t'))
  871. args.str++;
  872. ast_debug(1, "FUNCTION REGEX (%s)(%s)\n", args.reg, args.str);
  873. if ((errcode = regcomp(&regexbuf, args.reg, REG_EXTENDED | REG_NOSUB))) {
  874. regerror(errcode, &regexbuf, buf, len);
  875. ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, parse, buf);
  876. return -1;
  877. }
  878. strcpy(buf, regexec(&regexbuf, args.str, 0, NULL, 0) ? "0" : "1");
  879. regfree(&regexbuf);
  880. return 0;
  881. }
  882. static struct ast_custom_function regex_function = {
  883. .name = "REGEX",
  884. .read = regex,
  885. };
  886. #define HASH_PREFIX "~HASH~%s~"
  887. #define HASH_FORMAT HASH_PREFIX "%s~"
  888. static char *app_clearhash = "ClearHash";
  889. /* This function probably should migrate to main/pbx.c, as pbx_builtin_clearvar_prefix() */
  890. static void clearvar_prefix(struct ast_channel *chan, const char *prefix)
  891. {
  892. struct ast_var_t *var;
  893. int len = strlen(prefix);
  894. AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->varshead, var, entries) {
  895. if (strncasecmp(prefix, ast_var_name(var), len) == 0) {
  896. AST_LIST_REMOVE_CURRENT(entries);
  897. ast_free(var);
  898. }
  899. }
  900. AST_LIST_TRAVERSE_SAFE_END
  901. }
  902. static int exec_clearhash(struct ast_channel *chan, const char *data)
  903. {
  904. char prefix[80];
  905. snprintf(prefix, sizeof(prefix), HASH_PREFIX, data ? (char *)data : "null");
  906. clearvar_prefix(chan, prefix);
  907. return 0;
  908. }
  909. static int array(struct ast_channel *chan, const char *cmd, char *var,
  910. const char *value)
  911. {
  912. AST_DECLARE_APP_ARGS(arg1,
  913. AST_APP_ARG(var)[100];
  914. );
  915. AST_DECLARE_APP_ARGS(arg2,
  916. AST_APP_ARG(val)[100];
  917. );
  918. char *origvar = "", *value2, varname[256];
  919. int i, ishash = 0;
  920. if (!var) {
  921. return -1;
  922. }
  923. value2 = ast_strdupa(value);
  924. if (!strcmp(cmd, "HASH")) {
  925. const char *var2 = pbx_builtin_getvar_helper(chan, "~ODBCFIELDS~");
  926. origvar = var;
  927. if (var2)
  928. var = ast_strdupa(var2);
  929. else {
  930. if (chan)
  931. ast_autoservice_stop(chan);
  932. return -1;
  933. }
  934. ishash = 1;
  935. }
  936. /* The functions this will generally be used with are SORT and ODBC_*, which
  937. * both return comma-delimited lists. However, if somebody uses literal lists,
  938. * their commas will be translated to vertical bars by the load, and I don't
  939. * want them to be surprised by the result. Hence, we prefer commas as the
  940. * delimiter, but we'll fall back to vertical bars if commas aren't found.
  941. */
  942. ast_debug(1, "array (%s=%s)\n", var, S_OR(value2, ""));
  943. AST_STANDARD_APP_ARGS(arg1, var);
  944. AST_STANDARD_APP_ARGS(arg2, value2);
  945. for (i = 0; i < arg1.argc; i++) {
  946. ast_debug(1, "array set value (%s=%s)\n", arg1.var[i],
  947. S_OR(arg2.val[i], ""));
  948. if (i < arg2.argc) {
  949. if (ishash) {
  950. if (origvar[0] == '_') {
  951. if (origvar[1] == '_') {
  952. snprintf(varname, sizeof(varname), "__" HASH_FORMAT, origvar + 2, arg1.var[i]);
  953. } else {
  954. snprintf(varname, sizeof(varname), "_" HASH_FORMAT, origvar + 1, arg1.var[i]);
  955. }
  956. } else {
  957. snprintf(varname, sizeof(varname), HASH_FORMAT, origvar, arg1.var[i]);
  958. }
  959. pbx_builtin_setvar_helper(chan, varname, arg2.val[i]);
  960. } else {
  961. pbx_builtin_setvar_helper(chan, arg1.var[i], arg2.val[i]);
  962. }
  963. } else {
  964. /* We could unset the variable, by passing a NULL, but due to
  965. * pushvar semantics, that could create some undesired behavior. */
  966. if (ishash) {
  967. snprintf(varname, sizeof(varname), HASH_FORMAT, origvar, arg1.var[i]);
  968. pbx_builtin_setvar_helper(chan, varname, "");
  969. } else {
  970. pbx_builtin_setvar_helper(chan, arg1.var[i], "");
  971. }
  972. }
  973. }
  974. return 0;
  975. }
  976. static int hashkeys_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  977. {
  978. struct ast_var_t *newvar;
  979. struct ast_str *prefix = ast_str_alloca(80);
  980. ast_str_set(&prefix, -1, HASH_PREFIX, data);
  981. memset(buf, 0, len);
  982. AST_LIST_TRAVERSE(&chan->varshead, newvar, entries) {
  983. if (strncasecmp(ast_str_buffer(prefix), ast_var_name(newvar), ast_str_strlen(prefix)) == 0) {
  984. /* Copy everything after the prefix */
  985. strncat(buf, ast_var_name(newvar) + ast_str_strlen(prefix), len - strlen(buf) - 1);
  986. /* Trim the trailing ~ */
  987. buf[strlen(buf) - 1] = ',';
  988. }
  989. }
  990. /* Trim the trailing comma */
  991. buf[strlen(buf) - 1] = '\0';
  992. return 0;
  993. }
  994. static int hashkeys_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  995. {
  996. struct ast_var_t *newvar;
  997. struct ast_str *prefix = ast_str_alloca(80);
  998. char *tmp;
  999. ast_str_set(&prefix, -1, HASH_PREFIX, data);
  1000. AST_LIST_TRAVERSE(&chan->varshead, newvar, entries) {
  1001. if (strncasecmp(ast_str_buffer(prefix), ast_var_name(newvar), ast_str_strlen(prefix)) == 0) {
  1002. /* Copy everything after the prefix */
  1003. ast_str_append(buf, len, "%s", ast_var_name(newvar) + ast_str_strlen(prefix));
  1004. /* Trim the trailing ~ */
  1005. tmp = ast_str_buffer(*buf);
  1006. tmp[ast_str_strlen(*buf) - 1] = ',';
  1007. }
  1008. }
  1009. /* Trim the trailing comma */
  1010. tmp = ast_str_buffer(*buf);
  1011. tmp[ast_str_strlen(*buf) - 1] = '\0';
  1012. return 0;
  1013. }
  1014. static int hash_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
  1015. {
  1016. char varname[256];
  1017. AST_DECLARE_APP_ARGS(arg,
  1018. AST_APP_ARG(hashname);
  1019. AST_APP_ARG(hashkey);
  1020. );
  1021. if (!strchr(var, ',')) {
  1022. /* Single argument version */
  1023. return array(chan, "HASH", var, value);
  1024. }
  1025. AST_STANDARD_APP_ARGS(arg, var);
  1026. if (arg.hashname[0] == '_') {
  1027. if (arg.hashname[1] == '_') {
  1028. snprintf(varname, sizeof(varname), "__" HASH_FORMAT, arg.hashname + 2, arg.hashkey);
  1029. } else {
  1030. snprintf(varname, sizeof(varname), "_" HASH_FORMAT, arg.hashname + 1, arg.hashkey);
  1031. }
  1032. } else {
  1033. snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg.hashkey);
  1034. }
  1035. pbx_builtin_setvar_helper(chan, varname, value);
  1036. return 0;
  1037. }
  1038. static int hash_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  1039. {
  1040. char varname[256];
  1041. const char *varvalue;
  1042. AST_DECLARE_APP_ARGS(arg,
  1043. AST_APP_ARG(hashname);
  1044. AST_APP_ARG(hashkey);
  1045. );
  1046. AST_STANDARD_APP_ARGS(arg, data);
  1047. if (arg.argc == 2) {
  1048. snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg.hashkey);
  1049. varvalue = pbx_builtin_getvar_helper(chan, varname);
  1050. if (varvalue)
  1051. ast_copy_string(buf, varvalue, len);
  1052. else
  1053. *buf = '\0';
  1054. } else if (arg.argc == 1) {
  1055. char colnames[4096];
  1056. int i;
  1057. AST_DECLARE_APP_ARGS(arg2,
  1058. AST_APP_ARG(col)[100];
  1059. );
  1060. /* Get column names, in no particular order */
  1061. hashkeys_read(chan, "HASHKEYS", arg.hashname, colnames, sizeof(colnames));
  1062. pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames);
  1063. AST_STANDARD_APP_ARGS(arg2, colnames);
  1064. *buf = '\0';
  1065. /* Now get the corresponding column values, in exactly the same order */
  1066. for (i = 0; i < arg2.argc; i++) {
  1067. snprintf(varname, sizeof(varname), HASH_FORMAT, arg.hashname, arg2.col[i]);
  1068. varvalue = pbx_builtin_getvar_helper(chan, varname);
  1069. strncat(buf, varvalue, len - strlen(buf) - 1);
  1070. strncat(buf, ",", len - strlen(buf) - 1);
  1071. }
  1072. /* Strip trailing comma */
  1073. buf[strlen(buf) - 1] = '\0';
  1074. }
  1075. return 0;
  1076. }
  1077. static struct ast_custom_function hash_function = {
  1078. .name = "HASH",
  1079. .write = hash_write,
  1080. .read = hash_read,
  1081. };
  1082. static struct ast_custom_function hashkeys_function = {
  1083. .name = "HASHKEYS",
  1084. .read = hashkeys_read,
  1085. .read2 = hashkeys_read2,
  1086. };
  1087. static struct ast_custom_function array_function = {
  1088. .name = "ARRAY",
  1089. .write = array,
  1090. };
  1091. static int quote(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  1092. {
  1093. char *bufptr = buf, *dataptr = data;
  1094. if (len < 3){ /* at least two for quotes and one for binary zero */
  1095. ast_log(LOG_ERROR, "Not enough buffer\n");
  1096. return -1;
  1097. }
  1098. if (ast_strlen_zero(data)) {
  1099. ast_log(LOG_WARNING, "No argument specified!\n");
  1100. ast_copy_string(buf, "\"\"", len);
  1101. return 0;
  1102. }
  1103. *bufptr++ = '"';
  1104. for (; bufptr < buf + len - 3; dataptr++) {
  1105. if (*dataptr == '\\') {
  1106. *bufptr++ = '\\';
  1107. *bufptr++ = '\\';
  1108. } else if (*dataptr == '"') {
  1109. *bufptr++ = '\\';
  1110. *bufptr++ = '"';
  1111. } else if (*dataptr == '\0') {
  1112. break;
  1113. } else {
  1114. *bufptr++ = *dataptr;
  1115. }
  1116. }
  1117. *bufptr++ = '"';
  1118. *bufptr = '\0';
  1119. return 0;
  1120. }
  1121. static struct ast_custom_function quote_function = {
  1122. .name = "QUOTE",
  1123. .read = quote,
  1124. };
  1125. static int csv_quote(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  1126. {
  1127. char *bufptr = buf, *dataptr = data;
  1128. if (len < 3) { /* at least two for quotes and one for binary zero */
  1129. ast_log(LOG_ERROR, "Not enough buffer\n");
  1130. return -1;
  1131. }
  1132. if (ast_strlen_zero(data)) {
  1133. ast_copy_string(buf, "\"\"", len);
  1134. return 0;
  1135. }
  1136. *bufptr++ = '"';
  1137. for (; bufptr < buf + len - 3; dataptr++){
  1138. if (*dataptr == '"') {
  1139. *bufptr++ = '"';
  1140. *bufptr++ = '"';
  1141. } else if (*dataptr == '\0') {
  1142. break;
  1143. } else {
  1144. *bufptr++ = *dataptr;
  1145. }
  1146. }
  1147. *bufptr++ = '"';
  1148. *bufptr='\0';
  1149. return 0;
  1150. }
  1151. static struct ast_custom_function csv_quote_function = {
  1152. .name = "CSV_QUOTE",
  1153. .read = csv_quote,
  1154. };
  1155. static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
  1156. {
  1157. int length = 0;
  1158. if (data)
  1159. length = strlen(data);
  1160. snprintf(buf, buflen, "%d", length);
  1161. return 0;
  1162. }
  1163. static struct ast_custom_function len_function = {
  1164. .name = "LEN",
  1165. .read = len,
  1166. .read_max = 12,
  1167. };
  1168. static int acf_strftime(struct ast_channel *chan, const char *cmd, char *parse,
  1169. char *buf, size_t buflen)
  1170. {
  1171. AST_DECLARE_APP_ARGS(args,
  1172. AST_APP_ARG(epoch);
  1173. AST_APP_ARG(timezone);
  1174. AST_APP_ARG(format);
  1175. );
  1176. struct timeval when;
  1177. struct ast_tm tm;
  1178. buf[0] = '\0';
  1179. AST_STANDARD_APP_ARGS(args, parse);
  1180. ast_get_timeval(args.epoch, &when, ast_tvnow(), NULL);
  1181. ast_localtime(&when, &tm, args.timezone);
  1182. if (!args.format)
  1183. args.format = "%c";
  1184. if (ast_strftime(buf, buflen, args.format, &tm) <= 0)
  1185. ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
  1186. buf[buflen - 1] = '\0';
  1187. return 0;
  1188. }
  1189. static struct ast_custom_function strftime_function = {
  1190. .name = "STRFTIME",
  1191. .read = acf_strftime,
  1192. };
  1193. static int acf_strptime(struct ast_channel *chan, const char *cmd, char *data,
  1194. char *buf, size_t buflen)
  1195. {
  1196. AST_DECLARE_APP_ARGS(args,
  1197. AST_APP_ARG(timestring);
  1198. AST_APP_ARG(timezone);
  1199. AST_APP_ARG(format);
  1200. );
  1201. struct ast_tm tm;
  1202. buf[0] = '\0';
  1203. if (!data) {
  1204. ast_log(LOG_ERROR,
  1205. "Asterisk function STRPTIME() requires an argument.\n");
  1206. return -1;
  1207. }
  1208. AST_STANDARD_APP_ARGS(args, data);
  1209. if (ast_strlen_zero(args.format)) {
  1210. ast_log(LOG_ERROR,
  1211. "No format supplied to STRPTIME(<timestring>,<timezone>,<format>)");
  1212. return -1;
  1213. }
  1214. if (!ast_strptime(args.timestring, args.format, &tm)) {
  1215. ast_log(LOG_WARNING, "STRPTIME() found no time specified within the string\n");
  1216. } else {
  1217. struct timeval when;
  1218. when = ast_mktime(&tm, args.timezone);
  1219. snprintf(buf, buflen, "%d", (int) when.tv_sec);
  1220. }
  1221. return 0;
  1222. }
  1223. static struct ast_custom_function strptime_function = {
  1224. .name = "STRPTIME",
  1225. .read = acf_strptime,
  1226. };
  1227. static int function_eval(struct ast_channel *chan, const char *cmd, char *data,
  1228. char *buf, size_t buflen)
  1229. {
  1230. if (ast_strlen_zero(data)) {
  1231. ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
  1232. return -1;
  1233. }
  1234. pbx_substitute_variables_helper(chan, data, buf, buflen - 1);
  1235. return 0;
  1236. }
  1237. static int function_eval2(struct ast_channel *chan, const char *cmd, char *data,
  1238. struct ast_str **buf, ssize_t buflen)
  1239. {
  1240. if (ast_strlen_zero(data)) {
  1241. ast_log(LOG_WARNING, "EVAL requires an argument: EVAL(<string>)\n");
  1242. return -1;
  1243. }
  1244. ast_str_substitute_variables(buf, buflen, chan, data);
  1245. return 0;
  1246. }
  1247. static struct ast_custom_function eval_function = {
  1248. .name = "EVAL",
  1249. .read = function_eval,
  1250. .read2 = function_eval2,
  1251. };
  1252. static int keypadhash(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
  1253. {
  1254. char *bufptr, *dataptr;
  1255. for (bufptr = buf, dataptr = data; bufptr < buf + buflen - 1; dataptr++) {
  1256. if (*dataptr == '\0') {
  1257. *bufptr++ = '\0';
  1258. break;
  1259. } else if (*dataptr == '1') {
  1260. *bufptr++ = '1';
  1261. } else if (strchr("AaBbCc2", *dataptr)) {
  1262. *bufptr++ = '2';
  1263. } else if (strchr("DdEeFf3", *dataptr)) {
  1264. *bufptr++ = '3';
  1265. } else if (strchr("GgHhIi4", *dataptr)) {
  1266. *bufptr++ = '4';
  1267. } else if (strchr("JjKkLl5", *dataptr)) {
  1268. *bufptr++ = '5';
  1269. } else if (strchr("MmNnOo6", *dataptr)) {
  1270. *bufptr++ = '6';
  1271. } else if (strchr("PpQqRrSs7", *dataptr)) {
  1272. *bufptr++ = '7';
  1273. } else if (strchr("TtUuVv8", *dataptr)) {
  1274. *bufptr++ = '8';
  1275. } else if (strchr("WwXxYyZz9", *dataptr)) {
  1276. *bufptr++ = '9';
  1277. } else if (*dataptr == '0') {
  1278. *bufptr++ = '0';
  1279. }
  1280. }
  1281. buf[buflen - 1] = '\0';
  1282. return 0;
  1283. }
  1284. static struct ast_custom_function keypadhash_function = {
  1285. .name = "KEYPADHASH",
  1286. .read = keypadhash,
  1287. };
  1288. static int string_toupper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
  1289. {
  1290. char *bufptr = buf, *dataptr = data;
  1291. while ((bufptr < buf + buflen - 1) && (*bufptr++ = toupper(*dataptr++)));
  1292. return 0;
  1293. }
  1294. static int string_toupper2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
  1295. {
  1296. char *bufptr, *dataptr = data;
  1297. if (buflen > -1) {
  1298. ast_str_make_space(buf, buflen > 0 ? buflen : strlen(data) + 1);
  1299. }
  1300. bufptr = ast_str_buffer(*buf);
  1301. while ((bufptr < ast_str_buffer(*buf) + ast_str_size(*buf) - 1) && (*bufptr++ = toupper(*dataptr++)));
  1302. ast_str_update(*buf);
  1303. return 0;
  1304. }
  1305. static struct ast_custom_function toupper_function = {
  1306. .name = "TOUPPER",
  1307. .read = string_toupper,
  1308. .read2 = string_toupper2,
  1309. };
  1310. static int string_tolower(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
  1311. {
  1312. char *bufptr = buf, *dataptr = data;
  1313. while ((bufptr < buf + buflen - 1) && (*bufptr++ = tolower(*dataptr++)));
  1314. return 0;
  1315. }
  1316. static int string_tolower2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t buflen)
  1317. {
  1318. char *bufptr, *dataptr = data;
  1319. if (buflen > -1) {
  1320. ast_str_make_space(buf, buflen > 0 ? buflen : strlen(data) + 1);
  1321. }
  1322. bufptr = ast_str_buffer(*buf);
  1323. while ((bufptr < ast_str_buffer(*buf) + ast_str_size(*buf) - 1) && (*bufptr++ = tolower(*dataptr++)));
  1324. ast_str_update(*buf);
  1325. return 0;
  1326. }
  1327. static struct ast_custom_function tolower_function = {
  1328. .name = "TOLOWER",
  1329. .read = string_tolower,
  1330. .read2 = string_tolower2,
  1331. };
  1332. static int shift_pop(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  1333. {
  1334. #define beginning (cmd[0] == 'S') /* SHIFT */
  1335. char *after, delimiter[2] = ",", *varsubst;
  1336. size_t unused;
  1337. struct ast_str *before = ast_str_thread_get(&result_buf, 16);
  1338. char *(*search_func)(const char *s, int c) = (beginning ? strchr : strrchr);
  1339. AST_DECLARE_APP_ARGS(args,
  1340. AST_APP_ARG(var);
  1341. AST_APP_ARG(delimiter);
  1342. );
  1343. if (!before) {
  1344. return -1;
  1345. }
  1346. AST_STANDARD_APP_ARGS(args, data);
  1347. if (ast_strlen_zero(args.var)) {
  1348. ast_log(LOG_WARNING, "%s requires a variable name\n", cmd);
  1349. return -1;
  1350. }
  1351. varsubst = ast_alloca(strlen(args.var) + 4);
  1352. sprintf(varsubst, "${%s}", args.var);
  1353. ast_str_substitute_variables(&before, 0, chan, varsubst);
  1354. if (args.argc > 1 && !ast_strlen_zero(args.delimiter)) {
  1355. ast_get_encoded_char(args.delimiter, delimiter, &unused);
  1356. }
  1357. if (!ast_str_strlen(before)) {
  1358. /* Nothing to pop */
  1359. return -1;
  1360. }
  1361. if (!(after = search_func(ast_str_buffer(before), delimiter[0]))) {
  1362. /* Only one entry in array */
  1363. ast_str_set(buf, len, "%s", ast_str_buffer(before));
  1364. pbx_builtin_setvar_helper(chan, args.var, "");
  1365. } else {
  1366. *after++ = '\0';
  1367. ast_str_set(buf, len, "%s", beginning ? ast_str_buffer(before) : after);
  1368. pbx_builtin_setvar_helper(chan, args.var, beginning ? after : ast_str_buffer(before));
  1369. }
  1370. return 0;
  1371. #undef beginning
  1372. }
  1373. static struct ast_custom_function shift_function = {
  1374. .name = "SHIFT",
  1375. .read2 = shift_pop,
  1376. };
  1377. static struct ast_custom_function pop_function = {
  1378. .name = "POP",
  1379. .read2 = shift_pop,
  1380. };
  1381. static int unshift_push(struct ast_channel *chan, const char *cmd, char *data, const char *new_value)
  1382. {
  1383. #define beginning (cmd[0] == 'U') /* UNSHIFT */
  1384. char delimiter[2] = ",", *varsubst;
  1385. size_t unused;
  1386. struct ast_str *buf, *previous_value;
  1387. AST_DECLARE_APP_ARGS(args,
  1388. AST_APP_ARG(var);
  1389. AST_APP_ARG(delimiter);
  1390. );
  1391. if (!(buf = ast_str_thread_get(&result_buf, 16)) ||
  1392. !(previous_value = ast_str_thread_get(&tmp_buf, 16))) {
  1393. return -1;
  1394. }
  1395. AST_STANDARD_APP_ARGS(args, data);
  1396. if (ast_strlen_zero(args.var)) {
  1397. ast_log(LOG_WARNING, "%s requires a variable name\n", cmd);
  1398. return -1;
  1399. }
  1400. if (args.argc > 1 && !ast_strlen_zero(args.delimiter)) {
  1401. ast_get_encoded_char(args.delimiter, delimiter, &unused);
  1402. }
  1403. varsubst = ast_alloca(strlen(args.var) + 4);
  1404. sprintf(varsubst, "${%s}", args.var);
  1405. ast_str_substitute_variables(&previous_value, 0, chan, varsubst);
  1406. if (!ast_str_strlen(previous_value)) {
  1407. ast_str_set(&buf, 0, "%s", new_value);
  1408. } else {
  1409. ast_str_set(&buf, 0, "%s%c%s",
  1410. beginning ? new_value : ast_str_buffer(previous_value),
  1411. delimiter[0],
  1412. beginning ? ast_str_buffer(previous_value) : new_value);
  1413. }
  1414. pbx_builtin_setvar_helper(chan, args.var, ast_str_buffer(buf));
  1415. return 0;
  1416. #undef beginning
  1417. }
  1418. static struct ast_custom_function push_function = {
  1419. .name = "PUSH",
  1420. .write = unshift_push,
  1421. };
  1422. static struct ast_custom_function unshift_function = {
  1423. .name = "UNSHIFT",
  1424. .write = unshift_push,
  1425. };
  1426. static int passthru(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  1427. {
  1428. ast_str_set(buf, len, "%s", data);
  1429. return 0;
  1430. }
  1431. static struct ast_custom_function passthru_function = {
  1432. .name = "PASSTHRU",
  1433. .read2 = passthru,
  1434. };
  1435. #ifdef TEST_FRAMEWORK
  1436. AST_TEST_DEFINE(test_FIELDNUM)
  1437. {
  1438. int i, res = AST_TEST_PASS;
  1439. struct ast_channel *chan;
  1440. struct ast_str *str;
  1441. char expression[256];
  1442. struct {
  1443. const char *fields;
  1444. const char *delim;
  1445. const char *field;
  1446. const char *expected;
  1447. } test_args[] = {
  1448. {"abc,def,ghi,jkl", "\\,", "ghi", "3"},
  1449. {"abc def ghi jkl", " ", "abc", "1"},
  1450. {"abc/def/ghi/jkl", "\\\\x2f", "def", "2"},
  1451. {"abc$def$ghi$jkl", "", "ghi", "0"},
  1452. {"abc,def,ghi,jkl", "-", "", "0"},
  1453. {"abc-def-ghi-jkl", "-", "mno", "0"}
  1454. };
  1455. switch (cmd) {
  1456. case TEST_INIT:
  1457. info->name = "func_FIELDNUM_test";
  1458. info->category = "/funcs/func_strings/";
  1459. info->summary = "Test FIELDNUM function";
  1460. info->description = "Verify FIELDNUM behavior";
  1461. return AST_TEST_NOT_RUN;
  1462. case TEST_EXECUTE:
  1463. break;
  1464. }
  1465. if (!(chan = ast_dummy_channel_alloc())) {
  1466. ast_test_status_update(test, "Unable to allocate dummy channel\n");
  1467. return AST_TEST_FAIL;
  1468. }
  1469. if (!(str = ast_str_create(16))) {
  1470. ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
  1471. ast_channel_release(chan);
  1472. return AST_TEST_FAIL;
  1473. }
  1474. for (i = 0; i < ARRAY_LEN(test_args); i++) {
  1475. struct ast_var_t *var = ast_var_assign("FIELDS", test_args[i].fields);
  1476. AST_LIST_INSERT_HEAD(&chan->varshead, var, entries);
  1477. snprintf(expression, sizeof(expression), "${FIELDNUM(%s,%s,%s)}", var->name, test_args[i].delim, test_args[i].field);
  1478. ast_str_substitute_variables(&str, 0, chan, expression);
  1479. AST_LIST_REMOVE(&chan->varshead, var, entries);
  1480. ast_var_delete(var);
  1481. if (strcasecmp(ast_str_buffer(str), test_args[i].expected)) {
  1482. ast_test_status_update(test, "Evaluation of '%s' returned '%s' instead of the expected value '%s'\n",
  1483. expression, ast_str_buffer(str), test_args[i].expected);
  1484. res = AST_TEST_FAIL;
  1485. break;
  1486. }
  1487. }
  1488. ast_free(str);
  1489. ast_channel_release(chan);
  1490. return res;
  1491. }
  1492. AST_TEST_DEFINE(test_FILTER)
  1493. {
  1494. int i, res = AST_TEST_PASS;
  1495. const char *test_strings[][2] = {
  1496. {"A-R", "DAHDI"},
  1497. {"A\\-R", "A"},
  1498. {"\\x41-R", "DAHDI"},
  1499. {"0-9A-Ca-c", "0042133333A12212"},
  1500. {"0-9a-cA-C_+\\-", "0042133333A12212"},
  1501. {NULL, NULL},
  1502. };
  1503. switch (cmd) {
  1504. case TEST_INIT:
  1505. info->name = "func_FILTER_test";
  1506. info->category = "/funcs/func_strings/";
  1507. info->summary = "Test FILTER function";
  1508. info->description = "Verify FILTER behavior";
  1509. return AST_TEST_NOT_RUN;
  1510. case TEST_EXECUTE:
  1511. break;
  1512. }
  1513. for (i = 0; test_strings[i][0]; i++) {
  1514. char tmp[256], tmp2[256] = "";
  1515. snprintf(tmp, sizeof(tmp), "${FILTER(%s,0042133333&DAHDI/g1/2212)}", test_strings[i][0]);
  1516. pbx_substitute_variables_helper(NULL, tmp, tmp2, sizeof(tmp2) - 1);
  1517. if (strcmp(test_strings[i][1], tmp2)) {
  1518. ast_test_status_update(test, "Format string '%s' substituted to '%s'. Expected '%s'.\n", test_strings[i][0], tmp2, test_strings[i][1]);
  1519. res = AST_TEST_FAIL;
  1520. }
  1521. }
  1522. return res;
  1523. }
  1524. AST_TEST_DEFINE(test_STRREPLACE)
  1525. {
  1526. int i, res = AST_TEST_PASS;
  1527. struct ast_channel *chan; /* dummy channel */
  1528. struct ast_str *str; /* fancy string for holding comparing value */
  1529. const char *test_strings[][5] = {
  1530. {"Weasels have eaten my telephone system", "have eaten my", "are eating our", "", "Weasels are eating our telephone system"}, /*Test normal conditions */
  1531. {"Did you know twenty plus two is twenty-two?", "twenty", "thirty", NULL, "Did you know thirty plus two is thirty-two?"}, /* Test no third comma */
  1532. {"foofoofoofoofoofoofoo", "foofoo", "bar", NULL, "barbarbarfoo"}, /* Found string within previous match */
  1533. {"My pet dog once ate a dog who sat on a dog while eating a corndog.", "dog", "cat", "3", "My pet cat once ate a cat who sat on a cat while eating a corndog."},
  1534. {"One and one and one is three", "and", "plus", "1", "One plus one and one is three"}, /* Test <max-replacements> = 1*/
  1535. {"", "fhqwagads", "spelunker", NULL, ""}, /* Empty primary string */
  1536. {"Part of this string is missing.", "missing", NULL, NULL, "Part of this string is ."}, /* Empty replace string */
  1537. {"'Accidentally' left off a bunch of stuff.", NULL, NULL, NULL, ""}, /* Deliberate error test from too few args */
  1538. {"This test will also error.", "", "", "", ""}, /* Deliberate error test from blank find string */
  1539. {"This is an \"escape character\" test.", "\\\"escape character\\\"", "evil", NULL, "This is an evil test."}
  1540. };
  1541. switch (cmd) {
  1542. case TEST_INIT:
  1543. info->name = "func_STRREPLACE_test";
  1544. info->category = "/funcs/func_strings/";
  1545. info->summary = "Test STRREPLACE function";
  1546. info->description = "Verify STRREPLACE behavior";
  1547. return AST_TEST_NOT_RUN;
  1548. case TEST_EXECUTE:
  1549. break;
  1550. }
  1551. if (!(chan = ast_dummy_channel_alloc())) {
  1552. ast_test_status_update(test, "Unable to allocate dummy channel\n");
  1553. return AST_TEST_FAIL;
  1554. }
  1555. if (!(str = ast_str_create(64))) {
  1556. ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
  1557. ast_channel_release(chan);
  1558. return AST_TEST_FAIL;
  1559. }
  1560. for (i = 0; i < ARRAY_LEN(test_strings); i++) {
  1561. char tmp[512], tmp2[512] = "";
  1562. struct ast_var_t *var = ast_var_assign("test_string", test_strings[i][0]);
  1563. AST_LIST_INSERT_HEAD(&chan->varshead, var, entries);
  1564. if (test_strings[i][3]) {
  1565. snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s,%s,%s,%s)}", "test_string", test_strings[i][1], test_strings[i][2], test_strings[i][3]);
  1566. } else if (test_strings[i][2]) {
  1567. snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s,%s,%s)}", "test_string", test_strings[i][1], test_strings[i][2]);
  1568. } else if (test_strings[i][1]) {
  1569. snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s,%s)}", "test_string", test_strings[i][1]);
  1570. } else {
  1571. snprintf(tmp, sizeof(tmp), "${STRREPLACE(%s)}", "test_string");
  1572. }
  1573. ast_str_substitute_variables(&str, 0, chan, tmp);
  1574. if (strcmp(test_strings[i][4], ast_str_buffer(str))) {
  1575. ast_test_status_update(test, "Format string '%s' substituted to '%s'. Expected '%s'.\n", test_strings[i][0], tmp2, test_strings[i][4]);
  1576. res = AST_TEST_FAIL;
  1577. }
  1578. }
  1579. ast_free(str);
  1580. ast_channel_release(chan);
  1581. return res;
  1582. }
  1583. #endif
  1584. static int unload_module(void)
  1585. {
  1586. int res = 0;
  1587. AST_TEST_UNREGISTER(test_FIELDNUM);
  1588. AST_TEST_UNREGISTER(test_FILTER);
  1589. AST_TEST_UNREGISTER(test_STRREPLACE);
  1590. res |= ast_custom_function_unregister(&fieldqty_function);
  1591. res |= ast_custom_function_unregister(&fieldnum_function);
  1592. res |= ast_custom_function_unregister(&filter_function);
  1593. res |= ast_custom_function_unregister(&replace_function);
  1594. res |= ast_custom_function_unregister(&strreplace_function);
  1595. res |= ast_custom_function_unregister(&listfilter_function);
  1596. res |= ast_custom_function_unregister(&regex_function);
  1597. res |= ast_custom_function_unregister(&array_function);
  1598. res |= ast_custom_function_unregister(&quote_function);
  1599. res |= ast_custom_function_unregister(&csv_quote_function);
  1600. res |= ast_custom_function_unregister(&len_function);
  1601. res |= ast_custom_function_unregister(&strftime_function);
  1602. res |= ast_custom_function_unregister(&strptime_function);
  1603. res |= ast_custom_function_unregister(&eval_function);
  1604. res |= ast_custom_function_unregister(&keypadhash_function);
  1605. res |= ast_custom_function_unregister(&hashkeys_function);
  1606. res |= ast_custom_function_unregister(&hash_function);
  1607. res |= ast_unregister_application(app_clearhash);
  1608. res |= ast_custom_function_unregister(&toupper_function);
  1609. res |= ast_custom_function_unregister(&tolower_function);
  1610. res |= ast_custom_function_unregister(&shift_function);
  1611. res |= ast_custom_function_unregister(&pop_function);
  1612. res |= ast_custom_function_unregister(&push_function);
  1613. res |= ast_custom_function_unregister(&unshift_function);
  1614. res |= ast_custom_function_unregister(&passthru_function);
  1615. return res;
  1616. }
  1617. static int load_module(void)
  1618. {
  1619. int res = 0;
  1620. AST_TEST_REGISTER(test_FIELDNUM);
  1621. AST_TEST_REGISTER(test_FILTER);
  1622. AST_TEST_REGISTER(test_STRREPLACE);
  1623. res |= ast_custom_function_register(&fieldqty_function);
  1624. res |= ast_custom_function_register(&fieldnum_function);
  1625. res |= ast_custom_function_register(&filter_function);
  1626. res |= ast_custom_function_register(&replace_function);
  1627. res |= ast_custom_function_register(&strreplace_function);
  1628. res |= ast_custom_function_register(&listfilter_function);
  1629. res |= ast_custom_function_register(&regex_function);
  1630. res |= ast_custom_function_register(&array_function);
  1631. res |= ast_custom_function_register(&quote_function);
  1632. res |= ast_custom_function_register(&csv_quote_function);
  1633. res |= ast_custom_function_register(&len_function);
  1634. res |= ast_custom_function_register(&strftime_function);
  1635. res |= ast_custom_function_register(&strptime_function);
  1636. res |= ast_custom_function_register(&eval_function);
  1637. res |= ast_custom_function_register(&keypadhash_function);
  1638. res |= ast_custom_function_register(&hashkeys_function);
  1639. res |= ast_custom_function_register(&hash_function);
  1640. res |= ast_register_application_xml(app_clearhash, exec_clearhash);
  1641. res |= ast_custom_function_register(&toupper_function);
  1642. res |= ast_custom_function_register(&tolower_function);
  1643. res |= ast_custom_function_register(&shift_function);
  1644. res |= ast_custom_function_register(&pop_function);
  1645. res |= ast_custom_function_register(&push_function);
  1646. res |= ast_custom_function_register(&unshift_function);
  1647. res |= ast_custom_function_register(&passthru_function);
  1648. return res;
  1649. }
  1650. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "String handling dialplan functions");