-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
483 lines (433 loc) · 13.9 KB
/
parse.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* parse.c */
/*
* SklaffKOM, a simple conference system for UNIX.
*
* Copyright (C) 1993-1994 Torbj|rn B}}th, Peter Forsberg, Peter Lindberg,
* Odd Petersson, Carl Sundbom
*
* Program dedicated to the memory of Staffan Bergstr|m.
*
* For questions about this program, mail sklaff@skom.se
*
* 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <nlist.h>
#include "sklaff.h"
#include "ext_globals.h"
struct hitlist {
int wanted;
int matched_wanted;
int matched_found;
};
int find_max_match(struct hitlist * hl);
/*
* parse - parse user input and return pointer to function
* args: pointer to user input (buf), pointer to argument returned (args)
* ret: pointer to function or NULL
*/
int (*
parse(char *buf, char *args)) (void)
{
struct hitlist hl[MAX_COMMANDS];
int i, found, full_hit, arg_hit, part_hit, max_words, full_ind, arg_ind, part_ind;
if (atol(buf)) {
sprintf(buf, "%s %ld", MSG_TEXTPROMPT2, atol(buf));
}
bzero(args, LINE_LEN);
i = 0;
found = wc(buf);
while (Par_ent[i].func[0] != '\0') {
hl[i].wanted = wc(Par_ent[i].cmd);
hl[i].matched_wanted = parse_strings(Par_ent[i].cmd, buf,
hl[i].wanted, NULL);
hl[i].matched_found = parse_strings(Par_ent[i].cmd, buf,
found, NULL);
i++;
}
/* DBG output("Found: %d\n", found); */
/* DBG output(" Kommando Wanted M-wanted M-found\n"); */
/* DBG i = 0; while (Par_ent[i].func[0] != '\0') { */
/* DBG output("%2d %-20.20s %6d %8d %7d\n",i,Par_ent[i].cmd,
* hl[i].wanted, */
/* DBG hl[i].matched_wanted, hl[i].matched_found); i++; } */
i = 0;
full_hit = 0;
arg_hit = 0;
part_hit = 0;
while (Par_ent[i].func[0] != '\0') {
if ((hl[i].matched_wanted == hl[i].wanted) &&
(hl[i].matched_found == found)) { /* A Roxette hit */
full_hit++;
full_ind = i;
} else if ((hl[i].matched_wanted == hl[i].wanted) &&
(hl[i].matched_found < found)) {
arg_hit++;
arg_ind = i;
} else if ((hl[i].matched_wanted > 0) &&
(hl[i].matched_wanted < hl[i].wanted)) {
part_hit++;
part_ind = i;
}
i++;
}
/* DBG output("full_hit: %d arg_hit: %d part_hit: %d\n", */
/* DBG full_hit, arg_hit, part_hit); */
if (full_hit == 1) {
/* DBG output("cmd: [%s] func: [%s] addr: [%ld]\n", */
/* DBG Par_ent[full_ind].cmd, Par_ent[full_ind].func, */
/* DBG Par_ent[full_ind].addr); */
return Par_ent[full_ind].addr;
} else if (full_hit > 1) {
max_words = find_max_match(hl);
/* DBG output("max_words: %d\n", max_words); */
output("\n%s \"%s\". %s\n", MSG_MULTICOM, buf, MSG_CHOSECOM);
i = 0;
while (Par_ent[i].func[0] != '\0') {
if (parse_strings(Par_ent[i].cmd, buf, max_words,
NULL) == max_words) {
output("%s\n", Par_ent[i].cmd);
}
i++;
}
output("\n");
return (int (*) ()) 0;
} else if (arg_hit == 1) {
parse_strings(Par_ent[arg_ind].cmd, buf, hl[arg_ind].wanted,
args);
/* DBG* output("cmd: [%s] func: [%s] addr: [%ld] args:
* [%s]\n", Par_ent[arg_ind].cmd, Par_ent[arg_ind].func,
* Par_ent[arg_ind].addr, args); */
return Par_ent[arg_ind].addr;
} else if (arg_hit > 1) {
max_words = find_max_match(hl);
/* DBG* output("max_words: %d\n", max_words); */
i = 0;
arg_hit = 0;
while (Par_ent[i].func[0] != '\0') {
if (parse_strings(Par_ent[i].cmd, buf, max_words,
NULL) == max_words) {
arg_hit++;
arg_ind = i;
}
i++;
}
if (arg_hit == 1) {
parse_strings(Par_ent[arg_ind].cmd, buf,
hl[arg_ind].wanted, args);
/* DBG output("cmd: [%s] func: [%s]
* addr: [%ld] args: [%s]\n", */
/* DBG Par_ent[arg_ind].cmd, Par_ent[arg_ind].func, */
/* DBG Par_ent[arg_ind].addr, args); */
return Par_ent[arg_ind].addr;
} else {
max_words = find_max_match(hl);
/* DBG output("max_words: %d\n",
* max_words); */
output("\n%s \"%s\". %s\n", MSG_MULTICOM, buf, MSG_CHOSECOM);
i = 0;
while (Par_ent[i].func[0] != '\0') {
if (parse_strings(Par_ent[i].cmd, buf, max_words,
NULL) == max_words) {
output("%s\n", Par_ent[i].cmd);
}
i++;
}
output("\n");
return (int (*) ()) 0;
}
} else if (part_hit == 1) {
/* DBG output("cmd: [%s] func: [%s] addr: [%ld] args:
* [%s]\n", */
/* DBG Par_ent[part_ind].cmd, Par_ent[part_ind].func, */
/* DBG Par_ent[part_ind].addr, args); */
return Par_ent[part_ind].addr;
} else if ((full_hit > 1) || (arg_hit > 1) || (part_hit > 1)) {
max_words = find_max_match(hl);
/* DBG output("max_words: %d\n", max_words); */
output("\n%s \"%s\". %s\n", MSG_MULTICOM, buf, MSG_CHOSECOM);
i = 0;
while (Par_ent[i].func[0] != '\0') {
if (parse_strings(Par_ent[i].cmd, buf, max_words,
NULL) == max_words) {
output("%s\n", Par_ent[i].cmd);
}
i++;
}
output("\n");
return (int (*) ()) 0;
} else {
output("\n%s \"%s\". %s\n\n", MSG_COMERR, buf, MSG_HELPQ);
}
return (int (*) ()) 0;
}
/*
* find_max_match - find maximum number of words matched
* args: hitlist (hl)
* ret: number of words matched
*/
int
find_max_match(struct hitlist * hl)
{
int i = 0;
static int max_words;
max_words = 0;
while (Par_ent[i].func[0] != '\0') {
max_words = (max_words > hl[i].matched_wanted ?
max_words : hl[i].matched_wanted);
i++;
}
return max_words;
}
/*
* parse_init - initialize parser
* args: name of program (program_name)
* ret: number of commands or failure (-1)
*/
int
parse_init(char *program_name)
{
int i = 0, pf;
char *buf;
struct nlist commands[MAX_COMMANDS];
if ((pf = open_file(PARSE_FILE, OPEN_DEFAULT)) == -1) {
sys_error("parse_init", 1, "open_file");
return -1;
}
if ((buf = read_file(pf)) == NULL) {
sys_error("parse_init", 2, "read_file");
return -1;
}
close_file(pf);
while ((buf = get_parse_entry(buf, &Par_ent[i])) != NULL) {
commands[i].n_name = Par_ent[i].func;
i++;
}
commands[i].n_name = NULL;
/* This strange line of code is a result of a */
/* typedef/struct misfeature */
Par_ent[i].func[0] = '\0'; /* Ugly solution, huh?! */
nlist(program_name, commands);
i = 0;
while (Par_ent[i].func[0] != '\0') {
Par_ent[i].addr = (int (*) ()) commands[i].n_value;
if (Par_ent[i].addr == 0) {
buggy_sunOS_fix(i);
}
if (Par_ent[i].addr == 0) {
output("%s[%s #%d] %s(): %s\n", program_name,
"parse_init", 3, Par_ent[i].func,
"no such function");
return -1;
}
i++;
}
return i;
}
/* Because of a stupid bug, Sun OS 4.1.3. can't handle multiple queries for
the same symbol. Therefore we must fix it ourselves. */
/*
* buggy_sunOS_fix - fix bug in sunOS nlist system call
* args: indexnumber in Par_ent struct
*/
void
buggy_sunOS_fix(int i)
{
int j;
for (j = 0; j < i; j++) {
if (!strcmp(Par_ent[j].func, Par_ent[i].func)) {
Par_ent[i].addr = Par_ent[j].addr;
break;
}
}
}
#define EXPLIST_SIZE 20
struct expand_list {
LINE name; /* expanded name */
int type; /* type of name (USER, CONF, ...) */
};
/*
* invalid_name - outputs error message if expand_name failed
* args: type of message
*/
static void
invalid_name(int type)
{
switch (type) {
case CONF:
output(MSG_ECONFN);
break;
case USER:
output(MSG_EUSERN);
break;
case ACTIVE:
output(MSG_EUSERA);
break;
case SUBSCRIBED:
output(MSG_ECONFS);
break;
case UNSUBSCRIBED:
output(MSG_ECONFU);
break;
default:
output(MSG_ECOMN);
}
}
/*
* expand_name - expands user or conf. name...
* args: string to look for (name), type of object (type), don't
* output failures (quiet), pointer to int (expanded_type)
* ret: name expanded or NULL
*/
char *
expand_name(char *name, int type, int quiet, int *expanded_type)
{
int fd, i, overflow = 0, found = 0;
char *buf, *tmpbuf;
LINE tmp;
struct CONF_ENTRY ce;
struct USER_ENTRY ue;
static struct expand_list el[EXPLIST_SIZE];
if (!wc(name)) {
if (!quiet)
invalid_name(type);
return NULL;
}
if ((type & CONF) || (type & SUBSCRIBED) || (type & UNSUBSCRIBED)) {
if ((fd = open_file(CONF_FILE, 0)) == -1) {
sys_error("conf_name", 1, "open_file");
return NULL;
}
if ((tmpbuf = buf = read_file(fd)) == NULL) {
sys_error("conf_name", 2, "read_file");
return NULL;
}
if (close_file(fd) == -1) {
sys_error("conf_name", 3, "close_file");
return NULL;
}
while ((buf = get_conf_entry(buf, &ce)) && !overflow) {
if (cmp_strings(ce.name, name) && ((ce.creator != -1) || (type & ALSOERASED))) {
if ((type & CONF) ||
((type & SUBSCRIBED) && member_of(Uid, ce.num)) ||
((type & UNSUBSCRIBED) && (!member_of(Uid, ce.num)))) {
if (found < (EXPLIST_SIZE - 1)) {
if (can_see_conf(Uid, ce.num, ce.type, ce.creator)) {
strcpy(el[found].name, ce.name);
el[found].type = CONF;
found++;
}
} else {
overflow = 1;
}
}
}
}
free(tmpbuf);
if (!overflow) {
strcpy(tmp, MSG_MAILBOX);
if (cmp_strings(tmp, name)) {
if (found < (EXPLIST_SIZE - 1)) {
strcpy(el[found].name, MSG_MAILBOX);
el[found].type = CONF;
found++;
} else {
overflow = 1;
}
}
}
}
if (((type & USER) || (type & ACTIVE)) && !overflow) {
if ((fd = open_file(USER_FILE, 0)) == -1) {
sys_error("conf_name", 4, "open_file");
return NULL;
}
if ((tmpbuf = buf = read_file(fd)) == NULL) {
sys_error("conf_name", 5, "read_file");
return NULL;
}
if (close_file(fd) == -1) {
sys_error("conf_name", 6, "close_file");
return NULL;
}
while ((buf = get_user_entry(buf, &ue)) && !overflow) {
if ((type & USER) || ((type & ACTIVE) && user_is_active(ue.num))) {
if (cmp_strings(ue.name, name)) {
if (found < (EXPLIST_SIZE - 1)) {
strcpy(el[found].name, ue.name);
if (type & ACTIVE) {
el[found].type = ACTIVE;
} else {
el[found].type = USER;
}
found++;
} else {
overflow = 1;
}
}
}
}
free(tmpbuf);
}
if (expanded_type != NULL) {
*expanded_type = 0;
}
switch (found) {
case 0: /* no match */
if (!quiet) {
invalid_name(type);
}
return NULL;
case 1: /* one match */
if (expanded_type != NULL) {
*expanded_type = el[0].type;
}
return el[0].name;
default:
if (!quiet) {
output("\n%s\n", MSG_MULNAM);
for (i = 0; i < found; i++) {
if (output(" %2d. %s\n", i + 1, el[i].name) == -1) {
return NULL;
}
}
output("\n");
if (overflow) {
output("%s\n\n", MSG_MOREMUL);
}
output(MSG_PICKLIST);
input("", tmp, LINE_LEN, 0, 0, 0);
rtrim(tmp);
if (!strlen(tmp)) {
output("\n");
return NULL;
}
i = atoi(tmp);
if (!i) {
return expand_name(tmp, type, quiet, expanded_type);
} else {
if (i && (i <= found)) {
if (expanded_type != NULL) {
*expanded_type = el[i - 1].type;
}
return el[i - 1].name;
} else {
output("\n%s\n\n", MSG_BADPICK);
return NULL;
}
}
}
}
return 0;
}