00001
00002
00003
00004
00005
00006
00007
00008 #include <stdio.h>
00009 #include <unistd.h>
00010
00011 #include <ycp/y2log.h>
00012 #include <ycp/Parser.h>
00013 #include <y2/Y2StdioComponent.h>
00014 #include <scr/SCRAgent.h>
00015 #include <scr/SCR.h>
00016
00017
00018 void run_agent_instance (int, char*[], bool, SCRAgent*);
00019
00024 template <class Agent> inline void
00025 run_agent (int argc, char* argv[], bool load_scr)
00026 {
00027
00028 SCRAgent* agent = new Agent ();
00029 if (!agent)
00030 {
00031 fprintf (stderr, "Failed to create Agent\n");
00032 exit (EXIT_FAILURE);
00033 }
00034
00035 run_agent_instance (argc, argv, load_scr, agent);
00036
00037 delete agent;
00038 exit (EXIT_SUCCESS);
00039 }
00040
00044 const char*
00045 process_options (int argc, char* argv[])
00046 {
00047 const char* fname = 0;
00048
00049 if (argc > 1)
00050 {
00051 int argp = 1;
00052 while (argp < argc) {
00053 if ((argv[argp][0] == '-')
00054 && (argv[argp][1] == 'l')
00055 && (argp + 1 < argc)) {
00056 argp++;
00057 set_log_filename (argv[argp]);
00058 } else if ((argv[argp][0] == '-')
00059 && (argv[argp][1] == 'c')
00060 && (argp + 1 < argc)) {
00061 argp++;
00062 set_log_conf (argv[argp]);
00063 } else if (fname == 0) {
00064 fname = argv[argp];
00065 } else {
00066 fprintf (stderr, "Bad argument '%s'\nUsage: %s [name.ycp]\n",
00067 argv[0], argv[argp]);
00068 }
00069 argp++;
00070 }
00071 }
00072
00073 return fname;
00074 }
00075
00076
00077
00078 void
00079 run_agent_instance (int argc, char* argv[], bool load_scr, SCRAgent* agent)
00080 {
00081 const char* fname = process_options (argc, argv);
00082
00083
00084 SCR scr;
00085
00086
00087 Parser* parser = new Parser ();
00088 if (!parser)
00089 {
00090 fprintf (stderr, "Failed to create Parser\n");
00091 exit (EXIT_FAILURE);
00092 }
00093
00094
00095 Y2Component* user_interface = new Y2StdioComponent (false, true);
00096 if (!user_interface)
00097 {
00098 fprintf (stderr, "Failed to create Y2StdioComponent\n");
00099 exit (EXIT_FAILURE);
00100 }
00101
00102
00103 if (fname && load_scr)
00104 {
00105 int len = strlen (fname);
00106 if (len > 5
00107 && strcmp (&fname[len-4], ".ycp") == 0)
00108 {
00109 char* cname = strdup (fname);
00110 strcpy (&cname[len-4], ".scr");
00111 if (access (cname, R_OK) == 0)
00112 {
00113 YCPValue confval = SCRAgent::readconf (cname);
00114 if (confval.isNull ()
00115 || !confval->isTerm ())
00116 {
00117 fprintf (stderr, "Failed to read '%s'\n", cname);
00118 fprintf (stderr, "Read result: %s\n", confval->toString().c_str());
00119 exit (EXIT_FAILURE);
00120 }
00121 YCPTerm term = confval->asTerm();
00122 for (int i = 0; i < term->size (); i++)
00123 {
00124 agent->otherCommand (term->value (i)->asTerm ());
00125 }
00126 }
00127 }
00128 }
00129
00130
00131 FILE* infile = stdin;
00132 if (fname != 0)
00133 {
00134 infile = fopen (fname, "r");
00135 if (infile == 0)
00136 {
00137 fprintf (stderr, "Failed to open '%s'\n", fname);
00138 exit (EXIT_FAILURE);
00139 }
00140 }
00141 else
00142 {
00143 fname = "stdin";
00144 }
00145
00146
00147 parser->setInput (infile, fname);
00148 parser->setBuffered ();
00149 YCodePtr value = 0;
00150 while (true)
00151 {
00152 value = parser->parse ();
00153 if (value == 0)
00154 {
00155 break;
00156 }
00157 YCPValue result = value->evaluate ();
00158 printf ("(%s)\n", result->toString ().c_str ());
00159 fflush (0);
00160 }
00161
00162 if (infile != stdin)
00163 {
00164 fclose (infile);
00165 }
00166 delete user_interface;
00167 delete parser;
00168
00169 }