[go: up one dir, main page]

File: cook.c

package info (click to toggle)
cook 2.19-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,316 kB
  • ctags: 3,969
  • sloc: ansic: 50,331; sh: 13,190; makefile: 4,542; yacc: 3,174; perl: 224; awk: 154
file content (1026 lines) | stat: -rw-r--r-- 20,417 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
/*
 *	cook - file construction tool
 *	Copyright (C) 1999, 2000, 2001 Peter Miller;
 *	All rights reserved.
 *
 *	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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate cook native matching
 *
 * This is in the inner loop, so it must perform well.
 * A free list of match structures is maintained to avoid malloc calls;
 * malloc is only called when this free list is empty.
 *
 * The tough part about designing a pattern matcher for something like cook is
 * that the patterns must be reversible.  That is, it must be possible to use
 * the same string both as a pattern to be matched against and as a template
 * for building a string once a pattern has matched.  Rather like the
 * difference between the left and right sides of an editor search-and-replace
 * command using the same description for both the search pattern and the
 * replace template.  This is why classic regular expressions have not been
 * used.  They tend to be slow to match, too.
 *
 * This matcher has eleven match "fields", referenced as % and %0 to %9.
 * The % character can be escaped as %%.  The % and %1 to %9 forms match any
 * character except '/'.  The %0 form matches all characters, but must be
 * either empty, or have whole path components, including the trailing '/' on
 * each component.  A few examples will make this clearer:
 *	"%.c" matches "fred.c" with %="fred"
 *	"%.c" failes to match "snot/fred.c"
 *	"%1/%2.c"matches "snot/fred.c" with %1="snot" and %2="fred"
 *	"%1/%2.c" fails to match "etc/boo/fred.c"
 *	"%0%5.c" matches "fred.c" with %0="" and %5="fred"
 *	"%0%6.c" matches "snot/fred.c" with %0="snot/" and %6="fred"
 *	"%0%7.c" matches "etc/boo/fred.c" with %0="etc/boo/" and %7="fred"
 *	"/usr/%1/%1%2/%3.%2%4" matches "/usr/man/man1/fred.1x" with %1="man",
 *		%2="1", %3="fred" and %4="x".
 * The %0 behaviour is designed to allow patterns to range over subtrees in a
 * controlled manner.  Note that the use of this sort of pattern in a recipe
 * will result in deeper seraches than the naive recipe designer would expect.
 */

#include <ac/ctype.h>
#include <ac/string.h>

#include <error.h> /* for assert */
#include <error_intl.h>
#include <expr/position.h>
#include <match/cook.h>
#include <match/private.h>
#include <str.h>
#include <stracc.h>
#include <trace.h>

/*
 * Define this symbol if you want the original strict interpretation of
 * the %0 patter.  I.e. it may only apprae at the start of the pattern,
 * or immedately after a slash (/) synbol.  Relaxing this restruiction
 * does not change the semantics of any cookbooks.  However, it means
 * some errors will not be detected - except that most users didn't think
 * they were errors.  E.g.
 * 	[match_mask -I%0% [CFLAGS]]
 *
#define STRICT_PERCENT_0_USAGE
 */


#ifdef DEBUG
enum magic_t
{
	MAGIC_CONSTRUCTED = 4321,
	MAGIC_COMPILED,
	MAGIC_EXECUTED,
	MAGIC_RECONSTRUCTED,
	MAGIC_DESTROYED
};
typedef enum magic_t magic_t;
#endif /* DEBUG */

typedef struct match_cook_ty match_cook_ty;
struct match_cook_ty
{
	match_ty	inherited;
	string_ty	*formal;
	string_ty	*fill[11];
#ifdef DEBUG
	magic_t		magic;
#endif /* DEBUG */
};


#define MATCH_CHAR '%'


/*
 * NAME
 *	illegal_pattern - complain
 *
 * SYNOPSIS
 *	void illegal_pattern(char *s);
 *
 * DESCRIPTION
 *	The illegal_pattern function is used to complain about errors in
 *	pattern secifications.
 *
 * RETURNS
 *	void
 */

