yast2-core
run_agent.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 /*
4  * Author: Arvin Schnell <arvin@suse.de>
5  */
6 
7 
8 #include <stdio.h>
9 #include <unistd.h>
10 
11 #include <ycp/y2log.h>
12 #include <ycp/Parser.h>
13 #include <y2/Y2StdioComponent.h>
14 #include <scr/SCRAgent.h>
15 #include <scr/SCR.h>
16 
17 
18 void run_agent_instance (int, char*[], bool, SCRAgent*);
19 
24 template <class Agent> inline void
25 run_agent (int argc, char* argv[], bool load_scr)
26 {
27  // create Agent
28  SCRAgent* agent = new Agent ();
29  if (!agent)
30  {
31  fprintf (stderr, "Failed to create Agent\n");
32  exit (EXIT_FAILURE);
33  }
34 
35  run_agent_instance (argc, argv, load_scr, agent);
36 
37  delete agent;
38  exit (EXIT_SUCCESS);
39 }
40 
44 const char*
45 process_options (int argc, char* argv[])
46 {
47  const char* fname = 0;
48 
49  if (argc > 1)
50  {
51  int argp = 1;
52  while (argp < argc) {
53  if ((argv[argp][0] == '-')
54  && (argv[argp][1] == 'l')
55  && (argp + 1 < argc)) {
56  argp++;
57  set_log_filename (argv[argp]);
58  } else if ((argv[argp][0] == '-')
59  && (argv[argp][1] == 'c')
60  && (argp + 1 < argc)) {
61  argp++;
62  set_log_conf (argv[argp]);
63  } else if (fname == 0) {
64  fname = argv[argp];
65  } else {
66  fprintf (stderr, "Bad argument '%s'\nUsage: %s [name.ycp]\n",
67  argv[0], argv[argp]);
68  }
69  argp++;
70  }
71  }
72 
73  return fname;
74 }
75 
76 // alternate entry point, useful for testing eg. ag_ini where
77 // we need to use the ScriptingAgent and pass its constructor a parameter
78 void
79 run_agent_instance (int argc, char* argv[], bool load_scr, SCRAgent* agent)
80 {
81  const char* fname = process_options (argc, argv);
82 
83  // fill in SCR builtins
84  SCR scr;
85 
86  // create parser
87  Parser* parser = new Parser ();
88  if (!parser)
89  {
90  fprintf (stderr, "Failed to create Parser\n");
91  exit (EXIT_FAILURE);
92  }
93 
94  // create stdio as UI component, disable textdomain calls
95  Y2Component* user_interface = new Y2StdioComponent (false, true);
96  if (!user_interface)
97  {
98  fprintf (stderr, "Failed to create Y2StdioComponent\n");
99  exit (EXIT_FAILURE);
100  }
101 
102  // load config file (if existing)
103  if (fname && load_scr)
104  {
105  int len = strlen (fname);
106  if (len > 5
107  && strcmp (&fname[len-4], ".ycp") == 0)
108  {
109  char* cname = strdup (fname);
110  strcpy (&cname[len-4], ".scr");
111  if (access (cname, R_OK) == 0)
112  {
113  YCPValue confval = SCRAgent::readconf (cname);
114  if (confval.isNull ()
115  || !confval->isTerm ())
116  {
117  fprintf (stderr, "Failed to read '%s'\n", cname);
118  fprintf (stderr, "Read result: %s\n", confval->toString().c_str());
119  exit (EXIT_FAILURE);
120  }
121  YCPTerm term = confval->asTerm();
122  for (int i = 0; i < term->size (); i++)
123  {
124  agent->otherCommand (term->value (i)->asTerm ());
125  }
126  }
127  }
128  }
129 
130  // open ycp script
131  FILE* infile = stdin;
132  if (fname != 0)
133  {
134  infile = fopen (fname, "r");
135  if (infile == 0)
136  {
137  fprintf (stderr, "Failed to open '%s'\n", fname);
138  exit (EXIT_FAILURE);
139  }
140  }
141  else
142  {
143  fname = "stdin";
144  }
145 
146  // evaluate ycp script
147  parser->setInput (infile, fname);
148  parser->setBuffered ();
149  YCodePtr value = 0;
150  while (true)
151  {
152  value = parser->parse (); // error reports show our filename
153  if (value == 0)
154  {
155  break;
156  }
157  YCPValue result = value->evaluate ();
158  printf ("(%s)\n", result->toString ().c_str ()); // send result to caller
159  fflush (0);
160  }
161 
162  if (infile != stdin)
163  {
164  fclose (infile);
165  }
166  delete user_interface;
167  delete parser; // restores callers filename
168 
169 }
Definition: SCR.h:24
virtual YCPValue otherCommand(const YCPTerm &term)
Definition: SCRAgent.cc:67
static YCPValue readconf(const char *filename)
Definition: SCRAgent.cc:74
void run_agent_instance(int, char *[], bool, SCRAgent *)
Definition: run_agent.h:79
void setInput(FILE *file, const char *filename=0)
Definition: Parser.cc:89
YCodePtr parse(SymbolTable *gTable=0, SymbolTable *lTable=0)
Definition: Parser.cc:156
void setBuffered()
Definition: Parser.cc:119
Interface to a component via stdio.
Definition: Y2StdioComponent.h:36
Communication handle to a YaST2 component.
Definition: Y2Component.h:262
void run_agent(int argc, char *argv[], bool load_scr)
Definition: run_agent.h:25
YCPValue value(int n) const
Definition: YCPTerm.h:191
int size() const
Definition: YCPTerm.h:189
bool isNull() const
Definition: YCPElement.h:347
SuSE Configuration Repository Agent.
Definition: SCRAgent.h:37
void set_log_conf(string confname)
Definition: y2log.cc:494
void set_log_filename(string filename)
Definition: y2log.cc:352
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
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
static Parser * parser
Definition: ycpc.cc:47
const char * process_options(int argc, char *argv[])
Definition: run_agent.h:45
YCP language parser.
Definition: Parser.h:64

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