File indexing completed on 2024-04-14 14:32:18

0001 //
0002 // C++ Interface: cCmdParser
0003 //
0004 // Description: command parser
0005 //
0006 /*
0007 Copyright 2005-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef CCMDPARSER_H
0024 #define CCMDPARSER_H
0025 
0026 #include <cactionbase.h>
0027 #include <kmuddy_export.h>
0028 
0029 #include <qstringlist.h>
0030 
0031 /**
0032 The command parser. Parses command separators, speed-walk and similar things. Does not expand variables or internal scripting code.
0033 
0034 @author Tomas Mecir
0035 */
0036 class KMUDDY_EXPORT cCmdParser : public cActionBase
0037 {
0038  public:
0039   /** constructor */
0040   cCmdParser (int sess);
0041   /** destructor */
0042   ~cCmdParser () override;
0043   
0044   /** are we parsing commands ? */
0045   bool parsing () { return isparsing; };
0046   /** enable/disable command parsing */
0047   void setParsing (bool value);
0048 
0049   void setCmdSeparatorString (QString str);
0050   void setSpeedWalkString (QString str);
0051   void setMultiCommandString (QString str);
0052   void setNoParseString (QString str);
0053   void setMacroString (QString str);
0054 
0055   void setAllowEmptyWalkStr (bool val);
0056   /** set spaces stripping */
0057   void setStripSpaces (bool strip) { stripSpaces = strip; };
0058   /** backslash (\) expansion yes/no */
0059   void setExpandBackslashes (bool val) { expandbackslashes = val; };
0060   
0061   /** parse the command, return list of expanded commands */
0062   QStringList parse (const QString &command, bool expandAliases = true);
0063   
0064   /** Is this a macro call ? Also splits into name and params. */
0065   bool isMacroCall (const QString &cmd, QString &mname, QString &params);
0066   
0067   /** does this command need to be sent as-is ? */
0068   bool mustSendRaw (const QString &command);
0069   /** remove the raw flag from the command */
0070   QString fixRaw (const QString &command);
0071  protected:
0072   void eventNothingHandler (QString event, int session) override;
0073 
0074   /** split text into commands, using command separators and \n */
0075   QStringList splitIntoCommands (const QString &text);
0076 
0077   QStringList parseCommand (const QString &command, bool expandAliases = true);
0078 
0079   /** mark this command as raw, returning the marked command */
0080   QString markAsRaw (const QString &command);
0081 
0082   /** if this is a speed-walk command, returns length of speed-walk char
0083   or string (it returns 0 if it isn't speedwalk) */
0084   int isSpeedWalkCommand (const QString &command);
0085   /** if the command is speed-walk, this will process it, starting
0086   at position <pos> (computed by isSpeedWalkCommand) */
0087   QStringList expandSpeedWalk (const QString &command, int pos);
0088 
0089   /** handles repeater (multi-command) character. !!! modifies
0090   parameter command if there is a repeater !!!
0091   @param command Where to look for the repeater, repeater sequence is
0092   stripped off the command, if any */
0093   unsigned int repeater (QString &command);
0094 
0095   /** expand backslashes in the command */
0096   void expandBackslashes (QString &command);
0097 
0098   QString separstr, walkstr, multistr, noparsestr, macrostr;
0099   bool isparsing, allowemptywalkstr, stripSpaces, expandbackslashes;
0100 };
0101 
0102 #endif