1 /* info.c -- Display nodes of Info files in multiple windows.
2 $Id: info.c,v 1.60 2002/03/11 19:54:29 karl Exp $
4 Copyright (C) 1993, 96, 97, 98, 99, 2000, 01, 02
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 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
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 Written by Brian Fox (bfox@ai.mit.edu). */
27 #if defined (HANDLE_MAN_PAGES)
29 #endif /* HANDLE_MAN_PAGES */
31 static char *program_name = "info";
33 /* Non-zero means search all indices for APROPOS_SEARCH_STRING. */
34 static int apropos_p = 0;
36 /* Variable containing the string to search for when apropos_p is non-zero. */
37 static char *apropos_search_string = (char *)NULL;
39 /* Non-zero means search all indices for INDEX_SEARCH_STRING. Unlike
40 apropos, this puts the user at the node, running info. */
41 static int index_search_p = 0;
43 /* Non-zero means look for the node which describes the invocation
44 and command-line options of the program, and start the info
45 session at that node. */
46 static int goto_invocation_p = 0;
48 /* Variable containing the string to search for when index_search_p is
50 static char *index_search_string = (char *)NULL;
52 /* Non-zero means print version info only. */
53 static int print_version_p = 0;
55 /* Non-zero means print a short description of the options. */
56 static int print_help_p = 0;
58 /* Array of the names of nodes that the user specified with "--node" on the
60 static char **user_nodenames = (char **)NULL;
61 static int user_nodenames_index = 0;
62 static int user_nodenames_slots = 0;
64 /* String specifying the first file to load. This string can only be set
65 by the user specifying "--file" on the command line. */
66 static char *user_filename = (char *)NULL;
68 /* String specifying the name of the file to dump nodes to. This value is
69 filled if the user speficies "--output" on the command line. */
70 static char *user_output_filename = (char *)NULL;
72 /* Non-zero indicates that when "--output" is specified, all of the menu
73 items of the specified nodes (and their subnodes as well) should be
74 dumped in the order encountered. This basically can print a book. */
75 int dump_subnodes = 0;
77 /* Non-zero means make default keybindings be loosely modeled on vi(1). */
80 /* Non-zero means don't remove ANSI escape sequences from man pages. */
81 int raw_escapes_p = 0;
84 /* Non-zero indicates that screen output should be made 'speech-friendly'.
85 Since on MSDOS the usual behavior is to write directly to the video
86 memory, speech synthesizer software cannot grab the output. Therefore,
87 we provide a user option which tells us to avoid direct screen output
88 and use stdout instead (which loses the color output). */
89 int speech_friendly = 0;
92 /* Structure describing the options that Info accepts. We pass this structure
93 to getopt_long (). If you add or otherwise change this structure, you must
94 also change the string which follows it. */
95 #define APROPOS_OPTION 1
96 #define DRIBBLE_OPTION 2
97 #define RESTORE_OPTION 3
98 #define IDXSRCH_OPTION 4
99 static struct option long_options[] = {
100 { "apropos", 1, 0, APROPOS_OPTION },
101 { "directory", 1, 0, 'd' },
102 { "dribble", 1, 0, DRIBBLE_OPTION },
103 { "file", 1, 0, 'f' },
104 { "help", 0, &print_help_p, 1 },
105 { "index-search", 1, 0, IDXSRCH_OPTION },
106 { "node", 1, 0, 'n' },
107 { "output", 1, 0, 'o' },
108 { "raw-escapes", 0, &raw_escapes_p, 1 },
109 { "restore", 1, 0, RESTORE_OPTION },
110 { "show-options", 0, 0, 'O' },
111 { "subnodes", 0, &dump_subnodes, 1 },
112 { "usage", 0, 0, 'O' },
113 { "version", 0, &print_version_p, 1 },
114 { "vi-keys", 0, &vi_keys_p, 1 },
116 { "speech-friendly", 0, &speech_friendly, 1 },
121 /* String describing the shorthand versions of the long options found above. */
123 static char *short_options = "d:n:f:o:ORsb";
125 static char *short_options = "d:n:f:o:ORs";
128 /* When non-zero, the Info window system has been initialized. */
129 int info_windows_initialized_p = 0;
131 /* Some "forward" declarations. */
132 static void info_short_help (), remember_info_program_name ();
133 static void init_messages ();
134 extern void add_file_directory_to_path ();
137 /* **************************************************************** */
139 /* Main Entry Point to the Info Program */
141 /* **************************************************************** */
148 int getopt_long_index; /* Index returned by getopt_long (). */
149 NODE *initial_node; /* First node loaded by Info. */
151 #ifdef HAVE_SETLOCALE
152 /* Set locale via LC_ALL. */
153 setlocale (LC_ALL, "");
156 /* Set the text message domain. */
157 bindtextdomain (PACKAGE, LOCALEDIR);
158 textdomain (PACKAGE);
164 int option_character;
166 option_character = getopt_long
167 (argc, argv, short_options, long_options, &getopt_long_index);
169 /* getopt_long () returns EOF when there are no more long options. */
170 if (option_character == EOF)
173 /* If this is a long option, then get the short version of it. */
174 if (option_character == 0 && long_options[getopt_long_index].flag == 0)
175 option_character = long_options[getopt_long_index].val;
177 /* Case on the option that we have received. */
178 switch (option_character)
183 /* User wants to add a directory. */
185 info_add_path (optarg, INFOPATH_PREPEND);
188 /* User is specifying a particular node. */
190 add_pointer_to_array (optarg, user_nodenames_index, user_nodenames,
191 user_nodenames_slots, 10, char *);
194 /* User is specifying a particular Info file. */
197 free (user_filename);
199 user_filename = xstrdup (optarg);
202 /* User is specifying the name of a file to output to. */
204 if (user_output_filename)
205 free (user_output_filename);
206 user_output_filename = xstrdup (optarg);
209 /* User has specified that she wants to find the "Options"
210 or "Invocation" node for the program. */
212 goto_invocation_p = 1;
215 /* User has specified that she wants the escape sequences
216 in man pages to be passed thru unaltered. */
221 /* User is specifying that she wishes to dump the subnodes of
222 the node that she is dumping. */
228 /* User wants speech-friendly output. */
232 #endif /* __MSDOS__ */
234 /* User has specified a string to search all indices for. */
237 maybe_free (apropos_search_string);
238 apropos_search_string = xstrdup (optarg);
241 /* User has specified a dribble file to receive keystrokes. */
243 close_dribble_file ();
244 open_dribble_file (optarg);
247 /* User has specified an alternate input stream. */
249 info_set_input_from_file (optarg);
252 /* User has specified a string to search all indices for. */
255 maybe_free (index_search_string);
256 index_search_string = xstrdup (optarg);
260 fprintf (stderr, _("Try --help for more information.\n"));
265 /* If the output device is not a terminal, and no output filename has been
266 specified, make user_output_filename be "-", so that the info is written
267 to stdout, and turn on the dumping of subnodes. */
268 if ((!isatty (fileno (stdout))) && (user_output_filename == (char *)NULL))
270 user_output_filename = xstrdup ("-");
274 /* If the user specified --version, then show the version and exit. */
277 printf ("%s (GNU %s) %s\n", program_name, PACKAGE, VERSION);
279 printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
280 There is NO warranty. You may redistribute this software\n\
281 under the terms of the GNU General Public License.\n\
282 For more information about these matters, see the files named COPYING.\n"),
287 /* If the `--help' option was present, show the help and exit. */
294 /* If the user hasn't specified a path for Info files, default it.
295 Lowest priority is our messy hardwired list in filesys.h.
296 Then comes the user's INFODIR from the Makefile.
297 Highest priority is the environment variable, if set. */
300 char *path_from_env = getenv ("INFOPATH");
304 unsigned len = strlen (path_from_env);
305 /* Trailing : on INFOPATH means insert the default path. */
306 if (len && path_from_env[len - 1] == PATH_SEP[0])
308 path_from_env[len - 1] = 0;
309 info_add_path (DEFAULT_INFOPATH, INFOPATH_PREPEND);
311 #ifdef INFODIR /* from the Makefile */
312 info_add_path (INFODIR, INFOPATH_PREPEND);
314 info_add_path (path_from_env, INFOPATH_PREPEND);
318 info_add_path (DEFAULT_INFOPATH, INFOPATH_PREPEND);
319 #ifdef INFODIR /* from the Makefile */
320 info_add_path (INFODIR, INFOPATH_PREPEND);
325 /* If the user specified a particular filename, add the path of that
326 file to the contents of INFOPATH. */
328 add_file_directory_to_path (user_filename);
330 /* If the user wants to search every known index for a given string,
331 do that now, and report the results. */
334 info_apropos (apropos_search_string);
338 /* Get the initial Info node. It is either "(dir)Top", or what the user
339 specifed with values in user_filename and user_nodenames. */
340 initial_node = info_get_node (user_filename,
341 user_nodenames ? user_nodenames[0] : 0);
343 /* If we couldn't get the initial node, this user is in trouble. */
346 if (info_recent_file_error)
347 info_error (info_recent_file_error);
349 info_error (msg_cant_find_node,
350 user_nodenames ? user_nodenames[0] : "Top");
354 /* Special cases for when the user specifies multiple nodes. If we
355 are dumping to an output file, dump all of the nodes specified.
356 Otherwise, attempt to create enough windows to handle the nodes
357 that this user wants displayed. */
358 if (user_nodenames_index > 1)
362 if (user_output_filename)
364 (user_filename, user_nodenames, user_output_filename, dump_subnodes);
366 begin_multiple_window_info_session (user_filename, user_nodenames);
371 /* If there are arguments remaining, they are the names of menu items
372 in sequential info files starting from the first one loaded. That
373 file name is either "dir", or the contents of user_filename if one
376 char *errstr, *errarg1, *errarg2;
377 NODE *new_initial_node = info_follow_menus (initial_node, argv + optind,
378 &errstr, &errarg1, &errarg2);
380 if (new_initial_node && new_initial_node != initial_node)
381 initial_node = new_initial_node;
383 /* If the user specified that this node should be output, then do that
384 now. Otherwise, start the Info session with this node. Or act
385 accordingly if the initial node was not found. */
386 if (user_output_filename && !goto_invocation_p)
389 dump_node_to_file (initial_node, user_output_filename,
392 info_error (errstr, errarg1, errarg2);
398 begin_info_session_with_error (initial_node, errstr,
400 /* If the user specified `--index-search=STRING' or
401 --show-options, start the info session in the node
402 corresponding to what they want. */
403 else if (index_search_p || goto_invocation_p)
407 initialize_info_session (initial_node, 0);
409 if (goto_invocation_p
410 || index_entry_exists (windows, index_search_string))
412 terminal_prep_terminal ();
413 terminal_clear_screen ();
414 info_last_executed_command = (VFunction *)NULL;
417 do_info_index_search (windows, 0, index_search_string);
420 /* If they said "info --show-options foo bar baz",
421 the last of the arguments is the program whose
422 options they want to see. */
423 char **p = argv + optind;
430 program = xstrdup (*p);
432 else if (user_filename)
433 /* If there's no command-line arguments to
434 supply the program name, use the Info file
435 name (sans extension and leading directories)
437 program = program_name_from_file_name (user_filename);
439 program = xstrdup ("");
441 info_intuit_options_node (windows, initial_node, program);
445 if (user_output_filename)
447 dump_node_to_file (windows->node, user_output_filename,
451 info_read_and_dispatch ();
453 /* On program exit, leave the cursor at the bottom of the
454 window, and restore the terminal IO. */
455 terminal_goto_xy (0, screenheight - 1);
456 terminal_clear_to_eol ();
458 terminal_unprep_terminal ();
462 fprintf (stderr, _("no index entries found for `%s'\n"),
463 index_search_string);
467 close_dribble_file ();
471 begin_info_session (initial_node);
479 add_file_directory_to_path (filename)
482 char *directory_name = xstrdup (filename);
483 char *temp = filename_non_directory (directory_name);
485 if (temp != directory_name)
487 if (HAVE_DRIVE (directory_name) && temp == directory_name + 2)
489 /* The directory of "d:foo" is stored as "d:.", to avoid
490 mixing it with "d:/" when a slash is appended. */
495 info_add_path (directory_name, INFOPATH_PREPEND);
498 free (directory_name);
502 /* Error handling. */
504 /* Non-zero if an error has been signalled. */
505 int info_error_was_printed = 0;
507 /* Non-zero means ring terminal bell on errors. */
508 int info_error_rings_bell_p = 1;
510 /* Print FORMAT with ARG1 and ARG2. If the window system was initialized,
511 then the message is printed in the echo area. Otherwise, a message is
514 info_error (format, arg1, arg2)
518 info_error_was_printed = 1;
520 if (!info_windows_initialized_p || display_inhibited)
522 fprintf (stderr, "%s: ", program_name);
523 fprintf (stderr, format, arg1, arg2);
524 fprintf (stderr, "\n");
529 if (!echo_area_is_active)
531 if (info_error_rings_bell_p)
532 terminal_ring_bell ();
533 window_message_in_echo_area (format, arg1, arg2);
539 temp = build_message_node (format, arg1, arg2);
540 if (info_error_rings_bell_p)
541 terminal_ring_bell ();
542 inform_in_echo_area (temp->contents);
543 free (temp->contents);
550 /* Produce a scaled down description of the available options to Info. */
555 static const char speech_friendly_string[] = N_("\
556 -b, --speech-friendly be friendly to speech synthesizers.\n");
558 static const char speech_friendly_string[] = "";
563 Usage: %s [OPTION]... [MENU-ITEM...]\n\
565 Read documentation in Info format.\n\
568 --apropos=STRING look up STRING in all indices of all manuals.\n\
569 -d, --directory=DIR add DIR to INFOPATH.\n\
570 --dribble=FILENAME remember user keystrokes in FILENAME.\n\
571 -f, --file=FILENAME specify Info file to visit.\n\
572 -h, --help display this help and exit.\n\
573 --index-search=STRING go to node pointed by index entry STRING.\n\
574 -n, --node=NODENAME specify nodes in first visited Info file.\n\
575 -o, --output=FILENAME output selected nodes to FILENAME.\n\
576 -R, --raw-escapes don't remove ANSI escapes from man pages.\n\
577 --restore=FILENAME read initial keystrokes from FILENAME.\n\
578 -O, --show-options, --usage go to command-line options node.\n%s\
579 --subnodes recursively output menu items.\n\
580 --vi-keys use vi-like and less-like key bindings.\n\
581 --version display version information and exit.\n\
583 The first non-option argument, if present, is the menu entry to start from;\n\
584 it is searched for in all `dir' files along INFOPATH.\n\
585 If it is not present, info merges all `dir' files and shows the result.\n\
586 Any remaining arguments are treated as the names of menu\n\
587 items relative to the initial node visited.\n\
590 info show top-level dir menu\n\
591 info emacs start at emacs node from top-level dir\n\
592 info emacs buffers start at buffers node within emacs manual\n\
593 info --show-options emacs start at node with emacs' command line options\n\
594 info -f ./foo.info show file ./foo.info, not searching dir\n\
596 program_name, speech_friendly_string);
599 Email bug reports to bug-texinfo@gnu.org,\n\
600 general questions and discussion to help-texinfo@gnu.org.\n\
601 Texinfo home page: http://www.gnu.org/software/texinfo/"));
607 /* Initialize strings for gettext. Because gettext doesn't handle N_ or
608 _ within macro definitions, we put shared messages into variables and
609 use them that way. This also has the advantage that there's only one
610 copy of the strings. */
612 char *msg_cant_find_node;
613 char *msg_cant_file_node;
614 char *msg_cant_find_window;
615 char *msg_cant_find_point;
616 char *msg_cant_kill_last;
617 char *msg_no_menu_node;
618 char *msg_no_foot_node;
619 char *msg_no_xref_node;
620 char *msg_no_pointer;
621 char *msg_unknown_command;
622 char *msg_term_too_dumb;
623 char *msg_at_node_bottom;
624 char *msg_at_node_top;
625 char *msg_one_window;
626 char *msg_win_too_small;
627 char *msg_cant_make_help;
632 msg_cant_find_node = _("Cannot find node `%s'.");
633 msg_cant_file_node = _("Cannot find node `(%s)%s'.");
634 msg_cant_find_window = _("Cannot find a window!");
635 msg_cant_find_point = _("Point doesn't appear within this window's node!");
636 msg_cant_kill_last = _("Cannot delete the last window.");
637 msg_no_menu_node = _("No menu in this node.");
638 msg_no_foot_node = _("No footnotes in this node.");
639 msg_no_xref_node = _("No cross references in this node.");
640 msg_no_pointer = _("No `%s' pointer for this node.");
641 msg_unknown_command = _("Unknown Info command `%c'; try `?' for help.");
642 msg_term_too_dumb = _("Terminal type `%s' is not smart enough to run Info.");
643 msg_at_node_bottom = _("You are already at the last page of this node.");
644 msg_at_node_top = _("You are already at the first page of this node.");
645 msg_one_window = _("Only one window.");
646 msg_win_too_small = _("Resulting window would be too small.");
647 msg_cant_make_help = _("Not enough room for a help window, please delete a window.");