home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / texinfo-3.7-src.tgz / tar.out / fsf / texinfo / info / info.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  16KB  |  560 lines

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