static void illegal_pattern _((const expr_position_ty *, string_ty *, int));

static void
illegal_pattern(pp, s, why)
	const expr_position_ty *pp;
	string_ty	*s;
	int		why;
{
	sub_context_ty	*scp;

	if (why < 0)
	{
		scp = sub_context_new();
		sub_var_set(scp, "Name", "%c0", MATCH_CHAR);
		sub_var_set(scp, "Pattern", "%S", s);
		error_with_position
		(
			pp,
			scp,
		     i18n("illegal position of '$name' in \"$pattern\" pattern")
		);
		sub_context_delete(scp);
		return;
	}
	scp = sub_context_new();
	if (why >= 10)
		sub_var_set(scp, "Name", "%c", MATCH_CHAR);
	else
		sub_var_set(scp, "Name", "%c%d", MATCH_CHAR, why);
	sub_var_set(scp, "Pattern", "%S", s);
	error_with_position
	(
		pp,
		scp,
		i18n("illegal use of '$name' in \"$pattern\" pattern")
	);
	sub_context_delete(scp);
}


#ifdef DEBUG

static void check_magic _((match_cook_ty *, int, magic_t));

static void
check_magic(this, line, new_state)
	match_cook_ty	*this;
	int		line;
	magic_t		new_state;
{
	switch (this->magic)
	{
	default:
		fatal_raw("%s: %d: operating on wrong kind of object",
			__FILE__, line);

	case MAGIC_DESTROYED:
		fatal_raw("%s: %d: operating on destroyed object",
			__FILE__, line);

	case MAGIC_CONSTRUCTED:
	case MAGIC_COMPILED:
	case MAGIC_EXECUTED:
	case MAGIC_RECONSTRUCTED:
		break;
	}
	switch (new_state)
	{
	case MAGIC_CONSTRUCTED:
		fatal_raw("%s: %d: can't happen", __FILE__, line);
		break;

	case MAGIC_DESTROYED:
		break;

	case MAGIC_COMPILED:
#if 0
		if (this->magic != MAGIC_CONSTRUCTED)
			fatal_raw("%s: %d: may only compile once",
				__FILE__, line);
		}
#endif
		break;

	case MAGIC_EXECUTED:
		if (this->magic < MAGIC_COMPILED)
		{
			fatal_raw("%s: %d: executing when uncompiled",
				__FILE__, line);
		}
		break;

	case MAGIC_RECONSTRUCTED:
		if (this->magic < MAGIC_EXECUTED)
		{
			fatal_raw("%s: %d: reconstructing when unexecuted",
				__FILE__, line);
		}
		break;
	}
	this->magic = new_state;
}

#endif /* DEBUG */



/*
 * NAME
 *	match_free - dispose of match structure
 *
 * SYNOPSIS
 *	void match_free(match_ty *);
 *
 * DESCRIPTION
 *	The match_free function is used to dispose of a match structure
 *	allocated by the match_alloc function.
 *
 * RETURNS
 *	void
 */

static void destructor _((match_ty *));

static void
destructor(mp)
	match_ty	*mp;
{
	match_cook_ty	*this;
	int		j;

	trace(("match_cook::destructor(mp = %08X)\n{\n"/*}*/, mp));
	this = (match_cook_ty *)mp;
#ifdef DEBUG
	check_magic(this, __LINE__, MAGIC_DESTROYED);
#endif
	if (this->formal)
	{
		str_free(this->formal);
		this->formal = 0;
	}
	for (j = 0; j < SIZEOF(this->fill); ++j)
	{
		if (this->fill[j])
		{
			str_free(this->fill[j]);
			this->fill[j] = 0;
		}
	}
	trace((/*{*/"}\n"));
}


static void constructor _((match_ty *));

