| 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/cp \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 | for(int kk=0; kk<args.size(); kk++)  { cmd += args[kk]; cmd += ' '; } | 
|---|
| 45 | cout << "TCmdExecutor::Execute() : Executing " << cmd << endl; | 
|---|
| 46 | return system(cmd.c_str()); | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | int  InputLoop(Commander & cmd); | 
|---|
| 50 |  | 
|---|
| 51 | /* --Main-- */ | 
|---|
| 52 | int main(int narg, char *arg[]) | 
|---|
| 53 | { | 
|---|
| 54 |  | 
|---|
| 55 | SophyaInit(); | 
|---|
| 56 | InitTim(); | 
|---|
| 57 |  | 
|---|
| 58 | try { | 
|---|
| 59 | Commander cmd; | 
|---|
| 60 | TCmdExecutor cmdex(cmd); | 
|---|
| 61 | InputLoop(cmd); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | catch (PThrowable & exc) { | 
|---|
| 65 | cerr << " Catched Exception " << (string)typeid(exc).name() | 
|---|
| 66 | << " - Msg= " << exc.Msg() << endl; | 
|---|
| 67 | } | 
|---|
| 68 | catch (...) { | 
|---|
| 69 | cerr << " some other exception was caught ! " << endl; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | PrtTim(" End of tcmd "); | 
|---|
| 73 | return(0); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /* --Fonction-- */ | 
|---|
| 77 | int  InputLoop(Commander & cmd) | 
|---|
| 78 | { | 
|---|
| 79 | cout << " tcmd/InputLoop() : Type in your commands, \n" | 
|---|
| 80 | << "     end with a blanck line OR <Cntl>D " << endl; | 
|---|
| 81 | int line = 0; | 
|---|
| 82 | bool fg = true; | 
|---|
| 83 | char buff[1024]; | 
|---|
| 84 | char * ret; | 
|---|
| 85 | while (fg) { | 
|---|
| 86 | printf("%d-%s ", line+1, cmd.GetCurrentPrompt().c_str()); | 
|---|
| 87 | fflush(stdout); | 
|---|
| 88 | buff[0] = '\0'; | 
|---|
| 89 | ret = fgets(buff, 1024, stdin); | 
|---|
| 90 | buff[1023] = '\0'; | 
|---|
| 91 | if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) { | 
|---|
| 92 | buff[strlen(buff)-1] = '\0'; | 
|---|
| 93 | string cline = buff; | 
|---|
| 94 | cmd.Interpret(cline); | 
|---|
| 95 | line++; | 
|---|
| 96 | } | 
|---|
| 97 | else fg = false; | 
|---|
| 98 | } | 
|---|
| 99 | cout << " \n Total " << line << " lines from stdin -> Commander " << endl; | 
|---|
| 100 | if (line > 0) return(0); | 
|---|
| 101 | else return(1); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|