home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / docs / misc / amigafaq / src / addtoc.c
C/C++ Source or Header  |  1994-01-31  |  21KB  |  948 lines

  1. /*
  2.     addtoc.c    V1.0    29.08.1993
  3.  
  4.     Copyright (C)   1993    Jochen Wiedmann
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21.     This scans a texinfo file and adds a table of contents to the relating
  22.     AmigaGuide and Ascii files. Note that they have to be created first and
  23.     MUST be up to date. Best way to ensure this is using a Makefile.
  24.  
  25.     Usage:  addtoc TFILE/A,SPLITCHAP/M/N,GFILE/K,DFILE/K,DIFFS/K
  26.  
  27.     TFILE is the texinfo source, GFILE is the AmigaGuide file and DFILE is
  28.     the Ascii file. Files GFILE.new and DFILE.new are created.
  29.  
  30.     SPLITCHAP are numbers indicating chapters, after which to split the
  31.     FAQ into different parts. (Each part will be preceded by a toc.) This
  32.     is available for Ascii files only and is used to split FAQs in different
  33.     parts. Note, that these numbers must be adjacent, for example the
  34.     numbers 7 and 11 to split after chapter 6 and 10.
  35.  
  36.     DIFFS is the name of a file holding diffs to a previous version. This
  37.     must be produced using "gdiff -f new old". When creating the Ascii
  38.     version, changed or added lines will be marked with a "!" or "+",
  39.     respectively, together with the sections they belong to. 
  40.  
  41.     The texinfo file has to be in a special format: Each node (except the Top
  42.     node) MUST have a following line containing an @chapter, @section,
  43.     @subsection or @unnumbered command. The node commands MUST contain
  44.     nothing else than the node name. Note that @info and @tex commands are
  45.     ignored. In fact anything is ignored, except for the node and sectioning
  46.     commands.
  47.  
  48.     Author:      Jochen Wiedmann
  49.           Am Eisteich 9
  50.         72555 Metzingen (Germany)
  51.           Tel. 07123 / 14881
  52.  
  53.  
  54.     Computer:      Amiga 1200 (should run on any Amiga)
  55.  
  56.     Compiler:      Dice and Aztec-C (should run through SAS and gcc)
  57. */
  58.  
  59. #include <stdlib.h>
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include <errno.h>
  63. #ifndef FALSE
  64. #define FALSE 0
  65. #endif
  66. #ifndef TRUE
  67. #define TRUE (!FALSE)
  68. #endif
  69. #ifndef HAVE_STRICMP
  70. #define stricmp strcasecmp
  71. #define strnicmp strncasecmp
  72. #endif
  73.  
  74.  
  75. /*
  76.     Maximum length of a line in the source file. Probably works with longer
  77.     lines, but this may not be guaranteed. Even the line numbering WILL
  78.     be damaged.
  79. */
  80. #define MAXLINE 1024    /*  Maximum length of a node name.  */
  81.  
  82.  
  83. /*
  84.     This is used to hold the information we get by scanning the texinfo
  85.     source. Each node is represented by exactly one node structure.
  86. */
  87. struct node
  88. { struct node *next;
  89.   char *name;
  90.   char *title;
  91.   int type;
  92.   int changed;
  93. };
  94. #define TYPE_UNNUMBERED 0
  95. #define TYPE_CHAPTER    1
  96. #define TYPE_SECTION    2
  97. #define TYPE_SUBSECTION 3
  98.  
  99. /*
  100.     This is used to store the diffs information.
  101. */
  102. struct diffs
  103. { struct diffs *next;
  104.   int type;
  105.   int from, to;
  106. };
  107. #define DIFFTYPE_DELETED 0
  108. #define DIFFTYPE_CHANGED 1
  109. #define DIFFTYPE_ADDED 2
  110.  
  111.  
  112.  
  113. /*
  114.     The ignorespace() function removes trailing blanks from a line.
  115. */
  116. char *ignorespace(char *line)
  117.  
  118. { while (*line == ' '  ||  *line == '\t')
  119.   { ++line;
  120.   }
  121.   return(line);
  122. }
  123.  
  124.  
  125.  
  126.  
  127. /*
  128.     The salloc() function allocates memory for a string. Note, that it
  129.     removes trailing Line-Feeds.
  130. */
  131. char *salloc(char *str)
  132.  
  133. { char *ptr;
  134.   int len = strlen(str);
  135.  
  136.   while (len > 0  &&  str[len-1] == '\n')
  137.   { str[--len] = '\0';
  138.   }
  139.   if ((ptr = malloc(len+1))  !=  NULL)
  140.   { strcpy(ptr, str);
  141.   }
  142.   return(ptr);
  143. }
  144.  
  145.  
  146.  
  147.  
  148. /*
  149.     The memerror() function reports a memory error and terminates the
  150.     program.
  151. */
  152. void memerror(void)
  153.  
  154. { fprintf(stderr, "Out of memory!\n");
  155.   exit(20);
  156. }
  157.  
  158.  
  159.  
  160.  
  161. /*
  162.     The Scan() function scans the texinfo source file.
  163. */
  164. void Scan(struct node **first, char *tfile)
  165.  
  166. { FILE *fh;
  167.   struct node *node;
  168.   char line[MAXLINE+1];
  169.   char title[MAXLINE+1];
  170.   char *titleptr;
  171.   int lineno;
  172.   extern int errno;
  173.  
  174.   if ((fh = fopen(tfile, "r"))  ==  NULL)
  175.   { fprintf(stderr, "Cannot open %s as source file!\n", tfile);
  176.     exit(10);
  177.   }
  178.  
  179.   *first = NULL;
  180.   lineno = 0;
  181.   while (fgets(line, sizeof(line), fh)  !=  NULL)
  182.   { ++lineno;
  183.     if (strnicmp(line, "@node", 5)  ==  0           &&
  184.     fgets(title, sizeof(title), fh)  !=  NULL)
  185.     { int type;
  186.  
  187.       ++lineno;
  188.       type = -1;
  189.       if (strnicmp(title, "@unnumbered", 11)  ==  0)
  190.       { type = TYPE_UNNUMBERED;
  191.     titleptr = title+11;
  192.       }
  193.       else if (strnicmp(title, "@chapter", 8)  ==  0)
  194.       { type = TYPE_CHAPTER;
  195.     titleptr = title+8;
  196.       }
  197.       else if (strnicmp(title, "@section", 8)  ==  0)
  198.       { type = TYPE_SECTION;
  199.     titleptr = title+8;
  200.       }
  201.       else if (strnicmp(title, "@subsection", 11)  ==  0)
  202.       { type = TYPE_SUBSECTION;
  203.     titleptr = title+11;
  204.       }
  205.       else if (strnicmp(title, "@top", 4)  !=  0)
  206.       { fprintf(stderr, "Warning: Unknown sectioning command in line %d!\n",
  207.         lineno);
  208.     fprintf(stderr, 
  209.         "         Expected @chapter, @section, @subsection, @unnumbered or @top.\n");
  210.       }
  211.       if (type != -1)
  212.       { if ((node = calloc(1, sizeof(*node)))  ==  NULL)
  213.     { memerror();
  214.     }
  215.     if ((node->name = salloc(ignorespace(line+5)))  ==  NULL   ||
  216.         (node->title = salloc(ignorespace(titleptr)))  ==  NULL)
  217.     { memerror();
  218.     }
  219.     node->next = NULL;
  220.     node->type = type;
  221.     *first = node;
  222.     first = &node->next;
  223.       }
  224.     }
  225.   }
  226.   if (errno)
  227.   { perror("addtoc");
  228.   }
  229.   fclose(fh);
  230. }
  231.  
  232.  
  233.  
  234.  
  235. /*
  236.     The myscan() function scans a string like @{"title" Link "name"} for
  237.     name.
  238. */
  239. char *myscan(char *line, char *name)
  240.  
  241. { line = ignorespace(line);
  242.   if (strncmp(line, "@{\"", 3)  !=  0)
  243.   { return(NULL);
  244.   }
  245.   line += 3;
  246.   while (*line != '\"'  &&  *line != '\0')
  247.   { line++;
  248.   }
  249.   if (*line == '\0')
  250.   { return(NULL);
  251.   }
  252.   ++line;
  253.   line = ignorespace(line);
  254.   if (strnicmp(line, "Link", 4)  !=  0)
  255.   { return(NULL);
  256.   }
  257.   line+=4;
  258.   line = ignorespace(line);
  259.   if (*(line++) != '\"')
  260.   { return(NULL);
  261.   }
  262.   while (*line != '\"'  &&  *line != '\0')
  263.   { *(name++) = *(line++);
  264.   }
  265.   if (strncmp(line, "\"}", 2)  !=  0)
  266.   { return(NULL);
  267.   }
  268.   *name = '\0';
  269.   return(line+2);
  270. }
  271.  
  272.  
  273.  
  274.  
  275. /*
  276.     The subcmp() function checks for len occurences of c. Result is 0, if
  277.     there are, nonzero otherwise.
  278. */
  279. int subcmp(char *line, char c, int len)
  280.  
  281. { int i;
  282.  
  283.   for (i = 0;  i < len;  i++)
  284.   { if (line[i] != c)
  285.     { return(line[i]-c);
  286.     }
  287.   }
  288.   return(0);
  289. }
  290.  
  291.  
  292.  
  293.  
  294. /*
  295.     This function is used to read the diffs. These consist of lines like
  296.         am
  297.         cm n
  298.         dm n
  299.     where a means "following lines added after line m", c equals to "lines
  300.     m to n changed" and d is "lines m to n deleted". (n may be omitted, if
  301.     n == m.)
  302.  
  303.     a and c lines are followed by the appropriate number of lines to add or
  304.     change, followed by a line with a point only.
  305.  
  306.     When the lines are read, the doc file is scanned a first time to find,
  307.     which nodes are changed.
  308. */
  309. void ScanDiffs(struct diffs **first, char *file, char *docfile,
  310.            struct node *firstnode)
  311.  
  312. { FILE *fhin;
  313.   int linenr = 0;
  314.   char line[MAXLINE+1];
  315.   char subline[MAXLINE+1];
  316.   struct diffs *newdiffs;
  317.   struct diffs **first_diff = first;
  318.   struct node *currentnode = NULL;
  319.  
  320.   /*
  321.      Opening the diffs file.
  322.   */
  323.   if ((fhin = fopen(file, "r"))  ==  NULL)
  324.     { fprintf(stderr, "Cannot open diffs file %s.\n", file);
  325.       exit(10);
  326.     }
  327.  
  328.   while(fgets(line, sizeof(line), fhin)  !=  NULL)
  329.     { char *ptr2, *ptr = line;
  330.  
  331.       ++linenr;
  332.       if (*ptr != 'd'  &&  *ptr != 'a'  &&  *ptr != 'c')
  333.     { fprintf(stderr, 
  334.           "diffs file %s has inappropriate format in line %d.\n",
  335.           file, linenr);
  336.       exit(10);
  337.     }
  338.  
  339.       if ((newdiffs = calloc(1, sizeof(*newdiffs)))  ==  NULL)
  340.     { fprintf(stderr, "Out of memory.");
  341.       exit(10);
  342.     }
  343.       newdiffs->from = strtol(