Debian Codegraph @

terps/scare/scprintf.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o Is the whole interpolation and ALR passes thing right? There's no
24 * documentation on it, and it's not intuitively implemented in Adrift.
25 *
26 * o Is dissecting HTML tags the right thing to do?
27 */
28
29 #include <assert.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "scare.h"
35 #include "scprotos.h"
36
37
38 /* Assorted definitions and constants. */
39 static const sc_uint PRINTFILTER_MAGIC = 0xb4736417;
40 enum
41 { BUFFER_GROW_INCREMENT = 32,
42 ITERATION_LIMIT = 32
43 };
44 static const sc_char NUL = '\0';
45 static const sc_char LESSTHAN = '<';
46 static const sc_char GREATERTHAN = '>';
47 static const sc_char PERCENT = '%';
48 static const sc_char *const ENTITY_LESSTHAN = "&lt;",
49 *const ENTITY_GREATERTHAN = "&gt;",
50 *const ENTITY_PERCENT = "+percent+";
51 enum
52 { ENTITY_LENGTH = 4,
53 PERCENT_LENGTH = 9
54 };
55 static const sc_char *const ESCAPES = "<>%&+";
56 static const sc_char *const WHITESPACE = "\t\n\v\f\r ";
57
58 /* Trace flag, set before running. */
59 static sc_bool pf_trace = FALSE;
60
61
62 /*
63 * Table tying HTML-like tag strings to enumerated tag types. Since it's
64 * scanned sequentially by strncmp(), it's ordered so that longer strings
65 * come before shorter ones. The <br> tag is missing because this is
66 * handled separately, as a simple put of '\n'.
67 */
68 typedef struct
69 {
70 const sc_char *const name;
71 const sc_int length;
72 const sc_int tag;
73 } sc_html_tags_t;
74
75 static const sc_html_tags_t HTML_TAGS_TABLE[] = {
76 {"bgcolour", 8, SC_TAG_BGCOLOR}, {"bgcolor", 7, SC_TAG_BGCOLOR},
77 {"waitkey", 7, SC_TAG_WAITKEY},
78 {"center", 6, SC_TAG_CENTER}, {"/center", 7, SC_TAG_ENDCENTER},
79 {"centre", 6, SC_TAG_CENTER}, {"/centre", 7, SC_TAG_ENDCENTER},
80 {"right", 5, SC_TAG_RIGHT}, {"/right", 6, SC_TAG_ENDRIGHT},
81 {"font", 4, SC_TAG_FONT}, {"/font", 5, SC_TAG_ENDFONT},
82 {"wait", 4, SC_TAG_WAIT}, {"cls", 3, SC_TAG_CLS},
83 {"i", 1, SC_TAG_ITALICS}, {"/i", 2, SC_TAG_ENDITALICS},
84 {"b", 1, SC_TAG_BOLD}, {"/b", 2, SC_TAG_ENDBOLD},
85 {"u", 1, SC_TAG_UNDERLINE}, {"/u", 2, SC_TAG_ENDUNDERLINE},
86 {"c", 1, SC_TAG_COLOR}, {"/c", 2, SC_TAG_ENDCOLOR},
87 {NULL, 0, SC_TAG_UNKNOWN}
88 };
89
90 /*
91 * Printfilter structure definition. It defines a buffer for output,
92 * associated size and length, a note of any conversion to apply to the next
93 * buffered character, and a flag to let the filter ignore incoming text.
94 */
95 typedef struct sc_filter_s
96 {
97 sc_uint magic;
98 sc_int buffer_length;
99 sc_int buffer_allocation;
100 sc_char *buffer;
101 sc_bool new_sentence;
102 sc_bool is_muted;
103 sc_bool needs_filtering;
104 } sc_filter_t;
105
106
107 /*
108 * pf_is_valid()
109 *
110 * Return TRUE if pointer is a valid printfilter, FALSE otherwise.
111 */
112 static sc_bool
113 pf_is_valid (sc_filterref_t filter)
114 {
115 return filter && filter->magic == PRINTFILTER_MAGIC;
116 }
117
118
119 /*
120 * pf_create()
121 *
122 * Create and return a new printfilter.
123 */
124 sc_filterref_t
125 pf_create (void)
126 {
127 static sc_bool initialized = FALSE;
128
129 sc_filterref_t filter;
130
131 /* On first call only, verify the string lengths in the table. */
132 if (!initialized)
133 {
134 const sc_html_tags_t *entry;
135
136 /* Compare table lengths with string lengths. */
137 for (entry = HTML_TAGS_TABLE; entry->name; entry++)
138 {
139 if (entry->length != (sc_int) strlen (entry->name))
140 {
141 sc_fatal ("pf_create:"
142 " table string length is wrong for \"%s\"\n",
143 entry->name);
144 }
145 }
146
147 initialized = TRUE;
148 }
149
150 /* Create a new printfilter. */
151 filter = sc_malloc (sizeof (*filter));
152 filter->magic = PRINTFILTER_MAGIC;
153 filter->buffer_length = 0;
154 filter->buffer_allocation = 0;
155 filter->buffer = NULL;
156 filter->new_sentence = FALSE;
157 filter->is_muted = FALSE;
158 filter->needs_filtering = FALSE;
159
160 return filter;
161 }
162
163
164 /*
165 * pf_destroy()
166 *
167 * Destroy a printfilter and free its allocated memory.
168 */
169 void
170 pf_destroy (sc_filterref_t filter)
171 {
172 assert (pf_is_valid (filter));
173
174 /* Free buffer space, and poison and free the printfilter. */
175 sc_free (filter->buffer);
176 memset (filter, 0xaa, sizeof (*filter));
177 sc_free (filter);
178 }
179
180
181 /*
182 * pf_interpolate_vars()
183 *
184 * Replace %...% elements in a string by their variable values. If any
185 * variables were interpolated, returns an allocated string with replacements
186 * done, otherwise returns NULL.
187 *
188 * If a %...% element exists that is not a variable, then it's left in as is.
189 * Similarly, an unmatched (single) % in a string is also left as is. There
190 * appears to be no facility in the file format for escaping literal '%'
191 * characters, and since some games have strings with this character in them,
192 * this is probably all that can be done.
193 */
194 static sc_char *
195 pf_interpolate_vars (const sc_char *string, sc_var_setref_t vars)
196 {
197 sc_char *buffer, *cursor, *name;
198 const sc_char *marker;
199 sc_bool is_interpolated;
200
201 /*
202 * Begin with NULL buffer and name strings for lazy allocation, and clear
203 * interpolation detection flag.
204 */
205 buffer = NULL;
206 name = NULL;
207 is_interpolated = FALSE;
208
209 /* Run through the string looking for variables. */
210 marker = string;
211 for (cursor = strchr (marker, PERCENT);
212 cursor; cursor = strchr (marker, PERCENT))
213 {
214 sc_int type;
215 sc_vartype_t vt_rvalue;
216 sc_char close;
217
218 /*
219 * If not yet allocated, allocate a buffer for the return string and
220 * copy up to the percent character into it; otherwise append to buffer
221 * up to percent character. And if not yet done, allocate a name
222 * buffer guaranteed long enough.
223 */
224 if (!buffer)
225 {
226 buffer = sc_malloc (cursor - marker + 1);
227 memcpy (buffer, marker, cursor - marker);
228 buffer[cursor - marker] = NUL;
229 }
230 else
231 {
232 buffer = sc_realloc (buffer, strlen (buffer) + cursor - marker + 1);
233 strncat (buffer, marker, cursor - marker);
234 }
235 if (!name)
236 name = sc_malloc (strlen (string) + 1);
237
238 /*
239 * Get the variable name, and from that, the value. If we encounter a
240 * mismatched '%' or unknown variable, skip it.
241 */
242 if (sscanf (cursor, "%%%[^%]%c", name, &close) != 2
243 || close != PERCENT
244 || !var_get (vars, name, &type, &vt_rvalue))
245 {
246 buffer = sc_realloc (buffer, strlen (buffer) + 2);
247 strncat (buffer, cursor, 1);
248 marker = cursor + 1;
249 continue;
250 }
251
252 /* Get variable value and append to the string. */
253 switch (type)
254 {
255 case VAR_INTEGER:
256 {
257 sc_char value[32];
258
259 sprintf (value, "%ld", vt_rvalue.integer);
260 buffer = sc_realloc (buffer, strlen (buffer) + strlen (value) + 1);
261 strcat (buffer, value);
262 break;
263 }
264
265 case VAR_STRING:
266 buffer = sc_realloc (buffer,
267 strlen (buffer) + strlen (vt_rvalue.string) + 1);
268 strcat (buffer, vt_rvalue.string);
269 break;
270
271 default:
272 sc_fatal ("pf_interpolate_vars: invalid variable type, %ld\n", type);
273 }
274
275 /* Advance over the %...% variable name, and note success. */
276 marker = cursor + strlen (name) + 2;
277 is_interpolated = TRUE;
278 }
279
280 /*
281 * If we allocated a buffer and interpolated into it, append the remainder
282 * of the string. If we didn't interpolate successfully (the input contained
283 * a rogue '%' character), throw out the buffer as it will be the same as
284 * our input.
285 */
286 if (buffer)
287 {
288 if (is_interpolated)
289 {
290 buffer = sc_realloc (buffer, strlen (buffer) + strlen (marker) + 1);
291 strcat (buffer, marker);
292 }
293 else
294 {
295 sc_free (buffer);
296 buffer = NULL;
297 }
298 }
299
300 /* Clean up, and return either the updated string or NULL. */
301 sc_free (name);
302 return buffer;
303 }
304
305
306 /*
307 * pf_replace_alr()
308 *
309 * Helper for pf_replace_alrs(). Replace one ALR found in the string with
310 * its equivalent, updating the buffer at the address passed in, including
311 * reallocating if necessary. Return TRUE if the buffer was changed.
312 */
313 static sc_bool
314 pf_replace_alr (const sc_char *string,
315 sc_char **buffer, sc_int alr, sc_prop_setref_t bundle)
316 {
317 sc_vartype_t vt_key[3];
318 const sc_char *marker, *cursor, *original, *replacement;
319 sc_char *buffer_ = *buffer;
320
321 /* Retrieve the ALR original string, set replacement to NULL for now. */
322 vt_key[0].string = "ALRs";
323 vt_key[1].integer = alr;
324 vt_key[2].string = "Original";
325 original = prop_get_string (bundle, "S<-sis", vt_key);
326 replacement = NULL;
327
328 /* Ignore pathological empty originals. */
329 if (original[0] == NUL)
330 return FALSE;
331
332 /* Run through the marker string looking for things to replace. */
333 marker = string;
334 for (cursor = strstr (marker, original);
335 cursor; cursor = strstr (marker, original))
336 {
337 /* Optimize by retrieving the replacement string only on demand. */
338 if (!replacement)
339 {
340 vt_key[2].string = "Replacement";
341 replacement = prop_get_string (bundle, "S<-sis", vt_key);
342 }
343
344 /*
345 * If not yet allocated, allocate a buffer for the return string and
346 * copy; else append to the existing buffer: basic copy-on-write.
347 */
348 if (!buffer_)
349 {
350 buffer_ = sc_malloc (cursor - marker + strlen (replacement) + 1);
351 memcpy (buffer_, marker, cursor - marker);
352 buffer_[cursor - marker] = NUL;
353 strcat (buffer_, replacement);
354 }
355 else
356 {
357 buffer_ = sc_realloc (buffer_, strlen (buffer_) +
358 cursor - marker + strlen (replacement) + 1);
359 strncat (buffer_, marker, cursor - marker);
360 strcat (buffer_, replacement);
361 }
362
363 /* Advance over the original. */
364 marker = cursor + strlen (original);
365 }
366
367 /* If any pending text, append it to the buffer. */
368 if (replacement)
369 {
370 buffer_ = sc_realloc (buffer_, strlen (buffer_) + strlen (marker) + 1);
371 strcat (buffer_, marker);
372 }
373
374 /* Write back buffer, and if replacement set, the buffer was altered. */
375 *buffer = buffer_;
376 return replacement != NULL;
377 }
378
379
380 /*
381 * pf_replace_alrs()
382 *
383 * Replace any ALRs found in the string with their equivalents. If any
384 * ALRs were replaced, returns an allocated string with replacements done,
385 * otherwise returns NULL.
386 */
387 static sc_char *
388 pf_replace_alrs (const sc_char *string, sc_prop_setref_t bundle,
389 sc_bool alr_applied[], sc_int alr_count)
390 {
391 sc_int index_;
392 sc_char *buffer1, *buffer2, **buffer;
393 const sc_char *marker;
394
395 /*
396 * Begin with NULL buffers and alternate for lazy allocation. To avoid a
397 * lot of allocation and copying, we use two buffers to help with repeated
398 * ALR replacement.
399 */
400 buffer1 = buffer2 = NULL;
401 buffer = &buffer1;
402
403 /* Run through each ALR that exists. */
404 marker = string;
405 for (index_ = 0; index_ < alr_count; index_++)
406 {
407 sc_vartype_t vt_key[3];
408 sc_int alr;
409
410 /*
411 * Ignore ALR indexes that have already been applied. This prevents
412 * endless loops in ALR replacement.
413 */
414 if (alr_applied[index_])
415 continue;
416
417 /*
418 * Get the actual ALR number for the ALR. This comes from the index
419 * that we sorted earlier by length of original string. Try replacing
420 * that ALR in the current marker string.
421 */
422 vt_key[0].string = "ALRs2";
423 vt_key[1].integer = index_;
424 vt_key[2].string = "ALRIndex";
425 alr = prop_get_integer (bundle, "I<-sis", vt_key);
426
427 if (pf_replace_alr (marker, buffer, alr, bundle))
428 {
429 /*
430 * The current buffer in use has been altered. This means that we
431 * have to switch the marker string to the buffer containing the
432 * replacement, and switch 'buffer' to the other one for the next
433 * ALR iteration.
434 */
435 marker = *buffer;
436 buffer = (buffer == &buffer1) ? &buffer2 : &buffer1;
437
438 /* Discard any content in the buffer switched to above. */
439 if (*buffer)
440 (*buffer)[0] = NUL;
441
442 /* Note this ALR as "used up", and unavailable for future passes. */
443 alr_applied[index_] = TRUE;
444 }
445 }
446
447 /*
448 * If marker points to one or other of the buffers, that buffer is the
449 * return string, and the other is garbage, and should now be freed (or
450 * was never used, in which case it is NULL).
451 */
452 if (marker == buffer1)
453 {
454 sc_free (buffer2);
455 return buffer1;
456 }
457 else if (marker == buffer2)
458 {
459 sc_free (buffer1);
460 return buffer2;
461 }
462 else
463 return NULL;
464 }
465
466
467 /*
468 * pf_output_text()
469 *
470 * Edit the tag-stripped text element passed in, substituting &lt; &gt;
471 * +percent+ with < > %, then send to the OS-specific output functions.
472 */
473 static void
474 pf_output_text (const sc_char *string)
475 {
476 sc_int index_, b_index;
477 sc_char *buffer;
478
479 /* Optimize away the allocation and copy if possible. */
480 if (!(strstr (string, ENTITY_LESSTHAN)
481 || strstr (string, ENTITY_GREATERTHAN)
482 || strstr (string, ENTITY_PERCENT)))
483 {
484 if_print_string (string);
485 return;
486 }
487
488 /*
489 * Copy characters from the string into the buffer, replacing any &..;
490 * elements by their single-character equivalents. We also replace any
491 * +percent+ elements by percent characters; apparently an undocumented
492 * Adrift Runner extension.
493 */
494 buffer = sc_malloc (strlen (string) + 1);
495 for (index_ = 0, b_index = 0;
496 string[index_] != NUL; index_++, b_index++)
497 {
498 if (sc_strncasecmp (string + index_,
499 ENTITY_LESSTHAN, ENTITY_LENGTH) == 0)
500 {
501 buffer[b_index] = LESSTHAN;
502 index_ += ENTITY_LENGTH - 1;
503 }
504 else if (sc_strncasecmp (string + index_,
505 ENTITY_GREATERTHAN, ENTITY_LENGTH) == 0)
506 {
507 buffer[b_index] = GREATERTHAN;
508 index_ += ENTITY_LENGTH - 1;
509 }
510 else if (sc_strncasecmp (string + index_,
511 ENTITY_PERCENT, PERCENT_LENGTH) == 0)
512 {
513 buffer[b_index] = PERCENT;
514 index_ += PERCENT_LENGTH - 1;
515 }
516 else
517 buffer[b_index] = string[index_];
518 }
519
520 /* Terminate, print, and free the buffer. */
521 buffer[b_index] = NUL;
522 if_print_string (buffer);
523 sc_free (buffer);
524 }
525
526
527 /*
528 * pf_output_tag()
529 *
530 * Output an HTML-like tag element to the OS-specific tag handling function.
531 */
532 static void
533 pf_output_tag (const sc_char *contents)
534 {
535 const sc_html_tags_t *entry;
536 const sc_char *argument;
537
538 /* For a simple <br> tag, just print out a newline. */
539 if (sc_compare_word (contents, "br", 2))
540 {
541 if_print_character ('\n');
542 return;
543 }
544
545 /*
546 * Search for the name in the HTML tags table. It should be a full match,
547 * that is, the character after the matched name must be space or NUL.
548 * The <bgcolour="xyz"> tag is the exception; here the terminator is '='.
549 */
550 for (entry = HTML_TAGS_TABLE; entry->name; entry++)
551 {
552 if (sc_strncasecmp (contents, entry->name, entry->length) == 0)
553 {
554 sc_char next;
555
556 next = contents[entry->length];
557 if (next == NUL || sc_isspace (next)
558 || (entry->tag == SC_TAG_BGCOLOR && next == '='))
559 break;
560 }
561 }
562
563 /* If not matched, output an unknown tag with contents as its argument. */
564 if (!entry->name)
565 {
566 if_print_tag (SC_TAG_UNKNOWN, contents);
567 return;
568 }
569
570 /*
571 * Find the argument by skipping the tag name and any spaces. Again, for
572 * <bgcolour="xyz">, make a special case, passing the complete contents as
573 * argument (to match <font colour=...> for the client.
574 */
575 argument = contents;
576 argument += (entry->tag != SC_TAG_BGCOLOR) ? entry->length : 0;
577 while (sc_isspace (argument[0]))
578 argument++;
579 if_print_tag (entry->tag, argument);
580 }
581
582
583 /*
584 * pf_output_untagged()
585 *
586 * Break apart HTML-like string into normal text elements, and HTML-like
587 * tags.
588 */
589 static void
590 pf_output_untagged (const sc_char *string)
591 {
592 sc_char *temporary, *untagged, *contents, *cursor;
593 const sc_char *marker;
594
595 /*
596 * Optimize away the allocation and copy if possible. We need to check
597 * here both for tags and for entities; only if neither occurs is it safe
598 * to output the string directly.
599 */
600 if (!strchr (string, LESSTHAN)
601 && !(strstr (string, ENTITY_LESSTHAN)
602 || strstr (string, ENTITY_GREATERTHAN)
603 || strstr (string, ENTITY_PERCENT)))
604 {
605 if_print_string (string);
606 return;
607 }
608
609 /*
610 * Create a general temporary string, and alias it to both untagged text
611 * and the tag name, for sharing inside the loop.
612 */
613 temporary = sc_malloc (strlen (string) + 1);
614 untagged = contents = temporary;
615
616 /* Run through the string looking for <...> tags. */
617 marker = string;
618 for (cursor = strchr (marker, LESSTHAN);
619 cursor; cursor = strchr (marker, LESSTHAN))
620 {
621 sc_char close;
622
623 /* Handle characters up to the tag start; untagged text. */
624 memcpy (untagged, marker, cursor - marker);
625 untagged[cursor - marker] = NUL;
626 pf_output_text (untagged);
627
628 /* Catch and ignore completely empty tags. */
629 if (cursor[1] == GREATERTHAN)
630 {
631 marker = cursor + 2;
632 continue;
633 }
634
635 /*
636 * Get the text within the tag, reusing the temporary buffer. If this
637 * fails, allow the remainder of the line to be delivered as a tag;
638 * unknown, probably.
639 */
640 if (sscanf (cursor, "<%[^>]%c", contents, &close) != 2
641 || close != GREATERTHAN)
642 {
643 if (sscanf (cursor, "<%[^>]", contents) != 1)
644 {
645 sc_error ("pf_output_untagged: mismatched '%c'\n", LESSTHAN);
646 if_print_character (LESSTHAN);
647 marker = cursor + 1;
648 continue;
649 }
650 }
651
652 /* Output tag, and advance marker over the <...> tag. */
653 if (!sc_strempty (contents))
654 pf_output_tag (contents);
655 marker = cursor + strlen (contents) + 1;
656 marker += (marker[0] == GREATERTHAN) ? 1 : 0;
657 }
658
659 /* Output any remaining string text, and free the temporary buffer. */
660 pf_output_text (marker);
661 sc_free (temporary);
662 }
663
664
665 /*
666 * pf_filter_internal()
667 *
668 * Filters an output string, interpolating variables and replacing ALR's. If
669 * any filtering was done, returns an allocated string that the caller needs
670 * to free; otherwise, return NULL.
671 *
672 * Bundle may be NULL, requesting that the function suppress ALR replacements,
673 * and do only variables; used for game info strings.
674 *
675 * The way Adrift does this is somewhat obscure, but the following seems to
676 * replicate it well enough for most practical purposes (it's unlikely that
677 * any game assumes or relies on anything not covered by this):
678 *
679 * repeat some number of times
680 * repeat some number of times
681 * interpolate variables
682 * repeat [some number of times?]
683 * for each ALR unused so far this pass
684 * search the current string for the ALR original
685 * if found
686 * replace this ALR in the current string
687 * mark this ALR as used
688 * until no more changes in the current string
689 *
690 */
691 static sc_char *
692 pf_filter_internal (const sc_char *string,
693 sc_var_setref_t vars, sc_prop_setref_t bundle)
694 {
695 sc_int alr_count, iteration;
696 sc_char *current;
697 sc_bool *alr_applied;
698 assert (string && vars);
699
700 if (pf_trace)
701 sc_trace ("Printfilter: initial \"%s\"\n", string);
702
703 /* If including ALRs, create a common set of ALR application flags. */
704 if (bundle)
705 {
706 sc_vartype_t vt_key;
707
708 /* Obtain a count of ALRs. */
709 vt_key.string = "ALRs";
710 alr_count = prop_get_child_count (bundle, "I<-s", &vt_key);
711
712 /*
713 * Create a new set of ALR application flags. These are used to ensure
714 * that a given ALR is applied only once on a given pass. If the game
715 * has no ALRs, don't create a flag set.
716 */
717 if (alr_count > 0)
718 {
719 alr_applied = sc_malloc (alr_count * sizeof (*alr_applied));
720 memset (alr_applied, FALSE, alr_count * sizeof (*alr_applied));
721 }
722 else
723 alr_applied = NULL;
724 }
725 else
726 {
727 /* Not including ALRs, so set alr count to 0, and flags to NULL. */
728 alr_count = 0;
729 alr_applied = NULL;
730 }
731
732 /* Loop for a sort-of arbitrary number of passes; probably enough. */
733 current = NULL;
734 for (iteration = 0; iteration < ITERATION_LIMIT; iteration++)
735 {
736 sc_int inner_iteration;
737 const sc_char *initial;
738 sc_char *intermediate;
739
740 /* Note the initial string, so we can check for no change. */
741 initial = current;
742
743 for (inner_iteration = 0;
744 inner_iteration < ITERATION_LIMIT; inner_iteration++)
745 {
746 /*
747 * Interpolate variables. If any changes were made, advance current
748 * to the interpolated version, and free the old current if required.
749 * Work on the current string, if any, otherwise the input string.
750 */
751 intermediate = pf_interpolate_vars (current ? current : string, vars);
752 if (intermediate)
753 {
754 sc_free (current);
755 current = intermediate;
756 if (pf_trace)
757 {
758 sc_trace ("Printfilter: interpolated [%ld,%ld] \"%s\"\n",
759 iteration, inner_iteration, current);
760 }
761 }
762 else
763 break;
764 }
765
766 /* If we have ALRs to process, search out and replace all findable. */
767 if (alr_count > 0)
768 {
769 /* Replace ALRs until no more ALRs can be found. */
770 inner_iteration = 0;
771 while (TRUE)
772 {
773 /*
774 * Replace ALRs, and advance current as for variables above.
775 * Leave the loop when ALR replacements stop. Again, work on
776 * the current string if any, otherwise the input string.
777 */
778 intermediate = pf_replace_alrs (current ? current : string,
779 bundle, alr_applied, alr_count);
780 if (intermediate)
781 {
782 sc_free (current);
783 current = intermediate;
784 if (pf_trace)
785 {
786 sc_trace ("Printfilter: replaced [%ld,%ld] \"%s\"\n",
787 iteration, inner_iteration, current);
788 }
789 }
790 else
791 break;
792 inner_iteration++;
793 }
794 }
795
796 /* If nothing changed this iteration, stop now. */
797 if (current == initial)
798 break;
799 }
800
801 /* Free any ALR application flags, and return current, NULL if no change. */
802 sc_free (alr_applied);
803 return current;
804 }
805
806
807 /*
808 * pf_filter()
809 *
810 * A facet of pf_filter_internal(). Filter an output string, interpolating
811 * variables and replacing ALR's. Returns an allocated string that the caller
812 * needs to free.
813 */
814 sc_char *
815 pf_filter (const sc_char *string,
816 sc_var_setref_t vars, sc_prop_setref_t bundle)
817 {
818 sc_char *current;
819
820 /* Filter this string, including ALRs replacements. */
821 current = pf_filter_internal (string, vars, bundle);
822
823 /* Our contract is to return an allocated string; copy if required. */
824 if (!current)
825 {
826 current = sc_malloc (strlen (string) + 1);
827 strcpy (current, string);
828 }
829
830 return current;
831 }
832
833
834 /*
835 * pf_filter_for_info()
836 *
837 * A facet of pf_filter_internal(). Filters output, interpolating variables
838 * only (no ALR replacement), and returns the resulting string to the caller.
839 * Used on informational strings such as the game title and author. Returns
840 * an allocated string that the caller needs to free.
841 */
842 sc_char *
843 pf_filter_for_info (const sc_char *string, sc_var_setref_t vars)
844 {
845 sc_char *current;
846
847 /* Filter this string, excluding ALRs replacements. */
848 current = pf_filter_internal (string, vars, NULL);
849
850 /* Our contract is to return an allocated string; copy if required. */
851 if (!current)
852 {
853 current = sc_malloc (strlen (string) + 1);
854 strcpy (current, string);
855 }
856
857 return current;
858 }
859
860
861 /*
862 * pf_flush()
863 *
864 * Filter buffered data, interpolating variables and replacing ALR's, and
865 * send the resulting string to the output channel.
866 */
867 void
868 pf_flush (sc_filterref_t filter,
869 sc_var_setref_t vars, sc_prop_setref_t bundle)
870 {
871 assert (pf_is_valid (filter));
872 assert (vars && bundle);
873
874 /* See if there is any buffered data to flush. */
875 if (filter->buffer_length > 0)
876 {
877 /*
878 * Filter the buffered string, then print it untagged. Remember to free
879 * the filtered version. If filtering made no difference, or if the
880 * buffer was already filtered by, say, checkpointing, just print the
881 * original buffer untagged instead.
882 */
883 if (filter->needs_filtering)
884 {
885 sc_char *filtered;
886
887 filtered = pf_filter_internal (filter->buffer, vars, bundle);
888 if (filtered)
889 {
890 pf_output_untagged (filtered);
891 sc_free (filtered);
892 }
893 else
894 pf_output_untagged (filter->buffer);
895 }
896 else
897 pf_output_untagged (filter->buffer);
898
899 /* Remove buffered data by resetting length to zero. */
900 filter->buffer_length = 0;
901 filter->needs_filtering = FALSE;
902 }
903
904 /* Reset new sentence and mute flags. */
905 filter->new_sentence = FALSE;
906 filter->is_muted = FALSE;
907 }
908
909
910 /*
911 * pf_append_string()
912 *
913 * Append a string to the filter buffer.
914 */
915 static void
916 pf_append_string (sc_filterref_t filter, const sc_char *string)
917 {
918 sc_int length, required;
919
920 /*
921 * Calculate the required buffer size to append string. Remember to add
922 * one for the terminating NUL.
923 */
924 length = strlen (string);
925 required = filter->buffer_length + length + 1;
926
927 /* If this is more than the current buffer allocation, resize it. */
928 if (required > filter->buffer_allocation)
929 {
930 sc_int new_allocation;
931
932 /* Calculate the new malloc size, in increment chunks. */
933 new_allocation = ((required + BUFFER_GROW_INCREMENT - 1)
934 / BUFFER_GROW_INCREMENT) * BUFFER_GROW_INCREMENT;
935
936 /* Grow the buffer. */
937 filter->buffer = sc_realloc (filter->buffer, new_allocation);
938 filter->buffer_allocation = new_allocation;
939 }
940
941 /* If empty, put a NUL into the buffer to permit strcat. */
942 if (filter->buffer_length == 0)
943 filter->buffer[0] = NUL;
944
945 /* Append the string to the buffer and extend length. */
946 strcat (filter->buffer, string);
947 filter->buffer_length += length;
948 }
949
950
951 /*
952 * pf_checkpoint()
953 *
954 * Filter buffered data, interpolating variables and replacing ALR's, and
955 * store the result back in the buffer. This allows a string to be inter-
956 * polated in between main flushes; used to update buffered text with variable
957 * values before those values are updated by task actions.
958 */
959 void
960 pf_checkpoint (sc_filterref_t filter,
961 sc_var_setref_t vars, sc_prop_setref_t bundle)
962 {
963 assert (pf_is_valid (filter));
964 assert (vars && bundle);
965
966 /* See if there is any buffered data to filter. */
967 if (filter->buffer_length > 0)
968 {
969 /*
970 * Filter the buffered string, and place the filtered result, if any,
971 * back into the filter buffer. We do this by setting the buffer length
972 * back to zero, then appending the filtered string; this keeps the
973 * grown buffer intact.
974 */
975 if (filter->needs_filtering)
976 {
977 sc_char *filtered;
978
979 filtered = pf_filter_internal (filter->buffer, vars, bundle);
980 if (filtered)
981 {
982 filter->buffer_length = 0;
983 pf_append_string (filter, filtered);
984 sc_free (filtered);
985 }
986 }
987
988 /* Note the buffer as filtered, to avoid pointless filtering. */
989 filter->needs_filtering = FALSE;
990 }
991 }
992
993
994 /*
995 * pf_get_buffer()
996 * pf_transfer_buffer()
997 *
998 * Return the raw, unfiltered, buffered text. Returns NULL if no buffered
999 * data available. Transferring the buffer transfers ownership of the buffer
1000 * string to the caller, who is then responsible for freeing it.
1001 *
1002 * The second function is an optimization to avoid allocations and copying
1003 * in client code.
1004 */
1005 const sc_char *
1006 pf_get_buffer (sc_filterref_t filter)
1007 {
1008 assert (pf_is_valid (filter));
1009
1010 /*
1011 * Return buffer if filter length is greater than zero. Note that this
1012 * assumes that the buffer is a nul-terminated string.
1013 */
1014 if (filter->buffer_length > 0)
1015 {
1016 assert (filter->buffer[filter->buffer_length] == NUL);
1017 return filter->buffer;
1018 }
1019 else
1020 return NULL;
1021 }
1022
1023 sc_char *
1024 pf_transfer_buffer (sc_filterref_t filter)
1025 {
1026 assert (pf_is_valid (filter));
1027
1028 /*
1029 * If the filter length is greater than zero, pass out the buffer (a nul-
1030 * terminated string) and zero our length, allocation, and set the buffer
1031 * back to NULL; an empty in all except the free-ing.
1032 */
1033 if (filter->buffer_length > 0)
1034 {
1035 sc_char *retval;
1036
1037 /* Set the return value to be the buffered text. */
1038 assert (filter->buffer[filter->buffer_length] == NUL);
1039 retval = filter->buffer;
1040
1041 /* Clear all filter fields down to empty values. */
1042 filter->buffer_length = 0;
1043 filter->buffer_allocation = 0;
1044 filter->buffer = NULL;
1045 filter->new_sentence = FALSE;
1046 filter->is_muted = FALSE;
1047 filter->needs_filtering = FALSE;
1048
1049 /* Return the allocated buffered text. */
1050 return retval;
1051 }
1052 else
1053 return NULL;
1054 }
1055
1056
1057 /*
1058 * pf_empty()
1059 *
1060 * Empty any text currently buffered in the filter.
1061 */
1062 void
1063 pf_empty (sc_filterref_t filter)
1064 {
1065 assert (pf_is_valid (filter));
1066
1067 /* Free any allocation, and return the filter to initialization state. */
1068 filter->buffer_length = 0;
1069 filter->buffer_allocation = 0;
1070 sc_free (filter->buffer);
1071 filter->buffer = NULL;
1072 filter->new_sentence = FALSE;
1073 filter->is_muted = FALSE;
1074 filter->needs_filtering = FALSE;
1075 }
1076
1077
1078 /*
1079 * pf_buffer_string()
1080 * pf_buffer_character()
1081 *
1082 * Add a string, and a single character, to the printfilter buffer. If muted,
1083 * these functions do nothing.
1084 */
1085 void
1086 pf_buffer_string (sc_filterref_t filter, const sc_char *string)
1087 {
1088 assert (pf_is_valid (filter));
1089 assert (string);
1090
1091 /* Ignore the call if the printfilter is muted. */
1092 if (!filter->is_muted)
1093 {
1094 sc_int noted;
1095
1096 /* Note append start, then append the string to the buffer. */
1097 noted = filter->buffer_length;
1098 pf_append_string (filter, string);
1099
1100 /* Adjust the first character of the appended string if flagged. */
1101 if (filter->new_sentence)
1102 filter->buffer[noted] = sc_toupper (filter->buffer[noted]);
1103
1104 /* Clear new sentence, and note as currently needing filtering. */
1105 filter->needs_filtering = TRUE;
1106 filter->new_sentence = FALSE;
1107 }
1108 }
1109
1110 void
1111 pf_buffer_character (sc_filterref_t filter, sc_char character)
1112 {
1113 sc_char buffer[2];
1114 assert (pf_is_valid (filter));
1115
1116 buffer[0] = character;
1117 buffer[1] = NUL;
1118 pf_buffer_string (filter, buffer);
1119 }
1120
1121
1122 /*
1123 * pf_prepend_string()
1124 *
1125 * Add a string to the front of the printfilter buffer, rather than to the
1126 * end. Generally less efficient than an append, these are for use by task
1127 * running code, which needs to run task actions and then prepend the task's
1128 * completion text. If muted, this function does nothing.
1129 */
1130 void
1131 pf_prepend_string (sc_filterref_t filter, const sc_char *string)
1132 {
1133 assert (pf_is_valid (filter));
1134 assert (string);
1135
1136 /* Ignore the call if the printfilter is muted. */
1137 if (!filter->is_muted)
1138 {
1139 if (filter->buffer_length > 0)
1140 {
1141 sc_char *copy;
1142
1143 /* Take a copy of the current buffered string. */
1144 assert (filter->buffer[filter->buffer_length] == NUL);
1145 copy = sc_malloc (filter->buffer_length + 1);
1146 strcpy (copy, filter->buffer);
1147
1148 /*
1149 * Now restart buffering with the input string passed in. Removing
1150 * the current content by zeroing the length preserves the grown
1151 * allocation of the main buffer.
1152 */
1153 filter->buffer_length = 0;
1154 pf_append_string (filter, string);
1155
1156 /* Append the string saved above and then free it. */
1157 pf_append_string (filter, copy);
1158 sc_free (copy);
1159
1160 /* Adjust the first character of the prepended string if flagged. */
1161 if (filter->new_sentence)
1162 filter->buffer[0] = sc_toupper (filter->buffer[0]);
1163
1164 /* Clear new sentence, and note as currently needing filtering. */
1165 filter->needs_filtering = TRUE;
1166 filter->new_sentence = FALSE;
1167 }
1168 else
1169 /* No data, so the call is equivalent to a normal buffer. */
1170 pf_buffer_string (filter, string);
1171 }
1172 }
1173
1174
1175 /*
1176 * pf_new_sentence()
1177 *
1178 * Tells the printfilter to force the next non-space character to uppercase.
1179 * Ignored if the printfilter is muted.
1180 */
1181 void
1182 pf_new_sentence (sc_filterref_t filter)
1183 {
1184 assert (pf_is_valid (filter));
1185
1186 if (!filter->is_muted)
1187 filter->new_sentence = TRUE;
1188 }
1189
1190
1191 /*
1192 * pf_mute()
1193 * pf_clear_mute()
1194 *
1195 * A muted printfilter ignores all new text additions.
1196 */
1197 void
1198 pf_mute (sc_filterref_t filter)
1199 {
1200 assert (pf_is_valid (filter));
1201
1202 filter->is_muted = TRUE;
1203 }
1204
1205 void
1206 pf_clear_mute (sc_filterref_t filter)
1207 {
1208 assert (pf_is_valid (filter));
1209
1210 filter->is_muted = FALSE;
1211 }
1212
1213
1214 /*
1215 * pf_buffer_tag()
1216 *
1217 * Insert an HTML-like tag into the buffered output data. The call is ignored
1218 * if the printfilter is muted.
1219 */
1220 void
1221 pf_buffer_tag (sc_filterref_t filter, sc_int tag)
1222 {
1223 const sc_html_tags_t *entry;
1224 assert (pf_is_valid (filter));
1225
1226 /* Search the tags table for this tag. */
1227 for (entry = HTML_TAGS_TABLE; entry->name; entry++)
1228 {
1229 if (tag == entry->tag)
1230 break;
1231 }
1232
1233 /* If found, output the equivalent string, enclosed in '<>' characters. */
1234 if (entry->name)
1235 {
1236 pf_buffer_character (filter, LESSTHAN);
1237 pf_buffer_string (filter, entry->name);
1238 pf_buffer_character (filter, GREATERTHAN);
1239 }
1240 else
1241 sc_error ("pf_buffer_tag: invalid tag, %ld\n", tag);
1242 }
1243
1244
1245 /*
1246 * pf_strip_tags_common()
1247 *
1248 * Strip HTML-like tags from a string. Used to process strings used in ways
1249 * other than being passed to if_print_string(), for example room names and
1250 * status lines. It ignores all tags except <br>, which it replaces with
1251 * a newline if requested by allow_newlines.
1252 */
1253 static void
1254 pf_strip_tags_common (sc_char *string, sc_bool allow_newlines)
1255 {
1256 sc_char *marker, *cursor;
1257
1258 /* Run through the string looking for <...> tags. */
1259 marker = string;
1260 for (cursor = strchr (marker, LESSTHAN);
1261 cursor; cursor = strchr (marker, LESSTHAN))
1262 {
1263 sc_char *tag_end;
1264
1265 /* Locate tag end, and break if unterminated. */
1266 tag_end = strchr (cursor, GREATERTHAN);
1267 if (!tag_end)
1268 break;
1269
1270 /* If the tag is <br>, replace with newline if requested. */
1271 if (allow_newlines)
1272 {
1273 if (tag_end - cursor == 3
1274 && sc_strncasecmp (cursor + 1, "br", 2) == 0)
1275 *cursor++ = '\n';
1276 }
1277
1278 /* Remove the tag from the string, then advance input. */
1279 memmove (cursor, tag_end + 1, strlen (tag_end));
1280 marker = cursor;
1281 }
1282 }
1283
1284
1285 /*
1286 * pf_strip_tags()
1287 * pf_strip_tags_for_hints()
1288 *
1289 * Public interfaces to pf_strip_tags_common(). The hints version will
1290 * allow <br> tags to map into newlines in hints strings.
1291 */
1292 void
1293 pf_strip_tags (sc_char *string)
1294 {
1295 pf_strip_tags_common (string, FALSE);
1296 }
1297
1298 void
1299 pf_strip_tags_for_hints (sc_char *string)
1300 {
1301 pf_strip_tags_common (string, TRUE);
1302 }
1303
1304
1305 /*
1306 * pf_escape()
1307 *
1308 * Escape <, >, and % characters in the input string. Used to filter player
1309 * input prior to storing in referenced text.
1310 *
1311 * Adrift offers no escapes for & and + escapes, so for these we convert to
1312 * the character itself followed by a space. The return string is malloc'ed,
1313 * so the caller needs to remember to free it.
1314 */
1315 sc_char *
1316 pf_escape (const sc_char *string)
1317 {
1318 const sc_char *marker, *cursor;
1319 sc_char *buffer;
1320
1321 /* Start with an empty return buffer. */
1322 buffer = sc_malloc (strlen (string) + 1);
1323 buffer[0] = NUL;
1324
1325 /* Run through the string looking for <, >, %, or other escapes. */
1326 marker = string;
1327 for (cursor = marker + strcspn (marker, ESCAPES);
1328 cursor[0] != NUL; cursor = marker + strcspn (marker, ESCAPES))
1329 {
1330 const sc_char *escape;
1331 sc_char escape_buffer[3];
1332
1333 /* Extend buffer to hold the string so far. */
1334 if (cursor > marker)
1335 {
1336 buffer = sc_realloc (buffer, strlen (buffer) + cursor - marker + 1);
1337 buffer[strlen (buffer) + cursor - marker] = NUL;
1338 memcpy (buffer + strlen (buffer), marker, cursor - marker);
1339 }
1340
1341 /* Determine the appropriate character escape. */
1342 if (cursor[0] == LESSTHAN)
1343 escape = ENTITY_LESSTHAN;
1344 else if (cursor[0] == GREATERTHAN)
1345 escape = ENTITY_GREATERTHAN;
1346 else if (cursor[0] == PERCENT)
1347 escape = ENTITY_PERCENT;
1348 else
1349 {
1350 /*
1351 * No real escape available, so fake, badly, by appending a space
1352 * for cases where we've encountered a character entity; leave
1353 * others untouched.
1354 */
1355 escape_buffer[0] = cursor[0];
1356 if (sc_strncasecmp (cursor,
1357 ENTITY_LESSTHAN, ENTITY_LENGTH) == 0
1358 || sc_strncasecmp (cursor,
1359 ENTITY_GREATERTHAN, ENTITY_LENGTH) == 0
1360 || sc_strncasecmp (cursor,
1361 ENTITY_PERCENT, PERCENT_LENGTH) == 0)
1362 {
1363 escape_buffer[1] = ' ';
1364 escape_buffer[2] = NUL;
1365 }
1366 else
1367 escape_buffer[1] = NUL;
1368 escape = escape_buffer;
1369 }
1370
1371 buffer = sc_realloc (buffer, strlen (buffer) + strlen (escape) + 1);
1372 strcat (buffer, escape);
1373
1374 /* Pass over character escaped and continue. */
1375 cursor++;
1376 marker = cursor;
1377 }
1378
1379 /* Add all remaining characters to the buffer. */
1380 if (cursor > marker)
1381 {
1382 buffer = sc_realloc (buffer, strlen (buffer) + cursor - marker + 1);
1383 buffer[strlen (buffer) + cursor - marker] = NUL;
1384 memcpy (buffer + strlen (buffer), marker, cursor - marker);
1385 }
1386
1387 return buffer;
1388 }
1389
1390
1391 /*
1392 * pf_compare_words()
1393 *
1394 * Matches multiple words from words in string. Returns the extent of
1395 * the match if the string matched, 0 otherwise.
1396 */
1397 static sc_int
1398 pf_compare_words (const sc_char *string, const sc_char *words)
1399 {
1400 sc_int word_posn, posn;
1401
1402 /* None expected, but skip leading space. */
1403 for (word_posn = 0; sc_isspace (words[word_posn]) && words[word_posn] != NUL;)
1404 word_posn++;
1405
1406 /* Match characters from words with the string at position. */
1407 posn = 0;
1408 while (TRUE)
1409 {
1410 /* Any character mismatch means no words match. */
1411 if (sc_tolower (words[word_posn]) != sc_tolower (string[posn]))
1412 return 0;
1413
1414 /* Move to next character in each. */
1415 word_posn++;
1416 posn++;
1417
1418 /*
1419 * If at space, advance over whitespace in words list. Stop when we
1420 * hit the end of the words list.
1421 */
1422 while (sc_isspace (words[word_posn]) && words[word_posn] != NUL)
1423 word_posn++;
1424 if (words[word_posn] == NUL)
1425 break;
1426
1427 /*
1428 * About to match another word, so advance over whitespace in the
1429 * current string too.
1430 */
1431 while (sc_isspace (string[posn]) && string[posn] != NUL)
1432 posn++;
1433 }
1434
1435 /*
1436 * We reached the end of words. If we're at the end of the match string,
1437 * or at spaces, we've matched.
1438 */
1439 if (sc_isspace (string[posn]) || string[posn] == NUL)
1440 return posn;
1441
1442 /* More text after the match, so it's not quite a match. */
1443 return 0;
1444 }
1445
1446
1447 /*
1448 * pf_filter_input()
1449 *
1450 * Applies synonym changes to a player input string, and returns the resulting
1451 * string to the caller, or NULL if no synonym changes were needed. The
1452 * return string is malloc'ed, so the caller needs to remember to free it.
1453 */
1454 sc_char *
1455 pf_filter_input (const sc_char *string, sc_prop_setref_t bundle)
1456 {
1457 sc_vartype_t vt_key[3];
1458 sc_int synonym_count, buffer_allocation;
1459 sc_char *buffer;
1460 const sc_char *current;
1461 assert (string && bundle);
1462
1463 if (pf_trace)
1464 sc_trace ("Printfilter: input \"%s\"\n", string);
1465
1466 /* Obtain a count of synonyms. */
1467 vt_key[0].string = "Synonyms";
1468 synonym_count = prop_get_child_count (bundle, "I<-s", vt_key);
1469
1470 /* Begin with a NULL buffer for lazy allocation. */
1471 buffer_allocation = 0;
1472 buffer = NULL;
1473
1474 /* Loop over each word in the string. */
1475 current = string + strspn (string, WHITESPACE);
1476 while (current[0] != NUL)
1477 {
1478 sc_int index_, extent;
1479
1480 /* Search for a synonym match at this index into the buffer. */
1481 extent = 0;
1482 for (index_ = 0; index_ < synonym_count; index_++)
1483 {
1484 const sc_char *original;
1485
1486 /* Retrieve the synonym original string. */
1487 vt_key[0].string = "Synonyms";
1488 vt_key[1].integer = index_;
1489 vt_key[2].string = "Original";
1490 original = prop_get_string (bundle, "S<-sis", vt_key);
1491
1492 /* Compare the original at this point. */
1493 extent = pf_compare_words (current, original);
1494 if (extent > 0)
1495 break;
1496 }
1497
1498 /*
1499 * If a synonym found was, index_ indicates it, and extent shows how
1500 * much of the buffer to replace with it.
1501 */
1502 if (index_ < synonym_count && extent > 0)
1503 {
1504 const sc_char *replacement;
1505 sc_char *position;
1506 sc_int length, final;
1507
1508 /*
1509 * If not yet allocated, allocate a buffer now, and copy the input
1510 * string into it. Then switch current to the equivalent location
1511 * in the allocated buffer. More basic copy-on-write.
1512 */
1513 if (!buffer)
1514 {
1515 buffer_allocation = strlen (string) + 1;
1516 buffer = sc_malloc (buffer_allocation);
1517 strcpy (buffer, string);
1518 current = buffer + (current - string);
1519 }
1520
1521 /* Find the replacement text for this synonym. */
1522 vt_key[0].string = "Synonyms";
1523 vt_key[1].integer = index_;
1524 vt_key[2].string = "Replacement";
1525 replacement = prop_get_string (bundle, "S<-sis", vt_key);
1526 length = strlen (replacement);
1527
1528 /*
1529 * If necessary, grow the output buffer for the replacement,
1530 * remembering to adjust current for the new buffer allocated.
1531 * At the same time, note the last character index for the move.
1532 */
1533 if (length > extent)
1534 {
1535 sc_int offset;
1536
1537 offset = current - buffer;
1538 buffer_allocation += length - extent;
1539 buffer = sc_realloc (buffer, buffer_allocation);
1540 current = buffer + offset;
1541 final = length;
1542 }
1543 else
1544 final = extent;
1545
1546 /* Insert the replacement string into the buffer. */
1547 position = buffer + (current - buffer);
1548 memmove (position + length,
1549 position + extent,
1550 buffer_allocation - (current - buffer) - final);
1551 memcpy (position, replacement, length);
1552
1553 /* Adjust current to skip over the replacement. */
1554 current += length;
1555
1556 if (pf_trace)
1557 sc_trace ("Printfilter: synonym \"%s\"\n", buffer);
1558 }
1559 else
1560 {
1561 /* If no match, advance current over the unmatched word. */
1562 current += strcspn (current, WHITESPACE);
1563 }
1564
1565 /* Set current to the next word start. */
1566 current += strspn (current, WHITESPACE);
1567 }
1568
1569 /* Return the final string, or NULL if no synonym replacements. */
1570 return buffer;
1571 }
1572
1573
1574 /*
1575 * pf_debug_trace()
1576 *
1577 * Set filter tracing on/off.
1578 */
1579 void
1580 pf_debug_trace (sc_bool flag)
1581 {
1582 pf_trace = flag;
1583 }