1/****************************************************************************
2 * Copyright (c) 1998,2006 Free Software Foundation, Inc.                   *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/*
30 * $Id: makedef.cmd,v 1.5 2006/04/22 23:14:50 tom Exp $
31 *
32 * Author:  Juan Jose Garcia Ripoll <worm@arrakis.es>.
33 * Webpage: http://www.arrakis.es/~worm/
34 *
35 * makedef.cmd - update a DLL export list using a newly created library file
36 *		 in a.out format, plus an old .DEF file.
37 *
38 * standard output gets a sorted list with all entrypoints with entrycodes.
39 * This list, plus a few .def sentences (LIBRARY, DESCRIPTION and EXPORT)
40 * is used to build a new .def file.
41 *
42 * `_nc_*' symbols are ignored.
43 *
44 * returns 1 when the old def_file is corrupted -- that is, export items are
45 * not properly formatted.
46 *
47 * returns 0 if everything went OK.
48 */
49
50parse arg lib_file def_file
51
52lib_file = translate(lib_file,'\','/')
53def_file = translate(def_file,'\','/')
54
55call CleanQueue
56
57/*
58 * `codes' is the stem that links a code to every symbol
59 * `names' is the stem where symbols are stored sequentially
60 * `last' is the index of the last symbol defined
61 */
62last   = 0
63used.  = 0
64codes. = 0
65names. = ''
66
67tmp_name = 'foo.tmp'
68
69/*
70 * This sed expression cleans empty lines, comments and special .DEF
71 * commands, such as LIBRARY..., EXPORTS..., etc
72 */
73tidy_up  = '"/^[A-Z]/d;s/[ 	][ 	]*/ /g;s/;.*$//g;s/^[ ]*//g;/^[ ]*$/d"'
74
75/*
76 * First we find all public symbols (functions and variables). Next we
77 * concatenate this list with the old one, sorting it and wiping out
78 * all unused data (comments, DLL directives, blanks, etc). All this
79 * information is pushed into a REXX private list with the RXQUEUE
80 * utility program.
81 */
82'@echo off'
83'emxexp -u' lib_file '>' tmp_name
84'cat' tmp_name def_file '| sed' tidy_up '| sort > foo2.tmp'
85'type foo2.tmp | rxqueue'
86'del' tmp_name '1>NUL'
87
88/*
89 * This loop runs over the queue items
90 */
91do while queued() > 0
92   /*
93    * We retrieve the symbol name (NEW_NAME) and its number (NEW_NUMBER)
94    * When the line comes from `emximp's output, there's no number, so
95    * we assign it the special value 0.
96    */
97   parse pull new_symbol '@'new_code rest
98   if Left(new_symbol,1) = '"' then
99      parse var new_symbol '"' new_name '"' rest
100   else
101      do
102      echo 'Symbol 'new_symbol' was not quoted'
103      new_name = new_symbol
104      end
105
106   if new_code = '' then
107      new_code = 0
108   /*
109    * Here, one would place all smart checks that would kill unused symbols.
110    * However, export tables are not that big, so why bothering?
111   if Left(new_name,4) = '_nc_' then
112      iterate
113    */
114   /*
115    * The algorithm:
116    *	IF (this is the 2nd time the symbol appears) THEN
117    *		(this symbol comes from a .DEF file)
118    *		it has a valid code that we store
119    *		we mark that code as used
120    *   ELIF (it has no number) THEN
121    *		(it's a new symbol)
122    *		we increase the counter of defined symbols
123    *		we assign it the special number 0
124    *		(later on it'll be assigned an unused export code)
125    *   ELSE
126    *		this symbol was in the old DLL and it's no longer
127    *		here, so we skip it.
128    */
129   select
130      when new_name = '' then
131         'echo Warning: empty symbol found 1>&2'
132      when names.last = new_name then
133         do
134         codes.last = new_code
135         used.new_code = 1
136         end
137      when new_code = 0 then
138         do
139         last = last + 1
140         names.last = new_name
141         codes.last = 0
142         end
143   otherwise
144      'echo Warning: symbol "'new_name'" has disappeared 1>&2'
145   end /* select */
146end /* do while queued() */
147
148/*
149 * Finally we scan the stem, writing out all symbols with export codes.
150 * Those that did not have a valid one (just 0) are assigned a new one.
151 */
152new_code = 1
153inx = 1
154do while inx <= last
155   if codes.inx = 0 then
156      do
157      do while used.new_code \= 0
158         new_code = new_code + 1
159      end
160      codes.inx = new_code
161      used.new_code = 1
162      end
163   say '	"'names.inx'"	@'codes.inx'	NONAME'
164   inx = inx + 1
165end
166'del foo2.tmp 1>NUL'
167exit 0
168
169/*
170 * Cleans the REXX queue by pulling and forgetting every line.
171 * This is needed, at least, when `makedef.cmd' starts, because an aborted
172 * REXX program might have left some rubbish in.
173 */
174CleanQueue: procedure
175   do while queued() > 0
176      parse pull foo
177   end
178return
179
180