1 /* defun.c -- @defun and friends.
2 $Id: defun.c,v 1.11 2004/04/11 17:56:46 karl Exp $
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
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 2, or (at your option)
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, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "insertion.h"
30 #define DEFUN_SELF_DELIMITING(c) \
31 ((c) == '(' || (c) == ')' || (c) == '[' || (c) == ']')
33 struct token_accumulator
41 initialize_token_accumulator (struct token_accumulator
*accumulator
)
43 accumulator
->length
= 0;
44 accumulator
->index
= 0;
45 accumulator
->tokens
= NULL
;
49 accumulate_token (struct token_accumulator
*accumulator
, char *token
)
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 (char **string_pointer
)
66 char *scan_string
= (*string_pointer
) + 1;
67 unsigned int level
= 1;
68 int started_command
= 0;
75 *string_pointer
= scan_string
;
81 /* Tweak line_number to compensate for fact that
82 we gobbled the whole line before coming here. */
84 line_error (_("Missing `}' in @def arg"));
86 *string_pointer
= scan_string
- 1;
90 if (c
== '{' && !started_command
)
92 if (c
== '}' && !started_command
)
95 /* remember if at @. */
96 started_command
= (c
== '@' && !started_command
);
100 /* Return a list of tokens from the contents of STRING.
101 Commands and brace-delimited groups count as single tokens.
102 Contiguous whitespace characters are converted to a token
103 consisting of a single space. */
105 args_from_string (char *string
)
107 struct token_accumulator accumulator
;
108 char *token_start
, *token_end
;
109 char *scan_string
= string
;
111 initialize_token_accumulator (&accumulator
);
114 { /* Replace arbitrary whitespace by a single space. */
115 if (whitespace (*scan_string
))
118 while (whitespace (*scan_string
))
120 accumulate_token ((&accumulator
), (xstrdup (" ")));
124 /* Commands count as single tokens. */
125 if (*scan_string
== COMMAND_PREFIX
)
127 token_start
= scan_string
;
129 if (self_delimiting (*scan_string
))
138 if ((c
== 0) || (c
== '{') || (whitespace (c
)))
145 if (*scan_string
== '{')
147 char *s
= scan_string
;
148 (void) scan_group_in_string (&s
);
152 token_end
= scan_string
;
155 /* Parentheses and brackets are self-delimiting. */
156 else if (DEFUN_SELF_DELIMITING (*scan_string
))
158 token_start
= scan_string
;
160 token_end
= scan_string
;
163 /* Open brace introduces a group that is a single token. */
164 else if (*scan_string
== '{')
166 char *s
= scan_string
;
167 int balanced
= scan_group_in_string (&s
);
169 token_start
= scan_string
+ 1;
171 token_end
= balanced
? (scan_string
- 1) : scan_string
;
174 /* Make commas separate tokens so to differentiate them from
175 parameter types in XML output. */
176 else if (*scan_string
== ',')
178 token_start
= scan_string
;
180 token_end
= scan_string
;
183 /* Otherwise a token is delimited by whitespace, parentheses,
184 brackets, or braces. A token is also ended by a command. */
187 token_start
= scan_string
;
195 /* Do not back up if we're looking at a }; since the only
196 valid }'s are those matched with {'s, we want to give
197 an error. If we back up, we go into an infinite loop. */
198 if (!c
|| whitespace (c
) || DEFUN_SELF_DELIMITING (c
)
205 /* End token if we are looking at a comma, as commas are
213 /* If we encounter a command embedded within a token,
214 then end the token. */
215 if (c
== COMMAND_PREFIX
)
221 token_end
= scan_string
;
224 accumulate_token (&accumulator
, substring (token_start
, token_end
));
226 accumulate_token (&accumulator
, NULL
);
227 return accumulator
.tokens
;
231 process_defun_args (char **defun_args
, int auto_var_p
)
233 int pending_space
= 0;
237 xml_process_defun_args (defun_args
, auto_var_p
);
243 char *defun_arg
= *defun_args
++;
245 if (defun_arg
== NULL
)
248 if (defun_arg
[0] == ' ')
260 if (DEFUN_SELF_DELIMITING (defun_arg
[0]))
262 /* Within @deffn and friends, texinfo.tex makes parentheses
263 sans serif and brackets bold. We use roman instead. */
265 insert_html_tag (START
, "");
267 add_char (defun_arg
[0]);
270 insert_html_tag (END
, "");
272 /* else if (defun_arg[0] == '&' || defun_arg[0] == COMMAND_PREFIX) */
273 /* execute_string ("%s", defun_arg); */
274 /* else if (auto_var_p) */
275 /* execute_string ("%s", defun_arg); */
277 execute_string ("%s", defun_arg
);
282 next_nonwhite_defun_arg (char ***arg_pointer
)
284 char **scan
= (*arg_pointer
);
285 char *arg
= (*scan
++);
287 if ((arg
!= 0) && (*arg
== ' '))
295 return (arg
== 0) ? "" : arg
;
299 /* This is needed also in insertion.c. */
302 get_base_type (int type
)
307 case defivar
: base_type
= defcv
; break;
308 case defmac
: base_type
= deffn
; break;
309 case defmethod
: base_type
= defop
; break;
310 case defopt
: base_type
= defvr
; break;
311 case defspec
: base_type
= deffn
; break;
312 case deftypecv
: base_type
= deftypecv
; break;
313 case deftypefun
: base_type
= deftypefn
; break;
314 case deftypeivar
: base_type
= deftypeivar
; break;
315 case deftypemethod
: base_type
= deftypemethod
; break;
316 case deftypeop
: base_type
= deftypeop
; break;
317 case deftypevar
: base_type
= deftypevr
; break;
318 case defun
: base_type
= deffn
; break;
319 case defvar
: base_type
= defvr
; break;
328 /* Make the defun type insertion.
329 TYPE says which insertion this is.
330 X_P, if nonzero, says not to start a new insertion. */
332 defun_internal (int type
, int x_p
)
335 char **defun_args
, **scan_args
;
336 const char *category
;
338 char *type_name
= NULL
;
339 char *type_name2
= NULL
;
344 /* The @def.. line is the only place in Texinfo where you are
345 allowed to use unquoted braces that don't delimit arguments of
346 a command or a macro; in any other place it will trigger an
347 error message from the reader loop. The special handling of
348 this case inside `args_from_string' is an extra special hack
349 which allows this. The side effect is that if we try to expand
350 the rest of the line below, the recursive reader loop will
351 signal an error if there are brace-delimited arguments on that line.
353 The best solution to this would be to change the syntax of
354 @def.. commands so that it doesn't violate Texinfo's own rules.
355 But it's probably too late for this now, as it will break a lot
358 Unfortunately, this means that you can't call macros, use @value, etc.
359 inside @def.. commands, sigh. */
360 get_rest_of_line (0, &line
);
362 /* Basic line continuation. If a line ends with \s*@\s* concatanate
365 char *next_line
, *new_line
;
369 i
= strlen (line
) - 1;
371 if (line
[i
] == '@' && line
[i
-1] != '@')
373 get_rest_of_line (0, &next_line
);
374 new_line
= (char *) xmalloc (i
+ strlen (next_line
) + 2);
375 strncpy (new_line
, line
, i
);
378 strcat (new_line
, " ");
379 strcat (new_line
, next_line
);
380 line
= xstrdup (new_line
);
384 goto line_continuation
;
388 defun_args
= (args_from_string (line
));
392 scan_args
= defun_args
;
394 /* Get base type and category string. */
395 base_type
= get_base_type (type
);
397 /* xx all these const strings should be determined upon
398 documentlanguage argument and NOT via gettext (kama). */
403 category
= _("Function");
406 category
= _("Macro");
409 category
= _("Special Form");
413 category
= _("Variable");
416 category
= _("User Option");
420 category
= _("Instance Variable");
424 category
= _("Method");
427 category
= next_nonwhite_defun_arg (&scan_args
);
431 /* The class name. */
432 if ((base_type
== deftypecv
)
433 || (base_type
== deftypefn
)
434 || (base_type
== deftypevr
)
435 || (base_type
== defcv
)
436 || (base_type
== defop
)
437 || (base_type
== deftypeivar
)
438 || (base_type
== deftypemethod
)
439 || (base_type
== deftypeop
)
441 type_name
= next_nonwhite_defun_arg (&scan_args
);
443 /* The type name for typed languages. */
444 if ((base_type
== deftypecv
)
445 || (base_type
== deftypeivar
)
446 || (base_type
== deftypemethod
)
447 || (base_type
== deftypeop
)
449 type_name2
= next_nonwhite_defun_arg (&scan_args
);
451 /* The function or whatever that's actually being defined. */
452 defined_name
= next_nonwhite_defun_arg (&scan_args
);
454 /* This hack exists solely for the purposes of formatting the Texinfo
455 manual. I couldn't think of a better way. The token might be a
456 simple @@ followed immediately by more text. If this is the case,
457 then the next defun arg is part of this one, and we should
459 if (*scan_args
&& **scan_args
&& !whitespace (**scan_args
)
460 && STREQ (defined_name
, "@@"))
462 char *tem
= xmalloc (3 + strlen (scan_args
[0]));
464 sprintf (tem
, "@@%s", scan_args
[0]);
472 /* It's easy to write @defun foo(arg1 arg2), but a following ( is
473 misparsed by texinfo.tex and this is next to impossible to fix.
475 if (*scan_args
&& **scan_args
&& **scan_args
== '(')
476 warning ("`%c' follows defined name `%s' instead of whitespace",
477 **scan_args
, defined_name
);
480 begin_insertion (type
);
482 /* Write the definition header line.
483 This should start at the normal indentation. */
484 current_indent
-= default_indentation_increment
;
493 execute_string (" --- %s: %s", category
, defined_name
);
497 execute_string (" --- %s: %s %s", category
, type_name
, defined_name
);
500 execute_string (" --- %s %s %s: %s", category
, _("of"), type_name
,
505 execute_string (" --- %s %s %s: %s %s", category
, _("of"), type_name
,
506 type_name2
, defined_name
);
509 execute_string (" --- %s %s %s: %s", category
, _("on"), type_name
,
513 execute_string (" --- %s %s %s: %s %s", category
, _("on"), type_name
,
514 type_name2
, defined_name
);
517 execute_string (" --- %s %s %s: %s %s", category
, _("on"), type_name
,
518 type_name2
, defined_name
);
523 /* If this is not a @def...x version, it could only
524 be a normal version @def.... So start the table here. */
526 insert_string ("<div class=\"defun\">\n");
528 rollback_empty_tag ("blockquote");
530 /* xx The single words (on, off) used here, should depend on
531 documentlanguage and NOT on gettext --kama. */
539 execute_string ("--- %s: ", category
);
545 execute_string ("--- %s %s %s: ", category
, _("of"), type_name
);
551 execute_string ("--- %s %s %s: ", category
, _("on"), type_name
);
553 } /* switch (base_type)... */
560 /* <var> is for the following function arguments. */
561 insert_html_tag (START
, "b");
562 execute_string ("%s", defined_name
);
563 insert_html_tag (END
, "b");
564 insert_html_tag (START
, "var");
568 execute_string ("%s ", type_name
);
569 insert_html_tag (START
, "b");
570 execute_string ("%s", defined_name
);
571 insert_html_tag (END
, "b");
572 insert_html_tag (START
, "var");
576 insert_html_tag (START
, "b");
577 execute_string ("%s", defined_name
);
578 insert_html_tag (END
, "b");
579 insert_html_tag (START
, "var");
585 execute_string ("%s ", type_name2
);
586 insert_html_tag (START
, "b");
587 execute_string ("%s", defined_name
);
588 insert_html_tag (END
, "b");
589 insert_html_tag (START
, "var");
594 xml_begin_def_term (base_type
, category
, defined_name
, type_name
,
597 current_indent
+= default_indentation_increment
;
599 /* Now process the function arguments, if any. If these carry onto
600 the next line, they should be indented by two increments to
601 distinguish them from the body of the definition, which is indented
603 current_indent
+= default_indentation_increment
;
609 process_defun_args (scan_args
, 1);
612 /* Through Makeinfo 1.67 we processed remaining args only for deftp,
613 deftypefn, and deftypemethod. But the libc manual, for example,
615 @deftypevar {char *} tzname[2]
616 And simply allowing the extra text seems far simpler than trying
617 to invent yet more defn commands. In any case, we should either
618 output it or give an error, not silently ignore it. */
620 process_defun_args (scan_args
, 0);
624 current_indent
-= default_indentation_increment
;
626 close_single_paragraph ();
628 /* Make an entry in the appropriate index. (XML and
629 Docbook already got their entries, so skip them.) */
635 execute_string ("@findex %s\n", defined_name
);
641 execute_string ("@vindex %s\n", defined_name
);
644 execute_string ("@vindex %s %s %s\n", defined_name
, _("of"),
650 execute_string ("@findex %s %s %s\n", defined_name
, _("on"),
654 execute_string ("@tindex %s\n", defined_name
);
662 inhibit_paragraph_indentation
= 1;
664 insert_html_tag (END
, "var");
665 insert_string ("<br>\n");
666 /* Indent the definition a bit. */
667 add_html_block_elt ("<blockquote>");
669 inhibit_paragraph_indentation
= 0;
670 paragraph_is_open
= 0;
673 /* Deallocate the token list. */
674 scan_args
= defun_args
;
677 char * arg
= (*scan_args
++);
685 /* Add an entry for a function, macro, special form, variable, or option.
686 If the name of the calling command ends in `x', then this is an extra
687 entry included in the body of an insertion of the same type. */
692 char *base_command
= xstrdup (command
); /* command with any `x' removed */
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
);