static void
constructor(mp)
	match_ty	*mp;
{
	match_cook_ty	*this;
	int		j;

	trace(("match_cook::constructor(mp = %08X)\n{\n"/*}*/, mp));
	this = (match_cook_ty *)mp;
	this->formal = 0;
	for (j = 0; j < SIZEOF(this->fill); ++j)
		this->fill[j] = 0;
#ifdef DEBUG
	this->magic = MAGIC_CONSTRUCTED;
#endif /* DEBUG */
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *	attempt - match pattern to string
 *
 * SYNOPSIS
 *	int attempt(char *original_patn, char *patn, char *str,
 *		match_ty *field);
 *
 * DESCRIPTION
 *	The attempt function is used to match up a pattern with a string,
 *	filling in the fields as it goes.
 *
 * RETURNS
 *	int: zero if does not match, nonzero if does match.
 *		-1 on error
 *
 * CAVEAT
 *	The field structure is not allocated here.
 */

static int attempt_inner _((match_cook_ty *, char *, char *, char *, char *,
	const expr_position_ty *));

static int
attempt_inner(this, formal_begin, formal_end, actual_begin, actual_end, pp)
	match_cook_ty	*this;
	char		*formal_begin;
	char		*formal_end;
	char		*actual_begin;
	char		*actual_end;
	const expr_position_ty *pp;
{
	size_t		idx;
	string_ty	*sp;
	int		result;
	char		*q;
	int		sub_result;

	trace(("attempt_inner(this = %08lX, formal_begin = %08lX, \
actual_begin = %08lX)\n{\n",
		(long)this, (long)formal_begin, (long)actual_begin));
	trace(("formal = \"%.*s\";\n", (int)(formal_end - formal_begin),
		formal_begin));
	trace(("actual = \"%.*s\";\n", (int)(actual_end - actual_begin),
		actual_begin));

	/*
	 * Rip any matching constant string off the end of the formal
	 * and actual strings.  Nice, easy rejections here, and they lay
	 * the foundations for an optimization inside the main loop,
	 * avoiding a recursion.
	 */
	for (;;)
	{
		/*
		 * If we have run out of formal string, we must also run
		 * out of actual string.
		 */
		assert(formal_begin <= formal_end);
		if (formal_begin >= formal_end)
		{
			result = (actual_begin >= actual_end);
			goto ret;
		}

		/*
		 * If the last character could possibly be part of a
		 * matching sequence, stop.  We can't actually tell,
		 * because the matching sequence is only meaningful
		 * left-to-right.
		 *
		 * Examples of right-to-left broken-ness: %%0, %%%
		 */
		if (formal_end[-1] == MATCH_CHAR)
			break;
		if
		(
			formal_begin + 1 < formal_end
		&&
			formal_end[-2] == MATCH_CHAR
		&&
			isdigit((unsigned char)formal_end[-1])
		)
			break;

		/*
		 * We are looking at a constant.
		 * See if it matches.
		 */
		if
		(
			actual_begin >= actual_end
		||
			actual_end[-1] != formal_end[-1]
		)
		{
			result = 0;
			goto ret;
		}
		--formal_end;
		--actual_end;
	}
	trace(("formal = \"%.*s\";\n",
		(int)(formal_end - formal_begin), formal_begin));
	trace(("actual = \"%.*s\";\n",
		(int)(actual_end - actual_begin), actual_begin));

	for (;;)
	{
		/*
		 * Take care of the end of the string
		 */
		if (formal_begin >= formal_end)
		{
			result = (actual_begin >= actual_end);
			goto ret;
		}

		/*
		 * Take care of literal characters
		 */
		if (*formal_begin != MATCH_CHAR)
		{
			if
			(
				actual_begin >= actual_end
			||
				*formal_begin++ != *actual_begin++
			)
			{
				result = 0;
				goto ret;
			}
			continue;
		}

		/*
		 * take care of quoted match character
		 */
		if
		(
			formal_begin + 1 <= formal_end
		&&
			formal_begin[1] == MATCH_CHAR
		)
		{
			if
			(
				actual_begin >= actual_end
			||
				*actual_begin++ != MATCH_CHAR
			)
			{
				result = 0;
				goto ret;
			}
			formal_begin += 2;
			continue;
		}

		/*
		 * The %0 pattern element matches zero or more directory
		 * pieces, including the trailing slashes.
		 */
		if
		(
			formal_begin + 1 <= formal_end
		&&
			formal_begin[1] == '0'
		)
		{
			char	*midpoint;

			formal_begin += 2;

			/*
			 * It must appear at the beginning of the
			 * pattern, or immediately following slashes.
			 * It may not appear before a slash.
			 */
#ifdef STRICT_PERCENT_0_USAGE
			if
			(
				(
					(
						formal_begin
					>
						this->formal->str_text + 2
					)
				&&
					formal_begin[-3] != '/'
				)
			||
				(
					formal_begin < formal_end
				&&
					*formal_begin == '/'
				)
			)
			{
				illegal_pattern(pp, this->formal, -1);
				result = -1;
				goto ret;
			}
#endif /* STRICT_PERCENT_0_USAGE */

			/*
			 * It could have been seen earlier, must be
			 * identical if so.
			 */
			/* this->mask |= 1; */
			sp = this->fill[0];
			if (sp)
			{
				if
				(
					(
						actual_begin + sp->str_length
					>
						actual_end
					)
				||
					(
						0
					!=
						memcmp
						(
							actual_begin,
							sp->str_text,
							sp->str_length
						)
					)
				)
				{
					result = 0;
					goto ret;
				}
				actual_begin += sp->str_length;
				continue;
			}

			/*
			 * Match the largest number of whole directory chunks.
			 */
			midpoint = actual_end;
			for (;;)
			{
				while (midpoint > actual_begin)
				{
					if (midpoint[-1] == '/')
						break;
					--midpoint;
				}

				this->fill[0] =
					str_n_from_c
					(
						actual_begin,
						midpoint - actual_begin
					);
				trace_string(this->fill[0]->str_text);
				sub_result =
					attempt_inner
					(
						this,
						formal_begin,
						formal_end,
						midpoint,
						actual_end,
						pp
					);
				if (sub_result < 0)
				{
					result = -1;
					goto ret;
				}
				if (sub_result)
				{
					result = 1;
					goto ret;
				}
				str_free(this->fill[0]);
				this->fill[0] = 0;
				--midpoint;
				if (midpoint <= actual_begin)
				{
					result = 0;
					goto ret;
				}
			}
		}

		/*
		 * figure idx
		 */
		if (isdigit((unsigned char)formal_begin[1]))
		{
			idx = formal_begin[1] - '0';
			formal_begin += 2;
		}
		else
		{
			idx = 10;
			++formal_begin;
		}
		/* this->mask |= 1 << idx; */

		/*
		 * see if the field is already set
		 * must be identical if so
		 */
		sp = this->fill[idx];
		if (sp)
		{
			if
			(
				actual_begin + sp->str_length > actual_end
			||
				(
					0
				!=
					memcmp
					(
						actual_begin,
						sp->str_text,
						sp->str_length
					)
				)
			)
			{
				result = 0;
				break;
			}
			actual_begin += sp->str_length;
			continue;
		}

		/*
		 * Fast special case.  This is very common, because we
		 * stripped the constants off the end.
		 */
		if (formal_begin >= formal_end)
		{
			if
			(
				memchr
				(
					actual_begin,
					'/',
					actual_end - actual_begin
				)
			)
			{
				result = 0;
				goto ret;
			}
			this->fill[idx] =
				str_n_from_c
				(
					actual_begin,
					actual_end - actual_begin
				);
			trace(("idx = %ld;\n", (long)idx));
			result = 1;
			break;
		}

		/*
		 * The normal % and %N sequences can't match a slash at
		 * all.  This allows for a quick reject, and short
		 * circuits some of the recursion alternatives.
		 */
		q = memchr(actual_begin, '/', actual_end - actual_begin);
		if (q)
		{
			if
			(
				formal_begin < formal_end
			&&
				!memchr
				(
					formal_begin,
					'/',
					formal_end - formal_begin
				)
			)
			{
				result = 0;
				break;
			}
		}
		else
			q = actual_end;
		/*
                 * Pattern elements are not allowed to be empty at the
                 * start of the pattern because it causes problem with
                 * false matches against absolute paths.
		 * If you change this to a simple ``>='' test 36 will fail,
		 * If you change this to a simple ``>'' test 196 will fail.
		 */
		while
		(
			formal_begin > this->formal->str_text + 2
		?
			q >= actual_begin
		:
			q > actual_begin
		)
		{
			this->fill[idx] =
				str_n_from_c(actual_begin, q - actual_begin);
			trace(("idx = %ld;\n", (long)idx));
			trace_string(this->fill[idx]->str_text);
			sub_result =
				attempt_inner
				(
					this,
					formal_begin,
					formal_end,
					q,
					actual_end,
					pp
				);
			if (sub_result < 0)
			{
				result = -1;
				goto ret;
			}
			if (sub_result)
			{
				result = 1;
				goto ret;
			}
			str_free(this->fill[idx]);
			this->fill[idx] = 0;
			--q;
		}
		result = 0;
		break;
	}
ret:
	trace(("return %d;\n", result));
	trace(("}\n"));
	return result;
}


/*
 * NAME
 *	match - attempt to
 *
 * SYNOPSIS
 *	match_ty *match(string_ty *pattern, string_ty *string);
 *
 * DESCRIPTION
 *	The match function is used to match a pattern with a string.
 *	The matching fields are filled in in the returned structure.
 *
 * RETURNS
 *	match_ty *: a pointer to a match structure in dynamic memory with the
 *	match fields set as appropriate.
 *
 *	A NULL pointer is returned if the string does not match the
 *	pattern.
 *
 *	The value MATCH_ERROR will be returned if it was not a valid
 *	pattern; the error message will have been printed already.
 *
 * CAVEAT
 *	The match structure should be released by calling match_free.,
 */

static int compile _((match_ty *, string_ty *, const expr_position_ty *));

static int
compile(mp, formal, pp)
	match_ty	*mp;
	string_ty	*formal;
	const expr_position_ty *pp;
{
	match_cook_ty	*this;
	int		j;
	int		result;

	trace(("match_cook::compile(mp = %08lX, formal = %08lX)\n{\n",
		(long)mp, (long)formal));
	trace(("formal = \"%s\";\n", formal->str_text));
	this = (match_cook_ty *)mp;
	result = 0;
#ifdef DEBUG
	check_magic(this, __LINE__, MAGIC_COMPILED);
#endif /* DEBUG */

	/*
	 * Reset the match buffers.
	 */
	if (this->formal)
		str_free(this->formal);
	for (j = 0; j < SIZEOF(this->fill); ++j)
	{
		if (this->fill[j])
		{
			str_free(this->fill[j]);
			this->fill[j] = 0;
		}
	}

	/*
	 * remember the pattern
	 */
	this->formal = str_copy(formal);

	trace(("return %d;\n", result));
	trace(("}\n"));
	return result;
}


static int execute _((match_ty *, string_ty *, const expr_position_ty *));

static int
execute(mp, actual, pp)
	match_ty	*mp;
	string_ty	*actual;
	const expr_position_ty *pp;
{
	match_cook_ty	*this;
	int		j;
	int		result;

	trace(("match_cook::execute(mp = %08lX, actual = %08lX)\n{\n",
		(long)mp, (long)actual));
	trace(("actual = \"%s\";\n", actual->str_text));
	this = (match_cook_ty *)mp;
#ifdef DEBUG
	check_magic(this, __LINE__, MAGIC_EXECUTED);
#endif /* DEBUG */

	/*
	 * Reset the match buffers.
	 */
	for (j = 0; j < SIZEOF(this->fill); ++j)
	{
		if (this->fill[j])
		{
			str_free(this->fill[j]);
			this->fill[j] = 0;
		}
	}

	/*
	 * The inner matcher can recurse on itself.
	 * Because it nibbles away at both ends at various times, it
	 * also has a different calling interface.
	 */
	result =
		attempt_inner
		(
			this,
			this->formal->str_text,
			this->formal->str_text + this->formal->str_length,
			actual->str_text,
			actual->str_text + actual->str_length,
			pp
		);
#ifdef DEBUG
	if (result <= 0)
		this->magic = MAGIC_COMPILED;
#endif /* DEBUG */
	trace(("return %d;\n", result));
	trace(("}\n"));
	return result;
}


/*
 * NAME
 *	reconstruct - make string from pattern
 *
 * SYNOPSIS
 *	string_ty *reconstruct(string_ty *pattern, match_ty *field);
 *
 * DESCRIPTION
 *	The reconstruct function is used to rebuild a string from a replacement
 *	pattern and the match field values.
 *
 * RETURNS
 *	string_ty *; pointer to the reconstructed string
 *		or NULL on error (the error will already have been rinted)
 */

static string_ty *reconstruct _((const match_ty *, string_ty *,
	const expr_position_ty *));

static string_ty *
reconstruct(mp, pattern, pp)
	const match_ty	*mp;
	string_ty	*pattern;
	const expr_position_ty *pp;
{
	const match_cook_ty *this;
	static stracc	buffer;
	char		*p;
	string_ty	*s;
	int		idx;

	trace(("match_cook::reconstruct(mp = %08lX, pattern = %08X)\n{\n",
		(long)mp, (long)pattern));
	this = (const match_cook_ty *)mp;
#ifdef DEBUG
	/* magic is mutable */
	check_magic((match_cook_ty *)this, __LINE__, MAGIC_RECONSTRUCTED);
#endif /* DEBUG */
	trace_string(pattern->str_text);

	sa_open(&buffer);
	for (p = pattern->str_text; *p; ++p)
	{
		if (*p == MATCH_CHAR)
		{
			if (p[1] == MATCH_CHAR)
			{
				sa_char(&buffer, MATCH_CHAR);
				++p;
				continue;
			}
			if (p[1] >= '0' && p[1] <= '9')
			{
				idx = p[1] - '0';
				++p;
			}
			else
				idx = 10;
			s = this->fill[idx];
			if (!s)
			{
				illegal_pattern(pp, pattern, idx);
				trace(("return NULL;\n"));
				trace(("}\n"));
				return 0;
			}
			sa_chars(&buffer, s->str_text, s->str_length);
		}
		else
			sa_char(&buffer, *p);
	}

	s = sa_close(&buffer);
	trace_string(s->str_text);
	trace(("return %08lX;\n", s));
	trace(("}\n"));
	return s;
}


static int usage_mask _((const match_ty *, string_ty *,
	const expr_position_ty *));

static int
usage_mask(mp, s, pp)
	const match_ty	*mp;
	string_ty	*s;
	const expr_position_ty *pp;
{
	match_cook_ty	*this;
	char		*cp;
	int		result;

	this = (match_cook_ty *)mp;
	result = 0;
	for (cp = s->str_text; *cp; ++cp)
	{
		if (*cp != MATCH_CHAR)
			continue;
		switch (cp[1])
		{
		default:
			result |= (1 << 10);
			break;

		case MATCH_CHAR:
			++cp;
			break;

		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			result |= 1 << (*++cp - '0');
			break;
		}
	}
	return result;
}


static match_method_ty vtbl =
{
	"cook",
	sizeof(match_cook_ty),
	destructor,
	constructor,
	compile,
	execute,
	reconstruct, /* lhs */
	reconstruct, /* rhs */
	usage_mask,
};


/*
 * NAME
 *	match_alloc - allocate match structure
 *
 * SYNOPSIS
 *	match_ty *match_alloc(void);
 *
 * DESCRIPTION
 *	The match_alloc function is used to allocate a match structure.
 *	The returned structure will be all zeros.
 *
 * RETURNS
 *	match_ty * - a pointer to the match structure in dynamic memory
 *
 * CAVEAT
 *	When finished with it should be disposed of by calling the match_free
 *	function.
 */

match_ty *
match_cook_new()
{
	return match_private_new(&vtbl);
}