home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / gnuinfo / Source / c / info < prev    next >
Encoding:
Text File  |  1994-10-01  |  14.6 KB  |  509 lines

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