libbbb  1.2.1
Groups common code used in some applications and libraries.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
rexscript.h
Go to the documentation of this file.
1 /* ========================================================================= */
2 /* ------------------------------------------------------------------------- *//*
12 
13 
14  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15  Please read COPYING and README files in root folder
16  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 */
18 /* ------------------------------------------------------------------------- */
19 /* ========================================================================= */
20 #ifndef __REXSCRIPT_INC__
21 #define __REXSCRIPT_INC__
22 //
23 //
24 //
25 //
26 /* INCLUDES ------------------------------------------------------------ */
27 
28 #include <libbbb/libbbb.h>
29 #include <libbbb/rexscriptblock.h>
30 #include <libbbb/varmap.h>
31 #include <libbbb/rexscript-predefined-commands.h>
32 #include <libbbb/list2d.h>
33 
34 /* INCLUDES ============================================================ */
35 //
36 //
37 //
38 //
39 /* DEFINITIONS --------------------------------------------------------- */
40 
41 #if (!defined Q_OS_MSDOS) && (!defined Q_OS_WIN32) && (!defined Q_OS_WINCE)
42 #define ASCII_EC_BLACK "\x1b[30m"
43 #define ASCII_EC_RED "\x1b[31m"
44 #define ASCII_EC_GREEN "\x1b[32m"
45 #define ASCII_EC_GREY "\x1b[30;1m"
46 #define ASCII_EC_CLEAR "\x1b[0m"
47 #else
48 #define ASCII_EC_BLACK ""
49 #define ASCII_EC_RED ""
50 #define ASCII_EC_GREEN ""
51 #define ASCII_EC_GREY ""
52 #define ASCII_EC_CLEAR ""
53 #endif
54 
55 
56 namespace bbb {
57 
58 /* DEFINITIONS ========================================================= */
59 //
60 //
61 //
62 //
63 /* CLASS --------------------------------------------------------------- */
64 
68 class
69  BBBSHARED_EXPORT
70  RexScript : public QObject {
71  Q_OBJECT
72 
73  //
74  //
75  //
76  //
77  /* DEFINITIONS ----------------------------------------------------- */
78 
79 public:
80 
81 
82  class CmdEntry;
83 
85  enum LogTypes {
86  LOG_DEBUG = 0x00000,
87  LOG_FILES = 0x00001,
88  LOG_COMMANDS = 0x00004,
89  /* bits in 0x00FF reserved for this class */
90 
91  LOG_ALL =
92  LOG_FILES |
93  LOG_COMMANDS |
94  0
95  };
96 
98  typedef bool (*cmdRoutine) ( RexScript * rexs, const CmdEntry & cmd );
99 
101  struct CmdEntry {
102  QRegExp rex_;
103  cmdRoutine call_;
104  quint64 id_;
105  };
106 
108  struct CmdFile {
109  QString s_f_path_;
110  QStringList sl_lines_;
111  };
112 
115  LINE_START = -1,
116  LINE_END = -2
117  };
118 
120  struct CallStackEntry {
121  int idx_file_;
122  int line_;
123  };
124 
126 # define REX_VARIABLE_MARKER_START "\\$\\{"
127 
128 # define REX_VARIABLE_MARKER_END "\\}"
129 
131 # define VARIABLE_MARKER_START "${"
132 
133 # define VARIABLE_MARKER_END "}"
134 
136 # define REX_VARIABLE_2_MARKER_START "\\$\\$\\{\\{"
137 
138 # define REX_VARIABLE_2_MARKER_END "\\}\\}"
139 
141 # define VARIABLE_2_MARKER_START "$${{"
142 
143 # define VARIABLE_2_MARKER_END "}}"
144 
145 
147  typedef void (*EndOfRunCallback) ( RexScript * rex );
148 
149 
151  enum EndOfRun {
155  CallNoOne
156  };
157 
158 
160 # define VGLOB_CRTPATH "CRTPATH"
161 
162  /* DEFINITIONS ===================================================== */
163  //
164  //
165  //
166  //
167  /* DATA ------------------------------------------------------------ */
168 
169 private:
170 
172  QStringList init_files_;
173 
175  QVector<CallStackEntry> call_stack_;
176 
178  List2Dh block_stack_;
179 
181  QVector<CmdFile> files_;
182 
183 
185  int file_top_;
186 
188  int t_id_;
189 
191  int freq_;
192 
194  int log_types_;
195 
197  QVector<CmdEntry> commands_;
198 
200  int ret_val_;
201 
203  bool b_last_was_command;
204 
206  EndOfRun eor_;
207 
209  EndOfRunCallback eor_kb_;
210 
211 
212 
213 public:
214 
217 
218 
219  /* DATA ============================================================ */
220  //
221  //
222  //
223  //
224  /* FUNCTIONS ------------------------------------------------------- */
225 
226 public:
227 
228 
232  RexScript (
233  const QStringList & init_files,
234  const VarMap & var_map = VarMap()
235  );
236 
240  virtual ~RexScript ( void );
241 
243  bool start ( void );
244 
246  bool stop ( void );
247 
248 private:
249 
251  void end ( void );
252 
253 public:
254 
256  const QStringList & initFiles ( void ) const
257  { return init_files_; }
258 
260  QString crtFile ( void );
261 
263  QString crtRawLine ( void );
264 
266  RexScriptBlock *crtBlock ( void );
267 
269  QString rawLine ( int i_file, int i_line );
270 
272  void setCurrentLine ( int i ) {
273  call_stack_.last().line_ = i;
274  }
275 
277  const RexScript::CallStackEntry & currentStackEntry ( void )
278  {
279  Q_ASSERT( call_stack_.isEmpty() == false );
280  return call_stack_.last();
281  }
282 
284  bool isStackEmpty ( void ) const
285  { return call_stack_.isEmpty(); }
286 
288  bool stackSize ( void ) const
289  { return call_stack_.count(); }
290 
292  bool inBlock ( void )
293  { return ( block_stack_.isEmpty() == false ); }
294 
296  int returnValue ( void ) const
297  { return ret_val_; }
298 
300  bool isRunning ( void ) const
301  { return ( t_id_ > 0 ); }
302 
304  QString tempStoragePath ( void ) const;
305 
307  QString getTempPath ( const QString & s );
308 
310  bool isLogTypeOn ( int id_type ) const
311  { return ( ( log_types_ & id_type ) == id_type ); }
312 
314  EndOfRun typeOfEnd ( void ) const
315  { return eor_; }
316 
318  RexScriptBlock * blockInStack ( int i ) const
319  {
320  Q_ASSERT( i < block_stack_.count( ) );
321  Q_ASSERT( i >= 0 );
322  return static_cast<RexScriptBlock*>( block_stack_.at( i ) );
323  }
324 
325 
328  void setEndQuit ( void )
329  { eor_ = CallQuit; }
330  void setEndCallBack ( EndOfRunCallback kb )
331  { eor_ = CallBack; eor_kb_ = kb; }
332  void setEndSignal ( void )
333  { eor_ = CallSignal; }
334  void setEndQuiet ( void )
335  { eor_ = CallNoOne; }
337 
339  void prependCmd ( const CmdEntry & cmd )
340  { commands_.prepend( cmd ); }
341 
343  void appendCmd ( const CmdEntry & cmd )
344  { commands_.append( cmd ); }
345 
347  CmdEntry & command ( int i ) {
348  return commands_[i];
349  }
350 
352  void setLogTypeOn ( int id_type )
353  { log_types_ = log_types_ | id_type; }
354 
356  void setLogTypeOff ( int id_type )
357  { log_types_ = log_types_ & (~id_type); }
358 
360  bool appendLineToBlock( const QString & s );
361 
363  void fileError ( const QString & s_text );
364 
366  void blockError ( const QString & s_text, int line = -1 );
367 
369  void error ( const QString & s_text );
370 
372  void error (
373  const QString & s_title,
374  const QString & s_text
375  );
376 
378  void errorContin (
379  const QString & s_text
380  );
381 
383  void errorInRexScriptBlock ( void );
384 
386  void error (
387  const QString & s_file,
388  int i_line,
389  const QString & s_line,
390  const QString & s_text
391  );
392 
394  void warning (
395  const QString & s_title,
396  const QString & s_text
397  );
398 
400  void info (
401  const QString & s_text
402  );
403 
405  void log (
406  int ty,
407  const QString & s_text
408  );
409 
411  void logFromFile (
412  int ty,
413  const QString & s_text
414  );
415 
417  void fileDecoration ( void );
418 
420  void blockDecoration ( void );
421 
423  void startBlock ( RexScriptBlock * b );
424 
426  bool endBlock ( void );
427 
428 
430  bool helperOnOff (
431  const QString & s_cmd,
432  const QString & s_val,
433  bool & b_on
434  );
435 
437  void helperLogChange (
438  int ty,
439  bool b_on
440  );
441 
443  virtual bool helperLogType (
444  const QString & s_val,
445  int & ty
446  );
447 
449 
462  virtual QString interpret (
463  const QString & s_in,
464  bool & b_ok,
465  VarMap * vm = NULL
466  );
467 
469 
477  virtual bbb::VarData * getVarData (
478  const QString & s,
479  bool b_create = true
480  );
481 
483  virtual bool delVariable (
484  const QString & s
485  );
486 
488  int getIndex (
489  VarData * vd,
490  const QString & s
491  );
492 
494  QString getPath (
495  const QString & s
496  );
497 
499  bool getExpression(
500  const QString & s,
501  QString & s_res
502  );
503 
504 
505 private:
506 
508  void endProcessing ( void );
509 
510 
511 protected:
512 
513 
517  void timerEvent ( QTimerEvent * event );
518 
519 public:
520 
522  bool processFile ( const QString & s_file );
523 
525  bool processStack ( void );
526 
528  bool processLine ( const QString & s_line );
529 
531  bool processBlock ( RexScriptBlock * blk, const QString & s_line );
532 
534  QString replaceVars ( const QString & s_line );
535 
536 
537 private:
538 
540  void initCommands ( void );
541 
542 
543  /* -------------------------------------------------------------------- */
547 
548 
549  static bool commandNoop ( RexScript * rexs, const CmdEntry & cmd );
550  static bool commandWhites ( RexScript * rexs, const CmdEntry & cmd );
551  static bool commandComments ( RexScript * rexs, const CmdEntry & cmd );
552  static bool commandInclude ( RexScript * rexs, const CmdEntry & cmd );
553  static bool commandFrequency ( RexScript * rexs, const CmdEntry & cmd );
554  static bool commandLog ( RexScript * rexs, const CmdEntry & cmd );
555  static bool commandWait ( RexScript * rexs, const CmdEntry & cmd );
556  static bool commandEcho ( RexScript * rexs, const CmdEntry & cmd );
557  static bool commandBeginLog ( RexScript * rexs, const CmdEntry & cmd );
558  static bool commandBeginEcho ( RexScript * rexs, const CmdEntry & cmd );
559  static bool commandBeginTempFile( RexScript * rexs, const CmdEntry & cmd );
560  static bool commandBeginDir ( RexScript * rexs, const CmdEntry & cmd );
561  static bool commandSet ( RexScript * rexs, const CmdEntry & cmd );
562  static bool commandSetB ( RexScript * rexs, const CmdEntry & cmd );
563  static bool commandSetIdx ( RexScript * rexs, const CmdEntry & cmd );
564  static bool commandAppend ( RexScript * rexs, const CmdEntry & cmd );
565  static bool commandSplit ( RexScript * rexs, const CmdEntry & cmd );
566  static bool commandReadF ( RexScript * rexs, const CmdEntry & cmd );
567  static bool commandWriteF ( RexScript * rexs, const CmdEntry & cmd );
568  static bool commandAppendF ( RexScript * rexs, const CmdEntry & cmd );
569  static bool commandCompute ( RexScript * rexs, const CmdEntry & cmd );
570  static bool commandIf ( RexScript * rexs, const CmdEntry & cmd );
571  static bool commandForEach ( RexScript * rexs, const CmdEntry & cmd );
572  static bool commandFor ( RexScript * rexs, const CmdEntry & cmd );
573  static bool commandWhile ( RexScript * rexs, const CmdEntry & cmd );
574  static bool commandDel ( RexScript * rexs, const CmdEntry & cmd );
575  static bool commandDelF ( RexScript * rexs, const CmdEntry & cmd );
576  static bool commandCopyF ( RexScript * rexs, const CmdEntry & cmd );
577  static bool commandMoveF ( RexScript * rexs, const CmdEntry & cmd );
578  static bool commandGetEnv ( RexScript * rexs, const CmdEntry & cmd );
579  static bool commandSetEnv ( RexScript * rexs, const CmdEntry & cmd );
580 
582  /* ==================================================================== */
583 
584 
585  /* -------------------------------------------------------------------- */
589 
590 
591  static bool blockLog ( RexScript * rexs, const RexSBCallBack * blk );
592  static bool blockEcho ( RexScript * rexs, const RexSBCallBack * blk );
593  static bool blockTempFile ( RexScript * rexs, const RexSBCallBack * blk );
594  static bool blockDir ( RexScript * rexs, const RexSBCallBack * blk );
595  static bool blockSet ( RexScript * rexs, const RexSBCallBack * blk );
596 
598  /* ==================================================================== */
599 
600 signals:
601 
602 
604  void endOfTheRun ( void );
605 
606 
607 
608 
609  /* -------------------------------------------------------------------- */
613 
614 
615 public:
616 
618  static QString toUnixName ( const QString & s );
619 
621  static QString toProperName ( const QString & s );
622 
624 
630  static QString unixToCamelCase ( const QString & s, bool first_upper = true );
631 
633  /* ==================================================================== */
634 
635 
636 
637  /* FUNCTIONS ======================================================= */
638  //
639  //
640  //
641  //
642 
643 }; /* class RexScript */
644 
645 /* CLASS =============================================================== */
646 //
647 //
648 //
649 //
650 
651 } // namespace bbb
652 
653 #endif // __REXSCRIPT_INC__
654 /* ------------------------------------------------------------------------- */
655 /* ========================================================================= */