1 /* defun.c -- @defun and friends.
2 $Id: defun.c,v 1.18 2007/09/15 23:48:45 karl Exp $
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
5 Free Software Foundation, Inc.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "insertion.h"
29 #define DEFUN_SELF_DELIMITING(c) \
30 ((c) == '(' || (c) == ')' || (c) == '[' || (c) == ']')
32 struct token_accumulator
40 initialize_token_accumulator (struct token_accumulator *accumulator)
42 accumulator->length = 0;
43 accumulator->index = 0;
44 accumulator->tokens = NULL;
48 accumulate_token (struct token_accumulator *accumulator, char *token)
50 if (accumulator->index >= accumulator->length)
52 accumulator->length += 10;
53 accumulator->tokens = xrealloc (accumulator->tokens,
54 (accumulator->length * sizeof (char *)));
56 accumulator->tokens[accumulator->index] = token;
57 accumulator->index += 1;
60 /* Given STRING_POINTER pointing at an open brace, skip forward and return a
61 pointer to just past the matching close brace. */
63 scan_group_in_string (char **string_pointer)
65 char *scan_string = (*string_pointer) + 1;
66 unsigned int level = 1;
67 int started_command = 0;
74 *string_pointer = scan_string;
80 /* Tweak line_number to compensate for fact that
81 we gobbled the whole line before coming here. */
83 line_error (_("Missing `}' in @def arg"));
85 *string_pointer = scan_string - 1;
89 if (c == '{' && !started_command)
91 if (c == '}' && !started_command)
94 /* remember if at @. */
95 started_command = (c == '@' && !started_command);
99 /* Return a list of tokens from the contents of STRING.
100 Commands and brace-delimited groups count as single tokens.
101 Contiguous whitespace characters are converted to a token
102 consisting of a single space. */
104 args_from_string (char *string)
106 struct token_accumulator accumulator;
107 char *token_start, *token_end;
108 char *scan_string = string;
110 initialize_token_accumulator (&accumulator);
113 { /* Replace arbitrary whitespace by a single space. */
114 if (whitespace (*scan_string))
117 while (whitespace (*scan_string))
119 accumulate_token ((&accumulator), (xstrdup (" ")));
123 /* Commands count as single tokens. */
124 if (*scan_string == COMMAND_PREFIX)
126 token_start = scan_string;
128 if (self_delimiting (*scan_string))
137 if ((c == 0) || (c == '{') || (whitespace (c)))
144 if (*scan_string == '{')
146 char *s = scan_string;
147 (void) scan_group_in_string (&s);
151 token_end = scan_string;
154 /* Parentheses and brackets are self-delimiting. */
155 else if (DEFUN_SELF_DELIMITING (*scan_string))
157 token_start = scan_string;
159 token_end = scan_string;
162 /* Open brace introduces a group that is a single token. */
163 else if (*scan_string == '{')
165 char *s = scan_string;
166 int balanced = scan_group_in_string (&s);
168 token_start = scan_string + 1;
170 token_end = balanced ? (scan_string - 1) : scan_string;
173 /* Make commas separate tokens so to differentiate them from
174 parameter types in XML output. */
175 else if (*scan_string == ',')
177 token_start = scan_string;
179 token_end = scan_string;
182 /* Otherwise a token is delimited by whitespace, parentheses,
183 brackets, or braces. A token is also ended by a command. */
186 token_start = scan_string;
194 /* Do not back up if we're looking at a }; since the only
195 valid }'s are those matched with {'s, we want to give
196 an error. If we back up, we go into an infinite loop. */
197 if (!c || whitespace (c) || DEFUN_SELF_DELIMITING (c)
204 /* End token if we are looking at a comma, as commas are
212 /* If we encounter a command embedded within a token,
213 then end the token. */
214 if (c == COMMAND_PREFIX)
220 token_end = scan_string;
223 accumulate_token (&accumulator, substring (token_start, token_end));
225 accumulate_token (&accumulator, NULL);
226 return accumulator.tokens;
230 process_defun_args (char **defun_args, int auto_var_p)
232 int pending_space = 0;
236 xml_process_defun_args (defun_args, auto_var_p);
242 char *defun_arg = *defun_args++;
244 if (defun_arg == NULL)
247 if (defun_arg[0] == ' ')
259 if (DEFUN_SELF_DELIMITING (defun_arg[0]))
261 /* Within @deffn and friends, texinfo.tex makes parentheses
262 sans serif and brackets bold. We use roman instead. */
264 insert_html_tag (START, "");
266 add_char (defun_arg[0]);
269 insert_html_tag (END, "");
271 /* else if (defun_arg[0] == '&' || defun_arg[0] == COMMAND_PREFIX) */
272 /* execute_string ("%s", defun_arg); */
273 /* else if (auto_var_p) */
274 /* execute_string ("%s", defun_arg); */
276 execute_string ("%s", defun_arg);
281 next_nonwhite_defun_arg (char ***arg_pointer)
283 char **scan = (*arg_pointer);
284 char *arg = (*scan++);
286 if ((arg != 0) && (*arg == ' '))
294 return (arg == 0) ? "" : arg;
298 /* This is needed also in insertion.c. */
301 get_base_type (enum insertion_type type)
303 enum insertion_type base_type;
306 case defivar: base_type = defcv; break;
307 case defmac: base_type = deffn; break;
308 case defmethod: base_type = defop; break;
309 case defopt: base_type = defvr; break;
310 case defspec: base_type = deffn; break;
311 case deftypecv: base_type = deftypecv; break;
312 case deftypefun: base_type = deftypefn; break;
313 case deftypeivar: base_type = deftypeivar; break;
314 case deftypemethod: base_type = deftypemethod; break;
315 case deftypeop: base_type = deftypeop; break;
316 case deftypevar: base_type = deftypevr; break;
317 case defun: base_type = deffn; break;
318 case defvar: base_type = defvr; break;
327 /* Make the defun type insertion.
328 TYPE says which insertion this is.
329 X_P, if nonzero, says not to start a new insertion. */
331 defun_internal (enum insertion_type type, int x_p)
333 enum insertion_type base_type;
334 char **defun_args, **scan_args;
335 const char *category;
337 char *type_name = NULL;
338 char *type_name2 = NULL;
343 /* The @def.. line is the only place in Texinfo where you are
344 allowed to use unquoted braces that don't delimit arguments of
345 a command or a macro; in any other place it will trigger an
346 error message from the reader loop. The special handling of
347 this case inside `args_from_string' is an extra special hack
348 which allows this. The side effect is that if we try to expand
349 the rest of the line below, the recursive reader loop will
350 signal an error if there are brace-delimited arguments on that line.
352 The best solution to this would be to change the syntax of
353 @def.. commands so that it doesn't violate Texinfo's own rules.
354 But it's probably too late for this now, as it will break a lot
357 Unfortunately, this means that you can't call macros, use @value, etc.
358 inside @def.. commands, sigh. */
359 get_rest_of_line (0, &line);
361 /* Basic line continuation. If a line ends with \s*@\s* concatanate
364 char *next_line, *new_line;
368 i = strlen (line) - 1;
370 if (i > 0 && line[i] == '@' && line[i-1] != '@')
372 get_rest_of_line (0, &next_line);
373 new_line = (char *) xmalloc (i + strlen (next_line) + 2);
374 strncpy (new_line, line, i);
377 strcat (new_line, " ");
378 strcat (new_line, next_line);
379 line = xstrdup (new_line);
383 goto line_continuation;
387 defun_args = (args_from_string (line));
391 scan_args = defun_args;
393 /* Get base type and category string. */
394 base_type = get_base_type (type);
396 /* xx all these const strings should be determined upon
397 documentlanguage argument and NOT via gettext (kama). */
402 category = gdt("Function");
405 category = gdt("Macro");
408 category = gdt("Special Form");
412 category = gdt("Variable");
415 category = gdt("User Option");
419 category = gdt("Instance Variable");
423 category = gdt("Method");
426 category = next_nonwhite_defun_arg (&scan_args);
430 /* The class name. */
431 if ((base_type == deftypecv)
432 || (base_type == deftypefn)
433 || (base_type == deftypevr)
434 || (base_type == defcv)
435 || (base_type == defop)
436 || (base_type == deftypeivar)
437 || (base_type == deftypemethod)
438 || (base_type == deftypeop)
440 type_name = next_nonwhite_defun_arg (&scan_args);
442 /* The type name for typed languages. */
443 if ((base_type == deftypecv)
444 || (base_type == deftypeivar)
445 || (base_type == deftypemethod)
446 || (base_type == deftypeop)
448 type_name2 = next_nonwhite_defun_arg (&scan_args);
450 /* The function or whatever that's actually being defined. */
451 defined_name = next_nonwhite_defun_arg (&scan_args);
453 /* This hack exists solely for the purposes of formatting the Texinfo
454 manual. I couldn't think of a better way. The token might be a
455 simple @@ followed immediately by more text. If this is the case,
456 then the next defun arg is part of this one, and we should
458 if (*scan_args && **scan_args && !whitespace (**scan_args)
459 && STREQ (defined_name, "@@"))
461 char *tem = xmalloc (3 + strlen (scan_args[0]));
463 sprintf (tem, "@@%s", scan_args[0]);
471 /* It's easy to write @defun foo(arg1 arg2), but a following ( is
472 misparsed by texinfo.tex and this is next to impossible to fix.
474 if (*scan_args && **scan_args && **scan_args == '(')
475 warning ("`%c' follows defined name `%s' instead of whitespace",
476 **scan_args, defined_name);
479 begin_insertion (type);
481 /* Write the definition header line.
482 This should start at the normal indentation. */
483 current_indent -= default_indentation_increment;
492 execute_string (" --- %s: %s", category, defined_name);
496 execute_string (" --- %s: %s %s", category, type_name, defined_name);
499 execute_string (" --- %s %s %s: %s", category, gdt("of"), type_name,
504 execute_string (" --- %s %s %s: %s %s", category, gdt("of"), type_name,
505 type_name2, defined_name);
508 execute_string (" --- %s %s %s: %s", category, gdt("on"), type_name,
512 execute_string (" --- %s %s %s: %s %s", category, gdt("on"), type_name,
513 type_name2, defined_name);
516 execute_string (" --- %s %s %s: %s %s", category, gdt("on"), type_name,
517 type_name2, defined_name);
522 /* If this is not a @def...x version, it could only
523 be a normal version @def.... So start the table here. */
525 insert_string ("<div class=\"defun\">\n");
527 rollback_empty_tag ("blockquote");
529 /* xx The single words (on, off) used here, should depend on
530 documentlanguage and NOT on gettext --kama. */
538 execute_string ("--- %s: ", category);
544 execute_string ("--- %s %s %s: ", category, gdt("of"), type_name);
550 execute_string ("--- %s %s %s: ", category, gdt("on"), type_name);
552 } /* switch (base_type)... */
559 /* <var> is for the following function arguments. */
560 insert_html_tag (START, "b");
561 execute_string ("%s", defined_name);
562 insert_html_tag (END, "b");
563 insert_html_tag (START, "var");
567 execute_string ("%s ", type_name);
568 insert_html_tag (START, "b");
569 execute_string ("%s", defined_name);
570 insert_html_tag (END, "b");
571 insert_html_tag (START, "var");
575 insert_html_tag (START, "b");
576 execute_string ("%s", defined_name);
577 insert_html_tag (END, "b");
578 insert_html_tag (START, "var");
584 execute_string ("%s ", type_name2);
585 insert_html_tag (START, "b");
586 execute_string ("%s", defined_name);
587 insert_html_tag (END, "b");
588 insert_html_tag (START, "var");
593 xml_begin_def_term (base_type, category, defined_name, type_name,
596 current_indent += default_indentation_increment;
598 /* Now process the function arguments, if any. If these carry onto
599 the next line, they should be indented by two increments to
600 distinguish them from the body of the definition, which is indented
602 current_indent += default_indentation_increment;
608 process_defun_args (scan_args, 1);
611 /* Through Makeinfo 1.67 we processed remaining args only for deftp,
612 deftypefn, and deftypemethod. But the libc manual, for example,
614 @deftypevar {char *} tzname[2]
615 And simply allowing the extra text seems far simpler than trying
616 to invent yet more defn commands. In any case, we should either
617 output it or give an error, not silently ignore it. */
619 process_defun_args (scan_args, 0);
623 current_indent -= default_indentation_increment;
625 close_single_paragraph ();
627 /* Make an entry in the appropriate index. (XML and
628 Docbook already got their entries, so skip them.) */
634 execute_string ("@findex %s\n", defined_name);
640 execute_string ("@vindex %s\n", defined_name);
643 execute_string ("@vindex %s %s %s\n", defined_name, gdt("of"),
649 execute_string ("@findex %s %s %s\n", defined_name, gdt("on"),
653 execute_string ("@tindex %s\n", defined_name);
661 inhibit_paragraph_indentation = 1;
663 insert_html_tag (END, "var");
664 insert_string ("<br>\n");
665 /* Indent the definition a bit. */
666 add_html_block_elt ("<blockquote>");
668 inhibit_paragraph_indentation = 0;
669 paragraph_is_open = 0;
672 /* Deallocate the token list. */
673 scan_args = defun_args;
676 char * arg = (*scan_args++);
684 /* Add an entry for a function, macro, special form, variable, or option.
685 If the name of the calling command ends in `x', then this is an extra
686 entry included in the body of an insertion of the same type. */
690 enum insertion_type type;
691 char *base_command = xstrdup (command); /* command with any `x' removed */
692 /* FIXME: is strlen(command) allways > 0? */
693 int x_p = (command[strlen (command) - 1] == 'x');
696 base_command[strlen (base_command) - 1] = 0;
698 type = find_type_from_name (base_command);
700 /* If we are adding to an already existing insertion, then make sure
701 that we are already in an insertion of type TYPE. */
704 INSERTION_ELT *i = insertion_stack;
705 /* Skip over ifclear and ifset conditionals. */
706 while (i && (i->insertion == ifset || i->insertion == ifclear))
709 if (!i || i->insertion != type)
711 line_error (_("Must be in `@%s' environment to use `@%s'"),
712 base_command, command);
713 discard_until ("\n");
718 defun_internal (type, x_p);