macro.c 29 KB

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