Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / netsvcs / clients / Naming / Dump_Restore / Dump_Restore.cpp
blob9572f57efa0fcf4950cf94ef18571ec134dc0dca
1 #include "ace/Malloc_Base.h"
2 #include "ace/Read_Buffer.h"
3 #include "ace/Thread_Manager.h"
5 // FUZZ: disable check_for_streams_include
6 #include "ace/streams.h" /* Because dump () uses ofstream. */
8 #include "Dump_Restore.h"
9 #include "ace/OS_NS_signal.h"
10 #include "ace/OS_NS_stdio.h"
11 #include "ace/OS_NS_string.h"
12 #include "ace/OS_NS_unistd.h"
15 Dump_Restore::Dump_Restore (int argc, ACE_TCHAR *argv[])
16 : infile_ (0)
18 ACE_NEW (this->ns_context_,
19 ACE_Naming_Context);
21 // Cache the name options
22 this->name_options_ = this->ns_context_->name_options ();
23 this->name_options_->parse_args (argc, argv);
25 //determine name context
26 if (ACE_OS::strcmp (this->name_options_->nameserver_host (),
27 ACE_TEXT ("localhost")) == 0)
29 if (ns_context_->open (ACE_Naming_Context::PROC_LOCAL) == -1)
30 ACE_ERROR ((LM_ERROR,
31 ACE_TEXT ("%p\n"),
32 ACE_TEXT ("ns_context_->open")));
34 else
36 // Don't really need to do this but it's a hack to fix the
37 // problme of Display () not printing the right hostname
38 ACE_OS::strcpy (this->hostname_,
39 this->name_options_->nameserver_host ());
40 this->port_ =
41 this->name_options_->nameserver_port ();
43 if (this->ns_context_->open (ACE_Naming_Context::NET_LOCAL) == -1)
44 ACE_ERROR ((LM_ERROR,
45 ACE_TEXT ("%p\n"),
46 ACE_TEXT ("ns_context_->open")));
49 this->display_menu ();
51 if (ACE_Event_Handler::register_stdin_handler (this,
52 ACE_Reactor::instance (),
53 ACE_Thread_Manager::instance ()) == -1)
54 ACE_ERROR ((LM_ERROR,
55 ACE_TEXT ("%p\n"),
56 ACE_TEXT ("register_stdin_handler")));
59 Dump_Restore::~Dump_Restore ()
61 // Deregister this handler with the ACE_Reactor.
62 ACE_Reactor::instance ()->remove_handler
63 (ACE_STDIN,
64 ACE_Event_Handler::DONT_CALL | ACE_Event_Handler::READ_MASK);
66 ACE_OS::fclose (this->infile_);
69 int
70 Dump_Restore::handle_input (ACE_HANDLE)
72 char option[BUFSIZ];
73 char buf1[BUFSIZ];
74 u_short port;
76 if (::scanf ("%s", option) <= 0)
78 ACE_DEBUG ((LM_ERROR,
79 ACE_TEXT ("try again\n")));
80 return 0;
83 switch (option[0])
85 case 'P' :
86 case 'p' :
87 set_proc_local ();
88 break;
89 case 'N' :
90 case 'n' :
91 set_node_local ();
92 break;
93 case 'H' :
94 case 'h' :
95 if (::scanf ("%s %hu", buf1, &port) <= 0)
96 break;
97 set_host (ACE_TEXT_CHAR_TO_TCHAR (buf1), port);
98 break;
99 case 'F':
100 case 'f':
101 if (::scanf ("%s", filename_) <= 0)
102 break;
103 if (this->infile_)
104 ACE_OS::fclose (this->infile_);
105 this->infile_ = ACE_OS::fopen(filename_, ACE_TEXT("r"));
106 break;
107 case 'B' :
108 case 'b' :
109 populate (Dump_Restore::BIND);
110 break;
111 case 'U' :
112 case 'u' :
113 populate (Dump_Restore::UNBIND);
114 break;
115 case 'R' :
116 case 'r' :
117 populate (Dump_Restore::REBIND);
118 break;
119 case 'D':
120 case 'd':
121 if (::scanf ("%s", dump_filename_) <= 0)
122 break;
123 this->dump ();
124 break;
125 case 'Q' :
126 case 'q' :
127 quit ();
128 break;
129 default :
130 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Unrecognized command.\n")));
133 display_menu ();
134 return 0;
137 void
138 Dump_Restore::display_menu ()
140 ACE_DEBUG ((LM_DEBUG,
141 ACE_TEXT ("\n")));
142 ACE_DEBUG ((LM_DEBUG,
143 ACE_TEXT ("Name Service Main Menu\n")));
144 ACE_DEBUG ((LM_DEBUG,
145 ACE_TEXT ("----------------------\n")));
147 // Check if using local name space or remote name space
148 if (ACE_OS::strcmp (this->name_options_->nameserver_host (),
149 ACE_TEXT ("localhost")) == 0)
151 if (this->ns_scope_ == ACE_Naming_Context::PROC_LOCAL)
152 ACE_DEBUG ((LM_DEBUG,
153 ACE_TEXT (" *** Using Process Local Database ***\n\n")));
154 else
155 ACE_DEBUG ((LM_DEBUG,
156 ACE_TEXT (" *** Using Node Local Database ***\n\n")));
158 else
160 ACE_DEBUG ((LM_DEBUG,
161 ACE_TEXT (" Hostname: %s\n"),
162 this->hostname_));
163 ACE_DEBUG ((LM_DEBUG,
164 ACE_TEXT (" Port Number: %d\n"),
165 this->port_));
168 if (this->infile_)
169 ACE_DEBUG ((LM_DEBUG,
170 ACE_TEXT ("Input File: %C\n"),
171 this->filename_));
172 else
173 ACE_DEBUG ((LM_DEBUG,
174 ACE_TEXT ("** No Input File Specified **\n")));
176 ACE_DEBUG ((LM_DEBUG,
177 ACE_TEXT ("<P> Use Process Local Database\n")));
178 ACE_DEBUG ((LM_DEBUG,
179 ACE_TEXT ("<N> Use Node Local Database\n")));
180 ACE_DEBUG ((LM_DEBUG,
181 ACE_TEXT ("<H> Set Remote Name server <host> and <port>\n")));
182 ACE_DEBUG ((LM_DEBUG,
183 ACE_TEXT ("<F> Set Input File <file name>\n")));
184 ACE_DEBUG ((LM_DEBUG,
185 ACE_TEXT ("<B> Bind\n")));
186 ACE_DEBUG ((LM_DEBUG,
187 ACE_TEXT ("<U> Unbind\n")));
188 ACE_DEBUG ((LM_DEBUG,
189 ACE_TEXT ("<R> Rebind\n")));
190 ACE_DEBUG ((LM_DEBUG,
191 ACE_TEXT ("<D> Dump <file name>\n")));
192 ACE_DEBUG ((LM_DEBUG,
193 ACE_TEXT ("<Q> or ^C (exit)\n")));
198 Dump_Restore::set_proc_local ()
200 // Set Name Options
201 this->name_options_->nameserver_host (ACE_TEXT ("localhost"));
202 this->name_options_->nameserver_port (0);
204 // Set Naming Context scope
205 this->ns_scope_ =
206 ACE_Naming_Context::PROC_LOCAL;
208 // Remove old naming context
209 delete this->ns_context_;
211 // Create new Naming Context
212 ACE_NEW_RETURN (this->ns_context_,
213 ACE_Naming_Context,
214 -1);
216 if (this->ns_context_->open (ACE_Naming_Context::PROC_LOCAL) == -1)
217 ACE_ERROR_RETURN ((LM_ERROR,
218 ACE_TEXT ("%p\n"),
219 ACE_TEXT ("ns_context_->open")),
220 -1);
222 return 0;
226 Dump_Restore::set_node_local ()
228 // Set Name Options
229 this->name_options_->nameserver_host (ACE_TEXT ("localhost"));
230 this->name_options_->nameserver_port (0);
232 // Set Naming Context scope
233 this->ns_scope_ = ACE_Naming_Context::NODE_LOCAL;
235 // Remove old naming context
236 delete this->ns_context_;
238 // Create new Naming Context
239 ACE_NEW_RETURN (this->ns_context_,
240 ACE_Naming_Context,
241 -1);
243 if (ns_context_->open (ACE_Naming_Context::NODE_LOCAL) == -1)
244 ACE_ERROR_RETURN ((LM_ERROR,
245 ACE_TEXT ("%p\n"),
246 ACE_TEXT ("ns_context_->open")),
247 -1);
248 return 0;
252 Dump_Restore::set_host (const ACE_TCHAR *hostname,
253 int port)
255 // Set Name Options
256 this->name_options_->nameserver_host (hostname);
257 this->name_options_->nameserver_port (port);
259 // Don't really need to do this but it's a hack to fix the problme
260 // of Display () not printing the right hostname
261 ACE_OS::strcpy (this->hostname_, hostname);
262 this->port_ = port;
263 this->ns_scope_ = ACE_Naming_Context::NET_LOCAL;
265 // remove old naming context
266 delete this->ns_context_;
268 // Create new Naming Context
269 ACE_NEW_RETURN (this->ns_context_,
270 ACE_Naming_Context,
271 -1);
273 // assume net_local context
274 if (ns_context_->open (ACE_Naming_Context::NET_LOCAL) == -1)
275 ACE_ERROR_RETURN ((LM_ERROR,
276 ACE_TEXT ("%p\n"),
277 ACE_TEXT ("ns_context_->open")),
278 -1);
279 return 0;
283 Dump_Restore::doit (Dump_Restore::Operation_Type op,
284 const char *name,
285 const char *value,
286 const char *type)
288 int result = -1;
290 switch (op)
292 case Dump_Restore::BIND:
294 result = this->bind (name, value, type);
295 break;
297 case Dump_Restore::UNBIND:
299 result = this->unbind (name);
300 break;
302 case Dump_Restore::REBIND:
304 result = this->rebind (name, value, type);
305 break;
309 return result;
313 Dump_Restore::populate (Dump_Restore::Operation_Type op)
315 if (this->infile_)
317 int result = -1;
318 enum State { NAME, VALUE, TYPE };
320 State state = NAME;
321 // reset file pointer
322 ACE_OS::rewind (this->infile_);
324 ACE_Allocator *allocator =
325 ACE_Allocator::instance ();
326 ACE_Read_Buffer read_buffer (this->infile_,
328 allocator);
330 for (char *temp;
331 (temp = read_buffer.read ('\n')) != 0;
334 char *name = 0;
335 const char *actual_name = 0;
336 char *value = 0;
337 const char *actual_value = 0;
338 char *type = 0;
339 const char *actual_type = 0;
341 switch (state)
343 case NAME:
344 name = temp;
345 ACE_OS::strtok (name, "=");
346 actual_name = ACE_OS::strtok (0, "=");
347 state = VALUE;
348 break;
349 case VALUE:
350 value = temp;
351 ACE_OS::strtok (value, "=");
352 actual_value = ACE_OS::strtok (0, "=");
353 state = TYPE;
354 break;
355 case TYPE:
356 type = temp;
357 ACE_OS::strtok (type, "=");
358 actual_type = ACE_OS::strtok (0, "=");
360 if (actual_type)
361 result = this->doit (op,
362 actual_name,
363 actual_value,
364 actual_type);
365 else
366 result = this->doit (op,
367 actual_name,
368 actual_value);
369 if (name)
370 allocator->free(name);
371 if (value)
372 allocator->free(value);
373 if (type)
374 allocator->free(type);
375 state = NAME;
376 break;
377 default:
378 ACE_ERROR_RETURN ((LM_ERROR,
379 ACE_TEXT ("%p\n"),
380 ACE_TEXT ("populate")),
381 -1);
382 /* NOTREACHED */
386 return result;
388 else
389 return -1;
393 Dump_Restore::bind (const char *key,
394 const char *value,
395 const char *type)
397 int result = ns_context_->bind (key,
398 value,
399 type);
400 if (result == -1)
401 ACE_ERROR_RETURN ((LM_ERROR,
402 ACE_TEXT ("%p\n"),
403 ACE_TEXT ("ns_context_->bind")),
404 -1);
405 else if (result == 1)
406 ACE_ERROR_RETURN ((LM_ERROR,
407 ACE_TEXT ("%s%s%s\n"),
408 ACE_TEXT ("key <"),
409 key,
410 ACE_TEXT ("> already bound")),
412 return 0;
416 Dump_Restore::unbind (const char *key)
418 int result = ns_context_->unbind (key);
420 if (result == -1)
421 ACE_ERROR_RETURN ((LM_ERROR,
422 ACE_TEXT ("%p\n"),
423 ACE_TEXT ("ns_context_->unbind")),
424 -1);
425 return 0;
429 Dump_Restore::rebind (const char *key,
430 const char *value,
431 const char *type)
433 if (ns_context_->rebind (key,
434 value,
435 type) == -1)
436 ACE_ERROR_RETURN ((LM_ERROR,
437 ACE_TEXT ("%p\n"),
438 ACE_TEXT ("ns_context_->rebind")),
439 -1);
440 return 0;
444 Dump_Restore::quit ()
446 return ACE_OS::kill (ACE_OS::getpid (), SIGINT);
449 void
450 Dump_Restore::dump ()
452 ofstream output_file (dump_filename_);
454 ostream *orig_stream = ACE_Log_Msg::instance ()->msg_ostream ();
455 ACE_Log_Msg::instance ()->msg_ostream (&output_file);
456 ACE_Log_Msg::instance ()->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER );
457 ACE_Log_Msg::instance ()->set_flags (ACE_Log_Msg::OSTREAM);
459 ns_context_->dump ();
461 ACE_Log_Msg::instance ()->msg_ostream (orig_stream);
462 ACE_Log_Msg::instance ()->clr_flags (ACE_Log_Msg::STDERR);