Parser.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:6k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
- // Copyright 1996 by Jarle Aase. All rights reserved.
- // See the "War Software Series Licende Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : Parser.cpp
- // PURPOSE : Parsing of single line commands
- // PROGRAM :
- // DATE : Sept. 26 1996
- // AUTHOR : Jarle Aase
- // ---
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include "WarSoftware.h"
- #include "..IncludeParser.h"
- enum // Token ID's
- {
- TOK_QUIT = TOK_START_TAB, TOK_HELP
- };
- // Command tokens
- static STokenTab BaseTokens[] =
- {
- {TOK_QUIT, "QUIT" },
- {TOK_HELP, "HELP" },
- {TOK_INVALID, "" }
- };
- enum // Commands recognized on this level
- {
- CMD_QUIT = CMD_START_TREE, CMD_HELP
- };
- // Command tree
- static SCommandTree BaseCmds[] =
- {
- {TOK_QUIT, CMD_QUIT, 0, 0, NULL, ' ', LEX_WANT_CMD, NULL},
- {TOK_HELP, CMD_HELP, 0, 0, NULL, ' ', LEX_WANT_CMD, NULL},
- {TOK_INVALID, CMD_INVALID, 0, NULL, 0, 0}
- };
- CCommandParser::CCommandParser()
- {
- m_Commands = (SCommandTree *) BaseTokens;
- m_Tokens = (STokenTab *) BaseTokens;
- }
- int CCommandParser::ParseCmd(LPCSTR CmdLine, CCmdArgs& Args, int Level)
- {
- int CmdID = yyParse(CmdLine, m_Commands, m_Tokens, Args);
- //if (CmdID < CMD_START_TREE)
- //{
- // int TryCmdID = BaseClass::ParseCmd()
- // return max(TryCmdID,CmdID);
- //}
- return CmdID;
- }
- int CCommandParser::yyParse(LPCSTR CmdLine, SCommandTree *CmdPtr, STokenTab *LexTab, CCmdArgs& Args)
- {
- int CmdID = CMD_PARSE_ERROR, TokenID, LexEOT = ' ', LexWant = LEX_WANT_CMD;
- LPCSTR LexCmdLine = CmdLine;
- // Initialize
- int level = 0;
- LPCSTR CmdSave = LexCmdLine;
- again:
- while((TokenID = yyLex(&LexCmdLine, LexTab, LexEOT, LexWant, Args)) != TOK_END_OF_TOKENS)
- {
- if (!CmdPtr->NextLevel && (LexWant != LEX_WANT_CMD))
- {
- if ((LexWant & 0xfff) == TokenID)
- break;
- if ((CmdPtr->CommandID != TOK_INVALID) && ((CmdPtr)->TokenID == (++CmdPtr)->TokenID))
- {
- LexEOT = CmdPtr->LexTokenDelim;
- LexWant = CmdPtr->LexWhatWeWant;
- CmdID = CmdPtr->CommandID;
- LexCmdLine = CmdSave;
- goto again;
- }
- return CMD_UNKNOWN_PARAMETER;
- }
- if (level)
- {
- if (!CmdPtr->NextLevel)
- return CMD_UNKNOWN_PARAMETER;
- CmdPtr = CmdPtr->NextLevel;
- }
- for(;LexWant == LEX_WANT_CMD;)
- {
- if (CmdPtr->TokenID == TokenID)
- break;
- ++CmdPtr;
- if (CmdPtr->TokenID == TOK_INVALID)
- return level ? CMD_UNKNOWN_PARAMETER : CMD_UNKNOWN_COMMAND;
- }
- LexEOT = CmdPtr->LexTokenDelim;
- LexWant = CmdPtr->LexWhatWeWant;
- CmdID = CmdPtr->CommandID;
- ++level;
- CmdSave = LexCmdLine;
- if (CmdPtr->Flags & CMDF_PARAMETER)
- {
- if (!*LexCmdLine)
- return CMD_MISSING_PARAMETER;
- }
- if (CmdPtr->Flags & CMDF_NOPARAM)
- {
- if (*LexCmdLine)
- return CMD_UNKNOWN_PARAMETER;
- }
- if (CmdPtr->Flags & CMDF_NOT_IMPLEMENTED)
- return CMD_NOT_IMPLEMENTED;
- }
- return CmdID;
- }
- int CCommandParser::yyLex(LPCSTR *CmdLine, STokenTab *LexTab, int LexEOT, int Want, CCmdArgs& Args)
- {
- LPCSTR TokenStart = *CmdLine;
- LPCSTR Token = *CmdLine;
- int Len = 0, Digits = 0;
- char buf[16]; // To store numerical values in args
- int LexEOTsave = LexEOT;
- BOOL ParseQuotes = FALSE;
- BOOL IsParsingQuotes = FALSE;
- if (Want & LEX_PARSE_QUOTES)
- {
- Want &= ~LEX_PARSE_QUOTES;
- ParseQuotes = TRUE;
- }
- // Find the token (like strtok())
- while(*Token && (*Token != LexEOT))
- {
- if (Want == LEX_WANT_STRING)
- {
- if (ParseQuotes && (*Token == '"'))
- {
- if (!Len)
- {
- // Intro " ..
- LexEOT = 0;
- IsParsingQuotes = TRUE;
- }
- else if (IsParsingQuotes)
- {
- // Trailing " ..
- ++Token;
- ++Len;
- LexEOT = LexEOTsave;
- break;
- }
- }
- }
- if (((Token == *CmdLine) && (*Token == '-')) || isdigit(*Token))
- ++Digits;
- ++Len;
- ++Token;
- }
- // Skip to next token
- while(*Token && (*Token == LexEOT))
- Token++;
- // Set the restart marker to next token
- *CmdLine = Token;
- if (!Len)
- return TOK_END_OF_TOKENS;
- // Now, let's look at what we've got...
- switch(Want)
- {
- case LEX_WANT_CMD:
- while(LexTab->TokenID != TOK_INVALID)
- {
- if ((strlen(LexTab->Token) == (size_t)Len) && !strnicmp(LexTab->Token, TokenStart, Len))
- break;
- ++LexTab;
- }
- Args.AddArg(TokenStart, Len);
- return LexTab->TokenID;
- break;
- case LEX_WANT_STRING:
- // We have something, so let's just say it's a string...
- if (IsParsingQuotes)
- Args.AddArg(TokenStart + 1, Len - 2);
- else
- Args.AddArg(TokenStart, Len);
- return TOK_STRING;
- break;
- case LEX_WANT_BOOL:
- if (!strnicmp("TRUE", TokenStart, 4))
- {
- Args.AddArg(itoa(1,buf,10), Len);
- return TOK_BOOL;
- }
- if (!strnicmp("FALSE", TokenStart, 5))
- {
- Args.AddArg(itoa(0,buf,10), Len);
- return TOK_BOOL;
- }
- break;
- case LEX_WANT_INT:
- if (Digits == Len)
- {
- Args.AddArg(TokenStart, Len);
- return TOK_INT;
- }
- //return TOK_STRING;
- break;
- case LEX_WANT_CHAR:
- if (Len == 1)
- {
- Args.AddArg(TokenStart,1);
- return TOK_CHAR;
- }
- }
- return TOK_INVALID;
- }