Skip to content

Commit

Permalink
Introduced exceptions. Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Jun 20, 2012
1 parent 215c5cd commit 9ada24c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
70 changes: 38 additions & 32 deletions agent-ini/src/quotes.cc
Expand Up @@ -32,6 +32,7 @@

#include "quotes.h"
#include <ycp/y2log.h>
#include <stdexcept>

using namespace std;

Expand All @@ -52,14 +53,16 @@ string quote( const string & unquoted_string)
return dest;
}

bool parse_dquoted_string( string::const_iterator & sit, const string::const_iterator & last, string & ret)
string parse_dquoted_string( string::const_iterator & sit, const string::const_iterator & last)
{
string ret;

while( sit != last)
{
switch( *sit)
{
case '"':
return true;
return ret;

case '\\':
switch( *(++sit))
Expand All @@ -85,24 +88,28 @@ bool parse_dquoted_string( string::const_iterator & sit, const string::const_ite
}
}

return false;
throw invalid_argument( "Missing enclosing dquote.");
return "";
}

bool parse_squoted_string( string::const_iterator & sit, const string::const_iterator & last, string & ret)
string parse_squoted_string( string::const_iterator & sit, const string::const_iterator & last)
{
string ret;

while( sit != last)
{
if( *sit == '\'')
{
return true;
return ret;
}
else
{
ret += *(sit++);
}
}

return false;
throw invalid_argument( "Missing enclosing squote.");
return "";
}

/*
Expand All @@ -114,11 +121,11 @@ bool parse_squoted_string( string::const_iterator & sit, const string::const_ite
* - starts with ['"]
* - as a consequence: size > 0
*/
bool parse_quoted_string( string::const_iterator & sit, const string::const_iterator & last, string & ret)
string parse_quoted_string( string::const_iterator & sit, const string::const_iterator & last)
{
char quote_char = *( sit++);

return quote_char == '"' ? parse_dquoted_string( sit, last, ret) : parse_squoted_string( sit, last, ret);
return quote_char == '"' ? parse_dquoted_string( sit, last) : parse_squoted_string( sit, last);
}

string unquote( const string & quoted_string)
Expand All @@ -127,35 +134,34 @@ string unquote( const string & quoted_string)
string::const_iterator sit = quoted_string.begin();
string::const_iterator end = quoted_string.end();

while( sit != end)
try
{
while( sit != end &&
*sit != '"' &&
*sit != '\'')
{
if( *sit == '\\')
{
if( ++sit == end)
return res;
while( sit != end)
{
while( sit != end &&
*sit != '"' &&
*sit != '\'')
{
if( *sit == '\\')
{
if( ++sit == end)
return res;
}
res += *(sit++);
}
res += *(sit++);
}

if( !( sit != end))
break;
if( !( sit != end))
break;

string substr;
if( parse_quoted_string( sit, end, substr))
{
res += substr;
}
else
{
res = "";
ycp2error( "Unquoting error: missing closing quote. Unquoted value: <%s>.", quoted_string.c_str());
break;
res += parse_quoted_string( sit, end);
sit++;
}
sit++;
}
catch( invalid_argument &ia)
{
res = "";
ycp2error( "Unquoting error: %s Unquoted value: <%s>.", ia.what(), quoted_string.c_str());
}

return res;
}
2 changes: 1 addition & 1 deletion agent-ini/testsuite/tests/shellquotes.err
@@ -1 +1 @@
[Interpreter] tests/shellquotes.ycp:XXX Unquoting error: missing closing quote. Unquoted value: <"invalid - unclosed>.
[Interpreter] tests/shellquotes.ycp:XXX Unquoting error: Missing enclosing dquote. Unquoted value: <"invalid - unclosed>.

0 comments on commit 9ada24c

Please sign in to comment.