File: stack.c

package info (click to toggle)
ircii 20060725-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 5,648 kB
  • ctags: 3,794
  • sloc: ansic: 44,334; makefile: 1,091; sh: 524; perl: 127
file content (393 lines) | stat: -rw-r--r-- 8,311 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * stack.c - does the handling of stack functions
 *
 * written by matthew green
 *
 * Copyright (c) 1993-2006 Matthew R. Green.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "irc.h"
IRCII_RCSID("@(#)$eterna: stack.c,v 1.37 2006/07/22 03:50:10 mrg Exp $");

#include "stack.h"
#include "window.h"
#include "hook.h"
#include "ircaux.h"
#include "output.h"
#include "list.h"

static	OnStack	*on_stack = NULL;
static  AliasStack *alias_stack = NULL;
static	AliasStack *assign_stack = NULL;
static	SetStack *set_stack = NULL;

static	AliasStack *alias_get(u_char *, int);
static	AliasStack *alias_stack_find(u_char *, int);
static	void	alias_stack_add(AliasStack *, int);
static	void	do_stack_on(int, u_char *);
static	void	do_stack_alias(int, u_char *, int);
static	void	do_stack_set(int, u_char *);

static	void
do_stack_on(type, args)
	int	type;
	u_char	*args;
{
	u_char	foo[7];
	int	cnt, i, which = 0;
	size_t	len;
	Hook	*list;
	NumericList	*nhook,
			*nptr,
			*ntmp = NULL;

	if (!on_stack && (type == STACK_POP || type == STACK_LIST))
	{
		say("ON stack is empty!");
		return;
	}
	if (!args || !*args)
	{
		say("Missing event type for STACK ON");
		return;
	}
	len = my_strlen(args);
	for (cnt = 0, i = 0; i < NUMBER_OF_LISTS; i++)
	{
		if (!my_strnicmp(args, UP(hook_functions[i].name), len))
		{
			if (my_strlen(hook_functions[i].name) == len)
			{
				cnt = 1;
				which = i;
				break;
			}
			else
			{
				cnt++;
				which = i;
			}
		}
		else if (cnt)
			break;
	}
	if (!cnt)
	{
		if (is_number(args))
		{
			which = my_atoi(args);
			if (which < 1 || which > 999)
			{
				say("Numerics must be between 001 and 999");
				return;
			}
			which = -which;
		}
		else
		{
			say("No such ON function: %s", args);
			return;
		}
	}
	if (which < 0)
	{
		/* XXX: check cnt != 1 */
		snprintf(CP(foo), sizeof foo, "%3.3u", -which);
		if ((nhook = (NumericList *) find_in_list((List **)(void *)&numeric_list, foo, 0)) != NULL)
			list = nhook->list;
		else
			list = NULL;
	}
	else
		list = hook_functions[which].list;
	if (type == STACK_PUSH)
	{
		OnStack	*new;

		if (list == NULL)
		{
			say("The ON %s list is empty", args);
			return;
		}
		new = (OnStack *) new_malloc(sizeof(OnStack));
		new->next = on_stack;
		on_stack = new;
		new->which = which;
		new->list = list;
		if (which < 0)
		{
			if (nhook == numeric_list)
			{
				numeric_list = nhook->next;
				new_free(&nhook->name);
				new_free(&nhook);
				return;
			}
			for (nptr = numeric_list; nptr;
					ntmp = nptr, nptr = nptr->next)
			{
				if (nptr == nhook)
				{
					ntmp->next = nptr->next;
					new_free(&nptr->name);
					new_free(&nptr);
					return;
				}
			}
		}
		else
			hook_functions[which].list = NULL;
		return;
	}
	else if (type == STACK_POP)
	{
		OnStack	*p,
			*tmp = NULL;

		for (p = on_stack; p; tmp = p, p = p->next)
		{
			if (p->which == which)
			{
				if (p == on_stack)
					on_stack = p->next;
				else
					tmp->next = p->next;
				break;
			}
		}
		if (!p)
		{
			say("No %s on the stack", args);
			return;
		}
		if ((which < 0 && nhook) || hook_functions[which].list)
			remove_hook(which, NULL, 0, 0, 1);	/* free hooks */
		if (which < 0)
		{
			if ((nptr = (NumericList *) find_in_list((List **)(void *)&numeric_list, foo, 0)) == NULL)
			{
				nptr = (NumericList *) new_malloc(sizeof(NumericList));
				nptr->name = NULL;
				nptr->list = p->list;
				malloc_strcpy(&nptr->name, foo);
				add_to_list((List **)(void *)&numeric_list, (List *) nptr);
			}
			else
				add_to_list((List **)(void *)&numeric_list->list, (List *) p->list);
		}
		else
			hook_functions[which].list = p->list;
		return;
	}
	else if (type == STACK_LIST)
	{
		int	slevel = 0;
		OnStack	*osptr;

		for (osptr = on_stack; osptr; osptr = osptr->next)
			if (osptr->which == which)
			{
				Hook	*hptr;

				slevel++;
				say("Level %d stack", slevel);
				for (hptr = osptr->list; hptr; hptr = hptr->next)
					show_hook(hptr, args);
			}

		if (!slevel)
			say("The STACK ON %s list is empty", args);
		return;
	}
	say("Unknown STACK ON type ??");
}

