home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / emxtutor.zip / emxsrcd1.zip / emx / src / emxdoc / xref.c < prev    next >
C/C++ Source or Header  |  1995-10-19  |  6KB  |  252 lines

  1. /* xref.c -- Manage cross references
  2.    Copyright (c) 1993-1995 Eberhard Mattes
  3.  
  4. This file is part of emxdoc.
  5.  
  6. emxdoc is free software; you can redistribute it and/or modify it
  7. 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. emxdoc 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 emxdoc; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include "emxdoc.h"
  27. #include "xref.h"
  28.  
  29. static int first_keyword_flag = TRUE;
  30. static char inf_name[_MAX_FNAME] = "";
  31.  
  32.  
  33. void write_keyword (const uchar *p)
  34. {
  35.   int len;
  36.  
  37.   if (*p != 0 && strpbrk (p, " ,") == NULL)
  38.     {
  39.       if (first_keyword_flag)
  40.         {
  41.           if (title != NULL)
  42.             fprintf (output_file, "DESCRIPTION: %s\n", title);
  43.           else
  44.             fprintf (output_file, "DESCRIPTION: %s.inf\n", inf_name);
  45.           first_keyword_flag = FALSE;
  46.         }
  47.       len = strlen (p);
  48.       if (len > 2 && p[len-2] == '(' && p[len-1] == ')')
  49.         len -= 2;
  50.       fprintf (output_file, "(%.*s, view %s ~)\n", len, p, inf_name);
  51.     }
  52. }
  53.  
  54.  
  55. static void gather_define (struct word *wp)
  56. {
  57.   fprintf (output_file, "d %d %s\n", wp->ref, wp->str);
  58. }
  59.  
  60.  
  61. static void gather_reference (const uchar *p)
  62. {
  63.   struct word *wp;
  64.  
  65.   fprintf (output_file, "r %s\n", p);
  66.   wp = word_add (p);
  67.   wp->ref = -1;
  68. }
  69.  
  70.  
  71. struct word *define_label (const uchar *p, int ref, const char *msg)
  72. {
  73.   struct word *wp;
  74.  
  75.   wp = word_add (p);
  76.   if (wp->ref != 0)
  77.     {
  78.       if (wp->database == NULL || wp->ref != ref)
  79.         fatal ("%s:%d: %s %s multiply defined", input_fname, line_no, msg, p);
  80.     }
  81.   else
  82.     {
  83.       wp->ref = ref;
  84.       if (opt_g)
  85.         gather_define (wp);
  86.     }
  87.   return wp;
  88. }
  89.  
  90.  
  91. struct word *use_reference (const uchar *p)
  92. {
  93.   struct word *wp;
  94.  
  95.   wp = word_find (p, word_hash (p));
  96.   if ((wp == NULL || wp->ref == 0) && mode != 't' && mode != 'l')
  97.     {
  98.       if (!opt_g)
  99.         fatal ("%s:%d: Undefined label: %s", input_fname, line_no, p);
  100.       gather_reference (p);
  101.       return NULL;
  102.     }
  103.   else
  104.     return wp;
  105. }
  106.  
  107.  
  108. /* Misuse the style field of struct word */
  109.  
  110. #define STYLE_UNUSED STYLE_NORMAL
  111. #define STYLE_USED   STYLE_BOLD
  112.  
  113. void make_global (const uchar *name)
  114. {
  115.   struct word *wdb, *wp;
  116.   const uchar *p;
  117.   char *tmp;
  118.   long n;
  119.   char database[_MAX_FNAME];
  120.  
  121.   open_input (name);
  122.   init_file ();
  123.   wdb = NULL;
  124.   if (!out)
  125.     {
  126.       _splitpath (input_fname, NULL, NULL, database, NULL);
  127.       wdb = word_add (database);
  128.     }
  129.   read_line ();
  130.   while (!end_of_file)
  131.     {
  132.       p = input + 1;
  133.       while (isspace (*p))
  134.         ++p;
  135.       switch (input[0])
  136.         {
  137.         case 'd':
  138.           if (!out)
  139.             {
  140.               errno = 0;
  141.               n = strtol (p, &tmp, 10);
  142.               if (errno != 0 || *tmp != ' ')
  143.                 fatal ("%s:%d: Invalid reference number",
  144.                        input_fname, line_no);
  145.               p = tmp;
  146.               while (isspace (*p))
  147.                 ++p;
  148.               wp = word_add (p);
  149.               if (wp->ref != 0)
  150.                 fatal ("%s:%d: Label %s already defined",
  151.                        input_fname, line_no, p);
  152.               wp->ref = (int)n;
  153.               wp->style = STYLE_UNUSED;
  154.               wp->database = wdb;
  155.             }
  156.           break;
  157.         case 'r':
  158.           if (out)
  159.             {
  160.               wp = word_find (p, word_hash (p));
  161.               if (wp == NULL || wp->ref == 0)
  162.                 fatal ("%s:%d: Label %s not defined",
  163.                        input_fname, line_no, p);
  164.               if (wp->style == STYLE_UNUSED)
  165.                 {
  166.                   fprintf (output_file, "x %d %s %s\n",
  167.                            wp->ref, wp->database->str, p);
  168.                   wp->style = STYLE_USED;
  169.                 }
  170.             }
  171.           break;
  172.         }
  173.       read_line ();
  174.     }
  175.   fclose (input_file);
  176. }
  177.  
  178.  
  179. void keywords_keyword (const uchar *s)
  180. {
  181.   uchar word[512], *d;
  182.  
  183.   while (*s != 0)
  184.     {
  185.       d = word;
  186.       while (*s != 0 && !isspace (*s))
  187.         *d++ = *s++;
  188.       *d = 0;
  189.       write_keyword (word);
  190.       while (isspace (*s))
  191.         ++s;
  192.     }
  193. }
  194.  
  195.  
  196. void keywords_start (const char *fname)
  197. {
  198.   _splitpath (fname, NULL, NULL, inf_name, NULL);
  199.   fprintf (output_file, "EXTENSIONS: *\n");
  200. }
  201.  
  202.  
  203. void read_xref (const char *fname, struct toc *th)
  204. {
  205.   struct word *wp;
  206.   uchar database[512], *d;
  207.   const uchar *p;
  208.   char *tmp;
  209.   long n;
  210.   struct toc *tp;
  211.  
  212.   open_input (fname);
  213.   read_line ();
  214.   while (!end_of_file)
  215.     {
  216.       if (input[0] != 'x')
  217.         fatal ("%s:%d: Syntax error", fname, line_no);
  218.       p = input + 1;
  219.       while (isspace (*p))
  220.         ++p;
  221.       errno = 0;
  222.       n = strtol (p, &tmp, 10);
  223.       if (errno != 0 || *tmp != ' ')
  224.         fatal ("%s:%d: Syntax error", fname, line_no);
  225.       p = tmp;
  226.       while (isspace (*p))
  227.         ++p;
  228.       d = database;
  229.       while (*p != 0 && !isspace (*p))
  230.         *d++ = *p++;
  231.       *d = 0;
  232.       while (isspace (*p))
  233.         ++p;
  234.       if (d == database || *p == 0)
  235.         fatal ("%s:%d: Syntax error", fname, line_no);
  236.       if (out)
  237.         {
  238.           for (tp = th; tp != NULL; tp = tp->next)
  239.             if (tp->ref == (int)n)
  240.               tp->global = TRUE;
  241.         }
  242.       else
  243.         {
  244.           wp = word_add (p);
  245.           wp->database = word_add (database);
  246.           wp->ref = (int)n;
  247.         }
  248.       read_line ();
  249.     }
  250.   fclose (input_file);
  251. }
  252.