home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / builleve.zip / BldLevelInf.cmd next >
OS/2 REXX Batch file  |  2002-03-03  |  17KB  |  571 lines

  1. /* $Id: BldLevelInf.cmd,v 1.5 2001/01/26 21:33:13 phaller Exp $
  2.  *
  3.  * Adds a Description string to the given .def-file.
  4.  * Fills in default values; like build time and host.
  5.  *
  6.  */
  7.  
  8. if RxFuncQuery('SysLoadFuncs') = 1 then
  9. do
  10.     call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs';
  11.     call SysLoadFuncs;
  12. end
  13.  
  14.  
  15. /*
  16.  * Set default parameter values.
  17.  */
  18. sDefFileIn      = '';
  19. sDefFileOut     = '';
  20. sASDFeatureId   = '';
  21. sCountryCode    = '';
  22. sDateTime       = left(' 'date()' 'time(), 26);
  23. sDescription    = 'Odin32';
  24. sFixPakVer      = '';
  25. sHostname       = strip(substr(VALUE('HOSTNAME',,'OS2ENVIRONMENT'), 1, 11));
  26. sLanguageCode   = '';
  27. sMiniVer        = '';
  28. sVendor         = 'Project Odin';
  29. sVersion        = '0.5';
  30.  
  31.  
  32. /*
  33.  * Parse parameters.
  34.  */
  35. parse arg  sArgs
  36. if (sArgs = '') then
  37. do
  38.     call syntax;
  39.     exit(1);
  40. end
  41.  
  42. do while (sArgs <> '')
  43.     sArgs = strip(sArgs);
  44.     if (substr(sArgs, 1, 1) = '-' | substr(sArgs, 1, 1) = '/') then
  45.     do  /*
  46.          * Option.
  47.          */
  48.         ch = translate(substr(sArgs, 2, 1));
  49.         if (pos(ch, 'ACDHLMNPRTV') < 1) then
  50.         do
  51.             say 'invalid option:' substr(sArgs, 1, 2);
  52.             call syntax;
  53.             exit(2);
  54.         end
  55.  
  56.         /* get value and advance sArgs to next or to end. */
  57.         if (substr(sArgs, 3, 1) = '"') then
  58.         do
  59.             iNext = pos('"', sArgs, 4);
  60.             fQuote = 1;
  61.         end
  62.         else
  63.         do
  64.             iNext = pos(' ', sArgs, 3);
  65.             if (iNext <= 0) then
  66.                 iNext = length(sArgs);
  67.             fQuote = 0;
  68.         end
  69.  
  70.         if (iNext > 3 | ch = 'R') then
  71.         do
  72.             sValue = substr(sArgs, 3 + fQuote, iNext - 3 - fQuote);
  73.             sArgs = strip(substr(sArgs, iNext+1));
  74.             /*say 'iNext:' iNext  'sValue:' sValue  'sArgs:' sArgs; */
  75.  
  76.             /* check if we're gonna search for something in an file. */
  77.             if (sValue <> '' & pos('#define=', sValue) > 0) then
  78.                 sValue = LookupDefine(sValue);
  79.         end
  80.         else
  81.         do
  82.             say 'syntax error near' substr(sArgs, 1, 2)'.';
  83.             call syntax;
  84.             exit(3);
  85.         end
  86.  
  87.  
  88.         /* set value */
  89.         select
  90.             when (ch = 'A') then /* ASD Feature Id */
  91.                 sASDFeatureId   = sValue;
  92.  
  93.             when (ch = 'C') then /* Country code */
  94.                 sCountryCode    = sValue;
  95.  
  96.             when (ch = 'D') then /* Description */
  97.                 sDescription    = sValue;
  98.  
  99.             when (ch = 'H') then /* Hostname */
  100.                 sHostname       = sValue;
  101.  
  102.             when (ch = 'L') then /* Language code */
  103.                 sLanguageCode   = sValue;
  104.  
  105.             when (ch = 'M') then /* MiniVer */
  106.                 sMiniVer        = sValue;
  107.  
  108.             when (ch = 'N') then /* Vendor */
  109.                 sVendor         = sValue;
  110.  
  111.             when (ch = 'R') then /* Vendor */
  112.                 sDescription    = ReadDescription(sValue, sDefFile);
  113.  
  114.             when (ch = 'P') then /* Fixpak version */
  115.                 sFixPakVer      = sValue;
  116.  
  117.             when (ch = 'T') then /* Date Time */
  118.                 sDateTime       = sValue;
  119.  
  120.             when (ch = 'V') then /* Version */
  121.                 sVersion        = sValue;
  122.  
  123.             /* Otherwise it's an illegal option */
  124.             otherwise
  125.                 say 'invalid option:' substr(sArgs, 1, 2);
  126.                 call syntax;
  127.                 exit(2);
  128.         end /* select */
  129.     end
  130.     else
  131.     do  /*
  132.          * Defition file...
  133.          */
  134.         if (sDefFileOut <> '') then
  135.         do
  136.             say 'Syntax error: Can''t specify more than two defintion files!';
  137.             exit(4);
  138.         end
  139.         if (sDefFileIn = '') then
  140.             parse value sArgs with sDefFileIn' 'sArgs
  141.         else
  142.             parse value sArgs with sDefFileOut' 'sArgs
  143.         sArgs = strip(sArgs);
  144.     end
  145. end
  146.  
  147.  
  148. /* check that a defintion file was specified. */
  149. if (sDefFileIn = '') then
  150. do
  151.     say 'Syntax error: Will have to specify a .def-file to update.';
  152.     call syntax;
  153.     exit(5);
  154. end
  155.  
  156.  
  157. /*
  158.  * Trim strings to correct lengths.
  159.  */
  160. sVendor         = strip(substr(sVendor, 1, 31));
  161. if (substr(sDateTime, 1, 1) <> ' ') then
  162.     sDateTime = ' ' || sDateTime;
  163. sDateTime       = left(sDateTime, 26);
  164. sHostname       = strip(substr(sHostname, 1, 11));
  165. sMiniVer        = strip(substr(sMiniVer, 1, 11));
  166. sDescription    = strip(substr(sDescription, 1, 80));
  167. sCountryCode    = strip(substr(sCountryCode, 1, 4));
  168. sLanguageCode   = strip(substr(sLanguageCode, 1, 4));
  169. sASDFeatureId   = strip(substr(sASDFeatureId, 1, 11));
  170. sFixPakVer      = strip(substr(sFixPakVer, 1, 11));
  171.  
  172.  
  173. /*
  174.  * Signature
  175.  */
  176. sEnhSign = '##1##'
  177.  
  178. /*
  179.  * Build description string.
  180.  */
  181. sDescription =  '@#'sVendor':'sVersion'#@'sEnhSign||,
  182.                 sDateTime||sHostname||,
  183.                 ':'sASDFeatureId':'sLanguageCode':'sCountryCode':'sMiniVer||,
  184.                 '::'sFixPakVer'@@'sDescription;
  185.  
  186. /*
  187.  * Update .def-file.
  188.  */
  189. call SysFileDelete(sDefFileIn);
  190. rc = lineout( sDefFileIn, 'RCDATA 1 { "' || sDescription || '" }');
  191.  
  192. /*rc = UpdateDefFile(sDefFileIn, sDefFileOut, sDescription);*/
  193. exit(rc);
  194.  
  195.  
  196. /**
  197.  * Display script syntax.
  198.  */
  199. syntax: procedure
  200.     say 'Syntax: MakeDesc.cmd [options] <deffile in> <deffile out> [options]'
  201.     say '   <deffile>   Defitionfile which will have an DESCRIPTION appended.'
  202.     say 'Options:'
  203.     say '   -A<string>  ASD Feature Id.'
  204.     say '   -C<string>  Country code.'
  205.     say '   -D<string>  Description.'
  206.     say '   -R[deffile] Read description from .def file.'
  207.     say '   -H<string>  Hostname.'
  208.     say '   -L<string>  Language code.'
  209.     say '   -M<string>  MiniVer.'
  210.     say '   -N<string>  Vendor.'
  211.     say '   -P<string>  Fixpak version.'
  212.     say '   -T<string>  Date Time.'
  213.     say '   -V<string>  Version.'
  214.     say '<string> could be a double qoute qouted string or a single word.'
  215.     say '       You could also reference #defines in C/C++ include files.'
  216.     say '       The string should then have this form:'
  217.     say '           "#define=<DEFINE_NAME>,<includefile.h>"'
  218.     say '';
  219.  
  220.     return;
  221.  
  222.  
  223. /**
  224.  * Search for a #define in an C/C++ header or source file.
  225.  *
  226.  * @returns String containing the defined value
  227.  *          found for the define in the header file.
  228.  *          Quits on fatal errors.
  229.  * @param   A string on the form: "#define=DEFINETOFIND,includefile.h"
  230.  * @remark  Write only code... - let's hope it works.
  231.  */
  232. LookupDefine: procedure
  233.     parse arg '#'sDefine'='sMacro','sIncludeFile
  234.  
  235.     /*
  236.      * Validate parameters.
  237.      */
  238.     sMacro = strip(sMacro);
  239.     sIncludeFile = strip(sIncludeFile);
  240.     if (sMacro = '') then
  241.     do
  242.         say 'syntax error: #define=<DEFINE_NAME>,<includefile.h>.';
  243.         say '    <DEFINE_NAME> was empty.';
  244.         exit(-20);
  245.     end
  246.     if (sIncludeFile = '') then
  247.     do
  248.         say 'syntax error: #define=<DEFINE_NAME>,<includefile.h>.';
  249.         say '    <includefile.h> was empty.';
  250.         exit(-20);
  251.     end
  252.  
  253.  
  254.     sIllegal = translate(translate(sMacro),,
  255.                          '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',,
  256.                          'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_');
  257.  
  258.     if (strip(translate(sIllegal, ' ', '!')) <> '') then
  259.     do
  260.         say 'syntax error: #define=<DEFINE_NAME>,<includefile.h>.';
  261.         say '    <DEFINE_NAME> contains illegal charater(s).'
  262.         say '        'sMacro;
  263.         say '        'translate(sIllegal, ' ', '!');
  264.         exit(-20);
  265.     end
  266.  
  267.     /*
  268.      * Open include file.
  269.      */
  270.     sRc = stream(sIncludeFile, 'c', 'open read');
  271.     if (pos('READY', sRc) <> 1) then
  272.     do  /* search INCLUDE variable */
  273.         sFile = SysSearchPath('INCLUDE', sIncludeFile);
  274.         if (sFile = '') then
  275.         do
  276.             say 'Can''t find include file 'sIncludeFile'.';
  277.             exit(-20);
  278.         end
  279.         sIncludeFile = sFile;
  280.  
  281.         sRc = stream(sIncludeFile, 'c', 'open read');
  282.         if (pos('READY', sRc) <> 1) then
  283.         do
  284.             say 'Failed to open include file' sIncludeFile'.';
  285.             exit(-20);
  286.         end
  287.     end
  288.  
  289.     /*
  290.      * Search the file line by line.
  291.      *  We'll check for lines starting with a hash (#) char.
  292.      *  Then check that the word after the hash is 'define'.
  293.      *  Then match the next word with the macro name.
  294.      *  Then then get the next rest of the line to comment or continuation char.
  295.      *      (continuation is not supported)
  296.      *  Finally strip quotes.
  297.      */
  298.     sValue = '';
  299.     do while (lines(sIncludeFile) > 0)
  300.         sLine = strip(linein(sIncludeFile));
  301.         if (sLine = '') then
  302.             iterate;
  303.         if (substr(sLine, 1, 1) <> '#') then
  304.             iterate;
  305.         sLine = substr(sLine, 2);
  306.         if (word(sLine, 1) <> 'define') then
  307.             iterate;
  308.         sLine = strip(substr(sLine, wordpos(sLine, 1) + length('define')+1));
  309.         if (  substr(sLine, 1, length(sMacro)) <> sMacro,
  310.             | substr(sLine, length(sMacro)+1, 1) <> ' ') then
  311.             iterate;
  312.         sLine = strip(substr(sLine, length(sMacro) + 1));
  313.         if (sLine = '') then
  314.         do
  315.             say 'error: #define' sMacro' is empty.';
  316.             call stream sIncludeFile, 'c', 'close';
  317.             exit(-20);
  318.         end
  319.  
  320.         chQuote = substr(sLine, 1, 1);
  321.         if (chQuote = '"' | chQuote = "'") then
  322.         do  /* quoted string */
  323.             iLastQuote = 0;
  324.             do forever
  325.                 iLast = pos(chQuote, sLine, 2);
  326.                 if (iLast <= 0) then
  327.                     leave;
  328.                 if (substr(sLine, iLast, 1) = '\') then
  329.                     iterate;
  330.                 iLastQuote = iLast;
  331.                 leave;
  332.             end
  333.  
  334.             if (iLastQuote <= 0) then
  335.             do
  336.                 say 'C/C++ syntax error in 'sIncludefile': didn''t find end quote.';
  337.                 call stream sIncludeFile, 'c', 'close';
  338.                 exit(-20);
  339.             end
  340.  
  341.             call stream sIncludeFile, 'c', 'close';
  342.             sValue = substr(sLine, 2, iLastQuote - 2);
  343.             say 'Found 'sMacro'='sValue;
  344.             return sValue;
  345.         end
  346.         else
  347.         do
  348.             iCommentCPP = pos('//',sLine);
  349.             iCommentC   = pos('/*',sLine);
  350.             if (iCommentC > 0 & iCommentCPP > 0 & iCommentC > iCommentCPP) then
  351.                 iComment = iCommentCPP;
  352.             else if (iCommentC > 0 & iCommentCPP > 0 & iCommentC < iCommentCPP) then
  353.                 iComment = iCommentC;
  354.             else if (iCommentCPP > 0) then
  355.                 iComment = iCommentCPP;
  356.             else if (iCommentC > 0) then
  357.                 iComment = iCommentC;
  358.             else
  359.                 iComment = 0;
  360.  
  361.             if (iComment > 0) then
  362.                 sValue = strip(substr(sLine, 1, iComment-1));
  363.             else
  364.                 sValue = strip(sLine);
  365.  
  366.             if (sValue <> '') then
  367.             do
  368.                 if (substr(sValue, length(sValue)) = '\') then
  369.                 do
  370.                     say 'Found continuation char: Multiline definitions are not supported!\n';
  371.                     call stream sIncludeFile, 'c', 'close';
  372.                     exit(-20);
  373.                 end
  374.             end
  375.  
  376.             if (sValue = '') then
  377.                 say 'warning: The #define has no value.';
  378.  
  379.             call stream sIncludeFile, 'c', 'close';
  380.             say 'Found 'sMacro'='sValue;
  381.             return sValue;
  382.         end
  383.     end
  384.  
  385.     call stream sIncludeFile, 'c', 'close';
  386.     say 'error: didn''t find #define' sMacro'.';
  387.     exit(-20);
  388.  
  389.  
  390.  
  391. /**
  392.  * Reads the description line for a .def-file.
  393.  * @returns The Description string, with quotes removed.
  394.  *          Empty string is acceptable.
  395.  *          On error we'll terminate the script.
  396.  * @param   sDefFile    Filaname of .def-file to read the description from.
  397.  * @param   sDefFile2   Used if sDefFile is empty.
  398.  * @author  knut st. osmundsen (knut.stange.osmundsen@mynd.no)
  399.  */
  400. ReadDescription: procedure;
  401.     parse arg sDefFile, sDefFile2
  402.  
  403.     /*
  404.      * Validate parameters.
  405.      */
  406.     if (sDefFile = '') then
  407.         sDefFile = sDefFile2;
  408.     if (sDefFile = '') then
  409.     do
  410.         say 'error: no definition file to get description from.'
  411.         exit(-1);
  412.     end
  413.  
  414.     /*
  415.      * Open file
  416.      */
  417.     rc = stream(sDefFile, 'c', 'open read');
  418.     if (pos('READY', rc) <> 1) then
  419.     do
  420.         say 'error: failed to open deffile file.';
  421.         exit(-1);
  422.     end
  423.  
  424.  
  425.     /*
  426.      * Search for the 'DESCRIPTION' line.
  427.      */
  428.     do while (lines(sDefFile) > 0)
  429.         sLine = strip(linein(sDefFile));
  430.         if (sLine = '') then
  431.             iterate;
  432.         if (translate(word(sLine, 1)) <> 'DESCRIPTION') then
  433.             iterate;
  434.         sLine = strip(substr(sLine, wordpos(sLine, 1) + length('DESCRIPTION')+1));
  435.  
  436.         ch = substr(sLine, 1, 1);
  437.         if (ch <> "'" & ch <> '"') then
  438.         do
  439.             say 'syntax error: description line in' sDefFile 'is misformed.';
  440.             call stream sDefFile, 'c', 'close';
  441.             exit(-10);
  442.         end
  443.  
  444.         iEnd = pos(ch, sLine, 2);
  445.         if (iEnd <= 0) then
  446.         do
  447.             say 'syntax error: description line in' sDefFile 'is misformed.';
  448.             call stream sDefFile, 'c', 'close';
  449.             exit(-10);
  450.         end
  451.  
  452.         call stream sDefFile, 'c', 'close';
  453.         sValue = substr(sLine, 2, iEnd - 2);
  454.         say 'Found Description:' sValue;
  455.         return sValue;
  456.     end
  457.  
  458.     call stream sDefFile, 'c', 'close';
  459.     say 'info: Didn''t find description line in' sDefFile'.';
  460.     return '';
  461.  
  462.  
  463. /**
  464.  * This is a function which reads sDefFileIn into and
  465.  * internal array and changes the DESCRIPTION text if found.
  466.  * If DESCRIPTION isn't found, it is added at the end.
  467.  * The array is written to sDefFileOut.
  468.  * @returns 0 on succes.
  469.  *          Errorcode on error.
  470.  * @param   sDefFileIn      Input .def-file.
  471.  * @param   sDefFileOut     Output .def-file. Overwritten.
  472.  * @param   sDescription    New description string.
  473.  * @author  knut st. osmundsen (knut.stange.osmundsen@mynd.no)
  474.  */
  475. UpdateDefFile: procedure;
  476.     parse arg sDefFileIn, sDefFileOut, sDescription
  477.  
  478.     /*
  479.      * Validate parameters.
  480.      */
  481.     if (sDefFileOut = '') then
  482.         sDefFileOut = sDefFileIn;
  483.  
  484.     /*
  485.      * Open file input file.
  486.      */
  487.     rc = stream(sDefFileIn, 'c', 'open read');
  488.     if (pos('READY', rc) <> 1) then
  489.     do
  490.         say 'error: failed to open' sDefFileIn 'file.';
  491.         return 110;
  492.     end
  493.  
  494.  
  495.     /*
  496.      * Search for the 'BLDLEVEL' line.
  497.      */
  498.     i = 0;
  499.     fDescription = 0;
  500.     do while (lines(sDefFileIn) > 0)
  501.         /*
  502.          * Read line.
  503.          */
  504.         i = i + 1;
  505.         asFile.i = linein(sDefFileIn);
  506.  
  507.         /*
  508.          * Look for BLDLEVEL;
  509.          */
  510.         if (asFile.i = '') then
  511.             iterate;
  512.         if (translate(word(strip(asFile.i), 1)) <> 'BLDLEVEL') then
  513.             iterate;
  514.         if (fDescription) then
  515.         do
  516.             say 'warning: multiple descriptions lines. Line' i 'removed';
  517.             i = i - 1;
  518.             iterate;
  519.         end
  520.  
  521.         /*
  522.          * Found description - replace with new description.
  523.          */
  524.         asFile.i = "BldLevel = '"||sDescription||"'";
  525.         fDescription = 1;
  526.     end
  527.  
  528.     /*
  529.      * Add description is none was found.
  530.      */
  531.     if (\fDescription) then
  532.     do
  533.         i = i + 1;
  534.         asFile.i = "BldLevel = '"||sDescription||"'";
  535.     end
  536.     asFile.0 = i;
  537.  
  538.  
  539.     /*
  540.      * Close input file and open output file.
  541.      */
  542.     call stream sDefFileIn, 'c', 'close';
  543.     call SysFileDelete(sDefFileOut);
  544.     rc = stream(sDefFileOut, 'c', 'open write');
  545.     if (pos('READY', rc) <> 1) then
  546.     do
  547.         say 'error: failed to open outputfile' sDefFileOut 'file.';
  548.         return 110;
  549.     end
  550.  
  551.     /*
  552.      * Make firstline and write all the lines to the output file.
  553.      */
  554.     /*call lineout sDefFileOut, '; Updated by makedesc.cmd', 1;*/
  555.     do i = 1 to asFile.0
  556.         rc = lineout(sDefFileOut, asFile.i);
  557.         if (rc > 0) then
  558.         do
  559.             say 'error: failed to write line' i 'to' sDefFileOut'.'
  560.             call stream sDefFileOut, 'c', 'close';
  561.             return 5;
  562.         end
  563.     end
  564.  
  565.     /*
  566.      * Close output file and return succesfully.
  567.      */
  568.     call stream sDefFileOut, 'c', 'close';
  569.     return 0;
  570.  
  571.