home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / xlispplu / sources / putpatch.c < prev    next >
C/C++ Source or Header  |  1992-02-03  |  9KB  |  205 lines

  1. /************************************************************************/
  2. /*              putpatch.c              */
  3. /*                                                                      */
  4. /* Public-domain code by Jeff Prothero (jsp@glia.biostr.washington.edu) */
  5. /*                                                                      */
  6. /* This program expands special comments in the xlisp-plus source code  */
  7. /* into #include sequences.  It is useful when using xlisp-plus as an   */
  8. /* application language, the includes allow a complete application to   */
  9. /* be built around xlisp-plus without hacking the regular xlisp-plus    */
  10. /* source files.  Aside from simple cleanliness, the big advantage of   */
  11. /* this is that you will be able to effortlessly plug new releases      */
  12. /* of xlisp-plus into your application as they become available.        */
  13. /*                                                                      */
  14. /* This program looks for comments in the source of the form            */
  15. /*                                                                      */
  16. /*     /* $putpatch.c$: "MODULE_XLISP_C_WRAPUP" */
  17. /*                                                                      */
  18. /* and expands them into text looking like:                             */
  19. /*                                                                      */
  20. /*     #define MODULE_XLISP_C_WRAPUP                                    */
  21. /*     #include "../../xmodules.h"                                      */
  22. /*     #undef MODULE_XLISP_C_WRAPUP                                     */
  23. /*                                                                      */
  24. /* where the #included file is UNIVERSAL_INCLUDE, below.                */
  25. /*                                                                      */
  26. /* You should be able to compile with simply:                           */
  27. /*     cc putpatch.c -o putpatch                                        */
  28. /*                                                                      */
  29. /* Normal use is                                                        */
  30. /*     putpatch *.h *.c                                                 */
  31. /* in the main xlisp-plus source directory.                             */
  32. /*                                                                      */
  33. /* Docs and examples of using xlisp-plus as an application engine       */
  34. /* are available, but are not part of the standard xlisp-plus           */
  35. /* distribution.  Contact jsp@glia.biostr.washington.edu.               */
  36. /************************************************************************/
  37.  
  38. #define UNIVERSAL_INCLUDE "../../xmodules.h"
  39. #define MAX_UNIVERSAL_INCLUDE (256)
  40. char universal_include[ MAX_UNIVERSAL_INCLUDE ];
  41.  
  42. /************************************************************************/
  43. /*              contents                */
  44. /*. do_prehacks                                                     */
  45. /*. do_posthacks                                                    */
  46. /*. patch_a_file                                                    */
  47. /*. usage                                                           */
  48. /*. main                                                            */
  49. /*                                                                      */
  50. /*      (generated via "grep '/\*\.' putpatch.c")       */
  51. /************************************************************************/
  52.  
  53. /************************************************************************/
  54. /*              history                 */
  55. /*                                                                      */
  56. /* 92Jan27 CrT:  Created.  Operational.                                 */
  57. /************************************************************************/
  58.  
  59. #include <unistd.h>
  60. #include <stdio.h>
  61. #include <string.h>
  62.  
  63. #define PATCH_COOKIE " /* $putpatch.c$: \"%[^\"]\" */"
  64.  
  65. /* Following line is just so we can selftest */
  66. /* $putpatch.c$: "MODULE_PUTPATCH_C_GLOBALS" */
  67.  
  68. /************************************************************************/
  69. /*. do_prehacks                                                     */
  70. /************************************************************************/
  71. do_prehacks( fout, patch_name )
  72. FILE*        fout;
  73. char*          patch_name;
  74. {
  75.     /*************************************************************/
  76.     /* A couple of patchpoints need to define a special macro.   */
  77.     /* Haven't thought of a nicer mechanism, so it's hacked into */
  78.     /* putpatch.c here.  Yeah, kinda ugly.           */
  79.     /*************************************************************/
  80.     if (!strcmp( patch_name, "MODULE_XLFTAB_C_FUNTAB_S")) {
  81.     fprintf( fout, "#define DEFINE_SUBR(a,b) {a, S, b},\n" );
  82.     }
  83.     if (!strcmp( patch_name, "MODULE_XLFTAB_C_FUNTAB_F")) {
  84.     fprintf( fout, "#define DEFINE_FSUBR(a,b) {a, F, b},\n" );
  85.     }
  86. }
  87.  
  88. /************************************************************************/
  89. /*. do_posthacks                                                    */
  90. /************************************************************************/
  91. do_posthacks( fout, patch_name )
  92. FILE*         fout;
  93. char*           patch_name;
  94. {
  95.     if (!strcmp( patch_name, "MODULE_XLFTAB_C_FUNTAB_S")) {
  96.     fprintf( fout, "#undef DEFINE_SUBR\n" );
  97.     }
  98.     if (!strcmp( patch_name, "MODULE_XLFTAB_C_FUNTAB_F")) {
  99.     fprintf( fout, "#undef DEFINE_FSUBR\n" );
  100.     }
  101. }
  102.  
  103. /************************************************************************/
  104. /*. patch_a_file                                                    */
  105. /************************************************************************/
  106. #define MAX_PATCH_BUF (10000)
  107. patch_a_file( filename )
  108. char*         filename; 
  109. {
  110.     FILE* fout;
  111.     char buf[        MAX_PATCH_BUF ];
  112.     char patch_name[ MAX_PATCH_BUF ];
  113.     int  patches_done = 0;
  114.  
  115.     /* Open the file for input: */
  116.     FILE* f_in = fopen( filename, "r" );
  117.     if (f_in == NULL) {printf("Could not read '%s'\n",filename);exit(1);}
  118.  
  119.     /* Open a file "filename.N" for output: */
  120.     strcpy( buf, filename );
  121.     strcat( buf, ".N" );
  122.     if (fopen(buf,"r") != NULL) {printf("'%s' already exists!",buf);exit(1);}
  123.     fout = fopen( buf, "w" );
  124.     if (fout == NULL) {printf("Could not create '%s'\n",buf);exit(1);}
  125.  
  126.     /* Over all lines in input file: */
  127.     while (fgets( buf, MAX_PATCH_BUF, f_in ) != NULL) {
  128.  
  129.     /* Check for $putpatch.c$: */
  130.     if (sscanf( buf, PATCH_COOKIE, patch_name ) != 1) {
  131.  
  132.         /* No match, just copy line to output: */
  133.         fputs( buf, fout );
  134.  
  135.     } else {
  136.  
  137.         /* Found a patchpoint, replace comment by real patch: */
  138.         fprintf( fout, "#define %s\n", patch_name );
  139.         do_prehacks(  fout, patch_name );
  140.         fprintf( fout, "#include %s\n", UNIVERSAL_INCLUDE );
  141.         do_posthacks( fout, patch_name );
  142.         fprintf( fout, "#undef %s\n", patch_name );
  143.             ++patches_done;
  144.     }   
  145.     }
  146.     fclose( f_in );
  147.     fclose( fout );
  148.  
  149.     /* If no patches made, no point in keeping output file: */
  150.     strcpy( buf, filename );
  151.     strcat( buf, ".N" );
  152.     if (!patches_done) {
  153.         unlink( buf );
  154.     } else {
  155.         printf(
  156.         "%d patches for %s, result in %s\n",
  157.         patches_done,
  158.         filename,
  159.         buf
  160.     );
  161.     }
  162.     return patches_done != 0;
  163. }
  164.  
  165. /************************************************************************/
  166. /*. usage                                                           */
  167. /************************************************************************/
  168. usage() {
  169.     printf("usage: putpatch [-u myinclude.h] filename ...\n");
  170.     exit(1);
  171. }
  172.  
  173. /************************************************************************/
  174. /*. main                                                            */
  175. /************************************************************************/
  176. main( argc, argv )
  177. int   argc;
  178. char**      argv;
  179. {
  180.     int i;
  181.     int count = 0;
  182.  
  183.     /* Initialize stuff: */
  184.     strcpy( universal_include, UNIVERSAL_INCLUDE );
  185.  
  186.     for (i = 1;   i < argc;   ++i) {
  187.     char* t = argv[i];
  188.         if (*t == '-') {
  189.         switch (*++t) {
  190.         case 'u':
  191.         strcpy( universal_include, argv[ ++i ] );
  192.         break;
  193.         default:
  194.         usage();
  195.         }
  196.     } else {
  197.             count += patch_a_file( t );
  198.     }
  199.     }
  200.  
  201.     printf( "%d files patched.\n", count );
  202.     exit(0);
  203. }
  204.  
  205.