#include #include "ldapconf_defs.h" #include "popen.h" #include "daemoni.h" #include /* Small routines which are not LDAP spesific. */ /* Return value of keyed item, removing the key from the config line */ const char *get_keyval (VIEWITEMS &items, const char *key) { const char *ret = NULL; VIEWITEM *it = items.locate (key); if (it != NULL){ const char *pt = it->line.get(); pt = str_skip(pt); pt += strlen(key); pt = str_skip (pt); ret =pt; } return ret; } const char *get_keyval (VIEWITEMS &items, const char *key, const char *defval) { const char *ret = get_keyval (items,key); if (ret == NULL) ret = defval; return ret; } /* Set key and value in the config line If value is empty, remove key and value, save comment. */ void set_keyval (VIEWITEMS &items, const char *key, const char *val) { VIEWITEM *it = items.locate (key); if (val[0] == '\0'){ items.remove_del (it); }else{ if (it == NULL){ it = new VIEWITEM(key); items.add (it); } it->line.setfromf ("%s\t%s",key,val); } } /* OS functions */ /* Run raw system command */ int sys_command_raw ( const char *cmd, const char *args, SSTRINGS &tb) { int ret = -1; D(debugf(2,"---SYSCOMMAND: %s",cmd)); D(debugf(2,"---PARAMETERS: %s",args)); D(debugf(D_IO,"-->OUTPUT")); //fprintf (stderr,"sys_command :%s:\n%s\n",cmd,args); POPEN pop (cmd,args); if (pop.isok()){ SSTRINGS tbout_err; bool oneerror = false; SSTRING *last = NULL; while (pop.wait(30) > 0){ char buf[1000]; buf[0] = buf[1] = buf[2] = ' '; while (pop.readout(buf+3,sizeof(buf)-4)!=-1){ //fprintf (stderr,"buf=%s\n",buf); if (isspace(buf[3]) && last != NULL){ int len = last->getlen(); last->truncate (len-1); last->append (buf+4); }else{ last = new SSTRING(buf); tb.add (last); } tbout_err.add (new SSTRING(buf)); D(debugf(D_IO,"%s",buf)); } buf[0] = buf[1] = '*'; while (pop.readerr(buf+3,sizeof(buf)-4)!=-1){ tbout_err.add (new SSTRING(buf)); oneerror = true; } } ret = pop.getstatus(); if (oneerror){ dialog_textbox ("Error messages",tbout_err); } }else{ xconf_error (MSG_U(E_CANTEXEC,"Can't execute the command\n%s"),cmd); } D(debugf(D_IO,"<--OUTPUT")); return ret; } /* Run system command */ int sys_command ( const char *cmd, const char *args, SSTRINGS &tb) { int ret = -1; DAEMON_INTERNAL *dae = daemon_find (cmd); if (dae != NULL){ SSTRING title; title.setfromf ("%s: %s %s",MSG_U(I_OUTPUT,"Output of command") ,dae->getpath(),args); tb.add (new SSTRING(title)); ret = sys_command_raw (cmd,args,tb); } return ret; } /* View a system file */ int sys_showfile ( const char *file, SSTRINGS &tb) { char title[PATH_MAX+100]; snprintf (title,sizeof(title)-1,"%s %s",MSG_U(I_FILE,"File"),file); tb.add (new SSTRING(title)); return sys_command_raw ("cat",file,tb); } /* Run system command, and show result dialog */ void sys_command_title ( const char *title, const char *cmd, const char *args) { SSTRINGS tb; if (sys_command (cmd,args,tb) != -1){ dialog_textbox (title,tb); } } /* Get directory listing after filtering it with provided filter */ int dir_getfiltered( const char *path, SSTRINGS &lst, const char *filter) { int ret = 0; SSTRINGS tb; SSTRING * lsti=NULL; str_splitline(filter,' ',tb); // Get filter components dir_getlist (path,lst); // Get directory listing for (int i=0; istrstr(tb.getitem(f)->get())){ D(debugf(5,"Directory filter ignore: %s",lsti->get())); lst.remove(lsti); ret++; } } } return ret; }