home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / util / install-info.c < prev    next >
Text File  |  1997-11-15  |  36KB  |  1,152 lines

  1. /* install-info -- create Info directory entry(ies) for an Info file.
  2.    $Id: install-info.c,v 1.14 1997/07/25 18:09:04 karl Exp $
  3.  
  4.    Copyright (C) 1996, 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 of the License, or
  9.    (at your option) 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. #include "system.h"
  21. #include <getopt.h>
  22.  
  23. /* Name this program was invoked with.  */
  24. char *progname;
  25.  
  26. char *readfile ();
  27. struct line_data *findlines ();
  28. void fatal ();
  29. void insert_entry_here ();
  30. int compare_section_names ();
  31.  
  32. struct spec_entry;
  33.  
  34. /* Data structures.  */
  35.  
  36.  
  37. /* Record info about a single line from a file as read into core.  */
  38. struct line_data
  39. {
  40.   /* The start of the line.  */
  41.   char *start;
  42.   /* The number of characters in the line,
  43.      excluding the terminating newline.  */
  44.   int size;
  45.   /* Vector containing pointers to the entries to add before this line.
  46.      The vector is null-terminated.  */
  47.   struct spec_entry **add_entries_before;
  48.   /* 1 means output any needed new sections before this line.  */
  49.   int add_sections_before;
  50.   /* 1 means don't output this line.  */
  51.   int delete;
  52. };
  53.  
  54.  
  55. /* This is used for a list of the specified menu section names
  56.    in which entries should be added.  */
  57. struct spec_section
  58. {
  59.   struct spec_section *next;
  60.   char *name;
  61.   /* 1 means we have not yet found an existing section with this name
  62.      in the dir file--so we will need to add a new section.  */
  63.   int missing;
  64. };
  65.  
  66.  
  67. /* This is used for a list of the entries specified to be added.  */
  68. struct spec_entry
  69. {
  70.   struct spec_entry *next;
  71.   char *text;
  72. };
  73.  
  74.  
  75. /* This is used for a list of nodes found by parsing the dir file.  */
  76. struct node
  77. {
  78.   struct node *next;
  79.   /* The node name.  */
  80.   char *name;
  81.   /* The line number of the line where the node starts.
  82.      This is the line that contains control-underscore.  */
  83.   int start_line;
  84.   /* The line number of the line where the node ends,
  85.      which is the end of the file or where the next line starts.  */
  86.   int end_line;
  87.   /* Start of first line in this node's menu
  88.      (the line after the * Menu: line).  */
  89.   char *menu_start;
  90.   /* The start of the chain of sections in this node's menu.  */
  91.   struct menu_section *sections;
  92.   /* The last menu section in the chain.  */
  93.   struct menu_section *last_section;
  94. };
  95.  
  96.  
  97. /* This is used for a list of sections found in a node's menu.
  98.    Each  struct node  has such a list in the  sections  field.  */
  99. struct menu_section
  100. {
  101.   struct menu_section *next;
  102.   char *name;
  103.   /* Line number of start of section.  */
  104.   int start_line;
  105.   /* Line number of end of section.  */
  106.   int end_line;
  107. };
  108.  
  109. /* Memory allocation and string operations.  */
  110.  
  111. /* Like malloc but get fatal error if memory is exhausted.  */
  112. void *
  113. xmalloc (size)
  114.      unsigned int size;
  115. {
  116.   extern void *malloc ();
  117.   void *result = malloc (size);
  118.   if (result == NULL)
  119.     fatal (_("virtual memory exhausted"), 0);
  120.   return result;
  121. }
  122.  
  123. /* Like realloc but get fatal error if memory is exhausted.  */
  124. void *
  125. xrealloc (obj, size)
  126.      void *obj;
  127.      unsigned int size;
  128. {
  129.   extern void *realloc ();
  130.   void *result = realloc (obj, size);
  131.   if (result == NULL)
  132.     fatal (_("virtual memory exhausted"), 0);
  133.   return result;
  134. }
  135.  
  136. /* Return a newly-allocated string
  137.    whose contents concatenate those of S1, S2, S3.  */
  138. char *
  139. concat (s1, s2, s3)
  140.      char *s1, *s2, *s3;
  141. {
  142.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  143.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  144.  
  145.   strcpy (result, s1);
  146.   strcpy (result + len1, s2);
  147.   strcpy (result + len1 + len2, s3);
  148.   *(result + len1 + len2 + len3) = 0;
  149.  
  150.   return result;
  151. }
  152.  
  153. /* Return a string containing SIZE characters
  154.    copied from starting at STRING.  */
  155.  
  156. char *
  157. copy_string (string, size)
  158.      char *string;
  159.      int size;
  160. {
  161.   int i;
  162.   char *copy = (char *) xmalloc (size + 1);
  163.   for (i = 0; i < size; i++)
  164.     copy[i] = string[i];
  165.   copy[size] = 0;
  166.   return copy;
  167. }
  168.  
  169. /* Error message functions.  */
  170.  
  171. /* Print error message.  S1 is printf control string, S2 and S3 args for it. */
  172.  
  173. /* VARARGS1 */
  174. void
  175. error (s1, s2, s3)
  176.      char *s1, *s2, *s3;
  177. {
  178.   fprintf (stderr, "%s: ", progname);
  179.   fprintf (stderr, s1, s2, s3);
  180.   putc ('\n', stderr);
  181. }
  182.  
  183. /* VARARGS1 */
  184. void
  185. warning (s1, s2, s3)
  186.      char *s1, *s2, *s3;
  187. {
  188.   fprintf (stderr, _("%s: warning: "), progname);
  189.   fprintf (stderr, s1, s2, s3);
  190.   putc ('\n', stderr);
  191. }
  192.  
  193. /* Print error message and exit.  */
  194.  
  195. void
  196. fatal (s1, s2, s3)
  197.      char *s1, *s2, *s3;
  198. {
  199.   error (s1, s2, s3);
  200.   exit (1);
  201. }
  202.  
  203. /* Print fatal error message based on errno, with file name NAME.  */
  204.  
  205. void
  206. pfatal_with_name (name)
  207.      char *name;
  208. {
  209.   char *s = concat ("", strerror (errno), _(" for %s"));
  210.   fatal (s, name);
  211. }
  212.  
  213. /* Given the full text of a menu entry, null terminated,
  214.    return just the menu item name (copied).  */
  215.  
  216. char *
  217. extract_menu_item_name (item_text)
  218.      char *item_text;
  219. {
  220.   char *p;
  221.  
  222.   if (*item_text == '*')
  223.     item_text++;
  224.   while (*item_text == ' ')
  225.     item_text++;
  226.  
  227.   p = item_text;
  228.   while (*p && *p != ':') p++;
  229.   return copy_string (item_text, p - item_text);
  230. }
  231.  
  232. /* Given the full text of a menu entry, terminated by null or newline,
  233.    return just the menu item file (copied).  */
  234.  
  235. char *
  236. extract_menu_file_name (item_text)
  237.      char *item_text;
  238. {
  239.   char *p = item_text;
  240.  
  241.   /* If we have text that looks like * ITEM: (FILE)NODE...,
  242.      extract just FILE.  Otherwise return "(none)".  */
  243.  
  244.   if (*p == '*')
  245.     p++;
  246.   while (*p == ' ')
  247.     p++;
  248.  
  249.   /* Skip to and past the colon.  */
  250.   while (*p && *p != '\n' && *p != ':') p++;
  251.   if (*p == ':') p++;
  252.  
  253.   /* Skip past the open-paren.  */
  254.   while (1)
  255.     {
  256.       if (*p == '(')
  257.         break;
  258.       else if (*p == ' ' || *p == '\t')
  259.         p++;
  260.       else
  261.         return "(none)";
  262.     }
  263.   p++;
  264.  
  265.   item_text = p;
  266.  
  267.   /* File name ends just before the close-paren.  */
  268.   while (*p && *p != '\n' && *p != ')') p++;
  269.   if (*p != ')')
  270.     return "(none)";
  271.  
  272.   return copy_string (item_text, p - item_text);
  273. }
  274.  
  275. void
  276. suggest_asking_for_help ()
  277. {
  278.   fprintf (stderr, _("\tTry `%s --help' for a complete list of options.\n"),
  279.            progname);
  280.   exit (1);
  281. }
  282.  
  283. void
  284. print_help ()
  285. {
  286.   printf ("\nGNU %s %s - install-info\n\nUsage: ", PACKAGE, VERSION);
  287.   printf (_("%s [OPTION]... [INFO-FILE [DIR-FILE]]\n\
  288.   Install INFO-FILE in the Info directory file DIR-FILE.\n\
  289. \n\
  290. Options:\n\
  291. --delete          Delete existing entries in INFO-FILE;\n\
  292.                     don't insert any new entries.\n\
  293. --dir-file=NAME   Specify file name of Info directory file.\n\
  294.                     This is equivalent to using the DIR-FILE argument.\n\
  295. --entry=TEXT      Insert TEXT as an Info directory entry.\n\
  296.                     TEXT should have the form of an Info menu item line\n\
  297.                     plus zero or more extra lines starting with whitespace.\n\
  298.                     If you specify more than one entry, they are all added.\n\
  299.                     If you don't specify any entries, they are determined\n\
  300.                     from information in the Info file itself.\n\
  301. --help            Display this help and exit.\n\
  302. --info-file=FILE  Specify Info file to install in the directory.\n\
  303.                     This is equivalent to using the INFO-FILE argument.\n\
  304. --info-dir=DIR    Same as --dir-file=DIR/dir.\n\
  305. --item=TEXT       Same as --entry TEXT.\n\
  306.                     An Info directory entry is actually a menu item.\n\
  307. --quiet           Suppress warnings.\n\
  308. --remove          Same as --delete.\n\
  309. --section=SEC     Put this file's entries in section SEC of the directory.\n\
  310.                     If you specify more than one section, all the entries\n\
  311.                     are added in each of the sections.\n\
  312.                     If you don't specify any sections, they are determined\n\
  313.                     from information in the Info file itself.\n\
  314. --version         Display version information and exit.\n\
  315. \n\
  316. Email bug reports to bug-texinfo@prep.ai.mit.edu.\n\
  317. "), progname);
  318. }
  319.  
  320. /* If DIRFILE does not exist, create a minimal one (or abort).  If it
  321.    already exists, do nothing.  */
  322. void
  323. ensure_dirfile_exists (dirfile)
  324.      char *dirfile;
  325. {
  326.   int desc = open (dirfile, O_RDONLY);
  327.   if (desc < 0 && errno == ENOENT)
  328.     {
  329.       FILE *f;
  330.       char *readerr = strerror (errno);
  331.       close (desc);
  332.       f = fopen (dirfile, "w");
  333.       if (f)
  334.         {
  335.           fputs (_("This is the file .../info/dir, which contains the\n\
  336. topmost node of the Info hierarchy, called (dir)Top.\n\
  337. The first time you invoke Info you start off looking at this node.\n\
  338. \n\
  339. File: dir       Node: Top       This is the top of the INFO tree\n\
  340. \n\
  341.   This (the Directory node) gives a menu of major topics.\n\
  342.   Typing \"q\" exits, \"?\" lists all Info commands, \"d\" returns here,\n\
  343.   \"h\" gives a primer for first-timers,\n\
  344.   \"mEmacs<Return>\" visits the Emacs manual, etc.\n\
  345. \n\
  346.   In Emacs, you can click mouse button 2 on a menu item or cross reference\n\
  347.   to select it.\n\
  348. \n\
  349. * Menu:\n\
  350. "), f);
  351.           if (fclose (f) < 0)
  352.             pfatal_with_name (dirfile);
  353.         }
  354.       else
  355.         {
  356.           /* Didn't exist, but couldn't open for writing.  */
  357.           fprintf (stderr,
  358.                    _("%s: could not read (%s) and could not create (%s)\n"),
  359.                    dirfile, readerr, strerror (errno));
  360.           exit (1);
  361.         }
  362.     }
  363.   else
  364.     close (desc); /* It already existed, so fine.  */
  365. }
  366.  
  367. /* This table defines all the long-named options, says whether they
  368.    use an argument, and maps them into equivalent single-letter options.  */
  369.  
  370. struct option longopts[] =
  371. {
  372.   { "delete",                   no_argument, NULL, 'r' },
  373.   { "dir-file",                 required_argument, NULL, 'd' },
  374.   { "entry",                    required_argument, NULL, 'e' },
  375.   { "help",                     no_argument, NULL, 'h' },
  376.   { "info-dir",                 required_argument, NULL, 'D' },
  377.   { "info-file",                required_argument, NULL, 'i' },
  378.   { "item",                     required_argument, NULL, 'e' },
  379.   { "quiet",                    no_argument, NULL, 'q' },
  380.   { "remove",                   no_argument, NULL, 'r' },
  381.   { "section",                  required_argument, NULL, 's' },
  382.   { "version",                  no_argument, NULL, 'V' },
  383.   { 0 }
  384. };
  385.  
  386. int
  387. main (argc, argv)
  388.      int argc;
  389.      char **argv;
  390. {
  391.   char *infile = 0, *dirfile = 0;
  392.   char *infile_sans_info;
  393.   unsigned infilelen_sans_info;
  394.   FILE *output;
  395.  
  396.   /* Record the text of the Info file, as a sequence of characters
  397.      and as a sequence of lines.  */
  398.   char *input_data;
  399.   int input_size;
  400.   struct line_data *input_lines;
  401.   int input_nlines;
  402.  
  403.   /* Record here the specified section names and directory entries.  */
  404.   struct spec_section *input_sections = NULL;
  405.   struct spec_entry *entries_to_add = NULL;
  406.   int n_entries_to_add = 0;
  407.  
  408.   /* Record the old text of the dir file, as plain characters,
  409.      as lines, and as nodes.  */
  410.   char *dir_data;
  411.   int dir_size;
  412.   int dir_nlines;
  413.   struct line_data *dir_lines;
  414.   struct node *dir_nodes;
  415.  
  416.   /* Nonzero means --delete was specified (just delete existing entries).  */
  417.   int delete_flag = 0;
  418.   int something_deleted = 0;
  419.   /* Nonzero means -q was specified.  */
  420.   int quiet_flag = 0;
  421.  
  422.   int node_header_flag;
  423.   int prefix_length;
  424.   int i;
  425.  
  426.   progname = argv[0];
  427.  
  428.   if (argc == 1)
  429.   {
  430.     print_help ();
  431.     exit (0);
  432.   }
  433.  
  434. #ifdef HAVE_SETLOCALE
  435.   /* Set locale via LC_ALL.  */
  436.   setlocale (LC_ALL, "");
  437. #endif
  438.  
  439.   /* Set the text message domain.  */
  440.   bindtextdomain (PACKAGE, LOCALEDIR);
  441.   textdomain (PACKAGE);
  442.  
  443.   while (1)
  444.     {
  445.       int opt = getopt_long (argc, argv, "i:d:e:s:hHr", longopts, 0);
  446.  
  447.       if (opt == EOF)
  448.         break;
  449.  
  450.       switch (opt)
  451.         {
  452.         case 0:
  453.           /* If getopt returns 0, then it has already processed a
  454.              long-named option.  We should do nothing.  */
  455.           break;
  456.  
  457.         case 1:
  458.           abort ();
  459.  
  460.         case 'd':
  461.           if (dirfile)
  462.             {
  463.               fprintf (stderr, _("%s: Specify the Info directory only once.\n"),
  464.                        progname);
  465.               suggest_asking_for_help ();
  466.             }
  467.           dirfile = optarg;
  468.           break;
  469.  
  470.         case 'D':
  471.           if (dirfile)
  472.             {
  473.               fprintf (stderr, _("%s: Specify the Info directory only once.\n"),
  474.                        progname);
  475.               suggest_asking_for_help ();
  476.             }
  477.           dirfile = concat (optarg, "", "/dir");
  478.           break;
  479.  
  480.         case 'e':
  481.           {
  482.             struct spec_entry *next
  483.               = (struct spec_entry *) xmalloc (sizeof (struct spec_entry));
  484.             if (! (*optarg != 0 && optarg[strlen (optarg) - 1] == '\n'))
  485.               optarg = concat (optarg, "\n", "");
  486.             next->text = optarg;
  487.             next->next = entries_to_add;
  488.             entries_to_add = next;
  489.             n_entries_to_add++;
  490.           }
  491.           break;
  492.  
  493.         case 'h':
  494.         case 'H':
  495.           print_help ();
  496.           exit (0);
  497.  
  498.         case 'i':
  499.           if (infile)
  500.             {
  501.               fprintf (stderr, _("%s: Specify the Info file only once.\n"),
  502.                        progname);
  503.               suggest_asking_for_help ();
  504.             }
  505.           infile = optarg;
  506.           break;
  507.  
  508.         case 'q':
  509.           quiet_flag = 1;
  510.           break;
  511.  
  512.         case 'r':
  513.           delete_flag = 1;
  514.           break;
  515.  
  516.         case 's':
  517.           {
  518.             struct spec_section *next
  519.               = (struct spec_section *) xmalloc (sizeof (struct spec_section));
  520.             next->name = optarg;
  521.             next->next = input_sections;
  522.             next->missing = 1;
  523.             input_sections = next;
  524.           }
  525.           break;
  526.  
  527.         case 'V':
  528.           printf (_("install-info (GNU %s) %s\n"), PACKAGE, VERSION);
  529. puts (_("Copyright (C) 1996 Free Software Foundation, Inc.\n\
  530. There is NO warranty.  You may redistribute this software\n\
  531. under the terms of the GNU General Public License.\n\
  532. For more information about these matters, see the files named COPYING."));
  533.           exit (0);
  534.  
  535.         default:
  536.           suggest_asking_for_help ();
  537.         }
  538.     }
  539.  
  540.   /* Interpret the non-option arguments as file names.  */
  541.   for (; optind < argc; ++optind)
  542.     {
  543.       if (infile == 0)
  544.         infile = argv[optind];
  545.       else if (dirfile == 0)
  546.         dirfile = argv[optind];
  547.       else
  548.         error (_("excess command line argument `%s'"), argv[optind]);
  549.     }
  550.  
  551.   if (!infile)
  552.     fatal (_("No input file specified; try --help for more information."));
  553.   if (!dirfile)
  554.     fatal (_("No dir file specified; try --help for more information."));
  555.  
  556.   /* Read the Info file and parse it into lines.  */
  557.  
  558.   input_data = readfile (infile, &input_size);
  559.   input_lines = findlines (input_data, input_size, &input_nlines);
  560.  
  561.   /* Parse the input file to find the section names it specifies.  */
  562.  
  563.   if (input_sections == 0)
  564.     {
  565.       prefix_length = strlen ("INFO-DIR-SECTION ");
  566.       for (i = 0; i < input_nlines; i++)
  567.         {
  568.           if (!strncmp ("INFO-DIR-SECTION ", input_lines[i].start,
  569.                         prefix_length))
  570.             {
  571.               struct spec_section *next
  572.                 = (struct spec_section *) xmalloc (sizeof (struct spec_section));
  573.               next->name = copy_string (input_lines[i].start + prefix_length,
  574.                                         input_lines[i].size - prefix_length);
  575.               next->next = input_sections;
  576.               next->missing = 1;
  577.               input_sections = next;
  578.             }
  579.         }
  580.     }
  581.  
  582.   /* Default to section "Miscellaneous" if no sections specified.  */
  583.   if (input_sections == 0)
  584.     {
  585.       input_sections
  586.         = (struct spec_section *) xmalloc (sizeof (struct spec_section));
  587.       input_sections->name = "Miscellaneous";
  588.       input_sections->next = 0;
  589.       input_sections->missing = 1;
  590.     }
  591.  
  592.   /* Now find the directory entries specified in the file
  593.      and put them on entries_to_add.  But not if entries
  594.      were specified explicitly with command options.  */
  595.  
  596.   if (entries_to_add == 0)
  597.     {
  598.       char *start_of_this_entry = 0;
  599.       for (i = 0; i < input_nlines; i++)
  600.         {
  601.           if (!strncmp ("START-INFO-DIR-ENTRY", input_lines[i].start,
  602.                         input_lines[i].size)
  603.               && sizeof ("START-INFO-DIR-ENTRY") - 1 == input_lines[i].size)
  604.             {
  605.               if (start_of_this_entry != 0)
  606.                 fatal (_("START-INFO-DIR-ENTRY without matching END-INFO-DIR-ENTRY"));
  607.               start_of_this_entry = input_lines[i + 1].start;
  608.             }
  609.           if (!strncmp ("END-INFO-DIR-ENTRY", input_lines[i].start,
  610.                         input_lines[i].size)
  611.               && sizeof ("END-INFO-DIR-ENTRY") - 1 == input_lines[i].size)
  612.             {
  613.               if (start_of_this_entry != 0)
  614.                 {
  615.                   struct spec_entry *next
  616.                     = (struct spec_entry *) xmalloc (sizeof (struct spec_entry));
  617.                   next->text = copy_string (start_of_this_entry,
  618.                                             input_lines[i].start - start_of_this_entry);
  619.                   next->next = entries_to_add;
  620.                   entries_to_add = next;
  621.                   n_entries_to_add++;
  622.                   start_of_this_entry = 0;
  623.                 }
  624.               else
  625.                 fatal (_("END-INFO-DIR-ENTRY without matching START-INFO-DIR-ENTRY"));
  626.             }
  627.         }
  628.       if (start_of_this_entry != 0)
  629.         fatal (_("START-INFO-DIR-ENTRY without matching END-INFO-DIR-ENTRY"));
  630.     }
  631.  
  632.   if (!delete_flag)
  633.     if (entries_to_add == 0)
  634.       { /* No need to abort here, the original info file may not have
  635.            the requisite Texinfo commands.  This is not something an
  636.            installer should have to correct (it's a problem for the
  637.            maintainer), and there's no need to cause subsequent parts of
  638.            `make install' to fail.  */
  639.         warning (_("no info dir entry in `%s'"), infile);
  640.         exit (0);
  641.       }
  642.  
  643.   /* Now read in the Info dir file.  */
  644.   ensure_dirfile_exists (dirfile);
  645.   dir_data = readfile (dirfile, &dir_size);
  646.   dir_lines = findlines (dir_data, dir_size, &dir_nlines);
  647.  
  648.   /* We will be comparing the entries in the dir file against the
  649.      current filename, so need to strip off any directory prefix and any
  650.      .info suffix.  */
  651.   {
  652.     unsigned basename_len;
  653.     char *infile_basename = strrchr (infile, '/');
  654.     if (infile_basename)
  655.       infile_basename++;
  656.     else
  657.       infile_basename = infile;
  658.     
  659.     basename_len = strlen (infile_basename);
  660.     infile_sans_info
  661.       = (strlen (infile_basename) > 5
  662.          && strcmp (infile_basename + basename_len - 5, ".info") == 0)
  663.         ? copy_string (infile_basename, basename_len - 5)
  664.         : infile_basename;
  665.  
  666.     infilelen_sans_info = strlen (infile_sans_info);
  667.   }
  668.   
  669.   /* Parse the dir file.  Find all the nodes, and their menus,
  670.      and the sections of their menus.  */
  671.  
  672.   dir_nodes = 0;
  673.   node_header_flag = 0;
  674.   for (i = 0; i < dir_nlines; i++)
  675.     {
  676.       /* Parse node header lines.  */
  677.       if (node_header_flag)
  678.         {
  679.           int j, end;
  680.           for (j = 0; j < dir_lines[i].size; j++)
  681.             /* Find the node name and store it in the `struct node'.  */
  682.             if (!strncmp ("Node:", dir_lines[i].start + j, 5))
  683.               {
  684.                 char *line = dir_lines[i].start;
  685.                 /* Find the start of the node name.  */
  686.                 j += 5;
  687.                 while (line[j] == ' ' || line[j] == '\t')
  688.                   j++;
  689.                 /* Find the end of the node name.  */
  690.                 end = j;
  691.                 while (line[end] != 0 && line[end] != ',' && line[end] != '\n'
  692.                        && line[end] != '\t')
  693.                   end++;
  694.                 dir_nodes->name = copy_string (line + j, end - j);
  695.               }
  696.           node_header_flag = 0;
  697.         }
  698.  
  699.       /* Notice the start of a node.  */
  700.       if (*dir_lines[i].start == 037)
  701.         {
  702.           struct node *next
  703.             = (struct node *) xmalloc (sizeof (struct node));
  704.           next->next = dir_nodes;
  705.           next->name = NULL;
  706.           next->start_line = i;
  707.           next->end_line = 0;
  708.           next->menu_start = NULL;
  709.           next->sections = NULL;
  710.           next->last_section = NULL;
  711.  
  712.           if (dir_nodes != 0)
  713.             dir_nodes->end_line = i;
  714.           /* Fill in the end of the last menu section
  715.              of the previous node.  */
  716.           if (dir_nodes != 0 && dir_nodes->last_section != 0)
  717.             dir_nodes->last_section->end_line = i;
  718.  
  719.           dir_nodes = next;
  720.  
  721.           /* The following line is the header of this node;
  722.              parse it.  */
  723.           node_header_flag = 1;
  724.         }
  725.  
  726.       /* Notice the lines that start menus.  */
  727.       if (dir_nodes != 0
  728.           && !strncmp ("* Menu:", dir_lines[i].start, 7))
  729.         dir_nodes->menu_start = dir_lines[i + 1].start;
  730.  
  731.       /* Notice sections in menus.  */
  732.       if (dir_nodes != 0
  733.           && dir_nodes->menu_start != 0
  734.           && *dir_lines[i].start != '\n'
  735.           && *dir_lines[i].start != '*'
  736.           && *dir_lines[i].start != ' '
  737.           && *dir_lines[i].start != '\t')
  738.         {
  739.           /* Add this menu section to the node's list.
  740.              This list grows in forward order.  */
  741.           struct menu_section *next
  742.             = (struct menu_section *) xmalloc (sizeof (struct menu_section));
  743.           next->start_line = i + 1;
  744.           next->next = 0;
  745.           next->end_line = 0;
  746.           next->name = copy_string (dir_lines[i].start, dir_lines[i].size);
  747.           if (dir_nodes->sections)
  748.             {
  749.               dir_nodes->last_section->next = next;
  750.               dir_nodes->last_section->end_line = i;
  751.             }
  752.           else
  753.             dir_nodes->sections = next;
  754.           dir_nodes->last_section = next;
  755.         }
  756.  
  757.       /* Check for an existing entry that should be deleted.
  758.          Delete all entries which specify this file name.  */
  759.       if (*dir_lines[i].start == '*')
  760.         {
  761.           char *p = dir_lines[i].start;
  762.  
  763.           while (*p != 0 && *p != ':')
  764.             p++;
  765.           p++;
  766.           while (*p == ' ') p++;
  767.           if (*p == '(')
  768.             {
  769.               p++;
  770.               if ((dir_lines[i].size
  771.                    > (p - dir_lines[i].start + infilelen_sans_info))
  772.                   && !strncmp (p, infile_sans_info, infilelen_sans_info)
  773.                   && (p[infilelen_sans_info] == ')'
  774.                       || !strncmp (p + infilelen_sans_info, ".info)", 6)))
  775.                 {
  776.                   dir_lines[i].delete = 1;
  777.                   something_deleted = 1;
  778.                 }
  779.             }
  780.         }
  781.       /* Treat lines that start with whitespace
  782.          as continuations; if we are deleting an entry,
  783.          delete all its continuations as well.  */
  784.       else if (i > 0
  785.                && (*dir_lines[i].start == ' '
  786.                    || *dir_lines[i].start == '\t'))
  787.         {
  788.           dir_lines[i].delete = dir_lines[i - 1].delete;
  789.           something_deleted = 1;
  790.         }
  791.     }
  792.  
  793.   /* Finish the info about the end of the last node.  */
  794.   if (dir_nodes != 0)
  795.     {
  796.       dir_nodes->end_line = dir_nlines;
  797.       if (dir_nodes->last_section != 0)
  798.         dir_nodes->last_section->end_line = dir_nlines;
  799.     }
  800.  
  801.   /* Decide where to add the new entries (unless --delete was used).
  802.      Find the menu sections to add them in.
  803.      In each section, find the proper alphabetical place to add
  804.      each of the entries.  */
  805.  
  806.   if (!delete_flag)
  807.     {
  808.       struct node *node;
  809.       struct menu_section *section;
  810.       struct spec_section *spec;
  811.  
  812.       for (node = dir_nodes; node; node = node->next)
  813.         for (section = node->sections; section; section = section->next)
  814.           {
  815.             for (i = section->end_line; i > section->start_line; i--)
  816.               if (dir_lines[i - 1].size != 0)
  817.                 break;
  818.             section->end_line = i;
  819.  
  820.             for (spec = input_sections; spec; spec = spec->next)
  821.               if (!strcmp (spec->name, section->name))
  822.                 break;
  823.             if (spec)
  824.               {
  825.                 int add_at_line = section->end_line;
  826.                 struct spec_entry *entry;
  827.                 /* Say we have found at least one section with this name,
  828.                    so we need not add such a section.  */
  829.                 spec->missing = 0;
  830.                 /* For each entry, find the right place in this section
  831.                    to add it.  */
  832.                 for (entry = entries_to_add; entry; entry = entry->next)
  833.                   {
  834.                     int textlen = strlen (entry->text);
  835.                     /* Subtract one because dir_lines is zero-based,
  836.                        but the `end_line' and `start_line' members are
  837.                        one-based.  */
  838.                     for (i = section->end_line - 1;
  839.                          i >= section->start_line - 1; i--)
  840.                       {
  841.                         /* If an entry exists with the same name,
  842.                            and was not marked for deletion
  843.                            (which means it is for some other file),
  844.                            we are in trouble.  */
  845.                         if (dir_lines[i].start[0] == '*'
  846.                             && menu_line_equal (entry->text, textlen,
  847.                                                 dir_lines[i].start,
  848.                                                 dir_lines[i].size)
  849.                             && !dir_lines[i].delete)
  850.                           fatal (_("menu item `%s' already exists, for file `%s'"),
  851.                                  extract_menu_item_name (entry->text),
  852.                                  extract_menu_file_name (dir_lines[i].start));
  853.                         if (dir_lines[i].start[0] == '*'
  854.                             && menu_line_lessp (entry->text, textlen,
  855.                                                 dir_lines[i].start,
  856.                                                 dir_lines[i].size))
  857.                           add_at_line = i;
  858.                       }
  859.                     insert_entry_here (entry, add_at_line,
  860.                                        dir_lines, n_entries_to_add);
  861.                   }
  862.               }
  863.           }
  864.  
  865.       /* Mark the end of the Top node as the place to add any
  866.          new sections that are needed.  */
  867.       for (node = dir_nodes; node; node = node->next)
  868.         if (node->name && strcmp (node->name, "Top") == 0)
  869.           dir_lines[node->end_line].add_sections_before = 1;
  870.     }
  871.  
  872.   if (delete_flag && !something_deleted && !quiet_flag)
  873.     warning (_("no entries found for `%s'; nothing deleted"), infile);
  874.  
  875.   /* Output the old dir file, interpolating the new sections
  876.      and/or new entries where appropriate.  */
  877.  
  878.   output = fopen (dirfile, "w");
  879.   if (!output)
  880.     {
  881.       perror (dirfile);
  882.       exit (1);
  883.     }
  884.  
  885.   for (i = 0; i <= dir_nlines; i++)
  886.     {
  887.       int j;
  888.  
  889.       /* If we decided to output some new entries before this line,
  890.          output them now.  */
  891.       if (dir_lines[i].add_entries_before)
  892.         for (j = 0; j < n_entries_to_add; j++)
  893.           {
  894.             struct spec_entry *this = dir_lines[i].add_entries_before[j];
  895.             if (this == 0)
  896.               break;
  897.             fputs (this->text, output);
  898.           }
  899.       /* If we decided to add some sections here
  900.          because there are no such sections in the file,
  901.          output them now.  */
  902.       if (dir_lines[i].add_sections_before)
  903.         {
  904.           struct spec_section *spec;
  905.           struct spec_section **sections;
  906.           int n_sections = 0;
  907.  
  908.           /* Count the sections and allocate a vector for all of them.  */
  909.           for (spec = input_sections; spec; spec = spec->next)
  910.             n_sections++;
  911.           sections = ((struct spec_section **)
  912.                       xmalloc (n_sections * sizeof (struct spec_section *)));
  913.  
  914.           /* Fill the vector SECTIONS with pointers to all the sections,
  915.              and sort them.  */
  916.           j = 0;
  917.           for (spec = input_sections; spec; spec = spec->next)
  918.             sections[j++] = spec;
  919.           qsort (sections, n_sections, sizeof (struct spec_section *),
  920.                  compare_section_names);
  921.  
  922.           /* Generate the new sections in alphabetical order.
  923.              In each new section, output all of our entries.  */
  924.           for (j = 0; j < n_sections; j++)
  925.             {
  926.               spec = sections[j];
  927.               if (spec->missing)
  928.                 {
  929.                   struct spec_entry *entry;
  930.  
  931.                   putc ('\n', output);
  932.                   fputs (spec->name, output);
  933.                   putc ('\n', output);
  934.                   for (entry = entries_to_add; entry; entry = entry->next)
  935.                     fputs (entry->text, output);
  936.                 }
  937.             }
  938.  
  939.           free (sections);
  940.         }
  941.  
  942.       /* Output the original dir lines unless marked for deletion.  */
  943.       if (i < dir_nlines && !dir_lines[i].delete)
  944.         {
  945.           fwrite (dir_lines[i].start, 1, dir_lines[i].size, output);
  946.           putc ('\n', output);
  947.         }
  948.     }
  949.  
  950.   fclose (output);
  951.  
  952.   exit (0);
  953. }
  954.  
  955. /* Read all of file FILNAME into memory
  956.    and return the address of the data.
  957.    Store the size into SIZEP.
  958.    If there is trouble, do a fatal error.  */
  959.  
  960. char *
  961. readfile (filename, sizep)
  962.      char *filename;
  963.      int *sizep;
  964. {
  965.   int data_size = 1024;
  966.   char *data = (char *) xmalloc (data_size);
  967.   int filled = 0;
  968.   int nread = 0;
  969.  
  970.   int desc = open (filename, O_RDONLY);
  971.   if (desc < 0)
  972.     pfatal_with_name (filename);
  973.  
  974.   while (1)
  975.     {
  976.       nread = read (desc, data + filled, data_size - filled);
  977.       if (nread < 0)
  978.         pfatal_with_name (filename);
  979.       if (nread == 0)
  980.         break;
  981.  
  982.       filled += nread;
  983.       if (filled == data_size)
  984.         {
  985.           data_size *= 2;
  986.           data = (char *) xrealloc (data, data_size);
  987.         }
  988.     }
  989.  
  990.   *sizep = filled;
  991.   return data;
  992. }
  993.  
  994. /* Divide the text at DATA (of SIZE bytes) into lines.
  995.    Return a vector of struct line_data describing the lines.
  996.    Store the length of that vector into *NLINESP.  */
  997.  
  998. struct line_data *
  999. findlines (data, size, nlinesp)
  1000.      char *data;
  1001.      int size;
  1002.      int *nlinesp;
  1003. {
  1004.   struct line_data *lines;
  1005.   int lines_allocated = 512;
  1006.   int filled = 0;
  1007.   int i = 0;
  1008.   int lineflag;
  1009.  
  1010.   lines = (struct line_data *) xmalloc (lines_allocated * sizeof (struct line_data));
  1011.  
  1012.   lineflag = 1;
  1013.   for (i = 0; i < size; i++)
  1014.     {
  1015.       if (lineflag)
  1016.         {
  1017.           if (filled == lines_allocated)
  1018.             {
  1019.               lines_allocated *= 2;
  1020.               lines = (struct line_data *) xrealloc (lines, lines_allocated * sizeof (struct line_data));
  1021.             }
  1022.           lines[filled].start = &data[i];
  1023.           lines[filled].add_entries_before = 0;
  1024.           lines[filled].add_sections_before = 0;
  1025.           lines[filled].delete = 0;
  1026.           if (filled > 0)
  1027.             lines[filled - 1].size
  1028.               = lines[filled].start - lines[filled - 1].start - 1;
  1029.           filled++;
  1030.         }
  1031.       lineflag = (data[i] == '\n');
  1032.     }
  1033.   if (filled > 0)
  1034.     lines[filled - 1].size = &data[i] - lines[filled - 1].start - lineflag;
  1035.  
  1036.   /* Do not leave garbage in the last element.  */
  1037.   lines[filled].start = NULL;
  1038.   lines[filled].add_entries_before = NULL;
  1039.   lines[filled].add_sections_before = 0;
  1040.   lines[filled].delete = 0;
  1041.   lines[filled].size = 0;
  1042.  
  1043.   *nlinesp = filled;
  1044.   return lines;
  1045. }
  1046.  
  1047. /* Compare the menu item names in LINE1 (line length LEN1)
  1048.    and LINE2 (line length LEN2).  Return 1 if the item name
  1049.    in LINE1 is less, 0 otherwise.  */
  1050.  
  1051. int
  1052. menu_line_lessp (line1, len1, line2, len2)
  1053.      char *line1;
  1054.      int len1;
  1055.      char *line2;
  1056.      int len2;
  1057. {
  1058.   int minlen = (len1 < len2 ? len1 : len2);
  1059.   int i;
  1060.   
  1061.   for (i = 0; i < minlen; i++)
  1062.     {
  1063.       /* If one item name is a prefix of the other,
  1064.          the former one is less.  */
  1065.       if (line1[i] == ':' && line2[i] != ':')
  1066.         return 1;
  1067.       if (line2[i] == ':' && line1[i] != ':')
  1068.         return 0;
  1069.       /* If they both continue and differ, one is less.  */
  1070.       if (line1[i] < line2[i])
  1071.         return 1;
  1072.       if (line1[i] > line2[i])
  1073.         return 0;
  1074.     }
  1075.   /* With a properly formatted dir file,
  1076.      we can only get here if the item names are equal.  */
  1077.   return 0;
  1078. }
  1079.  
  1080. /* Compare the menu item names in LINE1 (line length LEN1)
  1081.    and LINE2 (line length LEN2).  Return 1 if the item names are equal,
  1082.    0 otherwise.  */
  1083.  
  1084. int
  1085. menu_line_equal (line1, len1, line2, len2)
  1086.      char *line1;
  1087.      int len1;
  1088.      char *line2;
  1089.      int len2;
  1090. {
  1091.   int minlen = (len1 < len2 ? len1 : len2);
  1092.   int i;
  1093.   
  1094.   for (i = 0; i < minlen; i++)
  1095.     {
  1096.       /* If both item names end here, they are equal.  */
  1097.       if (line1[i] == ':' && line2[i] == ':')
  1098.         return 1;
  1099.       /* If they both continue and differ, one is less.  */
  1100.       if (line1[i] != line2[i])
  1101.         return 0;
  1102.     }
  1103.   /* With a properly formatted dir file,
  1104.      we can only get here if the item names are equal.  */
  1105.   return 1;
  1106. }
  1107.  
  1108. /* This is the comparison function for qsort
  1109.    for a vector of pointers to struct spec_section.
  1110.    Compare the section names.  */
  1111.  
  1112. int
  1113. compare_section_names (sec1, sec2)
  1114.      struct spec_section **sec1, **sec2;
  1115. {
  1116.   char *name1 = (*sec1)->name;
  1117.   char *name2 = (*sec2)->name;
  1118.   return strcmp (name1, name2);
  1119. }
  1120.  
  1121. /* Insert ENTRY into the add_entries_before vector
  1122.    for line number LINE_NUMBER of the dir file.
  1123.    DIR_LINES and N_ENTRIES carry information from like-named variables
  1124.    in main.  */
  1125.  
  1126. void
  1127. insert_entry_here (entry, line_number, dir_lines, n_entries)
  1128.      struct spec_entry *entry;
  1129.      int line_number;
  1130.      struct line_data *dir_lines;
  1131.      int n_entries;
  1132. {
  1133.   int i;
  1134.  
  1135.   if (dir_lines[line_number].add_entries_before == 0)
  1136.     {
  1137.       dir_lines[line_number].add_entries_before
  1138.         = (struct spec_entry **) xmalloc (n_entries * sizeof (struct spec_entry *));
  1139.       for (i = 0; i < n_entries; i++)
  1140.         dir_lines[line_number].add_entries_before[i] = 0;
  1141.     }
  1142.  
  1143.   for (i = 0; i < n_entries; i++)
  1144.     if (dir_lines[line_number].add_entries_before[i] == 0)
  1145.       break;
  1146.  
  1147.   if (i == n_entries)
  1148.     abort ();
  1149.  
  1150.   dir_lines[line_number].add_entries_before[i] = entry;
  1151. }
  1152.