home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / docs / misc / amigafaqg / src / addtoc.c next >
C/C++ Source or Header  |  1994-07-28  |  22KB  |  992 lines

  1. /*
  2.     addtoc.c    V1.1    25.07.1994
  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.     V1.0        25.08.1993
  60.  
  61.     V1.1        25.07.1994        Added support of @include
  62. */
  63.  
  64. #include <stdlib.h>
  65. #include <stdio.h>
  66. #include <string.h>
  67. #include <errno.h>
  68. #ifndef FALSE
  69. #define FALSE 0
  70. #endif
  71. #ifndef TRUE
  72. #define TRUE (!FALSE)
  73. #endif
  74. #ifndef HAVE_STRICMP
  75. #define stricmp strcasecmp
  76. #define strnicmp strncasecmp
  77. #endif
  78.  
  79.  
  80. /*
  81.     Maximum length of a line in the source file. Probably works with longer
  82.     lines, but this may not be guaranteed. Even the line numbering WILL
  83.     be damaged.
  84. */
  85. #define MAXLINE 1024    /*  Maximum length of a node name.  */
  86.  
  87.  
  88. /*
  89.     This is used to hold the information we get by scanning the texinfo
  90.     source. Each node is represented by exactly one node structure.
  91. */
  92. struct node
  93. { struct node *next;
  94.   char *name;
  95.   char *title;
  96.   int type;
  97.   int changed;
  98. };
  99. #define TYPE_UNNUMBERED 0
  100. #define TYPE_CHAPTER    1
  101. #define TYPE_SECTION    2
  102. #define TYPE_SUBSECTION 3
  103.  
  104. /*
  105.     This is used to store the diffs information.
  106. */
  107. struct diffs
  108. { struct diffs *next;
  109.   int type;
  110.   int from, to;
  111. };
  112. #define DIFFTYPE_DELETED 0
  113. #define DIFFTYPE_CHANGED 1
  114. #define DIFFTYPE_ADDED 2
  115.  
  116.  
  117.  
  118. /*
  119.     The ignorespace() function removes trailing blanks from a line.
  120. */
  121. char *ignorespace(char *line)
  122.  
  123. { while (*line == ' '  ||  *line == '\t')
  124.   { ++line;
  125.   }
  126.   return(line);
  127. }
  128.  
  129.  
  130.  
  131.  
  132. /*
  133.     The salloc() function allocates memory for a string. Note, that it
  134.     removes trailing Line-Feeds.
  135. */
  136. char *salloc(char *str)
  137.  
  138. { char *ptr;
  139.   int len = strlen(str);
  140.  
  141.   while (len > 0  &&  str[len-1] == '\n')
  142.   { str[--len] = '\0';
  143.   }
  144.   if ((ptr = malloc(len+1))  !=  NULL)
  145.   { strcpy(ptr, str);
  146.   }
  147.   return(ptr);
  148. }
  149.  
  150.  
  151.  
  152.  
  153. /*
  154.     The memerror() function reports a memory error and terminates the
  155.     program.
  156. */
  157. void memerror(void)
  158.  
  159. { fprintf(stderr, "Out of memory!\n");
  160.   exit(20);
  161. }
  162.  
  163.  
  164.  
  165.  
  166. /*
  167.    This is a subfunction of Scan() which is used to support @inclde:
  168.    It calls itself to do this.
  169. */
  170. void ScanFILE(FILE *fh, char *filename, struct node **first)
  171.  
  172. { extern int errno;
  173.   char line[MAXLINE+1];
  174.   char title[MAXLINE+1];
  175.   char *titleptr;
  176.   struct node *node;
  177.   int lineno = 0;
  178.  
  179.   while (fgets(line, sizeof(line), fh)  !=  NULL)
  180.   { ++lineno;
  181.     if (strnicmp(line, "@include", 8) == 0)
  182.     { char *includefilename = strtok(line+8, " \t\n\r\f");
  183.       FILE *includefh;
  184.  
  185.       if (!(includefh = fopen(includefilename, "r")))
  186.       { fprintf(stderr, "Cannot open %s as source file!\n", filename);
  187.     exit(10);
  188.       }
  189.  
  190.       ScanFILE(includefh, includefilename, first);
  191.       fclose(includefh);
  192.     }
  193.     else if (strnicmp(line, "@node", 5)  ==  0           &&
  194.     fgets(title, sizeof(title), fh)  !=  NULL)
  195.     { int type;
  196.  
  197.       ++lineno;
  198.       type = -1;
  199.       if (strnicmp(title, "@unnumbered", 11)  ==  0)
  200.       { type = TYPE_UNNUMBERED;
  201.     titleptr = title+11;
  202.       }
  203.       else if (strnicmp(title, "@chapter", 8)  ==  0)
  204.       { type = TYPE_CHAPTER;
  205.     titleptr = title+8;
  206.       }
  207.       else if (strnicmp(title, "@section", 8)  ==  0)
  208.       { type = TYPE_SECTION;
  209.     titleptr = title+8;
  210.       }
  211.       else if (strnicmp(title, "@subsection", 11)  ==  0)
  212.       { type = TYPE_SUBSECTION;
  213.     titleptr = title+11;
  214.       }
  215.       else if (strnicmp(title, "@top", 4)  !=  0)
  216.       { fprintf(stderr, "%s, %d, warning: Unknown sectioning command.\n",
  217.         lineno);
  218.     fprintf(stderr, 
  219.         "         Expected @chapter, @section, @subsection, @unnumbered or @top.\n");
  220.       }
  221.       if (type != -1)
  222.       { if ((node = calloc(1, sizeof(*node)))  ==  NULL)
  223.     { memerror();
  224.     }
  225.     if ((node->name = salloc(ignorespace(line+5)))  ==  NULL   ||
  226.         (node->title = salloc(ignorespace(titleptr)))  ==  NULL)
  227.     { memerror();
  228.     }
  229.     node->next = NULL;
  230.     node->type = type;
  231.  
  232.     /* 
  233.        Look for the last node in the current list and add the
  234.        current node.
  235.     */
  236.     { struct node **last;
  237.  
  238.       for (last = first;  *last != NULL;
  239.            last = &((*last)->next))
  240.       {
  241.       }
  242.       *last = node;
  243.     }
  244.       }
  245.     }
  246.   }
  247.  
  248.   if (errno)
  249.   { perror("addtoc");
  250.   }
  251. }
  252.  
  253.  
  254.  
  255.  
  256. /*
  257.     The Scan() function scans the texinfo source file. It uses
  258.     ScanFILE in order to work recursively.
  259. */
  260. void Scan(struct node **first, char *tfile)
  261.  
  262. { FILE *fh;
  263.  
  264.   if ((fh = fopen(tfile, "r"))  ==  NULL)
  265.   { fprintf(stderr, "Cannot open %s as source file!\n", tfile);
  266.     exit(10);
  267.   }
  268.  
  269.   *first = NULL;
  270.  
  271.   ScanFILE(fh, tfile, first);
  272.  
  273.   fclose(fh);
  274. }
  275.  
  276.  
  277.  
  278.  
  279. /*
  280.     The myscan() function scans a string like @{"title" Link "name"} for
  281.     name.
  282. */
  283. char *myscan(char *line, char *name)
  284.  
  285. { line = ignorespace(line);
  286.   if (strncmp(line, "@{\"", 3)  !=  0)
  287.   { return(NULL);
  288.   }
  289.   line += 3;
  290.   while (*line != '\"'  &&  *line != '\0')
  291.   { line++;
  292.   }
  293.   if (*line == '\0')
  294.   { return(NULL);
  295.   }
  296.   ++line;
  297.   line = ignorespace(line);
  298.   if (strnicmp(line, "Link", 4)  !=  0)
  299.   { return(NULL);
  300.   }
  301.   line+=4;
  302.   line = ignorespace(line);
  303.   if (*(line++) != '\"')
  304.   { return(NULL);
  305.   }
  306.   while (*line != '\"'  &&  *line != '\0')
  307.   { *(name++) = *(line++);
  308.   }
  309.   if (strncmp(line, "\"}", 2)  !=  0)
  310.   { return(NULL);
  311.   }
  312.   *name = '\0';
  313.   return(line+2);
  314. }
  315.  
  316.  
  317.  
  318.  
  319. /*
  320.     The subcmp() function checks for len occurences of c. Result is 0, if
  321.     there are, nonzero otherwise.
  322. */
  323. int subcmp(char *line, char c, int len)
  324.  
  325. { int i;
  326.  
  327.   for (i = 0;  i < len;  i++)
  328.   { if (line[i] != c)
  329.     { return(line[i]-c);
  330.     }
  331.   }
  332.   return(0);
  333. }
  334.  
  335.  
  336.  
  337.  
  338. /*
  339.     This function is used to read the diffs. These consist of lines like
  340.         am
  341.         cm n
  342.         dm n
  343.     where a means "following lines added after line m", c equals to "lines
  344.     m to n changed" and d is "lines m to n deleted". (n may be omitted, if
  345.     n == m.)
  346.  
  347.     a and c lines are followed by the appropriate number of lines to add or
  348.     change, followed by a line with a point only.
  349.  
  350.     When the lines are read, the doc file is scanned a first time to find,
  351.     which nodes are changed