diff options
Diffstat (limited to 'src/recursivedescent.cc')
-rw-r--r-- | src/recursivedescent.cc | 174 |
1 files changed, 77 insertions, 97 deletions
diff --git a/src/recursivedescent.cc b/src/recursivedescent.cc index 0f0cf8d..e490865 100644 --- a/src/recursivedescent.cc +++ b/src/recursivedescent.cc @@ -1,9 +1,10 @@ -/** -------------------------------------------------------------------- +/** * @file recursivedescent.cc * @brief Implementation of a recursive descent IMAP command parser. * @author Andreas Aardal Hanssen * @date 2002-2005 - * ----------------------------------------------------------------- **/ + */ + #include "recursivedescent.h" #include "convert.h" @@ -19,13 +20,12 @@ #include <ctype.h> #include <stdio.h> -using namespace ::std; using namespace Binc; +using std::string; -stack<int> Binc::inputBuffer; +std::stack<int> Binc::inputBuffer; int Binc::charnr = 0; -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectThisString(const string &s_in) { Session &session = Session::getInstance(); @@ -53,11 +53,11 @@ Operator::ParseResult Binc::expectThisString(const string &s_in) if (!match) { bincClient.unreadStr(tmp); return Operator::REJECT; - } else + } else { return Operator::ACCEPT; + } } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectDateTime(string &s_in) { Session &session = Session::getInstance(); @@ -102,31 +102,31 @@ Operator::ParseResult Binc::expectDateTime(string &s_in) s_in += "-"; /* month */ - if ((res = expectThisString("Jan")) == Operator::ACCEPT) + if ((res = expectThisString("Jan")) == Operator::ACCEPT) { s_in += "Jan"; - else if ((res = expectThisString("Feb")) == Operator::ACCEPT) + } else if ((res = expectThisString("Feb")) == Operator::ACCEPT) { s_in += "Feb"; - else if ((res = expectThisString("Mar")) == Operator::ACCEPT) + } else if ((res = expectThisString("Mar")) == Operator::ACCEPT) { s_in += "Mar"; - else if ((res = expectThisString("Apr")) == Operator::ACCEPT) + } else if ((res = expectThisString("Apr")) == Operator::ACCEPT) { s_in += "Apr"; - else if ((res = expectThisString("May")) == Operator::ACCEPT) + } else if ((res = expectThisString("May")) == Operator::ACCEPT) { s_in += "May"; - else if ((res = expectThisString("Jun")) == Operator::ACCEPT) + } else if ((res = expectThisString("Jun")) == Operator::ACCEPT) { s_in += "Jun"; - else if ((res = expectThisString("Jul")) == Operator::ACCEPT) + } else if ((res = expectThisString("Jul")) == Operator::ACCEPT) { s_in += "Jul"; - else if ((res = expectThisString("Aug")) == Operator::ACCEPT) + } else if ((res = expectThisString("Aug")) == Operator::ACCEPT) { s_in += "Aug"; - else if ((res = expectThisString("Sep")) == Operator::ACCEPT) + } else if ((res = expectThisString("Sep")) == Operator::ACCEPT) { s_in += "Sep"; - else if ((res = expectThisString("Oct")) == Operator::ACCEPT) + } else if ((res = expectThisString("Oct")) == Operator::ACCEPT) { s_in += "Oct"; - else if ((res = expectThisString("Nov")) == Operator::ACCEPT) + } else if ((res = expectThisString("Nov")) == Operator::ACCEPT) { s_in += "Nov"; - else if ((res = expectThisString("Dec")) == Operator::ACCEPT) + } else if ((res = expectThisString("Dec")) == Operator::ACCEPT) { s_in += "Dec"; - else { + } else { session.setLastError("expected month"); return res; } @@ -204,7 +204,6 @@ Operator::ParseResult Binc::expectDateTime(string &s_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectTime(string &s_in) { Session &session = Session::getInstance(); @@ -279,7 +278,6 @@ Operator::ParseResult Binc::expectTime(string &s_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectZone(string &s_in) { Session &session = Session::getInstance(); @@ -328,7 +326,6 @@ Operator::ParseResult Binc::expectZone(string &s_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectListWildcards(int &c_in) { Operator::ParseResult res; @@ -338,11 +335,11 @@ Operator::ParseResult Binc::expectListWildcards(int &c_in) } else if ((res = expectThisString("*")) == Operator::ACCEPT) { c_in = '*'; return Operator::ACCEPT; - } else + } else { return res; + } } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectListMailbox(string &s_in) { Operator::ParseResult res; @@ -357,7 +354,9 @@ Operator::ParseResult Binc::expectListMailbox(string &s_in) if ((res = expectAtomChar(c)) != Operator::ACCEPT && (res = expectListWildcards(c)) != Operator::ACCEPT && (res = expectThisString("]")) != Operator::ACCEPT) + { return Operator::ACCEPT; + } } while (1); } @@ -366,44 +365,41 @@ Operator::ParseResult Binc::expectListMailbox(string &s_in) return res; } -//---------------------------------------------------------------------- -Operator::ParseResult Binc::expectFlag(vector<string> &v_in) +Operator::ParseResult Binc::expectFlag(std::vector<string> &v_in) { Session &session = Session::getInstance(); Operator::ParseResult res; string flag; - if ((res = expectThisString("\\Answered")) == Operator::ACCEPT) + if ((res = expectThisString("\\Answered")) == Operator::ACCEPT) { v_in.push_back("\\Answered"); - else if ((res = expectThisString("\\Flagged")) == Operator::ACCEPT) + } else if ((res = expectThisString("\\Flagged")) == Operator::ACCEPT) { v_in.push_back("\\Flagged"); - else if ((res = expectThisString("\\Deleted")) == Operator::ACCEPT) + } else if ((res = expectThisString("\\Deleted")) == Operator::ACCEPT) { v_in.push_back("\\Deleted"); - else if ((res = expectThisString("\\Seen")) == Operator::ACCEPT) + } else if ((res = expectThisString("\\Seen")) == Operator::ACCEPT) { v_in.push_back("\\Seen"); - else if ((res = expectThisString("\\Draft")) == Operator::ACCEPT) + } else if ((res = expectThisString("\\Draft")) == Operator::ACCEPT) { v_in.push_back("\\Draft"); - else if ((res = expectThisString("\\Answered")) == Operator::ACCEPT) + } else if ((res = expectThisString("\\Answered")) == Operator::ACCEPT) { v_in.push_back("\\Answered"); - else { - if ((res = expectThisString("\\")) == Operator::ACCEPT) { - if ((res = expectAtom(flag)) == Operator::ACCEPT) - v_in.push_back("\\" + flag); - else { - session.setLastError("expected atom"); - return res; - } - - } else if (expectAtom(flag) == Operator::ACCEPT) { - v_in.push_back(flag); - } else + } else if ((res = expectThisString("\\")) == Operator::ACCEPT) { + if ((res = expectAtom(flag)) == Operator::ACCEPT) { + v_in.push_back("\\" + flag); + } else { + session.setLastError("expected atom"); return res; + } + + } else if (expectAtom(flag) == Operator::ACCEPT) { + v_in.push_back(flag); + } else { + return res; } return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectDate(string &s_in) { Session &session = Session::getInstance(); @@ -437,31 +433,31 @@ Operator::ParseResult Binc::expectDate(string &s_in) s_in += '-'; /* month */ - if ((res = expectThisString("Jan")) == Operator::ACCEPT) + if ((res = expectThisString("Jan")) == Operator::ACCEPT) { s_in += "Jan"; - else if ((res = expectThisString("Feb")) == Operator::ACCEPT) + } else if ((res = expectThisString("Feb")) == Operator::ACCEPT) { s_in += "Feb"; - else if ((res = expectThisString("Mar")) == Operator::ACCEPT) + } else if ((res = expectThisString("Mar")) == Operator::ACCEPT) { s_in += "Mar"; - else if ((res = expectThisString("Apr")) == Operator::ACCEPT) + } else if ((res = expectThisString("Apr")) == Operator::ACCEPT) { s_in += "Apr"; - else if ((res = expectThisString("May")) == Operator::ACCEPT) + } else if ((res = expectThisString("May")) == Operator::ACCEPT) { s_in += "May"; - else if ((res = expectThisString("Jun")) == Operator::ACCEPT) + } else if ((res = expectThisString("Jun")) == Operator::ACCEPT) { s_in += "Jun"; - else if ((res = expectThisString("Jul")) == Operator::ACCEPT) + } else if ((res = expectThisString("Jul")) == Operator::ACCEPT) { s_in += "Jul"; - else if ((res = expectThisString("Aug")) == Operator::ACCEPT) + } else if ((res = expectThisString("Aug")) == Operator::ACCEPT) { s_in += "Aug"; - else if ((res = expectThisString("Sep")) == Operator::ACCEPT) + } else if ((res = expectThisString("Sep")) == Operator::ACCEPT) { s_in += "Sep"; - else if ((res = expectThisString("Oct")) == Operator::ACCEPT) + } else if ((res = expectThisString("Oct")) == Operator::ACCEPT) { s_in += "Oct"; - else if ((res = expectThisString("Nov")) == Operator::ACCEPT) + } else if ((res = expectThisString("Nov")) == Operator::ACCEPT) { s_in += "Nov"; - else if ((res = expectThisString("Dec")) == Operator::ACCEPT) + } else if ((res = expectThisString("Dec")) == Operator::ACCEPT) { s_in += "Dec"; - else { + } else { session.setLastError("expected month"); return res; } @@ -508,16 +504,16 @@ Operator::ParseResult Binc::expectDate(string &s_in) s_in += yearstr.str(); - if (quoted) + if (quoted) { if ((res = expectThisString("\"")) != Operator::ACCEPT) { session.setLastError("expected \""); return res; } + } return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectCRLF(void) { Operator::ParseResult res; @@ -527,7 +523,6 @@ Operator::ParseResult Binc::expectCRLF(void) return res; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectCR(void) { Session &session = Session::getInstance(); @@ -540,15 +535,14 @@ Operator::ParseResult Binc::expectCR(void) return Operator::ERROR; } - if (c == 0x0d) + if (c == 0x0d) { return Operator::ACCEPT; - else { + } else { bincClient.unreadChar(c); return Operator::REJECT; } } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectLF(void) { Session &session = Session::getInstance(); @@ -561,15 +555,14 @@ Operator::ParseResult Binc::expectLF(void) return Operator::ERROR; } - if (c == 0x0a) + if (c == 0x0a) { return Operator::ACCEPT; - else { + } else { bincClient.unreadChar(c); return Operator::REJECT; } } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectTagChar(int &c_in) { Session &session = Session::getInstance(); @@ -680,20 +673,19 @@ Operator::ParseResult Binc::expectTagChar(int &c_in) return Operator::REJECT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectTag(string &s_in) { string tag; int tagchar; int eres = expectTagChar(tagchar); - if (eres == Operator::REJECT) + if (eres == Operator::REJECT) { return Operator::REJECT; - else if (eres == Operator::ERROR) + } else if (eres == Operator::ERROR) { return Operator::ERROR; - else if (eres == Operator::TIMEOUT) + } else if (eres == Operator::TIMEOUT) { return Operator::TIMEOUT; - else { + } else { tag += tagchar; bool done = false; @@ -719,7 +711,6 @@ Operator::ParseResult Binc::expectTag(string &s_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectSPACE(void) { Session &session = Session::getInstance(); @@ -732,21 +723,19 @@ Operator::ParseResult Binc::expectSPACE(void) return Operator::ERROR; } - if (c == ' ') + if (c == ' ') { return Operator::ACCEPT; - else { + } else { bincClient.unreadChar(c); return Operator::REJECT; } } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectMailbox(string &s_in) { return expectAstring(s_in); } -//---------------------------------------------------------------------- // FIXME: This rule is wrong. Operator::ParseResult Binc::expectAstring(string &s_in) { @@ -758,7 +747,6 @@ Operator::ParseResult Binc::expectAstring(string &s_in) return res; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectAtomChar(int &c_in) { Session &session = Session::getInstance(); @@ -869,7 +857,6 @@ Operator::ParseResult Binc::expectAtomChar(int &c_in) return Operator::REJECT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectAtom(string &s_in) { string atom; @@ -882,13 +869,13 @@ Operator::ParseResult Binc::expectAtom(string &s_in) if (atom == "") { bincClient.unreadStr(atom); return res; - } else + } else { s_in = atom; + } return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectString(string &s_in) { Operator::ParseResult res; @@ -899,7 +886,6 @@ Operator::ParseResult Binc::expectString(string &s_in) return res; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectQuoted(string &s_in) { string quoted; @@ -920,7 +906,6 @@ Operator::ParseResult Binc::expectQuoted(string &s_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectQuotedChar(int &c_in) { Session &session = Session::getInstance(); @@ -1085,7 +1070,6 @@ Operator::ParseResult Binc::expectQuotedChar(int &c_in) return Operator::REJECT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectLiteral(string &s_in) { Session &session = Session::getInstance(); @@ -1121,7 +1105,7 @@ Operator::ParseResult Binc::expectLiteral(string &s_in) // Only send the reply if the client did not send a LITERAL+ // request. if (!literalPlus) { - bincClient << "+ ok, send " << nchar << " bytes of data." << endl; + bincClient << "+ ok, send " << nchar << " bytes of data." << std::endl; bincClient.flush(); } @@ -1141,26 +1125,23 @@ Operator::ParseResult Binc::expectLiteral(string &s_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectNumber(unsigned int &i_in) { i_in = 0; unsigned int n; Operator::ParseResult res; - while ((res = expectDigit(n)) == Operator::ACCEPT) { + while ((res = expectDigit(n)) == Operator::ACCEPT) if (i_in == 0) i_in = n; else i_in = (i_in * 10) + n; - } if (res == Operator::TIMEOUT) return res; return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectDigit(unsigned int &i_in) { Session &session = Session::getInstance(); @@ -1176,15 +1157,15 @@ Operator::ParseResult Binc::expectDigit(unsigned int &i_in) if (c == '0') { i_in = 0; return Operator::ACCEPT; - } else + } else { bincClient.unreadChar(c); + } if (expectDigitNZ(i_in) != Operator::ACCEPT) return Operator::REJECT; return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectDigitNZ(unsigned int &i_in) { Session &session = Session::getInstance(); @@ -1238,7 +1219,6 @@ Operator::ParseResult Binc::expectDigitNZ(unsigned int &i_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectSet(SequenceSet &s_in) { Session &session = Session::getInstance(); @@ -1268,16 +1248,16 @@ Operator::ParseResult Binc::expectSet(SequenceSet &s_in) /* if _after_ a set there is a ',', then there will always be * a set after the comma. if not, it's a syntax error. */ - if ((res = expectThisString(",")) == Operator::ACCEPT) + if ((res = expectThisString(",")) == Operator::ACCEPT) { if ((res = expectSet(s_in)) != Operator::ACCEPT) { session.setLastError("expected set"); return res; } + } return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectSequenceNum(unsigned int &i_in) { Session &session = Session::getInstance(); @@ -1293,8 +1273,9 @@ Operator::ParseResult Binc::expectSequenceNum(unsigned int &i_in) if (c == '*') { i_in = (unsigned int)-1; return Operator::ACCEPT; - } else + } else { bincClient.unreadChar(c); + } if (expectNZNumber(i_in) != Operator::ACCEPT) return Operator::REJECT; @@ -1302,7 +1283,6 @@ Operator::ParseResult Binc::expectSequenceNum(unsigned int &i_in) return Operator::ACCEPT; } -//---------------------------------------------------------------------- Operator::ParseResult Binc::expectNZNumber(unsigned int &i_in) { unsigned int c; |