home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / text / docs / amigafaq / english / src / addtoc.c next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  12.6 KB  |  604 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,GFILE/K,DFILE/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.     The texinfo file has to be in a special format: Each node (except the Top
  31.     node) MUST have a following line containing an @chapter, @section,
  32.     @subsection or @unnumbered command. The node commands MUST contain
  33.     nothing else than the node name.
  34.     Note that @info and @tex commands are ignored. In fact anything is
  35.     ignored, except for the node and sectioning commands.
  36.  
  37.     Author:      Jochen Wiedmann
  38.           Am Eisteich 9
  39.         72555 Metzingen (Germany)
  40.           Tel. 07123 / 14881
  41.  
  42.  
  43.     Computer:      Amiga 1200 (should run on any Amiga)
  44.  
  45.     Compiler:      Dice and Aztec-C (should run through SAS and gcc)
  46. */
  47.  
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #ifndef FALSE
  52. #define FALSE 0
  53. #endif
  54. #ifndef TRUE
  55. #define TRUE (!FALSE)
  56. #endif
  57.  
  58.  
  59.  
  60. /*
  61.     Maximum length of a line in the source file. Probably works with longer
  62.     lines, but this may not be guaranteed. Even the line numbering WILL
  63.     be damaged.
  64. */
  65. #define MAXLINE 1024    /*  Maximum length of a node name.  */
  66.  
  67.  
  68. /*
  69.     This is used to hold the information we get by scanning the texinfo
  70.     source. Each node is represented by exactly one node structure.
  71. */
  72. struct node
  73. { struct node *next;
  74.   char *name;
  75.   char *title;
  76.   int type;
  77. };
  78. #define TYPE_UNNUMBERED 0
  79. #define TYPE_CHAPTER    1
  80. #define TYPE_SECTION    2
  81. #define TYPE_SUBSECTION 3
  82.  
  83.  
  84.  
  85.  
  86. /*
  87.     The ignorespace() function removes trailing blanks from a line.
  88. */
  89. char *ignorespace(char *line)
  90.  
  91. { while (*line == ' '  ||  *line == '\t')
  92.   { ++line;
  93.   }
  94.   return(line);
  95. }
  96.  
  97.  
  98.  
  99.  
  100. /*
  101.     The salloc() function allocates memory for a string. Note, that it
  102.     removes trailing Line-Feeds.
  103. */
  104. char *salloc(char *str)
  105.  
  106. { char *ptr;
  107.   int len = strlen(str);
  108.  
  109.   while (len > 0  &&  str[len-1] == '\n')
  110.   { str[--len] = '\0';
  111.   }
  112.   if ((ptr = malloc(len+1))  !=  NULL)
  113.   { strcpy(ptr, str);
  114.   }
  115.   return(ptr);
  116. }
  117.  
  118.  
  119.  
  120.  
  121. /*
  122.     The memerror() function reports a memory error and terminates the
  123.     program.
  124. */
  125. void memerror(void)
  126.  
  127. { fprintf(stderr, "Out of memory!\n");
  128.   exit(20);
  129. }
  130.  
  131.  
  132.  
  133.  
  134. /*
  135.     The Scan() function scans the texinfo source file.
  136. */
  137. void Scan(struct node **first, char *tfile)
  138.  
  139. { FILE *fh;
  140.   struct node *node;
  141.   char line[MAXLINE+1];
  142.   char title[MAXLINE+1];
  143.   char *titleptr;
  144.   int lineno;
  145.   extern int errno;
  146.  
  147.   if ((fh = fopen(tfile, "r"))  ==  NULL)
  148.   { fprintf(stderr, "Cannot open %s as source file!\n", tfile);
  149.     exit(10);
  150.   }
  151.  
  152.   *first = NULL;
  153.   lineno = 0;
  154.   while (fgets(line, sizeof(line), fh)  !=  NULL)
  155.   { ++lineno;
  156.     if (strnicmp(line, "@node", 5)  ==  0           &&
  157.     fgets(title, sizeof(title), fh)  !=  NULL)
  158.     { int type;
  159.  
  160.       ++lineno;
  161.       type = -1;
  162.       if (strnicmp(title, "@unnumbered", 11)  ==  0)
  163.       { type = TYPE_UNNUMBERED;
  164.     titleptr = title+11;
  165.       }
  166.       else if (strnicmp(title, "@chapter", 8)  ==  0)
  167.       { type = TYPE_CHAPTER;
  168.     titleptr = title+8;
  169.       }
  170.       else if (strnicmp(title, "@section", 8)  ==  0)
  171.       { type = TYPE_SECTION;
  172.     titleptr = title+8;
  173.       }
  174.       else if (strnicmp(title, "@subsection", 11)  ==  0)
  175.       { type = TYPE_SUBSECTION;
  176.     titleptr = title+11;
  177.       }
  178.       else if (strnicmp(title, "@top", 4)  !=  0)
  179.       { fprintf(stderr, "Warning: Unknown sectioning command in line %d!\n",
  180.         lineno);
  181.     fprintf(stderr, "         Expected @chapter, @section, @subsection "
  182.             ", @unnumbered or @top.\n");
  183.       }
  184.       if (type != -1)
  185.       { if ((node = malloc(sizeof(*node)))  ==  NULL)
  186.     { memerror();
  187.     }
  188.     if ((node->name = salloc(ignorespace(line+5)))  ==  NULL   ||
  189.         (node->title = salloc(ignorespace(titleptr)))  ==  NULL)
  190.     { memerror();
  191.     }
  192.     node->next = NULL;
  193.     node->type = type;
  194.     *first = node;
  195.     first = &node->next;
  196.       }
  197.     }
  198.   }
  199.   if (errno)
  200.   { perror("addtoc");
  201.   }
  202.   fclose(fh);
  203. }
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.     The myscan() function scans a string like @{"title" Link "name"} for
  210.     name.
  211. */
  212. char *myscan(char *line, char *name)
  213.  
  214. { line = ignorespace(line);
  215.   if (strncmp(line, "@{\"", 3)  !=  0)
  216.   { return(NULL);
  217.   }
  218.   line += 3;
  219.   while (*line != '\"'  &&  *line != '\0')
  220.   { line++;
  221.   }
  222.   if (*line == '\0')
  223.   { return(NULL);
  224.   }
  225.   ++line;
  226.   line = ignorespace(line);
  227.   if (strnicmp(line, "Link", 4)  !=  0)
  228.   { return(NULL);
  229.   }
  230.   line+=4;
  231.   line = ignorespace(line);
  232.   if (*(line++) != '\"')
  233.   { return(NULL);
  234.   }
  235.   while (*line != '\"'  &&  *line != '\0')
  236.   { *(name++) = *(line++);
  237.   }
  238.   if (strncmp(line, "\"}", 2)  !=  0)
  239.   { return(NULL);
  240.   }
  241.   *name = '\0';
  242.   return(line+2);
  243. }
  244.  
  245.  
  246.  
  247.  
  248. /*
  249.     The ScanGuide() function scans the AmigaGuide file, removes the menu
  250.     in the top node and replaces it by the table of contents.
  251. */
  252. void ScanGuide(struct node *first, char *gtitle)
  253.  
  254. { FILE *fhin;
  255.   FILE *fhout;
  256.   char *newtitle;
  257.   struct node *node;
  258.   int InMain;
  259.   int lineno;
  260.   char line[MAXLINE+1];
  261.   char name[MAXLINE+1];
  262.  
  263.   /*
  264.       Opening files
  265.   */
  266.   if ((newtitle = malloc(strlen(gtitle)+5))  ==  NULL)
  267.   { memerror();
  268.   }
  269.   sprintf(newtitle, "%s.new", gtitle);
  270.  
  271.   if ((fhin = fopen(gtitle, "r"))  ==  NULL)
  272.   { fprintf(stderr, "Cannot open %s as input!\n", gtitle);
  273.     exit(10);
  274.   }
  275.   if ((fhout = fopen(newtitle, "w"))  ==  NULL)
  276.   { fprintf(stderr, "Cannot open %s as output!\n", newtitle);
  277.     exit(11);
  278.   }
  279.  
  280.   /*
  281.       Looking for the Top Node
  282.   */
  283.   InMain = FALSE;
  284.   lineno = 0;
  285.   while(fgets(line, sizeof(line), fhin)  !=  NULL)
  286.   { ++lineno;
  287.     if (strnicmp(line, "@Node Main", 10)  ==  0)
  288.     { InMain = TRUE;
  289.     }
  290.     else if (strnicmp(line, "@EndNode", 8)  ==  0)
  291.     { InMain = FALSE;
  292.     }
  293.     if (InMain  &&  strnicmp(ignorespace(line), "@{\"", 3) == 0)
  294.     { if (myscan(line, name)  ==  NULL)
  295.       { fprintf(stderr,
  296.         "Error: Cannot scan line %d of %s (unknown format)!\n",
  297.         lineno, gtitle);
  298.       }
  299.       else
  300.       { int blanks;
  301.  
  302.     fputs(line, fhout);
  303.     blanks = ignorespace(line)-line;
  304.     for (node = first;  node != NULL;  node = node->next)
  305.     { if (strncmp(name, node->name, strlen(name)) == 0  &&
  306.           (node->type == TYPE_CHAPTER  ||  node->type == TYPE_UNNUMBERED))
  307.       { break;
  308.       }
  309.     }
  310.  
  311.     node = node->next;
  312.     while (node != NULL  &&  node->type != TYPE_CHAPTER  &&
  313.            node->type != TYPE_UNNUMBERED)
  314.     { switch (node->type)
  315.       { case TYPE_UNNUMBERED:
  316.         case TYPE_CHAPTER:
  317.           break;
  318.         case TYPE_SECTION:
  319.           fprintf(fhout, "    ");
  320.           break;
  321.         case TYPE_SUBSECTION:
  322.           fprintf(fhout, "        ");
  323.           break;
  324.       }
  325.       fprintf(fhout, "@{\" %s \" Link \"%s\"}\n", node->title,
  326.           node->name);
  327.       node = node->next;
  328.     }
  329.       }
  330.     }
  331.     else
  332.     { fputs(line, fhout);
  333.     }
  334.   }
  335.   if (errno)
  336.   { perror("addtoc");
  337.   }
  338.   fclose(fhin);
  339.   fclose(fhout);
  340. }
  341.  
  342.  
  343.  
  344.  
  345. /*
  346.     The subcmp() function checks for len occurences of c. Result is 0, if
  347.     there are, nonzero otherwise.
  348. */
  349. int subcmp(char *line, char c, int len)
  350.  
  351. { int i;
  352.  
  353.   for (i = 0;  i < len;  i++)
  354.   { if (line[i] != c)
  355.     { return(line[i]-c);
  356.     }
  357.   }
  358.   return(0);
  359. }
  360.  
  361.  
  362.  
  363.  
  364. /*
  365.     The ScanDoc function scans the Ascii document and adds the table of
  366.     contents and section numbers.
  367. */
  368. void ScanDoc(struct node *first, char *dtitle)
  369.  
  370. { FILE *fhin, *fhout;
  371.   char *newtitle;
  372.   int lineno;
  373.   int chapter, section, subsection;
  374.   int tocdone;
  375.   char line[MAXLINE+1];
  376.   char subline[MAXLINE+1];
  377.   static char subchar[3] = "*=-";
  378.   char c;
  379.  
  380.   /*
  381.       Opening files
  382.   */
  383.   if ((newtitle = malloc(strlen(dtitle)+5))  ==  NULL)
  384.   { memerror();
  385.   }
  386.   sprintf(newtitle, "%s.new", dtitle);
  387.  
  388.   if ((fhin = fopen(dtitle, "r"))  ==  NULL)
  389.   { fprintf(stderr, "Cannot open %s as input!\n", dtitle);
  390.     exit(10);
  391.   }
  392.   if ((fhout = fopen(newtitle, "w"))  ==  NULL)
  393.   { fprintf(stderr, "Cannot open %s as output!\n", newtitle);
  394.     exit(11);
  395.   }
  396.  
  397.  
  398.   /*
  399.       Scanning for nodes
  400.   */
  401.   tocdone = FALSE;
  402.   lineno = 0;
  403.   chapter = section = subsection = 0;
  404.   while (fgets(line, sizeof(line), fhin)  !=  NULL)
  405.   { ++lineno;
  406.     if (first != NULL  &&
  407.     strncmp(line, first->title, strlen(first->title))  ==  0   &&
  408.     fgets(subline, sizeof(subline), fhin)  !=  NULL)
  409.     { ++lineno;
  410.       switch(first->type)
  411.       { case TYPE_UNNUMBERED:
  412.     case TYPE_CHAPTER:
  413.       c = subchar[0];
  414.       break;
  415.     case TYPE_SECTION:
  416.       c = subchar[1];
  417.       break;
  418.     case TYPE_SUBSECTION:
  419.       c = subchar[2];
  420.       break;
  421.       }
  422.       if(subcmp(subline, c, strlen(first->title))  ==  0)
  423.       { int i;
  424.     int level;
  425.     struct node *node;
  426.     char number[128];
  427.  
  428.     /*
  429.         Add the table of contents, if we have found the first
  430.         node.
  431.     */
  432.     if (!tocdone)
  433.     { int chapter, section, subsection;
  434.  
  435.       chapter = section = subsection = 0;
  436.       for (node = first;  node != NULL;  node = node->next)
  437.       { if (node->type == TYPE_CHAPTER  ||  node->type == TYPE_UNNUMBERED)
  438.         { putc('\n', fhout);
  439.         }
  440.         switch (node->type)
  441.         { case TYPE_CHAPTER:
  442.         ++chapter;
  443.         section = subsection = 0;
  444.         fprintf(fhout, "%d. ", chapter);
  445.         break;
  446.           case TYPE_UNNUMBERED:
  447.         break;
  448.           case TYPE_SECTION:
  449.         ++section;
  450.         subsection = 0;
  451.         fprintf(fhout, "  %d. ", section);
  452.         break;
  453.           case TYPE_SUBSECTION:
  454.         ++subsection;
  455.         fprintf(fhout, "    %d. ", subsection);
  456.         break;
  457.         }
  458.         fprintf(fhout, "%s\n", node->title);
  459.       }
  460.       fprintf(fhout, "\n\n\n");
  461.       tocdone = TRUE;
  462.     }
  463.  
  464.     switch(first->type)
  465.     { case TYPE_UNNUMBERED:
  466.         strcpy(number, "");
  467.         level = 0;
  468.         break;
  469.       case TYPE_CHAPTER:
  470.         ++chapter;
  471.         section = subsection = 0;
  472.         sprintf(number, "%d. ", chapter);
  473.         level = 0;
  474.         break;
  475.       case TYPE_SECTION:
  476.         ++section;
  477.         subsection = 0;
  478.         sprintf(number, "%d.%d. ", chapter, section);
  479.         level = 1;
  480.         break;
  481.       case TYPE_SUBSECTION:
  482.         ++subsection;
  483.         sprintf(number, "%d.%d.%d. ", chapter, section, subsection);
  484.         level = 2;
  485.         break;
  486.     }
  487.     fprintf(fhout, "%s%s", number, line);
  488.     for (i = 0;  i < strlen(number);  i++)
  489.     { putc((int) subchar[level], fhout);
  490.     }
  491.     fputs(subline, fhout);
  492.     first = first->next;
  493.       }
  494.       else
  495.       { fputs(line, fhout);
  496.     fputs(subline, fhout);
  497.       }
  498.     }
  499.     else
  500.     { fputs(line, fhout);
  501.     }
  502.   }
  503.   if (first != NULL)
  504.   { fprintf(stderr, "Missing item, probably different text in header "
  505.             "and menu:\n%s\n", first->title);
  506.   }
  507.   if (errno)
  508.   { perror("addtoc");
  509.   }
  510.   fclose(fhin);
  511.   fclose(fhout);
  512. }
  513.  
  514.  
  515.  
  516.  
  517. /*
  518.     This prints the usage information and terminates.
  519. */
  520. void Usage(void)
  521.  
  522. { fprintf(stderr,
  523.     "Usage: addtoc TFILE/A,GFILE/K,DFILE/K\n\n"
  524.     "TFILE is the texinfo source file.\n"
  525.     "GFILE is the AmigaGuide file created from TFILE and DFILE the "
  526.     "corresponding\n"
  527.     "Ascii file. <GFILE>.new and <TFILE>.new, respectively, are created,\n"
  528.     "if the latter options are present.\n\n");
  529.   exit(1);
  530. }
  531.  
  532.  
  533.  
  534.  
  535. /*
  536.     This is main(). Does nothing except processing the arguments and calling
  537.     the Scan... functions.
  538. */
  539. void main(int argc, char *argv[])
  540.  
  541. { char *tfile, *gfile, *dfile;
  542.   struct node *first;
  543.   int i;
  544.  
  545. #ifdef DEBUG
  546.   tfile = "/Amiga-FAQ.texinfo";
  547.   gfile = "/Amiga-FAQ.guide";
  548.   dfile = "/Amiga-FAQ.doc";
  549. #else
  550.   if (argc < 2)
  551.   { Usage();
  552.   }
  553.  
  554.   tfile = NULL;
  555.   gfile = NULL;
  556.   dfile = NULL;
  557.  
  558.   for (i = 1;  i < argc;  i++)
  559.   { if (stricmp(argv[i], "gfile")  ==  0)
  560.     { if (++i == argc  ||  gfile != NULL)
  561.       { Usage();
  562.       }
  563.       gfile = argv[i];
  564.     }
  565.     else if (strnicmp(argv[i], "gfile=", 6)  ==  0)
  566.     { if (gfile != NULL)
  567.       { Usage();
  568.       }
  569.       gfile = &argv[i][6];
  570.     }
  571.     else if (stricmp(argv[i], "dfile")  ==  0)
  572.     { if (++i == argc  ||  dfile != NULL)
  573.       { Usage();
  574.       }
  575.       dfile = argv[i];
  576.     }
  577.     else if (strnicmp(argv[i], "dfile=", 6)  ==  0)
  578.     { if (dfile != NULL)
  579.       { Usage();
  580.       }
  581.       dfile = &argv[i][6];
  582.     }
  583.     else
  584.     { if (tfile != NULL)
  585.       { Usage();
  586.       }
  587.       tfile = argv[i];
  588.     }
  589.   }
  590.   if (tfile == NULL)
  591.   { Usage();
  592.   }
  593. #endif    /*  !DEBUG   */
  594.  
  595.  
  596.   Scan(&first, tfile);
  597.   if (gfile)
  598.   { ScanGuide(first, gfile);
  599.   }
  600.   if (dfile)
  601.   { ScanDoc(first, dfile);
  602.   }
  603. }
  604.