yast2-core
Y2AgentComponent.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 /*
4  * Author: Arvin Schnell <arvin@suse.de>
5  */
6 
7 
8 #ifndef Y2AgentComponent_h
9 #define Y2AgentComponent_h
10 
11 
12 #include <ycp/y2log.h>
13 #include <ycp/YCPTerm.h>
14 #include <y2/Y2Component.h>
15 
16 #include <ycp/YCPCode.h>
17 #include <ycp/YCPVoid.h>
18 #include <ycp/YCPTerm.h>
19 #include <ycp/YCPPath.h>
20 
21 class SCRAgent;
22 
23 
27 template <class Agent> class Y2AgentComp : public Y2Component
28 {
29 
30 public:
31 
35  Y2AgentComp (const char*);
36 
40  virtual ~Y2AgentComp ();
41 
45  virtual string name () const { return my_name; }
46 
50  virtual YCPValue evaluate (const YCPValue &command);
51 
55  virtual SCRAgent* getSCRAgent ();
56 
57  virtual YCPValue Read (const YCPPath &path);
58 
59 protected:
60 
64  const char* my_name;
65 
69  Agent* agent;
70 
71 };
72 
73 
74 template <class Agent>
76  : my_name (my_name),
77  agent (0)
78 {
79 }
80 
81 
82 template <class Agent>
84 {
85  if (agent)
86  {
87  delete agent;
88  }
89 }
90 
91 
92 template <class Agent> YCPValue
94 {
95  y2debug ("evaluate (%s)", v->toString ().c_str ());
96 
97  if (!agent)
98  getSCRAgent ();
99 
100  y2debug ("Going to evaluate %s", v->toString ().c_str ());
101 
102  YCPValue value = v;
103  if (value->isCode ())
104  {
105  YCodePtr c = v->asCode ()->code ();
106 
107 
108  if ( c->kind () != YCode::yeTerm)
109  {
110  y2milestone ("Evaluating an expression, not SCR builtin");
111  value = value->asCode ()->evaluate ();
112  return value;
113  }
114 
115  value = value->asCode ()->evaluate ();
116  }
117 
118  if (value.isNull () || value->isVoid ())
119  return value;
120 
121  y2debug ("After code evaluation: %s", value->toString ().c_str ());
122 
123  if( value->isTerm () ) {
124  YCPTerm term = value ->asTerm ();
125  string command = term->name ();
126  YCPList args = term->args ();
127 
128  // evaluate the term in native functions
129  if( command == "Read" ) {
130  return getSCRAgent ()-> Read (args->value (0)->asPath (), args->size() > 1 ? args->value (1) : YCPNull ()) ;
131  }
132  else if( command == "Write" ) {
133  return getSCRAgent ()-> Write (args->value (0)->asPath (), args->value (1), args->size () > 2 ? args->value (2) : YCPNull ()) ;
134  }
135  else if( command == "Dir" ) {
136  return getSCRAgent ()-> Dir (args->value (0)->asPath ()) ;
137  }
138  else if( command == "Error" ) {
139  return getSCRAgent ()-> Error (args->value (0)->asPath ()) ;
140  }
141  else if( command == "Execute" ) {
142  y2debug( "Execute, arg size is %d", args->size() );
143  switch( args->size() ) {
144  case 1:
145  return getSCRAgent ()-> Execute (args->value (0)->asPath ()) ;
146  case 2:
147  return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1)) ;
148  default:
149  return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1), args->value (2)) ;
150  }
151  }
152  else {
153  y2debug( "Passing term to otherCommand" );
154  return getSCRAgent ()-> otherCommand (term);
155  }
156  }
157 #if 0
158  if( value->isCode () ) {
159  y2debug( "Passing (evaluated) code to otherCommand" );
160  return getSCRAgent ()-> otherCommand (value->asCode ()->evaluate ()->asTerm ());
161  }
162 #endif
163 
164  y2error( "Unhandled value (%s): %s", value->valuetype_str (), value->toString ().c_str () );
165 
166  return YCPVoid();
167 }
168 
169 
170 template <class Agent> SCRAgent*
172 {
173  if (!agent)
174  {
175  agent = new Agent ();
176  }
177 
178  return agent;
179 }
180 
181 template <class Agent> YCPValue Y2AgentComp<Agent>::Read (const YCPPath &path)
182 {
183  y2error( "Y2AgentComp::Read" );
184  return getSCRAgent()->Read (path);
185 }
186 
187 #endif // Y2AgentComponent_h
virtual YCPValue Read(const YCPPath &path, const YCPValue &arg=YCPNull(), const YCPValue &opt=YCPNull())=0
This is a Modules Agent TODO file $Id path
Definition: agent-modules/doc/TODO.txt:9
const char * my_name
Definition: Y2AgentComponent.h:64
virtual SCRAgent * getSCRAgent()
Definition: Y2AgentComponent.h:171
virtual ~Y2AgentComp()
Definition: Y2AgentComponent.h:83
virtual string name() const
Definition: Y2AgentComponent.h:45
Execute(.run,"cmd")`Execute(.run_output
#define y2milestone(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:110
YCPValue value(int n) const
Definition: YCPList.h:266
Communication handle to a YaST2 component.
Definition: Y2Component.h:262
SuSE Configuration Repository Agent.
Definition: SCRAgent.h:37
Definition: YCode.h:116
string name() const
Definition: YCPTerm.h:185
#define y2error(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:112
Wrapper for YCPListRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPListRep with the arrow operator. See YCPListRep.
Definition: YCPList.h:236
Y2AgentComp(const char *)
Definition: Y2AgentComponent.h:75
virtual YCPValue evaluate(const YCPValue &command)
Definition: Y2AgentComponent.h:93
Wrapper for YCPVoidRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPVoidRep with the arrow operator. See YCPVoidRep.
Definition: YCPVoid.h:75
Wrapper for YCPTermRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPTermRep with the arrow operator. See YCPTermRep.
Definition: YCPTerm.h:177
YCPList args() const
Definition: YCPTerm.h:186
Wrapper for YCPValueRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPValueRep with the arrow operator. See YCPValueRep.
Definition: YCPValue.h:275
Wrapper for YCPPathRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPPathRep with the arrow operator. See YCPPathRep.
Definition: YCPPath.h:175
virtual YCPValue Read(const YCPPath &path)
Definition: Y2AgentComponent.h:181
Definition: Y2AgentComponent.h:27
cmd num Write(.buffer_size_err, num)`Write(.stdin
#define y2debug(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:107
Definition: YCPElement.h:125
Agent * agent
Definition: Y2AgentComponent.h:69
int size() const
Definition: YCPList.h:250

Generated on a sunny day for yast2-core by doxygen 1.8.11