mirror of
https://github.com/asterisk/asterisk.git
synced 2025-11-07 18:38:02 +00:00
- move the string join() function to utils.c since it is used in both cli.c and res_agi.c
- reimplement ast_join to be of linear effieciency instead of quadratic - remove some useless checks for "if (e)" - reorder checks for strings starting with '_' to avoid a useless call to ast_join() - check array bounds when parsing arguments to AGI (issue #5868) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@7556 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
13
cli.c
13
cli.c
@@ -1019,17 +1019,8 @@ static struct ast_cli_entry *find_cli(char *cmds[], int exact)
|
|||||||
|
|
||||||
static void join(char *dest, size_t destsize, char *w[], int tws)
|
static void join(char *dest, size_t destsize, char *w[], int tws)
|
||||||
{
|
{
|
||||||
int x;
|
ast_join(dest, destsize, w);
|
||||||
/* Join words into a string */
|
|
||||||
if (!dest || destsize < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
dest[0] = '\0';
|
|
||||||
for (x=0;w[x];x++) {
|
|
||||||
if (x)
|
|
||||||
strncat(dest, " ", destsize - strlen(dest) - 1);
|
|
||||||
strncat(dest, w[x], destsize - strlen(dest) - 1);
|
|
||||||
}
|
|
||||||
if (tws && !ast_strlen_zero(dest))
|
if (tws && !ast_strlen_zero(dest))
|
||||||
strncat(dest, " ", destsize - strlen(dest) - 1);
|
strncat(dest, " ", destsize - strlen(dest) - 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ void ast_copy_string(char *dst, const char *src, size_t size),
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Build a string in a buffer, designed to be called repeatedly
|
\brief Build a string in a buffer, designed to be called repeatedly
|
||||||
|
|
||||||
@@ -210,6 +211,18 @@ int ast_true(const char *val);
|
|||||||
*/
|
*/
|
||||||
int ast_false(const char *val);
|
int ast_false(const char *val);
|
||||||
|
|
||||||
|
/*
|
||||||
|
\brief Join an array of strings into a single string.
|
||||||
|
\param s the resulting string buffer
|
||||||
|
\param len the length of the result buffer, s
|
||||||
|
\param w an array of strings to join
|
||||||
|
|
||||||
|
This function will join all of the strings in the array 'w' into a single
|
||||||
|
string. It will also place a space in the result buffer in between each
|
||||||
|
string from 'w'.
|
||||||
|
*/
|
||||||
|
void ast_join(char *s, size_t len, char * const w[]);
|
||||||
|
|
||||||
/* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
|
/* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
|
||||||
|
|
||||||
struct ast_realloca {
|
struct ast_realloca {
|
||||||
|
|||||||
@@ -1627,22 +1627,6 @@ static agi_command commands[MAX_COMMANDS] = {
|
|||||||
{ { "wait", "for", "digit", NULL }, handle_waitfordigit, "Waits for a digit to be pressed", usage_waitfordigit },
|
{ { "wait", "for", "digit", NULL }, handle_waitfordigit, "Waits for a digit to be pressed", usage_waitfordigit },
|
||||||
};
|
};
|
||||||
|
|
||||||
static void join(char *s, size_t len, char *w[])
|
|
||||||
{
|
|
||||||
int x;
|
|
||||||
|
|
||||||
/* Join words into a string */
|
|
||||||
if (!s) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
s[0] = '\0';
|
|
||||||
for (x=0; w[x]; x++) {
|
|
||||||
if (x)
|
|
||||||
strncat(s, " ", len - strlen(s) - 1);
|
|
||||||
strncat(s, w[x], len - strlen(s) - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int help_workhorse(int fd, char *match[])
|
static int help_workhorse(int fd, char *match[])
|
||||||
{
|
{
|
||||||
char fullcmd[80];
|
char fullcmd[80];
|
||||||
@@ -1650,20 +1634,17 @@ static int help_workhorse(int fd, char *match[])
|
|||||||
int x;
|
int x;
|
||||||
struct agi_command *e;
|
struct agi_command *e;
|
||||||
if (match)
|
if (match)
|
||||||
join(matchstr, sizeof(matchstr), match);
|
ast_join(matchstr, sizeof(matchstr), match);
|
||||||
for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
|
for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
|
||||||
if (!commands[x].cmda[0]) break;
|
|
||||||
e = &commands[x];
|
e = &commands[x];
|
||||||
if (e)
|
if (!e->cmda[0])
|
||||||
join(fullcmd, sizeof(fullcmd), e->cmda);
|
break;
|
||||||
/* Hide commands that start with '_' */
|
/* Hide commands that start with '_' */
|
||||||
if (fullcmd[0] == '_')
|
if ((e->cmda[0])[0] == '_')
|
||||||
|
continue;
|
||||||
|
ast_join(fullcmd, sizeof(fullcmd), e->cmda);
|
||||||
|
if (match && strncasecmp(matchstr, fullcmd, strlen(matchstr)))
|
||||||
continue;
|
continue;
|
||||||
if (match) {
|
|
||||||
if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ast_cli(fd, "%20.20s %s\n", fullcmd, e->summary);
|
ast_cli(fd, "%20.20s %s\n", fullcmd, e->summary);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1801,10 +1782,9 @@ normal:
|
|||||||
static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf)
|
static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf)
|
||||||
{
|
{
|
||||||
char *argv[MAX_ARGS];
|
char *argv[MAX_ARGS];
|
||||||
int argc = 0;
|
int argc = MAX_ARGS;
|
||||||
int res;
|
int res;
|
||||||
agi_command *c;
|
agi_command *c;
|
||||||
argc = MAX_ARGS;
|
|
||||||
|
|
||||||
parse_args(buf, &argc, argv);
|
parse_args(buf, &argc, argv);
|
||||||
#if 0
|
#if 0
|
||||||
@@ -1929,7 +1909,7 @@ static int handle_showagi(int fd, int argc, char *argv[]) {
|
|||||||
if (find_command(argv + 2, -1)) {
|
if (find_command(argv + 2, -1)) {
|
||||||
return help_workhorse(fd, argv + 1);
|
return help_workhorse(fd, argv + 1);
|
||||||
} else {
|
} else {
|
||||||
join(fullcmd, sizeof(fullcmd), argv+1);
|
ast_join(fullcmd, sizeof(fullcmd), argv+1);
|
||||||
ast_cli(fd, "No such command '%s'.\n", fullcmd);
|
ast_cli(fd, "No such command '%s'.\n", fullcmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1942,7 +1922,6 @@ static int handle_showagi(int fd, int argc, char *argv[]) {
|
|||||||
static int handle_dumpagihtml(int fd, int argc, char *argv[]) {
|
static int handle_dumpagihtml(int fd, int argc, char *argv[]) {
|
||||||
struct agi_command *e;
|
struct agi_command *e;
|
||||||
char fullcmd[80];
|
char fullcmd[80];
|
||||||
char *tempstr;
|
|
||||||
int x;
|
int x;
|
||||||
FILE *htmlfile;
|
FILE *htmlfile;
|
||||||
|
|
||||||
@@ -1961,29 +1940,27 @@ static int handle_dumpagihtml(int fd, int argc, char *argv[]) {
|
|||||||
fprintf(htmlfile, "<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n");
|
fprintf(htmlfile, "<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n");
|
||||||
|
|
||||||
for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
|
for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
|
||||||
char *stringp=NULL;
|
char *stringp, *tempstr;
|
||||||
if (!commands[x].cmda[0]) break;
|
|
||||||
e = &commands[x];
|
e = &commands[x];
|
||||||
if (e)
|
if (!e->cmda[0]) /* end ? */
|
||||||
join(fullcmd, sizeof(fullcmd), e->cmda);
|
break;
|
||||||
/* Hide commands that start with '_' */
|
/* Hide commands that start with '_' */
|
||||||
if (fullcmd[0] == '_')
|
if ((e->cmda[0])[0] == '_')
|
||||||
continue;
|
continue;
|
||||||
|
ast_join(fullcmd, sizeof(fullcmd), e->cmda);
|
||||||
|
|
||||||
fprintf(htmlfile, "<TR><TD><TABLE BORDER=\"1\" CELLPADDING=\"5\" WIDTH=\"100%%\">\n");
|
fprintf(htmlfile, "<TR><TD><TABLE BORDER=\"1\" CELLPADDING=\"5\" WIDTH=\"100%%\">\n");
|
||||||
fprintf(htmlfile, "<TR><TH ALIGN=\"CENTER\"><B>%s - %s</B></TD></TR>\n", fullcmd,e->summary);
|
fprintf(htmlfile, "<TR><TH ALIGN=\"CENTER\"><B>%s - %s</B></TD></TR>\n", fullcmd,e->summary);
|
||||||
|
|
||||||
|
|
||||||
stringp=e->usage;
|
stringp=e->usage;
|
||||||
tempstr = strsep(&stringp, "\n");
|
tempstr = strsep(&stringp, "\n");
|
||||||
|
|
||||||
fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">%s</TD></TR>\n", tempstr);
|
fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">%s</TD></TR>\n", tempstr);
|
||||||
|
|
||||||
fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">\n");
|
fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">\n");
|
||||||
while ((tempstr = strsep(&stringp, "\n")) != NULL) {
|
while ((tempstr = strsep(&stringp, "\n")) != NULL)
|
||||||
fprintf(htmlfile, "%s<BR>\n",tempstr);
|
fprintf(htmlfile, "%s<BR>\n",tempstr);
|
||||||
|
|
||||||
}
|
|
||||||
fprintf(htmlfile, "</TD></TR>\n");
|
fprintf(htmlfile, "</TD></TR>\n");
|
||||||
fprintf(htmlfile, "</TABLE></TD></TR>\n\n");
|
fprintf(htmlfile, "</TABLE></TD></TR>\n\n");
|
||||||
|
|
||||||
@@ -2016,9 +1993,8 @@ static int agi_exec_full(struct ast_channel *chan, void *data, int enhanced, int
|
|||||||
ast_copy_string(buf, data, sizeof(buf));
|
ast_copy_string(buf, data, sizeof(buf));
|
||||||
|
|
||||||
memset(&agi, 0, sizeof(agi));
|
memset(&agi, 0, sizeof(agi));
|
||||||
while ((stringp = strsep(&tmp, "|"))) {
|
while ((stringp = strsep(&tmp, "|")) && argc < MAX_ARGS-1)
|
||||||
argv[argc++] = stringp;
|
argv[argc++] = stringp;
|
||||||
}
|
|
||||||
argv[argc] = NULL;
|
argv[argc] = NULL;
|
||||||
|
|
||||||
LOCAL_USER_ADD(u);
|
LOCAL_USER_ADD(u);
|
||||||
|
|||||||
19
utils.c
19
utils.c
@@ -903,3 +903,22 @@ char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
|
|||||||
*dataPut = 0;
|
*dataPut = 0;
|
||||||
return dataPut;
|
return dataPut;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ast_join(char *s, size_t len, char * const w[])
|
||||||
|
{
|
||||||
|
int x, ofs = 0;
|
||||||
|
const char *src;
|
||||||
|
|
||||||
|
/* Join words into a string */
|
||||||
|
if (!s)
|
||||||
|
return;
|
||||||
|
for (x=0; ofs < len && w[x]; x++) {
|
||||||
|
if (x > 0)
|
||||||
|
s[ofs++] = ' ';
|
||||||
|
for (src = w[x]; *src && ofs < len; src++)
|
||||||
|
s[ofs++] = *src;
|
||||||
|
}
|
||||||
|
if (ofs == len)
|
||||||
|
ofs--;
|
||||||
|
s[ofs] = '\0';
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user