Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Examples

run_agent.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 
00003 /*
00004  *  Author: Arvin Schnell <arvin@suse.de>
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 
00022 template <class Agent> inline void
00023 run_agent (int argc, char* argv[], bool load_scr)
00024 {
00025     // create Agent
00026     SCRAgent* agent = new Agent ();
00027     if (!agent)
00028     {
00029         fprintf (stderr, "Failed to create Agent\n");
00030         exit (EXIT_FAILURE);
00031     }
00032 
00033     run_agent_instance (argc, argv, load_scr, agent);
00034 
00035     delete agent;
00036     exit (EXIT_SUCCESS);
00037 }
00038 
00042 const char*
00043 process_options (int argc, char* argv[])
00044 {
00045     const char* fname = 0;
00046 
00047     if (argc > 1)
00048     {
00049         int argp = 1;
00050         while (argp < argc) {
00051             if ((argv[argp][0] == '-')
00052                 && (argv[argp][1] == 'l')
00053                 && (argp + 1 < argc)) {
00054                 argp++;
00055                 set_log_filename (argv[argp]);
00056             } else if ((argv[argp][0] == '-')
00057                 && (argv[argp][1] == 'c')
00058                 && (argp + 1 < argc)) {
00059                 argp++;
00060                 set_log_conf (argv[argp]);
00061             } else if (fname == 0) {
00062                 fname = argv[argp];
00063             } else {
00064                 fprintf (stderr, "Bad argument '%s'\nUsage: %s [name.ycp]\n",
00065                          argv[0], argv[argp]);
00066             }
00067             argp++;
00068         }
00069     }
00070 
00071     return fname;
00072 }
00073 
00074 // alternate entry point, useful for testing eg. ag_ini where
00075 // we need to use the ScriptingAgent and pass its constructor a parameter
00076 void
00077 run_agent_instance (int argc, char* argv[], bool load_scr, SCRAgent* agent)
00078 {
00079     const char* fname = process_options (argc, argv);
00080     
00081     // fill in SCR builtins
00082     SCR scr;
00083 
00084     // create parser
00085     Parser* parser = new Parser ();
00086     if (!parser)
00087     {
00088         fprintf (stderr, "Failed to create Parser\n");
00089         exit (EXIT_FAILURE);
00090     }
00091     
00092     // create stdio as UI component, disable textdomain calls
00093     Y2Component* user_interface = new Y2StdioComponent (false, true);
00094     if (!user_interface)
00095     {
00096         fprintf (stderr, "Failed to create Y2StdioComponent\n");
00097         exit (EXIT_FAILURE);
00098     }
00099 
00100     // load config file (if existing)
00101     if (fname && load_scr)
00102     {
00103         int len = strlen (fname);
00104         if (len > 5
00105             && strcmp (&fname[len-4], ".ycp") == 0)
00106         {
00107             char* cname = strdup (fname);
00108             strcpy (&cname[len-4], ".scr");
00109             if (access (cname, R_OK) == 0)
00110             {
00111                 YCPValue confval = SCRAgent::readconf (cname);
00112                 if (confval.isNull ()
00113                     || !confval->isTerm ())
00114                 {
00115                     fprintf (stderr, "Failed to read '%s'\n", cname);
00116                     fprintf (stderr, "Read result: %s\n", confval->toString().c_str());
00117                     exit (EXIT_FAILURE);
00118                 }
00119                 YCPTerm term = confval->asTerm();
00120                 for (int i = 0; i < term->size (); i++)
00121                 {
00122                     agent->otherCommand (term->value (i)->asTerm ());
00123                 }
00124             }
00125         }
00126     }
00127 
00128     // open ycp script
00129     FILE* infile = stdin;
00130     if (fname != 0)
00131     {
00132         infile = fopen (fname, "r");
00133         if (infile == 0)
00134         {
00135             fprintf (stderr, "Failed to open '%s'\n", fname);
00136             exit (EXIT_FAILURE);
00137         }
00138     }
00139     else
00140     {
00141         fname = "stdin";
00142     }
00143 
00144     // evaluate ycp script
00145     parser->setInput (infile, fname);
00146     parser->setBuffered ();
00147     YCodePtr value = 0;
00148     while (true)
00149     {
00150         value = parser->parse ();                       // error reports show our filename
00151         if (value == 0)
00152         {
00153             break;
00154         }
00155         parser->restoreFilename ();                     // restore callers filename
00156         YCPValue result = value->evaluate ();
00157         printf ("(%s)\n", result->toString ().c_str ());        // send result to caller
00158         fflush (0);
00159         parser->setFilename (fname);                    // set our filename
00160     }
00161 
00162     if (infile != stdin)
00163     {
00164         fclose (infile);
00165     }
00166     delete user_interface;
00167     delete parser;                                      // restores callers filename
00168 
00169 }

Generated on Fri Nov 9 18:15:22 2007 for yast2-core by doxygen 1.3.6