00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00020 #include<ImLib3D/PluginManagement.hpp>
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <unistd.h>
00024 #include <dirent.h>
00025 #include<ImLib3D/ImageProcessorDescription.hpp>
00026 #include<ImLib3D/Base64.hpp>
00027
00028 vector<string>
00029 PluginManagement::PluginPaths()
00030 {
00031 vector<string> res;
00032 char *cpath=getenv("IMLIB3D_PLUGIN_PATH");
00033 string path=(cpath ? cpath : "");
00034
00035 vector<string> paths;
00036 Split(path, ':', paths);
00037
00038 for(int i=0;i<(int)paths.size();i++)
00039 {
00040 if(paths[i]==""){continue;}
00041 struct stat buf;
00042 if(stat(paths[i].c_str(),&buf)>=0 && S_ISDIR(buf.st_mode))
00043 {
00044 res.push_back(paths[i]);
00045 }
00046 else
00047 {
00048 printf("ignoring invalid entry in IMLIB3D_PLUGIN_PATH path \"%s\"\n",paths[i].c_str());
00049 }
00050 }
00051 return res;
00052 }
00053
00054
00055 PluginManagement::PluginManagement():ImageProcessorsDescription()
00056 {
00057
00058 paths=PluginPaths();
00059
00060
00061 #include"HardcodedImageProcessorsDescription.cpp"
00062 ReadFromString(Base64::Decode(hardcodedDescription));
00063
00064 imlib3dExec=FindFileInPath(paths,"imlib3d");
00065 if(imlib3dExec==""){imlib3dExec="imlib3d";}
00066
00067
00068 pluginNames=FindPlugins();
00069 pluginNames.insert(pluginNames.begin(),imlib3dExec);
00070
00071
00072 for(uint j=0;j<imageProcessors.size();j++)
00073 {
00074 imageProcessorsPluginId[imageProcessors[j].name]=0;
00075 }
00076
00077
00078 for(uint i=1;i<pluginNames.size();i++)
00079 {
00080
00081 string descriptionStr=Command(SPrintf("%s --query-full-xml-description",pluginNames[i]));
00082 if(descriptionStr.substr(0,string("<?xml").size())!="<?xml")
00083 {
00084 ThrowError("PluginManagement description read from plugin \"%s\" does not begin with \"<?xml\" and does not seem to be valid xml");
00085 }
00086 xmlpp::DomParser pluginXmlParser;
00087 pluginXmlParser.parse_memory(descriptionStr);
00088 XIPD::ImageProcessorsDescription pluginDesc
00089 (pluginXmlParser.get_document()->get_root_node());
00090
00091 for(uint j=0;j<pluginDesc.imageProcessors.size();j++)
00092 {
00093 imageProcessorsPluginId[pluginDesc.imageProcessors[j].name]=i;
00094 }
00095
00096 Merge(pluginDesc);
00097 }
00099 Init();
00100 }
00101
00102 vector<string>
00103 PluginManagement::FindPlugins()
00104 {
00105 vector<string> res;
00106
00107 for(uint i=0;i<paths.size();i++)
00108 {
00109 fprintf(stderr,"searching for plugins, considering directory:\"%s\"\n", paths[i].c_str());
00110
00111 vector<string> dirNames;
00112
00113 {
00114 struct dirent **namelist;
00115 int n=scandir(paths[i].c_str(), &namelist, NULL, alphasort);
00116 if(n<0) {perror("scandir");continue;}
00117 while(n--)
00118 {
00119 dirNames.push_back(namelist[n]->d_name);
00120 free(namelist[n]);
00121 }
00122 free(namelist);
00123 }
00124 for(uint j=0;j<dirNames.size();j++)
00125 {
00126 if(dirNames[j]==".." || dirNames[j]=="."){continue;}
00127 string name=paths[i]+"/";
00128 name+=dirNames[j];
00129 string pluginSuffix=".plugin";
00130 if(name.rfind(pluginSuffix)!=name.size()-pluginSuffix.size()){continue;}
00131 mprintf("searching for plugins, considering:\"%s\"\n",name);
00132 struct stat filestat;
00133 if(stat(name.c_str(),&filestat)<0){perror("stat");continue;}
00134 if(!S_ISREG(filestat.st_mode)){continue;}
00135 if(!(filestat.st_mode & 0111)){continue;}
00136
00137 string queryRes=Command(SPrintf("%s --query-imlib3d",name));
00138 if(queryRes.substr(0,string("imlib3d-plugin").size())=="imlib3d-plugin")
00139 {
00140 fprintf(stderr,"Found plugin:\"%s\" ... ok.\n",name.c_str());
00141 res.push_back(name);
00142 }
00143 else
00144 {
00145 printf("invalid plugin response to --query-imlib3d for candidate plugin: \"%s\" ... ignored\n",
00146 name.c_str());
00147 continue;
00148 }
00149 }
00150 }
00151 return res;
00152 }
00153
00154 int
00155 PluginManagement::PluginNumber(const string &name)
00156 {
00157 if(imageProcessorsPluginId.find(name)==imageProcessorsPluginId.end())
00158 {
00159 ThrowError("PluginManagement::PluginNumber Unknown image processor:\"%s\"",name);
00160 }
00161 return imageProcessorsPluginId[name];
00162 }
00163
00164 void
00165 PluginManagement::ExecuteImageProcessor(const vector<string> &args)
00166 {
00167 int id=PluginNumber(args[0]);
00168 mprintf("trying plugin %2d:\"%s\"\n",id,pluginNames[id]);
00169 string cmd=pluginNames[id]+" ";
00170 for(uint j=0;j<args.size();j++){cmd+=args[j]+" ";}
00171 mprintf("trying to execute:\"%s\"\n",cmd);
00172 bool callFailed=system(cmd.c_str());
00173 if(callFailed){printf("Tried for plugin, failed\n");exit(1);}
00174 }
00175
00176