Asterisk - The Open Source Telephony Project  18.5.0
func_rand.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  * Copyright (C) 2006, Claude Patry
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17 
18 /*! \file
19  *
20  * \brief Generate Random Number
21  *
22  * \author Claude Patry <[email protected]>
23  * \author Tilghman Lesher ( http://asterisk.drunkcoder.com/ )
24  * \ingroup functions
25  */
26 
27 /*** MODULEINFO
28  <support_level>core</support_level>
29  ***/
30 
31 #include "asterisk.h"
32 
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 
39 /*** DOCUMENTATION
40  <function name="RAND" language="en_US">
41  <synopsis>
42  Choose a random number in a range.
43  </synopsis>
44  <syntax>
45  <parameter name="min" />
46  <parameter name="max" />
47  </syntax>
48  <description>
49  <para>Choose a random number between <replaceable>min</replaceable> and <replaceable>max</replaceable>.
50  <replaceable>min</replaceable> defaults to <literal>0</literal>, if not specified, while <replaceable>max</replaceable> defaults
51  to <literal>RAND_MAX</literal> (2147483647 on many systems).</para>
52  <para>Example: Set(junky=${RAND(1,8)});
53  Sets junky to a random number between 1 and 8, inclusive.</para>
54  </description>
55  </function>
56  ***/
57 static int acf_rand_exec(struct ast_channel *chan, const char *cmd,
58  char *parse, char *buffer, size_t buflen)
59 {
60  int min_int, response_int, max_int;
64  );
65 
67 
68  if (ast_strlen_zero(args.min) || sscanf(args.min, "%30d", &min_int) != 1)
69  min_int = 0;
70 
71  if (ast_strlen_zero(args.max) || sscanf(args.max, "%30d", &max_int) != 1)
72  max_int = RAND_MAX;
73 
74  if (max_int < min_int) {
75  int tmp = max_int;
76 
77  max_int = min_int;
78  min_int = tmp;
79  ast_debug(1, "max<min\n");
80  }
81 
82  response_int = min_int + (ast_random() % (max_int - min_int + 1));
83  ast_debug(1, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
84  snprintf(buffer, buflen, "%d", response_int);
85 
86  return 0;
87 }
88 
89 static struct ast_custom_function acf_rand = {
90  .name = "RAND",
91  .read = acf_rand_exec,
92  .read_max = 12,
93 };
94 
95 static int unload_module(void)
96 {
98 
99  return 0;
100 }
101 
102 static int load_module(void)
103 {
104  return ast_custom_function_register(&acf_rand);
105 }
106 
107 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function");
const char * name
Definition: pbx.h:119
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
static int tmp()
Definition: bt_open.c:389
const char * args
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Utility functions.
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static int unload_module(void)
Definition: func_rand.c:95
General Asterisk PBX channel definitions.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
static int acf_rand_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
Definition: func_rand.c:57
long int ast_random(void)
Definition: main/utils.c:2064
Core PBX routines and definitions.
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
static struct ast_custom_function acf_rand
Definition: func_rand.c:89
static int load_module(void)
Definition: func_rand.c:102
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define min(a, b)
Definition: f2c.h:197
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508
#define AST_APP_ARG(name)
Define an application argument.
#define max(a, b)
Definition: f2c.h:198