home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / 3d / irit / docs / irithlp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-11  |  15.3 KB  |  476 lines

  1. /*****************************************************************************
  2. *   Documentation processor of IRIT- a 3d solid modeller.             *
  3. ******************************************************************************
  4. * Usage:                                     *
  5. *   irithlp [-t] [-l] [-h] [-o OutFileName] [-z] [InFileName]             *
  6. *                                         *
  7. * Written by:  Gershon Elber                Ver 1.0, Nov. 1991   *
  8. *****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include "irit_sm.h"
  14.  
  15. #define MAX_ARRAY_SIZE    1000
  16.  
  17. typedef enum {
  18.     UNDEFINED_DOC_PROCESS = 0,
  19.  
  20.     PLAIN_TEXT_DOCS,
  21.     LATEX_DOCS,
  22.     IRIT_HELP_DOCS
  23. } DocProcessType;
  24.  
  25. struct {
  26.     char *FindString;
  27.     char *ReplaceString;
  28. } GlblStrSubstStruct[] = {
  29.     { "_IRIT_VERSION_", VERSION },
  30.     { NULL, NULL }
  31. };
  32.  
  33. void ProcessFiles(FILE *Fin, FILE *Fout, DocProcessType OutputDocType);
  34. void FilterLatexMods(char *Line1, char *Line2);
  35. void MacroSubstitute(char *Line);
  36.  
  37. /*****************************************************************************
  38. * DESCRIPTION:                                                               M
  39. * Main module - process command line options.                     M
  40. *                                                                            *
  41. * PARAMETERS:                                                                M
  42. *   argc, argv : command line options.                         M
  43. *                                                                            *
  44. * RETURN VALUE:                                                              M
  45. *   void                                                                     M
  46. *                                                                            *
  47. * KEYWORDS:                                                                  M
  48. *   main                                                                     M
  49. *****************************************************************************/
  50. void main(int argc, char **argv)
  51. {
  52.     FILE
  53.     *Fin = stdin,
  54.     *Fout = stdout;
  55.     DocProcessType
  56.     OutputDocType = UNDEFINED_DOC_PROCESS;
  57.  
  58.     while (argc > 1) {
  59.     if (strcmp(argv[1], "-t") == 0) {
  60.         OutputDocType = PLAIN_TEXT_DOCS;
  61.         argc--;
  62.         argv++;
  63.     }
  64.     else if (strcmp(argv[1], "-l") == 0) {
  65.         OutputDocType = LATEX_DOCS;
  66.         argc--;
  67.         argv++;
  68.     }
  69.     else if (strcmp(argv[1], "-h") == 0) {
  70.         OutputDocType = IRIT_HELP_DOCS;
  71.         argc--;
  72.         argv++;
  73.     }
  74.     else if (strcmp(argv[1], "-o") == 0) {
  75.         if ((Fout = fopen(argv[2], "w")) == NULL) {
  76.         fprintf(stderr, "Failed to open \"%s\".\n", argv[2]);
  77.         exit(1);
  78.         }
  79.         argc -= 2;
  80.         argv += 2;
  81.     }
  82.     else if (strcmp(argv[1], "-z") == 0) {
  83.         fprintf(stderr, "Usage: IritHlp [-t] [-l] [-h] [-o OutFileName] [-z] [InFileName].\n");
  84.         exit(0);
  85.     }
  86.     else
  87.         break;
  88.     }
  89.  
  90.     if (argc > 1) {
  91.     if ((Fin = fopen(argv[1], "r")) == NULL) {
  92.         fprintf(stderr, "Failed to open \"%s\".\n", argv[2]);
  93.         exit(1);
  94.     }
  95.     }
  96.  
  97.     if (OutputDocType == UNDEFINED_DOC_PROCESS) {
  98.     fprintf(stderr, "One of [-t] [-l] [-h] must be specified.\n");
  99.     exit(1);
  100.     }
  101.  
  102.     ProcessFiles(Fin, Fout, OutputDocType);
  103.  
  104.     fclose(Fin);
  105.     fclose(Fout);
  106.  
  107.     exit(0);
  108. }
  109.  
  110. /*****************************************************************************
  111. * DESCRIPTION:                                                               *
  112. * Reads lines from Fin and dumps them to Fout after the following         *
  113. * processing take place:                             *
  114. * Allways:                                            *
  115. *    a. Lines start with ';' are ignored ans skipped (comments).         *
  116. *    b. Lines start with '#' followed by four ingeters (indentation,         *
  117. *       # elements per line, a Boolean to specify vertical seperator, and    *
  118. *       a Boolean flag on the row (TRUE) or col (FALSE) entries order)         *
  119. *       signifies an array. All entries, one per line are part of the array  *
  120. *       until another '#' occurs. Each of the method below should do its     *
  121. *       best to create such an array.                         *
  122. *    c. Lines start with '%' are copied verbatim to all output types.         *
  123. * 1. OutputDocType = PLAIN_TEXT_DOCS                         *
  124. *    + Lines start with '*' are echoed as is with no tex related processing. *
  125. *    + Lines start with '@' are ignored.                     *
  126. *    + Lines start with '!' are echoed as is with underline.             *
  127. *    + Lines start with '$' are ignored.                     *
  128. *    + Lines start with '^' are echoed as is.                     *
  129. *    + Lines start with '&' are echoed as is with underline.             *
  130. *    + Other lines are dumped out as is after filtering out of '{\cmd * }'   *
  131. *      latex modifiers.                                 *
  132. *    + all lines are dumped out without their first character.             *
  133. * 2. OutputDocType = LATEX_DOCS                             *
  134. *    + Lines start with '*' are echoed as is with no tex related processing. *
  135. *    + Lines start with '@' are echoed as is.                     *
  136. *    + Lines start with '!' are ignored.                     *
  137. *    + Lines start with '$' are ignored.                     *
  138. *    + Lines start with '^' are ignored.                     *
  139. *    + Lines start with '&' are ignored.                     *
  140. *    + Other lines are dumped out as is.                     *
  141. *    + All lines are dumped out without their first character.             *
  142. * 3. OutputDocType = IRIT_HELP_DOCS                         *
  143. *    + Lines start with '*' are echoed as is with no tex related processing. *
  144. *    + Lines start with '@' are ignored.                     *
  145. *    + Lines start with '!' are dumped out as is.                 *
  146. *    + Lines start with '$' causes echoing of the '$' in a single line.      *
  147. *    + Lines start with '^' are echoed as is.                     *
  148. *    + Lines start with '&' are ignored.                     *
  149. *    + Other lines are dumped out as is after filtering out of '{\cmd * }'   *
  150. *      latex modifiers.                                 *
  151. *    + All lines are dumped out with their first character as in input.      *
  152. *                                                                            *
  153. *                                                                            *
  154. *                                                                            *
  155. * PARAMETERS:                                                                *
  156. *   Fin:           File to read from, probably irit.src                      *
  157. *   Fout:          Output file.                                              *
  158. *   OutputDocType: Type of output - tex, hlp, doc, etc.                      *
  159. *                                                                            *
  160. * RETURN VALUE:                                                              *
  161. *   void                                                                     *
  162. *****************************************************************************/
  163. void ProcessFiles(FILE *Fin, FILE *Fout, DocProcessType OutputDocType)
  164. {
  165.     int i, j, k,
  166.     LineNum = 0;
  167.     char Line1[LINE_LEN_LONG], Line2[LINE_LEN_LONG], *p;
  168.  
  169.     while (fgets(Line1, LINE_LEN_LONG - 1, Fin) != NULL) {
  170.     LineNum++;
  171.     MacroSubstitute(Line1);
  172.     if (Line1[0] == ';')
  173.         continue;
  174.  
  175.     if (Line1[0] == '%') {
  176.         if (OutputDocType != LATEX_DOCS)
  177.         fprintf(Fout, " ");
  178.         fprintf(Fout, &Line1[1]);
  179.         continue;
  180.     }
  181.         
  182.     if (Line1[0] == '#') {             /* Beginning of array definition. */
  183. #if defined(AMIGA) && defined(__SASC)
  184.         static char __far ArrayEntries[MAX_ARRAY_SIZE][LINE_LEN_LONG];
  185. #else
  186.           static char ArrayEntries[MAX_ARRAY_SIZE][LINE_LEN_LONG];
  187. #endif
  188.         int Indentation, NumPerLine, VertSep, NumLines, EntriesRowBased,
  189.         StartLineNum = LineNum,
  190.         NumArrayEntries = 0;
  191.  
  192.         if (sscanf(&Line1[1], "%d %d %d %d",
  193.                &Indentation, &NumPerLine,
  194.                &VertSep, &EntriesRowBased) != 4) {
  195.         fprintf(stderr, "Four integers expected in line %d\n", LineNum);
  196.         exit(1);
  197.         }
  198.  
  199.         /* Gets all the entries of the array: */
  200.         while ((p =    fgets(ArrayEntries[NumArrayEntries++],
  201.                         LINE_LEN_LONG - 1, Fin)) != NULL &&
  202.            p[0] != '#' &&
  203.            NumArrayEntries < MAX_ARRAY_SIZE) {
  204.         p += strlen(p);              /* Remove the EOL from string. */
  205.         while (*--p < ' ');
  206.         p[1] = 0;
  207.  
  208.         LineNum++;
  209.             }
  210.         if (p != NULL)
  211.         LineNum++;
  212.         NumArrayEntries--;
  213.  
  214.         if (p[0] != '#') {
  215.         fprintf(stderr,
  216.             "Array at line %d too large (max %d) or missing '#'.\n",
  217.             StartLineNum, MAX_ARRAY_SIZE);
  218.         exit(1);
  219.         }
  220.  
  221.         if (NumPerLine == 0) {
  222.         fprintf(stderr,
  223.             "Number of items per line is zero at line %d.\n",
  224.             StartLineNum);
  225.         exit(1);
  226.         }
  227.  
  228.         /* Now we have all the array elements. Computes how many lines   */
  229.         /* need and print the lines in the format this documentation is  */
  230.         /* suppose to be in.                         */
  231.         NumLines = (NumArrayEntries + NumPerLine - 1) / NumPerLine;
  232.         switch (OutputDocType) {
  233.         case PLAIN_TEXT_DOCS:
  234.             sprintf(Line2, "%%-%ds", Indentation);
  235.             fprintf(Fout, "\n");
  236.             for (i = 0; i < NumLines; i++) {
  237.             k = EntriesRowBased ? i * NumPerLine : i;
  238.             fprintf(Fout, "   ");
  239.             for (j = 0; j < NumPerLine; j++) {
  240.                 if (k >= NumArrayEntries)
  241.                 fprintf(Fout, Line2, "");
  242.                 else {
  243.                 FilterLatexMods(ArrayEntries[k],
  244.                         ArrayEntries[k]);
  245.                 fprintf(Fout, Line2, &ArrayEntries[k][1]);
  246.                 }
  247.                 k += EntriesRowBased ? 1 : NumLines;
  248.             }
  249.             fprintf(Fout, "\n");
  250.             }
  251.             fprintf(Fout, "\n");
  252.             break;
  253.         case LATEX_DOCS:
  254.             fprintf(Fout,
  255.                 "\n\n\\smallskip\n\n\\begin{center}\n    \\begin{tabular}{|");
  256.             if (VertSep)
  257.             for (i = 0; i < NumPerLine; i++)
  258.                 fprintf(Fout, "|l");
  259.             else {
  260.             fprintf(Fout, "|");
  261.             for (i = 0; i < NumPerLine; i++)
  262.                 fprintf(Fout, "l");
  263.             }
  264.             fprintf(Fout,"||} \\hline \\hline\n");
  265.  
  266.             for (i = 0; i < NumLines; i++) {
  267.             k = EntriesRowBased ? i * NumPerLine : i;
  268.             fprintf(Fout, "\t");
  269.             for (j = 0; j < NumPerLine; j++) {
  270.                 fprintf(Fout, "%s %s ",
  271.                     k < NumArrayEntries ? &ArrayEntries[k][1] : "",
  272.                     j == NumPerLine - 1 ? "\\\\\n" : "&");
  273.                 k += EntriesRowBased ? 1 : NumLines;
  274.             }
  275.             }
  276.             fprintf(Fout,
  277.                 "\t\\hline\n    \\end{tabular}\n\\end{center}\n\n\\smallskip\n\n");
  278.             break;
  279.         case IRIT_HELP_DOCS:
  280.             sprintf(Line2, "%%-%ds", Indentation);
  281.             fprintf(Fout, "\n");
  282.             for (i = 0; i < NumLines; i++) {
  283.             k = EntriesRowBased ? i * NumPerLine : i;
  284.             fprintf(Fout, "   ");
  285.             for (j = 0; j < NumPerLine; j++) {
  286.                 if (k >= NumArrayEntries)
  287.                 fprintf(Fout, Line2, "");
  288.                 else {
  289.                 FilterLatexMods(ArrayEntries[k],
  290.                         ArrayEntries[k]);
  291.                 fprintf(Fout, Line2, &ArrayEntries[k][1]);
  292.                 }
  293.                 k += EntriesRowBased ? 1 : NumLines;
  294.             }
  295.             fprintf(Fout, "\n");
  296.             }
  297.             fprintf(Fout, "\n");
  298.             break;
  299.         case UNDEFINED_DOC_PROCESS:
  300.             break;
  301.         }
  302.  
  303.         continue;
  304.     }
  305.  
  306.     switch (OutputDocType) {
  307.         case PLAIN_TEXT_DOCS:
  308.         switch (Line1[0]) {
  309.             case '@':
  310.             case '$':
  311.             break;
  312.             case '^':
  313.             case '*':
  314.             fprintf(Fout, "%s", &Line1[1]);
  315.             break;
  316.             case '&':
  317.             case '!':
  318.                 j = (78 - (int) strlen(&Line1[1])) / 2;
  319.                 for (i = 0; i < j; i++)
  320.                 fprintf(Fout, " ");
  321.             fprintf(Fout, "%s", &Line1[1]);
  322.                 for (i = 0; i < j; i++)
  323.                 fprintf(Fout, " ");
  324.             for (i = 1; i < (int) strlen(&Line1[1]); i++)
  325.                 fprintf(Fout, "-");
  326.             fprintf(Fout, "\n");
  327.             break;
  328.             default:
  329.             fprintf(stderr,
  330.                 "Undefined first char command at line %d.\n",
  331.                 LineNum);
  332.             /* Let it print without the first char... */
  333.             case 0:
  334.             case 10:
  335.             case 13:
  336.             case ' ':
  337.             FilterLatexMods(Line1, Line2);
  338.             fprintf(Fout, "%s", &Line2[1]);
  339.             break;
  340.         }
  341.         break;
  342.         case LATEX_DOCS:
  343.         switch (Line1[0]) {
  344.             case '!':
  345.             case '$':
  346.             case '&':
  347.             case '^':
  348.             break;
  349.             case '@':
  350.             case '*':
  351.             fprintf(Fout, "%s", &Line1[1]);
  352.             break;
  353.             default:
  354.             fprintf(stderr,
  355.                 "Undefined first char command at line %d.\n",
  356.                 LineNum);
  357.             /* Let it print without the first char... */
  358.             case ' ':
  359.             case 0:
  360.                 case 10:
  361.             case 13:
  362.             fprintf(Fout, "%s", Line1[1] == 0 ? "\n" : &Line1[1]);
  363.             break;
  364.         }
  365.         break;
  366.         case IRIT_HELP_DOCS:
  367.         switch (Line1[0]) {
  368.             case '@':
  369.             case '&':
  370.             break;
  371.             case '*':
  372.             case '^':
  373.             case '!':
  374.             fprintf(Fout, "%s", &Line1[1]);
  375.             break;
  376.             case '$':
  377.             fprintf(Fout, "$\n");
  378.             break;
  379.             default:
  380.             fprintf(stderr,
  381.                 "Undefined first char command at line %d.\n",
  382.                 LineNum);
  383.             /* Let it print without the first char... */
  384.             case ' ':
  385.             case 0:
  386.             case 10:
  387.             case 13:
  388.             FilterLatexMods(Line1, Line2);
  389.             fprintf(Fout, "%s", &Line2[1]);
  390.             break;
  391.         }
  392.         break;
  393.         case UNDEFINED_DOC_PROCESS:
  394.         break;
  395.     }
  396.     }
  397. }
  398.  
  399. /*****************************************************************************
  400. * DESCRIPTION:                                                               *
  401. * Filters out expression of the form '{\cmd * }' in Line1 into '*' in line2. *
  402. * These are latex modifiers not wanted in plain text output.             *
  403. *   Also are filtered out any \? to ?, any $*$ to * and \verb+^+ to ^.       *
  404. *   A space is forced as first char in line.                     *
  405. *                                                                            *
  406. *                                                                            *
  407. * PARAMETERS:                                                                *
  408. *   Line1:     Input line to filter out.                                      *
  409. *   Line2:     Filtered version of Line1.                                    *
  410. *                                                                            *
  411. * RETURN VALUE:                                                              *
  412. *   void                                                                     *
  413. *****************************************************************************/
  414. void FilterLatexMods(char *Line1, char *Line2)
  415. {
  416.     int i = 0,
  417.     j = 0,
  418.     InModifier = FALSE;
  419.     char Line3[LINE_LEN_LONG];
  420.  
  421.     Line3[j++] = ' ';
  422.  
  423.     while (Line1[i]) {
  424.     if (Line1[i] == '{' && Line1[i + 1] == '\\' && !isspace(Line1[i + 2])) {
  425.         /* Here is one - filter it out. */
  426.         while (!isspace(Line1[i++]));
  427.         while (isspace(Line1[i++]));
  428.         i--;
  429.         InModifier = TRUE;
  430.     }
  431.     else if (Line1[i] == '}' && InModifier) {
  432.         i++;
  433.         InModifier = FALSE;
  434.     }
  435.     else if (strncmp(&Line1[i], "\\verb+^+", 8) == 0) {
  436.         i += 8;
  437.         Line3[j++] = '^';
  438.     }
  439.     else if (Line1[i] == '\\') {
  440.         i++;
  441.         Line3[j++] = Line1[i++];
  442.     }
  443.     else if (Line1[i] == '$' && i > 0) {
  444.         i++;
  445.     }
  446.     else
  447.         Line3[j++] = Line1[i++];
  448.     }
  449.     Line3[j] = 0;
  450.     strcpy(Line2, Line3);
  451. }
  452.  
  453. /*****************************************************************************
  454. * DESCRIPTION:                                                               *
  455. * Find and replace all occurances of the string in GlblStrSubstStruct.       *
  456. *                                                                            *
  457. * PARAMETERS:                                                                *
  458. *   Line:     Line to global find and replace on.                            *
  459. *                                                                            *
  460. * RETURN VALUE:                                                              *
  461. *   void                                                                     *
  462. *****************************************************************************/
  463. void MacroSubstitute(char *Line)
  464. {
  465.     int i;
  466.     char *p, TmpLine[LINE_LEN_LONG];
  467.  
  468.     for (i = 0; GlblStrSubstStruct[i].FindString != NULL; i++) {
  469.     while ((p = strstr(Line, GlblStrSubstStruct[i].FindString)) != NULL) {
  470.         strcpy(TmpLine, p + strlen(GlblStrSubstStruct[i].FindString));
  471.         strcpy(p, GlblStrSubstStruct[i].ReplaceString);
  472.         strcat(p, TmpLine);
  473.     }
  474.     }
  475. }
  476.