PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/src/freebsd/contrib/texinfo/makeinfo/macro.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 1108 lines | 803 code | 170 blank | 135 comment | 204 complexity | 9470b68376773fc58e8a082a309607d2 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* macro.c -- user-defined macros for Texinfo.
  2. $Id: macro.c,v 1.6 2004/04/11 17:56:47 karl Exp $
  3. Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #include "cmds.h"
  17. #include "files.h"
  18. #include "macro.h"
  19. #include "makeinfo.h"
  20. #include "insertion.h"
  21. /* If non-NULL, this is an output stream to write the full macro expansion
  22. of the input text to. The result is another texinfo file, but
  23. missing @include, @infoinclude, @macro, and macro invocations. Instead,
  24. all of the text is placed within the file. */
  25. FILE *macro_expansion_output_stream = NULL;
  26. /* Output file for -E. */
  27. char *macro_expansion_filename;
  28. /* Nonzero means a macro string is in execution, as opposed to a file. */
  29. int me_executing_string = 0;
  30. /* Nonzero means we want only to expand macros and
  31. leave everything else intact. */
  32. int only_macro_expansion = 0;
  33. static ITEXT **itext_info = NULL;
  34. static int itext_size = 0;
  35. /* Return the arglist on the current line. This can behave in two different
  36. ways, depending on the variable BRACES_REQUIRED_FOR_MACRO_ARGS. */
  37. int braces_required_for_macro_args = 0;
  38. /* Array of macros and definitions. */
  39. MACRO_DEF **macro_list = NULL;
  40. int macro_list_len = 0; /* Number of elements. */
  41. int macro_list_size = 0; /* Number of slots in total. */
  42. /* Return the length of the array in ARRAY. */
  43. int
  44. array_len (char **array)
  45. {
  46. int i = 0;
  47. if (array)
  48. for (i = 0; array[i]; i++);
  49. return i;
  50. }
  51. void
  52. free_array (char **array)
  53. {
  54. if (array)
  55. {
  56. int i;
  57. for (i = 0; array[i]; i++)
  58. free (array[i]);
  59. free (array);
  60. }
  61. }
  62. /* Return the macro definition of NAME or NULL if NAME is not defined. */
  63. MACRO_DEF *
  64. find_macro (char *name)
  65. {
  66. int i;
  67. MACRO_DEF *def;
  68. def = NULL;
  69. for (i = 0; macro_list && (def = macro_list[i]); i++)
  70. {
  71. if ((!def->inhibited) && (strcmp (def->name, name) == 0))
  72. break;
  73. }
  74. return def;
  75. }
  76. /* Add the macro NAME with ARGLIST and BODY to the list of defined macros.
  77. SOURCE_FILE is the name of the file where this definition can be found,
  78. and SOURCE_LINENO is the line number within that file. If a macro already
  79. exists with NAME, then a warning is produced, and that previous
  80. definition is overwritten. */
  81. static void
  82. add_macro (char *name, char **arglist, char *body, char *source_file,
  83. int source_lineno, int flags)
  84. {
  85. MACRO_DEF *def;
  86. def = find_macro (name);
  87. if (!def)
  88. {
  89. if (macro_list_len + 2 >= macro_list_size)
  90. macro_list = xrealloc
  91. (macro_list, ((macro_list_size += 10) * sizeof (MACRO_DEF *)));
  92. macro_list[macro_list_len] = xmalloc (sizeof (MACRO_DEF));
  93. macro_list[macro_list_len + 1] = NULL;
  94. def = macro_list[macro_list_len];
  95. macro_list_len += 1;
  96. def->name = name;
  97. }
  98. else
  99. {
  100. char *temp_filename = input_filename;
  101. int temp_line = line_number;
  102. warning (_("macro `%s' previously defined"), name);
  103. input_filename = def->source_file;
  104. line_number = def->source_lineno;
  105. warning (_("here is the previous definition of `%s'"), name);
  106. input_filename = temp_filename;
  107. line_number = temp_line;
  108. if (def->arglist)
  109. {
  110. int i;
  111. for (i = 0; def->arglist[i]; i++)
  112. free (def->arglist[i]);
  113. free (def->arglist);
  114. }
  115. free (def->source_file);
  116. free (def->body);
  117. }
  118. def->source_file = xstrdup (source_file);
  119. def->source_lineno = source_lineno;
  120. def->body = body;
  121. def->arglist = arglist;
  122. def->inhibited = 0;
  123. def->flags = flags;
  124. }
  125. char **
  126. get_brace_args (int quote_single)
  127. {
  128. char **arglist, *word;
  129. int arglist_index, arglist_size;
  130. int character, escape_seen, start;
  131. int depth = 1;
  132. /* There is an arglist in braces here, so gather the args inside of it. */
  133. skip_whitespace_and_newlines ();
  134. input_text_offset++;
  135. arglist = NULL;
  136. arglist_index = arglist_size = 0;
  137. get_arg:
  138. skip_whitespace_and_newlines ();
  139. start = input_text_offset;
  140. escape_seen = 0;
  141. while ((character = curchar ()))
  142. {
  143. if (character == '\\')
  144. {
  145. input_text_offset += 2;
  146. escape_seen = 1;
  147. }
  148. else if (character == '{')
  149. {
  150. depth++;
  151. input_text_offset++;
  152. }
  153. else if ((character == ',' && !quote_single) ||
  154. ((character == '}') && depth == 1))
  155. {
  156. int len = input_text_offset - start;
  157. if (len || (character != '}'))
  158. {
  159. word = xmalloc (1 + len);
  160. memcpy (word, input_text + start, len);
  161. word[len] = 0;
  162. /* Clean up escaped characters. */
  163. if (escape_seen)
  164. {
  165. int i;
  166. for (i = 0; word[i]; i++)
  167. if (word[i] == '\\')
  168. memmove (word + i, word + i + 1,
  169. 1 + strlen (word + i + 1));
  170. }
  171. if (arglist_index + 2 >= arglist_size)
  172. arglist = xrealloc
  173. (arglist, (arglist_size += 10) * sizeof (char *));
  174. arglist[arglist_index++] = word;
  175. arglist[arglist_index] = NULL;
  176. }
  177. input_text_offset++;
  178. if (character == '}')
  179. break;
  180. else
  181. goto get_arg;
  182. }
  183. else if (character == '}')
  184. {
  185. depth--;
  186. input_text_offset++;
  187. }
  188. else
  189. {
  190. input_text_offset++;
  191. if (character == '\n') line_number++;
  192. }
  193. }
  194. return arglist;
  195. }
  196. static char **
  197. get_macro_args (MACRO_DEF *def)
  198. {
  199. int i;
  200. char *word;
  201. /* Quickly check to see if this macro has been invoked with any arguments.
  202. If not, then don't skip any of the following whitespace. */
  203. for (i = input_text_offset; i < input_text_length; i++)
  204. if (!cr_or_whitespace (input_text[i]))
  205. break;
  206. if (input_text[i] != '{')
  207. {
  208. if (braces_required_for_macro_args)
  209. {
  210. return NULL;
  211. }
  212. else
  213. {
  214. /* Braces are not required to fill out the macro arguments. If
  215. this macro takes one argument, it is considered to be the
  216. remainder of the line, sans whitespace. */
  217. if (def->arglist && def->arglist[0] && !def->arglist[1])
  218. {
  219. char **arglist;
  220. get_rest_of_line (0, &word);
  221. if (input_text[input_text_offset - 1] == '\n')
  222. {
  223. input_text_offset--;
  224. line_number--;
  225. }
  226. /* canon_white (word); */
  227. arglist = xmalloc (2 * sizeof (char *));
  228. arglist[0] = word;
  229. arglist[1] = NULL;
  230. return arglist;
  231. }
  232. else
  233. {
  234. /* The macro either took no arguments, or took more than
  235. one argument. In that case, it must be invoked with
  236. arguments surrounded by braces. */
  237. return NULL;
  238. }
  239. }
  240. }
  241. return get_brace_args (def->flags & ME_QUOTE_ARG);
  242. }
  243. /* Substitute actual parameters for named parameters in body.
  244. The named parameters which appear in BODY must by surrounded
  245. reverse slashes, as in \foo\. */
  246. static char *
  247. apply (char **named, char **actuals, char *body)
  248. {
  249. int i;
  250. int new_body_index, new_body_size;
  251. char *new_body, *text;
  252. int length_of_actuals;
  253. length_of_actuals = array_len (actuals);
  254. new_body_size = strlen (body);
  255. new_body = xmalloc (1 + new_body_size);
  256. /* Copy chars from BODY into NEW_BODY. */
  257. i = 0;
  258. new_body_index = 0;
  259. while (body[i])
  260. { /* Anything but a \ is easy. */
  261. if (body[i] != '\\')
  262. new_body[new_body_index++] = body[i++];
  263. else
  264. { /* Snarf parameter name, check against named parameters. */
  265. char *param;
  266. int param_start, len;
  267. param_start = ++i;
  268. while (body[i] && body[i] != '\\')
  269. i++;
  270. len = i - param_start;
  271. param = xmalloc (1 + len);
  272. memcpy (param, body + param_start, len);
  273. param[len] = 0;
  274. if (body[i]) /* move past \ */
  275. i++;
  276. if (len == 0)
  277. { /* \\ always means \, even if macro has no args. */
  278. len++;
  279. text = xmalloc (1 + len);
  280. sprintf (text, "\\%s", param);
  281. }
  282. else
  283. {
  284. int which;
  285. /* Check against named parameters. */
  286. for (which = 0; named && named[which]; which++)
  287. if (STREQ (named[which], param))
  288. break;
  289. if (named && named[which])
  290. {
  291. text = which < length_of_actuals ? actuals[which] : NULL;
  292. if (!text)
  293. text = "";
  294. len = strlen (text);
  295. text = xstrdup (text); /* so we can free it */
  296. }
  297. else
  298. { /* not a parameter, so it's an error. */
  299. warning (_("\\ in macro expansion followed by `%s' instead of parameter name"),
  300. param);
  301. len++;
  302. text = xmalloc (1 + len);
  303. sprintf (text, "\\%s", param);
  304. }
  305. }
  306. if (strlen (param) + 2 < len)
  307. {
  308. new_body_size += len + 1;
  309. new_body = xrealloc (new_body, new_body_size);
  310. }
  311. free (param);
  312. strcpy (new_body + new_body_index, text);
  313. new_body_index += len;
  314. free (text);
  315. }
  316. }
  317. new_body[new_body_index] = 0;
  318. return new_body;
  319. }
  320. /* Expand macro passed in DEF, a pointer to a MACRO_DEF, and
  321. return its expansion as a string. */
  322. char *
  323. expand_macro (MACRO_DEF *def)
  324. {
  325. char **arglist;
  326. int num_args;
  327. char *execution_string = NULL;
  328. int start_line = line_number;
  329. /* Find out how many arguments this macro definition takes. */
  330. num_args = array_len (def->arglist);
  331. /* Gather the arguments present on the line if there are any. */
  332. arglist = get_macro_args (def);
  333. if (num_args < array_len (arglist))
  334. {
  335. free_array (arglist);
  336. line_error (_("Macro `%s' called on line %d with too many args"),
  337. def->name, start_line);
  338. return execution_string;
  339. }
  340. if (def->body)
  341. execution_string = apply (def->arglist, arglist, def->body);
  342. free_array (arglist);
  343. return execution_string;
  344. }
  345. /* Execute the macro passed in DEF, a pointer to a MACRO_DEF. */
  346. void
  347. execute_macro (MACRO_DEF *def)
  348. {
  349. char *execution_string;
  350. int start_line = line_number, end_line;
  351. if (macro_expansion_output_stream && !executing_string && !me_inhibit_expansion)
  352. me_append_before_this_command ();
  353. execution_string = expand_macro (def);
  354. if (!execution_string)
  355. return;
  356. if (def->body)
  357. {
  358. /* Reset the line number to where the macro arguments began.
  359. This makes line numbers reported in error messages correct in
  360. case the macro arguments span several lines and the expanded
  361. arguments invoke other commands. */
  362. end_line = line_number;
  363. line_number = start_line;
  364. if (macro_expansion_output_stream
  365. && !executing_string && !me_inhibit_expansion)
  366. {
  367. remember_itext (input_text, input_text_offset);
  368. me_execute_string (execution_string);
  369. }
  370. else
  371. execute_string ("%s", execution_string);
  372. free (execution_string);
  373. line_number = end_line;
  374. }
  375. }
  376. /* Read and remember the definition of a macro. If RECURSIVE is set,
  377. set the ME_RECURSE flag. MACTYPE is either "macro" or "rmacro", and
  378. tells us what the matching @end should be. */
  379. static void
  380. define_macro (char *mactype, int recursive)
  381. {
  382. int i, start;
  383. char *name, *line;
  384. char *last_end = NULL;
  385. char *body = NULL;
  386. char **arglist = NULL;
  387. int body_size = 0, body_index = 0;
  388. int depth = 1;
  389. int flags = 0;
  390. int defining_line = line_number;
  391. if (macro_expansion_output_stream && !executing_string)
  392. me_append_before_this_command ();
  393. skip_whitespace ();
  394. /* Get the name of the macro. This is the set of characters which are
  395. not whitespace and are not `{' immediately following the @macro. */
  396. start = input_text_offset;
  397. {
  398. int len;
  399. for (i = start; i < input_text_length && input_text[i] != '{'
  400. && !cr_or_whitespace (input_text[i]);
  401. i++) ;
  402. len = i - start;
  403. name = xmalloc (1 + len);
  404. memcpy (name, input_text + start, len);
  405. name[len] = 0;
  406. input_text_offset = i;
  407. }
  408. skip_whitespace ();
  409. /* It is not required that the definition of a macro includes an arglist.
  410. If not, don't try to get the named parameters, just use a null list. */
  411. if (curchar () == '{')
  412. {
  413. int character;
  414. int arglist_index = 0, arglist_size = 0;
  415. int gathering_words = 1;
  416. char *word = NULL;
  417. /* Read the words inside of the braces which determine the arglist.
  418. These words will be replaced within the body of the macro at
  419. execution time. */
  420. input_text_offset++;
  421. skip_whitespace_and_newlines ();
  422. while (gathering_words)
  423. {
  424. int len;
  425. for (i = input_text_offset;
  426. (character = input_text[i]);
  427. i++)
  428. {
  429. switch (character)
  430. {
  431. case '\n':
  432. line_number++;
  433. case ' ':
  434. case '\t':
  435. case ',':
  436. case '}':
  437. /* Found the end of the current arglist word. Save it. */
  438. len = i - input_text_offset;
  439. word = xmalloc (1 + len);
  440. memcpy (word, input_text + input_text_offset, len);
  441. word[len] = 0;
  442. input_text_offset = i;
  443. /* Advance to the comma or close-brace that signified
  444. the end of the argument. */
  445. while ((character = curchar ())
  446. && character != ','
  447. && character != '}')
  448. {
  449. input_text_offset++;
  450. if (character == '\n')
  451. line_number++;
  452. }
  453. /* Add the word to our list of words. */
  454. if (arglist_index + 2 >= arglist_size)
  455. {
  456. arglist_size += 10;
  457. arglist = xrealloc (arglist,
  458. arglist_size * sizeof (char *));
  459. }
  460. arglist[arglist_index++] = word;
  461. arglist[arglist_index] = NULL;
  462. break;
  463. }
  464. if (character == '}')
  465. {
  466. input_text_offset++;
  467. gathering_words = 0;
  468. break;
  469. }
  470. if (character == ',')
  471. {
  472. input_text_offset++;
  473. skip_whitespace_and_newlines ();
  474. i = input_text_offset - 1;
  475. }
  476. }
  477. }
  478. /* If we have exactly one argument, do @quote-arg implicitly. Not
  479. only does this match TeX's behavior (which can't feasibly be
  480. changed), but it's a good idea. */
  481. if (arglist_index == 1)
  482. flags |= ME_QUOTE_ARG;
  483. }
  484. /* Read the text carefully until we find an "@end macro" which
  485. matches this one. The text in between is the body of the macro. */
  486. skip_whitespace_and_newlines ();
  487. while (depth)
  488. {
  489. if ((input_text_offset + 9) > input_text_length)
  490. {
  491. file_line_error (input_filename, defining_line,
  492. _("%cend macro not found"), COMMAND_PREFIX);
  493. return;
  494. }
  495. get_rest_of_line (0, &line);
  496. /* Handle commands only meaningful within a macro. */
  497. if ((*line == COMMAND_PREFIX) && (depth == 1) &&
  498. (strncmp (line + 1, "allow-recursion", 15) == 0) &&
  499. (line[16] == 0 || whitespace (line[16])))
  500. {
  501. for (i = 16; whitespace (line[i]); i++);
  502. strcpy (line, line + i);
  503. flags |= ME_RECURSE;
  504. if (!*line)
  505. {
  506. free (line);
  507. continue;
  508. }
  509. }
  510. if ((*line == COMMAND_PREFIX) && (depth == 1) &&
  511. (strncmp (line + 1, "quote-arg", 9) == 0) &&
  512. (line[10] == 0 || whitespace (line[10])))
  513. {
  514. for (i = 10; whitespace (line[i]); i++);
  515. strcpy (line, line + i);
  516. if (arglist && arglist[0] && !arglist[1])
  517. {
  518. flags |= ME_QUOTE_ARG;
  519. if (!*line)
  520. {
  521. free (line);
  522. continue;
  523. }
  524. }
  525. else
  526. line_error (_("@quote-arg only useful for single-argument macros"));
  527. }
  528. if (*line == COMMAND_PREFIX
  529. && (strncmp (line + 1, "macro ", 6) == 0
  530. || strncmp (line + 1, "rmacro ", 7) == 0))
  531. depth++;
  532. /* Incorrect implementation of nesting -- just check that the last
  533. @end matches what we started with. Since nested macros don't
  534. work in TeX anyway, this isn't worth the trouble to get right. */
  535. if (*line == COMMAND_PREFIX && strncmp (line + 1, "end macro", 9) == 0)
  536. {
  537. depth--;
  538. last_end = "macro";
  539. }
  540. if (*line == COMMAND_PREFIX && strncmp (line + 1, "end rmacro", 10) == 0)
  541. {
  542. depth--;
  543. last_end = "rmacro";
  544. }
  545. if (depth)
  546. {
  547. if ((body_index + strlen (line) + 3) >= body_size)
  548. body = xrealloc (body, body_size += 3 + strlen (line));
  549. strcpy (body + body_index, line);
  550. body_index += strlen (line);
  551. body[body_index++] = '\n';
  552. body[body_index] = 0;
  553. }
  554. free (line);
  555. }
  556. /* Check that @end matched the macro command. */
  557. if (!STREQ (last_end, mactype))
  558. warning (_("mismatched @end %s with @%s"), last_end, mactype);
  559. /* If it was an empty macro like
  560. @macro foo
  561. @end macro
  562. create an empty body. (Otherwise, the macro is not expanded.) */
  563. if (!body)
  564. {
  565. body = (char *)malloc(1);
  566. *body = 0;
  567. }
  568. /* We now have the name, the arglist, and the body. However, BODY
  569. includes the final newline which preceded the `@end macro' text.
  570. Delete it. */
  571. if (body && strlen (body))
  572. body[strlen (body) - 1] = 0;
  573. if (recursive)
  574. flags |= ME_RECURSE;
  575. add_macro (name, arglist, body, input_filename, defining_line, flags);
  576. if (macro_expansion_output_stream && !executing_string)
  577. {
  578. /* Remember text for future expansions. */
  579. remember_itext (input_text, input_text_offset);
  580. /* Bizarrely, output the @macro itself. This is so texinfo.tex
  581. will have a chance to read it when texi2dvi calls makeinfo -E.
  582. The problem is that we don't really expand macros in all
  583. contexts; a @table's @item is one. And a fix is not obvious to
  584. me, since it appears virtually identical to any other internal
  585. expansion. Just setting a variable in cm_item caused other
  586. strange expansion problems. */
  587. write_region_to_macro_output ("@", 0, 1);
  588. write_region_to_macro_output (mactype, 0, strlen (mactype));
  589. write_region_to_macro_output (" ", 0, 1);
  590. write_region_to_macro_output (input_text, start, input_text_offset);
  591. }
  592. }
  593. void
  594. cm_macro (void)
  595. {
  596. define_macro ("macro", 0);
  597. }
  598. void
  599. cm_rmacro (void)
  600. {
  601. define_macro ("rmacro", 1);
  602. }
  603. /* Delete the macro with name NAME. The macro is deleted from the list,
  604. but it is also returned. If there was no macro defined, NULL is
  605. returned. */
  606. static MACRO_DEF *
  607. delete_macro (char *name)
  608. {
  609. int i;
  610. MACRO_DEF *def;
  611. def = NULL;
  612. for (i = 0; macro_list && (def = macro_list[i]); i++)
  613. if (strcmp (def->name, name) == 0)
  614. {
  615. memmove (macro_list + i, macro_list + i + 1,
  616. ((macro_list_len + 1) - i) * sizeof (MACRO_DEF *));
  617. macro_list_len--;
  618. break;
  619. }
  620. return def;
  621. }
  622. void
  623. cm_unmacro (void)
  624. {
  625. int i;
  626. char *line, *name;
  627. MACRO_DEF *def;
  628. if (macro_expansion_output_stream && !executing_string)
  629. me_append_before_this_command ();
  630. get_rest_of_line (0, &line);
  631. for (i = 0; line[i] && !whitespace (line[i]); i++);
  632. name = xmalloc (i + 1);
  633. memcpy (name, line, i);
  634. name[i] = 0;
  635. def = delete_macro (name);
  636. if (def)
  637. {
  638. free (def->source_file);
  639. free (def->name);
  640. free (def->body);
  641. if (def->arglist)
  642. {
  643. int i;
  644. for (i = 0; def->arglist[i]; i++)
  645. free (def->arglist[i]);
  646. free (def->arglist);
  647. }
  648. free (def);
  649. }
  650. free (line);
  651. free (name);
  652. if (macro_expansion_output_stream && !executing_string)
  653. remember_itext (input_text, input_text_offset);
  654. }
  655. /* How to output sections of the input file verbatim. */
  656. /* Set the value of POINTER's offset to OFFSET. */
  657. ITEXT *
  658. remember_itext (char *pointer, int offset)
  659. {
  660. int i;
  661. ITEXT *itext = NULL;
  662. /* If we have no info, initialize a blank list. */
  663. if (!itext_info)
  664. {
  665. itext_info = xmalloc ((itext_size = 10) * sizeof (ITEXT *));
  666. for (i = 0; i < itext_size; i++)
  667. itext_info[i] = NULL;
  668. }
  669. /* If the pointer is already present in the list, then set the offset. */
  670. for (i = 0; i < itext_size; i++)
  671. if ((itext_info[i]) &&
  672. (itext_info[i]->pointer == pointer))
  673. {
  674. itext = itext_info[i];
  675. itext_info[i]->offset = offset;
  676. break;
  677. }
  678. if (i == itext_size)
  679. {
  680. /* Find a blank slot (or create a new one), and remember the
  681. pointer and offset. */
  682. for (i = 0; i < itext_size; i++)
  683. if (itext_info[i] == NULL)
  684. break;
  685. /* If not found, then add some slots. */
  686. if (i == itext_size)
  687. {
  688. int j;
  689. itext_info = xrealloc
  690. (itext_info, (itext_size += 10) * sizeof (ITEXT *));
  691. for (j = i; j < itext_size; j++)
  692. itext_info[j] = NULL;
  693. }
  694. /* Now add the pointer and the offset. */
  695. itext_info[i] = xmalloc (sizeof (ITEXT));
  696. itext_info[i]->pointer = pointer;
  697. itext_info[i]->offset = offset;
  698. itext = itext_info[i];
  699. }
  700. return itext;
  701. }
  702. /* Forget the input text associated with POINTER. */
  703. void
  704. forget_itext (char *pointer)
  705. {
  706. int i;
  707. for (i = 0; i < itext_size; i++)
  708. if (itext_info[i] && (itext_info[i]->pointer == pointer))
  709. {
  710. free (itext_info[i]);
  711. itext_info[i] = NULL;
  712. break;
  713. }
  714. }
  715. /* Append the text which appeared in input_text from the last offset to
  716. the character just before the command that we are currently executing. */
  717. void
  718. me_append_before_this_command (void)
  719. {
  720. int i;
  721. for (i = input_text_offset; i && (input_text[i] != COMMAND_PREFIX); i--)
  722. ;
  723. maybe_write_itext (input_text, i);
  724. }
  725. /* Similar to execute_string, but only takes a single string argument,
  726. and remembers the input text location, etc. */
  727. void
  728. me_execute_string (char *execution_string)
  729. {
  730. int saved_escape_html = escape_html;
  731. int saved_in_paragraph = in_paragraph;
  732. escape_html = me_executing_string == 0;
  733. in_paragraph = 0;
  734. pushfile ();
  735. input_text_offset = 0;
  736. /* The following xstrdup is so we can relocate input_text at will. */
  737. input_text = xstrdup (execution_string);
  738. input_filename = xstrdup (input_filename);
  739. input_text_length = strlen (execution_string);
  740. remember_itext (input_text, 0);
  741. me_executing_string++;
  742. reader_loop ();
  743. free (input_text);
  744. free (input_filename);
  745. popfile ();
  746. me_executing_string--;
  747. in_paragraph = saved_in_paragraph;
  748. escape_html = saved_escape_html;
  749. }
  750. /* A wrapper around me_execute_string which saves and restores
  751. variables important for output generation. This is called
  752. when we need to produce macro-expanded output for input which
  753. leaves no traces in the Info output. */
  754. void
  755. me_execute_string_keep_state (char *execution_string, char *append_string)
  756. {
  757. int op_orig, opcol_orig, popen_orig;
  758. int fill_orig, newline_orig, indent_orig, meta_pos_orig;
  759. remember_itext (input_text, input_text_offset);
  760. op_orig = output_paragraph_offset;
  761. meta_pos_orig = meta_char_pos;
  762. opcol_orig = output_column;
  763. popen_orig = paragraph_is_open;
  764. fill_orig = filling_enabled;
  765. newline_orig = last_char_was_newline;
  766. filling_enabled = 0;
  767. indent_orig = no_indent;
  768. no_indent = 1;
  769. me_execute_string (execution_string);
  770. if (append_string)
  771. write_region_to_macro_output (append_string, 0, strlen (append_string));
  772. output_paragraph_offset = op_orig;
  773. meta_char_pos = meta_pos_orig;
  774. output_column = opcol_orig;
  775. paragraph_is_open = popen_orig;
  776. filling_enabled = fill_orig;
  777. last_char_was_newline = newline_orig;
  778. no_indent = indent_orig;
  779. }
  780. /* Append the text which appears in input_text from the last offset to
  781. the current OFFSET. */
  782. void
  783. append_to_expansion_output (int offset)
  784. {
  785. int i;
  786. ITEXT *itext = NULL;
  787. for (i = 0; i < itext_size; i++)
  788. if (itext_info[i] && itext_info[i]->pointer == input_text)
  789. {
  790. itext = itext_info[i];
  791. break;
  792. }
  793. if (!itext)
  794. return;
  795. if (offset > itext->offset)
  796. {
  797. write_region_to_macro_output (input_text, itext->offset, offset);
  798. remember_itext (input_text, offset);
  799. }
  800. }
  801. /* Only write this input text iff it appears in our itext list. */
  802. void
  803. maybe_write_itext (char *pointer, int offset)
  804. {
  805. int i;
  806. ITEXT *itext = NULL;
  807. for (i = 0; i < itext_size; i++)
  808. if (itext_info[i] && (itext_info[i]->pointer == pointer))
  809. {
  810. itext = itext_info[i];
  811. break;
  812. }
  813. if (itext && (itext->offset < offset))
  814. {
  815. write_region_to_macro_output (itext->pointer, itext->offset, offset);
  816. remember_itext (pointer, offset);
  817. }
  818. }
  819. void
  820. write_region_to_macro_output (char *string, int start, int end)
  821. {
  822. if (macro_expansion_output_stream)
  823. fwrite (string + start, 1, end - start, macro_expansion_output_stream);
  824. }
  825. /* Aliases. */
  826. typedef struct alias_struct
  827. {
  828. char *alias;
  829. char *mapto;
  830. struct alias_struct *next;
  831. } alias_type;
  832. static alias_type *aliases;
  833. /* @alias aname = cmdname */
  834. void
  835. cm_alias (void)
  836. {
  837. alias_type *a = xmalloc (sizeof (alias_type));
  838. skip_whitespace ();
  839. get_until_in_line (0, "=", &(a->alias));
  840. canon_white (a->alias);
  841. discard_until ("=");
  842. skip_whitespace ();
  843. get_until_in_line (0, " ", &(a->mapto));
  844. a->next = aliases;
  845. aliases = a;
  846. }
  847. /* Perform an alias expansion. Called from read_command. */
  848. char *
  849. alias_expand (char *tok)
  850. {
  851. alias_type *findit = aliases;
  852. while (findit)
  853. if (strcmp (findit->alias, tok) == 0)
  854. {
  855. free (tok);
  856. return alias_expand (xstrdup (findit->mapto));
  857. }
  858. else
  859. findit = findit->next;
  860. return tok;
  861. }
  862. /* definfoenclose implementation. */
  863. /* This structure is used to track enclosure macros. When an enclosure
  864. macro is recognized, a pointer to the enclosure block corresponding
  865. to its name is saved in the brace element for its argument. */
  866. typedef struct enclose_struct
  867. {
  868. char *enclose;
  869. char *before;
  870. char *after;
  871. struct enclose_struct *next;
  872. } enclosure_type;
  873. static enclosure_type *enclosures;
  874. typedef struct enclosure_stack_struct
  875. {
  876. enclosure_type *current;
  877. struct enclosure_stack_struct *next;
  878. } enclosure_stack_type;
  879. static enclosure_stack_type *enclosure_stack;
  880. /* @definfoenclose */
  881. void
  882. cm_definfoenclose (void)
  883. {
  884. enclosure_type *e = xmalloc (sizeof (enclosure_type));
  885. skip_whitespace ();
  886. get_until_in_line (1, ",", &(e->enclose));
  887. discard_until (",");
  888. get_until_in_line (0, ",", &(e->before));
  889. discard_until (",");
  890. get_until_in_line (0, "\n", &(e->after));
  891. e->next = enclosures;
  892. enclosures = e;
  893. }
  894. /* If TOK is an enclosure command, push it on the enclosure stack and
  895. return 1. Else return 0. */
  896. int
  897. enclosure_command (char *tok)
  898. {
  899. enclosure_type *findit = enclosures;
  900. while (findit)
  901. if (strcmp (findit->enclose, tok) == 0)
  902. {
  903. enclosure_stack_type *new = xmalloc (sizeof (enclosure_stack_type));
  904. new->current = findit;
  905. new->next = enclosure_stack;
  906. enclosure_stack = new;
  907. return 1;
  908. }
  909. else
  910. findit = findit->next;
  911. return 0;
  912. }
  913. /* actually perform the enclosure expansion */
  914. void
  915. enclosure_expand (int arg, int start, int end)
  916. {
  917. if (arg == START)
  918. add_word (enclosure_stack->current->before);
  919. else
  920. {
  921. enclosure_stack_type *temp;
  922. add_word (enclosure_stack->current->after);
  923. temp = enclosure_stack;
  924. enclosure_stack = enclosure_stack->next;
  925. free (temp);
  926. }
  927. }