Minsky
RESTService.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2019
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "RESTMinsky.h"
21 #include "cairoItems.h"
22 #include "minsky_epilogue.h"
23 
24 using namespace classdesc;
25 using namespace std;
26 
27 #ifdef READLINE
28 #include <readline/readline.h>
29 #include <readline/history.h>
30 #endif
31 
32 using namespace minsky;
34 
35 namespace minsky
36 {
37  Minsky& minsky()
38  {
39  return rminsky;
40  }
41 
42  LocalMinsky::LocalMinsky(Minsky& minsky) {}
43  LocalMinsky::~LocalMinsky() {}
44 
45  // GUI callback needed only to solve linkage problems
46  void doOneEvent(bool idleTasksOnly) {}
47 }
48 
49 
50 string toREST(string x)
51 {
52  replace(x.begin(),x.end(),'.','/');
53  return '/'+x;
54 }
55 
56 void processBuffer(const string& buffer)
57 {
58  if (buffer[0]=='#') return;
59  if (buffer[0]!='/')
60  {
61  cerr << buffer << "command doesn't starts with /"<<endl;
62  return;
63  }
64  if (buffer=="/list")
65  {
66  for (auto& i: rminsky.registry)
67  cout << toREST(i.first) << endl;
68  return;
69  }
70 
71  try
72  {
73  auto n=buffer.find(' ');
74  json_pack_t jin(json5_parser::mValue::null);
75  string cmd;
76  if (n==string::npos)
77  cmd=buffer;
78  else
79  { // read argument(s)
80  cmd=buffer.substr(0,n);
81  read(buffer.substr(n),jin);
82  }
83  cout<<cmd<<"=>";
84  cmd.erase(0,1); // remove leading '/'
85  replace(cmd.begin(), cmd.end(), '/', '.');
86  write(rminsky.registry.process(cmd, jin)->asBuffer(),cout);
87  cout << endl;
88  rminsky.commandHook(cmd, jin);
89  }
90  catch (const std::exception& ex)
91  {
92  cerr << "Exception: "<<ex.what() << endl;
93  }
94 }
95 
96 int main(int argc, const char* argv[])
97 {
98  const LocalMinsky lm(rminsky);
99  const bool batch=argc>1 && argv[1]==string("-batch");
100 
101 
102  char* c;
103  string buffer;
104 
105 #ifdef READLINE
106  if (batch)
107  while (getline(cin,buffer))
108  processBuffer(buffer);
109  else // interactive, use readline
110  while ((c=readline("cmd>"))!=nullptr)
111  {
112  const string buffer=c;
113  processBuffer(buffer);
114  if (strlen(c)) add_history(c);
115  free(c);
116  }
117 #else
118  while (getline(cin,buffer))
119  processBuffer(buffer);
120 #endif
121 }
void processBuffer(const string &buffer)
Definition: RESTService.cc:56
void doOneEvent(bool idletasksOnly)
checks if any GUI events are waiting, and proces an event if so
Definition: tclmain.cc:161
void commandHook(const std::string &command, const RP &args)
Definition: RESTMinsky.h:32
RESTProcess_t registry
Definition: RESTMinsky.h:29
minsky::Minsky minsky
Definition: pyminsky.cc:28
STL namespace.
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
int main(int argc, const char *argv[])
Definition: RESTService.cc:96
string toREST(string x)
Definition: RESTService.cc:50
RAII set the minsky object to a different one for the current scope.
Definition: minsky.h:551
RESTMinsky rminsky
Definition: RESTService.cc:33