home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / wat2map.zip / wat2map.cmd
OS/2 REXX Batch file  |  2002-04-26  |  9KB  |  206 lines

  1. /* $Id: wat2map.cmd,v 1.2 2002/04/26 23:09:44 smilcke Exp $ */
  2.  
  3. /* SCCSID = src/dev/mme/pciaudio/wat2map.cmd, pciaudio, c.basedd 99/08/20 */
  4. /****************************************************************************
  5.  *                                                                          *
  6.  * Copyright (c) IBM Corporation 1994 - 1997.                               *
  7.  *                                                                          *
  8.  * The following IBM OS/2 source code is provided to you solely for the     *
  9.  * the purpose of assisting you in your development of OS/2 device drivers. *
  10.  * You may use this code in accordance with the IBM License Agreement       *
  11.  * provided in the IBM Device Driver Source Kit for OS/2.                   *
  12.  *                                                                          *
  13.  ****************************************************************************/
  14. /**@internal src/dev/mme/pciaudio/wat2map.cmd, pciaudio, c.basedd
  15.  * WAT2MAP - translate symbol map from Watcom format to MS format.
  16.  * @version 1.1
  17.  * @context
  18.  *  Unless otherwise noted, all interfaces are Ring-0, 16-bit, kernel stack.
  19.  * @notes
  20.  *  Usage:  WAT2MAP <watcom_mapfile >ms_mapfile
  21.  *               - or -
  22.  *          type watcom_mapfile | WAT2MAP >ms_mapfile
  23.  *
  24.  *          Reads from stdin, writes to stdout.  Will accept the Watcom map filename
  25.  *          as an argument (in place of reading from stdin).  Eg.,
  26.  *
  27.  *          WAT2MAP watcom_mapfile >ms_mapfile
  28.  *
  29.  *  Notes:
  30.  *    1.)  The symbol handling in the debug kernel won't work for some of the
  31.  *         characters used in the C++ name space.  WAT2MAP handles these symbols
  32.  *         as follows.
  33.  *             Scoping operator symbol '::' is translated to '__'.
  34.  *             Destructor symbol '~' is translated to 'd'.
  35.  *             Symbols for operators '::operator' are not provided.
  36.  *
  37.  *         Eg., for user defined class 'A', the symbol for constructor A::A is
  38.  *         translated to A__A, and the destructor symbol A::~A becomes A__dA, and
  39.  *         the assignment operator, 'A::operator =', is not translated.
  40.  *
  41.  *    2.)  Bug - C++ provides for defining multiple functions with same fn name
  42.  *         but different function signatures (different parameter lists).  This
  43.  *         utility just translates all the address / symbol combinations it finds,
  44.  *         so you can end up with several addresses for a given fn name.
  45.  * @history
  46. */
  47. /* <End of helpText> - don't modify this string - used to flag end of help. */
  48. /****************************************************************************/
  49. CALL RXFUNCADD 'sysloadfuncs','rexxutil','sysloadfuncs'
  50. call sysloadfuncs
  51.  
  52. Parse Arg arg1 arg2 rest
  53. If (Length( arg1 ) = 0) | (Verify( arg1, '/-?' ) = 0) Then Do;
  54.    Do i = 1 to 1000
  55.       helpText = Sourceline(i)
  56.       If Pos( '<End of helpText>', helpText ) <> 0 Then Leave;       /* quit loop */
  57.       Say helpText
  58.    End;
  59.    Return
  60. End;
  61. If Length( arg2 ) = 0 Then Do;
  62.    Say " Way to go Beaver... How about an out-put file name ?"
  63.    Return
  64. End;
  65. mapFile = arg1          /* Can be Null, in which case we pull from stdin. */
  66. outFile = arg2
  67.  
  68. /* erase outfile */  /* kill the old map file */
  69. rc=SysFileDelete(outfile)
  70.  
  71.  
  72. /*--- 1.  Find & translate module name.  ---*/
  73. Do While Lines( mapFile ) <> 0
  74.    watcomText = LineIn( mapFile )
  75.    Parse Value watcomText With "Executable Image: " fileName "." fileExt
  76.    If fileName <> "" Then Do;   /* Found match */
  77.       call lineout outfile ,' '
  78.       call lineout outfile ,' ' || fileName
  79.       call lineout outfile ,' '
  80.       Leave;                    /* Break from loop. */
  81.    End;
  82. End
  83. If Lines( mapFile ) = 0 Then Do;        /* If end of file ... */
  84.    Say "Error:  Expected to find line with text 'Executable Image:' "
  85.    Return
  86. End
  87.  
  88. /*--- 2.  Skip the group definitions - Rob's notes say we don't need them. -*/
  89.  
  90. /*--- 3.  Skip to the start of the segment table.  ---*/
  91. Do While Lines( mapFile ) <> 0
  92.    watcomText = LineIn( mapFile )
  93.    Parse Value watcomText With "Segment" header_2_3 "Address" header_5
  94.    If Strip( header_5 ) = "Size" Then Leave;      /* Found header line for Segment table. */
  95. End
  96. If Lines( mapFile ) = 0 Then Do;        /* If end of file ... */
  97.    Say "Error:  Expected to find line with text 'Segments ... Size' "
  98.    Return
  99. End
  100. junk = LineIn( mapFile )       /* Discard a couple lines of formatting. */
  101. junk = LineIn( mapFile )
  102.  
  103. /*--- 4.  Translate segment table.  ---*/
  104. call lineout outfile , " Start     Length     Name                   Class"
  105. Do While Lines( mapFile ) <> 0
  106.    watcomText = LineIn( mapFile )
  107.    Parse Value watcomText With segName className groupName address size .
  108.    If segName = "" Then Leave;          /* Empty line, break from loop. */
  109.    length = Substr( size, 4, 5 ) || 'H     '
  110.    segName = Left( segName, 23 )
  111.    call lineout outfile ,' ' || address || ' ' || length || segName || className
  112. End
  113. call lineout outfile ,' '     /* Extra line feed. */
  114.  
  115.  
  116. /*--- 5.  For all remaining lines in the input file:  if the line starts
  117.    with a 16:16 address, assume it's a symbol declaration and translate
  118.    it into MS format.  ---*/
  119.  
  120. call lineout outfile ,'  Address         Publics by Value'
  121. /* call lineout outfile ,' '*/
  122.  
  123. Do While Lines( mapFile ) <> 0
  124.    watcomText = LineIn( mapFile )
  125.    Parse Value watcomText With seg ':' ofs 10 . 16 declaration
  126.    is_Adress = (is_Hex(seg) = 1) & (is_Hex(ofs) = 1)
  127.    If (is_Adress = 1) Then Do;
  128.  
  129.       /*--- Haven't done the work to xlate operator symbols - skip the line. */
  130.       If Pos( '::operator', declaration ) <> 0 Then Iterate;
  131.  
  132.       /*--- Strip any arguement list if this is a function prototype.  */
  133.       declaration = StripMatchedParen( declaration )
  134.  
  135.       /*--- Strip array brackets if this is an array. */
  136.       sqBracket = Pos( '[', declaration )
  137.       If sqBracket <> 0
  138.          Then declaration = Substr(declaration, 1, sqBracket-1);
  139.  
  140.       /*--- Strip leading tokens from the function name.
  141.          Eg., remove function return type, near/far, etc.  */
  142.       declaration = Word( declaration, Words(declaration) )
  143.  
  144.       /*--- Strip any remaining parens around function name. ---*/
  145.       declaration = ReplaceSubstr( '(', ' ', declaration )
  146.       declaration = ReplaceSubstr( ')', ' ', declaration )
  147.  
  148.       /*--- Debug kernel doesn't like symbol for scoping operator "::"
  149.          in symbol names.  Replace :: with double underscore "__". ---*/
  150.       declaration = ReplaceSubstr( '::', '__', declaration )
  151.  
  152.       /*--- Debug kernel doesn't like symbol for destructor "~"
  153.          in symbol names.  Replace ~ with character "d" for "destructor.
  154.          Note destructor for a class will translate "A::~A" -> "A__dA". ---*/
  155.       declaration = ReplaceSubstr( '~', 'd', declaration )
  156.  
  157.       call lineout outfile ,' ' || seg || ':' || ofs || '       ' || declaration
  158.    End;
  159. End; /* End While through symbol section, end of input file. */
  160.  
  161. Return;  /* End of program.  */
  162.  
  163. /*--- Helper subroutines. ---*/
  164.  
  165. StripMatchedParen:
  166. /* Strips matched "( )" from end of string.  Returns
  167.    a substring with the trailing, matched parens deleted. */
  168.  
  169.    Parse Arg string
  170.  
  171.    ixOpenParen = LastPos( "(", string );
  172.    ixCloseParen = LastPos( ")", Substr( string, 1, Length(string)-1 ));
  173.  
  174.    If (ixOpenParen = 0)                     /* No match. */
  175.       Then Return string
  176.    Else If ixCloseParen < ixOpenParen       /* Found match, no imbedded "()". */
  177.       Then Return Substr( string, 1, ixOpenParen-1 )
  178.    Else Do;                                 /* Imbedded (), must skip over them. */
  179.       /* Parse Value string With first ixCloseParen+1 rest */
  180.       first = Substr( string, 1, ixCloseParen)
  181.       rest = Substr( string, ixCloseParen+1 )
  182.       string = StripMatchedParen( first ) || rest
  183.       Return StripMatchedParen( string )
  184.    End;
  185. End;
  186.  
  187. ReplaceSubstr:
  188. /* Replaces oldPat (old pattern) with newPat (new pattern) in string. */
  189.  
  190.    Parse Arg oldPat , newPat , string
  191.  
  192.    ix = Pos( oldPat, string )
  193.    if ix <> 0 Then Do;
  194.       first = Substr( string, 1, ix-1 )
  195.       rest  = Substr( string, ix + Length( oldPat ) )
  196.       string = first || newPat || rest
  197.    End;
  198.    Return string
  199. End;
  200.  
  201. is_Hex:
  202. /* Returns 1 if String is valid hex number, 0 otherwise. */
  203.    Parse Arg string
  204.    Return (Length(string) > 0) &  (Verify( string, '0123456789abcdefABCDEF' ) = 0)
  205. End;
  206.