home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / doc / extensions / PEX / SI / xref.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-15  |  8.8 KB  |  345 lines

  1. /* $XConsortium: xref.c,v 5.1 91/02/16 09:45:52 rws Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright (c) 1990, 1991 by Sun Microsystems, Inc. and the X Consortium.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Sun Microsystems,
  13. the X Consortium, and MIT not be used in advertising or publicity 
  14. pertaining to distribution of the software without specific, written 
  15. prior permission.  
  16.  
  17. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  19. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  20. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include "xref.h"
  31.  
  32. struct codeword_entry    *root;
  33. struct counters {
  34.     int    levels[5];    /*  Chapter Level Counter  */
  35.     int    table_number;    /*  Table Number  */
  36.     int    figure_number;    /*  Figure Number  */
  37. }    counters = {{0, 0, 0, 0, 0}, 0, 0};
  38.  
  39. int    outline_file_read = FALSE;    /*  TRUE if outline file(s) supplied  */
  40. int    appendix_seen = FALSE;    /*  TRUE when appendix counting  */
  41. int    start_chapter = 1;    /*  Starting Chapter Number  */
  42. int    start_appendix = 1;    /*  Starting Appendix Number  */
  43. int    plain_preprocessor = FALSE;    /* plain = don't source files */
  44.  
  45. main(argc, argv)
  46. int    argc;
  47. char    *argv[];
  48. {
  49.     int    file_number;
  50.     int    ap_ix;
  51.  
  52.  
  53.     command_name = argv[0];
  54.     argc--;
  55.     argv++;
  56.     root = NULL;
  57.  
  58.     if (argc < 1) {
  59.         fprintf(stderr, "Usage: %s [-sbegin] [-o outline] filename ...\n", command_name);
  60.         exit(1);
  61.     }
  62.     while (argc > 0 && argv[0][0] == '-') {
  63.                     /*  look for starting chapter or appendix  */
  64.         if (argv[0][1] == 's') {
  65.             if (isdigit(argv[0][2]))
  66.                 start_chapter = atoi(&argv[0][2]);
  67.             else if (isalpha(argv[0][2])) {
  68.                 start_appendix = 0;
  69.                 for (ap_ix = 2; isalpha(argv[0][ap_ix]); ap_ix++) {
  70.                     if (isupper(argv[0][ap_ix]))
  71.                         start_appendix = 26*start_appendix
  72.                         + argv[0][ap_ix] - 'A' + 1;
  73.                     else
  74.                         start_appendix = 26*start_appendix
  75.                         + toupper(argv[0][ap_ix]) - 'A' + 1;
  76.                     /*  outline file  */
  77.                 }
  78.             }
  79.         } else if (argv[0][1] == 'o') {
  80.             current_filename = argv[1];
  81.             process_current_file(current_filename, GATHER_REFERENCES);
  82.             outline_file_read = TRUE;
  83.             argc--;
  84.             argv++;
  85.         } else if (argv[0][1] == 'p') {
  86.             plain_preprocessor = TRUE;
  87.         } else {
  88.             fprintf(stderr, "%s: unknown option -%c\n",
  89.                 command_name, argv[0][1]);
  90.             exit(1);
  91.         }
  92.         argc--;
  93.         argv++;
  94.     }
  95.  
  96.     if (argc < 1) {
  97.         fprintf(stderr, "Usage: %s [-sbegin] [-o outline] filename ...\n", command_name);
  98.         exit(1);
  99.     }
  100.  
  101.     if (!outline_file_read) {
  102.         for (file_number = 0;  file_number < argc;  file_number++) {
  103.             current_filename = argv[file_number];
  104.             process_current_file(current_filename, GATHER_REFERENCES);
  105.         }
  106.     }
  107.     for (file_number = 0;  file_number < argc;  file_number++) {
  108.         current_filename = argv[file_number];
  109.         process_current_file(current_filename, SUBSTITUTE_REFERENCES);
  110.     }
  111. #ifdef DEBUG
  112.     dump_codewords(root);
  113. #endif
  114.  
  115. }
  116.  
  117. process_current_file(filename, phase)
  118.     char    *filename;
  119.     int    phase;
  120. {
  121.     FILE    *current_file;
  122.     char    line[MAXLINE];
  123.     char    new_filename[BUFSIZ];
  124.  
  125.     if ((current_file = fopen(filename, "r")) == NULL) {
  126.         fprintf(stderr, "%s: cannot access %s\n", command_name, filename);
  127.         exit(1);
  128.     }
  129.     line_number = 0;
  130.     while (fgets(line, sizeof(line), current_file) != NULL) {
  131.         line_number++;
  132.         if (source_another_file(line, new_filename)) {
  133.             process_current_file(new_filename, phase);
  134.         } else {
  135.         if (phase == GATHER_REFERENCES)
  136.             build_tree(line);
  137.         else
  138.             fill_in_references(line);
  139.         }
  140.     }
  141.     fclose(current_file);
  142. }
  143.  
  144. source_another_file(line, filename)
  145.     char    *line;
  146.     char    *filename;
  147. {
  148.  
  149.     int    char_p;
  150.     char    *file_pos;
  151.  
  152.     char_p = 0;
  153.     if (plain_preprocessor == TRUE)
  154.         return(FALSE);
  155.     if (line[char_p] == '\0' || line[char_p] == '\n')
  156.         return(FALSE);
  157.     if (line[char_p++] != '.')
  158.         return(FALSE);
  159.     while (line[char_p] == ' ' || line[char_p] == '\t')
  160.         char_p++;
  161.     if (line[char_p] == '\0' || line[char_p] == '\n')
  162.         return(FALSE);
  163.     if (line[char_p] != 's')
  164.         return(FALSE);
  165.     char_p++;
  166.     if (line[char_p] == '\0' || line[char_p] == '\n')
  167.         return(FALSE);
  168.     if (line[char_p] != 'o')
  169.         return(FALSE);
  170.     char_p++;
  171.     while (line[char_p] == ' ' || line[char_p] == '\t')
  172.         char_p++;
  173.     if (line[char_p] == '\0' || line[char_p] == '\n')
  174.         return(FALSE);
  175.     file_pos = filename;
  176.     while (line[char_p] != '\n' && line[char_p] != ' ' && line[char_p] != '\t')
  177.         *file_pos++ = line[char_p++];
  178.     *file_pos = '\0';
  179.     if (file_pos == filename)
  180.         return(FALSE);
  181.     else
  182.         return(TRUE);
  183. }
  184.  
  185. build_tree(line)
  186.     char    *line;
  187. {
  188.     int    char_p;
  189.     int    entry_type;
  190.     struct codeword_entry    *node;
  191.  
  192.     char_p = 0;
  193.                 /*  Obtain type of codeword entry  */
  194.                 /*  HEADING or TABLE or FIGURE  */
  195.     if ((entry_type = get_codeword_type(line, &char_p)) == 0)
  196.         return;
  197.  
  198.                 /*  Read stuff from the line  */
  199.     if ((node = build_codeword_entry(line, &char_p, entry_type, &counters)) == NULL)
  200.         return;
  201.  
  202.     node->entry_type = entry_type;
  203.  
  204.                 /*  Insert codeword entry into the tree  */
  205.     if (root == NULL)
  206.         root = node;
  207.     else
  208.         node = insert_codeword_entry(root, node);
  209. }
  210.  
  211. fill_in_references(line)
  212.     char    *line;
  213. {
  214.     int    char_p;
  215.     char    out_line[MAXLINE];
  216.  
  217.     char_p = 0;
  218.                     /*  Decide if this line is a cross reference  */
  219.     if (try_reference_line(line, &char_p) == FALSE) {
  220.         strcpy(out_line, line);
  221.         fputs(out_line, stdout);
  222.         return;
  223.     }
  224.     substitute_references(line, &char_p);
  225. }
  226.  
  227. /*
  228.  * Read codeword entry from the codeword file.  Build a new codeword entry.
  229.  */
  230. struct    codeword_entry *
  231. build_codeword_entry(line, char_p, entry_type, counters)
  232.     char    *line;
  233.     int    *char_p;
  234.     int    entry_type;
  235.     struct    counters *counters;
  236. {
  237.     struct codeword_entry    *node;
  238.     char    current_token[MAXLINE];
  239.     char    *field;
  240.  
  241.     int    clear_level,  i;
  242.  
  243.  
  244.     node = new(struct codeword_entry);
  245.     node->entry_type = entry_type;
  246.                 /*  If this is a heading we must  */
  247.                 /*  set up the various counters  */
  248.     if (entry_type == HEADING) {
  249.         get_token(line, char_p, current_token, SKIP_SPACES);
  250.                     /*  Chapter Level  */
  251.         if (strcmp(current_token, "C") == 0 || strcmp(current_token, "1") == 0) {
  252.             node->appendix = FALSE;
  253.             if (strcmp(current_token, "1") == 0)
  254.                 document_type = MINOR_SECTIONED;
  255.             else {
  256.                 document_type = MAJOR_SECTIONED;
  257.                 counters->table_number = 0;
  258.                 counters->figure_number = 0;
  259.             }
  260.             if (start_chapter == 0)
  261.                 counters->levels[0]++;
  262.             else {
  263.                 counters->levels[0] = start_chapter;
  264.                 start_chapter = 0;
  265.             }
  266.             clear_level = 1;
  267.         }
  268.                         /*  Appendix Level  */
  269.         else if (strcmp(current_token, "A") == 0 || strcmp(current_token, "PA") == 0) {
  270.             node->appendix = TRUE;
  271.             if (strcmp(current_token, "PA") == 0)
  272.                 document_type = MINOR_SECTIONED;
  273.             else {
  274.                 document_type = MAJOR_SECTIONED;
  275.                 counters->table_number = 0;
  276.                 counters->figure_number = 0;
  277.             }
  278.             if (appendix_seen)
  279.                 counters->levels[0]++;
  280.             else {
  281.                 appendix_seen = TRUE;
  282.                 if (start_appendix == 0)
  283.                     counters->levels[0] = 1;
  284.                 else {
  285.                     counters->levels[0] = start_appendix;
  286.                     start_appendix = 0;
  287.                 }
  288.             }
  289.             clear_level = 1;
  290.         }
  291.                         /*  Section Level  */
  292.         else if (strcmp(current_token, "2") == 0) {
  293.             counters->levels[1]++;
  294.             clear_level = 2;
  295.         }
  296.                         /*  SubSection Level  */
  297.         else if (strcmp(current_token, "3") == 0) {
  298.             counters->levels[2]++;
  299.             clear_level = 3;
  300.         }
  301.                         /*  Paragraph Level  */
  302.         else if (strcmp(current_token, "4") == 0) {
  303.             counters->levels[3]++;
  304.             clear_level = 4;
  305.         }
  306.                         /*  SubParagraph Level  */
  307.         else if (strcmp(current_token, "5") == 0) {
  308.             counters->levels[4]++;
  309.             clear_level = 5;
  310.         }
  311.         for (i = clear_level;  i < 5;  i++)
  312.             counters->levels[i] = 0;
  313.     }
  314.     if (entry_type == TABLE) {
  315.         counters->table_number++;
  316.     }
  317.     if (entry_type == FIGURE) {
  318.         counters->figure_number++;
  319.     }
  320.     if (entry_type == CROSSREF) {    /*  ignore for now  */
  321.         free(node);
  322.         return(NULL);
  323.     }
  324.                 /*  Read the title  */
  325.     get_token(line, char_p, current_token, SKIP_SPACES);
  326.         node->title = strdup(current_token);
  327.                 /*  Read the codeword  */
  328.     get_token(line, char_p, current_token, SKIP_SPACES);
  329.     if (strcmp(current_token, "") == 0) {
  330.         free(node);
  331.         return(NULL);
  332.     } else {
  333.         node->codeword = strdup(current_token);
  334.         node->appendix = appendix_seen;
  335.         node->h1_counter = counters->levels[0];
  336.         node->h2_counter = counters->levels[1];
  337.         node->h3_counter = counters->levels[2];
  338.         node->h4_counter = counters->levels[3];
  339.         node->h5_counter = counters->levels[4];
  340.         node->table_number = counters->table_number;
  341.         node->figure_number = counters->figure_number;
  342.         return(node);
  343.     }
  344. }
  345.