home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / lib / glut / cannotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.6 KB  |  357 lines

  1. #include <stdlib.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8.  
  9. #define TEX    1         /* generate annotated TeX forC code */
  10. #define CCODE    2         /* generate shipping C code */
  11. #define PCODE    3         /* generate publishable C code */
  12.  
  13. #define T_LINE    0
  14. #define T_TEX    1
  15. #define T_UNTEX    2
  16. #define T_CODE    3
  17. #define T_DEAD    4
  18.  
  19. FILE *ifile;
  20. FILE *ofile;
  21. char buffer[1024];
  22. int mode = TEX;
  23. int verbose = 1;
  24. int tex, code;
  25. int line;
  26. int new_line_pending;
  27. int indent = 0;
  28. int just_entered_code;
  29.  
  30. int
  31. line_type(char *buf)
  32. {
  33.   if (!strcmp("//tex\n", buf)) {
  34.     return T_TEX;
  35.   } else if (!strcmp("//untex\n", buf)) {
  36.     return T_UNTEX;
  37.   } else if (!strcmp("//code\n", buf)) {
  38.     return T_CODE;
  39.   } else if (!strcmp("//dead\n", buf)) {
  40.     return T_DEAD;
  41.   } else {
  42.     return T_LINE;
  43.   }
  44. }
  45.  
  46. int
  47. non_code(char *buf)
  48. {
  49.   if (!strcmp("#include <assert.h>\n", buf)) {
  50.     return 1;
  51.   }
  52.   if (strstr(buf, "assert(")) {
  53.     return 1;
  54.   }
  55.   if (strstr(buf, "ENDCENTRY")) {
  56.     return 1;
  57.   }
  58.   if (strstr(buf, "CENTRY")) {
  59.     return 1;
  60.   }
  61.   if (strstr(buf, "INDENT-ON")) {
  62.     return 1;
  63.   }
  64.   if (strstr(buf, "INDENT-OFF")) {
  65.     return 1;
  66.   }
  67.   return 0;
  68. }
  69.  
  70. int
  71. blank_line(char *buf)
  72. {
  73.   for(;*buf; buf++) {
  74.     if(!isspace(*buf)) {
  75.       return 0;
  76.     }
  77.   }
  78.   return 1;
  79. }
  80.  
  81. int
  82. main(int argc, char **argv)
  83. {
  84.   char *iname, *suffix, *tail, *oname, obuf[512];
  85.   char *gotsome;
  86.   int type;
  87.   int output, output_this;
  88.   mode_t savemask;
  89.   int i;
  90.  
  91.   for (i = 1; i < argc; i++) {
  92.     if (argv[i][0] == '-') {
  93.       switch (argv[i][1]) {
  94.       case 'i':
  95.     indent = atoi(&argv[i][2]);
  96.     break;
  97.       case 't':
  98.         mode = TEX;
  99.         break;
  100.       case 'c':
  101.         mode = CCODE;
  102.         break;
  103.       case 'd':
  104.         mode = PCODE;
  105.         break;
  106.       case 'v':
  107.         verbose = 1;
  108.         break;
  109.       default:
  110.         fprintf(stderr, "can: unrecognized option: %s\n", argv[i]);
  111.         break;
  112.       }
  113.     } else {
  114.       break;            /* exit loop; done with options */
  115.     }
  116.   }
  117.   for (; i < argc; i++) {
  118.     iname = argv[i];
  119.     ifile = fopen(iname, "r");
  120.     if (ifile == NULL) {
  121.       fprintf(stderr, "can: could not open %s\n", iname);
  122.       exit(1);
  123.     }
  124.     suffix = strrchr(iname, '.');
  125.     if (suffix) {
  126.       *suffix = '\0';
  127.       suffix++;
  128.       if(*suffix == 'a') {
  129.     suffix++;
  130.       } else {
  131.     suffix = "c";
  132.       }
  133.     } else {
  134.       suffix = "c";
  135.     }
  136.     switch (mode) {
  137.     case TEX:
  138.       sprintf(obuf, "%s.tex", iname);
  139.       break;
  140.     case CCODE:
  141.       sprintf(obuf, "%s.%s", iname, suffix);
  142.       break;
  143.     case PCODE:
  144.       sprintf(obuf, "%s_pub.%s", iname, suffix);
  145.       break;
  146.     }
  147.     tail = strrchr(obuf, '/');
  148.     if(tail) {
  149.       oname = tail + 1;
  150.     } else {
  151.       oname = obuf;
  152.     }
  153.     /* Make sure the file is not writable after creation! */
  154.     savemask = umask((mode_t) 0);
  155.     umask((mode_t) savemask | S_IWUSR | S_IWGRP | S_IWOTH);
  156.     (void) unlink(oname); /* Remove existing file named oname. */
  157.     ofile = fopen(oname, "w");
  158.     umask((mode_t) savemask);
  159.     if (ofile == NULL) {
  160.       fprintf(stderr, "can: could not open %s\n", oname);
  161.       exit(1);
  162.     }
  163.     line = 0;
  164.     tex = 0;
  165.     code = 0;
  166.     output = 1;
  167.     new_line_pending = 0;
  168.     while (!feof(ifile)) {
  169.       gotsome = fgets(buffer, sizeof(buffer), ifile);
  170.       if (gotsome) {
  171.         line++;
  172.         type = line_type(buffer);
  173.         switch (mode) {
  174.         case TEX:
  175.           switch (type) {
  176.           case T_TEX:
  177.             if (tex) {
  178.               fprintf(stderr, "can: duplicate //tex seen: %d\n", line);
  179.             }
  180.             tex = 1;
  181.             output_this = 0;
  182.             output = 1;
  183.             if (code) {
  184.           new_line_pending = 0;
  185. #if 0
  186.               fprintf(ofile, "\\end{verbatim}\n}\n", buffer);
  187. #else
  188.               fprintf(ofile, "\\end{verbatim}\n", buffer);
  189. #endif
  190.             }
  191.             code = 0;
  192.             break;
  193.           case T_UNTEX:
  194.             if (!tex) {
  195.               fprintf(stderr, "can: unexpected //untex seen: %d\n", line);
  196.             }
  197.             if (code) {
  198.               fprintf(stderr, "can: unexpected //untex seen: %d\n", line);
  199.             }
  200.             tex = 0;
  201.             output_this = 0;
  202.             output = 1;
  203.             break;
  204.           case T_CODE:
  205.             if (tex) {
  206.           tex = 0;
  207.             }
  208.             output_this = 0;
  209.             output = 1;
  210.             if (!code) {
  211.           new_line_pending = 0;
  212. #if 0
  213.               fprintf(ofile, "{\\footnotesize\n\\begin{verbatim}\n", buffer);
  214. #else
  215.               fprintf(ofile, "\\begin{verbatim}\n", buffer);
  216. #endif
  217.             }
  218.             code = 1;
  219.         just_entered_code = 1;
  220.             break;
  221.           case T_DEAD:
  222.             if (tex) {
  223.           tex = 0;
  224.             }
  225.             output_this = 0;
  226.             output = 0;
  227.             break;
  228.           case T_LINE:
  229.             if (code) {
  230.           if(non_code(buffer)) {
  231.                 output_this = 0;
  232.           } else {
  233.             if(blank_line(buffer) && just_entered_code) {
  234.           output_this = 0;
  235.             } else {
  236.           output_this = 1;
  237.               just_entered_code = 0;
  238.                 }
  239.           }
  240.             } else {
  241.               output_this = 1;
  242.             }
  243.             break;
  244.           }
  245.           break;
  246.         case CCODE:
  247.           switch (type) {
  248.           case T_TEX:
  249.             if (tex) {
  250.               fprintf(stderr, "can: duplicate //tex seen: %d\n", line);
  251.             }
  252.             tex = 1;
  253.             output_this = 0;
  254.             output = 0;
  255.             break;
  256.           case T_UNTEX:
  257.             if (!tex) {
  258.               fprintf(stderr, "can: unexpected //untex seen: %d\n", line);
  259.             }
  260.             tex = 0;
  261.             output_this = 0;
  262.             output = 1;
  263.             break;
  264.           case T_CODE:
  265.             if (tex) {
  266.           tex = 0;
  267.             }
  268.             output_this = 0;
  269.             output = 1;
  270.             break;
  271.           case T_DEAD:
  272.             if (tex) {
  273.           tex = 0;
  274.             }
  275.             output_this = 0;
  276.             output = 1;
  277.             break;
  278.           case T_LINE:
  279.             output_this = 1;
  280.             break;
  281.           }
  282.           break;
  283.         case PCODE:
  284.           switch (type) {
  285.           case T_TEX:
  286.             if (tex) {
  287.               fprintf(stderr, "can: duplicate //tex seen: %d\n", line);
  288.             }
  289.             tex = 1;
  290.             output_this = 0;
  291.             output = 0;
  292.             break;
  293.           case T_UNTEX:
  294.             if (!tex) {
  295.               fprintf(stderr, "can: unexpected //untex seen: %d\n", line);
  296.             }
  297.             tex = 0;
  298.             output_this = 0;
  299.             output = 1;
  300.             break;
  301.           case T_CODE:
  302.             if (tex) {
  303.           tex = 0;
  304.             }
  305.             output_this = 0;
  306.             output = 1;
  307.             break;
  308.           case T_DEAD:
  309.             if (tex) {
  310.           tex = 0;
  311.             }
  312.             output_this = 0;
  313.             output = 0;
  314.             break;
  315.           case T_LINE:
  316.             if (code && non_code(buffer)) {
  317.               output_this = 0;
  318.             } else {
  319.               output_this = 1;
  320.             }
  321.             break;
  322.           }
  323.           break;
  324.         }
  325.         if (output && output_this) {
  326.       if(blank_line(buffer)) {
  327.         new_line_pending = 1;
  328.       } else {
  329.         if(new_line_pending) {
  330.               fprintf(ofile, "\n");
  331.           new_line_pending = 0;
  332.         }
  333.         if(indent && code) {
  334.           int s;
  335.  
  336.           for(s=0;s<indent;s++) {
  337.              fputc(' ', ofile);
  338.           }
  339.         }
  340.             fprintf(ofile, "%s", buffer);
  341.       }
  342.         }
  343.       }
  344.     }
  345.     if(code && mode == TEX) {
  346. #if 0
  347.       fprintf(ofile, "\\end{verbatim}\n}\n");
  348. #else
  349.       fprintf(ofile, "\\end{verbatim}\n");
  350. #endif
  351.     }
  352.     fclose(ofile);
  353.     fclose(ifile);
  354.   }
  355.   return 0;
  356. }
  357.