Changeset 8358691 for root_commands.c


Ignore:
Timestamp:
2010-08-31T20:05:36Z (14 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
f5c0d8e
Parents:
ad2d8bc
Message:

Added root_command_add() and use it to create the "otr" command.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • root_commands.c

    rad2d8bc r8358691  
    2929#include "help.h"
    3030#include "ipc.h"
    31 #include "otr.h"
    3231
    3332void root_command_string( irc_t *irc, char *command )
     
    5655       
    5756        len = strlen( cmd[0] );
    58         for( i = 0; commands[i].command; i++ )
    59                 if( g_strncasecmp( commands[i].command, cmd[0], len ) == 0 )
    60                 {
    61                         if( commands[i+1].command &&
    62                             g_strncasecmp( commands[i+1].command, cmd[0], len ) == 0 )
     57        for( i = 0; root_commands[i].command; i++ )
     58                if( g_strncasecmp( root_commands[i].command, cmd[0], len ) == 0 )
     59                {
     60                        if( root_commands[i+1].command &&
     61                            g_strncasecmp( root_commands[i+1].command, cmd[0], len ) == 0 )
    6362                                /* Only match on the first letters if the match is unique. */
    6463                                break;
    6564                       
    66                         MIN_ARGS( commands[i].required_parameters );
     65                        MIN_ARGS( root_commands[i].required_parameters );
    6766                       
    68                         commands[i].execute( irc, cmd );
     67                        root_commands[i].execute( irc, cmd );
    6968                        return;
    7069                }
     
    13251324
    13261325/* IMPORTANT: Keep this list sorted! The short command logic needs that. */
    1327 const command_t commands[] = {
     1326command_t root_commands[] = {
    13281327        { "account",        1, cmd_account,        0 },
    13291328        { "add",            2, cmd_add,            0 },
     
    13401339        { "info",           1, cmd_info,           0 },
    13411340        { "no",             0, cmd_yesno,          0 },
    1342         { "otr",            1, cmd_otr,            0 },
    13431341        { "qlist",          0, cmd_qlist,          0 },
    13441342        { "register",       1, cmd_register,       0 },
     
    13491347        { "transfer",       0, cmd_transfer,       0 },
    13501348        { "yes",            0, cmd_yesno,          0 },
    1351         { NULL }
     1349        /* Not expecting too many plugins adding root commands so just make a
     1350           dumb array with some empty entried at the end. */
     1351        { NULL },
     1352        { NULL },
     1353        { NULL },
     1354        { NULL },
     1355        { NULL },
     1356        { NULL },
     1357        { NULL },
     1358        { NULL },
     1359        { NULL },
    13521360};
     1361static const int num_root_commands = sizeof( root_commands ) / sizeof( command_t );
     1362
     1363gboolean root_command_add( const char *command, int params, void (*func)(irc_t *, char **args), int flags )
     1364{
     1365        int i;
     1366       
     1367        if( root_commands[num_root_commands-2].command )
     1368                /* Planning fail! List is full. */
     1369                return FALSE;
     1370       
     1371        for( i = 0; root_commands[i].command; i++ )
     1372        {
     1373                if( g_strcasecmp( root_commands[i].command, command ) == 0 )
     1374                        return FALSE;
     1375                else if( g_strcasecmp( root_commands[i].command, command ) > 0 )
     1376                        break;
     1377        }
     1378        memmove( root_commands + i + 1, root_commands + i,
     1379                 sizeof( command_t ) * ( num_root_commands - i - 1 ) );
     1380       
     1381        root_commands[i].command = g_strdup( command );
     1382        root_commands[i].required_parameters = params;
     1383        root_commands[i].execute = func;
     1384        root_commands[i].flags = flags;
     1385       
     1386        return TRUE;
     1387}
Note: See TracChangeset for help on using the changeset viewer.