1 /* defun.c -- @defun and friends.
2 $Id: defun.c,v 1.19 2002/03/18 16:54:54 karl Exp $
4 Copyright (C) 1998, 99, 2000, 01, 02 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "insertion.h"
27 #define DEFUN_SELF_DELIMITING(c) \
28 ((c) == '(' || (c) == ')' || (c) == '[' || (c) == ']')
30 struct token_accumulator
38 initialize_token_accumulator (accumulator)
39 struct token_accumulator *accumulator;
41 accumulator->length = 0;
42 accumulator->index = 0;
43 accumulator->tokens = NULL;
47 accumulate_token (accumulator, token)
48 struct token_accumulator *accumulator;
51 if (accumulator->index >= accumulator->length)
53 accumulator->length += 10;
54 accumulator->tokens = xrealloc (accumulator->tokens,
55 (accumulator->length * sizeof (char *)));
57 accumulator->tokens[accumulator->index] = token;
58 accumulator->index += 1;
61 /* Given STRING_POINTER pointing at an open brace, skip forward and return a
62 pointer to just past the matching close brace. */
64 scan_group_in_string (string_pointer)
65 char **string_pointer;
67 char *scan_string = (*string_pointer) + 1;
68 unsigned int level = 1;
69 int started_command = 0;
76 *string_pointer = scan_string;
82 /* Tweak line_number to compensate for fact that
83 we gobbled the whole line before coming here. */
85 line_error (_("Missing `}' in @def arg"));
87 *string_pointer = scan_string - 1;
91 if (c == '{' && !started_command)
93 if (c == '}' && !started_command)
96 /* remember if at @. */
97 started_command = (c == '@' && !started_command);
101 /* Return a list of tokens from the contents of STRING.
102 Commands and brace-delimited groups count as single tokens.
103 Contiguous whitespace characters are converted to a token
104 consisting of a single space. */
106 args_from_string (string)
109 struct token_accumulator accumulator;
110 char *token_start, *token_end;
111 char *scan_string = string;
113 initialize_token_accumulator (&accumulator);
116 { /* Replace arbitrary whitespace by a single space. */
117 if (whitespace (*scan_string))
120 while (whitespace (*scan_string))
122 accumulate_token ((&accumulator), (xstrdup (" ")));
126 /* Commands count as single tokens. */
127 if (*scan_string == COMMAND_PREFIX)
129 token_start = scan_string;
131 if (self_delimiting (*scan_string))
140 if ((c == 0) || (c == '{') || (whitespace (c)))
147 if (*scan_string == '{')
149 char *s = scan_string;
150 (void) scan_group_in_string (&s);
154 token_end = scan_string;
157 /* Parentheses and brackets are self-delimiting. */
158 else if (DEFUN_SELF_DELIMITING (*scan_string))
160 token_start = scan_string;
162 token_end = scan_string;
165 /* Open brace introduces a group that is a single token. */
166 else if (*scan_string == '{')
168 char *s = scan_string;
169 int balanced = scan_group_in_string (&s);
171 token_start = scan_string + 1;
173 token_end = balanced ? (scan_string - 1) : scan_string;
176 /* Otherwise a token is delimited by whitespace, parentheses,
177 brackets, or braces. A token is also ended by a command. */
180 token_start = scan_string;
188 /* Do not back up if we're looking at a }; since the only
189 valid }'s are those matched with {'s, we want to give
190 an error. If we back up, we go into an infinite loop. */
191 if (!c || whitespace (c) || DEFUN_SELF_DELIMITING (c)
198 /* If we encounter a command embedded within a token,
199 then end the token. */
200 if (c == COMMAND_PREFIX)
206 token_end = scan_string;
209 accumulate_token (&accumulator, substring (token_start, token_end));
211 accumulate_token (&accumulator, NULL);
212 return accumulator.tokens;
216 process_defun_args (defun_args, auto_var_p)
220 int pending_space = 0;
224 char *defun_arg = *defun_args++;
226 if (defun_arg == NULL)
229 if (defun_arg[0] == ' ')
241 if (DEFUN_SELF_DELIMITING (defun_arg[0]))
242 add_char (defun_arg[0]);
243 else if (defun_arg[0] == '&')
246 defun_arg = escape_string (xstrdup (defun_arg));
247 add_word (defun_arg);
251 add_word (defun_arg);
252 else if (defun_arg[0] == COMMAND_PREFIX)
253 execute_string ("%s", defun_arg);
257 defun_arg = escape_string (xstrdup (defun_arg));
258 add_word (defun_arg);
262 add_word (defun_arg);
264 add_word (defun_arg);
269 next_nonwhite_defun_arg (arg_pointer)
272 char **scan = (*arg_pointer);
273 char *arg = (*scan++);
275 if ((arg != 0) && (*arg == ' '))
283 return (arg == 0) ? "" : arg;
287 /* This is needed also in insertion.c. */
291 enum insertion_type type;
293 enum insertion_type base_type;
296 case defivar: base_type = defcv; break;
297 case defmac: base_type = deffn; break;
298 case defmethod: base_type = defop; break;
299 case defopt: base_type = defvr; break;
300 case defspec: base_type = deffn; break;
301 case deftypefun: base_type = deftypefn; break;
302 case deftypeivar: base_type = deftypeivar; break;
303 case deftypemethod: base_type = deftypemethod; break;
304 case deftypeop: base_type = deftypeop; break;
305 case deftypevar: base_type = deftypevr; break;
306 case defun: base_type = deffn; break;
307 case defvar: base_type = defvr; break;
316 /* Make the defun type insertion.
317 TYPE says which insertion this is.
318 X_P, if nonzero, says not to start a new insertion. */
320 defun_internal (type, x_p)
321 enum insertion_type type;
324 enum insertion_type base_type;
325 char **defun_args, **scan_args;
326 char *category, *defined_name, *type_name, *type_name2;
331 /* The @def.. line is the only place in Texinfo where you are
332 allowed to use unquoted braces that don't delimit arguments of
333 a command or a macro; in any other place it will trigger an
334 error message from the reader loop. The special handling of
335 this case inside `args_from_string' is an extra special hack
336 which allows this. The side effect is that if we try to expand
337 the rest of the line below, the recursive reader loop will
338 signal an error if there are brace-delimited arguments on that line.
340 The best solution to this would be to change the syntax of
341 @def.. commands so that it doesn't violate Texinfo's own rules.
342 But it's probably too late for this now, as it will break a lot
345 Unfortunately, this means that you can't call macros, use @value, etc.
346 inside @def.. commands, sigh. */
347 get_rest_of_line (0, &line);
348 defun_args = (args_from_string (line));
352 scan_args = defun_args;
354 /* Get base type and category string. */
355 base_type = get_base_type (type);
357 /* xx all these const strings should be determined upon
358 documentlanguage argument and NOT via gettext (kama). */
363 category = _("Function");
366 category = _("Macro");
369 category = _("Special Form");
373 category = _("Variable");
376 category = _("User Option");
380 category = _("Instance Variable");
384 category = _("Method");
387 category = next_nonwhite_defun_arg (&scan_args);
391 /* The class name. */
392 if ((base_type == deftypefn)
393 || (base_type == deftypevr)
394 || (base_type == defcv)
395 || (base_type == defop)
396 || (base_type == deftypeivar)
397 || (base_type == deftypemethod)
398 || (base_type == deftypeop)
400 type_name = next_nonwhite_defun_arg (&scan_args);
402 /* The type name for typed languages. */
403 if ((base_type == deftypemethod)
404 || (base_type == deftypeivar)
405 || (base_type == deftypeop)
407 type_name2 = next_nonwhite_defun_arg (&scan_args);
409 /* The function or whatever that's actually being defined. */
410 defined_name = next_nonwhite_defun_arg (&scan_args);
412 /* This hack exists solely for the purposes of formatting the Texinfo
413 manual. I couldn't think of a better way. The token might be a
414 simple @@ followed immediately by more text. If this is the case,
415 then the next defun arg is part of this one, and we should
417 if (*scan_args && **scan_args && !whitespace (**scan_args)
418 && STREQ (defined_name, "@@"))
420 char *tem = xmalloc (3 + strlen (scan_args[0]));
422 sprintf (tem, "@@%s", scan_args[0]);
430 /* It's easy to write @defun foo(arg1 arg2), but a following ( is
431 misparsed by texinfo.tex and this is next to impossible to fix.
433 if (*scan_args && **scan_args && **scan_args == '(')
434 warning ("`%c' follows defined name `%s' instead of whitespace",
435 **scan_args, defined_name);
438 begin_insertion (type);
440 /* Write the definition header line.
441 This should start at the normal indentation. */
442 current_indent -= default_indentation_increment;
446 /* Start the definition on new paragraph. */
450 docbook_begin_paragraph ();
453 if (!html && !docbook)
459 execute_string (" -- %s: %s", category, defined_name);
463 execute_string (" -- %s: %s %s", category, type_name, defined_name);
466 execute_string (" -- %s %s %s: %s", category, _("of"), type_name,
470 execute_string (" -- %s %s %s: %s %s", category, _("of"), type_name,
471 type_name2, defined_name);
474 execute_string (" -- %s %s %s: %s", category, _("on"), type_name,
478 execute_string (" -- %s %s %s: %s %s", category, _("on"), type_name,
479 type_name2, defined_name);
482 execute_string (" -- %s %s %s: %s %s", category, _("on"), type_name,
483 type_name2, defined_name);
489 /* If this is not a @def...x version, it could only
490 be a normal version @def.... So start the table here. */
493 add_html_elt ("<table width=");
494 add_word ("\"100%\">\n");
497 /* If this is an @def...x there has to be an other @def... before
498 it, so this is only a new row within an existing table. With
499 two complete standalone tables the gap between them is too big. */
501 add_html_elt ("<td align=\"left\">");
508 /* <i> is for the following function arguments. */
510 execute_string ("%s", defined_name);
511 add_word ("</b><i>");
515 execute_string ("%s ", type_name);
517 execute_string ("%s", defined_name);
518 add_word ("</b><i>");
523 execute_string ("%s", defined_name);
524 add_word ("</b><i>");
529 execute_string ("%s ", type_name2);
531 execute_string ("%s", defined_name);
532 add_word ("</b><i>");
546 add_word_args ("<%s>%s</%s>", DB_FUNCTION, defined_name,
551 add_word_args ("%s <%s>%s</%s>", type_name, DB_FUNCTION,
552 defined_name, DB_FUNCTION);
557 add_word_args ("%s <%s>%s</%s>", type_name2, DB_FUNCTION,
558 defined_name, DB_FUNCTION);
562 } /* if (docbook)... */
564 current_indent += default_indentation_increment;
566 /* Now process the function arguments, if any. If these carry onto
567 the next line, they should be indented by two increments to
568 distinguish them from the body of the definition, which is indented
570 current_indent += default_indentation_increment;
576 process_defun_args (scan_args, 1);
579 /* Through Makeinfo 1.67 we processed remaining args only for deftp,
580 deftypefn, and deftypemethod. But the libc manual, for example,
582 @deftypevar {char *} tzname[2]
583 And simply allowing the extra text seems far simpler than trying
584 to invent yet more defn commands. In any case, we should either
585 output it or give an error, not silently ignore it. */
587 process_defun_args (scan_args, 0);
591 current_indent -= default_indentation_increment;
592 close_single_paragraph ();
596 /* xx The single words (on, off) used here, should depend on
597 documentlanguage and NOT on gettext --kama. */
605 add_word ("</i>"); /* close italic area for arguments */
606 /* put the rest into the second column */
607 add_word ("</td>\n");
608 add_html_elt ("<td align=\"right\">");
609 execute_string ("%s", category);
613 add_word ("</td>\n");
614 add_html_elt ("<td align=\"right\">");
615 execute_string ("%s %s %s", category, _("of"), type_name);
622 add_word ("</td>\n");
623 add_html_elt ("<td align=\"right\">");
624 execute_string ("%s %s %s", category, _("on"), type_name);
629 add_word ("</td>\n");
630 add_html_elt ("<td align=\"right\">");
631 execute_string ("%s %s %s", category, _("of"), type_name);
633 } /* switch (base_type)... */
635 add_word ("</td>\n"); /* close second column */
636 add_word ("</tr>\n"); /* close row */
638 /* This is needed because I have to know if the next line is
639 normal text or another @def..x. If text follows, create a new
640 table to get the indentation for the following text.
642 This construction would fail if someone uses:
648 But we don't care. */
649 if (!looking_at ("@def"))
651 add_word ("</table>\n");
652 add_html_elt ("<table width=\"95%\" align=\"center\">");
653 add_word ("\n<tr><td>\n");
658 /* Make an entry in the appropriate index. */
663 execute_string ("@findex %s\n", defined_name);
668 execute_string ("@vindex %s\n", defined_name);
671 execute_string ("@vindex %s %s %s\n", defined_name, _("of"), type_name);
676 execute_string ("@findex %s %s %s\n", defined_name, _("on"), type_name);
679 execute_string ("@tindex %s\n", defined_name);
683 /* Deallocate the token list. */
684 scan_args = defun_args;
687 char * arg = (*scan_args++);
695 /* Add an entry for a function, macro, special form, variable, or option.
696 If the name of the calling command ends in `x', then this is an extra
697 entry included in the body of an insertion of the same type. */
702 enum insertion_type type;
703 char *temp = xstrdup (command);
705 x_p = (command[strlen (command) - 1] == 'x');
708 temp[strlen (temp) - 1] = 0;
710 type = find_type_from_name (temp);
713 /* If we are adding to an already existing insertion, then make sure
714 that we are already in an insertion of type TYPE. */
715 if (x_p && (!insertion_level || insertion_stack->insertion != type))
717 line_error (_("Must be in `%s' insertion to use `%sx'"),
719 discard_until ("\n");
723 defun_internal (type, x_p);