-
Notifications
You must be signed in to change notification settings - Fork 8
/
slang_text.c
200 lines (175 loc) · 5.03 KB
/
slang_text.c
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
193
194
195
196
197
198
199
200
/*
* Copyright (C) 2000,2001 Florian Sander
*
* 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.
*
* 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.
*/
struct slang_text {
struct slang_text *next;
char *string;
void (*command) ();
};
struct slang_text_commands {
char *command;
void (*targetfunc) ();
};
struct slang_command_list {
struct slang_command_list *next;
struct slang_text_commands *commands;
};
static struct slang_text *slang_text_parse(char *);
static struct slang_text *slang_text_create(struct slang_text *);
static void slang_text_add_string(struct slang_text *, char *);
static void slang_text_add_command(struct slang_text *, char *);
static void slang_text_free(struct slang_text *);
static int slang_text_expmem(struct slang_text *);
static char *slang_text_get(struct slang_text *);
#ifndef SLANG_NOTYPES
static int slang_text_strcasecmp(struct slang_text *, char *);
#endif
static struct slang_text *slang_text_parse(char *text)
{
char *cmdstart, *cmdend;
struct slang_text *firstitem, *item;
firstitem = slang_text_create(NULL);
item = firstitem;
while ((cmdstart = strstr(text, "<?"))) {
cmdstart[0] = 0;
slang_text_add_string(item, text);
item = slang_text_create(item);
text += 2;
cmdstart += 2;
cmdend = strstr(cmdstart, "/?>");
if (!cmdend) {
putlog(LOG_MISC, "*", "ERROR parsing slang text: unterminated command \"%s\"!", cmdstart);
break;
}
cmdend[0] = 0;
slang_text_add_command(item, cmdstart);
item = slang_text_create(item);
text = cmdend + 3;
}
slang_text_add_string(item, text);
return firstitem;
}
static struct slang_text *slang_text_create(struct slang_text *where)
{
struct slang_text *newpart;
newpart = nmalloc(sizeof(struct slang_text));
newpart->next = NULL;
newpart->string = NULL;
newpart->command = NULL;
while (where && where->next)
where = where->next;
if (where)
where->next = newpart;
return newpart;
}
static void slang_text_add_string(struct slang_text *item, char *s)
{
Assert(item);
Assert(!item->string);
item->string = nmalloc(strlen(s) + 1);
strcpy(item->string, s);
}
static void slang_text_free(struct slang_text *item)
{
if (!item)
return;
slang_text_free(item->next);
if (item->string)
nfree(item->string);
nfree(item);
}
static int slang_text_expmem(struct slang_text *item)
{
int size = 0;
while (item) {
size += sizeof(struct slang_text);
if (item->string)
size += strlen(item->string) + 1;
item = item->next;
}
return size;
}
#ifndef SLANG_NOTYPES
static int slang_text_strcasecmp(struct slang_text *item, char *text)
{
Assert(item);
debug2("s_t_sc: '%s', '%s'", text, item->string);
if (item->command || item->next)
return 1;
return strcasecmp(item->string, text);
}
#endif
static char slang_text_buf[500];
static char *slang_text_get(struct slang_text *item)
{
slang_text_buf[0] = 0;
while (item) {
if (item->string)
strncat(slang_text_buf, item->string, sizeof(slang_text_buf));
else if (item->command)
item->command();
item = item->next;
}
return slang_text_buf;
}
/*****************************************************/
static struct slang_command_list *glob_slang_cmd_list;
static struct slang_command_list *slang_commands_list_add(struct slang_command_list *where, struct slang_text_commands *what)
{
struct slang_command_list *newcommandlist;
newcommandlist = nmalloc(sizeof(struct slang_command_list));
newcommandlist->commands = what;
newcommandlist->next = where;
return newcommandlist;
}
static int slang_commands_list_expmem(struct slang_command_list *what)
{
int size = 0;
while (what) {
size += sizeof(struct slang_command_list);
what = what->next;
}
return size;
}
static void slang_commands_list_free(struct slang_command_list *what)
{
struct slang_command_list *next;
while (what) {
next = what->next;
nfree(what);
what = next;
}
}
static void slang_text_add_command(struct slang_text *item, char *s)
{
struct slang_command_list *cmdlist;
char *cmd;
int i;
cmd = newsplit(&s);
i = 0;
for (cmdlist = glob_slang_cmd_list; cmdlist; cmdlist = cmdlist->next) {
for (i = 0; 1; i++) {
if (!cmdlist->commands[i].command)
break;
if (!strcasecmp(cmdlist->commands[i].command, cmd)) {
item->command = cmdlist->commands[i].targetfunc;
return;
}
}
}
putlog(LOG_MISC, "*", "ERROR! Unknown slang-command: '%s'", cmd);
}