home *** CD-ROM | disk | FTP | other *** search
/ Media Depot 5 / mediadepotvolume51993.iso / FILES / 13 / FAQ202S.ZIP / faq / enum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  4.2 KB  |  159 lines

  1. /*
  2.  * Automated chapter numbering for Texinfo files.
  3.  *
  4.  * Restrictions:
  5.  *
  6.  *   1. Only supports @chapter and @section directives.
  7.  *   2. The chapters and sections MUST be in their correct
  8.  *      order, and all the sections MUST be between their
  9.  *      parent chapter and the next chapter.
  10.  *   3. Conditional (@ifset, @ifclear) and file inclusion
  11.  *      (@include) directives not supported.  If you must
  12.  *      have file inclusion, feed the program with expanded
  13.  *      file (use -E switch to Makeinfo).
  14.  *
  15.  * Author: Eli Zaretskii <eliz@is.elta.co.il>
  16.  *
  17.  * Version: 1.1
  18.  *
  19.  * Last updated: 22 June, 1996
  20.  *
  21.  * ----------------------------------------------------------
  22.  *
  23.  * You can do whatever you like with this program, except:
  24.  * (1) preventing other people (including the author) do
  25.  * whatever they like, and (2) removing the author and
  26.  * version info above.
  27.  *
  28.  * ----------------------------------------------------------
  29.  *
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <errno.h>
  36.  
  37. #ifdef  __DJGPP__
  38.  
  39. /* Make so our start-up code is minimal: disable filename
  40.    globbing, and don't load environment file.  */
  41. #include <crt0.h>
  42.  
  43. char ** __crt0_glob_function(char *arg) { return (char **)0; }
  44. void   __crt0_load_environment_file(char *app_name) {}
  45.  
  46. #else
  47.  
  48. /* Some Unix boxes don't have functon prototypes on the header files.
  49.    -Wall will complain about this, so here are the prototypes:  */
  50.  
  51. void perror (const char *);
  52. int  fgetc  (FILE *);
  53. int  fputs  (const char *, FILE *);
  54. int  fprintf(FILE *, const char *, ...);
  55. int  fclose (FILE *);
  56.  
  57. #endif  /* __DJGPP__ */
  58.  
  59. static char chapter[] = "@chapter";
  60. static char section[] = "@section";
  61.  
  62. int
  63. main(int argc, char *argv[])
  64. {
  65.   if (argc == 3)
  66.     {
  67.       size_t maxline = 100;     /* not necessarily a limit, see below */
  68.       char *linebuf = (char *)malloc(maxline);
  69.       int chap_no  = 0, sec_no = 0;
  70.       FILE *fp_in  = fopen(argv[1], "r");
  71.       FILE *fp_out = fopen(argv[2], "w");
  72.  
  73.       if (linebuf == (char *)0)
  74.         {
  75.           errno = ENOMEM;
  76.           perror("line storage allocation");
  77.           return 2;
  78.         }
  79.       if (fp_in == (FILE *)0)
  80.         {
  81.           perror(argv[1]);
  82.           return 2;
  83.         }
  84.       if (fp_out == (FILE *)0)
  85.         {
  86.           perror(argv[2]);
  87.           return 2;
  88.         }
  89.  
  90.       while (fgets(linebuf, maxline, fp_in))
  91.         {
  92.           size_t linelen = strlen(linebuf);
  93.  
  94.           /* If this line is longer than linebuf[], enlarge the
  95.              buffer and read until end of this line.  */
  96.           while (linebuf[linelen - 1] != '\n')
  97.             {
  98.               maxline *= 2;
  99.               linebuf = (char *)realloc(linebuf, maxline);
  100.               if (linebuf == (char *)0)
  101.                 {
  102.                   errno = ENOMEM;
  103.                   perror("line storage re-allocation");
  104.                   return 2;
  105.                 }
  106.  
  107.               while (linelen < maxline && linebuf[linelen - 1] != '\n')
  108.                 {
  109.                   linebuf[linelen++] = fgetc(fp_in);
  110.  
  111.                   if (feof(fp_in))
  112.                     linebuf[linelen-1] = '\n';
  113.                 }
  114.  
  115.               linebuf[linelen] = '\0';
  116.             }
  117.  
  118.           if (memcmp(linebuf, chapter, sizeof(chapter) - 1) == 0)
  119.             {
  120.               chap_no++;
  121.               sec_no = 0;
  122.               fputs(chapter, fp_out);
  123.               fprintf(fp_out, " %d.", chap_no);
  124.               fputs(linebuf + sizeof(chapter) - 1, fp_out);
  125.             }
  126.           else if (memcmp(linebuf, section, sizeof(section) - 1) == 0)
  127.             {
  128.               sec_no++;
  129.               fputs(section, fp_out);
  130.               fprintf(fp_out, " %d.%d", chap_no, sec_no);
  131.               fputs(linebuf + sizeof(section) - 1, fp_out);
  132.             }
  133.           else
  134.             fputs(linebuf, fp_out);
  135.         }
  136.  
  137.       if (feof(fp_in))
  138.         {
  139.           fprintf(stderr, "%s has %d chapters\n", argv[1], chap_no);
  140.           fclose(fp_in);
  141.           fclose(fp_out);
  142.           return 0;
  143.         }
  144.       if (ferror(fp_in))
  145.         {
  146.           perror(argv[1]);
  147.           return 2;
  148.         }
  149.  
  150.       fprintf(stderr, "Not EOF and not Error???\n");
  151.       return 2;
  152.     }
  153.   else
  154.     {
  155.       fprintf(stderr, "Usage: %s infile outfile\n", *argv);
  156.       return 1;
  157.     }
  158. }
  159.