static	void
do_stack_alias(type, args, which)
	int	type;
	u_char	*args;
	int	which;
{
	char	*name;
	AliasStack	*aptr,
			**aptrptr;

	if (which == STACK_DO_ALIAS)
	{
		name = "ALIAS";
		aptrptr = &alias_stack;
	}
	else
	{
		name = "ASSIGN";
		aptrptr = &assign_stack;
	}
	if (!*aptrptr && (type == STACK_POP || type == STACK_LIST))
	{
		say("%s stack is empty!", name);
		return;
	}

	if (STACK_PUSH == type)
	{
		aptr = alias_get(args, which);
		if ((AliasStack *) 0 == aptr)
		{
			say("No such %s %s", name, args);
			return;
		}
		if (aptrptr)
			aptr->next = *aptrptr;
		*aptrptr = aptr;
		return;
	}
	if (STACK_POP == type)
	{
		aptr = alias_stack_find(args, which);
		if ((AliasStack *) 0 == aptr)
		{
			say("%s is not on the %s stack!", args, name);
			return;
		}
		alias_stack_add(aptr, which);
		return;
	}
	if (STACK_LIST == type)
	{
		say("stack list is not implimented yet");
		return;
	}
	say("Unknown STACK type ??");
}

static	void
do_stack_set(type, args)
	int	type;
	u_char	*args;
{
	SetStack *tmp = set_stack;

	tmp++;
}

/*
 * alias_get: this returns a point to an `AliasStack' structure that
 * has be extracted from the current aliases, and removed from that
 * list.
 */
static	AliasStack* alias_get(args, which)
	u_char	*args;
	int	which;
{
	return (AliasStack *) 0;
}

/*
 * alias_stack_find: this returns the pointer to the struct with the
 * most recent alias for `args' in the stack.
 */
static	AliasStack*
alias_stack_find(args, which)
	u_char	*args;
	int	which;
{
	return (AliasStack *) 0;
}

/*
 * alias_stack_add: this adds `aptr' to the alias/assign stack.
 */
static	void
alias_stack_add(aptr, which)
	AliasStack *aptr;
	int which;
{
	return;
}

void
stackcmd(command, args, subargs)
	u_char	*command,
		*args,
		*subargs;
{
	u_char	*arg,
		*cmd = NULL;
	int	type;
	size_t	len;

	if ((arg = next_arg(args, &args)) != NULL)
	{
		len = my_strlen(arg);
		malloc_strcpy(&cmd, arg);
		upper(cmd);
		if (!my_strncmp(cmd, "PUSH", len))
			type = STACK_PUSH;
		else if (!my_strncmp(cmd, "POP", len))
			type = STACK_POP;
		else if (!my_strncmp(cmd, "LIST", len))
			type = STACK_LIST;
		else
		{
			say("%s is unknown stack type", arg);
			new_free(&cmd);
			return;
		}
		new_free(&cmd);
	}
	else
	{
		say("Need operation for STACK");
		return;
	}
	if ((arg = next_arg(args, &args)) != NULL)
	{
		len = my_strlen(arg);
		malloc_strcpy(&cmd, arg);
		upper(cmd);
		if (!my_strncmp(cmd, "ON", len))
			do_stack_on(type, args);
		else if (!my_strncmp(cmd, "ALIAS", len))
			do_stack_alias(type, args, STACK_DO_ALIAS);
		else if (!my_strncmp(cmd, "ASSIGN", len))
			do_stack_alias(type, args, STACK_DO_ASSIGN);
		else if (!my_strncmp(cmd, "SET", len))
			do_stack_set(type, args);
		else
		{
			say("%s is not a valid STACK type");
			new_free(&cmd);
			return;
		}
		new_free(&cmd);
	}
	else
	{
		say("Need stack type for STACK");
		return;
	}
}