Minsky: 3.17.0
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 "variableInstanceList.h"
21 #include "RESTMinsky.h"
22 #include "cairoItems.h"
23 #include "minsky_epilogue.h"
24 
25 using namespace classdesc;
26 using namespace std;
27 
28 #ifdef READLINE
29 #include <readline/readline.h>
30 #include <readline/history.h>
31 #endif
32 
33 using namespace minsky;
35 
36 namespace minsky
37 {
38  Minsky& minsky()
39  {
40  return rminsky;
41  }
42 
43  LocalMinsky::LocalMinsky(Minsky& minsky) {}
44  LocalMinsky::~LocalMinsky() {}
45 
46  // GUI callback needed only to solve linkage problems
47  void doOneEvent(bool idleTasksOnly) {}
48 }
49 
50 
51 string toREST(string x)
52 {
53  replace(x.begin(),x.end(),'.','/');
54  return '/'+x;
55 }
56 
57 void processBuffer(const string& buffer)
58 {
59  if (buffer[0]=='#') return;
60  if (buffer[0]!='/')
61  {
62  cerr << buffer << "command doesn't starts with /"<<endl;
63  return;
64  }
65  if (buffer=="/list")
66  {
67  for (auto& i: rminsky.registry)
68  cout << toREST(i.first) << endl;
69  return;
70  }
71 
72  try
73  {
74  auto n=buffer.find(' ');
75  json_pack_t jin(json5_parser::mValue::null);
76  string cmd;
77  if (n==string::npos)
78  cmd=buffer;
79  else
80  { // read argument(s)
81  cmd=buffer.substr(0,n);
82  read(buffer.substr(n),jin);
83  }
84  cout<<cmd<<"=>";
85  cmd.erase(0,1); // remove leading '/'
86  replace(cmd.begin(), cmd.end(), '/', '.');
87  write(rminsky.registry.process(cmd, jin)->asBuffer(),cout);
88  cout << endl;
89  rminsky.commandHook(cmd, jin);
90  }
91  catch (const std::exception& ex)
92  {
93  cerr << "Exception: "<<ex.what() << endl;
94  }
95 }
96 
97 int main(int argc, const char* argv[])
98 {
99  const LocalMinsky lm(rminsky);
100  const bool batch=argc>1 && argv[1]==string("-batch");
101 
102 
103  char* c;
104  string buffer;
105 
106 #ifdef READLINE
107  if (batch)
108  while (getline(cin,buffer))
109  processBuffer(buffer);
110  else // interactive, use readline
111  while ((c=readline("cmd>"))!=nullptr)
112  {
113  const string buffer=c;
114  processBuffer(buffer);
115  if (strlen(c)) add_history(c);
116  free(c);
117  }
118 #else
119  while (getline(cin,buffer))
120  processBuffer(buffer);
121 #endif
122 }
void processBuffer(const string &buffer)
Definition: RESTService.cc:57
void doOneEvent(bool idleTasksOnly)
checks if any GUI events are waiting, and proces an event if so
Definition: addon.cc:550
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.
int main(int argc, const char *argv[])
Definition: RESTService.cc:97
string toREST(string x)
Definition: RESTService.cc:51
RAII set the minsky object to a different one for the current scope.
Definition: minsky.h:540
RESTMinsky rminsky
Definition: RESTService.cc:34