home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / biblioteki / c_library / easylibs / source / xtract.c < prev   
C/C++ Source or Header  |  1994-07-09  |  6KB  |  301 lines

  1. /*
  2.     This program can extract autodocs, prototypes and FD files from a
  3.     series of source files.
  4.  
  5.     When generating autodocs, it looks for lines beginning with the word
  6.     "AUTODOCS". The rest of the line will be used as the name of a new
  7.     autodocs section. The following lines are extracted from the source
  8.     and written to stdout until a line is found that terminates a C comment
  9.     at the beginning of the line.
  10.  
  11.     For generating prototypes this will look for lines beginning with the
  12.     word "Prototype". (Hey, Dice owners, you know? :-) The line is extracted
  13.     and written to stdout by replacing the word "Prototype" with the word
  14.     "extern". (This allows to create prototypes of variables.)
  15.  
  16.     Generating FD files works quite the same, except that the word
  17.     "FDPrototype" is searched and nothing is replaced.
  18.  
  19.     This is public domain, use it as you want, but WITHOUT ANY WARRANTY!
  20.  
  21.     Computer:    Amiga 1200            Compiler: Any ANSI compiler
  22.  
  23.     Author:    Jochen Wiedmann
  24.         Am Eisteich 9
  25.         72555 Metzingen (Germany)
  26.         Phone: 07123 / 14881
  27.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  28.  
  29.     V1.1,   31.03.94
  30. */
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. #ifndef FALSE
  37. #define FALSE 0
  38. #endif
  39. #ifndef TRUE
  40. #define TRUE (!FALSE)
  41. #endif
  42.  
  43.  
  44. char VERSION[] = "$VER: Xtract 1.1 (31.03.94)    by Jochen Wiedmann";
  45.  
  46.  
  47.  
  48.  
  49.  
  50. void Usage(void)
  51.  
  52. {
  53.   fprintf(stderr, "Usage: Xtract files/M,FD/S,PROTOS/S\n\n");
  54.   fprintf(stderr, "Extracts autodocs, FD files or prototype files ");
  55.   fprintf(stderr, "from the given\n");
  56.   fprintf(stderr, "source files.\n\n%s\n", VERSION+6);
  57. }
  58.  
  59.  
  60.  
  61.  
  62. /*
  63. AUTODOCS Xtract/ScanFiles()
  64.  
  65.     NAME
  66.     ScanFiles
  67.  
  68.     SYNOPSIS
  69.     Success = ScanFiles(argc, argv, autodocs, fd, protos)
  70.  
  71.     int ScanFiles(int, char **, int, int, int);
  72.  
  73.     FUNCTION
  74.     Called from main to visit each file in the list given
  75.     by argc and argv. Extracts the wished portions from
  76.     the source and writes them to stdout.
  77.  
  78.     INPUTS
  79.     argc, argv  - the arguments of main()
  80.     autodocs    - 1 or 2 (depending on the pass number) when
  81.               creating autodocs, 0 otherwise
  82.     fd        - 1 when creating FD files, 0 otherwise
  83.     protos        - 1 when creating prototype files, 0 otherwise
  84.  
  85.     RESULT
  86.     TRUE, if successful, FALSE otherwise
  87.  
  88.     NOTE
  89.     Be sure, that only one of autodocs, fd and protos is
  90.     nonzero!
  91.  
  92.     SEE ALSO
  93.     main()
  94.  
  95. */
  96. /*
  97. Prototype int ScanFiles(int, char **, int, int, int);
  98. */
  99. int ScanFiles(int argc, char *argv[], int autodocs, int fd, int protos)
  100.  
  101. { int i;
  102.  
  103.   if (fd)
  104.   { if (fputs("*\n*\tMACHINE GENERATED\t\t\n*\n", stdout) < 0)
  105.     { return(FALSE);
  106.     }
  107.   }
  108.   else if (protos)
  109.   { if (fputs("\n/*\tMACHINE GENERATED\t\t*/\n\n", stdout) < 0)
  110.     { return(FALSE);
  111.     }
  112.   }
  113.   else if (autodocs == 1)
  114.   { if (fputs("TABLE OF CONTENTS\n\n", stdout) < 0)
  115.     { return(FALSE);
  116.     }
  117.   }
  118.  
  119.   for (i = 1;  i < argc;  i++)
  120.   { FILE *fp;
  121.     int write_autodocs = FALSE;
  122.     char line[2048];
  123.     int linenum;
  124.  
  125.     if (stricmp(argv[i], "fd") == 0  ||
  126.     stricmp(argv[i], "protos") == 0)
  127.     { continue;
  128.     }
  129.  
  130.     if (!(fp = fopen(argv[i], "r")))
  131.     { fprintf(stderr, "Cannot open %s for output.\n");
  132.       exit(10);
  133.     }
  134.  
  135.     if (protos)
  136.     { if (printf("\n/*\t%s\t\t*/\n\n", argv[i])  <  0)
  137.       { return(FALSE);
  138.       }
  139.     }
  140.  
  141.     linenum = 0;
  142.     while (fgets(line, sizeof(line), fp))
  143.     { int len;
  144.  
  145.       ++linenum;
  146.       len = strlen(line);
  147.       if (line[len-1] != '\n')
  148.       { fprintf(stderr, "Error: Line %d too long.\n", linenum);
  149.       }
  150.  
  151.       if (protos)
  152.       { if (strncmp("Prototype", line, 9) == 0)
  153.     { if (printf("extern %s", line+9) < 0)
  154.       { return(FALSE);
  155.       }
  156.     }
  157.       }
  158.       else if (fd)
  159.       { if (strncmp("FDPrototype", line, 11) == 0)
  160.     { char *ptr;
  161.  
  162.       ptr = line+11;
  163.       while (*ptr == ' '  ||  *ptr == '\t')
  164.       { ptr++;
  165.       }
  166.  
  167.       if (printf("%s", ptr) < 0)
  168.       { return(FALSE);
  169.       }
  170.     }
  171.       }
  172.       else
  173.       { if (strncmp("AUTODOCS", line, 8) == 0)
  174.     { char *ptr;
  175.  
  176.       ptr = line+8;
  177.       while (*ptr == ' '  ||  *ptr == '\t')
  178.       { ptr++;
  179.       }
  180.  
  181.       write_autodocs = TRUE;
  182.       if (autodocs == 1)
  183.       { if (printf("%s", ptr) < 0)
  184.         { return(FALSE);
  185.         }
  186.       }
  187.       else
  188.       { if (printf("\014%s", ptr) < 0)
  189.         { return(FALSE);
  190.         }
  191.       }
  192.     }
  193.     else if (line[0] == '*'  &&  line[1] == '/')
  194.     { write_autodocs = FALSE;
  195.     }
  196.     else if (autodocs == 2  &&  write_autodocs)
  197.     { if (printf("%s", line) < 0)
  198.       { return(FALSE);
  199.       }
  200.     }
  201.       }
  202.     }
  203.  
  204.     fclose(fp);
  205.   }
  206.  
  207.   if (fd)
  208.   { if (printf("##end\n") < 0)
  209.     { return(FALSE);
  210.     }
  211.   }
  212.  
  213.   return(TRUE);
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220. /*
  221. AUTODOCS Xtract/main()
  222.  
  223.     NAME
  224.     main    --  we all know what it is.
  225.  
  226.     SYNOPSIS
  227.     main(argc, argv)
  228.  
  229.     void main(int, char **);
  230.  
  231.     FUNCTION
  232.     Called by the startup code; checks the arguments and calls
  233.     ScanFiles() for the first pass. A second pass is needed
  234.     when generating autodocs, hence will call ScanFiles() again
  235.     in that case.
  236.  
  237.     INPUTS
  238.     argc    - number of arguments (including the programs name),
  239.           0, when called from the workbench. This should
  240.           *never* happen.
  241.     argv
  242.  
  243.     RESULT
  244.     what do you expect from a void function?
  245.  
  246.     SEE ALSO
  247.     ScanFiles()
  248. */
  249. /*
  250. Prototype void main(int, char **);
  251. FDPrototype *
  252. FDPrototype * Sorry, guys, but this is no shared library.
  253. FDPrototype *
  254. FDPrototype * But you could expect that the FD prototypes would
  255. FDPrototype * follow here.
  256. */
  257. void main(int argc, char *argv[])
  258.  
  259. { int i;
  260.   int autodocs = TRUE;
  261.   int protos = FALSE;
  262.   int fd = FALSE;
  263.  
  264.   if (argc == 0)    /*  Prevent start from workbench.   */
  265.   { exit(-1);
  266.   }
  267.  
  268.   if (argc == 1  ||
  269.       strcmp(argv[1], "?") == 0  ||
  270.       strcmp(argv[1], "-h") == 0)
  271.   { Usage();
  272.     exit(5);
  273.   }
  274.  
  275.   /*
  276.       Lets see, if we should generate autodocs, prototypes or FD files.
  277.   */
  278.   for (i = 1;  i < argc;  ++i)
  279.   { if (stricmp(argv[i], "fd") == 0)
  280.     { fd = TRUE;
  281.       autodocs = FALSE;
  282.     }
  283.     else if (stricmp(argv[i], "protos") == 0)
  284.     { protos = TRUE;
  285.       autodocs = FALSE;
  286.     }
  287.   }
  288.   if (fd  &&  protos)
  289.   { fprintf(stderr, "Can not generate both FD and prototype files.\n");
  290.     exit(10);
  291.   }
  292.  
  293.  
  294.   if (!ScanFiles(argc, argv, autodocs, fd, protos)  ||
  295.       (autodocs  &&  !ScanFiles(argc, argv, 2, fd, protos)))
  296.   { fprintf(stderr, "Error while writing.\n");
  297.     exit(10);
  298.   }
  299.   exit(0);
  300. }
  301.