Commit 1e2603e8 authored by Benoit Perrot's avatar Benoit Perrot
Browse files

Index: ChangeLog

from  Benoît Perrot  <benoit@lrde.epita.fr>

        * src/shell/cmd.hh,
        * src/shell/shell.hh, src/shell/shell.cc:
        Use std::string instead of char* or std::string*, avoiding
        hazardous memory manipulations.
parent f9e11802
2004-05-01 Benot Perrot <benoit@lrde.epita.fr>
* src/shell/cmd.hh,
* src/shell/shell.hh, src/shell/shell.cc:
Use std::string instead of char* or std::string*, avoiding
hazardous memory manipulations.
2004-04-25 Benot Perrot <benoit@lrde.epita.fr>
* src/shell/cmd.hh,
* src/shell/shell.cc, src/shell/shell.hh:
Use references instead of pointers for aggregated attributes.
Use references for composed attributes, and pointers for aggregated
attributes.
2004-04-25 Benot Perrot <benoit@lrde.epita.fr>
......
......@@ -2,21 +2,22 @@
(Created-By-Prcs-Version 1 3 2)
(Project-Description "")
(Project-Version mipsy 0 90)
(Parent-Version mipsy 0 89)
(Project-Version mipsy 0 91)
(Parent-Version mipsy 0 90)
(Version-Log
"Index: ChangeLog
from Benot Perrot <benoit@lrde.epita.fr>
* src/shell/cmd.hh,
* src/shell/shell.cc, src/shell/shell.hh:
Use references instead of pointers for aggregated attributes.
* src/shell/cmd.hh,
* src/shell/shell.hh, src/shell/shell.cc:
Use std::string instead of char* or std::string*, avoiding
hazardous memory manipulations.
")
(New-Version-Log
"")
(Checkin-Time "Sun, 25 Apr 2004 18:43:32 +0200")
(Checkin-Time "Sun, 02 May 2004 15:29:43 +0200")
(Checkin-Login benoit)
(Files
......@@ -24,7 +25,7 @@ from Beno
;; ./
(AUTHORS (mipsy/0_AUTHORS 1.2 644))
(COPYING (mipsy/1_COPYING 1.1 644))
(ChangeLog (mipsy/2_ChangeLog 1.84 604))
(ChangeLog (mipsy/2_ChangeLog 1.85 604))
(INSTALL (mipsy/b/12_INSTALL 1.1 604))
(Makefile.am (mipsy/3_Makefile.a 1.9 604))
(NEWS (mipsy/b/25_NEWS 1.10 604))
......@@ -99,11 +100,11 @@ from Beno
;; ./src/shell/
(src/shell/Makefile.am (mipsy/b/17_Makefile.a 1.1 644))
(src/shell/cmd.hh (mipsy/b/18_cmd.hh 1.2 644))
(src/shell/cmd.hh (mipsy/b/18_cmd.hh 1.3 644))
(src/shell/shell-tasks.cc (mipsy/b/19_shell-task 1.1 644))
(src/shell/shell-tasks.hh (mipsy/c/22_shell-task 1.2 644))
(src/shell/shell.cc (mipsy/c/23_shell.cc 1.2 644))
(src/shell/shell.hh (mipsy/c/24_shell.hh 1.2 644))
(src/shell/shell.cc (mipsy/c/23_shell.cc 1.3 644))
(src/shell/shell.hh (mipsy/c/24_shell.hh 1.3 644))
;; ./src/task/
(src/task/Makefile.am (mipsy/45_Makefile.a 1.2 644))
......
......@@ -33,7 +33,6 @@
# include <algorithm>
# include "common.hh"
# include "modules.hh"
# include "task/task_register.hh"
......@@ -67,7 +66,7 @@ namespace shell
cmd_help
};
typedef std::list<const std::string*> list_args_type;
typedef std::list<std::string> list_args_type;
/**\name Constructor and destructor
\{ */
......@@ -75,25 +74,19 @@ namespace shell
/// Construct a Cmd.
Cmd(const cmd_type_type cmd_type, Shell &sh):
cmd_type_(cmd_type),
sh_(sh),
list_args_()
sh_(sh)
{
}
/// Destroy a Cmd.
virtual ~Cmd()
{
for (list_args_type::iterator
i = list_args_.begin(); i != list_args_.end(); ++i)
delete *i;
list_args_.erase(list_args_.begin(), list_args_.end());
}
~Cmd()
{}
/** \} */
/// Add an argument
void push_arg(const std::string& arg)
void push_arg(const std::string &arg)
{
list_args_.push_back(&arg);
list_args_.push_back(arg);
}
/// Execute the Cmd.
......@@ -158,11 +151,9 @@ namespace shell
private:
void e_load()
{
const std::string* fileName = *(list_args_.begin());
char* tmpbuf = new char[fileName->length()];
strcpy(tmpbuf, fileName->c_str());
//filename = fileName->c_str();
filename = tmpbuf;
// FIXME: introducing memory leak!
filename = strdup(list_args_.begin()->c_str());
//std::cerr << "#DEBUG: _load: filename == " << filename << std::endl;
parse::tasks::parse();
......@@ -230,22 +221,19 @@ namespace shell
return;
}
const std::string* label = *(list_args_.begin());
//std::cerr <<"#DEBUG: label = " << *label << std::endl;
// sh_.vm_.add_break(*label);
sh_.vm_.add_breakpoint(*label);
sh_.vm_.add_breakpoint(*list_args_.begin());
}
void e_display()
{
const std::string* r = *(list_args_.begin());
const std::string* rr = new std::string(*r);
sh_.add_display(rr);
sh_.add_display(*list_args_.begin());
}
void e_undisplay()
{
sh_.remove_display(*(list_args_.begin()));
sh_.remove_display(*list_args_.begin());
}
void e_dump()
......@@ -255,8 +243,9 @@ namespace shell
void e_print()
{
const std::string* r = *(list_args_.begin());
std::cout << *r << " = " << sh_.get_register(*r) << std::endl;
std::cout
<< *list_args_.begin() << " = "
<< sh_.get_register(*list_args_.begin()) << std::endl;
}
void e_fill_delay_slot()
......@@ -266,7 +255,7 @@ namespace shell
void e_mem()
{
const std::string* addr = *(list_args_.begin());
// const std::string* addr = *(list_args_.begin());
//std::cout << "addr: " << *addr << " = " << sh_.vm_.get_memory_offset(atoi(addr->c_str())) << std::endl;
}
......
......@@ -28,7 +28,6 @@ namespace shell
Shell::Shell():
vm_(true, false),
cmd_buf_(0),
pos_(0)
{
......@@ -129,7 +128,7 @@ namespace shell
}
Cmd*
Shell::build_cmd(const char* str)
Shell::build_cmd(const std::string &str)
{
pos_ = 0;
cmd_buf_ = str;
......@@ -141,28 +140,30 @@ namespace shell
case RUN:
{
command = new Cmd(Cmd::cmd_run, *this);
const std::string* tmp = 0;
while ((tmp = get_next_word()) != 0)
command->push_arg(*tmp);
std::string tmp = get_next_word();
while (0 < tmp.length())
{
command->push_arg(tmp);
tmp = get_next_word();
}
return command;
}
case BREAK:
{
command = new Cmd(Cmd::cmd_break, *this);
const std::string* tmp = get_next_word();
if (tmp == 0)
std::string tmp = get_next_word();
if (tmp.empty())
{
delete command;
break;
}
// check tmp is a label
if (is_label(*tmp))
if (is_label(tmp))
{
command->push_arg(*tmp);
command->push_arg(tmp);
return command;
}
delete command;
delete tmp;
break;
}
case CONTINUE:
......@@ -174,83 +175,80 @@ namespace shell
case LOAD:
{
command = new Cmd(Cmd::cmd_load, *this);
const std::string* tmp = get_next_word();
if (tmp == 0)
std::string tmp = get_next_word();
if (tmp.empty())
{
delete command;
break;
}
command->push_arg(*tmp);
command->push_arg(tmp);
return command;
}
case PRINT:
{
command = new Cmd(Cmd::cmd_print, *this);
const std::string* tmp = get_next_word();
if (tmp == 0)
std::string tmp = get_next_word();
if (tmp.empty())
{
delete command;
break;
}
// check tmp is a register
if (is_register(*tmp))
if (is_register(tmp))
{
command->push_arg(*tmp);
command->push_arg(tmp);
return command;
}
delete command;
delete tmp;
break;
}
case DISPLAY:
{
command = new Cmd(Cmd::cmd_display, *this);
const std::string* tmp = get_next_word();
if (tmp == 0)
std::string tmp = get_next_word();
if (tmp.empty())
{
delete command;
break;
}
// check tmp is a register
if (is_register(*tmp))
if (is_register(tmp))
{
command->push_arg(*tmp);
command->push_arg(tmp);
return command;
}
delete command;
delete tmp;
break;
}
case UNDISPLAY:
{
command = new Cmd(Cmd::cmd_undisplay, *this);
const std::string* tmp = get_next_word();
if (tmp == 0)
std::string tmp = get_next_word();
if (tmp.empty())
{
delete command;
break;
}
// check tmp is a register
if (is_register(*tmp))
if (is_register(tmp))
{
command->push_arg(*tmp);
command->push_arg(tmp);
return command;
}
delete command;
delete tmp;
break;
}
case MEM:
{
command = new Cmd(Cmd::cmd_mem, *this);
const std::string* tmp = get_next_word();
if (tmp == 0)
std::string tmp = get_next_word();
if (tmp.empty())
{
delete command;
break;
}
// check tmp is a memory addr
command->push_arg(*tmp);
command->push_arg(tmp);
return command;
}
case FILL_DELAY_SLOT:
......
......@@ -31,7 +31,10 @@
# include <algorithm>
# include "common.hh"
# include "modules.hh"
# include "modules.hh" // FIXME: check if usefull
# include "misc/has.hh"
# include "task/task_register.hh"
# include "vm/virtual_machine.hh"
......@@ -58,17 +61,6 @@ namespace shell
HELP = 213
};
// FIXME: move to another file.
/** Object function to compare two char**. */
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
/** Forward declaration. */
class Cmd;
......@@ -81,9 +73,9 @@ namespace shell
public:
/** \name Shortand type definition
\{ */
typedef std::map<const char*, shell_token_type, ltstr> map_token_type;
typedef std::map<const char*, vm::Cpu::kind_type, ltstr> map_register_type;
typedef std::list<const std::string*> list_disp_type;
typedef std::map<std::string, shell_token_type> map_token_type;
typedef std::map<std::string, vm::Cpu::kind_type> map_register_type;
typedef std::list<std::string> list_disp_type;
/** \} */
/** \name Constructor and destructor
......@@ -94,11 +86,7 @@ namespace shell
/// Destroy a Shell.
~Shell()
{
for (list_disp_type::iterator
i = list_displayable_.begin(); i != list_displayable_.end(); ++i)
delete *i;
}
{}
/** \} */
public:
......@@ -106,36 +94,36 @@ namespace shell
void run();
/// Build a command.
Cmd* build_cmd(const char* str);
Cmd* build_cmd(const std::string &str);
public:
/// Access to a register.
int get_register(const std::string& r)
int get_register(const std::string &r)
{
if (r == "pc")
return vm_.get_cpu_pc();
return vm_.get_cpu_register(map_register_[r.c_str()]);
return vm_.get_cpu_register(map_register_[r]); // FIXME: assert r exists
}
/** \name Display list
\{ */
public:
/// Add a register in the display list.
void add_display(const std::string* r)
void add_display(const std::string &r)
{
list_displayable_.push_back(r);
}
/// Remove a register from the display list.
void remove_display(const std::string* r)
void remove_display(const std::string &r)
{
list_disp_type::iterator i;
for (i = list_displayable_.begin(); i != list_displayable_.end(); ++i)
if (**i == *r)
break;
list_disp_type::iterator i = find(list_displayable_, r);
// for (i = list_displayable_.begin(); i != list_displayable_.end(); ++i)
// if (*i == *r)
// break;
if (i != list_displayable_.end())
{
delete *i;
list_displayable_.erase(i);
}
}
......@@ -145,26 +133,21 @@ namespace shell
{
for (list_disp_type::const_iterator
i = list_displayable_.begin(); i != list_displayable_.end(); ++i)
std::cout << **i << " = " << get_register(**i) << std::endl;
std::cout << *i << " = " << get_register(*i) << std::endl;
}
/** \} */
// Internal scanner/parser
private:
/// Check if str is a valid register.
bool is_register(const std::string& str)
bool is_register(const std::string &str)
{
const char* tmpstr = str.c_str();
if (strcmp("pc", tmpstr) == 0)
if (str == "pc")
return true;
for (map_register_type::const_iterator
i = map_register_.begin(); i != map_register_.end(); ++i)
if (strcmp(i->first, tmpstr) == 0)
{
//std::cerr << "#DEBUG: is_register map OK" << std::endl;
return true;
}
map_register_type::const_iterator it = map_register_.find(str);
if (it != map_register_.end())
return true;
// TODO: check unlimited register here
return false;
......@@ -181,7 +164,7 @@ namespace shell
}
/// Get the next word in the command line.
const std::string* get_next_word()
std::string get_next_word()
{
std::stringstream strb;
......@@ -190,10 +173,10 @@ namespace shell
while (cmd_buf_[pos_] && (cmd_buf_[pos_] != ' '))
strb << cmd_buf_[pos_++];
if ((strb.str().length() == 0) && (cmd_buf_[pos_] == 0))
return 0;
return std::string(); // FIXME: awkward!
if (cmd_buf_[pos_] != 0)
++pos_;
return new std::string(strb.str());
return std::string(strb.str());
}
/// Eat a word in the command line and return the corresponding keyword.
......@@ -207,11 +190,11 @@ namespace shell
strb << cmd_buf_[pos_++];
if ((strb.str().length() == 0) && (cmd_buf_[pos_] == 0))
return EOL;
if (map_token_.find(strb.str().c_str()) == map_token_.end())
if (map_token_.find(strb.str()) == map_token_.end())
return TERROR;
if (cmd_buf_[pos_] != 0)
++pos_;
return map_token_[strb.str().c_str()];
return map_token_[strb.str()];
}
private:
......@@ -219,7 +202,7 @@ namespace shell
vm::VirtualMachine vm_;
/// command line link.
const char* cmd_buf_;
std::string cmd_buf_;
/// position in the command line parsing.
int pos_;
/// map of token word.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment