File: autocomplete.c

package info (click to toggle)
f-irc 1.36-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 632 kB
  • ctags: 904
  • sloc: ansic: 12,538; makefile: 61
file content (218 lines) | stat: -rw-r--r-- 4,460 bytes parent folder | download | duplicates (2)
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
/* GPLv2 applies
 * SVN revision: $Revision: 716 $
 * (C) 2006-2014 by folkert@vanheusden.com
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gen.h"
#include "autocomplete.h"
#include "utils.h"
#include "term.h"
#include "buffer.h"
#include "channels.h"
#include "servers.h"
#include "loop.h"
#include "main.h"

tab_completion_t tab_completion[] = 
{
	{ "/ADDSERVER" },
	{ "/ADMIN" },
	{ "/AWAY" },
	{ "/BAN" },
	{ "/CTCP" },
	{ "/CONNECT" },
	{ "/DCCSEND" },
	{ "/EXIT" },
	{ "/IGNORE" },
	{ "/INFO" },
	{ "/INVITE" },
	{ "/ISON" },
	{ "/JOIN" },
	{ "/KEEPTOPIC" },
	{ "/KICK" },
	{ "/KILL" },
	{ "/LEAVE" },
	{ "/LINKS" },
	{ "/LIST" },
	{ "/MODE" },
	{ "/MSG" },
	{ "/NAMES" },
	{ "/NICK" },
	{ "/NOTICE" },
	{ "/OPER" },
	{ "/PART" },
	{ "/PASS" },
	{ "/PING" },
	{ "/PRIVMSG" },
	{ "/QUIT" },
	{ "/QUOTE" },
	{ "/RAW" },
	{ "/REHASH" },
	{ "/RESTART" },
	{ "/SAVECONFIG" },
	{ "/SEARCHALL" },
	{ "/SPAM" },
	{ "/STATS" },
	{ "/SUMMON" },
	{ "/TIME" },
	{ "/TOPIC" },
	{ "/TRACE" },
	{ "/UNIGNORE" },
	{ "/USER" },
	{ "/USERHOST" },
	{ "/USERS" },
	{ "/VERSION" },
	{ "/WALLOPS" },
	{ "/WHO" },
	{ "/WHOIS" },
	{ "/WHOWAS" },
	{ NULL }
};

char *make_complete_command(const char *in)
{
	int n_elements_equal = 0;
	char *matching_str = NULL, *dummy = NULL;
	int matching_str_len = 32767;
	int index = 0;
	int in_len = strlen(in);
	int matching_index = -1;
	char str_buffer[4096] = { 0 };
	unsigned int nb = 0;

	while(tab_completion[index].what)
	{
		if (strncasecmp(tab_completion[index].what, in, in_len) == 0)
		{
			if (matching_str)
			{
				int loop;
				int cur_len = strlen(tab_completion[index].what);

				for(loop=in_len; loop<min(matching_str_len, cur_len); loop++)
				{
					if (tab_completion[index].what[loop] != matching_str[loop])
						break;
				}

				matching_str[loop] = 0x00;
				matching_str_len = loop;
				matching_index = -1;	/* no longer completely matching */
			}
			else
			{
				matching_index = index;
				matching_str = strdup(tab_completion[index].what);
				matching_str_len = strlen(matching_str);
			}

			nb += snprintf(&str_buffer[nb], sizeof str_buffer - nb, "%s ", tab_completion[index].what);
			n_elements_equal++;

			if (nb >= sizeof str_buffer - 1)
				break;
		}

		index++;
	}

	if (n_elements_equal > 1)
		update_statusline(current_server, current_server_channel_nr, "Matching: %s", str_buffer);

	if (matching_index != -1)
	{
		asprintf(&dummy, "%s ", matching_str);

		myfree(matching_str);
		matching_str = dummy;
	}

	return matching_str;
}

char *make_complete_nickorchannel(char *in, int start_of_line)
{
	int n_elements_equal = 0;
	char *matching_str = NULL, *dummy = NULL;
	int matching_str_len = 32767;
	int index = 0;
	int in_len = strlen(in);
	int matching_index = -1;
	int cur_n_channels = cur_server() -> n_channels;
	int cur_n_names = cur_channel() -> n_names;
	int matching_type = -1;
	char str_buffer[4096] = { 0 };
	unsigned int nb = 0;

	for(index=0; index<(cur_n_channels + cur_n_names); index++)
	{
		char *compare_str;
		int type;

		if (index >= cur_n_channels)
		{
			char *dummy = cur_channel() -> persons[index - cur_n_channels].nick;
			if (dummy[0] == '@')
				compare_str = &dummy[1];
			else
				compare_str = dummy;
			type = 1; /* nick */
		}
		else
		{
			compare_str = cur_server() -> pchannels[index].channel_name;
			type = 0; /* channel */
		}

		if (strncasecmp(compare_str, in, in_len) == 0)
		{
			if (matching_str)
			{
				int loop;
				int cur_len = strlen(compare_str);

				for(loop=in_len; loop<min(matching_str_len, cur_len); loop++)
				{
					if (compare_str[loop] != matching_str[loop])
						break;
				}
				matching_str[loop] = 0x00;
				matching_str_len = loop;
				matching_index = -1;	/* no longer completely matching */
			}
			else
			{
				matching_index = index;
				matching_type = type;
				matching_str = strdup(compare_str);
				matching_str_len = strlen(matching_str);
			}

			nb += snprintf(&str_buffer[nb], sizeof str_buffer - nb, "%s ", compare_str);
			n_elements_equal++;

			if (nb >= sizeof str_buffer - 1)
				break;
		}
	}

	if (n_elements_equal > 1)
		update_statusline(current_server, current_server_channel_nr, "Matching: %s", str_buffer);

	if (matching_index != -1)
	{
		if (matching_type == 1 && start_of_line)
			asprintf(&dummy, "%s: ", matching_str);
		else
			asprintf(&dummy, "%s ", matching_str);

		myfree(matching_str);
		matching_str = dummy;
	}

	return matching_str;
}