home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / clarion / brfcla2.zip / CCOMPILE.M < prev    next >
Text File  |  1989-06-06  |  14KB  |  407 lines

  1. ;;*
  2. ;;*        BRIEF -- Basic Reconfigurable Interactive Editing Facility
  3. ;;*
  4. ;;*        Written by Dave Nanian and Michael Strickman.
  5. ;;*
  6. ;;*
  7. ;;*        ccompile.m:
  8. ;;*
  9. ;;*        This file contains replacement macros for the standard BRIEF macros 
  10. ;;*      cc and compile_it  for compiling    files.
  11. ;;*
  12. ;;*        Revision history:
  13. ;;*        -----------------
  14. ;;*      1-June-1989     Kevin Baradet 
  15. ;;*                      Macros (Compile_it) and (CC) modified to provide
  16. ;;*                      support for CLARION compiler CCMP. To locate the
  17. ;;*                      changes search for the string ";;*" exclusive of
  18. ;;*                      the quotation marks.
  19. ;;*      Installation:
  20. ;;*      -------------
  21. ;;*      1) Add the following to your autoexec.bat: 
  22. ;;*            SET BCCLA=!"ccmp %%s.cla NO B"
  23. ;;*
  24. ;;*      2) Add the file CLAINITL.INC to your initials macro file to provide
  25. ;;*         next_error, previous_error and error_info support for CLARION
  26. ;;*         files along with the autoload function for the replacement
  27. ;;*         macros (cc) and (compile_it). To do this add the following at the
  28. ;;*         end of your initials macro source file in column 1:
  29. ;;*            #include "clainitl.inc"
  30. ;;*         Add the following to your initials macro before the (return): 
  31. ;;*            (autoload "ccompile" "compile_it" "cc")
  32. ;;*         Note: the .inc file should be in the same directory as the 
  33. ;;*               initials macro source code (???.m file).
  34. ;;*          SEE sample initials macro file ABC.M for more info.
  35. ;;*
  36. ;;*
  37. ;;*      3) Recompile your initials macro
  38. ;;*
  39. ;;*      4) Compile the file CCOMPILE.M
  40. ;;*     
  41. ;;*      5) Add the following line to your SEARCH.M file at the end:
  42. ;;*         #include "clasrch.inc"
  43. ;;*         SEE clasrch.inc for more info
  44. ;;*      
  45. ;;*      6) Recompile SEARCH.M
  46. ;;*
  47. ;;*      7) Note: This works with BRIEF v2.1 may work w/ v2.0 also
  48. ;;*
  49. ;;*      8) For further information on how to use BRIEF programming
  50. ;;*         support, read the chapter in the BRIEF Manual "Using BRIEF
  51. ;;*         Programming Support". See also source for ERRORFIX.M
  52. ;;*
  53. ;;*      9) ^N will advance you to the next error, ^P to previous errors,
  54. ;;*          Alt-F10 to compile the file in the current buffer. File must
  55. ;;*          have an extension of CLA for all of this to work.
  56. ;;*
  57. ;;*     10)  If you are using BRIEF v2.0 change the string "replacement" to
  58. ;;*          the string "macro" in the (cc) and (compile_it) macros below.
  59. ;;*
  60. ;;*     11)  The next time you load BRIEF, the CLARION compiler support
  61. ;;*          will be in effect.
  62.  
  63. (extern    add_to_path
  64.             next_error
  65.             _check_warnings
  66.          cm
  67. )
  68. ;*** <f>
  69. ;**
  70. ;**        compile_it:
  71. ;**
  72. ;**        This function automatically compiles the file in the current
  73. ;**    buffer.  It uses the "BC<extension>" environment variable to
  74. ;**    determine what to do with any given file.  If no "BC<extension>"
  75. ;**    environment variable exists for the specific file extension being
  76. ;**    compiled, compile_it checks to see if it's ".c", ".m" or ".asm".  If
  77. ;**    it's a macro file the "cm" macro is executed; if it's a C file, a
  78. ;**    generic "cc" command is used, and if it's an ASM file, the Macro
  79. ;**    Assembler is invoked.
  80. ;**
  81. ;**        Other compilers can be supported very easily.  Simply set a
  82. ;**    "BC<extension>" environment variable to the "pass string" you
  83. ;**    want to use.  For example, if you wanted to call the (fictitious)
  84. ;**    UnderWare C compiler, which has two passes called "under" and "ware",
  85. ;**    you'd use the command:
  86. ;**
  87. ;**            set bcc="under %s;ware %s"
  88. ;**
  89. ;**        You must place the pass string in quotes.  If you don't, compile_it
  90. ;**    calls a macro named whatever is in the pass string.  So, for example,
  91. ;**    if you accidently set your pass string using the command:
  92. ;**
  93. ;**            set bcc=under %s;ware %s
  94. ;**
  95. ;**    compile_it would call a macro named "under %s;ware %s".  This probably
  96. ;**    isn't what you want -- but this feature can be useful if you want to
  97. ;**    run some sort of custom macro for the file extension (compile_it does
  98. ;**    this when compiling cm files).
  99. ;**
  100. ;**        Also note that DOS requires you to double any % characters that
  101. ;**    appear in a batch file.  So if you were setting the BCC variable in
  102. ;**    your autoexec, you would use the line:
  103. ;**
  104. ;**            set bcc="under %%s;ware %%s"
  105. ;**
  106. ;**        Each pass begins with the name of the executable program that does
  107. ;**    that compilation pass.  That is followed by the a space, the special
  108. ;**    string "%s", which is replaced by the filename (with NO extension),
  109. ;**    and the multiple pass separation character ";".  If you want to put
  110. ;**    a ";" in your pass string, use "\;".
  111. ;**
  112. ;**        These special characters are very important -- don't forget them!
  113. ;**    Remember that the special "%s" string is only replaced by the filename,
  114. ;**    not the filename and the extension.  Up to two of these can be specified
  115. ;**    in any given pass.  If your compiler requires the extension as well,
  116. ;**    place it after this string (e.g. "cc -c %s.c").
  117. ;**
  118. ;**        If you want to pass options to your compiler, you can place them
  119. ;**    either before or after the "%s".  Placing them before puts the option
  120. ;**    before the filename, and vice versa.
  121. ;**
  122. ;**        If you compiler doesn't return an error code, put an exclamation
  123. ;**    point in front of the first pass string (either inside or outside the
  124. ;**    quotes); this will override the current warnings_only setting and
  125. ;**    automatically check for errors in the compiler output.
  126. ;**
  127. ;;*
  128. ;;*  Revision History
  129. ;;*  ================
  130. ;;*  1-June-1989       Kevin Baradet    Clarion Support added
  131.  
  132.  
  133.  
  134. (replacement compile_it
  135.     (
  136.         (string    extension
  137.                     command
  138.         )
  139.  
  140.       ;;* add the following line
  141.       (global extension)
  142.  
  143.         (inq_names NULL extension)
  144.         (= command (trim (ltrim (inq_environment (+ "BC" (upper extension))))))
  145.  
  146.         (if (!= command "")
  147.             (
  148.                 (if (index command "\"")
  149.                     (
  150.                         (int    loc)
  151.  
  152.                         (while (= loc (index command "\""))
  153.                             (= command (+ (substr command 1 (- loc 1)) (substr command (+ loc 1))))
  154.                         )
  155.                         (if (== "!" (substr command 1 1))
  156.                             (cc (substr command 2) extension 1)
  157.                         ;else
  158.                             (cc command extension)
  159.                         )
  160.                     )
  161.                 ;else
  162.                     (if (== "!" (substr command 1 1))
  163.                         (execute_macro (substr command 2) 1)
  164.                     ;else
  165.                         (execute_macro command _check_warnings)
  166.                     )
  167.                 )
  168.             )
  169.         ;else
  170.             (
  171.                 (if (== extension "m")
  172.                     (cm)
  173.                 ;else
  174.                     (if (== extension "asm")
  175.                         (cc "masm %s\\;" extension)
  176.                     ;else
  177.                         (if (== extension "c")
  178.                             (cc "cc -c %s.c" extension)
  179.                         ;else
  180.                             (error "Can't compile: no BC%s environment variable." (upper extension))
  181.                         )
  182.                     )
  183.                 )
  184.             )
  185.         )
  186.     )
  187. )
  188.  
  189. ;**
  190. ;**        cc:
  191. ;**
  192. ;**        This routine compiles the file in the current buffer using the
  193. ;**    passed "pass string" and the BRIEF DOS command.  It needs a lot
  194. ;**    of memory to run (you should have at least 256K and start with -M20)
  195. ;**    so be careful!
  196. ;**
  197. ;**        The "pass string" passed should be of the form:
  198. ;**
  199. ;**            pass_1 %s;pass_2 %s;...pass_n %s
  200. ;**
  201. ;**        The optional second parameter is an extended file type -- this is
  202. ;**    used by the "cm" macro, and to compile other types of files (e.g.
  203. ;**    .asm).
  204. ;**
  205. ;**        If no pass string is specified, it defaults to a generic "cc"
  206. ;**    command.  If no extension is specified, it defaults to "c".
  207. ;**
  208. ;;*
  209. ;;*  Revision History
  210. ;;*  ================
  211. ;;*  1-June-1989       Kevin Baradet    Clarion Support added
  212.  
  213. (replacement cc
  214.     (
  215.         (string        file_name            ;** The name of the file we're compiling.
  216.                         command_line        ;** The compile command line.
  217.                         path                    ;** The path of the file we're compiling.
  218.                         old_path                ;** The original path we were on.
  219.                         passes                ;** The names of the passes.
  220.                         error_file            ;** The file to put error information in.
  221.         )
  222.         (int            loc                    ;** Generic index place holder.
  223.                         ret_code                ;** Return code from DOS.
  224.                         buffer_id            ;** Buffer ID of error buffer.
  225.                         check_warnings        ;** Examine resule of compile for errors?
  226.         )
  227.         ;**
  228.         ;**        We get the name of the file from inq_names (the extension is
  229.         ;**    put in the command_line variable so we don't have to declare too
  230.         ;**    many strings) and check to see if it is a C file.
  231.         ;**
  232.  
  233.         (if (! (get_parm 1 passes))
  234.             (= passes "c")
  235.         )
  236.         (if (! (get_parm 2 check_warnings))
  237.             (= check_warnings _check_warnings)
  238.         )
  239.         (inq_names path command_line file_name)
  240.         (= ret_code 1)
  241.  
  242.         (if (== command_line passes)
  243.             (
  244.             ;**
  245.             ;**        If the file has been modified, we want to make sure the
  246.             ;**    current version gets compiled, so we write it to disk.
  247.             ;**
  248.             ;**        Note that if the user does not specify a pass string, it
  249.             ;**    defaults to a generic "cc" command, which works with the
  250.             ;**    Wizard C compiler (and a few others).
  251.             ;**
  252.  
  253.                 (if (! (get_parm 0 passes))
  254.                     (= passes "cc -c %s.c")
  255.                 )
  256.                 (if (inq_modified)
  257.                     (
  258.                         (int            old_msg_level)
  259.  
  260.                         (= old_msg_level (inq_msg_level))
  261.                         (set_msg_level 0)
  262.                         (= ret_code (write_buffer))
  263.                         (set_msg_level old_msg_level)
  264.                     )
  265.                 )
  266.                 (if (>= ret_code 0)
  267.                     (
  268.                         ;**
  269.                         ;**        Now we parse the filename off the path string,
  270.                         ;**    making sure to handle the possible presence of forward
  271.                         ;**    and backward slash characters.  We then replace the
  272.                         ;**    file_name's ".c" with ".err" for redirection purposes.
  273.                         ;**
  274.         
  275.                         (= path (substr path 1 (rindex path (substr path 3 1))))
  276.  
  277.                         (if (> (strlen path) 3)
  278.                             (= path (substr path 1 (- (strlen path) 1)))
  279.                         )
  280.                         (= file_name (substr file_name 1 (- (index file_name ".") 1)))
  281.                         (= error_file (+ file_name ".err"))
  282.                         (= ret_code 0)
  283.         
  284.                         ;**
  285.                         ;**        We want the .obj file to end up in the file's
  286.                         ;**    directory, so we change to the directory where the
  287.                         ;**    file is, saving the current directory.  We also make
  288.                         ;**    the file's drive the default drive.
  289.                         ;**
  290.         
  291.                         (getwd "" command_line)
  292.                         (getwd path old_path)
  293.                         (= old_path (+ (substr command_line 1 1) (substr old_path 2)))
  294.                         (cd path)
  295.                         (cd (substr path 1 2))
  296.         
  297.                         ;**
  298.                         ;**        If there is already a buffer for the error file, we
  299.                         ;**    "create" it (create_buffer returns the ID of a buffer
  300.                         ;**    that already existed) and then delete it immediately.
  301.                         ;**    Note that under some very obscure circumstances, the
  302.                         ;**    create_buffer call could fail.  If it does, it'll
  303.                         ;**    return 0, which is an invalid buffer id.  We check for
  304.                         ;**    this case since delete_buffer does not.
  305.                         ;**
  306.  
  307.                         (if (= buffer_id (create_buffer "C Errors" error_file 1))
  308.                             (delete_buffer buffer_id)
  309.                         )
  310.  
  311.                         ;**
  312.                         ;**        This loop goes through each pass of the compiler,
  313.                         ;**    checks to see if the return code was OK, and, if so,
  314.                         ;**    continues along.  If an error occurs, the loop exits
  315.                         ;**    immediately.
  316.                         ;**
  317.         
  318.                         (while (&& (! ret_code) (strlen passes))
  319.                             (
  320.                                 (if (= loc (search_string "[~\\\\]\\c;" passes))
  321.                                     (
  322.                                         (= command_line (substr passes 1 (- loc 1)))
  323.                                         (= passes (substr passes (+ loc 1)))
  324.                                     )
  325.                                 ;else
  326.                                     (
  327.                                         (= command_line passes)
  328.                                         (= passes "")
  329.                                     )
  330.                                 )
  331.  
  332.                         ;;* Original line is below
  333.                                 ;;* (+= command_line (+ " >&" error_file))
  334.  
  335.                         ;;**** Change Begins ****
  336.                         ;;* If we're not compiling a Clarion .CLA
  337.                         ;;*  source code file, we want to redirect
  338.                         ;;*  the standard error output to a .ERR file.
  339.                         ;;* The Clarion compiler will do the redirection
  340.                         ;;*  automatically for us if there are compile
  341.                         ;;*  time errors
  342.  
  343.                         (if (!= (upper extension) "CLA")
  344.                            (
  345.                                      (+= command_line (+ " >&" error_file))
  346.                            )
  347.                         )
  348.                         ;;* Change ends
  349.  
  350.                                 (while (= loc (search_string "\\\\;" command_line))
  351.                                     (= command_line (+ (substr command_line 1 (- loc 1)) (substr command_line (+ loc 1))))
  352.                                 )
  353.                                 (sprintf command_line command_line file_name file_name file_name file_name file_name file_name file_name)
  354.                                 (message command_line)
  355.                                 (= ret_code (dos command_line 0))
  356.                             )
  357.                         )
  358.  
  359.                   ;;* Since the Clarion Compiler doesn't return an errorcode,
  360.                   ;;* we need to check to see if an error file was created by
  361.                   ;;* the compiler. If the error file exists, then there were 
  362.                   ;;* compile time errors so we must manually set the 
  363.                   ;;* returncode to a non-zero value.
  364.  
  365.                         (if (== (upper extension) "CLA")
  366.                      (
  367.                         (if (exist error_file) (= ret_code 1) (= ret_code 0))
  368.                      )
  369.                   )
  370.                   ;;* Change Ends
  371.  
  372.                         ;**
  373.                         ;**        Finally, we restore the old directory.  If the
  374.                         ;**    compilation did not succeed, the next_error macro
  375.                         ;**    is called to place the cursor under the error.
  376.                         ;**
  377.                         ;**        Otherwise, the temporary file is deleted and a
  378.                         ;**    message is printed.
  379.                         ;**
  380.         
  381.                         (if (> ret_code 0)
  382.                             (next_error)
  383.                         ;else
  384.                             (
  385.                                 (if (|| (< ret_code 0) (|| (! check_warnings) (! (next_error 2))))
  386.                                     (
  387.                                         (del error_file)
  388.  
  389.                                         (if (== ret_code 0)
  390.                                             (message "Compilation successful.")
  391.                                         )
  392.                                     )
  393.                                 )
  394.                             )
  395.                         )
  396.                         (cd (substr old_path 3))
  397.                         (cd (substr old_path 1 2))
  398.                     )
  399.                 )
  400.             )
  401.         ;else
  402.             (error "Current buffer is not a .%s file." passes)
  403.         )
  404.         (returns ret_code)
  405.     )
  406. )
  407.