commit ac41ddb793934db73f6677bea19f39562ef76aba Author: Gavin Smith Date: Thu Mar 13 16:18:29 2014 +0000 info_utils.c (rewrite_p): New file-level variable. (scan_node_contents): Tag expansion, moved from tag.c:tags_expand. nodes.c (info_node_of_file_buffer_tags): Don't call tags_expand. tags.c (struct tag_handler, find_tag_handler): No longer static. (tags_expand): Function deleted. diff --git a/info-utils.c b/info-utils.c index b7669c8..6c1f7e6 100644 --- a/info-utils.c +++ b/info-utils.c @@ -21,6 +21,8 @@ #include "info.h" #include "info-utils.h" +#include "tag.h" + #if defined (HANDLE_MAN_PAGES) # include "man.h" #endif /* HANDLE_MAN_PAGES */ @@ -445,6 +447,9 @@ printed_representation (const char *character, size_t len, size_t hpos, /* Whether to strip syntax from the text of nodes. */ int preprocess_nodes_p; +/* Whether contents of nodes should be rewritten. */ +static int rewrite_p; + static char *input_start, *inptr; struct text_buffer output_buf; @@ -459,7 +464,7 @@ static long int output_bytes_difference; static void init_output_stream (void) { - if (preprocess_nodes_p) + if (rewrite_p) { text_buffer_init (&output_buf); output_bytes_difference = 0; @@ -467,26 +472,9 @@ init_output_stream (void) } static void -skip_input (size_t n) -{ - inptr += n; - output_bytes_difference += n; -} - -static void -write_extra_bytes_to_output (char *input, size_t n) -{ - if (preprocess_nodes_p) - { - text_buffer_add_string (&output_buf, input, n); - output_bytes_difference -= n; - } -} - -static void copy_input_to_output (size_t n) { - if (preprocess_nodes_p) + if (rewrite_p) { text_buffer_add_string (&output_buf, inptr, n); inptr += n; @@ -514,6 +502,31 @@ copy_input_to_output (size_t n) } } +static void +skip_input (size_t n) +{ + if (preprocess_nodes_p || !rewrite_p) + { + inptr += n; + output_bytes_difference += n; + } + else if (rewrite_p) + { + /* We are expanding tags only. Do not skip output. */ + copy_input_to_output (n); + } +} + +static void +write_extra_bytes_to_output (char *input, size_t n) +{ + if (preprocess_nodes_p) + { + text_buffer_add_string (&output_buf, input, n); + output_bytes_difference -= n; + } +} + /* ANSI escape codes */ #define ANSI_UNDERLINING_OFF "\033[24m" #define ANSI_UNDERLINING_ON "\033[4m" @@ -636,7 +649,13 @@ scan_node_contents (NODE **tag) long position; - init_output_stream (); + if (preprocess_nodes_p) + rewrite_p = 1; + else + rewrite_p = 0; + + if (rewrite_p) + init_output_stream (); /* Set anchor_to_adjust to first anchor in node, if any. */ anchor_to_adjust = tag + 1; @@ -794,7 +813,7 @@ search_again: /* Point reference to where we will put the displayed reference, which could be after whitespace. */ - if (preprocess_nodes_p) + if (rewrite_p) { entry->start = text_buffer_off (&output_buf); } @@ -934,31 +953,74 @@ search_again: if (s.start >= s.end) break; } + { /* Search may have stopped too early because of null byte in index marker ("address@hidden@^H]") or in image marker - ("address@hidden address@hidden"). Skip past these and try again. */ - char *ptr_to_null_byte; + ("address@hidden address@hidden"). Process these and try again. */ + char *p; - ptr_to_null_byte = inptr + strlen (inptr); + /* Forwards to null byte. */ + p = inptr + strlen (inptr); - /* Three byte sequence "address@hidden" starts tag. Check there is enough - space for this in the rest of the node. */ - if (ptr_to_null_byte <= node->contents + node->nodelen - 3) + /* Check there is enough space for opening byte sequence "address@hidden" + in the rest of the node contents and that it appears. */ + if ( (p <= node->contents + node->nodelen - 3) + && (memcmp(p + 1, "\b[", 2) == 0)) { - ptr_to_null_byte += 3; - - /* Output is different for index nodes */ - if (!strcmp ("index", ptr_to_null_byte)) - in_index = 1; + char *q; - /* Go to second null byte */ - ptr_to_null_byte += strlen (ptr_to_null_byte); + p += 3; + q = p + strlen (p); /* forward to next null */ - /* Three byte sequence "address@hidden" ends tag */ - if (ptr_to_null_byte <= node->contents + node->nodelen - 3) + /* Check there is enough space for closing byte sequence "address@hidden" + in the rest of the node contents and that it appears. */ + if ( (q <= node->contents + node->nodelen - 3) + && (memcmp (q + 1, "\b]", 2) == 0)) { - /* Write out up to tag. */ - copy_input_to_output (ptr_to_null_byte + 3 - inptr); + struct tag_handler *tp; + int len; + + /* Output is different for index nodes */ + if (!strcmp ("index", p)) + in_index = 1; + + len = strcspn (p, " \t"); /* tag name */ + tp = find_tag_handler (p, len); + + if (tp) + { + while (p[len] == ' ' || p[len] == '\t') + ++len; /* move past whitespace */ + + if (!rewrite_p) + { + rewrite_p = 1; + init_output_stream (); + + /* Put inptr back to start so that + copy_input_to_output below gets all + preceding contents. */ + inptr = node->contents; + } + + /* Write out up to tag */ + copy_input_to_output (p - inptr - 3); + + if (tp->handler) + { + size_t saved_off = text_buffer_off (&output_buf); + tp->handler (p + len, &output_buf); + + /* This is like a call to write_extra_bytes_to_output + with the output of tp->handler. */ + output_bytes_difference -= + text_buffer_off (&output_buf) - saved_off; + } + } + + /* Like "skip_input (q + 3 - inptr)" if preprocess_nodes_p==1. */ + output_bytes_difference += q + 3 - inptr; + inptr = q + 3; /* Update search to start after tag. */ s.buffer = inptr; @@ -967,6 +1029,12 @@ search_again: goto search_again; } } + else + /* We encountered a null byte, but it did not introduce a valid tag + (image, index, or otherwise). Write out the rest of the node + contents with no further processing. */ + ; + } /* If we haven't accidentally gone past the end of the node, write out the rest of it. */ @@ -974,11 +1042,12 @@ search_again: copy_input_to_output ((node->contents + node->nodelen) - inptr); /* Null to terminate buffer. */ - write_extra_bytes_to_output ("\0", 1); + if (rewrite_p) + text_buffer_add_string (&output_buf, "\0", 1); node->references = refs; - if (preprocess_nodes_p) + if (rewrite_p) { node->contents = text_buffer_base (&output_buf); node->flags &= N_WasRewritten; diff --git a/nodes.c b/nodes.c index f9a57cf..8541c8f 100644 --- a/nodes.c +++ b/nodes.c @@ -1015,10 +1015,10 @@ info_node_of_file_buffer_tags (FILE_BUFFER *file_buffer, char *nodename) char *buff_end; /* TAG->nodestart may be incorrect by a few bytes due to an - incorrect file tag table. Adjust TAG->nodestart to be the - exact address of the start of the node separator which starts - this node, and TAG->contents to be the address of the line - defining this node. */ + incorrect file tag table. Adjust TAG->nodestart to be the + exact address of the start of the node separator which starts + this node, and TAG->contents to be the address of the line + defining this node. */ min = max = DEFAULT_INFO_FUDGE; @@ -1058,26 +1058,8 @@ info_node_of_file_buffer_tags (FILE_BUFFER *file_buffer, char *nodename) /* Read locations of references in node and similar. Rewrite node from tag->contents if preprocess_nodes=On. */ - scan_node_contents (&file_buffer->tags[i]); - /* Disabled - we will expand tags in scan_node_contents. */ -#if 0 - char *old_contents = tag->contents; - - /* Expand eventual \b[...\b] constructs in the contents. */ - - int new_nodelen; - if (tags_expand (tag->contents, tag->nodelen, - &tag->contents, &new_nodelen)) - { - tag->nodelen = new_nodelen; - - /* Free output of parse_node. */ - if (old_contents) - free (old_contents); - } -#endif *node = *tag; } else if (tag->nodelen == 0) /* anchor, return containing node */ diff --git a/tag.c b/tag.c index f2a907d..f3a799b 100644 --- a/tag.c +++ b/tag.c @@ -21,13 +21,6 @@ #include "tag.h" #include "info-utils.h" -struct tag_handler -{ - const char *name; - size_t len; - int (*handler) (char *, struct text_buffer *); -}; - struct info_tag { struct info_tag *next; @@ -191,7 +184,7 @@ static struct tag_handler tagtab[] = { { NULL } }; -static struct tag_handler * +struct tag_handler * find_tag_handler (char *tag, size_t taglen) { struct tag_handler *tp; @@ -202,70 +195,6 @@ find_tag_handler (char *tag, size_t taglen) return NULL; } -/* Expand \b[...\b] constructs in INPUT (of INPUTLEN bytes). If encountered, - put the expanded text into PBUF, store its length in PBUFLEN, and return - 1. Otherwise, don't touch neither of the latter and return 0. */ -int -tags_expand (char *input, size_t inputlen, char **pbuf, size_t *pbuflen) -{ - char *endp = input + inputlen; - struct text_buffer outbuf; - int text_buffer_used = 0; - char *p; - - while ((p = input + strlen (input)) < endp) /* go forward to null */ - { - if (memcmp(p + 1, "\b[", 2) == 0) /* opening magic? */ - { - char *q; - - if (!text_buffer_used) - { - text_buffer_init (&outbuf); - text_buffer_used = 1; - } - - p += 3; - q = p + strlen (p); /* forward to next null */ - if (memcmp (q + 1, "\b]", 2) == 0) /* closing magic? */ - { - size_t len; - struct tag_handler *tp; - - len = strcspn (p, " \t"); /* tag name */ - tp = find_tag_handler (p, len); - if (tp) - { - while (p[len] == ' ' || p[len] == '\t') - ++len; /* move past whitespace */ - - text_buffer_add_string (&outbuf, input, p - input - 3); - if (!tp->handler || tp->handler (p + len, &outbuf) == 0) - { - input = q + 3; - continue; - } - } - } - } - - if (text_buffer_used) - text_buffer_add_string (&outbuf, input, p - input); - - input = p + 1; - } - - if (text_buffer_used && text_buffer_off (&outbuf)) - { - if (input < endp) - text_buffer_add_string (&outbuf, input, endp - input); - *pbuflen = text_buffer_off (&outbuf); - *pbuf = text_buffer_base (&outbuf); - return 1; - } - return 0; -} - void handle_tag (char *tag) { diff --git a/tag.h b/tag.h index 65195b8..f121c23 100644 --- a/tag.h +++ b/tag.h @@ -19,7 +19,16 @@ #ifndef TAG_H #define TAG_H -int tags_expand (char *input, size_t inputlen, char **pbuf, size_t *pbuflen); +#include "info-utils.h" + +struct tag_handler +{ + const char *name; + size_t len; + int (*handler) (char *, struct text_buffer *); +}; + void handle_tag (char *tag); +struct tag_handler *find_tag_handler (char *tag, size_t taglen); #endif