home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / info / info.c < prev    next >
C/C++ Source or Header  |  1997-11-22  |  21KB  |  638 lines

  1. /* info.c -- Display nodes of Info files in multiple windows.
  2.    $Id: info.c,v 1.10 1997/07/30 15:21:44 karl Exp $
  3.  
  4.    Copyright (C) 1993, 96, 97 Free Software Foundation, Inc.
  5.  
  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)
  9.    any later version.
  10.  
  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.
  15.  
  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
  18.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20.    Written by Brian Fox (bfox@ai.mit.edu). */
  21.  
  22. #include "info.h"
  23. #include "indices.h"
  24. #include "dribble.h"
  25. #include "getopt.h"
  26. #if defined (HANDLE_MAN_PAGES)
  27. #  include "man.h"
  28. #endif /* HANDLE_MAN_PAGES */
  29.  
  30. /* The version numbers of this version of Info. */
  31. int info_major_version = 2;
  32. int info_minor_version = 17;
  33. int info_patch_level = 0;
  34.  
  35. /* Non-zero means search all indices for APROPOS_SEARCH_STRING. */
  36. static int apropos_p = 0;
  37.  
  38. /* Variable containing the string to search for when apropos_p is non-zero. */
  39. static char *apropos_search_string = (char *)NULL;
  40.  
  41. /* Non-zero means search all indices for INDEX_SEARCH_STRING.  Unlike
  42.    apropos, this puts the user at the node, running info. */
  43. static int index_search_p = 0;
  44.  
  45. /* Variable containing the string to search for when index_search_p is
  46.    non-zero. */ 
  47. static char *index_search_string = (char *)NULL;
  48.  
  49. /* Non-zero means print version info only. */
  50. static int print_version_p = 0;
  51.  
  52. /* Non-zero means print a short description of the options. */
  53. static int print_help_p = 0;
  54.  
  55. /* Array of the names of nodes that the user specified with "--node" on the
  56.    command line. */
  57. static char **user_nodenames = (char **)NULL;
  58. static int user_nodenames_index = 0;
  59. static int user_nodenames_slots = 0;
  60.  
  61. /* String specifying the first file to load.  This string can only be set
  62.    by the user specifying "--file" on the command line. */
  63. static char *user_filename = (char *)NULL;
  64.  
  65. /* String specifying the name of the file to dump nodes to.  This value is
  66.    filled if the user speficies "--output" on the command line. */
  67. static char *user_output_filename = (char *)NULL;
  68.  
  69. /* Non-zero indicates that when "--output" is specified, all of the menu
  70.    items of the specified nodes (and their subnodes as well) should be
  71.    dumped in the order encountered.  This basically can print a book. */
  72. int dump_subnodes = 0;
  73.  
  74. /* Structure describing the options that Info accepts.  We pass this structure
  75.    to getopt_long ().  If you add or otherwise change this structure, you must
  76.    also change the string which follows it. */
  77. #define APROPOS_OPTION 1
  78. #define DRIBBLE_OPTION 2
  79. #define RESTORE_OPTION 3
  80. #define IDXSRCH_OPTION 4
  81. static struct option long_options[] = {
  82.   { "apropos", 1, 0, APROPOS_OPTION },
  83.   { "directory", 1, 0, 'd' },
  84.   { "node", 1, 0, 'n' },
  85.   { "file", 1, 0, 'f' },
  86.   { "subnodes", 0, &dump_subnodes, 1 },
  87.   { "output", 1, 0, 'o' },
  88.   { "help", 0, &print_help_p, 1 },
  89.   { "version", 0, &print_version_p, 1 },
  90.   { "dribble", 1, 0, DRIBBLE_OPTION },
  91.   { "restore", 1, 0, RESTORE_OPTION },
  92.   { "index-search", 1, 0, IDXSRCH_OPTION },
  93.   {NULL, 0, NULL, 0}
  94. };
  95.  
  96. /* String describing the shorthand versions of the long options found above. */
  97. static char *short_options = "d:n:f:o:sh?";
  98.  
  99. /* When non-zero, the Info window system has been initialized. */
  100. int info_windows_initialized_p = 0;
  101.  
  102. /* Some "forward" declarations. */
  103. static void usage (), info_short_help (), remember_info_program_name ();
  104.  
  105.  
  106. /* **************************************************************** */
  107. /*                                                                  */
  108. /*                Main Entry Point to the Info Program              */
  109. /*                                                                  */
  110. /* **************************************************************** */
  111.  
  112. int
  113. main (argc, argv)
  114.      int argc;
  115.      char **argv;
  116. {
  117.   int getopt_long_index;        /* Index returned by getopt_long (). */
  118.   NODE *initial_node;           /* First node loaded by Info. */
  119.  
  120.   remember_info_program_name (argv[0]);
  121.   setvbuf(stdout, NULL, _IOFBF, BUFSIZ);
  122.  
  123. #ifdef HAVE_SETLOCALE
  124.   /* Set locale via LC_ALL.  */
  125.   setlocale (LC_ALL, "");
  126. #endif
  127.  
  128.   /* Set the text message domain.  */
  129.   bindtextdomain (PACKAGE, LOCALEDIR);
  130.   textdomain (PACKAGE);
  131.  
  132.   while (1)
  133.     {
  134.       int option_character;
  135.  
  136.       option_character = getopt_long
  137.         (argc, argv, short_options, long_options, &getopt_long_index);
  138.  
  139.       /* getopt_long () returns EOF when there are no more long options. */
  140.       if (option_character == EOF)
  141.         break;
  142.  
  143.       /* If this is a long option, then get the short version of it. */
  144.       if (option_character == 0 && long_options[getopt_long_index].flag == 0)
  145.         option_character = long_options[getopt_long_index].val;
  146.  
  147.       /* Case on the option that we have received. */
  148.       switch (option_character)
  149.         {
  150.         case 0:
  151.           break;
  152.  
  153.           /* User wants to add a directory. */
  154.         case 'd':
  155.           info_add_path (optarg, INFOPATH_PREPEND);
  156.           break;
  157.  
  158.           /* User is specifying a particular node. */
  159.         case 'n':
  160.           add_pointer_to_array (optarg, user_nodenames_index, user_nodenames,
  161.                                 user_nodenames_slots, 10, char *);
  162.           break;
  163.  
  164.           /* User is specifying a particular Info file. */
  165.         case 'f':
  166.           if (user_filename)
  167.             free (user_filename);
  168.  
  169.           user_filename = xstrdup (optarg);
  170.           break;
  171.  
  172.       /* User is requesting help. */
  173.     case 'h':
  174.       print_help_p = 1;
  175.       break;
  176.  
  177.           /* User is specifying the name of a file to output to. */
  178.         case 'o':
  179.           if (user_output_filename)
  180.             free (user_output_filename);
  181.           user_output_filename = xstrdup (optarg);
  182.           break;
  183.  
  184.           /* User is specifying that she wishes to dump the subnodes of
  185.              the node that she is dumping. */
  186.         case 's':
  187.           dump_subnodes = 1;
  188.           break;
  189.  
  190.           /* User has specified a string to search all indices for. */
  191.         case APROPOS_OPTION:
  192.           apropos_p = 1;
  193.           maybe_free (apropos_search_string);
  194.           apropos_search_string = xstrdup (optarg);
  195.           break;
  196.  
  197.           /* User has specified a dribble file to receive keystrokes. */
  198.         case DRIBBLE_OPTION:
  199.           close_dribble_file ();
  200.           open_dribble_file (optarg);
  201.           break;
  202.  
  203.           /* User has specified an alternate input stream. */
  204.         case RESTORE_OPTION:
  205.           info_set_input_from_file (optarg);
  206.           break;
  207.  
  208.           /* User has specified a string to search all indices for. */
  209.         case IDXSRCH_OPTION:
  210.           index_search_p = 1;
  211.           maybe_free (index_search_string);
  212.           index_search_string = xstrdup (optarg);
  213.           break;
  214.  
  215.         default:
  216.           usage ();
  217.         }
  218.     }
  219.  
  220.   /* If the output device is not a terminal, and no output filename has been
  221.      specified, make user_output_filename be "-", so that the info is written
  222.      to stdout, and turn on the dumping of subnodes. */
  223.   if ((!isatty (fileno (stdout))) && (user_output_filename == (char *)NULL))
  224.     {
  225.       user_output_filename = xstrdup ("-");
  226.       dump_subnodes = 1;
  227.     }
  228.  
  229.   /* If the user specified --version, then show the version and exit. */
  230.   if (print_version_p)
  231.     {
  232.       printf ("info (GNU %s %s) %s\n", PACKAGE, VERSION, version_string ());
  233.       puts ("Copyright (C) 1997 Free Software Foundation, Inc.\n\
  234. There is NO warranty.  You may redistribute this software\n\
  235. under the terms of the GNU General Public License.\n\
  236. For more information about these matters, see the files named COPYING.");
  237.       exit (0);
  238.     }
  239.  
  240.   /* If the `--help' option was present, show the help and exit. */
  241.   if (print_help_p)
  242.     {
  243.       info_short_help ();
  244.       exit (0);
  245.     }
  246.   
  247.   /* If the user hasn't specified a path for Info files, default it.  */
  248.   if (!infopath)
  249.     {
  250.       char *path_from_env = getenv ("INFOPATH");
  251.  
  252.       if (path_from_env)
  253.         {
  254.           unsigned len = strlen (path_from_env);
  255.           /* Trailing : on INFOPATH means insert the default path.  */
  256.           if (len && path_from_env[len - 1] == ':')
  257.             {
  258.               path_from_env[len - 1] = 0;
  259.               info_add_path (DEFAULT_INFOPATH, INFOPATH_PREPEND);
  260.             }
  261.           info_add_path (path_from_env, INFOPATH_PREPEND);
  262.         }
  263.       else
  264.         info_add_path (DEFAULT_INFOPATH, INFOPATH_PREPEND);
  265.     }
  266.  
  267.   /* If the user specified a particular filename, add the path of that
  268.      file to the contents of INFOPATH. */
  269.   if (user_filename)
  270.     {
  271.       char *directory_name = xstrdup (user_filename);
  272.       char *temp = filename_non_directory (directory_name);
  273.  
  274.       if (temp != directory_name)
  275.         {
  276.           *temp = 0;
  277.           info_add_path (directory_name, INFOPATH_PREPEND);
  278.         }
  279.  
  280.       free (directory_name);
  281.     }
  282.  
  283.   /* If the user wants to search every known index for a given string,
  284.      do that now, and report the results. */
  285.   if (apropos_p)
  286.     {
  287.       info_apropos (apropos_search_string);
  288.       exit (0);
  289.     }
  290.  
  291.   /* Get the initial Info node.  It is either "(dir)Top", or what the user
  292.      specifed with values in user_filename and user_nodenames. */
  293.   initial_node = info_get_node (user_filename,
  294.                                 user_nodenames ? user_nodenames[0] : NULL);
  295.  
  296.   /* If we couldn't get the initial node, this user is in trouble. */
  297.   if (!initial_node)
  298.     {
  299.       if (info_recent_file_error)
  300.         info_error (info_recent_file_error);
  301.       else
  302.         info_error
  303.           (CANT_FIND_NODE, user_nodenames ? user_nodenames[0] : "Top");
  304.       exit (1);
  305.     }
  306.  
  307.   /* Special cases for when the user specifies multiple nodes.  If we
  308.      are dumping to an output file, dump all of the nodes specified.
  309.      Otherwise, attempt to create enough windows to handle the nodes
  310.      that this user wants displayed. */
  311.   if (user_nodenames_index > 1)
  312.     {
  313.       free (initial_node);
  314.  
  315.       if (user_output_filename)
  316.         dump_nodes_to_file
  317.           (user_filename, user_nodenames, user_output_filename, dump_subnodes);
  318.       else
  319.         begin_multiple_window_info_session (user_filename, user_nodenames);
  320.  
  321.       exit (0);
  322.     }
  323.  
  324.   /* If the user specified `--index-search=STRING', start the info
  325.      session in the node corresponding to the first match. */
  326.   if (index_search_p)
  327.     {
  328.       int status = 0;
  329.  
  330.       initialize_info_session (initial_node, 0);
  331.  
  332.       if (index_entry_exists (windows, index_search_string))
  333.         {
  334.           terminal_clear_screen ();
  335.           terminal_prep_terminal ();
  336.           display_update_display (windows);
  337.           info_last_executed_command = (VFunction *)NULL;
  338.  
  339.           do_info_index_search (windows, 0, index_search_string);
  340.  
  341.           info_read_and_dispatch ();
  342.  
  343.           terminal_unprep_terminal ();
  344.  
  345.           /* On program exit, leave the cursor at the bottom of the
  346.              window, and restore the terminal IO. */
  347.           terminal_goto_xy (0, screenheight - 1);
  348.           terminal_clear_to_eol ();
  349.           fflush (stdout);
  350.         }
  351.       else
  352.         {
  353.           fputs (_("no entries found\n"), stderr);
  354.           status = 2;
  355.         }
  356.  
  357.       close_dribble_file (); 
  358.       exit (status);
  359.     }
  360.  
  361.   /* If there are arguments remaining, they are the names of menu items
  362.      in sequential info files starting from the first one loaded.  That
  363.      file name is either "dir", or the contents of user_filename if one
  364.      was specified. */
  365.   while (optind != argc)
  366.     {
  367.       REFERENCE **menu;
  368.       REFERENCE *entry;
  369.       NODE *node;
  370.       char *arg;
  371.       static char *first_arg = (char *)NULL;
  372.  
  373.       /* Remember the name of the menu entry we want. */
  374.       arg = argv[optind++];
  375.  
  376.       if (!first_arg)
  377.         first_arg = arg;
  378.  
  379.       /* Build and return a list of the menu items in this node. */
  380.       menu = info_menu_of_node (initial_node);
  381.  
  382.       /* If there wasn't a menu item in this node, stop here, but let
  383.          the user continue to use Info.  Perhaps they wanted this node
  384.          and didn't realize it. */
  385.       if (!menu)
  386.         {
  387. #if defined (HANDLE_MAN_PAGES)
  388.           if (first_arg == arg)
  389.             {
  390.               node = make_manpage_node (first_arg);
  391.               if (node)
  392.                 goto maybe_got_node;
  393.             }
  394. #endif /* HANDLE_MAN_PAGES */
  395.           begin_info_session_with_error
  396.             (initial_node, _("There is no menu in this node."));
  397.           exit (0);
  398.         }
  399.  
  400.       /* Find the specified menu item. */
  401.       entry = info_get_labeled_reference (arg, menu);
  402.  
  403.       /* If the item wasn't found, search the list sloppily.  Perhaps this
  404.          user typed "buffer" when they really meant "Buffers". */
  405.       if (!entry)
  406.         {
  407.           register int i;
  408.           int best_guess = -1;
  409.  
  410.           for (i = 0; (entry = menu[i]); i++)
  411.             {
  412.               if (strcasecmp (entry->label, arg) == 0)
  413.                 break;
  414.               else
  415.                 if (strncasecmp (entry->label, arg, strlen (arg)) == 0)
  416.                   best_guess = i;
  417.             }
  418.  
  419.           if (!entry && best_guess != -1)
  420.             entry = menu[best_guess];
  421.         }
  422.  
  423.       /* If we failed to find the reference, start Info with the current
  424.          node anyway.  It is probably a misspelling. */
  425.       if (!entry)
  426.         {
  427.           char *error_message = _("There is no menu item \"%s\" in this node.");
  428.  
  429. #if defined (HANDLE_MAN_PAGES)
  430.           if (first_arg == arg)
  431.             {
  432.               node = make_manpage_node (first_arg);
  433.               if (node)
  434.                 goto maybe_got_node;
  435.             }
  436. #endif /* HANDLE_MAN_PAGES */
  437.  
  438.           info_free_references (menu);
  439.  
  440.           /* If we were supposed to dump this node, complain. */
  441.           if (user_output_filename)
  442.             info_error (error_message, arg);
  443.           else
  444.             begin_info_session_with_error (initial_node, error_message, arg);
  445.  
  446.           exit (0);
  447.         }
  448.  
  449.       /* We have found the reference that the user specified.  Clean it
  450.          up a little bit. */
  451.       if (!entry->filename)
  452.         {
  453.           if (initial_node->parent)
  454.             entry->filename = xstrdup (initial_node->parent);
  455.           else
  456.             entry->filename = xstrdup (initial_node->filename);
  457.         }
  458.  
  459.       /* Find this node.  If we can find it, then turn the initial_node
  460.          into this one.  If we cannot find it, try using the label of the
  461.          entry as a file (i.e., "(LABEL)Top").  Otherwise the Info file is
  462.          malformed in some way, and we will just use the current value of
  463.          initial node. */
  464.       node = info_get_node (entry->filename, entry->nodename);
  465.  
  466. #if defined (HANDLE_MAN_PAGES)
  467.           if ((first_arg == arg) && !node)
  468.             {
  469.               node = make_manpage_node (first_arg);
  470.               if (node)
  471.                 goto maybe_got_node;
  472.             }
  473. #endif /* HANDLE_MAN_PAGES */
  474.  
  475.       if (!node && entry->nodename &&
  476.           (strcmp (entry->label, entry->nodename) == 0))
  477.         node = info_get_node (entry->label, "Top");
  478.  
  479.     maybe_got_node:
  480.       if (node)
  481.         {
  482.           free (initial_node);
  483.           initial_node = node;
  484.           info_free_references (menu);
  485.         }
  486.       else
  487.         {
  488.           char *temp = xstrdup (entry->label);
  489.           char *error_message;
  490.  
  491.           error_message = _("Unable to find the node referenced by \"%s\".");
  492.  
  493.           info_free_references (menu);
  494.  
  495.           /* If we were trying to dump the node, then give up.  Otherwise,
  496.              start the session with an error message. */
  497.           if (user_output_filename)
  498.             info_error (error_message, temp);
  499.           else
  500.             begin_info_session_with_error (initial_node, error_message, temp);
  501.  
  502.           exit (0);
  503.         }
  504.     }
  505.  
  506.   /* If the user specified that this node should be output, then do that
  507.      now.  Otherwise, start the Info session with this node. */
  508.   if (user_output_filename)
  509.     dump_node_to_file (initial_node, user_output_filename, dump_subnodes);
  510.   else
  511.     begin_info_session (initial_node);
  512.  
  513.   exit (0);
  514. }
  515.  
  516. /* Return a string describing the current version of Info. */
  517. char *
  518. version_string ()
  519. {
  520.   static char *vstring = (char *)NULL;
  521.  
  522.   if (!vstring)
  523.     {
  524.       vstring = (char *)xmalloc (50);
  525.       sprintf (vstring, "%d.%d", info_major_version, info_minor_version);
  526.       if (info_patch_level)
  527.         sprintf (vstring + strlen (vstring), "-p%d", info_patch_level);
  528.     }
  529.   return (vstring);
  530. }
  531.  
  532. /* **************************************************************** */
  533. /*                                                                  */
  534. /*                 Error Handling for Info                          */
  535. /*                                                                  */
  536. /* **************************************************************** */
  537.  
  538. static char *program_name = (char *)NULL;
  539.  
  540. static void
  541. remember_info_program_name (fullpath)
  542.      char *fullpath;
  543. {
  544.   char *filename;
  545.  
  546.   filename = filename_non_directory (fullpath);
  547.   program_name = xstrdup (filename);
  548. }
  549.  
  550. /* Non-zero if an error has been signalled. */
  551. int info_error_was_printed = 0;
  552.  
  553. /* Non-zero means ring terminal bell on errors. */
  554. int info_error_rings_bell_p = 1;
  555.  
  556. /* Print FORMAT with ARG1 and ARG2.  If the window system was initialized,
  557.    then the message is printed in the echo area.  Otherwise, a message is
  558.    output to stderr. */
  559. void
  560. info_error (format, arg1, arg2)
  561.      char *format;
  562.      void *arg1, *arg2;
  563. {
  564.   info_error_was_printed = 1;
  565.  
  566.   if (!info_windows_initialized_p || display_inhibited)
  567.     {
  568.       fprintf (stderr, "%s: ", program_name);
  569.       fprintf (stderr, format, arg1, arg2);
  570.       fprintf (stderr, "\n");
  571.       fflush (stderr);
  572.     }
  573.   else
  574.     {
  575.       if (!echo_area_is_active)
  576.         {
  577.           if (info_error_rings_bell_p)
  578.             terminal_ring_bell ();
  579.           window_message_in_echo_area (format, arg1, arg2);
  580.         }
  581.       else
  582.         {
  583.           NODE *temp;
  584.  
  585.           temp = build_message_node (format, arg1, arg2);
  586.           if (info_error_rings_bell_p)
  587.             terminal_ring_bell ();
  588.           inform_in_echo_area (temp->contents);
  589.           free (temp->contents);
  590.           free (temp);
  591.         }
  592.     }
  593. }
  594.  
  595. /* Produce a very brief descripton of the available options and exit with
  596.    an error. */
  597. static void
  598. usage ()
  599. {
  600.   fprintf (stderr,"\nGNU %s %s - info %s\n\n", 
  601.            PACKAGE, VERSION, version_string ());
  602.   fprintf (stderr,"%s\n%s\n%s\n%s\n%s\n",
  603. _("Usage: info [-d dir-path] [-f info-file] [-o output-file] [-n node-name]..."),
  604. _("            [--directory dir-path] [--file info-file] [--node node-name]..."),
  605. _("            [--help] [--output output-file] [--subnodes] [--version]"),
  606. _("            [--dribble dribble-file] [--restore from-file]"),
  607. _("            [menu-selection ...]"));
  608.   exit (1);
  609. }
  610.  
  611. /* Produce a scaled down description of the available options to Info. */
  612. static void
  613. info_short_help ()
  614. {
  615.   puts (_("\
  616. Here is a quick description of Info's options.  For a more complete\n\
  617. description of how to use Info, type `info info options'.\n\
  618. \n\
  619.    --directory DIR              Add DIR to INFOPATH.\n\
  620.    --dribble FILENAME           Remember user keystrokes in FILENAME.\n\
  621.    --file FILENAME              Specify Info file to visit.\n\
  622.    --node NODENAME              Specify nodes in first visited Info file.\n\
  623.    --output FILENAME            Output selected nodes to FILENAME.\n\
  624.    --restore FILENAME           Read initial keystrokes from FILENAME.\n\
  625.    --subnodes                   Recursively output menu items.\n\
  626.    --help                       Get this help message.\n\
  627.    --version                    Display Info's version information.\n\
  628. \n\
  629. Remaining arguments to Info are treated as the names of menu\n\
  630. items in the initial node visited.  You can easily move to the\n\
  631. node of your choice by specifying the menu names which describe\n\
  632. the path to that node.  For example, `info emacs buffers'.\n\
  633. \n\
  634. Email bug reports to bug-texinfo@prep.ai.mit.edu."));
  635.  
  636.   exit (0);
  637. }
  638.