00001 #include "ldapconf_defs.h"
00002 #include "popen.h"
00003 #include "daemoni.h"
00004 #include <strings.h>
00005 00006 00007 00008 00009
00010
00011 00012 00013
00014
00015 const char *get_keyval (VIEWITEMS &items, const char *key)
00016 {
00017 const char *ret = NULL;
00018 VIEWITEM *it = items.locate (key);
00019 if (it != NULL){
00020 const char *pt = it->line.get();
00021 pt = str_skip(pt);
00022 pt += strlen(key);
00023 pt = str_skip (pt);
00024 ret =pt;
00025 }
00026 return ret;
00027 }
00028
00029 const char *get_keyval (VIEWITEMS &items, const char *key, const char *defval)
00030 {
00031 const char *ret = get_keyval (items,key);
00032 if (ret == NULL) ret = defval;
00033 return ret;
00034 }
00035
00036 00037 00038 00039
00040
00041 void set_keyval (VIEWITEMS &items, const char *key, const char *val)
00042 {
00043 VIEWITEM *it = items.locate (key);
00044 if (val[0] == '\0'){
00045 items.remove_del (it);
00046 }else{
00047 if (it == NULL){
00048 it = new VIEWITEM(key);
00049 items.add (it);
00050 }
00051 it->line.setfromf ("%s\t%s",key,val);
00052 }
00053 }
00054
00055
00056
00057 00058 00059
00060 int sys_command_raw (
00061 const char *cmd,
00062 const char *args,
00063 SSTRINGS &tb)
00064 {
00065 int ret = -1;
00066
00067 D(debugf(2,"sys_command: %s\nargs: %s\n",cmd,args));
00068 D(debugf(4,"RESULT BEGIN\n\n"));
00069
00070 POPEN pop (cmd,args);
00071 if (pop.isok()){
00072 SSTRINGS tbout_err;
00073 bool oneerror = false;
00074 while (pop.wait(30) > 0){
00075 char buf[1000];
00076 buf[0] = buf[1] = buf[2] = ' ';
00077 while (pop.readout(buf+3,sizeof(buf)-4)!=-1){
00078 tb.add (new SSTRING(buf));
00079 tbout_err.add (new SSTRING(buf));
00080 D(debugf(4,"%s",buf));
00081 }
00082
00083 buf[0] = buf[1] = '*';
00084 while (pop.readerr(buf+3,sizeof(buf)-4)!=-1){
00085 tbout_err.add (new SSTRING(buf));
00086 oneerror = true;
00087 }
00088 }
00089 ret = pop.getstatus();
00090 if (oneerror){
00091 dialog_textbox ("Error messages",tbout_err);
00092 }
00093 }else{
00094 xconf_error (MSG_U(E_CANTEXEC,"Can't execute the command\n%s"),cmd);
00095 }
00096
00097 D(debugf(4,"\nRESULT END\n") );
00098 return ret;
00099 }
00100
00101 00102 00103
00104
00105 int sys_command (
00106 const char *cmd,
00107 const char *args,
00108 SSTRINGS &tb)
00109 {
00110 int ret = -1;
00111 char title[PATH_MAX+100];
00112 DAEMON_INTERNAL *dae = daemon_find (cmd);
00113 if (dae != NULL){
00114 snprintf (title,sizeof(title)-1
00115 ,"%s: %s %s",MSG_U(I_OUTPUT,"Output of command")
00116 ,dae->getpath(),args);
00117 tb.add (new SSTRING(title));
00118 ret = sys_command_raw (cmd,args,tb);
00119 }
00120 return ret;
00121 }
00122
00123 00124 00125
00126
00127 int sys_showfile (
00128 const char *file,
00129 SSTRINGS &tb)
00130 {
00131 char title[PATH_MAX+100];
00132 snprintf (title,sizeof(title)-1,"%s %s",MSG_U(I_FILE,"File"),file);
00133 tb.add (new SSTRING(title));
00134 return sys_command_raw ("cat",file,tb);
00135 }
00136
00137 00138 00139
00140
00141 void sys_command_title (
00142 const char *title,
00143 const char *cmd,
00144 const char *args)
00145 {
00146 SSTRINGS tb;
00147 if (sys_command (cmd,args,tb) != -1){
00148 dialog_textbox (title,tb);
00149 }
00150 }
00151
00152 00153 00154 00155 00156
00157 int dir_getfiltered(
00158 const char *path,
00159 SSTRINGS &lst,
00160 const char *filter)
00161 {
00162 int ret = 0;
00163 SSTRINGS tb;
00164 SSTRING * lsti=NULL;
00165 str_splitline(filter,' ',tb);
00166 dir_getlist (path,lst);
00167 for (int i=0; i<lst.getnb(); i++){
00168 for (int f=0; f<tb.getnb(); f++){
00169 lsti=lst.getitem(i);
00170 if (lsti && lsti->strstr(tb.getitem(f)->get())){
00171 D(debugf(5,"Directory filter ignore: %s",lsti->get()));
00172 lst.remove(lsti);
00173 ret++;
00174 }
00175 }
00176 }
00177 return ret;
00178 }