/* $FreeBSD: src/contrib/texinfo/makeinfo/makeinfo.c,v 1.9.2.2 2002/08/07 16:53:39 ru Exp $ */ /* $DragonFly: src/contrib/texinfo/makeinfo/Attic/makeinfo.c,v 1.2 2003/06/17 04:24:07 dillon Exp $ */ /* makeinfo -- convert Texinfo source into other formats. $Id: makeinfo.c,v 1.205 2002/03/28 16:33:48 karl Exp $ Copyright (C) 1987, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 01, 02 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Makeinfo was authored by Brian Fox (bfox@ai.mit.edu). */ #include "system.h" #include "getopt.h" #define COMPILING_MAKEINFO #include "makeinfo.h" #include "cmds.h" #include "files.h" #include "footnote.h" #include "html.h" #include "index.h" #include "insertion.h" #include "macro.h" #include "node.h" #include "toc.h" #include "xml.h" /* You can change some of the behavior of Makeinfo by changing the following defines: */ /* Define INDENT_PARAGRAPHS_IN_TABLE if you want the paragraphs which appear within an @table, @ftable, or @itemize environment to have standard paragraph indentation. Without this, such paragraphs have no starting indentation. */ /* #define INDENT_PARAGRAPHS_IN_TABLE */ /* Define PARAGRAPH_START_INDENT to be the amount of indentation that the first lines of paragraphs receive by default, where no other value has been specified. Users can change this value on the command line, with the --paragraph-indent option, or within the texinfo file, with the @paragraphindent command. */ #define PARAGRAPH_START_INDENT 3 /* Define DEFAULT_PARAGRAPH_SPACING as the number of blank lines that you wish to appear between paragraphs. A value of 1 creates a single blank line between paragraphs. Paragraphs are defined by 2 or more consecutive newlines in the input file (i.e., one or more blank lines). */ #define DEFAULT_PARAGRAPH_SPACING 1 /* Global variables. */ /* The output file name. */ char *output_filename = NULL; /* Name of the output file that the user elected to pass on the command line. Such a name overrides any name found with the @setfilename command. */ char *command_output_filename = NULL; static char *save_command_output_filename = NULL; /* Flags which control initial output string for xrefs. */ int px_ref_flag = 0; int ref_flag = 0; #define INITIAL_PARAGRAPH_SPACE 5000 int paragraph_buffer_len = INITIAL_PARAGRAPH_SPACE; /* The amount of indentation to add at the starts of paragraphs. 0 means don't change existing indentation at paragraph starts. > 0 is amount to indent new paragraphs by. < 0 means indent to column zero by removing indentation if necessary. This is normally zero, but some people prefer paragraph starts to be somewhat more indented than paragraph bodies. A pretty value for this is 3. */ int paragraph_start_indent = PARAGRAPH_START_INDENT; /* Indentation that is pending insertion. We have this for hacking lines which look blank, but contain whitespace. We want to treat those as blank lines. */ int pending_indent = 0; /* The index in our internal command table of the currently executing command. */ int command_index; /* A search string which is used to find the first @setfilename. */ char setfilename_search[] = { COMMAND_PREFIX, 's', 'e', 't', 'f', 'i', 'l', 'e', 'n', 'a', 'm', 'e', 0 }; /* Values for calling handle_variable_internal (). */ #define SET 1 #define CLEAR 2 #define IFSET 3 #define IFCLEAR 4 /* Flags controlling the operation of the program. */ /* Default is to remove output if there were errors. */ int force = 0; /* Default is to notify users of bad choices. */ int print_warnings = 1; /* Number of errors that we tolerate on a given fileset. */ int max_error_level = 100; /* The actual last inserted character. Note that this may be something other than NEWLINE even if last_char_was_newline is 1. */ int last_inserted_character = 0; /* Nonzero means that a newline character has already been inserted, so close_paragraph () should insert one less. */ int line_already_broken = 0; /* When nonzero we have finished an insertion (see end_insertion ()) and we want to ignore false continued paragraph closings. */ int insertion_paragraph_closed = 0; /* Nonzero means attempt to make all of the lines have fill_column width. */ int do_justification = 0; /* Nonzero means don't replace whitespace with   in HTML mode. */ int in_html_elt = 0; typedef struct brace_element { struct brace_element *next; COMMAND_FUNCTION *proc; char *command; int pos, line; int in_fixed_width_font; } BRACE_ELEMENT; BRACE_ELEMENT *brace_stack = NULL; extern void do_multitable (), end_multitable (); void push_node_filename (), pop_node_filename (); void remember_error (); void convert_from_stream (), convert_from_file (), convert_from_loaded_file (); void init_internals (), init_paragraph (), init_brace_stack (); void init_insertion_stack (), init_indices (); void init_tag_table (), write_tag_table (), write_tag_table_internal (); void validate_file (), validate_other_references (), split_file (); void free_node_references (), handle_variable (); void handle_variable_internal (); void normalize_node_name (); void add_anchor_name (); void free_node_node_references (), remember_node_node_reference (); char **get_brace_args (); int array_len (); void free_array (); static int end_of_sentence_p (); static void isolate_nodename (); void reader_loop (); void remember_brace (), remember_brace_1 (); void pop_and_call_brace (), discard_braces (); void add_word (), add_char (), insert (), flush_output (); void insert_string (); void close_paragraph (); void ignore_blank_line (); void do_flush_right_indentation (), discard_insertions (); void start_paragraph (), indent (); void inhibit_output_flushing (), uninhibit_output_flushing (); int set_paragraph_indent (); int self_delimiting (), search_forward (); int multitable_item (), number_of_node (); extern void add_link (), add_escaped_anchor_name (); void me_execute_string_keep_state (); void maybe_update_execution_strings (); extern char *escape_string (); extern void insert_html_tag (); extern void sectioning_html (); extern void add_link (); #if defined (VA_FPRINTF) && __STDC__ /* Unfortunately we must use prototypes if we are to use . */ void add_word_args (char *, ...); void execute_string (char *, ...); #else void add_word_args (); void execute_string (); #endif /* no prototypes */ /* Error handling. */ /* Number of errors encountered. */ int errors_printed = 0; /* Print the last error gotten from the file system. */ int fs_error (filename) char *filename; { remember_error (); perror (filename); return 0; } /* Print an error message, and return false. */ void #if defined (VA_FPRINTF) && __STDC__ error (char *format, ...) #else error (format, va_alist) char *format; va_dcl #endif { #ifdef VA_FPRINTF va_list ap; #endif remember_error (); VA_START (ap, format); #ifdef VA_FPRINTF VA_FPRINTF (stderr, format, ap); #else fprintf (stderr, format, a1, a2, a3, a4, a5, a6, a7, a8); #endif /* not VA_FPRINTF */ va_end (ap); putc ('\n', stderr); } /* Just like error (), but print the input file and line number as well. */ void #if defined (VA_FPRINTF) && __STDC__ file_line_error (char *infile, int lno, char *format, ...) #else file_line_error (infile, lno, format, va_alist) char *infile; int lno; char *format; va_dcl #endif { #ifdef VA_FPRINTF va_list ap; #endif remember_error (); fprintf (stderr, "%s:%d: ", infile, lno); VA_START (ap, format); #ifdef VA_FPRINTF VA_FPRINTF (stderr, format, ap); #else fprintf (stderr, format, a1, a2, a3, a4, a5, a6, a7, a8); #endif /* not VA_FPRINTF */ va_end (ap); fprintf (stderr, ".\n"); } /* Just like file_line_error (), but take the input file and the line number from global variables. */ void #if defined (VA_FPRINTF) && __STDC__ line_error (char *format, ...) #else line_error (format, va_alist) char *format; va_dcl #endif { #ifdef VA_FPRINTF va_list ap; #endif remember_error (); fprintf (stderr, "%s:%d: ", input_filename, line_number); VA_START (ap, format); #ifdef VA_FPRINTF VA_FPRINTF (stderr, format, ap); #else fprintf (stderr, format, a1, a2, a3, a4, a5, a6, a7, a8); #endif /* not VA_FPRINTF */ va_end (ap); fprintf (stderr, ".\n"); } void #if defined (VA_FPRINTF) && __STDC__ warning (char *format, ...) #else warning (format, va_alist) char *format; va_dcl #endif { #ifdef VA_FPRINTF va_list ap; #endif if (print_warnings) { fprintf (stderr, _("%s:%d: warning: "), input_filename, line_number); VA_START (ap, format); #ifdef VA_FPRINTF VA_FPRINTF (stderr, format, ap); #else fprintf (stderr, format, a1, a2, a3, a4, a5, a6, a7, a8); #endif /* not VA_FPRINTF */ va_end (ap); fprintf (stderr, ".\n"); } } /* Remember that an error has been printed. If more than max_error_level have been printed, then exit the program. */ void remember_error () { errors_printed++; if (max_error_level && (errors_printed > max_error_level)) { fprintf (stderr, _("Too many errors! Gave up.\n")); flush_file_stack (); cm_bye (); xexit (1); } } /* The other side of a malformed expression. */ void misplaced_brace () { line_error (_("Misplaced %c"), '}'); } /* Main. */ /* Display the version info of this invocation of Makeinfo. */ static void print_version_info () { printf ("makeinfo (GNU %s) %s\n", PACKAGE, VERSION); } /* If EXIT_VALUE is zero, print the full usage message to stdout. Otherwise, just say to use --help for more info. Then exit with EXIT_VALUE. */ static void usage (exit_value) int exit_value; { if (exit_value != 0) fprintf (stderr, _("Try `%s --help' for more information.\n"), progname); else { printf (_("Usage: %s [OPTION]... TEXINFO-FILE...\n"), progname); printf ("\n"); puts (_("\ Translate Texinfo source documentation to various other formats, by default\n\ Info files suitable for reading online with Emacs or standalone GNU Info.\n")); printf (_("\ General options:\n\ --error-limit=NUM quit after NUM errors (default %d).\n\ --force preserve output even if errors.\n\ --help display this help and exit.\n\ --no-validate suppress node cross-reference validation.\n\ --no-warn suppress warnings (but not errors).\n\ --reference-limit=NUM warn about at most NUM references (default %d).\n\ -v, --verbose explain what is being done.\n\ --version display version information and exit.\n"), max_error_level, reference_warning_limit); printf ("\n"); /* xgettext: no-wrap */ puts (_("\ Output format selection (default is to produce Info):\n\ --docbook output DocBook rather than Info.\n\ --html output HTML rather than Info.\n\ --xml output XML (TexinfoML) rather than Info.\n\ ")); puts (_("\ General output options:\n\ -E, --macro-expand FILE output macro-expanded source to FILE.\n\ ignoring any @setfilename.\n\ --no-headers suppress node separators, Node: lines, and menus\n\ from Info output (thus producing plain text)\n\ or from HTML (thus producing shorter output);\n\ also, write to standard output by default.\n\ --no-split suppress splitting of Info or HTML output,\n\ generate only one output file.\n\ --number-sections output chapter and sectioning numbers.\n\ -o, --output=FILE output to FILE (directory if split HTML),\n\ ")); printf (_("\ Options for Info and plain text:\n\ --enable-encoding output accented and special characters in\n\ Info output based on @documentencoding.\n\ --fill-column=NUM break Info lines at NUM characters (default %d).\n\ --footnote-style=STYLE output footnotes in Info according to STYLE:\n\ `separate' to put them in their own node;\n\ `end' to put them at the end of the node\n\ in which they are defined (default).\n\ --paragraph-indent=VAL indent Info paragraphs by VAL spaces (default %d).\n\ If VAL is `none', do not indent; if VAL is\n\ `asis', preserve existing indentation.\n\ --split-size=NUM split Info files at size NUM (default %d).\n"), fill_column, paragraph_start_indent, DEFAULT_SPLIT_SIZE); } printf ("\n"); puts (_("\ Input file options:\n\ --commands-in-node-names allow @ commands in node names.\n\ -D VAR define the variable VAR, as with @set.\n\ -I DIR append DIR to the @include search path.\n\ -P DIR prepend DIR to the @include search path.\n\ -U VAR undefine the variable VAR, as with @clear.\n\ ")); puts (_("\ Conditional processing in input:\n\ --ifhtml process @ifhtml and @html even if not generating HTML.\n\ --ifinfo process @ifinfo even if not generating Info.\n\ --ifplaintext process @ifplaintext even if not generating plain text.\n\ --iftex process @iftex and @tex; implies --no-split.\n\ --no-ifhtml do not process @ifhtml and @html text.\n\ --no-ifinfo do not process @ifinfo text.\n\ --no-ifplaintext do not process @ifplaintext text.\n\ --no-iftex do not process @iftex and @tex text.\n\ ")); puts (_("\ The defaults for the @if... conditionals depend on the output format:\n\ if generating HTML, --ifhtml is on and the others are off;\n\ if generating Info, --ifinfo is on and the others are off;\n\ if generating plain text, --ifplaintext is on and the others are off;\n\ ")); fputs (_("\ Examples:\n\ makeinfo foo.texi write Info to foo's @setfilename\n\ makeinfo --html foo.texi write HTML to @setfilename\n\ makeinfo --xml foo.texi write XML to @setfilename\n\ makeinfo --docbook foo.texi write DocBook XML to @setfilename\n\ makeinfo --no-headers foo.texi write plain text to standard output\n\ \n\ makeinfo --html --no-headers foo.texi write html without node lines, menus\n\ makeinfo --number-sections foo.texi write Info with numbered sections\n\ makeinfo --no-split foo.texi write one Info file however big\n\ "), stdout); puts (_("\n\ Email bug reports to bug-texinfo@gnu.org,\n\ general questions and discussion to help-texinfo@gnu.org.\n\ Texinfo home page: http://www.gnu.org/software/texinfo/")); xexit (exit_value); } struct option long_options[] = { { "commands-in-node-names", 0, &expensive_validation, 1 }, { "docbook", 0, 0, 'd' }, { "enable-encoding", 0, &enable_encoding, 1 }, { "error-limit", 1, 0, 'e' }, { "fill-column", 1, 0, 'f' }, { "footnote-style", 1, 0, 's' }, { "force", 0, &force, 1 }, { "help", 0, 0, 'h' }, { "html", 0, 0, 'w' }, { "ifhtml", 0, &process_html, 1 }, { "ifinfo", 0, &process_info, 1 }, { "ifplaintext", 0, &process_plaintext, 1 }, { "iftex", 0, &process_tex, 1 }, { "macro-expand", 1, 0, 'E' }, { "no-headers", 0, &no_headers, 1 }, { "no-ifhtml", 0, &process_html, 0 }, { "no-ifinfo", 0, &process_info, 0 }, { "no-ifplaintext", 0, &process_plaintext, 0 }, { "no-iftex", 0, &process_tex, 0 }, { "no-number-footnotes", 0, &number_footnotes, 0 }, { "no-number-sections", 0, &number_sections, 0 }, { "no-pointer-validate", 0, &validating, 0 }, { "no-split", 0, &splitting, 0 }, { "no-validate", 0, &validating, 0 }, { "no-warn", 0, &print_warnings, 0 }, { "number-footnotes", 0, &number_footnotes, 1 }, { "number-sections", 0, &number_sections, 1 }, { "output", 1, 0, 'o' }, { "paragraph-indent", 1, 0, 'p' }, { "reference-limit", 1, 0, 'r' }, { "split-size", 1, 0, 'S'}, { "verbose", 0, &verbose_mode, 1 }, { "version", 0, 0, 'V' }, { "xml", 0, 0, 'x' }, {NULL, 0, NULL, 0} }; /* For each file mentioned in the command line, process it, turning Texinfo commands into wonderfully formatted output text. */ int main (argc, argv) int argc; char **argv; { extern int errors_printed; int c, ind; int reading_from_stdin = 0; #ifdef HAVE_SETLOCALE /* Do not use LC_ALL, because LC_NUMERIC screws up the scanf parsing of the argument to @multicolumn. */ setlocale (LC_TIME, ""); setlocale (LC_MESSAGES, ""); setlocale (LC_CTYPE, ""); setlocale (LC_COLLATE, ""); #endif /* Set the text message domain. */ bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* Parse argument flags from the input line. */ while ((c = getopt_long (argc, argv, "D:de:E:f:hI:o:p:P:r:s:U:vV:wx", long_options, &ind)) != EOF) { if (c == 0 && long_options[ind].flag == 0) c = long_options[ind].val; switch (c) { case 'D': case 'U': /* User specified variable to set or clear. */ handle_variable_internal ((c == 'D') ? SET : CLEAR, optarg); break; case 'd': /* --docbook */ splitting = 0; xml = 1; docbook = 1; break; case 'e': /* --error-limit */ if (sscanf (optarg, "%d", &max_error_level) != 1) { fprintf (stderr, _("%s: %s arg must be numeric, not `%s'.\n"), "--error-limit", progname, optarg); usage (1); } break; case 'E': /* --macro-expand */ if (!macro_expansion_output_stream) { macro_expansion_filename = optarg; macro_expansion_output_stream = strcmp (optarg, "-") == 0 ? stdout : fopen (optarg, "w"); if (!macro_expansion_output_stream) error (_("Couldn't open macro expansion output `%s'"), optarg); } else error (_("Cannot specify more than one macro expansion output")); break; case 'f': /* --fill-column */ if (sscanf (optarg, "%d", &fill_column) != 1) { fprintf (stderr, _("%s: %s arg must be numeric, not `%s'.\n"), "--fill-column", progname, optarg); usage (1); } break; case 'h': /* --help */ usage (0); break; case 'I': /* Append user-specified dir to include file path. */ if (!include_files_path) include_files_path = xstrdup ("."); include_files_path = (char *) xrealloc (include_files_path, 2 + strlen (include_files_path) + strlen (optarg)); strcat (include_files_path, PATH_SEP); strcat (include_files_path, optarg); break; case 'o': /* --output */ command_output_filename = xstrdup (optarg); save_command_output_filename = command_output_filename; break; case 'p': /* --paragraph-indent */ if (set_paragraph_indent (optarg) < 0) { fprintf (stderr, _("%s: --paragraph-indent arg must be numeric/`none'/`asis', not `%s'.\n"), progname, optarg); usage (1); } break; case 'P': /* Prepend user-specified include dir to include path. */ if (!include_files_path) { include_files_path = xstrdup (optarg); include_files_path = xrealloc (include_files_path, strlen (include_files_path) + 3); /* 3 for ":.\0" */ strcat (strcat (include_files_path, PATH_SEP), "."); } else { char *tmp = xstrdup (include_files_path); include_files_path = xrealloc (include_files_path, strlen (include_files_path) + strlen (optarg) + 2); /* 2 for ":\0" */ strcpy (include_files_path, optarg); strcat (include_files_path, ":"); strcat (include_files_path, tmp); free (tmp); } break; case 'r': /* --reference-limit */ if (sscanf (optarg, "%d", &reference_warning_limit) != 1) { fprintf (stderr, _("%s: %s arg must be numeric, not `%s'.\n"), "--reference-limit", progname, optarg); usage (1); } break; case 's': /* --footnote-style */ if (set_footnote_style (optarg) < 0) { fprintf (stderr, _("%s: --footnote-style arg must be `separate' or `end', not `%s'.\n"), progname, optarg); usage (1); } footnote_style_preset = 1; break; case 'S': /* --split-size */ if (sscanf (optarg, "%d", &split_size) != 1) { fprintf (stderr, _("%s: %s arg must be numeric, not `%s'.\n"), "--split-size", progname, optarg); usage (1); } break; case 'v': verbose_mode++; break; case 'V': /* --version */ print_version_info (); puts (""); printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\ There is NO warranty. You may redistribute this software\n\ under the terms of the GNU General Public License.\n\ For more information about these matters, see the files named COPYING.\n"), "2002"); exit (0); break; case 'w': /* --html */ html = 1; process_html = 1; break; case 'x': /* --xml */ splitting = 0; xml = 1; break; case '?': usage (1); break; } } if (!validating) expensive_validation = 0; if (optind == argc) { /* Check to see if input is a file. If so, process that. */ if (!isatty (fileno (stdin))) reading_from_stdin = 1; else { fprintf (stderr, _("%s: missing file argument.\n"), progname); usage (1); } } if (no_headers) { if (html && splitting) { /* --no-headers --no-split --html indicates confusion. */ fprintf (stderr, "%s: --no-headers conflicts with --no-split for --html.\n", progname); usage (1); } /* --no-headers implies --no-split. */ splitting = 0; /* If the user did not specify an output file, use stdout. */ if (!command_output_filename) command_output_filename = xstrdup ("-"); } if (process_info == -1) { /* no explicit --[no-]ifinfo option, so we'll do @ifinfo if we're generating info or (for compatibility) plain text. */ process_info = !html && !xml; } if (process_plaintext == -1) { /* no explicit --[no-]ifplaintext option, so we'll do @ifplaintext if we're generating plain text. */ process_plaintext = no_headers && !html && !xml; } if (verbose_mode) print_version_info (); /* Remaining arguments are file names of texinfo files. Convert them, one by one. */ if (!reading_from_stdin) { while (optind != argc) convert_from_file (argv[optind++]); } else convert_from_stream (stdin, "stdin"); return errors_printed ? 2 : 0; } /* Hacking tokens and strings. */ /* Return the next token as a string pointer. We cons the string. */ char * read_token () { int i, character; char *result; /* If the first character to be read is self-delimiting, then that is the command itself. */ character = curchar (); if (self_delimiting (character)) { input_text_offset++; if (character == '\n') line_number++; result = xstrdup (" "); *result = character; return result; } for (i = 0; ((input_text_offset != input_text_length) && (character = curchar ()) && command_char (character)); i++, input_text_offset++); result = xmalloc (i + 1); memcpy (result, &input_text[input_text_offset - i], i); result[i] = 0; return result; } /* Return nonzero if CHARACTER is self-delimiting. */ int self_delimiting (character) int character; { /* @; and @\ are not Texinfo commands, but they are listed here anyway. I don't know why. --karl, 10aug96. */ return strchr ("~{|}`^\\@?=;:.-,*\'\" !\n\t", character) != NULL; } /* Clear whitespace from the front and end of string. */ void canon_white (string) char *string; { int len = strlen (string); int x; if (!len) return; for (x = 0; x < len; x++) { if (!cr_or_whitespace (string[x])) { strcpy (string, string + x); break; } } len = strlen (string); if (len) len--; while (len > -1 && cr_or_whitespace (string[len])) len--; string[len + 1] = 0; } /* Bash STRING, replacing all whitespace with just one space. */ void fix_whitespace (string) char *string; { char *temp = xmalloc (strlen (string) + 1); int string_index = 0; int temp_index = 0; int c; canon_white (string); while (string[string_index]) { c = temp[temp_index++] = string[string_index++]; if (c == ' ' || c == '\n' || c == '\t') { temp[temp_index - 1] = ' '; while ((c = string[string_index]) && (c == ' ' || c == '\t' || c == '\n')) string_index++; } } temp[temp_index] = 0; strcpy (string, temp); free (temp); } /* Discard text until the desired string is found. The string is included in the discarded text. */ void discard_until (string) char *string; { int temp = search_forward (string, input_text_offset); int tt = (temp < 0) ? input_text_length : temp + strlen (string); int from = input_text_offset; /* Find out what line we are on. */ while (from != tt) if (input_text[from++] == '\n') line_number++; if (temp < 0) { input_text_offset = input_text_length - strlen (string); if (strcmp (string, "\n") != 0) { line_error (_("Expected `%s'"), string); return; } } else input_text_offset = temp; input_text_offset += strlen (string); } /* Read characters from the file until we are at MATCH. Place the characters read into STRING. On exit input_text_offset is after the match string. Return the offset where the string starts. */ int get_until (match, string) char *match, **string; { int len, current_point, x, new_point, tem; current_point = x = input_text_offset; new_point = search_forward (match, input_text_offset); if (new_point < 0) new_point = input_text_length; len = new_point - current_point; /* Keep track of which line number we are at. */ tem = new_point + (strlen (match) - 1); while (x != tem) if (input_text[x++] == '\n') line_number++; *string = xmalloc (len + 1); memcpy (*string, &input_text[current_point], len); (*string)[len] = 0; /* Now leave input_text_offset in a consistent state. */ input_text_offset = tem; if (input_text_offset > input_text_length) input_text_offset = input_text_length; return new_point; } /* Replace input_text[FROM .. TO] with its expansion. */ void replace_with_expansion (from, to) int from, *to; { char *xp; unsigned xp_len, new_len; char *old_input = input_text; unsigned raw_len = *to - from; char *str; /* The rest of the code here moves large buffers, so let's not waste time if the input cannot possibly expand into anything. Unfortunately, we cannot avoid expansion when we see things like @code etc., even if they only asked for expansion of macros, since any Texinfo command can be potentially redefined with a macro. */ if (only_macro_expansion && memchr (input_text + from, COMMAND_PREFIX, raw_len) == 0) return; /* Get original string from input. */ str = xmalloc (raw_len + 1); memcpy (str, input_text + from, raw_len); str[raw_len] = 0; /* We are going to relocate input_text, so we had better output pending portion of input_text now, before the pointer changes. */ if (macro_expansion_output_stream && !executing_string && !me_inhibit_expansion) append_to_expansion_output (from); /* Expand it. */ xp = expansion (str, 0); xp_len = strlen (xp); free (str); /* Plunk the expansion into the middle of `input_text' -- which is terminated by a newline, not a null. Avoid expensive move of the rest of the input if the expansion has the same length as the original string. */ if (xp_len != raw_len) { new_len = from + xp_len + input_text_length - *to + 1; if (executing_string) { /* If we are in execute_string, we might need to update the relevant element in the execution_strings[] array, since it could have to be relocated from under our feet. (input_text is reallocated here as well, if needed.) */ maybe_update_execution_strings (&input_text, new_len); } else if (new_len > input_text_length + 1) /* Don't bother to realloc if we have enough space. */ input_text = xrealloc (input_text, new_len); memmove (input_text + from + xp_len, input_text + *to, input_text_length - *to + 1); *to += xp_len - raw_len; /* Since we change input_text_length here, the comparison above isn't really valid, but it seems the worst that might happen is an extra xrealloc or two, so let's not worry. */ input_text_length += xp_len - raw_len; } memcpy (input_text + from, xp, xp_len); free (xp); /* Synchronize the macro-expansion pointers with our new input_text. */ if (input_text != old_input) forget_itext (old_input); if (macro_expansion_output_stream && !executing_string) remember_itext (input_text, from); } /* Read characters from the file until we are at MATCH or end of line. Place the characters read into STRING. If EXPAND is nonzero, expand the text before looking for MATCH for those cases where MATCH might be produced by some macro. */ void get_until_in_line (expand, match, string) int expand; char *match, **string; { int real_bottom = input_text_length; int limit = search_forward ("\n", input_text_offset); if (limit < 0) limit = input_text_length; /* Replace input_text[input_text_offset .. limit-1] with its expansion. This allows the node names and menu entries themselves to be constructed via a macro, as in: @macro foo{p, q} Together: \p\ & \q\. @end macro @node @foo{A,B}, next, prev, top Otherwise, the `,' separating the macro args A and B is taken as the node argument separator, so the node name is `@foo{A'. This expansion is only necessary on the first call, since we expand the whole line then. */ if (expand) { replace_with_expansion (input_text_offset, &limit); } real_bottom = input_text_length; input_text_length = limit; get_until (match, string); input_text_length = real_bottom; } void get_rest_of_line (expand, string) int expand; char **string; { xml_no_para ++; if (expand) { char *tem; /* Don't expand non-macros in input, since we want them intact in the macro-expanded output. */ only_macro_expansion++; get_until_in_line (1, "\n", &tem); only_macro_expansion--; *string = expansion (tem, 0); free (tem); } else get_until_in_line (0, "\n", string); canon_white (*string); if (curchar () == '\n') /* as opposed to the end of the file... */ { line_number++; input_text_offset++; } xml_no_para --; } /* Backup the input pointer to the previous character, keeping track of the current line number. */ void backup_input_pointer () { if (input_text_offset) { input_text_offset--; if (curchar () == '\n') line_number--; } } /* Read characters from the file until we are at MATCH or closing brace. Place the characters read into STRING. */ void get_until_in_braces (match, string) char *match, **string; { char *temp; int i, brace = 0; int match_len = strlen (match); for (i = input_text_offset; i < input_text_length; i++) { if (i < input_text_length - 1 && input_text[i] == '@') { i++; /* skip commands like @, and @{ */ continue; } else if (input_text[i] == '{') brace++; else if (input_text[i] == '}') { brace--; /* If looking for a brace, don't stop at the interior brace, like after "baz" in "@foo{something @bar{baz} more}". */ if (brace == 0) continue; } else if (input_text[i] == '\n') line_number++; if (brace < 0 || (brace == 0 && strncmp (input_text + i, match, match_len) == 0)) break; } match_len = i - input_text_offset; temp = xmalloc (2 + match_len); memcpy (temp, input_text + input_text_offset, match_len); temp[match_len] = 0; input_text_offset = i; *string = temp; } /* Converting a file. */ /* Convert the file named by NAME. The output is saved on the file named as the argument to the @setfilename command. */ static char *suffixes[] = { /* ".txi" is checked first so that on 8+3 DOS filesystems, if they have "texinfo.txi" and "texinfo.tex" in the same directory, the former is used rather than the latter, due to file name truncation. */ ".txi", ".texinfo", ".texi", ".txinfo", "", NULL }; void initialize_conversion () { init_tag_table (); init_indices (); init_internals (); init_paragraph (); /* This is used for splitting the output file and for doing section headings. It was previously initialized in `init_paragraph', but its use there loses with the `init_paragraph' calls done by the multitable code; the tag indices get reset to zero. */ output_position = 0; } typedef struct generic_list { struct generic_list *next; } GENERIC_LIST; /* Reverse the chain of structures in LIST. Output the new head of the chain. You should always assign the output value of this function to something, or you will lose the chain. */ GENERIC_LIST * reverse_list (list) GENERIC_LIST *list; { GENERIC_LIST *next; GENERIC_LIST *prev = NULL; while (list) { next = list->next; list->next = prev; prev = list; list = next; } return prev; } /* We read in multiples of 4k, simply because it is a typical pipe size on unix systems. */ #define READ_BUFFER_GROWTH (4 * 4096) /* Convert the Texinfo file coming from the open stream STREAM. Assume the source of the stream is named NAME. */ void convert_from_stream (stream, name) FILE *stream; char *name; { char *buffer = NULL; int buffer_offset = 0, buffer_size = 0; initialize_conversion (); /* Read until the end of the stream. This isn't strictly correct, since the texinfo input may end before the stream ends, but it is a quick working hueristic. */ while (!feof (stream)) { int count; if (buffer_offset + (READ_BUFFER_GROWTH + 1) >= buffer_size) buffer = (char *) xrealloc (buffer, (buffer_size += READ_BUFFER_GROWTH)); count = fread (buffer + buffer_offset, 1, READ_BUFFER_GROWTH, stream); if (count < 0) { perror (name); xexit (1); } buffer_offset += count; if (count == 0) break; } /* Set the globals to the new file. */ input_text = buffer; input_text_length = buffer_offset; input_filename = xstrdup (name); node_filename = xstrdup (name); input_text_offset = 0; line_number = 1; /* Not strictly necessary. This magic prevents read_token () from doing extra unnecessary work each time it is called (that is a lot of times). The INPUT_TEXT_LENGTH is one past the actual end of the text. */ input_text[input_text_length] = '\n'; convert_from_loaded_file (name); } void convert_from_file (name) char *name; { int i; char *filename = xmalloc (strlen (name) + 50); initialize_conversion (); /* Try to load the file specified by NAME, concatenated with our various suffixes. Prefer files like `makeinfo.texi' to `makeinfo'. */ for (i = 0; suffixes[i]; i++) { strcpy (filename, name); strcat (filename, suffixes[i]); if (find_and_load (filename)) break; if (!suffixes[i][0] && strrchr (filename, '.')) { fs_error (filename); free (filename); return; } } if (!suffixes[i]) { fs_error (name); free (filename); return; } input_filename = filename; convert_from_loaded_file (name); } /* Given OUTPUT_FILENAME == ``/foo/bar/baz.html'', return "/foo/bar/baz/baz.html". This routine is called only if html && splitting. Split html output goes into the subdirectory of the toplevel filename, without extension. For example: @setfilename foo.info produces output in files foo/index.html, foo/second-node.html, ... But if the user said -o foo.whatever on the cmd line, then use foo.whatever unchanged. */ static char * insert_toplevel_subdirectory (output_filename) char *output_filename; { char *dir, *subdir, *base, *basename, *p; char buf[PATH_MAX]; struct stat st; static const char index_name[] = "index.html"; const int index_len = sizeof (index_name) - 1; strcpy (buf, output_filename); dir = pathname_part (buf); base = filename_part (buf); basename = xstrdup (base); /* remember real @setfilename name */ p = dir + strlen (dir) - 1; if (p > dir && IS_SLASH (*p)) *p = 0; p = strrchr (base, '.'); if (p) *p = 0; /* Split html output goes into subdirectory of toplevel name. */ subdir = ""; if (FILENAME_CMP (base, filename_part (dir)) != 0) { if (save_command_output_filename && STREQ (output_filename, save_command_output_filename)) subdir = basename; /* from user, use unchanged */ else subdir = base; /* implicit, omit suffix */ } free (output_filename); output_filename = xmalloc (strlen (dir) + 1 + strlen (basename) + 1 + index_len + 1); strcpy (output_filename, dir); if (strlen (dir)) strcat (output_filename, "/"); strcat (output_filename, subdir); if (mkdir (output_filename, 0777) == -1 && errno != EEXIST /* output_filename might exist, but be a non-directory. */ || (stat (output_filename, &st) == 0 && !S_ISDIR (st.st_mode))) { /* that failed, try subdir name with .html */ strcpy (output_filename, dir); if (strlen (dir)) strcat (output_filename, "/"); strcat (output_filename, basename); if (mkdir (output_filename, 0777) == -1) { char *errmsg = strerror (errno); if ((errno == EEXIST #ifdef __MSDOS__ || errno == EACCES #endif ) && (stat (output_filename, &st) == 0 && !S_ISDIR (st.st_mode))) errmsg = _("File exists, but is not a directory"); line_error (_("Can't create directory `%s': %s"), output_filename, errmsg); exit (1); } strcat (output_filename, "/"); } else if (strlen (subdir)) strcat (output_filename, "/"); strcat (output_filename, index_name); return output_filename; } /* FIXME: this is way too hairy */ void convert_from_loaded_file (name) char *name; { char *real_output_filename = NULL; remember_itext (input_text, 0); input_text_offset = 0; /* Avoid the `\input texinfo' line in HTML output (assuming it starts the file). */ if (looking_at ("\\input")) discard_until ("\n"); /* Search this file looking for the special string which starts conversion. Once found, we may truly begin. */ while (input_text_offset >= 0) { input_text_offset = search_forward (setfilename_search, input_text_offset); if (input_text_offset == 0 || (input_text_offset > 0 && input_text[input_text_offset -1] == '\n')) break; else if (input_text_offset > 0) input_text_offset++; } if (input_text_offset < 0) { if (!command_output_filename) { #if defined (REQUIRE_SETFILENAME) error (_("No `%s' found in `%s'"), setfilename_search, name); goto finished; #else command_output_filename = output_name_from_input_name (name); #endif /* !REQUIRE_SETFILENAME */ } { int i, end_of_first_line; /* Find the end of the first line in the file. */ for (i = 0; i < input_text_length - 1; i++) if (input_text[i] == '\n') break; end_of_first_line = i + 1; for (i = 0; i < end_of_first_line; i++) { if ((input_text[i] == '\\') && (strncmp (input_text + i + 1, "input", 5) == 0)) { input_text_offset = i; break; } } } } else input_text_offset += strlen (setfilename_search); if (!command_output_filename) { get_until ("\n", &output_filename); /* read rest of line */ if (xml && !docbook) xml_begin_document (output_filename); if (html || xml) { /* Change any extension to .html or .xml. */ char *html_name, *directory_part, *basename_part, *temp; canon_white (output_filename); directory_part = pathname_part (output_filename); basename_part = filename_part (output_filename); /* Zap any existing extension. */ temp = strrchr (basename_part, '.'); if (temp) *temp = 0; /* Construct new filename. */ html_name = xmalloc (strlen (directory_part) + strlen (basename_part) + 6); strcpy (html_name, directory_part); strcat (html_name, basename_part); strcat (html_name, html ? ".html" : ".xml"); /* Replace name from @setfilename with the html name. */ free (output_filename); output_filename = html_name; } } else { if (input_text_offset != -1) discard_until ("\n"); else input_text_offset = 0; real_output_filename = output_filename = command_output_filename; command_output_filename = NULL; /* for included files or whatever */ } canon_white (output_filename); toplevel_output_filename = xstrdup (output_filename); if (real_output_filename && strcmp (real_output_filename, "-") == 0) { if (macro_expansion_filename && strcmp (macro_expansion_filename, "-") == 0) { fprintf (stderr, _("%s: Skipping macro expansion to stdout as Info output is going there.\n"), progname); macro_expansion_output_stream = NULL; } real_output_filename = xstrdup (real_output_filename); output_stream = stdout; splitting = 0; /* Cannot split when writing to stdout. */ } else { if (html && splitting) { if (FILENAME_CMP (output_filename, NULL_DEVICE) == 0 || FILENAME_CMP (output_filename, ALSO_NULL_DEVICE) == 0) splitting = 0; else output_filename = insert_toplevel_subdirectory (output_filename); real_output_filename = xstrdup (output_filename); } else if (!real_output_filename) real_output_filename = expand_filename (output_filename, name); else real_output_filename = xstrdup (real_output_filename); output_stream = fopen (real_output_filename, "w"); } set_current_output_filename (real_output_filename); if (verbose_mode) printf (_("Making %s file `%s' from `%s'.\n"), no_headers ? "text" : html ? "HTML" : xml ? "XML" : "info", output_filename, input_filename); if (output_stream == NULL) { fs_error (real_output_filename); goto finished; } /* Make the displayable filename from output_filename. Only the base portion of the filename need be displayed. */ flush_output (); /* in case there was no @bye */ if (output_stream != stdout) pretty_output_filename = filename_part (output_filename); else pretty_output_filename = xstrdup ("stdout"); /* For this file only, count the number of newlines from the top of the file to here. This way, we keep track of line numbers for error reporting. Line_number starts at 1, since the user isn't zero-based. */ { int temp = 0; line_number = 1; while (temp != input_text_offset) if (input_text[temp++] == '\n') line_number++; } /* html fixxme: should output this as trailer on first page. */ if (!no_headers && !html && !xml) add_word_args (_("This is %s, produced by makeinfo version %s from %s.\n"), output_filename, VERSION, input_filename); close_paragraph (); reader_loop (); if (xml) xml_end_document (); finished: discard_insertions (0); close_paragraph (); flush_file_stack (); if (macro_expansion_output_stream) { fclose (macro_expansion_output_stream); if (errors_printed && !force && strcmp (macro_expansion_filename, "-") != 0 && FILENAME_CMP (macro_expansion_filename, NULL_DEVICE) != 0 && FILENAME_CMP (macro_expansion_filename, ALSO_NULL_DEVICE) != 0) { fprintf (stderr, _("%s: Removing macro output file `%s' due to errors; use --force to preserve.\n"), progname, macro_expansion_filename); if (unlink (macro_expansion_filename) < 0) perror (macro_expansion_filename); } } if (output_stream) { output_pending_notes (); if (tag_table) { tag_table = (TAG_ENTRY *) reverse_list (tag_table); if (!no_headers && !html) write_tag_table (); } if (html) { start_paragraph (); add_word ("\n"); close_paragraph (); } flush_output (); /* in case there was no @bye */ if (output_stream != stdout) fclose (output_stream); /* If validating, then validate the entire file right now. */ if (validating) validate_file (tag_table); /* If we need to output the table of contents, do it now. */ if (contents_filename || shortcontents_filename) toc_update (); if (splitting && !html && (!errors_printed || force)) split_file (real_output_filename, split_size); else if (errors_printed && !force && strcmp (real_output_filename, "-") != 0 && FILENAME_CMP (real_output_filename, NULL_DEVICE) != 0 && FILENAME_CMP (real_output_filename, ALSO_NULL_DEVICE) != 0) { /* If there were errors, and no --force, remove the output. */ fprintf (stderr, _("%s: Removing output file `%s' due to errors; use --force to preserve.\n"), progname, real_output_filename); if (unlink (real_output_filename) < 0) perror (real_output_filename); } } free (real_output_filename); } void free_and_clear (pointer) char **pointer; { if (*pointer) { free (*pointer); *pointer = NULL; } } /* Initialize some state. */ void init_internals () { free_and_clear (&output_filename); free_and_clear (&command); free_and_clear (&input_filename); free_node_references (); free_node_node_references (); toc_free (); init_insertion_stack (); init_brace_stack (); current_node = NULL; /* sometimes already freed */ command_index = 0; in_menu = 0; in_detailmenu = 0; top_node_seen = 0; non_top_node_seen = 0; node_number = -1; } void init_paragraph () { free_and_clear (&output_paragraph); output_paragraph = xmalloc (paragraph_buffer_len); output_paragraph[0] = 0; output_paragraph_offset = 0; output_column = 0; paragraph_is_open = 0; current_indent = 0; meta_char_pos = 0; } /* This is called from `reader_loop' when we are at the * beginning a menu line. */ static void handle_menu_entry () { char *tem; /* Ugh, glean_node_from_menu wants to read the * itself. */ input_text_offset--; /* Find node name in menu entry and save it in references list for later validation. Use followed_reference type for detailmenu references since we don't want to use them for default node pointers. */ tem = glean_node_from_menu (1, in_detailmenu ? followed_reference : menu_reference); if (html && tem) { /* Start a menu item with the cleaned-up line. Put an anchor around the start text (before `:' or the node name). */ char *string; discard_until ("* "); /* The line number was already incremented in reader_loop when we saw the newline, and discard_until has now incremented again. */ line_number--; if (had_menu_commentary) { add_word ("