home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 4 Drivers / 04-Drivers.zip / cs0929a.zip / wat2map.cmd < prev    next >
OS/2 REXX Batch file  |  1997-09-30  |  8KB  |  204 lines

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