forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
2057 lines (1790 loc) · 51.4 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Pattern Parser package for LPmud
Ver 3.1
If you have questions or complaints about this code please refer them
*/
#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "config.h"
#include "lint.h"
#include "mstring.h"
#include "interpret.h"
#include "object.h"
#include "exec.h"
#include "simulate.h"
#include "mapping.h"
#include "inline_svalue.h"
extern int d_flag; /* for debugging purposes */
extern struct object *previous_ob;
struct object *vbfc_object;
#ifndef tolower /* On some systems this is a function */
extern int tolower (int);
#endif
/*****************************************************
This is the parser used by the efun parse_command
*/
/*
General documentation:
parse_command() is one of the most complex efun in LPmud to use. It takes
some effort to learn and use, but when mastered, very powerful constructs
can be implemented.
Basically parse_command() is a hotted sscanf operating on word basis. It
works similar to sscanf in that it takes a pattern and a variable set of
destination arguments. It is together with sscanf the only efun to use
pass by reference for other variables than arrays.
To make the efun useful it must have a certain support from the mudlib,
there is a set of functions that it needs to call to get relevant
information before it can parse in a sensible manner.
In earlier versions it used the normal id() lfun in the LPC objects to
find out if a given object was identified by a certain string. This was
highly inefficient as it could result in hundreds or maybe thousands of
calls when very long commands were parsed.
The new version relies on the LPC objects to give it three lists of 'names'.
1 - The normal singular names.
2 - The plural forms of the names.
3 - The acknowledged adjectives of the object.
These are fetched by calls to the functions:
1 - string *parse_command_id_list();
2 - string *parse_command_plural_id_list();
3 - string *parse_command_adjectiv_id_list();
The only really needed list is the first. If the second does not exist
than the efun will try to create one from the singular list. For
grammatical reasons it does not always succeed in a perfect way. This is
especially true when the 'names' are not single words but phrases.
The third is very nice to have because it makes constructs like
'get all the little blue ones' possible.
Apart from these functions that should exist in all objects, and which
are therefore best put in /std/object.c there is also a set of functions
needed in /secure/master.c These are not absolutely necessary but they
give extra power to the efun.
Basically these /secure/master.c lfuns are there to give default values
for the lists of names fetched from each object.
The names in these lists are applicable to any and all objects, the first
three are identical to the lfun's in the objects:
string *parse_command_id_list()
- Would normally return: ({ "one", "thing" })
string *parse_command_plural_id_list()
- Would normally return: ({ "ones", "things", "them" })
string *parse_command_adjectiv_id_list()
- Would normally return ({ "iffish" })
The last two are the default list of the prepositions and a single so called
'all' word.
string *parse_command_prepos_list()
- Would normally return: ({ "in", "on", "under" })
string parse_command_all_word()
- Would normally return: "all"
IF you want to use a different language than English but still want the
default pluralform maker to work, you need to replace parse.c with the
following file:
#if 0
* Language configured parse.c
*
#define PARSE_FOREIGN
char *parse_to_plural(str)
char *str;
{
* Your own plural converter for your language *
}
* The numberwords below should be replaced for the new language *
static char *ord1[] = {"", "first", "second", "third", "fourth", "fifth",
"sixth", "seventh", "eighth", "ninth", "tenth",
"eleventh", "twelfth", "thirteenth", "fourteenth",
"fifteenth", "sixteenth", "seventeenth",
"eighteenth","nineteenth"};
static char *ord10[] = {"", "", "twenty","thirty","forty","fifty","sixty",
"seventy", "eighty","ninety"};
static char *sord10[] = {"", "", "twentieth", "thirtieth", "fortieth",
"fiftieth", "sixtieth","seventieth", "eightieth",
"ninetieth"};
static char *num1[] = {"", "one","two","three","four","five","six",
"seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen",
"sixteen", "seventeen","eighteen","nineteen"};
static char *num10[] = {"", "", "twenty","thirty","forty","fifty","sixty",
"seventy", "eighty","ninety"};
#include "parse_english.c" * This parse.c file *
#endif
When all these things are defined parse_command() works best and most
efficient. What follows is the docs for how to use it from LPC:
Doc for LPC function
int parse_command(string, object/object*, string, destargs...)
Returns 1 if pattern matches
string Given command
object* if arr
object array holding the accessible objects
if ob
object from which to recurse and create
the list of accessible objects, normally
ob = environment(this_player())
string Parsepattern as list of words and formats:
Example string = " 'get' / 'take' %i "
Syntax:
'word' obligatory text
[word] optional text
/ Alternative marker
%o Single item, object
%l Living objects
%s Any text
%w Any word
%p One of a list (prepositions)
%i Any items
%d Number 0- or tx(0-99)
destargs This is the list of result variables as in sscanf
One variable is needed for each %_
The return types of different %_ is:
%o Returns an object
%s Returns a string of words
%w Returns a string of one word
%p Can on entry hold a list of word in array
or an empty variable
Returns:
if empty variable: a string
if array: array[0]=matched word
%i Returns a special array on the form:
[0] = (int) +(wanted) -(order) 0(all)
[1..n] (object) Objectpointers
%l Returns a special array on the form:
[0] = (int) +(wanted) -(order) 0(all)
[1..n] (object) Objectpointers
These are only living objects.
%d Returns a number
The only types of % that uses all the loaded information from the objects
are %i and %l. These are in fact identical except that %l filters out
all nonliving objects from the list of objects before trying to parse.
The return values of %i and %l is also the most complex. They return an
array consisting of first a number and then all possible objects matching.
As the typical string matched by %i/%l looks like: 'three red roses',
'all nasty bugs' or 'second blue sword' the number indicates which
of these numerical constructs was matched:
if numeral >0 then three, four, five etc were matched
if numeral <0 then second, twentyfirst etc were matched
if numeral==0 then 'all' or a generic plural form such as 'apples'
were matched.
NOTE!
The efun makes no semantic implication on the given numeral. It does
not matter if 'all apples' or 'second apple' is given. A %i will
return ALL possible objects matching in the array. It is up to the
caller to decide what 'second' means in a given context.
Also when given an object and not an explicit array of objects the
entire recursive inventory of the given object is searched. It is up
to the caller to decide which of the objects are actually visible
meaning that 'second' might not at all mean the second object in
the returned array of objects.
Example:
if (parse_command("spray car",environment(this_player()),
" 'spray' / 'paint' [paint] %i ",items))
{
If the pattern matched then items holds a return array as described
under 'destargs' %i above.
}
BUGS / Features
:
Patterns of type: "%s %w %i"
Might not work as one would expect. %w will always succeed so the arg
corresponding to %s will always be empty.
Patterns of the type: 'word' and [word]
The 'word' can not contain spaces. It must be a single word. This is so
because the pattern is exploded on " " (space) and a pattern element can
therefore not contain spaces.
This will be fixed in the future
*/
/* Some useful string macros
*/
#define EQ(x,y) (strcmp(x,y)==0)
/* Function in LPC which returns a list of ids
*/
#define QGET_ID "parse_command_id_list"
#define M_QGET_ID M_PARSE_COMMAND_ID_LIST
/* Function in LPC which returns a list of plural ids
*/
#define QGET_PLURID "parse_command_plural_id_list"
#define M_QGET_PLURID M_PARSE_COMMAND_PLURAL_ID_LIST
/* Function in LPC which returns a list of adjectiv ids
*/
#define QGET_ADJID "parse_command_adjectiv_id_list"
#define M_QGET_ADJID M_PARSE_COMMAND_ADJECTIV_ID_LIST
/* Function in LPC which returns a list of prepositions
*/
#define QGET_PREPOS "parse_command_prepos_list"
#define M_QGET_PREPOS M_PARSE_COMMAND_PREPOS_LIST
/* Function in LPC which returns the 'all' word
*/
#define QGET_ALLWORD "parse_command_all_word"
#define M_QGET_ALLWORD M_PARSE_COMMAND_ALL_WORD
/* Global vectors for 'caching' of ids
The main 'parse' routine stores these on call, making the entire
parse_command() reentrant.
*/
static struct vector *gId_list = 0;
static struct vector *gPluid_list = 0;
static struct vector *gAdjid_list = 0;
static struct vector *gId_list_d = 0; /* From master */
static struct vector *gPluid_list_d = 0; /* From master */
static struct vector *gAdjid_list_d = 0; /* From master */
static struct vector *gPrepos_list = 0; /* From master */
static char *gAllword = 0; /* From master */
/*
* Master has been (re)loaded; fetch params needed for parsing
*/
void
load_parse_information()
{
struct svalue *pval;
free_parse_information();
/* Get the default ids of 'general references' from master object
*/
pval = apply_master_ob(M_QGET_ID, 0);
if (pval && pval->type == T_POINTER)
{
gId_list_d = pval->u.vec;
INCREF(pval->u.vec->ref); /* Otherwise next sapply will free it */
}
else
gId_list_d = 0;
pval = apply_master_ob(M_QGET_PLURID,0);
if (pval && pval->type == T_POINTER)
{
gPluid_list_d = pval->u.vec;
INCREF(pval->u.vec->ref); /* Otherwise next sapply will free it */
}
else
gPluid_list_d = 0;
pval = apply_master_ob(M_QGET_ADJID, 0);
if (pval && pval->type == T_POINTER)
{
gAdjid_list_d = pval->u.vec;
INCREF(pval->u.vec->ref); /* Otherwise next sapply will free it */
}
else
gAdjid_list_d = 0;
pval = apply_master_ob(M_QGET_PREPOS,0);
if (pval && pval->type == T_POINTER)
{
gPrepos_list = pval->u.vec;
INCREF(pval->u.vec->ref); /* Otherwise next sapply will free it */
}
else
gPrepos_list = 0;
pval = apply_master_ob(M_QGET_ALLWORD,0);
if (pval && pval->type == T_STRING)
gAllword = string_copy(pval->u.string);
else
gAllword = 0;
}
/*
* Function name: load_lpc_info
* Description: Loads relevant information from a given object.
* This is the ids, plural ids and adjectiv ids. This
* is the only calls to LPC objects other than the
* master object that occur within the efun
* parse_command().
* Arguments: ix: Index in the array
* ob: The object to call for information.
*/
void
load_lpc_info(int ix, struct object *ob)
{
struct vector *tmp, *sing;
struct svalue sval, *ret;
int il, make_plural = 0;
char *str;
char *parse_to_plural(char *);
if (!ob)
return;
if (gPluid_list &&
gPluid_list->size > ix &&
gPluid_list->item[ix].type == T_NUMBER &&
gPluid_list->item[ix].u.number == 0)
{
ret = apply(QGET_PLURID, ob, 0, 1);
if (ret && ret->type == T_POINTER)
assign_svalue_no_free(&gPluid_list->item[ix], ret);
else
{
make_plural = 1;
gPluid_list->item[ix].u.number = 1;
}
}
if (gId_list &&
gId_list->size > ix &&
gId_list->item[ix].type == T_NUMBER &&
gId_list->item[ix].u.number == 0)
{
ret = apply(QGET_ID, ob, 0, 1);
if (ret && ret->type == T_POINTER)
{
assign_svalue_no_free(&gId_list->item[ix], ret);
if (make_plural)
{
tmp = allocate_array(ret->u.vec->size);
sing = ret->u.vec;
sval.type = T_STRING;
sval.string_type = STRING_MSTRING;
for (il = 0; il < tmp->size; il++)
{
if (sing->item[il].type == T_STRING)
{
str = parse_to_plural(sing->item[il].u.string);
sval.u.string = str;
assign_svalue_no_free(&tmp->item[il],&sval);
free_mstring(sval.u.string);
}
}
sval.type = T_POINTER;
sval.u.vec = tmp;
assign_svalue_no_free(&gPluid_list->item[ix], &sval);
free_svalue(&sval);
}
}
else
{
gId_list->item[ix].u.number = 1;
}
}
if (gAdjid_list &&
gAdjid_list->size > ix &&
gAdjid_list->item[ix].type == T_NUMBER &&
gAdjid_list->item[ix].u.number == 0)
{
ret = apply(QGET_ADJID, ob, 0, 1);
if (ret && ret->type == T_POINTER)
assign_svalue_no_free(&gAdjid_list->item[ix], ret);
else
gAdjid_list->item[ix].u.number = 1;
}
}
void
free_parse_information()
{
if (gId_list_d)
free_vector(gId_list_d);
gId_list_d = 0;
if (gPluid_list_d)
free_vector(gPluid_list_d);
gPluid_list_d = 0;
if (gAdjid_list_d)
free_vector(gAdjid_list_d);
gAdjid_list_d = 0;
if (gPrepos_list)
free_vector(gPrepos_list);
gPrepos_list = 0;
if (gAllword)
free(gAllword);
gAllword = 0;
}
/* Main function, called from interpret.c
*/
/*
* Function name: parse
* Description: The main function for the efun: parse_command()
* It parses a given command using a given pattern and
* a set of objects (see args below). For details
* see LPC documentation of the efun.
* Arguments: cmd: The command to parse
* ob_or_array: A list of objects or one object from
* which to make a list of objects by
* using the objects deep_inventory
* pattern: The given parse pattern somewhat like sscanf
* but with different %-codes, see efun docs.
* stack_args: Pointer to destination arguments.
* num_arg: Number of destination arguments.
* Returns: True if command matched pattern.
*/
int
parse (char *cmd, struct svalue *ob_or_array, char *pattern,
struct svalue *stack_args, int num_arg)
/*
char *cmd; Command to parse
struct svalue *ob_or_array; Object or array of objects
char *pattern; Special parsing pattern
struct svalue *stack_args; Pointer to lvalue args on stack
int num_arg; Number of args on stack
*/
{
struct vector *obvec, *patvec, *wvec;
struct vector *old_id, *old_plid, *old_adjid;
int pix, cix, six, fail, fword, ocix, fpix;
struct svalue *pval;
void check_for_destr(struct svalue *); /* In interpret.c */
struct svalue *sub_parse(struct vector *, struct vector *, int *, struct vector *, int *, int *, struct svalue *);
struct svalue *slice_words(struct vector *, int, int), tmp;
void stack_put(struct svalue *, struct svalue *, int, int);
struct vector *deep_inventory(struct object *, int);
/*
* Pattern and commands can not be empty
*/
if (*cmd == '\0' || *pattern == '\0')
return 0;
wvec = explode_string(cmd," "); /* Array of words in command */
patvec = explode_string(pattern," "); /* Array of pattern elements */
/*
* Explode can return '0'.
*/
if (!wvec)
wvec = allocate_array(0);
if (!patvec)
patvec = allocate_array(0);
INCREF(wvec->ref); /* Do not lose these arrays */
INCREF(patvec->ref);
if (ob_or_array->type == T_POINTER)
obvec = ob_or_array->u.vec;
else if (ob_or_array->type == T_OBJECT)
obvec = deep_inventory(ob_or_array->u.ob, 1); /* 1 == ob + deepinv */
else
{
obvec = 0;
error("Bad second argument to parse_command()\n");
}
tmp.type = T_POINTER;
tmp.u.vec = obvec;
check_for_destr(&tmp);
INCREF(obvec->ref);
/* Copy and make space for id arrays
*/
old_id = gId_list;
old_plid = gPluid_list;
old_adjid = gAdjid_list;
gId_list = allocate_array(obvec->size);
gPluid_list = allocate_array(obvec->size);
gAdjid_list = allocate_array(obvec->size);
/* Loop through the pattern. Handle %s but not '/'
*/
pix = 0;
#ifndef OLD_EXPLODE
while (pix < patvec->size && !*(patvec->item[pix].u.string))
pix++;
#endif
six = 0;
cix = 0;
fail = 0;
for (; pix < patvec->size; pix++)
{
pval = 0; /* The 'fill-in' value */
fail = 0; /* 1 if match failed */
if (EQ(patvec->item[pix].u.string, "%s"))
{
/* We are at end of pattern, scrap up the remaining
words and put them in the fill-in value.
*/
if (pix == (patvec->size-1))
{
pval = slice_words(wvec, cix, wvec->size - 1);
cix = wvec->size;
stack_put(pval, stack_args, six++, num_arg);
}
else
/*
There is something after %s, try to parse with the
next pattern. Begin with the current word and step
one word for each fail, until match or end of words.
*/
{
ocix = fword = cix; /* Current word */
fpix = ++pix; /* pix == next pattern */
do
{
fail = 0;
/*
Parse the following pattern, fill-in values:
stack_args[six] = result of %s
stack_args[six + 1] = result of following pattern,
if it is a fill-in pattern
*/
pval = sub_parse(obvec, patvec, &pix, wvec, &cix, &fail,
((six + 1) < num_arg) ?
stack_args[six + 1].u.lvalue
: 0);
if (fail)
{
cix = ++ocix;
pix = fpix;
}
} while ((fail) && (cix < wvec->size));
/*
We found something matching the pattern after %s.
First
stack_args[six + 1] = result of match
Then
stack_args[six] = the skipped words before match
*/
if (!fail)
{
/* A match with a value fill in param */
if (pval)
{
stack_put(pval, stack_args, six + 1, num_arg);
pval = slice_words(wvec, fword, ocix - 1);
stack_put(pval, stack_args, six, num_arg);
six += 2;
}
else
{
/* A match with a non value ie 'word' */
pval = slice_words(wvec, fword, ocix - 1);
stack_put(pval, stack_args, six++, num_arg);
}
pval = 0;
}
}
}
else if (!EQ(patvec->item[pix].u.string,"/"))
{
/* The pattern was not %s, parse the pattern if
* it is not '/', a '/' here is skipped.
* If match, put in fill-in value.
*/
pval = sub_parse(obvec, patvec, &pix, wvec, &cix, &fail,
(six < num_arg) ? stack_args[six].u.lvalue : 0);
if (!fail && pval)
stack_put(pval, stack_args, six++, num_arg);
}
/* Terminate parsing if no match
*/
if (fail)
break;
}
/* Also fail when there are words left to parse and pattern exhausted
*/
if (cix < wvec->size)
fail = 1;
/* Delete and free the id arrays
*/
if (gId_list)
{
free_vector(gId_list);
}
if (gPluid_list)
{
free_vector(gPluid_list);
}
if (gAdjid_list)
{
free_vector(gAdjid_list);
}
gId_list = old_id;
gPluid_list = old_plid;
gAdjid_list = old_adjid;
DECREF(wvec->ref);
DECREF(patvec->ref);
DECREF(obvec->ref);
free_vector(wvec);
free_vector(patvec);
/*
* A vector we made should be freed
*/
if (ob_or_array->type == T_OBJECT)
{
free_vector(obvec);
}
return !fail;
}
/*
* Function name: stack_put
* Description: Puts an svalue on the stack.
* Arguments: pval: Value to put
* sp: Stackpointer
* pos: Position on stack to put value
* max: The number of args on the stack
*/
void
stack_put(struct svalue *pval, struct svalue *msp, int pos, int max)
{
if (pos >= max)
return;
if ((pval) && (msp[pos].type == T_LVALUE))
assign_svalue(msp[pos].u.lvalue, pval);
}
/*
* Function name: slice_words
* Description: Gives an imploded string of words from an array
* Arguments: wvec: array of words
* from: First word to use
* to: Last word to use
* Returns: A pointer to a static svalue now containing string.
*/
struct svalue *
slice_words(struct vector *wvec, int from, int to)
{
struct vector *slice;
char *tx;
static struct svalue stmp = { T_NUMBER };
tx = 0;
if (from <= to)
{
slice = slice_array(wvec, from, to);
if (slice->size)
tx = implode_string(slice, " ");
free_vector(slice);
}
free_svalue(&stmp); /* May be allocated! */
if (tx)
{
stmp.type = T_STRING;
stmp.string_type = STRING_MSTRING;
stmp.u.string = tx;
}
else
{
stmp.type = T_STRING;
stmp.string_type = STRING_CSTRING;
stmp.u.string = "";
}
return &stmp;
}
/*
* Function name: sub_parse
* Description: Parses a vector of words against a pattern. Gives
* result as an svalue. Sets fail if parsing fails and
* updates pointers in pattern and word vectors. It
* handles alternate patterns but not "%s"
*/
struct svalue *
sub_parse(struct vector *obvec, struct vector *patvec, int *pix_in,
struct vector *wvec, int *cix_in, int *fail, struct svalue *msp)
{
int cix, pix, subfail;
struct svalue *pval;
struct svalue *one_parse(struct vector *, char *, struct vector *,
int *, int *, struct svalue *);
cix = *cix_in; pix = *pix_in; subfail = 0;
pval = one_parse(obvec, patvec->item[pix].u.string,
wvec, &cix, &subfail, msp);
while (subfail)
{
pix++;
cix = *cix_in;
/*
Find the next alternative pattern, consecutive '/' are skipped
*/
while ((pix < patvec->size) && (EQ(patvec->item[pix].u.string, "/")))
{
subfail = 0;
pix++;
}
if (!subfail && (pix < patvec->size))
{
pval = one_parse(obvec, patvec->item[pix].u.string, wvec, &cix,
&subfail, msp);
}
else if (subfail == 2) /* failed optional */
{
subfail = 0;
pix = pix-1;
}
else
{
*fail = 1; *pix_in = pix-1;
return 0;
}
}
/* If there are alternatives left after the matching pattern, skip them
*/
if ((pix + 1 < patvec->size) && (EQ(patvec->item[pix + 1].u.string, "/")))
{
while ((pix + 1 <patvec->size) &&
(EQ(patvec->item[pix + 1].u.string, "/")))
{
pix += 2;
}
if (pix>=patvec->size)
pix = patvec->size-1;
}
*cix_in = cix;
*pix_in = pix;
*fail = 0;
return pval;
}
/*
* Function name: one_parse
* Description: Checks one parse pattern to see if match. Consumes
* needed number of words from wvec.
* Arguments: obvec: Vector of objects relevant to parse
* pat: The pattern to match against.
* wvec: Vector of words in the command to parse
* cix_in: Current word in commandword vector
* fail: Fail flag if parse did not match
* prep_param: Only used on %p (see prepos_parse)
* Returns: svalue holding result of parse.
*/
struct svalue *
one_parse(struct vector *obvec, char *pat, struct vector *wvec, int *cix_in,
int *fail, struct svalue *prep_param)
{
char ch;
struct svalue *pval;
static struct svalue stmp = { T_NUMBER };
char *str1, *str2;
struct svalue *item_parse(struct vector *, struct vector *, int *, int *);
struct svalue *living_parse(struct vector *, struct vector *, int *, int *);
struct svalue *single_parse(struct vector *, struct vector *, int *, int *);
struct svalue *prepos_parse(struct vector *, int *, int *, struct svalue *);
struct svalue *number_parse(struct vector *, int *, int *);
/*
Fail if we have a pattern left but no words to parse
*/
if (*cix_in >= wvec->size)
{
if (pat[0] == '[')
*fail = 0;
else
*fail = 1;
return 0;
}
pval = 0;
ch = (pat[0] == '%' ? pat[1] : pat[0]);
switch (ch)
{
case 'i':
pval = item_parse(obvec, wvec, cix_in, fail);
break;
case 'l':
pval = living_parse(obvec, wvec, cix_in, fail);
break;
case 's':
*fail = 0; /* This is double %s in pattern, skip it */
break;
case 'w':
free_svalue(&stmp);
stmp.type = T_STRING;
stmp.string_type = STRING_SSTRING;
stmp.u.string = make_sstring(wvec->item[*cix_in].u.string);
pval = &stmp;
(*cix_in)++;
*fail = 0;
break;
case 'o':
pval = single_parse(obvec, wvec, cix_in, fail);
break;
case 'p':
pval = prepos_parse(wvec, cix_in, fail, prep_param);
break;
case 'd':
pval = number_parse(wvec, cix_in, fail);
break;
case '\'':
str1 = &pat[1]; str2 = wvec->item[*cix_in].u.string;
if ((strncmp(str1, str2, strlen(str1) - 1) == 0) &&
(strlen(str1) == strlen(str2) + 1))
{
*fail = 0;
(*cix_in)++;
}
else
*fail = 1;
break;
case '[':
str1 = &pat[1]; str2 = wvec->item[*cix_in].u.string;
if ((strncmp(str1, str2, strlen(str1) - 1) == 0) &&
(strlen(str1) == strlen(str2) + 1))
{
(*cix_in)++;
*fail = 0;
}
else
{
*fail = 2;
}
break;
default:
*fail = 0; /* Skip invalid patterns */
}
return pval;
}
/*
We normally define these, see initial documentation (top of file)
*/
#ifndef PARSE_FOREIGN
static char *ord1[] = {"", "first", "second", "third", "fourth", "fifth",
"sixth", "seventh", "eighth", "ninth", "tenth",
"eleventh", "twelfth", "thirteenth", "fourteenth",
"fifteenth", "sixteenth", "seventeenth",
"eighteenth","nineteenth"};
static char *ord10[] = {"", "", "twenty","thirty","forty","fifty","sixty",
"seventy", "eighty","ninety"};
static char *sord10[] = {"", "", "twentieth", "thirtieth", "fortieth",
"fiftieth", "sixtieth","seventieth", "eightieth",
"ninetieth"};
static char *num1[] = {"", "one","two","three","four","five","six",
"seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen",
"sixteen", "seventeen","eighteen","nineteen"};
static char *num10[] = {"", "", "twenty","thirty","forty","fifty","sixty",
"seventy", "eighty","ninety"};
#endif
/*
* Function name: number_parse
* Description: Tries to interpret the word in wvec as a numeral
* descriptor and returns the result on the form:
* ret.type == T_NUMBER
* num == 0, 'zero', '0', gAllword
* num > 0, one, two, three etc or numbers given
* num < 0, first, second,third etc given
* Arguments: wvec: Vector of words in the command to parse
* cix_in: Current word in commandword vector
* fail: Fail flag if parse did not match
* Returns: svalue holding result of parse.
*/
struct svalue *
number_parse(struct vector *wvec, int *cix_in, int *fail)
{
int cix, ten, ones;
long long num;
char buf[100];
static struct svalue stmp; /* No need to free, only numbers */
cix = *cix_in; *fail = 0;
if (sscanf(wvec->item[cix].u.string, "%lld", &num))
{
if (num > 0)
{
(*cix_in)++;
stmp.type = T_NUMBER;
stmp.u.number = num;
return &stmp;
}
*fail = 1;
return 0; /* Only positive numbers */
}
if (gAllword && (strcmp(wvec->item[cix].u.string, gAllword) == 0))
{