[go: up one dir, main page]

Menu

[r456]: / tags / start / src / commands.c  Maximize  Restore  History

Download this file

193 lines (150 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* -------------------------------------------------------
* Copyright (C) 2003-2004 Tommi Saviranta <tsaviran@cs.helsinki.fi>
* (C) 2002 Lee Hardy <lee@leeh.co.uk>
* -------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <string.h>
#include <stdio.h>
#include "commands.h"
#include "common.h"
static struct commandhash *cmd_hash[MAX_CMD];
static int command_hash(const char *p);
void command_add(const char *p, int cmdvalue);
/* The struct for the command hash table. */
struct commandhash {
const char *cmd;
int cmdvalue;
struct commandhash *next;
};
/* The struct for the table below. */
struct commandaddstruct {
char *cmd;
int mask;
};
/*
* The table of commands that we need to add at startup, and their relevant
* bitmasks, as defined in commands.h.
*/
static struct commandaddstruct cmd_add_table[] =
{
/* command, bitmask, */
{"PING", CMD_PING, },
{"PONG", CMD_PONG, },
{"MODE", CMD_MODE, },
{"NICK", CMD_NICK, },
{"NOTICE", CMD_NOTICE, },
{"KICK", CMD_KICK, },
{"JOIN", CMD_JOIN, },
{"PART", CMD_PART, },
{"TOPIC", CMD_TOPIC, },
{"KILL", CMD_KILL, },
{"PRIVMSG", CMD_PRIVMSG, },
{"QUIT", CMD_QUIT, },
{NULL, CMD_NONE, }
};
/*
* Hashes a command to a value.
*/
static int
command_hash(
const char *p
)
{
int hash_val = 0;
while (*p) {
hash_val += ((int) (*p) & 0xDF);
p++;
}
return (hash_val % MAX_CMD);
} /* static int command_hash(const char *) */
/*
* Searches for a command in the command hash table.
*/
int
command_find(
char *p
)
{
struct commandhash *ptr;
int cmdindex;
cmdindex = command_hash(p);
for (ptr = cmd_hash[cmdindex]; ptr; ptr = ptr->next) {
if (xstrcasecmp(p, ptr->cmd) == 0) {
return ptr->cmdvalue;
}
}
return 0;
} /* int command_find(char *) */
/*
* Adds a command to the command hash table.
*/
void
command_add(
const char *cmd,
int cmdvalue
)
{
struct commandhash *ptr;
struct commandhash *temp_ptr;
struct commandhash *last_ptr = NULL;
int cmdindex;
cmdindex = command_hash(cmd);
/* command exists */
for (temp_ptr = cmd_hash[cmdindex]; temp_ptr;
temp_ptr = temp_ptr->next) {
if (xstrcasecmp(cmd, temp_ptr->cmd) == 0) {
return;
}
last_ptr = temp_ptr;
}
ptr = (struct commandhash *) xcalloc(1, sizeof(struct commandhash));
ptr->cmd = cmd;
ptr->cmdvalue = cmdvalue;
if (last_ptr) {
last_ptr->next = ptr;
} else {
cmd_hash[cmdindex] = ptr;
}
} /* void command_add(const char *, int) */
/*
* Walks the commandsadd table and adds all the entries.
*/
void
command_setup(
)
{
int i;
for (i = 0; i < MAX_CMD; i++) {
cmd_hash[i] = NULL;
}
for (i = 0; cmd_add_table[i].cmd; i++) {
command_add(cmd_add_table[i].cmd, cmd_add_table[i].mask);
}
} /* void command_setup() */
/*
* Free memore allocated by command-hash.
*/
void
command_free(
)
{
int i;
struct commandhash *ptr;
struct commandhash *next = NULL;
for (i = 0; i < MAX_CMD; i++) {
for (ptr = cmd_hash[i]; ptr != NULL; ptr = next) {
next = ptr->next;
xfree(ptr);
}
}
} /* void command_free() */