| 1 | #include "machdefs.h"
 | 
|---|
| 2 | #include <stdio.h>
 | 
|---|
| 3 | #include <stdlib.h>
 | 
|---|
| 4 | #include <math.h>
 | 
|---|
| 5 | #include <iostream>
 | 
|---|
| 6 | #include <fstream>
 | 
|---|
| 7 | #include "sophyainit.h"
 | 
|---|
| 8 | #include "timing.h"
 | 
|---|
| 9 | #include "commander.h"
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | class TCmdExecutor : public CmdExecutor {
 | 
|---|
| 13 | public:
 | 
|---|
| 14 |   TCmdExecutor(Commander& cmd);
 | 
|---|
| 15 |   virtual ~TCmdExecutor() { }
 | 
|---|
| 16 | 
 | 
|---|
| 17 |   virtual int Execute(string& keyw,vector<string>& args, string& toks);
 | 
|---|
| 18 | };
 | 
|---|
| 19 | 
 | 
|---|
| 20 | TCmdExecutor::TCmdExecutor(Commander& cmd)
 | 
|---|
| 21 | { 
 | 
|---|
| 22 | string hgrp = "TCmdExecutor";
 | 
|---|
| 23 | string usage,kw;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | kw = "ls";
 | 
|---|
| 26 | usage = "ls: Execute /usr/bin/ls \n";
 | 
|---|
| 27 | cmd.RegisterCommand(kw, usage, this, hgrp);
 | 
|---|
| 28 | kw = "mv";
 | 
|---|
| 29 | usage = "mv: Execute /usr/bin/mv \n";
 | 
|---|
| 30 | cmd.RegisterCommand(kw, usage, this, hgrp);
 | 
|---|
| 31 | kw = "cp";
 | 
|---|
| 32 | usage = "cp: Execute /usr/bin/mv \n";
 | 
|---|
| 33 | cmd.RegisterCommand(kw, usage, this, hgrp);
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | int TCmdExecutor::Execute(string& kw, vector<string>& args, string& toks)
 | 
|---|
| 37 | {
 | 
|---|
| 38 |   if ((kw!="ls")&&(kw!="mv")&&(kw!="cp"))  return 99; 
 | 
|---|
| 39 |   string cmd;
 | 
|---|
| 40 |   if (kw == "ls")     cmd = "/usr/bin/ls " ; 
 | 
|---|
| 41 |   else if (kw == "mv") cmd = "/usr/bin/mv " ;
 | 
|---|
| 42 |   else if (kw == "cp") cmd = "/usr/bin/cp " ;
 | 
|---|
| 43 |   else cmd = "/usr/bin/echo " ;
 | 
|---|
| 44 |   cout << "TCmdExecutor::Execute() : Executing " << cmd << endl;
 | 
|---|
| 45 |   return system(cmd.c_str());
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | int  InputLoop(Commander & cmd);
 | 
|---|
| 49 | 
 | 
|---|
| 50 | /* --Main-- */
 | 
|---|
| 51 | int main(int narg, char *arg[])
 | 
|---|
| 52 | {
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   SophyaInit();
 | 
|---|
| 55 |   InitTim();
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   try {
 | 
|---|
| 58 |     Commander cmd;
 | 
|---|
| 59 |     TCmdExecutor cmdex(cmd);
 | 
|---|
| 60 |     InputLoop(cmd);
 | 
|---|
| 61 |   } 
 | 
|---|
| 62 |  
 | 
|---|
| 63 |   catch (PThrowable & exc) {
 | 
|---|
| 64 |     cerr << " Catched Exception " << (string)typeid(exc).name()
 | 
|---|
| 65 |          << " - Msg= " << exc.Msg() << endl;
 | 
|---|
| 66 |   }
 | 
|---|
| 67 |   catch (...) {
 | 
|---|
| 68 |     cerr << " some other exception was caught ! " << endl;
 | 
|---|
| 69 |   }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   PrtTim(" End of tcmd ");
 | 
|---|
| 72 |   return(0);
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | /* --Fonction-- */
 | 
|---|
| 76 | int  InputLoop(Commander & cmd)
 | 
|---|
| 77 | {
 | 
|---|
| 78 |   cout << " tcmd/InputLoop() : Type in your commands, \n" 
 | 
|---|
| 79 |        << "     end with a blanck line OR <Cntl>D " << endl;
 | 
|---|
| 80 |   int line = 0;
 | 
|---|
| 81 |   bool fg = true;
 | 
|---|
| 82 |   char buff[1024];
 | 
|---|
| 83 |   char * ret;
 | 
|---|
| 84 |   while (fg) {
 | 
|---|
| 85 |     printf("%d-%s ", line+1, cmd.GetCurrentPrompt().c_str());
 | 
|---|
| 86 |     fflush(stdout);
 | 
|---|
| 87 |     buff[0] = '\0';
 | 
|---|
| 88 |     ret = fgets(buff, 1024, stdin);
 | 
|---|
| 89 |     buff[1023] = '\0';
 | 
|---|
| 90 |     if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) { 
 | 
|---|
| 91 |       //      buff[strlen(buff)-1] = '\0';
 | 
|---|
| 92 |       string cline = buff;
 | 
|---|
| 93 |       cmd.Interpret(cline);
 | 
|---|
| 94 |       line++;
 | 
|---|
| 95 |     }
 | 
|---|
| 96 |     else fg = false;
 | 
|---|
| 97 |   }
 | 
|---|
| 98 |   cout << " \n Total " << line << " lines from stdin -> Commander " << endl;
 | 
|---|
| 99 |   if (line > 0) return(0);
 | 
|---|
| 100 |   else return(1);
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | 
 | 
|---|