Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / apps / soreduce / Obj_Module.cpp
blob75fe1ce38a5b67b2dd8ae24814d1ad98a2d2ea58
1 // -*- C++ -*-
2 // File: Obj_Module.cpp
4 // Author: Phil Mesnier
6 // This file contains the implementation of the classes responsible for
7 // managing the contents of a single object module (.o file).
9 #include "ace/OS_NS_string.h"
10 #include "ace/OS_NS_unistd.h"
11 #include "ace/Process.h"
12 #include "ace/Pipe.h"
13 #include "ace/Message_Block.h"
14 #include "ace/Log_Msg.h"
16 #include "Obj_Module.h"
18 //----------------------------------------------------------------------------
20 Obj_Module::Obj_Module (const ACE_CString &name, int cap)
21 : name_ (name),
22 imports_(cap),
23 exports_(cap),
24 extrefs_(0)
28 ACE_CString &
29 Obj_Module::name()
31 return name_;
34 Sig_List &
35 Obj_Module::exports()
37 return exports_;
40 Sig_List &
41 Obj_Module::imports()
43 return imports_;
46 int
47 Obj_Module::extref()
49 return extrefs_;
52 void
53 Obj_Module::add_extref()
55 extrefs_ ++;
58 void
59 Obj_Module::remove_extref()
61 extrefs_ --;
64 int
65 Obj_Module::read_line (ACE_HANDLE src, ACE_Message_Block **buf)
67 int eoln = 0;
68 // ACE_Time_Value timeout (1,0);
69 if (buf == 0 || *buf == 0) {
70 char dummy;
71 while (!eoln && ACE_OS::read(src,&dummy,1) == 1) {
72 eoln = (dummy == '\n');
74 return eoln;
76 // while (!eoln && ACE::recv(src,buf->wr_ptr(),1,&timeout) == 1) {
77 while (!eoln && ACE_OS::read(src,(*buf)->wr_ptr(),1) == 1) {
78 eoln = (*(*buf)->wr_ptr() == '\n');
79 (*buf)->wr_ptr(1);
80 if ((*buf)->space() == 0) {
81 (*buf)->cont(new ACE_Message_Block(102400));
82 *buf = (*buf)->cont();
85 return eoln;
88 void
89 Obj_Module::add_source(const char *p, int imports_only)
91 ACE_Process nmproc;
92 ACE_Process_Options nm_opts;
93 ACE_CString path (p);
95 ACE_CString::size_type pathsep = path.rfind('/');
97 ACE_CString src_name;
98 ACE_CString workpath;
100 if (pathsep == ACE_CString::npos) {
101 src_name = path;
102 workpath = ".";
103 } else {
104 src_name = path.substr(pathsep+1);
105 workpath= path.substr(0,pathsep);
108 ACE_HANDLE pipe[2];
109 ACE_Pipe io(pipe);
111 nm_opts.working_directory (workpath.c_str());
112 nm_opts.set_handles (ACE_STDIN,pipe[1]);
114 // Options for the command line shown here are for the GNU nm 2.9.5
116 int result = nm_opts.command_line ("nm -C %s",src_name.c_str());
117 // Prevent compiler warning about "unused variable" if ACE_ASSERT is
118 // an empty macro.
119 ACE_UNUSED_ARG (result);
120 ACE_ASSERT (result == 0);
122 nmproc.spawn (nm_opts);
123 if (ACE_OS::close(pipe[1]) == -1)
124 ACE_DEBUG ((LM_DEBUG, "%p\n", "close"));
125 nm_opts.release_handles();
127 int import_lines = 0;
128 int export_lines = 0;
129 ACE_Message_Block im_buffer (102400);
130 ACE_Message_Block ex_buffer (102400);
131 ACE_Message_Block *im_buf_cur = &im_buffer;
132 ACE_Message_Block *ex_buf_cur = &ex_buffer;
133 char dummy;
134 int eoln = 1;
135 // ACE_Time_Value timeout (1,0);
136 int is_import = 1;
137 int is_export = 1;
139 while (eoln == 1) {
140 for (int i = 0; i < 10; i++) {
141 if (ACE_OS::read(pipe[0],&dummy,1) != 1) {
142 eoln = 2;
143 break;
146 if (eoln == 2)
147 break;
148 is_import = dummy == 'U';
149 is_export = !imports_only && (ACE_OS::strchr("BCDRTVW",dummy) != 0);
151 // if (ACE::recv(pipe[0],&dummy,1,&timeout) != 1)
152 if (ACE_OS::read(pipe[0],&dummy,1) != 1)
153 break;
155 eoln = this->read_line (pipe[0], is_import ? &im_buf_cur :
156 (is_export ? &ex_buf_cur : 0));
157 import_lines += is_import;
158 export_lines += is_export;
160 // ACE_DEBUG ((LM_DEBUG, "read %d import lines and %d export lines\n",
161 // import_lines, export_lines));
163 nmproc.wait ();
164 ACE_OS::close (pipe[0]);
166 this->populate_sig_list (imports_,import_lines,&im_buffer);
167 if (!imports_only)
168 this->populate_sig_list (exports_,export_lines,&ex_buffer);
171 void
172 Obj_Module::populate_sig_list (Sig_List &siglist,
173 int lines,
174 ACE_Message_Block *buf)
176 char *c;
177 ACE_CString temp;
179 for (int i = 0; i < lines; i++)
181 for (c = buf->rd_ptr (); c != buf->wr_ptr () && *c != '\n'; ++c)
183 // No action.
186 temp += ACE_CString (buf->rd_ptr (), (c - buf->rd_ptr ()));
187 buf->rd_ptr (c + 1);
189 if (*c == '\n')
191 // ACE_DEBUG ((LM_DEBUG, "%s\n",temp.c_str()));
192 siglist.add (temp);
193 temp.clear ();
195 else
197 buf = buf->cont ();
199 if (buf == 0)
201 siglist.add (temp);