Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / core / commands.c
index 96a740088015aab24d271e97d8fb1858cf6defcc..1b811bc42611de6f19c966b03d5f8e867d9a5cad 100644 (file)
@@ -13,9 +13,9 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include "module.h"
@@ -89,6 +89,7 @@ command_module_find_and_remove(COMMAND_REC *rec, SIGNAL_FUNC func)
                        if (cb->func == func) {
                                rec->callbacks =
                                        g_slist_remove(rec->callbacks, cb);
+                               g_free(cb);
                                return rec;
                        }
                }
@@ -324,7 +325,7 @@ void command_runsub(const char *cmd, const char *data,
 
        subcmd = g_strconcat("command ", newcmd, NULL);
 
-       g_strdown(subcmd);
+       ascii_strdown(subcmd);
        if (!signal_emit(subcmd, 3, args, server, item)) {
                defcmd = g_strdup_printf("default command %s", cmd);
                if (!signal_emit(defcmd, 3, data, server, item)) {
@@ -402,7 +403,7 @@ static void command_calc_options(COMMAND_REC *rec, const char *options)
 
                oldopt = optlist_find(list, name);
                if (oldopt != NULL) {
-                        /* already specified - overwrite old defination */
+                        /* already specified - overwrite old definition */
                        g_free(oldopt->data);
                        oldopt->data = g_strdup(*tmp);
                } else {
@@ -483,7 +484,7 @@ char *cmd_get_param(char **data)
        return pos;
 }
 
-static char *cmd_get_quoted_param(char **data)
+char *cmd_get_quoted_param(char **data)
 {
        char *pos, quote;
 
@@ -497,13 +498,18 @@ static char *cmd_get_quoted_param(char **data)
        quote = **data; (*data)++;
 
        pos = *data;
-       while (**data != '\0' && **data != quote) {
+       while (**data != '\0' && (**data != quote ||
+                                 ((*data)[1] != ' ' && (*data)[1] != '\0'))) {
                if (**data == '\\' && (*data)[1] != '\0')
                         g_memmove(*data, (*data)+1, strlen(*data));
                (*data)++;
        }
 
-       if (**data != '\0') *(*data)++ = '\0';
+       if (**data == quote) {
+               *(*data)++ = '\0';
+               if (**data == ' ')
+                       (*data)++;
+       }
 
        return pos;
 }
@@ -555,7 +561,7 @@ static int get_cmd_options(char **data, int ignore_unknown,
        char *option, *arg, **optlist;
        int pos;
 
-       /* get option definations */
+       /* get option definitions */
        rec = cmd == NULL ? NULL : command_find(cmd);
        optlist = rec == NULL ? NULL : rec->options;
 
@@ -569,17 +575,17 @@ static int get_cmd_options(char **data, int ignore_unknown,
                        }
 
                        (*data)++;
-                       if (**data == '-' && i_isspace((*data)[1])) {
+                       if (**data == '-' && (*data)[1] == ' ') {
                                /* -- option means end of options even
                                   if next word starts with - */
                                (*data)++;
-                               while (i_isspace(**data)) (*data)++;
+                               while (**data == ' ') (*data)++;
                                break;
                        }
 
                        if (**data == '\0')
                                option = "-";
-                       else if (!i_isspace(**data))
+                       else if (**data != ' ')
                                option = cmd_get_param(data);
                        else {
                                option = "-";
@@ -624,14 +630,14 @@ static int get_cmd_options(char **data, int ignore_unknown,
                            *optlist[pos] == '!')
                                option = NULL;
 
-                       while (i_isspace(**data)) (*data)++;
+                       while (**data == ' ') (*data)++;
                        continue;
                }
 
                if (option == NULL)
                        break;
 
-               if (*optlist[pos] == '@' && !i_isdigit(**data))
+               if (*optlist[pos] == '@' && !is_numeric(*data, ' '))
                        break; /* expected a numeric argument */
 
                /* save the argument */
@@ -642,7 +648,7 @@ static int get_cmd_options(char **data, int ignore_unknown,
                }
                option = NULL;
 
-               while (i_isspace(**data)) (*data)++;
+               while (**data == ' ') (*data)++;
        }
 
        return 0;
@@ -653,11 +659,12 @@ typedef struct {
         GHashTable *options;
 } CMD_TEMP_REC;
 
-static char *get_optional_channel(WI_ITEM_REC *active_item, char **data,
-                                 int require_name)
+static const char *
+get_optional_channel(WI_ITEM_REC *active_item, char **data, int require_name)
 {
         CHANNEL_REC *chanrec;
-       char *tmp, *origtmp, *channel, *ret;
+       const char *ret;
+       char *tmp, *origtmp, *channel;
 
        if (active_item == NULL) {
                 /* no active channel in window, channel required */
@@ -670,10 +677,10 @@ static char *get_optional_channel(WI_ITEM_REC *active_item, char **data,
        if (strcmp(channel, "*") == 0 && !require_name) {
                 /* "*" means active channel */
                cmd_get_param(data);
-               ret = active_item->name;
+               ret = window_item_get_target(active_item);
        } else if (!server_ischannel(active_item->server, channel)) {
                 /* we don't have channel parameter - use active channel */
-               ret = active_item->name;
+               ret = window_item_get_target(active_item);
        } else {
                /* Find the channel first and use it's name if found.
                   This allows automatic !channel -> !XXXXXchannel replaces. */
@@ -730,7 +737,7 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...)
                        /* optional channel as first parameter */
                        require_name = (count & PARAM_FLAG_OPTCHAN_NAME) ==
                                PARAM_FLAG_OPTCHAN_NAME;
-                       arg = get_optional_channel(item, &datad, require_name);
+                       arg = (char *) get_optional_channel(item, &datad, require_name);
 
                        str = (char **) va_arg(args, char **);
                        if (str != NULL) *str = arg;
@@ -884,7 +891,7 @@ static void parse_command(const char *command, int expand_aliases,
        }
 
        cmd = g_strconcat("command ", newcmd, NULL);
-       g_strdown(cmd);
+       ascii_strdown(cmd);
 
        oldcmd = current_command;
        current_command = cmd+8;
@@ -911,13 +918,8 @@ static void event_command(const char *line, SERVER_REC *server, void *item)
 
        g_return_if_fail(line != NULL);
 
-       if (*line == '\0') {
-               /* empty line, forget it. */
-                signal_stop();
-               return;
-       }
-
-       cmdchar = strchr(settings_get_str("cmdchars"), *line);
+       cmdchar = *line == '\0' ? NULL :
+               strchr(settings_get_str("cmdchars"), *line);
        if (cmdchar != NULL && line[1] == ' ') {
                /* "/ text" = same as sending "text" to active channel. */
                line += 2;
@@ -929,7 +931,7 @@ static void event_command(const char *line, SERVER_REC *server, void *item)
                return;
        }
 
-       /* same cmdchar twice ignores aliases ignores aliases */
+       /* same cmdchar twice ignores aliases */
        line++;
        if (*line == *cmdchar) {
                line++;
@@ -943,12 +945,18 @@ static void event_command(const char *line, SERVER_REC *server, void *item)
        parse_command(line, expand_aliases, server, item);
 }
 
+static int eval_recursion_depth=0;
 /* SYNTAX: EVAL <command(s)> */
 static void cmd_eval(const char *data, SERVER_REC *server, void *item)
 {
        g_return_if_fail(data != NULL);
+       if (eval_recursion_depth > 100)
+               cmd_return_error(CMDERR_EVAL_MAX_RECURSE);
+
 
+       eval_recursion_depth++;
        eval_special_string(data, "", server, item);
+       eval_recursion_depth--;
 }
 
 /* SYNTAX: CD <directory> */