Skip to content

Commit

Permalink
Fixed hang on no input. Also improved environment system.
Browse files Browse the repository at this point in the history
Calls to any env function will now automatically translate the
parameters into all caps.
  • Loading branch information
hal7df committed Jun 4, 2014
1 parent 5fad1d9 commit bd9eb35
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
71 changes: 46 additions & 25 deletions iash/iash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ iash::iash (string app_nm, bool useInPrompt)
m_appName = app_nm;
f_useAppNameInPrompt = useInPrompt;

setenv("APP_NAME",m_appName);
setenv("APP_NAME_IN_PROMPT",f_useAppNameInPrompt);
setenv("SYNC_ENV", true);
setenv("IASH_APP_NAME",m_appName);
setenv("IASH_APP_NAME_IN_PROMPT",f_useAppNameInPrompt);
setenv("IASH_SYNC_ENV", true);
}

vector<string> iash::getCmdLine()
Expand All @@ -20,15 +20,19 @@ vector<string> iash::getCmdLine()

clear();

if (getenv_bool("SYNC_ENV"))
if (getenv_bool("IASH_SYNC_ENV"))
updateAttached();

if (f_useAppNameInPrompt)
cout<<m_appName<<"> ";
else
cout<<">>> ";
do
{
if (f_useAppNameInPrompt)
cout<<m_appName<<"> ";
else
cout<<">>> ";

getline(cin,raw);
} while (raw == "");

getline(cin,raw);
lastSpace = 0;

do
Expand Down Expand Up @@ -169,30 +173,47 @@ void iash::setenv(string name, bool value)
}
}

string iash::getenv_string(string name)
{
int x;

for (x = 0; x < name.length(); x++)
name[x] = toupper(name[x]);

return m_env.at(name);
}

bool iash::getenv_bool(string name)
{
if (m_env.at(name) == "true")
if (getenv_string(name) == "true")
return true;
else
return false;
}

void iash::rmenv(string name)
{
if (name != "APP_NAME" && name != "APP_NAME_IN_PROMPT" && name != "SYNC_ENV")
int x;

for (x = 0; x < name.length(); x++)
name[x] = toupper(name[x]);

if (name.find("IASH_") == string::npos)
m_env.erase(name);
else
cout<<"Error: cannot remove required environment variable '"<<name<<"'."<<endl;
}

void iash::setAppName(string app_nm)
{
m_appName = app_nm;
setenv("APP_NAME",app_nm);
setenv("IASH_APP_NAME",app_nm);
}

void iash::useAppNameInPrompt(bool name)
{
f_useAppNameInPrompt = name;
setenv("APP_NAME_IN_PROMPT",name);
setenv("IASH_APP_NAME_IN_PROMPT",name);
}

void iash::debugConsole()
Expand All @@ -202,7 +223,7 @@ void iash::debugConsole()
m_appName = "iash";
f_useAppNameInPrompt = true;

setenv("SYNC_ENV",false);
setenv("IASH_SYNC_ENV",false);

do {
cmdLine = getCmdLine();
Expand All @@ -211,10 +232,10 @@ void iash::debugConsole()
debugConsole(cmdLine);
} while (cmdLine[1] != "exit" && cmdLine[1] != "quit");

setenv("SYNC_ENV",true);
setenv("IASH_SYNC_ENV",true);

m_appName = getenv_string("APP_NAME");
f_useAppNameInPrompt = getenv_bool("APP_NAME_IN_PROMPT");
m_appName = getenv_string("IASH_APP_NAME");
f_useAppNameInPrompt = getenv_bool("IASH_APP_NAME_IN_PROMPT");
clear();
}

Expand Down Expand Up @@ -311,26 +332,26 @@ void iash::debugConsole(vector<string> cmd)
if (cmd.size() > 2)
{
if (cmd[2] == "quit" || cmd[2] == "exit")
exit(0);
exit(EXIT_SUCCESS);
else if (cmd[2] == "kill" || cmd[2] == "crash")
exit(1);
exit(EXIT_FAILURE);
else if (cmd[2] == "name" && cmd.size() == 4)
setenv("APP_NAME",cmd[3]);
setenv("IASH_APP_NAME",cmd[3]);
else
cout<<cmd[2]<<": invalid "<<cmd[1]<<" argument"<<endl;
}
else
cout<<getenv_string("APP_NAME")<<endl;
cout<<getenv_string("IASH_APP_NAME")<<endl;
}
else if (cmd[1] != "exit" && cmd[1] != "quit")
cmdNotFound_dbg();
}

void iash::updateAttached()
{
if (getenv_string("APP_NAME") != m_appName)
m_appName = getenv_string("APP_NAME");
if (getenv_string("IASH_APP_NAME") != m_appName)
m_appName = getenv_string("IASH_APP_NAME");

if (getenv_bool("APP_NAME_IN_PROMPT") != f_useAppNameInPrompt)
f_useAppNameInPrompt = getenv_bool("APP_NAME_IN_PROMPT");
if (getenv_bool("IASH_APP_NAME_IN_PROMPT") != f_useAppNameInPrompt)
f_useAppNameInPrompt = getenv_bool("IASH_APP_NAME_IN_PROMPT");
}
6 changes: 3 additions & 3 deletions iash/iash.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class iash
* @param name: The name of the variable
* @return: The variable
*/
string getenv_string (string name) { return m_env.at(name); }
int getenv_int (string name) { return stoi(m_env.at(name)); }
float getenv_float (string name) { return stof(m_env.at(name)); }
string getenv_string (string name);
int getenv_int (string name) { return stoi(getenv_string(name)); }
float getenv_float (string name) { return stof(getenv_string(name)); }
bool getenv_bool (string name);

/**
Expand Down

0 comments on commit bd9eb35

Please sign in to comment.