1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096 |
- /* macro.c -- user-defined macros for Texinfo.
- $Id: macro.c,v 1.12 2007-07-01 21:20:32 karl Exp $
- Copyright (C) 1998, 1999, 2002, 2003, 2005, 2007
- Free Software Foundation, Inc.
- 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 3 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, see <http://www.gnu.org/licenses/>. */
- #include "system.h"
- #include "cmds.h"
- #include "files.h"
- #include "macro.h"
- #include "makeinfo.h"
- #include "insertion.h"
- /* If non-NULL, this is an output stream to write the full macro expansion
- of the input text to. The result is another texinfo file, but
- missing @include, @infoinclude, @macro, and macro invocations. Instead,
- all of the text is placed within the file. */
- FILE *macro_expansion_output_stream = NULL;
- /* Output file for -E. */
- char *macro_expansion_filename;
- /* Nonzero means a macro string is in execution, as opposed to a file. */
- int me_executing_string = 0;
- /* Nonzero means we want only to expand macros and
- leave everything else intact. */
- int only_macro_expansion = 0;
- static ITEXT **itext_info = NULL;
- static int itext_size = 0;
- /* Return the arglist on the current line. This can behave in two different
- ways, depending on the variable BRACES_REQUIRED_FOR_MACRO_ARGS. */
- int braces_required_for_macro_args = 0;
- /* Array of macros and definitions. */
- MACRO_DEF **macro_list = NULL;
- int macro_list_len = 0; /* Number of elements. */
- int macro_list_size = 0; /* Number of slots in total. */
- /* Return the length of the array in ARRAY. */
- int
- array_len (char **array)
- {
- int i = 0;
- if (array)
- for (i = 0; array[i]; i++);
- return i;
- }
- void
- free_array (char **array)
- {
- if (array)
- {
- int i;
- for (i = 0; array[i]; i++)
- free (array[i]);
- free (array);
- }
- }
- /* Return the macro definition of NAME or NULL if NAME is not defined. */
- MACRO_DEF *
- find_macro (char *name)
- {
- int i;
- MACRO_DEF *def;
- def = NULL;
- for (i = 0; macro_list && (def = macro_list[i]); i++)
- {
- if ((!def->inhibited) && (strcmp (def->name, name) == 0))
- break;
- }
- return def;
- }
- /* Add the macro NAME with ARGLIST and BODY to the list of defined macros.
- SOURCE_FILE is the name of the file where this definition can be found,
- and SOURCE_LINENO is the line number within that file. If a macro already
- exists with NAME, then a warning is produced, and that previous
- definition is overwritten. */
- static void
- add_macro (char *name, char **arglist, char *body, char *source_file,
- int source_lineno, int flags)
- {
- MACRO_DEF *def;
- def = find_macro (name);
- if (!def)
- {
- if (macro_list_len + 2 >= macro_list_size)
- macro_list = xrealloc
- (macro_list, ((macro_list_size += 10) * sizeof (MACRO_DEF *)));
- macro_list[macro_list_len] = xmalloc (sizeof (MACRO_DEF));
- macro_list[macro_list_len + 1] = NULL;
- def = macro_list[macro_list_len];
- macro_list_len += 1;
- def->name = name;
- }
- else
- {
- char *temp_filename = input_filename;
- int temp_line = line_number;
- warning (_("macro `%s' previously defined"), name);
- input_filename = def->source_file;
- line_number = def->source_lineno;
- warning (_("here is the previous definition of `%s'"), name);
- input_filename = temp_filename;
- line_number = temp_line;
- if (def->arglist)
- {
- int i;
- for (i = 0; def->arglist[i]; i++)
- free (def->arglist[i]);
- free (def->arglist);
- }
- free (def->source_file);
- free (def->body);
- }
- def->source_file = xstrdup (source_file);
- def->source_lineno = source_lineno;
- def->body = body;
- def->arglist = arglist;
- def->argcount = array_len (arglist);
- def->inhibited = 0;
- def->flags = flags;
- }
- char **
- get_brace_args (enum quote_type quote)
- {
- char **arglist, *word;
- int arglist_index, arglist_size;
- int character, escape_seen, start;
- int depth = 1;
- /* There is an arglist in braces here, so gather the args inside of it. */
- skip_whitespace_and_newlines ();
- input_text_offset++;
- arglist = NULL;
- arglist_index = arglist_size = 0;
- get_arg:
- skip_whitespace_and_newlines ();
- start = input_text_offset;
- escape_seen = 0;
- while ((character = curchar ()))
- {
- if (character == '\\')
- {
- input_text_offset += 2;
- escape_seen = 1;
- }
- else if (character == '{')
- {
- depth++;
- input_text_offset++;
- }
- else if ((character == ','
- && !(quote == quote_single
- || (quote == quote_many && depth > 1)))
- || ((character == '}') && depth == 1))
- {
- int len = input_text_offset - start;
- if (len || (character != '}'))
- {
- word = xmalloc (1 + len);
- memcpy (word, input_text + start, len);
- word[len] = 0;
- /* Clean up escaped characters. */
- if (escape_seen)
- {
- int i;
- for (i = 0; word[i]; i++)
- if (word[i] == '\\')
- memmove (word + i, word + i + 1,
- 1 + strlen (word + i + 1));
- }
- if (arglist_index + 2 >= arglist_size)
- arglist = xrealloc
- (arglist, (arglist_size += 10) * sizeof (char *));
- arglist[arglist_index++] = word;
- arglist[arglist_index] = NULL;
- }
- input_text_offset++;
- if (character == '}')
- break;
- else
- goto get_arg;
- }
- else if (character == '}')
- {
- depth--;
- input_text_offset++;
- }
- else
- {
- input_text_offset++;
- if (character == '\n') line_number++;
- }
- }
- return arglist;
- }
- static char **
- get_macro_args (MACRO_DEF *def)
- {
- int i;
- char *word;
- /* Quickly check to see if this macro has been invoked with any arguments.
- If not, then don't skip any of the following whitespace. */
- for (i = input_text_offset; i < input_text_length; i++)
- if (!cr_or_whitespace (input_text[i]))
- break;
- if (input_text[i] != '{')
- {
- if (braces_required_for_macro_args)
- {
- return NULL;
- }
- else
- {
- /* Braces are not required to fill out the macro arguments. If
- this macro takes one argument, it is considered to be the
- remainder of the line, sans whitespace. */
- if (def->arglist && def->arglist[0] && !def->arglist[1])
- {
- char **arglist;
- get_rest_of_line (0, &word);
- if (input_text_offset > 0
- && input_text[input_text_offset - 1] == '\n')
- {
- input_text_offset--;
- line_number--;
- }
- /* canon_white (word); */
- arglist = xmalloc (2 * sizeof (char *));
- arglist[0] = word;
- arglist[1] = NULL;
- return arglist;
- }
- else
- {
- /* The macro either took no arguments, or took more than
- one argument. In that case, it must be invoked with
- arguments surrounded by braces. */
- return NULL;
- }
- }
- }
- return get_brace_args (def->argcount == 1 ? quote_single : quote_many);
- }
- /* Substitute actual parameters for named parameters in body.
- The named parameters which appear in BODY must by surrounded
- reverse slashes, as in \foo\. */
- static char *
- apply (char **named, char **actuals, char *body)
- {
- int i;
- int new_body_index, new_body_size;
- char *new_body, *text;
- int length_of_actuals;
- length_of_actuals = array_len (actuals);
- new_body_size = strlen (body);
- new_body = xmalloc (1 + new_body_size);
- /* Copy chars from BODY into NEW_BODY. */
- i = 0;
- new_body_index = 0;
- while (body[i])
- { /* Anything but a \ is easy. */
- if (body[i] != '\\')
- new_body[new_body_index++] = body[i++];
- else
- { /* Snarf parameter name, check against named parameters. */
- char *param;
- int param_start, len;
- param_start = ++i;
- while (body[i] && body[i] != '\\')
- i++;
- len = i - param_start;
- param = xmalloc (1 + len);
- memcpy (param, body + param_start, len);
- param[len] = 0;
- if (body[i]) /* move past \ */
- i++;
- if (len == 0)
- { /* \\ always means \, even if macro has no args. */
- len++;
- text = xmalloc (1 + len);
- sprintf (text, "\\%s", param);
- }
- else
- {
- int which;
-
- /* Check against named parameters. */
- for (which = 0; named && named[which]; which++)
- if (STREQ (named[which], param))
- break;
- if (named && named[which])
- {
- text = which < length_of_actuals ? actuals[which] : NULL;
- if (!text)
- text = "";
- len = strlen (text);
- text = xstrdup (text); /* so we can free it */
- }
- else
- { /* not a parameter, so it's an error. */
- warning (_("\\ in macro expansion followed by `%s' instead of parameter name"),
- param);
- len++;
- text = xmalloc (1 + len);
- sprintf (text, "\\%s", param);
- }
- }
- if (strlen (param) + 2 < len)
- {
- new_body_size += len + 1;
- new_body = xrealloc (new_body, new_body_size);
- }
- free (param);
- strcpy (new_body + new_body_index, text);
- new_body_index += len;
- free (text);
- }
- }
- new_body[new_body_index] = 0;
- return new_body;
- }
- /* Expand macro passed in DEF, a pointer to a MACRO_DEF, and
- return its expansion as a string. */
- char *
- expand_macro (MACRO_DEF *def)
- {
- char **arglist;
- char *execution_string = NULL;
- int start_line = line_number;
- /* Gather the arguments present on the line if there are any. */
- arglist = get_macro_args (def);
- if (def->argcount < array_len (arglist))
- {
- free_array (arglist);
- line_error (_("Macro `%s' called on line %d with too many args"),
- def->name, start_line);
- return execution_string;
- }
- if (def->body)
- execution_string = apply (def->arglist, arglist, def->body);
- free_array (arglist);
- return execution_string;
- }
- /* Execute the macro passed in DEF, a pointer to a MACRO_DEF. */
- void
- execute_macro (MACRO_DEF *def)
- {
- char *execution_string;
- int start_line = line_number, end_line;
- if (macro_expansion_output_stream && !executing_string && !me_inhibit_expansion)
- me_append_before_this_command ();
- execution_string = expand_macro (def);
- if (!execution_string)
- return;
- if (def->body)
- {
- /* Reset the line number to where the macro arguments began.
- This makes line numbers reported in error messages correct in
- case the macro arguments span several lines and the expanded
- arguments invoke other commands. */
- end_line = line_number;
- line_number = start_line;
- if (macro_expansion_output_stream
- && !executing_string && !me_inhibit_expansion)
- {
- remember_itext (input_text, input_text_offset);
- me_execute_string (execution_string);
- }
- else
- execute_string ("%s", execution_string);
- free (execution_string);
- line_number = end_line;
- }
- }
- /* Read and remember the definition of a macro. If RECURSIVE is set,
- set the ME_RECURSE flag. MACTYPE is either "macro" or "rmacro", and
- tells us what the matching @end should be. */
- static void
- define_macro (char *mactype, int recursive)
- {
- int i, start;
- char *name, *line;
- char *last_end = NULL;
- char *body = NULL;
- char **arglist = NULL;
- int body_size = 0, body_index = 0;
- int depth = 1;
- int flags = 0;
- int defining_line = line_number;
- if (macro_expansion_output_stream && !executing_string)
- me_append_before_this_command ();
- skip_whitespace ();
- /* Get the name of the macro. This is the set of characters which are
- not whitespace and are not `{' immediately following the @macro. */
- start = input_text_offset;
- {
- int len;
- for (i = start; i < input_text_length && input_text[i] != '{'
- && !cr_or_whitespace (input_text[i]);
- i++) ;
- len = i - start;
- name = xmalloc (1 + len);
- memcpy (name, input_text + start, len);
- name[len] = 0;
- input_text_offset = i;
- }
- skip_whitespace ();
- /* It is not required that the definition of a macro includes an arglist.
- If not, don't try to get the named parameters, just use a null list. */
- if (curchar () == '{')
- {
- int character;
- int arglist_index = 0, arglist_size = 0;
- int gathering_words = 1;
- char *word = NULL;
- /* Read the words inside of the braces which determine the arglist.
- These words will be replaced within the body of the macro at
- execution time. */
- input_text_offset++;
- skip_whitespace_and_newlines ();
- while (gathering_words)
- {
- int len;
- for (i = input_text_offset;
- (character = input_text[i]);
- i++)
- {
- switch (character)
- {
- case '\n':
- line_number++;
- case ' ':
- case '\t':
- case ',':
- case '}':
- /* Found the end of the current arglist word. Save it. */
- len = i - input_text_offset;
- word = xmalloc (1 + len);
- memcpy (word, input_text + input_text_offset, len);
- word[len] = 0;
- input_text_offset = i;
- /* Advance to the comma or close-brace that signified
- the end of the argument. */
- while ((character = curchar ())
- && character != ','
- && character != '}')
- {
- input_text_offset++;
- if (character == '\n')
- line_number++;
- }
- /* Add the word to our list of words. */
- if (arglist_index + 2 >= arglist_size)
- {
- arglist_size += 10;
- arglist = xrealloc (arglist,
- arglist_size * sizeof (char *));
- }
- arglist[arglist_index++] = word;
- arglist[arglist_index] = NULL;
- break;
- }
- if (character == '}')
- {
- input_text_offset++;
- gathering_words = 0;
- break;
- }
- if (character == ',')
- {
- input_text_offset++;
- skip_whitespace_and_newlines ();
- i = input_text_offset - 1;
- }
- }
- }
- }
- /* Read the text carefully until we find an "@end macro" which
- matches this one. The text in between is the body of the macro. */
- skip_whitespace_and_newlines ();
- while (depth)
- {
- if ((input_text_offset + 9) > input_text_length)
- {
- file_line_error (input_filename, defining_line,
- _("%cend macro not found"), COMMAND_PREFIX);
- return;
- }
- get_rest_of_line (0, &line);
- /* Handle commands only meaningful within a macro. */
- if ((*line == COMMAND_PREFIX) && (depth == 1) &&
- (strncmp (line + 1, "allow-recursion", 15) == 0) &&
- (line[16] == 0 || whitespace (line[16])))
- {
- warning (_("@allow-recursion is deprecated; please use @rmacro instead"));
- for (i = 16; whitespace (line[i]); i++);
- strcpy (line, line + i);
- flags |= ME_RECURSE;
- if (!*line)
- {
- free (line);
- continue;
- }
- }
- if ((*line == COMMAND_PREFIX) && (depth == 1) &&
- (strncmp (line + 1, "quote-arg", 9) == 0) &&
- (line[10] == 0 || whitespace (line[10])))
- {
- warning (_("@quote-arg is deprecated; arguments are quoted by default"));
- for (i = 10; whitespace (line[i]); i++);
- strcpy (line, line + i);
- if (!*line)
- {
- free (line);
- continue;
- }
- }
- if (*line == COMMAND_PREFIX
- && (strncmp (line + 1, "macro ", 6) == 0
- || strncmp (line + 1, "rmacro ", 7) == 0))
- depth++;
- /* Incorrect implementation of nesting -- just check that the last
- @end matches what we started with. Since nested macros don't
- work in TeX anyway, this isn't worth the trouble to get right. */
- if (*line == COMMAND_PREFIX && strncmp (line + 1, "end macro", 9) == 0)
- {
- depth--;
- last_end = "macro";
- }
- if (*line == COMMAND_PREFIX && strncmp (line + 1, "end rmacro", 10) == 0)
- {
- depth--;
- last_end = "rmacro";
- }
- if (depth)
- {
- if ((body_index + strlen (line) + 3) >= body_size)
- body = xrealloc (body, body_size += 3 + strlen (line));
- strcpy (body + body_index, line);
- body_index += strlen (line);
- body[body_index++] = '\n';
- body[body_index] = 0;
- }
- free (line);
- }
- /* Check that @end matched the macro command. */
- if (!STREQ (last_end, mactype))
- warning (_("mismatched @end %s with @%s"), last_end, mactype);
- /* If it was an empty macro like
- @macro foo
- @end macro
- create an empty body. (Otherwise, the macro is not expanded.) */
- if (!body)
- {
- body = (char *)malloc(1);
- *body = 0;
- }
- /* We now have the name, the arglist, and the body. However, BODY
- includes the final newline which preceded the `@end macro' text.
- Delete it. */
- if (body && strlen (body))
- body[strlen (body) - 1] = 0;
- if (recursive)
- flags |= ME_RECURSE;
-
- add_macro (name, arglist, body, input_filename, defining_line, flags);
- if (macro_expansion_output_stream && !executing_string)
- {
- /* Remember text for future expansions. */
- remember_itext (input_text, input_text_offset);
- /* Bizarrely, output the @macro itself. This is so texinfo.tex
- will have a chance to read it when texi2dvi calls makeinfo -E.
- The problem is that we don't really expand macros in all
- contexts; a @table's @item is one. And a fix is not obvious to
- me, since it appears virtually identical to any other internal
- expansion. Just setting a variable in cm_item caused other
- strange expansion problems. */
- write_region_to_macro_output ("@", 0, 1);
- write_region_to_macro_output (mactype, 0, strlen (mactype));
- write_region_to_macro_output (" ", 0, 1);
- write_region_to_macro_output (input_text, start, input_text_offset);
- }
- }
- void
- cm_macro (void)
- {
- define_macro ("macro", 0);
- }
- void
- cm_rmacro (void)
- {
- define_macro ("rmacro", 1);
- }
- /* Delete the macro with name NAME. The macro is deleted from the list,
- but it is also returned. If there was no macro defined, NULL is
- returned. */
- static MACRO_DEF *
- delete_macro (char *name)
- {
- int i;
- MACRO_DEF *def;
- def = NULL;
- for (i = 0; macro_list && (def = macro_list[i]); i++)
- if (strcmp (def->name, name) == 0)
- {
- memmove (macro_list + i, macro_list + i + 1,
- ((macro_list_len + 1) - i) * sizeof (MACRO_DEF *));
- macro_list_len--;
- break;
- }
- return def;
- }
- void
- cm_unmacro (void)
- {
- int i;
- char *line, *name;
- MACRO_DEF *def;
- if (macro_expansion_output_stream && !executing_string)
- me_append_before_this_command ();
- get_rest_of_line (0, &line);
- for (i = 0; line[i] && !whitespace (line[i]); i++);
- name = xmalloc (i + 1);
- memcpy (name, line, i);
- name[i] = 0;
- def = delete_macro (name);
- if (def)
- {
- free (def->source_file);
- free (def->name);
- free (def->body);
- if (def->arglist)
- {
- int i;
- for (i = 0; def->arglist[i]; i++)
- free (def->arglist[i]);
- free (def->arglist);
- }
- free (def);
- }
- free (line);
- free (name);
- if (macro_expansion_output_stream && !executing_string)
- remember_itext (input_text, input_text_offset);
- }
- /* How to output sections of the input file verbatim. */
- /* Set the value of POINTER's offset to OFFSET. */
- ITEXT *
- remember_itext (char *pointer, int offset)
- {
- int i;
- ITEXT *itext = NULL;
- /* If we have no info, initialize a blank list. */
- if (!itext_info)
- {
- itext_info = xmalloc ((itext_size = 10) * sizeof (ITEXT *));
- for (i = 0; i < itext_size; i++)
- itext_info[i] = NULL;
- }
- /* If the pointer is already present in the list, then set the offset. */
- for (i = 0; i < itext_size; i++)
- if ((itext_info[i]) &&
- (itext_info[i]->pointer == pointer))
- {
- itext = itext_info[i];
- itext_info[i]->offset = offset;
- break;
- }
- if (i == itext_size)
- {
- /* Find a blank slot (or create a new one), and remember the
- pointer and offset. */
- for (i = 0; i < itext_size; i++)
- if (itext_info[i] == NULL)
- break;
- /* If not found, then add some slots. */
- if (i == itext_size)
- {
- int j;
- itext_info = xrealloc
- (itext_info, (itext_size += 10) * sizeof (ITEXT *));
- for (j = i; j < itext_size; j++)
- itext_info[j] = NULL;
- }
- /* Now add the pointer and the offset. */
- itext_info[i] = xmalloc (sizeof (ITEXT));
- itext_info[i]->pointer = pointer;
- itext_info[i]->offset = offset;
- itext = itext_info[i];
- }
- return itext;
- }
- /* Forget the input text associated with POINTER. */
- void
- forget_itext (char *pointer)
- {
- int i;
- for (i = 0; i < itext_size; i++)
- if (itext_info[i] && (itext_info[i]->pointer == pointer))
- {
- free (itext_info[i]);
- itext_info[i] = NULL;
- break;
- }
- }
- /* Append the text which appeared in input_text from the last offset to
- the character just before the command that we are currently executing. */
- void
- me_append_before_this_command (void)
- {
- int i;
- for (i = input_text_offset; i && (input_text[i] != COMMAND_PREFIX); i--)
- ;
- maybe_write_itext (input_text, i);
- }
- /* Similar to execute_string, but only takes a single string argument,
- and remembers the input text location, etc. */
- void
- me_execute_string (char *execution_string)
- {
- int saved_escape_html = escape_html;
- int saved_in_paragraph = in_paragraph;
- escape_html = me_executing_string == 0;
- in_paragraph = 0;
-
- pushfile ();
- input_text_offset = 0;
- /* The following xstrdup is so we can relocate input_text at will. */
- input_text = xstrdup (execution_string);
- input_filename = xstrdup (input_filename);
- input_text_length = strlen (execution_string);
- remember_itext (input_text, 0);
- me_executing_string++;
- reader_loop ();
- free (input_text);
- free (input_filename);
- popfile ();
- me_executing_string--;
- in_paragraph = saved_in_paragraph;
- escape_html = saved_escape_html;
- }
- /* A wrapper around me_execute_string which saves and restores
- variables important for output generation. This is called
- when we need to produce macro-expanded output for input which
- leaves no traces in the Info output. */
- void
- me_execute_string_keep_state (char *execution_string, char *append_string)
- {
- int op_orig, popen_orig;
- int fill_orig, newline_orig, indent_orig, meta_pos_orig;
- remember_itext (input_text, input_text_offset);
- op_orig = output_paragraph_offset;
- meta_pos_orig = meta_char_pos;
- popen_orig = paragraph_is_open;
- fill_orig = filling_enabled;
- newline_orig = last_char_was_newline;
- filling_enabled = 0;
- indent_orig = no_indent;
- no_indent = 1;
- me_execute_string (execution_string);
- if (append_string)
- write_region_to_macro_output (append_string, 0, strlen (append_string));
- output_paragraph_offset = op_orig;
- meta_char_pos = meta_pos_orig;
- paragraph_is_open = popen_orig;
- filling_enabled = fill_orig;
- last_char_was_newline = newline_orig;
- no_indent = indent_orig;
- }
- /* Append the text which appears in input_text from the last offset to
- the current OFFSET. */
- void
- append_to_expansion_output (int offset)
- {
- int i;
- ITEXT *itext = NULL;
- for (i = 0; i < itext_size; i++)
- if (itext_info[i] && itext_info[i]->pointer == input_text)
- {
- itext = itext_info[i];
- break;
- }
- if (!itext)
- return;
- if (offset > itext->offset)
- {
- write_region_to_macro_output (input_text, itext->offset, offset);
- remember_itext (input_text, offset);
- }
- }
- /* Only write this input text iff it appears in our itext list. */
- void
- maybe_write_itext (char *pointer, int offset)
- {
- int i;
- ITEXT *itext = NULL;
- for (i = 0; i < itext_size; i++)
- if (itext_info[i] && (itext_info[i]->pointer == pointer))
- {
- itext = itext_info[i];
- break;
- }
- if (itext && (itext->offset < offset))
- {
- write_region_to_macro_output (itext->pointer, itext->offset, offset);
- remember_itext (pointer, offset);
- }
- }
- void
- write_region_to_macro_output (char *string, int start, int end)
- {
- if (macro_expansion_output_stream)
- fwrite (string + start, 1, end - start, macro_expansion_output_stream);
- }
- /* Aliases. */
- typedef struct alias_struct
- {
- char *alias;
- char *mapto;
- struct alias_struct *next;
- } alias_type;
- static alias_type *aliases;
- /* @alias aname = cmdname */
- void
- cm_alias (void)
- {
- alias_type *a = xmalloc (sizeof (alias_type));
- skip_whitespace ();
- get_until_in_line (0, "=", &(a->alias));
- canon_white (a->alias);
- discard_until ("=");
- skip_whitespace ();
- get_until_in_line (0, " ", &(a->mapto));
- a->next = aliases;
- aliases = a;
- }
- /* Perform an alias expansion. Called from read_command. */
- char *
- alias_expand (char *tok)
- {
- alias_type *findit = aliases;
- while (findit)
- if (strcmp (findit->alias, tok) == 0)
- {
- free (tok);
- return alias_expand (xstrdup (findit->mapto));
- }
- else
- findit = findit->next;
- return tok;
- }
- /* definfoenclose implementation. */
- /* This structure is used to track enclosure macros. When an enclosure
- macro is recognized, a pointer to the enclosure block corresponding
- to its name is saved in the brace element for its argument. */
- typedef struct enclose_struct
- {
- char *enclose;
- char *before;
- char *after;
- struct enclose_struct *next;
- } enclosure_type;
- static enclosure_type *enclosures;
- typedef struct enclosure_stack_struct
- {
- enclosure_type *current;
- struct enclosure_stack_struct *next;
- } enclosure_stack_type;
- static enclosure_stack_type *enclosure_stack;
- /* @definfoenclose */
- void
- cm_definfoenclose (void)
- {
- enclosure_type *e = xmalloc (sizeof (enclosure_type));
- skip_whitespace ();
- get_until_in_line (1, ",", &(e->enclose));
- discard_until (",");
- get_until_in_line (0, ",", &(e->before));
- discard_until (",");
- get_until_in_line (0, "\n", &(e->after));
- e->next = enclosures;
- enclosures = e;
- }
- /* If TOK is an enclosure command, push it on the enclosure stack and
- return 1. Else return 0. */
- int
- enclosure_command (char *tok)
- {
- enclosure_type *findit = enclosures;
- while (findit)
- if (strcmp (findit->enclose, tok) == 0)
- {
- enclosure_stack_type *new = xmalloc (sizeof (enclosure_stack_type));
- new->current = findit;
- new->next = enclosure_stack;
- enclosure_stack = new;
- return 1;
- }
- else
- findit = findit->next;
- return 0;
- }
- /* actually perform the enclosure expansion */
- void
- enclosure_expand (int arg, int start, int end)
- {
- if (arg == START)
- add_word (enclosure_stack->current->before);
- else
- {
- enclosure_stack_type *temp;
- add_word (enclosure_stack->current->after);
- temp = enclosure_stack;
- enclosure_stack = enclosure_stack->next;
- free (temp);
- }
- }
|