source: Sophya/trunk/SophyaProg/Tests/tcmd.cc@ 2480

Last change on this file since 2480 was 2480, checked in by ansari, 22 years ago

programme test de fonctionalite PPF, en particulier ecriture de PositionTag (utilisation pour Swap) et I/O PPFNameTag - Reza 9 Dec 2003

File size: 2.4 KB
Line 
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
12class TCmdExecutor : public CmdExecutor {
13public:
14 TCmdExecutor(Commander& cmd);
15 virtual ~TCmdExecutor() { }
16
17 virtual int Execute(string& keyw,vector<string>& args, string& toks);
18};
19
20TCmdExecutor::TCmdExecutor(Commander& cmd)
21{
22string hgrp = "TCmdExecutor";
23string usage,kw;
24
25kw = "ls";
26usage = "ls: Execute /usr/bin/ls \n";
27cmd.RegisterCommand(kw, usage, this, hgrp);
28kw = "mv";
29usage = "mv: Execute /usr/bin/mv \n";
30cmd.RegisterCommand(kw, usage, this, hgrp);
31kw = "cp";
32usage = "cp: Execute /usr/bin/cp \n";
33cmd.RegisterCommand(kw, usage, this, hgrp);
34}
35
36int 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
49int InputLoop(Commander & cmd);
50
51/* --Main-- */
52int 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-- */
77int 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
Note: See TracBrowser for help on using the repository browser.