Debian Codegraph @

terps/jacl/encapsulate.c

raw symbols SCIP
1 /* encapsulate.c --- Command chopping function.
2 * (C) 2008 Stuart Allen, distribute and use
3 * according to GNU GPL, see file COPYING for details.
4 */
5
6 #include "jacl.h"
7 #include "types.h"
8 #include "prototypes.h"
9 #include "encapsulate.h"
10 #include <string.h>
11
12 extern struct synonym_type *synonym_table;
13 extern struct filter_type *filter_table;
14
15 char text_buffer[1024];
16
17 /* THIS IS A STRING CONSTANT TO POINT TO WHENEVER A COMMA IS
18 * USED IN THE PLAYER'S INPUT */
19 const char *comma = "comma\0";
20 const char *then = "then\0";
21 const char *word[MAX_WORDS];
22 int quoted[MAX_WORDS];
23 int percented[MAX_WORDS];
24 int wp;
25
26 void
27 encapsulate()
28 {
29 int index,
30 length;
31 int position = 0;
32 int new_word = TRUE;
33
34 length = strlen(text_buffer);
35
36 /* QUOTED IS USED TO STORE WHETHER EACH WORD WAS ENCLOSED IN QUOTES
37 * IN THE PLAYERS COMMAND - RESET EACH WORD TO NO */
38 for (index = 0; index < MAX_WORDS; index++) {
39 quoted[index] = 0;
40 percented[index] = 0;
41 }
42
43 for (index = 0; index < length; index++) {
44
45 switch (text_buffer[index]) {
46 case ':':
47 case '\t':
48 case ' ':
49 case ',':
50 text_buffer[index] = 0;
51 new_word = TRUE;
52 break;
53 case ';':
54 case '#':
55 case '\r':
56 case '\n':
57 /* TERMINATE THE WHOLE COMMAND ON HITTING A NEWLINE CHARACTER, A
58 * SEMICOLON OR A HASH */
59 text_buffer[index] = 0;
60 index = length;
61 break;
62 case '"':
63 index++;
64 /* NEED TO REMEMBER THAT THIS WORD WAS ENCLOSED IN QUOTES FOR
65 * THE COMMAND 'write'*/
66 quoted[position] = 1;
67
68 word[position] = &text_buffer[index];
69
70 if (position < MAX_WORDS)
71 position++;
72
73 /* IF A WORD IS ENCLOSED IN QUOTES, KEEP GOING UNTIL THE END
74 * OF THE LINE OR A CLOSING QUOTE IS FOUND, NOT BREAKING AT
75 * WHITESPACE AS USUAL */
76 for (; index < length; index++) {
77 if (text_buffer[index] == '"') {
78 text_buffer[index] = 0;
79 new_word = TRUE;
80 break;
81 } else if (text_buffer[index] == '\r' || text_buffer[index] == '\n') {
82 text_buffer[index] = 0;
83 index = length;
84 break;
85 }
86 }
87 break;
88 default:
89 if (new_word) {
90 if (text_buffer[index] == '%' && text_buffer[index+1] != ' ' && text_buffer[index+1] != '\t') {
91 percented[position]++;
92 break;
93 }
94 word[position] = &text_buffer[index];
95 new_word = FALSE;
96 if (position < MAX_WORDS)
97 position++;
98 }
99 break;
100 }
101
102 }
103
104 /* NULL OUT ALL THE WORD POINTERS BEYOND THE LAST WORD */
105 for (index = position; index < MAX_WORDS; index++)
106 word[index] = NULL;
107
108 wp = 0;
109 }
110
111 // THIS VERSION OF ENCAPSULATE DOESN'T LOOK FOR CERTAIN SPECIAL CHARACTERS
112 void
113 command_encapsulate()
114 {
115 int index,
116 length;
117 int position = 0;
118 int new_word = TRUE;
119
120 length = strlen(text_buffer);
121
122 // QUOTED IS USED TO STORE WHETHER EACH WORD WAS ENCLOSED IN QUOTES
123 // IN THE PLAYERS COMMAND - RESET EACH WORD TO NO
124 for (index = 0; index < MAX_WORDS; index++) {
125 quoted[index] = 0;
126 }
127
128 for (index = 0; index < length; index++) {
129
130 // REDUSE EVERYTHING TO LOWER CASE EXCEPT TEXT ENCLOSED IN QUOTES
131 text_buffer[index] = tolower((int) text_buffer[index]);
132
133 switch (text_buffer[index]) {
134 case ':':
135 case '\t':
136 case ' ':
137 text_buffer[index] = 0;
138 new_word = TRUE;
139 break;
140 case ',':
141 text_buffer[index] = 0;
142 // SET THIS WORD TO POINT TO A STRING CONSTANT OF 'comma' AS THE
143 // COMMA ITSELF WILL BE NULLED OUT TO TERMINATE THE PRECEEDING
144 // WORD IN THE COMMAND.
145 word[position] = comma;
146 if (position < MAX_WORDS)
147 position++;
148 new_word = TRUE;
149 break;
150 case '.':
151 text_buffer[index] = 0;
152 // SET THIS WORD TO POINT TO A STRING CONSTANT OF 'comma' AS THE
153 // COMMA ITSELF WILL BE NULLED OUT TO TERMINATE THE PRECEEDING
154 // WORD IN THE COMMAND
155 word[position] = then;
156 if (position < MAX_WORDS)
157 position++;
158 new_word = TRUE;
159 break;
160 case ';':
161 case '\r':
162 case '\n':
163 // TERMINATE THE WHOLE COMMAND ON HITTING A NEWLINE CHARACTER, A
164 // SEMICOLON OR A HASH
165 text_buffer[index] = 0;
166 index = length;
167 break;
168 case '"':
169 index++;
170 // NEED TO REMEMBER THAT THIS WORD WAS ENCLOSED IN QUOTES FOR
171 // THE COMMAND 'write'
172 quoted[position] = 1;
173
174 word[position] = &text_buffer[index];
175
176 if (position < MAX_WORDS)
177 position++;
178
179 // IF A WORD IS ENCLOSED IN QUOTES, KEEP GOING UNTIL THE END
180 // OF THE LINE OR A CLOSING QUOTE IS FOUND, NOT BREAKING AT
181 // WHITESPACE AS USUAL
182 for (; index < length; index++) {
183 if (text_buffer[index] == '"') {
184 text_buffer[index] = 0;
185 new_word = TRUE;
186 break;
187 } else if (text_buffer[index] == '\r' || text_buffer[index] == '\n') {
188 text_buffer[index] = 0;
189 index = length;
190 break;
191 }
192 }
193 break;
194 default:
195 if (new_word) {
196 word[position] = &text_buffer[index];
197 new_word = FALSE;
198 if (position < MAX_WORDS)
199 position++;
200 }
201 break;
202 }
203
204 }
205
206 // NULL OUT ALL THE WORD POINTERS BEYOND THE LAST WORD
207 for (index = position; index < MAX_WORDS; index++) {
208 word[index] = NULL;
209 }
210
211 wp = 0;
212 }
213
214 void
215 jacl_truncate()
216 {
217 int index,
218 counter,
219 match;
220 int position = 0;
221
222 struct synonym_type *synonym;
223 struct filter_type *filter = filter_table;
224
225 // REMOVE ALL THE DEFINED 'filter's FROM THE PLAYER'S COMMAND
226 if (filter != NULL) {
227 while (word[position] != NULL) {
228 match = FALSE;
229 do {
230 if (!strcmp(word[position], filter->word)) {
231 for (index = position; word[index + 1] != NULL;
232 index++)
233 word[index] = word[index + 1];
234 word[index] = NULL;
235 match = TRUE;
236 }
237 filter = filter->next_filter;
238 }
239 while (filter != NULL && !match);
240 filter = filter_table;
241 if (!match)
242 position++;
243 };
244 }
245
246 // SUBTITUTE ALL THE DEFINED 'synonym's IN THE PLAYER'S COMMAND
247 if (synonym_table != NULL) {
248 for (counter = 0; word[counter] != NULL; counter++) {
249 synonym = synonym_table;
250 do {
251 if (!strcmp(word[counter], synonym->original)) {
252 word[counter] = synonym->standard;
253 break;
254 }
255 if (synonym->next_synonym != NULL)
256 synonym = synonym->next_synonym;
257 else
258 break;
259 }
260 while (TRUE);
261 }
262 }
263 }