home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xwplascr.zip / XWPL0208.ZIP / tools / BuildLevel.cmd next >
OS/2 REXX Batch file  |  2000-08-13  |  16KB  |  312 lines

  1. /***********************************************************************\
  2.  *                           BuildLevel.cmd                            *
  3.  *             Copyright (C) by Stangl Roman, 1996, 1997               *
  4.  *                                                                     *
  5.  * This Code may be freely distributed, provided the Copyright isn't   *
  6.  * removed.                                                            *
  7.  *                                                                     *
  8.  * BuildLevel.cmd                                                      *
  9.  *              Batch file to replace the DESCRIPTION statement in a   *
  10.  *              module's (EXE or DLL) with one the OS/2 BLDLEVEL       *
  11.  *              command is able to read.                               *
  12.  *              Syntax: BuildLevel ModuleDefinitionFile Headerfile     *
  13.  *              e.g. BuildLevel PC2.def PC2.h                          *
  14.  *                                                                     *
  15.  * Changes by Ulrich Möller (1999) are marked with (*UM).              *
  16.  *                                                                     *
  17. \***********************************************************************/
  18.  
  19. /* static char     _VERSION_[]="BuildLevel.cmd - V2.00"; */
  20.  
  21.  
  22. Call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  23. Call SysLoadFuncs
  24.                                         /* Trap CTRL+BRK processing to avoid data corruption */
  25. Signal On HALT NAME SignalHandler
  26.  
  27.                                         /* OS/2 returncode */
  28. ReturnCode=1
  29.                                         /* 1st commandline argument (1 *.DEF file) */
  30. DefinitionFile=""
  31.                                         /* As the contents of the 1st commandline argument
  32.                                            file get changed, we make a temporary copy to read
  33.                                            from and write the new contents to the original file */
  34. DefinitionCopy="$Temp$"
  35.                                         /* 2nd commandline argument (1 *.H* file) */
  36. HeaderFile=""
  37.                                         /* Name of the binary module which gets filled from
  38.                                            the NAME (for *.EXE) or LIBRARY (for *.DLL) statement */
  39. ModuleName=""
  40.                                         /* Description statement which gets constructed from the
  41.                                            header (*.H*) file to replace/add the DESCRIPTION
  42.                                            statement in the *.DEF file */
  43. Description=""
  44. Description_Header="Description '"
  45. Description_Trailer="'"
  46.                                         /* The build level part of the DESCRIPTION statement is built
  47.                                            from the following substrings */
  48. BldLevel=""
  49. BldLevel_Header="@#"                    /* fixed (*UM) */
  50. BldLevel_Vendor=""
  51. BldLevel_Version=""
  52. BldLevel_Info=""
  53. BldLevel_Trailer="@#"
  54.  
  55. /*--------------------------------------------------------------------------------------*\
  56.  * Main entry point.                                                                    *
  57.  * Req:                                                                                 *
  58.  *      none                                                                            *
  59.  * Returns:                                                                             *
  60.  *      ReturnCode .... Error code (0 if no error)                                      *
  61. \*--------------------------------------------------------------------------------------*/
  62.                                         /* Get the DefinitionFile (*.DEF) we want to check
  63.                                            the BLDLEVEL and the Headerfile (*.H*) where
  64.                                            the required BLDLEVEL data is defined in */
  65. Parse Arg DefinitionFile HeaderFile InfoOverride        /* (*UM) */
  66.  
  67. if ((DefinitionFile = "") | (HeaderFile = "")) then do  /* (*UM) */
  68.     Say "BuildLevel V1.00, (W) 1996-99 Roman Stangl, Ulrich Möller"
  69.     Say "Syntax: BuildLevel <def> <h> [<info>]"
  70.     Say "with:"
  71.     Say "   <def>           .DEF file to be modified"
  72.     Say "   <h>             .H file containing the following #define's:"
  73.     Say "                       BLDLEVEL_VENDOR"
  74.     Say "                       BLDLEVEL_VERSION"
  75.     Say "                       BLDLEVEL_INFO"
  76.     Say "   <info>          optional info, which overrides BLDLEVEL_INFO in <h>"
  77.     exit;
  78. end
  79.  
  80. Do While (DefinitionFile\="" & HeaderFile\="")
  81.                                         /* We have valid commandline arguments, so first see
  82.                                            if the Headerfile contents are ok for us */
  83.     ReturnCode=ParseHeaderFile()
  84.     If ReturnCode\=0 then do
  85.         Say "Error "||ReturnCode||" parsing header file";
  86.         Leave
  87.     end
  88.  
  89.     if (InfoOverride \= "") then do
  90.         /* BLDLEVEL_INFO overridden: (*UM) */
  91.         if (substr(InfoOverride, 1, 1) = '"') then
  92.             InfoOverride = substr(InfoOverride, 2, length(InfoOverride)-2);
  93.         BldLevel_Info = InfoOverride;
  94.     end
  95.                                         /* Parse the definition file (*.DEF) for the module
  96.                                            name, to see if we got a valid file */
  97.     ReturnCode=ParseDefinitionFile()
  98.     If ReturnCode\=0 then do
  99.         Say "Error "||ReturnCode||" parsing DEF file";
  100.         Leave
  101.     end
  102.                                         /* Copy the definition file, because we're going to
  103.                                            change the original */
  104.     Command="@copy "||DefinitionFile||" "||DefinitionCopy||" /v >NUL"
  105.     Command
  106.     Command="@del "||DefinitionFile||" >NUL 2>&1"
  107.     Command
  108.     If (rc\=0) Then Do
  109.         ReturnCode=2
  110.         Leave
  111.     End
  112.                                         /* Build DESCRIPTION statement for *.DEF file */
  113.     BldLevel=BldLevel_Header||BldLevel_Vendor||":"||BldLevel_Version||"#@ "||BldLevel_Info
  114.                                         /* fixed (*UM) */
  115.     Description=Description_Header||BldLevel||Description_Trailer
  116.                                         /* Copy the module definition file to be able to change
  117.                                            the contents of the original one. In case of an error
  118.                                            copy back the original one */
  119.     ReturnCode=ModifyDefinitionFile()
  120.     If ReturnCode\=0 Then Do
  121.         Command="@copy "||DefinitionCopy||" "||DefinitionFile||" /v >NUL"
  122.         Command
  123.         Command="@del "||DefinitionCopy||" >NUL 2>&1"
  124.         Command
  125.         Leave
  126.     End
  127.  
  128.                                         /* Exit with success */
  129.     Command="@del "||DefinitionCopy||" >NUL 2>&1"
  130.     Command
  131.     ReturnCode=0
  132.     Leave
  133. End
  134.                                         /* Exit to commandline */
  135. If (ReturnCode\=0) Then Do
  136.     Say ""
  137.     Say "Error "||ReturnCode||" modifying "||DefinitionFile||", no changes made"
  138.     Say ""
  139. End
  140. Else Do
  141.     Say ""
  142.     Say "Module definition file "||DefinitionFile||" successfully modified"
  143.     Say ""
  144. End
  145. Return ReturnCode
  146.  
  147. /*--------------------------------------------------------------------------------------*\
  148.  * Parse a C/C++ header file to test that the macros we build the BLDLEVEL information  *
  149.  * from (BLDLEVEL_VENDOR, BLDLEVEL_VERSION, BLDLEVEL_INFO) are defined.                 *
  150.  * Req:                                                                                 *
  151.  *      none                                                                            *
  152.  * Returns:                                                                             *
  153.  *      ReturnCode .... Error code (0 if no error)                                      *
  154. \*--------------------------------------------------------------------------------------*/
  155. ParseHeaderFile:
  156.     LinesLeft=2
  157.                                         /* Read ahead 1st line */
  158.     CurrentLine=LineIn(HeaderFile, 1, 1)
  159.                                         /* Do while there are lines in the file */
  160.     Do While(LinesLeft>0)
  161.                                         /* Parse for constructs of the form:
  162.                                            #define BLDLEVEL_Macro "Value" Comment */
  163.                                         /* Kill that ... tabs */
  164.         CurrentLine=Translate(CurrentLine, ' ', X2C(9))
  165.         Parse Var CurrentLine Define Macro '"' Value '"' .
  166.         Parse Upper Var Define Define
  167.         Define=Space(Define, 0)
  168.         Parse Upper Var Macro Macro
  169.         Macro=Space(Macro, 0)
  170.                                         /* Test for the macros we expect */
  171.         If (Define="#DEFINE") Then Do
  172.             Select
  173.             When (Macro="BLDLEVEL_VENDOR") Then Do
  174.                 BldLevel_Vendor=Value
  175.             End
  176.             When (Macro="BLDLEVEL_VERSION") Then Do
  177.                 BldLevel_Version=Value
  178.             End
  179.             When (Macro="BLDLEVEL_INFO") Then Do
  180.                 BldLevel_Info=Value
  181.             End
  182.             Otherwise
  183.             End
  184.         End
  185.         CurrentLine=LineIn(HeaderFile)
  186.                                         /* As Lines() returns 0 after having read the last line
  187.                                            we have to ensure that this last line will be processed too */
  188.         If (Lines(HeaderFile)=0) Then
  189.             LinesLeft=LinesLeft-1
  190.     End
  191.     If (BldLevel_Vendor\="" & BldLevel_Version\="" & BldLevel_Info\="") Then
  192.         Return 0
  193.     Else
  194.         Return 10
  195.  
  196. /*--------------------------------------------------------------------------------------*\
  197.  * Parse a C/C++ module definition file to test if this file is used to build a OS/2    *
  198.  * EXE or DLL file and if it contains a DESCRIPTION line.                               *
  199.  * Req:                                                                                 *
  200.  *      none                                                                            *
  201.  * Returns:                                                                             *
  202.  *      ReturnCode .... Error code (0 if no error)                                      *
  203. \*--------------------------------------------------------------------------------------*/
  204. ParseDefinitionFile:
  205.                                         /* Read ahead 1st line */
  206.     Command=Stream(DefinitionFile, 'C', 'OPEN READ')
  207.     LinesLeft=2
  208.     CurrentLine=LineIn(DefinitionFile)
  209.                                         /* Do while there are lines in the file */
  210.     Do While(LinesLeft>0)
  211.                                         /* Parse for constructs of the form:
  212.                                            NAME/LIBRARY ModuleName Options */
  213.                                         /* Kill that ... tabs */
  214.         CurrentLine=Translate(CurrentLine, ' ', X2C(9))
  215.         if (left(CurrentLine, 15) == "PHYSICAL DEVICE") then do
  216.             statement = "PHYSICAL DEVICE";
  217.             Parse var CurrentLine x1 x2 Module Options
  218.         end
  219.         else do
  220.             /* parse current line into "statement" and "module" */
  221.             Parse Var CurrentLine Statement Module Options
  222.             Parse Upper Var Statement Statement
  223.             Statement=Space(Statement, 0)
  224.         end
  225.         Module=Space(Module, 0)
  226.         If (Module\="") Then Do
  227.             If (Statement="NAME") Then
  228.                 ModuleName=Module||".exe"
  229.             If (Statement="LIBRARY") Then
  230.                 ModuleName=Module||".dll"
  231.             If (Statement="PHYSICAL DEVICE") Then
  232.                 ModuleName=Module||".sys"
  233.         End
  234.                                         /* See if there is a DESCRIPTION statement */
  235.         Parse Var CurrentLine Statement Options
  236.         Parse Upper Var Statement Statement
  237.         Statement=Space(Statement, 0)
  238.         If (Statement="DESCRIPTION") Then
  239.             DescriptionStatement=Options
  240.         CurrentLine=LineIn(DefinitionFile)
  241.                                         /* As Lines() returns 0 after having read the last line
  242.                                            we have to ensure that this last line will be processed too */
  243.         If (Lines(DefinitionFile)=0) Then
  244.             LinesLeft=LinesLeft-1
  245.     End
  246.                                         /* Close the stream */
  247.     Command=Stream(DefinitionFile, 'C', 'CLOSE')
  248.     If (ModuleName\="" & DescriptionStatement\="") Then
  249.         Return 0
  250.     Else do
  251.         say "parse error in ParseDefinitionFile"
  252.         say "ModuleName is:" ModuleName
  253.         say "DescriptionStatement is:" DescriptionStatement
  254.         Return 20
  255.     end
  256.  
  257. /*--------------------------------------------------------------------------------------*\
  258.  * Parse a C/C++ module definition file to modify the DESCRIPTION statement into a form *
  259.  * that is readable by BLDLEVEL (the original gets discarded).                          *
  260.  * Req:                                                                                 *
  261.  *      none                                                                            *
  262.  * Returns:                                                                             *
  263.  *      ReturnCode .... Error code (0 if no error)                                      *
  264. \*--------------------------------------------------------------------------------------*/
  265. ModifyDefinitionFile:
  266.                                         /* Read and write ahead 1st line */
  267.     Command=Stream(DefinitionFile, 'C', 'OPEN WRITE')
  268.     If (Command="ERROR") Then
  269.         Return 30
  270.     Command=Stream(DefinitionCopy, 'C', 'OPEN READ')
  271.     If (Command="ERROR") Then
  272.         Return 31
  273.     LinesLeft=2
  274.     CurrentLine=LineIn(DefinitionCopy)
  275.                                         /* Do while there are lines in the file */
  276.     Do While(LinesLeft>0)
  277.                                         /* Parse for constructs of the form:
  278.                                            DESCRIPTION Options */
  279.                                         /* Kill that ... tabs */
  280.         CurrentLine=Translate(CurrentLine, ' ', X2C(9))
  281.         Parse Var CurrentLine Statement Options
  282.         Parse Upper Var Statement Statement
  283.         Statement=Space(Statement, 0)
  284.         If (Statement="DESCRIPTION") Then Do
  285.             Command=LineOut(DefinitionFile, Description)
  286.         End
  287.         Else Do
  288.             Command=LineOut(DefinitionFile, CurrentLine)
  289.         End
  290.         CurrentLine=LineIn(DefinitionCopy)
  291.                                         /* As Lines() returns 0 after having read the last line
  292.                                            we have to ensure that this last line will be processed too */
  293.         If (Lines(DefinitionCopy)=0) Then
  294.             LinesLeft=LinesLeft-1
  295.     End
  296.                                         /* Close the streams */
  297.     Command=Stream(DefinitionFile, 'C', 'CLOSE')
  298.     Command=Stream(DefinitionCopy, 'C', 'CLOSE')
  299.     Return 0
  300.  
  301. /*--------------------------------------------------------------------------------------*\
  302.  * Signal Handler that avoids interruption and just tells user that CTRL+BRK is not     *
  303.  * allowed currently.                                                                   *
  304.  * Req:                                                                                 *
  305.  *      none                                                                            *
  306.  * Returns:                                                                             *
  307.  *      none                                                                            *
  308. \*--------------------------------------------------------------------------------------*/
  309. SignalHandler:
  310.     Say "    CTRL+BRK currently disabled"
  311.     Return 0
  312.