home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / info / man.c < prev    next >
C/C++ Source or Header  |  1997-12-27  |  17KB  |  682 lines

  1. /*  man.c: How to read and format man files.
  2.     $Id: man.c,v 1.6 1997/07/31 23:49:59 karl Exp $
  3.  
  4.    Copyright (C) 1995, 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 Thu May  4 09:17:52 1995 (bfox@ai.mit.edu). */
  21.  
  22. #include "info.h"
  23. #include <sys/ioctl.h>
  24. #include "signals.h"
  25. #if defined (HAVE_SYS_TIME_H)
  26. #include <sys/time.h>
  27. #endif
  28. #if defined (HAVE_SYS_WAIT_H)
  29. #include <sys/wait.h>
  30. #endif
  31. #ifdef __EMX__
  32. #include <process.h>
  33. #include <sys/wait.h>
  34. #endif
  35.  
  36. #include "tilde.h"
  37. #include "man.h"
  38.  
  39. #if !defined (_POSIX_VERSION)
  40. #define pid_t int
  41. #endif
  42.  
  43. #if defined (FD_SET)
  44. #  if defined (hpux)
  45. #    define fd_set_cast(x) (int *)(x)
  46. #  else
  47. #    define fd_set_cast(x) (fd_set *)(x)
  48. #  endif /* !hpux */
  49. #endif /* FD_SET */
  50.  
  51. static char *read_from_fd ();
  52. static void clean_manpage ();
  53. static NODE *manpage_node_of_file_buffer ();
  54. static char *get_manpage_contents ();
  55.  
  56. NODE *
  57. make_manpage_node (pagename)
  58.      char *pagename;
  59. {
  60.   return (info_get_node (MANPAGE_FILE_BUFFER_NAME, pagename));
  61. }
  62.  
  63. NODE *
  64. get_manpage_node (file_buffer, pagename)
  65.      FILE_BUFFER *file_buffer;
  66.      char *pagename;
  67. {
  68.   NODE *node;
  69.  
  70.   node = manpage_node_of_file_buffer (file_buffer, pagename);
  71.  
  72.   if (!node)
  73.     {
  74.       char *page;
  75.  
  76.       page = get_manpage_contents (pagename);
  77.  
  78.       if (page)
  79.         {
  80.           char header[1024];
  81.           long oldsize, newsize;
  82.           int hlen, plen;
  83.  
  84.           sprintf (header, "\n\n%c\n%s %s,  %s %s,  %s (dir)\n\n",
  85.                    INFO_COOKIE,
  86.                    INFO_FILE_LABEL, file_buffer->filename,
  87.                    INFO_NODE_LABEL, pagename,
  88.                    INFO_UP_LABEL);
  89.           oldsize = file_buffer->filesize;
  90.           hlen = strlen (header);
  91.           plen = strlen (page);
  92.           newsize = (oldsize + hlen + plen);
  93.           file_buffer->contents =
  94.             (char *)xrealloc (file_buffer->contents, 1 + newsize);
  95.           memcpy (file_buffer->contents + oldsize, header, hlen);
  96.           oldsize += hlen;
  97.           memcpy (file_buffer->contents + oldsize, page, plen);
  98.           file_buffer->contents[newsize] = '\0';
  99.           file_buffer->filesize = newsize;
  100.           file_buffer->finfo.st_size = newsize;
  101.           build_tags_and_nodes (file_buffer);
  102.           free (page);
  103.         }
  104.  
  105.       node = manpage_node_of_file_buffer (file_buffer, pagename);
  106.     }
  107.  
  108.   return (node);
  109. }
  110.  
  111. FILE_BUFFER *
  112. create_manpage_file_buffer ()
  113. {
  114.   FILE_BUFFER *file_buffer = make_file_buffer ();
  115.   file_buffer->filename = xstrdup (MANPAGE_FILE_BUFFER_NAME);
  116.   file_buffer->fullpath = xstrdup (MANPAGE_FILE_BUFFER_NAME);
  117.   file_buffer->finfo.st_size = 0;
  118.   file_buffer->filesize = 0;
  119.   file_buffer->contents = (char *)NULL;
  120.   file_buffer->flags = (N_IsInternal | N_CannotGC | N_IsManPage);
  121.   
  122.   return (file_buffer);
  123. }
  124.  
  125. /* Scan the list of directories in PATH looking for FILENAME.  If we find
  126.    one that is an executable file, return it as a new string.  Otherwise,
  127.    return a NULL pointer. */
  128. static char *
  129. executable_file_in_path (filename, path)
  130.      char *filename, *path;
  131. {
  132.   struct stat finfo;
  133.   char *temp_dirname;
  134.   int statable, dirname_index;
  135.  
  136.   dirname_index = 0;
  137.  
  138.   while ((temp_dirname = extract_colon_unit (path, &dirname_index)))
  139.     {
  140.       char *temp;
  141.  
  142.       /* Expand a leading tilde if one is present. */
  143.       if (*temp_dirname == '~')
  144.         {
  145.           char *expanded_dirname;
  146.  
  147.           expanded_dirname = tilde_expand_word (temp_dirname);
  148.           free (temp_dirname);
  149.           temp_dirname = expanded_dirname;
  150.         }
  151.  
  152.       temp = (char *)xmalloc (30 + strlen (temp_dirname) + strlen (filename));
  153.       strcpy (temp, temp_dirname);
  154.       if (temp[(strlen (temp)) - 1] != '/')
  155.         strcat (temp, "/");
  156.       strcat (temp, filename);
  157.  
  158.       free (temp_dirname);
  159.  
  160. #ifdef __EMX__
  161.       strcat (temp, ".cmd");
  162.       statable = (stat (temp, &finfo) == 0);
  163.       if (!statable)
  164.       {
  165.     strcpy(temp + strlen (temp) - 4, ".exe");
  166.       statable = (stat (temp, &finfo) == 0);
  167.       }
  168. #else
  169.       statable = (stat (temp, &finfo) == 0);
  170. #endif
  171.  
  172.       /* If we have found a regular executable file, then use it. */
  173.       if ((statable) && (S_ISREG (finfo.st_mode)) &&
  174.           (access (temp, X_OK) == 0))
  175.         return (temp);
  176.       else
  177.         free (temp);
  178.     }
  179.   return ((char *)NULL);
  180. }
  181.  
  182. /* Return the full pathname of the system man page formatter. */
  183. static char *
  184. find_man_formatter ()
  185. {
  186.   return (executable_file_in_path ("man", (char *)getenv ("PATH")));
  187. }
  188.  
  189. static char *manpage_pagename = (char *)NULL;
  190. static char *manpage_section  = (char *)NULL;
  191.  
  192. static void
  193. get_page_and_section (pagename)
  194.      char *pagename;
  195. {
  196.   register int i;
  197.  
  198.   if (manpage_pagename)
  199.     free (manpage_pagename);
  200.  
  201.   if (manpage_section)
  202.     free (manpage_section);
  203.  
  204.   manpage_pagename = (char *)NULL;
  205.   manpage_section  = (char *)NULL;
  206.  
  207.   for (i = 0; pagename[i] != '\0' && pagename[i] != '('; i++);
  208.  
  209.   manpage_pagename = (char *)xmalloc (1 + i);
  210.   strncpy (manpage_pagename, pagename, i);
  211.   manpage_pagename[i] = '\0';
  212.  
  213.   if (pagename[i] == '(')
  214.     {
  215.       int start;
  216.  
  217.       start = i + 1;
  218.  
  219.       for (i = start; pagename[i] != '\0' && pagename[i] != ')'; i++);
  220.  
  221.       manpage_section = (char *)xmalloc (1 + (i - start));
  222.       strncpy (manpage_section, pagename + start, (i - start));
  223.       manpage_section[i - start] = '\0';
  224.     }
  225. }
  226.  
  227. static void
  228. reap_children (sig)
  229.      int sig;
  230. {
  231.   int status;
  232.   wait (&status);
  233. }
  234.  
  235. static char *
  236. get_manpage_contents (pagename)
  237.      char *pagename;
  238. {
  239.   static char *formatter_args[4] = { (char *)NULL };
  240.   int pipes[2];
  241.   pid_t child;
  242.   char *formatted_page = (char *)NULL;
  243.   int arg_index = 1;
  244.  
  245.   if (formatter_args[0] == (char *)NULL)
  246.     formatter_args[0] = find_man_formatter ();
  247.  
  248.   if (formatter_args[0] == (char *)NULL)
  249.     return ((char *)NULL);
  250.  
  251.   get_page_and_section (pagename);
  252.  
  253.   if (manpage_section != (char *)NULL)
  254.     formatter_args[arg_index++] = manpage_section;
  255.  
  256.   formatter_args[arg_index++] = manpage_pagename;
  257.   formatter_args[arg_index] = (char *)NULL;
  258.  
  259.   /* Open a pipe to this program, read the output, and save it away
  260.      in FORMATTED_PAGE.  The reader end of the pipe is pipes[0]; the
  261.      writer end is pipes[1]. */
  262.   pipe (pipes);
  263.  
  264. #ifdef __EMX__
  265.   {
  266.     int pid, old_stdout, old_stderr;
  267.     
  268.     old_stdout = dup (fileno (stdout));
  269.     fcntl (old_stdout, F_SETFD, FD_CLOEXEC);
  270.     dup2 (pipes[1], fileno (stdout));
  271.  
  272.     old_stderr = dup (fileno (stderr));
  273.     fcntl (old_stderr, F_SETFD, FD_CLOEXEC);
  274.     close (fileno (stderr));
  275.     
  276.     close (pipes[1]);
  277.     fcntl (pipes[0], F_SETFD, FD_CLOEXEC);
  278.  
  279.     pid = spawnv (P_NOWAIT, formatter_args[0], formatter_args);
  280.  
  281.     close (fileno (stdout));
  282.     dup2 (old_stdout, fileno (stdout));
  283.     close (old_stdout);
  284.  
  285.     dup2 (old_stderr, fileno (stderr));
  286.     close (old_stderr);
  287.  
  288.     if (pid != -1)
  289.       formatted_page = read_from_fd (pipes[0]);
  290.  
  291.     close (pipes[0]);
  292.  
  293.     waitpid (pid, NULL, WNOHANG);
  294.   }
  295. #else
  296.   signal (SIGCHLD, reap_children);
  297.  
  298.   child = fork ();
  299.  
  300.   if (child == -1)
  301.     return ((char *)NULL);
  302.  
  303.   if (child != 0)
  304.     {
  305.       /* In the parent, close the writing end of the pipe, and read from
  306.          the exec'd child. */
  307.       close (pipes[1]);
  308.       formatted_page = read_from_fd (pipes[0]);
  309.       close (pipes[0]);
  310.     }
  311.   else
  312.     {
  313.       /* In the child, close the read end of the pipe, make the write end
  314.          of the pipe be stdout, and execute the man page formatter. */
  315.       close (pipes[0]);
  316.       close (fileno (stderr));
  317.       close (fileno (stdin));   /* Don't print errors. */
  318.       dup2 (pipes[1], fileno (stdout));
  319.  
  320.       execv (formatter_args[0], formatter_args);
  321.  
  322.       /* If we get here, we couldn't exec, so close out the pipe and
  323.          exit. */
  324.       close (pipes[1]);
  325.       exit (0);
  326.     }
  327. #endif
  328.  
  329.   /* If we have the page, then clean it up. */
  330.   if (formatted_page)
  331.     clean_manpage (formatted_page);
  332.  
  333.   return (formatted_page);
  334. }
  335.  
  336. static void
  337. clean_manpage (manpage)
  338.      char *manpage;
  339. {
  340.   register int i, j;
  341.   int newline_count = 0;
  342.   char *newpage;
  343.  
  344.   newpage = (char *)xmalloc (1 + strlen (manpage));
  345.  
  346.   for (i = 0, j = 0; (newpage[j] = manpage[i]); i++, j++)
  347.     {
  348.       if (manpage[i] == '\n')
  349.         newline_count++;
  350.       else
  351.         newline_count = 0;
  352.  
  353.       if (newline_count == 3)
  354.         {
  355.           j--;
  356.           newline_count--;
  357.         }
  358.  
  359.       if (manpage[i] == '\b' || manpage[i] == '\f')
  360.         j -= 2;
  361.     }
  362.  
  363.   newpage[j++] = '\0';
  364.  
  365.   strcpy (manpage, newpage);
  366.   free (newpage);
  367. }
  368.  
  369. static NODE *
  370. manpage_node_of_file_buffer (file_buffer, pagename)
  371.      FILE_BUFFER *file_buffer;
  372.      char *pagename;
  373. {
  374.   NODE *node = (NODE *)NULL;
  375.   TAG *tag = (TAG *)NULL;
  376.  
  377.   if (file_buffer->contents)
  378.     {
  379.       register int i;
  380.  
  381.       for (i = 0; (tag = file_buffer->tags[i]); i++)
  382.         {
  383.           if (strcasecmp (pagename, tag->nodename) == 0)
  384.             break;
  385.         }
  386.     }
  387.  
  388.   if (tag)
  389.     {
  390.       node = (NODE *)xmalloc (sizeof (NODE));
  391.       node->filename = file_buffer->filename;
  392.       node->nodename = tag->nodename;
  393.       node->contents = file_buffer->contents + tag->nodestart;
  394.       node->nodelen = tag->nodelen;
  395.       node->flags    = 0;
  396.       node->parent   = (char *)NULL;
  397.       node->flags = (N_HasTagsTable | N_IsManPage);
  398.       node->contents += skip_node_separator (node->contents);
  399.     }
  400.  
  401.   return (node);
  402. }
  403.  
  404. static char *
  405. read_from_fd (fd)
  406.      int fd;
  407. {
  408.   struct timeval timeout;
  409.   char *buffer = (char *)NULL;
  410.   int bsize = 0;
  411.   int bindex = 0;
  412.   int select_result;
  413. #if defined (FD_SET) && !defined(__EMX__)
  414.   fd_set read_fds;
  415.  
  416.   timeout.tv_sec = 15;
  417.   timeout.tv_usec = 0;
  418.  
  419.   FD_ZERO (&read_fds);
  420.   FD_SET (fd, &read_fds);
  421.  
  422.   select_result = select (fd + 1, fd_set_cast (&read_fds), 0, 0, &timeout);
  423. #else /* !FD_SET */
  424.   select_result = 1;
  425. #endif /* !FD_SET */
  426.  
  427.   switch (select_result)
  428.     {
  429.     case 0:
  430.     case -1:
  431.       break;
  432.  
  433.     default:
  434.       {
  435.         int amount_read;
  436.         int done = 0;
  437.  
  438.         while (!done)
  439.           {
  440.             while ((bindex + 1024) > (bsize))
  441.               buffer = (char *)xrealloc (buffer, (bsize += 1024));
  442.             buffer[bindex] = '\0';
  443.  
  444.             amount_read = read (fd, buffer + bindex, 1023);
  445.  
  446.             if (amount_read < 0)
  447.               {
  448.                 done = 1;
  449.               }
  450.             else
  451.               {
  452.                 bindex += amount_read;
  453.                 buffer[bindex] = '\0';
  454.                 if (amount_read == 0)
  455.                   done = 1;
  456.               }
  457.           }
  458.       }
  459.     }
  460.  
  461.   if ((buffer != (char *)NULL) && (*buffer == '\0'))
  462.     {
  463.       free (buffer);
  464.       buffer = (char *)NULL;
  465.     }
  466.  
  467.   return (buffer);
  468. }
  469.  
  470. static char *reference_section_starters[] =
  471. {
  472.   "\nRELATED INFORMATION",
  473.   "\nRELATED\tINFORMATION",
  474.   "RELATED INFORMATION\n",
  475.   "RELATED\tINFORMATION\n",
  476.   "\nSEE ALSO",
  477.   "\nSEE\tALSO",
  478.   "SEE ALSO\n",
  479.   "SEE\tALSO\n",
  480.   (char *)NULL
  481. };
  482.  
  483. static SEARCH_BINDING frs_binding;
  484.  
  485. static SEARCH_BINDING *
  486. find_reference_section (node)
  487.      NODE *node;
  488. {
  489.   register int i;
  490.   long position = -1;
  491.  
  492.   frs_binding.buffer = node->contents;
  493.   frs_binding.start = 0;
  494.   frs_binding.end = node->nodelen;
  495.   frs_binding.flags = S_SkipDest;
  496.  
  497.   for (i = 0; reference_section_starters[i] != (char *)NULL; i++)
  498.     {
  499.       position = search_forward (reference_section_starters[i], &frs_binding);
  500.       if (position != -1)
  501.         break;
  502.     }
  503.  
  504.   if (position == -1)
  505.     return ((SEARCH_BINDING *)NULL);
  506.  
  507.   /* We found the start of the reference section, and point is right after
  508.      the string which starts it.  The text from here to the next header
  509.      (or end of buffer) contains the only references in this manpage. */
  510.   frs_binding.start = position;
  511.  
  512.   for (i = frs_binding.start; i < frs_binding.end - 2; i++)
  513.     {
  514.       if ((frs_binding.buffer[i] == '\n') &&
  515.           (!whitespace (frs_binding.buffer[i + 1])))
  516.         {
  517.           frs_binding.end = i;
  518.           break;
  519.         }
  520.     }
  521.  
  522.   return (&frs_binding);
  523. }
  524.  
  525. REFERENCE **
  526. xrefs_of_manpage (node)
  527.      NODE *node;
  528. {
  529.   SEARCH_BINDING *reference_section;
  530.   REFERENCE **refs = (REFERENCE **)NULL;
  531.   int refs_index = 0;
  532.   int refs_slots = 0;
  533.   long position;
  534.  
  535.   reference_section = find_reference_section (node);
  536.  
  537.   if (reference_section == (SEARCH_BINDING *)NULL)
  538.     return ((REFERENCE **)NULL);
  539.  
  540.   /* Grovel the reference section building a list of references found there.
  541.      A reference is alphabetic characters followed by non-whitespace text
  542.      within parenthesis. */
  543.   reference_section->flags = 0;
  544.  
  545.   while ((position = search_forward ("(", reference_section)) != -1)
  546.     {
  547.       register int start, end;
  548.  
  549.       for (start = position; start > reference_section->start; start--)
  550.         if (whitespace (reference_section->buffer[start]))
  551.           break;
  552.  
  553.       start++;
  554.  
  555.       for (end = position; end < reference_section->end; end++)
  556.         {
  557.           if (whitespace (reference_section->buffer[end]))
  558.             {
  559.               end = start;
  560.               break;
  561.             }
  562.  
  563.           if (reference_section->buffer[end] == ')')
  564.             {
  565.               end++;
  566.               break;
  567.             }
  568.         }
  569.  
  570.       if (end != start)
  571.         {
  572.           REFERENCE *entry;
  573.           int len = end - start;
  574.  
  575.           entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
  576.           entry->label = (char *)xmalloc (1 + len);
  577.           strncpy (entry->label, (reference_section->buffer) + start, len);
  578.           entry->label[len] = '\0';
  579.           entry->filename = xstrdup (node->filename);
  580.           entry->nodename = xstrdup (entry->label);
  581.           entry->start = start;
  582.           entry->end = end;
  583.  
  584.           add_pointer_to_array
  585.             (entry, refs_index, refs, refs_slots, 10, REFERENCE *);
  586.         }
  587.  
  588.       reference_section->start = position + 1;
  589.     }
  590.  
  591.   return (refs);
  592. }
  593.  
  594. long
  595. locate_manpage_xref (node, start, dir)
  596.      NODE *node;
  597.      long start;
  598.      int dir;
  599. {
  600.   REFERENCE **refs;
  601.   long position = -1;
  602.  
  603.   refs = xrefs_of_manpage (node);
  604.  
  605.   if (refs)
  606.     {
  607.       register int i, count;
  608.       REFERENCE *entry;
  609.  
  610.       for (i = 0; refs[i]; i++);
  611.       count = i;
  612.  
  613.       if (dir > 0)
  614.         {
  615.           for (i = 0; (entry = refs[i]); i++)
  616.             if (entry->start > start)
  617.               {
  618.                 position = entry->start;
  619.                 break;
  620.               }
  621.         }
  622.       else
  623.         {
  624.           for (i = count - 1; i > -1; i--)
  625.             {
  626.               entry = refs[i];
  627.  
  628.               if (entry->start < start)
  629.                 {
  630.                   position = entry->start;
  631.                   break;
  632.                 }
  633.             }
  634.         }
  635.  
  636.       info_free_references (refs);
  637.     }
  638.   return (position);
  639. }
  640.  
  641. /* This one was a little tricky.  The binding buffer that is passed in has
  642.    a START and END value of 0 -- strlen (window-line-containing-point).
  643.    The BUFFER is a pointer to the start of that line. */
  644. REFERENCE **
  645. manpage_xrefs_in_binding (node, binding)
  646.      NODE *node;
  647.      SEARCH_BINDING *binding;
  648. {
  649.   register int i;
  650.   REFERENCE **all_refs = xrefs_of_manpage (node);
  651.   REFERENCE **brefs = (REFERENCE **)NULL;
  652.   REFERENCE *entry;
  653.   int brefs_index = 0;
  654.   int brefs_slots = 0;
  655.   int start, end;
  656.  
  657.   if (!all_refs)
  658.     return ((REFERENCE **)NULL);
  659.  
  660.   start = binding->start + (binding->buffer - node->contents);
  661.   end = binding->end + (binding->buffer - node->contents);
  662.  
  663.   for (i = 0; (entry = all_refs[i]); i++)
  664.     {
  665.       if ((entry->start > start) && (entry->end < end))
  666.         {
  667.           add_pointer_to_array
  668.             (entry, brefs_index, brefs, brefs_slots, 10, REFERENCE *);
  669.         }
  670.       else
  671.         {
  672.           maybe_free (entry->label);
  673.           maybe_free (entry->filename);
  674.           maybe_free (entry->nodename);
  675.           free (entry);
  676.         }
  677.     }
  678.  
  679.   free (all_refs);
  680.   return (brefs);
  681. }
  682.