defun.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /* defun.c -- @defun and friends.
  2. $Id: defun.c,v 1.18 2007-09-15 23:48:45 karl Exp $
  3. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 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 "defun.h"
  17. #include "xml.h"
  18. #include "insertion.h"
  19. #include "makeinfo.h"
  20. #include "cmds.h"
  21. #include "html.h"
  22. #define DEFUN_SELF_DELIMITING(c) \
  23. ((c) == '(' || (c) == ')' || (c) == '[' || (c) == ']')
  24. struct token_accumulator
  25. {
  26. unsigned int length;
  27. unsigned int index;
  28. char **tokens;
  29. };
  30. static void
  31. initialize_token_accumulator (struct token_accumulator *accumulator)
  32. {
  33. accumulator->length = 0;
  34. accumulator->index = 0;
  35. accumulator->tokens = NULL;
  36. }
  37. static void
  38. accumulate_token (struct token_accumulator *accumulator, char *token)
  39. {
  40. if (accumulator->index >= accumulator->length)
  41. {
  42. accumulator->length += 10;
  43. accumulator->tokens = xrealloc (accumulator->tokens,
  44. (accumulator->length * sizeof (char *)));
  45. }
  46. accumulator->tokens[accumulator->index] = token;
  47. accumulator->index += 1;
  48. }
  49. /* Given STRING_POINTER pointing at an open brace, skip forward and return a
  50. pointer to just past the matching close brace. */
  51. static int
  52. scan_group_in_string (char **string_pointer)
  53. {
  54. char *scan_string = (*string_pointer) + 1;
  55. unsigned int level = 1;
  56. int started_command = 0;
  57. for (;;)
  58. {
  59. int c;
  60. if (level == 0)
  61. {
  62. *string_pointer = scan_string;
  63. return 1;
  64. }
  65. c = *scan_string++;
  66. if (c == 0)
  67. {
  68. /* Tweak line_number to compensate for fact that
  69. we gobbled the whole line before coming here. */
  70. line_number--;
  71. line_error (_("Missing `}' in @def arg"));
  72. line_number++;
  73. *string_pointer = scan_string - 1;
  74. return 0;
  75. }
  76. if (c == '{' && !started_command)
  77. level++;
  78. if (c == '}' && !started_command)
  79. level--;
  80. /* remember if at @. */
  81. started_command = (c == '@' && !started_command);
  82. }
  83. }
  84. /* Return a list of tokens from the contents of STRING.
  85. Commands and brace-delimited groups count as single tokens.
  86. Contiguous whitespace characters are converted to a token
  87. consisting of a single space. */
  88. static char **
  89. args_from_string (char *string)
  90. {
  91. struct token_accumulator accumulator;
  92. char *token_start, *token_end;
  93. char *scan_string = string;
  94. initialize_token_accumulator (&accumulator);
  95. while (*scan_string)
  96. { /* Replace arbitrary whitespace by a single space. */
  97. if (whitespace (*scan_string))
  98. {
  99. scan_string += 1;
  100. while (whitespace (*scan_string))
  101. scan_string += 1;
  102. accumulate_token ((&accumulator), (xstrdup (" ")));
  103. continue;
  104. }
  105. /* Commands count as single tokens. */
  106. if (*scan_string == COMMAND_PREFIX)
  107. {
  108. token_start = scan_string;
  109. scan_string += 1;
  110. if (self_delimiting (*scan_string))
  111. scan_string += 1;
  112. else
  113. {
  114. int c;
  115. while (1)
  116. {
  117. c = *scan_string++;
  118. if ((c == 0) || (c == '{') || (whitespace (c)))
  119. {
  120. scan_string -= 1;
  121. break;
  122. }
  123. }
  124. if (*scan_string == '{')
  125. {
  126. char *s = scan_string;
  127. (void) scan_group_in_string (&s);
  128. scan_string = s;
  129. }
  130. }
  131. token_end = scan_string;
  132. }
  133. /* Parentheses and brackets are self-delimiting. */
  134. else if (DEFUN_SELF_DELIMITING (*scan_string))
  135. {
  136. token_start = scan_string;
  137. scan_string += 1;
  138. token_end = scan_string;
  139. }
  140. /* Open brace introduces a group that is a single token. */
  141. else if (*scan_string == '{')
  142. {
  143. char *s = scan_string;
  144. int balanced = scan_group_in_string (&s);
  145. token_start = scan_string + 1;
  146. scan_string = s;
  147. token_end = balanced ? (scan_string - 1) : scan_string;
  148. }
  149. /* Make commas separate tokens so to differentiate them from
  150. parameter types in XML output. */
  151. else if (*scan_string == ',')
  152. {
  153. token_start = scan_string;
  154. scan_string += 1;
  155. token_end = scan_string;
  156. }
  157. /* Otherwise a token is delimited by whitespace, parentheses,
  158. brackets, or braces. A token is also ended by a command. */
  159. else
  160. {
  161. token_start = scan_string;
  162. for (;;)
  163. {
  164. int c;
  165. c = *scan_string++;
  166. /* Do not back up if we're looking at a }; since the only
  167. valid }'s are those matched with {'s, we want to give
  168. an error. If we back up, we go into an infinite loop. */
  169. if (!c || whitespace (c) || DEFUN_SELF_DELIMITING (c)
  170. || c == '{')
  171. {
  172. scan_string--;
  173. break;
  174. }
  175. /* End token if we are looking at a comma, as commas are
  176. delimiters too. */
  177. if (c == ',')
  178. {
  179. scan_string--;
  180. break;
  181. }
  182. /* If we encounter a command embedded within a token,
  183. then end the token. */
  184. if (c == COMMAND_PREFIX)
  185. {
  186. scan_string--;
  187. break;
  188. }
  189. }
  190. token_end = scan_string;
  191. }
  192. accumulate_token (&accumulator, substring (token_start, token_end));
  193. }
  194. accumulate_token (&accumulator, NULL);
  195. return accumulator.tokens;
  196. }
  197. static void
  198. process_defun_args (char **defun_args, int auto_var_p)
  199. {
  200. int pending_space = 0;
  201. if (xml)
  202. {
  203. xml_process_defun_args (defun_args, auto_var_p);
  204. return;
  205. }
  206. for (;;)
  207. {
  208. char *defun_arg = *defun_args++;
  209. if (defun_arg == NULL)
  210. break;
  211. if (defun_arg[0] == ' ')
  212. {
  213. pending_space = 1;
  214. continue;
  215. }
  216. if (pending_space)
  217. {
  218. add_char (' ');
  219. pending_space = 0;
  220. }
  221. if (DEFUN_SELF_DELIMITING (defun_arg[0]))
  222. {
  223. /* Within @deffn and friends, texinfo.tex makes parentheses
  224. sans serif and brackets bold. We use roman instead. */
  225. if (html)
  226. insert_html_tag (START, "");
  227. add_char (defun_arg[0]);
  228. if (html)
  229. insert_html_tag (END, "");
  230. }
  231. /* else if (defun_arg[0] == '&' || defun_arg[0] == COMMAND_PREFIX) */
  232. /* execute_string ("%s", defun_arg); */
  233. /* else if (auto_var_p) */
  234. /* execute_string ("%s", defun_arg); */
  235. else
  236. execute_string ("%s", defun_arg);
  237. }
  238. }
  239. static char *
  240. next_nonwhite_defun_arg (char ***arg_pointer)
  241. {
  242. char **scan = (*arg_pointer);
  243. char *arg = (*scan++);
  244. if ((arg != 0) && (*arg == ' '))
  245. arg = *scan++;
  246. if (arg == 0)
  247. scan -= 1;
  248. *arg_pointer = scan;
  249. return (arg == 0) ? "" : arg;
  250. }
  251. /* This is needed also in insertion.c. */
  252. enum insertion_type
  253. get_base_type (enum insertion_type type)
  254. {
  255. enum insertion_type base_type;
  256. switch (type)
  257. {
  258. case defivar: base_type = defcv; break;
  259. case defmac: base_type = deffn; break;
  260. case defmethod: base_type = defop; break;
  261. case defopt: base_type = defvr; break;
  262. case defspec: base_type = deffn; break;
  263. case deftypecv: base_type = deftypecv; break;
  264. case deftypefun: base_type = deftypefn; break;
  265. case deftypeivar: base_type = deftypeivar; break;
  266. case deftypemethod: base_type = deftypemethod; break;
  267. case deftypeop: base_type = deftypeop; break;
  268. case deftypevar: base_type = deftypevr; break;
  269. case defun: base_type = deffn; break;
  270. case defvar: base_type = defvr; break;
  271. default:
  272. base_type = type;
  273. break;
  274. }
  275. return base_type;
  276. }
  277. /* Make the defun type insertion.
  278. TYPE says which insertion this is.
  279. X_P, if nonzero, says not to start a new insertion. */
  280. static void
  281. defun_internal (enum insertion_type type, int x_p)
  282. {
  283. enum insertion_type base_type;
  284. char **defun_args, **scan_args;
  285. const char *category;
  286. char *defined_name;
  287. char *type_name = NULL;
  288. char *type_name2 = NULL;
  289. {
  290. char *line;
  291. /* The @def.. line is the only place in Texinfo where you are
  292. allowed to use unquoted braces that don't delimit arguments of
  293. a command or a macro; in any other place it will trigger an
  294. error message from the reader loop. The special handling of
  295. this case inside `args_from_string' is an extra special hack
  296. which allows this. The side effect is that if we try to expand
  297. the rest of the line below, the recursive reader loop will
  298. signal an error if there are brace-delimited arguments on that line.
  299. The best solution to this would be to change the syntax of
  300. @def.. commands so that it doesn't violate Texinfo's own rules.
  301. But it's probably too late for this now, as it will break a lot
  302. of existing manuals.
  303. Unfortunately, this means that you can't call macros, use @value, etc.
  304. inside @def.. commands, sigh. */
  305. get_rest_of_line (0, &line);
  306. /* Basic line continuation. If a line ends with \s*@\s* concatanate
  307. the next line. */
  308. {
  309. char *next_line, *new_line;
  310. int i;
  311. line_continuation:
  312. i = strlen (line) - 1;
  313. if (i > 0 && line[i] == '@' && line[i-1] != '@')
  314. {
  315. get_rest_of_line (0, &next_line);
  316. new_line = (char *) xmalloc (i + strlen (next_line) + 2);
  317. strncpy (new_line, line, i);
  318. new_line[i] = '\0';
  319. free (line);
  320. strcat (new_line, " ");
  321. strcat (new_line, next_line);
  322. line = xstrdup (new_line);
  323. free (next_line);
  324. free (new_line);
  325. goto line_continuation;
  326. }
  327. }
  328. defun_args = (args_from_string (line));
  329. free (line);
  330. }
  331. scan_args = defun_args;
  332. /* Get base type and category string. */
  333. base_type = get_base_type (type);
  334. /* xx all these const strings should be determined upon
  335. documentlanguage argument and NOT via gettext (kama). */
  336. switch (type)
  337. {
  338. case defun:
  339. case deftypefun:
  340. category = gdt("Function");
  341. break;
  342. case defmac:
  343. category = gdt("Macro");
  344. break;
  345. case defspec:
  346. category = gdt("Special Form");
  347. break;
  348. case defvar:
  349. case deftypevar:
  350. category = gdt("Variable");
  351. break;
  352. case defopt:
  353. category = gdt("User Option");
  354. break;
  355. case defivar:
  356. case deftypeivar:
  357. category = gdt("Instance Variable");
  358. break;
  359. case defmethod:
  360. case deftypemethod:
  361. category = gdt("Method");
  362. break;
  363. default:
  364. category = next_nonwhite_defun_arg (&scan_args);
  365. break;
  366. }
  367. /* The class name. */
  368. if ((base_type == deftypecv)
  369. || (base_type == deftypefn)
  370. || (base_type == deftypevr)
  371. || (base_type == defcv)
  372. || (base_type == defop)
  373. || (base_type == deftypeivar)
  374. || (base_type == deftypemethod)
  375. || (base_type == deftypeop)
  376. )
  377. type_name = next_nonwhite_defun_arg (&scan_args);
  378. /* The type name for typed languages. */
  379. if ((base_type == deftypecv)
  380. || (base_type == deftypeivar)
  381. || (base_type == deftypemethod)
  382. || (base_type == deftypeop)
  383. )
  384. type_name2 = next_nonwhite_defun_arg (&scan_args);
  385. /* The function or whatever that's actually being defined. */
  386. defined_name = next_nonwhite_defun_arg (&scan_args);
  387. /* This hack exists solely for the purposes of formatting the Texinfo
  388. manual. I couldn't think of a better way. The token might be a
  389. simple @@ followed immediately by more text. If this is the case,
  390. then the next defun arg is part of this one, and we should
  391. concatenate them. */
  392. if (*scan_args && **scan_args && !whitespace (**scan_args)
  393. && STREQ (defined_name, "@@"))
  394. {
  395. char *tem = xmalloc (3 + strlen (scan_args[0]));
  396. sprintf (tem, "@@%s", scan_args[0]);
  397. free (scan_args[0]);
  398. scan_args[0] = tem;
  399. scan_args++;
  400. defined_name = tem;
  401. }
  402. /* It's easy to write @defun foo(arg1 arg2), but a following ( is
  403. misparsed by texinfo.tex and this is next to impossible to fix.
  404. Warn about it. */
  405. if (*scan_args && **scan_args && **scan_args == '(')
  406. warning ("`%c' follows defined name `%s' instead of whitespace",
  407. **scan_args, defined_name);
  408. if (!x_p)
  409. begin_insertion (type);
  410. /* Write the definition header line.
  411. This should start at the normal indentation. */
  412. current_indent -= default_indentation_increment;
  413. start_paragraph ();
  414. if (!html && !xml)
  415. switch (base_type)
  416. {
  417. case deffn:
  418. case defvr:
  419. case deftp:
  420. execute_string (" --- %s: %s", category, defined_name);
  421. break;
  422. case deftypefn:
  423. case deftypevr:
  424. execute_string (" --- %s: %s %s", category, type_name, defined_name);
  425. break;
  426. case defcv:
  427. execute_string (" --- %s %s %s: %s", category, gdt("of"), type_name,
  428. defined_name);
  429. break;
  430. case deftypecv:
  431. case deftypeivar:
  432. execute_string (" --- %s %s %s: %s %s", category, gdt("of"), type_name,
  433. type_name2, defined_name);
  434. break;
  435. case defop:
  436. execute_string (" --- %s %s %s: %s", category, gdt("on"), type_name,
  437. defined_name);
  438. break;
  439. case deftypeop:
  440. execute_string (" --- %s %s %s: %s %s", category, gdt("on"), type_name,
  441. type_name2, defined_name);
  442. break;
  443. case deftypemethod:
  444. execute_string (" --- %s %s %s: %s %s", category, gdt("on"), type_name,
  445. type_name2, defined_name);
  446. break;
  447. }
  448. else if (html)
  449. {
  450. /* If this is not a @def...x version, it could only
  451. be a normal version @def.... So start the table here. */
  452. if (!x_p)
  453. insert_string ("<div class=\"defun\">\n");
  454. else
  455. rollback_empty_tag ("blockquote");
  456. /* xx The single words (on, off) used here, should depend on
  457. documentlanguage and NOT on gettext --kama. */
  458. switch (base_type)
  459. {
  460. case deffn:
  461. case defvr:
  462. case deftp:
  463. case deftypefn:
  464. case deftypevr:
  465. execute_string ("--- %s: ", category);
  466. break;
  467. case defcv:
  468. case deftypecv:
  469. case deftypeivar:
  470. execute_string ("--- %s %s %s: ", category, gdt("of"), type_name);
  471. break;
  472. case defop:
  473. case deftypemethod:
  474. case deftypeop:
  475. execute_string ("--- %s %s %s: ", category, gdt("on"), type_name);
  476. break;
  477. } /* switch (base_type)... */
  478. switch (base_type)
  479. {
  480. case deffn:
  481. case defvr:
  482. case deftp:
  483. /* <var> is for the following function arguments. */
  484. insert_html_tag (START, "b");
  485. execute_string ("%s", defined_name);
  486. insert_html_tag (END, "b");
  487. insert_html_tag (START, "var");
  488. break;
  489. case deftypefn:
  490. case deftypevr:
  491. execute_string ("%s ", type_name);
  492. insert_html_tag (START, "b");
  493. execute_string ("%s", defined_name);
  494. insert_html_tag (END, "b");
  495. insert_html_tag (START, "var");
  496. break;
  497. case defcv:
  498. case defop:
  499. insert_html_tag (START, "b");
  500. execute_string ("%s", defined_name);
  501. insert_html_tag (END, "b");
  502. insert_html_tag (START, "var");
  503. break;
  504. case deftypecv:
  505. case deftypeivar:
  506. case deftypemethod:
  507. case deftypeop:
  508. execute_string ("%s ", type_name2);
  509. insert_html_tag (START, "b");
  510. execute_string ("%s", defined_name);
  511. insert_html_tag (END, "b");
  512. insert_html_tag (START, "var");
  513. break;
  514. }
  515. }
  516. else if (xml)
  517. xml_begin_def_term (base_type, category, defined_name, type_name,
  518. type_name2);
  519. current_indent += default_indentation_increment;
  520. /* Now process the function arguments, if any. If these carry onto
  521. the next line, they should be indented by two increments to
  522. distinguish them from the body of the definition, which is indented
  523. by one increment. */
  524. current_indent += default_indentation_increment;
  525. switch (base_type)
  526. {
  527. case deffn:
  528. case defop:
  529. process_defun_args (scan_args, 1);
  530. break;
  531. /* Through Makeinfo 1.67 we processed remaining args only for deftp,
  532. deftypefn, and deftypemethod. But the libc manual, for example,
  533. needs to say:
  534. @deftypevar {char *} tzname[2]
  535. And simply allowing the extra text seems far simpler than trying
  536. to invent yet more defn commands. In any case, we should either
  537. output it or give an error, not silently ignore it. */
  538. default:
  539. process_defun_args (scan_args, 0);
  540. break;
  541. }
  542. current_indent -= default_indentation_increment;
  543. if (!html)
  544. close_single_paragraph ();
  545. /* Make an entry in the appropriate index. (XML and
  546. Docbook already got their entries, so skip them.) */
  547. if (!xml)
  548. switch (base_type)
  549. {
  550. case deffn:
  551. case deftypefn:
  552. execute_string ("@findex %s\n", defined_name);
  553. break;
  554. case defcv:
  555. case deftypecv:
  556. case deftypevr:
  557. case defvr:
  558. execute_string ("@vindex %s\n", defined_name);
  559. break;
  560. case deftypeivar:
  561. execute_string ("@vindex %s %s %s\n", defined_name, gdt("of"),
  562. type_name);
  563. break;
  564. case defop:
  565. case deftypeop:
  566. case deftypemethod:
  567. execute_string ("@findex %s %s %s\n", defined_name, gdt("on"),
  568. type_name);
  569. break;
  570. case deftp:
  571. execute_string ("@tindex %s\n", defined_name);
  572. break;
  573. }
  574. if (xml)
  575. xml_end_def_term ();
  576. else if (html)
  577. {
  578. inhibit_paragraph_indentation = 1;
  579. no_indent = 1;
  580. insert_html_tag (END, "var");
  581. insert_string ("<br>\n");
  582. /* Indent the definition a bit. */
  583. add_html_block_elt ("<blockquote>");
  584. no_indent = 0;
  585. inhibit_paragraph_indentation = 0;
  586. paragraph_is_open = 0;
  587. }
  588. /* Deallocate the token list. */
  589. scan_args = defun_args;
  590. while (1)
  591. {
  592. char * arg = (*scan_args++);
  593. if (arg == NULL)
  594. break;
  595. free (arg);
  596. }
  597. free (defun_args);
  598. }
  599. /* Add an entry for a function, macro, special form, variable, or option.
  600. If the name of the calling command ends in `x', then this is an extra
  601. entry included in the body of an insertion of the same type. */
  602. void
  603. cm_defun (void)
  604. {
  605. enum insertion_type type;
  606. char *base_command = xstrdup (command); /* command with any `x' removed */
  607. /* FIXME: is strlen(command) allways > 0? */
  608. int x_p = (command[strlen (command) - 1] == 'x');
  609. if (x_p)
  610. base_command[strlen (base_command) - 1] = 0;
  611. type = find_type_from_name (base_command);
  612. /* If we are adding to an already existing insertion, then make sure
  613. that we are already in an insertion of type TYPE. */
  614. if (x_p)
  615. {
  616. INSERTION_ELT *i = insertion_stack;
  617. /* Skip over ifclear and ifset conditionals. */
  618. while (i && (i->insertion == ifset || i->insertion == ifclear))
  619. i = i->next;
  620. if (!i || i->insertion != type)
  621. {
  622. line_error (_("Must be in `@%s' environment to use `@%s'"),
  623. base_command, command);
  624. discard_until ("\n");
  625. return;
  626. }
  627. }
  628. defun_internal (type, x_p);
  629. free (base_command);
  630. }