Module: sems
Branch: master
Commit: 042b073780b1db357b4621bcf6a952c1fce7cf4d
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=042b073780b1db357b4621bcf6a952c1fce7cf4d

Author: Stefan Sayer <[email protected]>
Committer: Stefan Sayer <[email protected]>
Date:   Thu Sep 16 18:29:00 2010 +0200

DSM: sys.popen function to exec external program

example:
  sys.popen($myresult="/bin/ls wav/*");
  logVars(2);

---

 apps/dsm/mods/mod_sys/ModSys.cpp |   47 ++++++++++++++++++++++++++++++++++++-
 apps/dsm/mods/mod_sys/ModSys.h   |    1 +
 doc/dsm/examples/test_popen.dsm  |   17 +++++++++++++
 doc/dsm/mods/Readme.mod_sys.txt  |   10 ++++++++
 4 files changed, 73 insertions(+), 2 deletions(-)

diff --git a/apps/dsm/mods/mod_sys/ModSys.cpp b/apps/dsm/mods/mod_sys/ModSys.cpp
index 3375b63..af67f6f 100644
--- a/apps/dsm/mods/mod_sys/ModSys.cpp
+++ b/apps/dsm/mods/mod_sys/ModSys.cpp
@@ -34,6 +34,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 SC_EXPORT(MOD_CLS_NAME);
 
@@ -45,7 +46,7 @@ MOD_ACTIONEXPORT_BEGIN(MOD_CLS_NAME) {
   DEF_CMD("sys.unlink", SCUnlinkAction);
   DEF_CMD("sys.unlinkArray", SCUnlinkArrayAction);
   DEF_CMD("sys.tmpnam", SCTmpNamAction);
-
+  DEF_CMD("sys.popen", SCPopenAction);
 } MOD_ACTIONEXPORT_END;
 
 MOD_CONDITIONEXPORT_BEGIN(MOD_CLS_NAME) {
@@ -54,7 +55,6 @@ MOD_CONDITIONEXPORT_BEGIN(MOD_CLS_NAME) {
     return new FileExistsCondition(params, false);
   }
 
-  // ahem... missing not? 
   if (cmd == "sys.file_not_exists") {
     return new FileExistsCondition(params, true);
   }
@@ -257,3 +257,46 @@ EXEC_ACTION_START(SCTmpNamAction) {
     sc_sess->SET_ERRNO(DSM_ERRNO_OK);
   }
 } EXEC_ACTION_END;
+
+
+CONST_ACTION_2P(SCPopenAction, '=', false);
+EXEC_ACTION_START(SCPopenAction) {
+  string dst_var = par1;
+  if (dst_var.length() && dst_var[0]=='$')
+    dst_var = dst_var.substr(1);
+
+  string cmd = resolveVars(par2, sess, sc_sess, event_params);
+  
+  DBG("executing '%s' while saving output to $%s\n", 
+      cmd.c_str(), dst_var.c_str());
+
+  char buf[100];
+  string res;
+  FILE* fp = popen(cmd.c_str(), "r");
+  if (fp==NULL) {
+    throw DSMException("sys", "type", "popen", "cause", strerror(errno));
+  }
+
+  size_t rlen;
+
+  while (true) {    
+    rlen = fread(buf, 1, 100, fp);
+    if (rlen < 100) {
+      if (rlen)
+       res += string(buf, rlen);
+      break;
+    }
+
+    res += string(buf, rlen);
+  }
+
+  sc_sess->var[dst_var] = res;
+  
+  int status = pclose(fp);
+  if (status==-1) {
+    throw DSMException("sys", "type", "pclose", "cause", strerror(errno));
+  }
+  sc_sess->var[dst_var+".status"] = int2str(status);
+  DBG("child process returned status %d\n", status);
+
+} EXEC_ACTION_END;
diff --git a/apps/dsm/mods/mod_sys/ModSys.h b/apps/dsm/mods/mod_sys/ModSys.h
index 66e53a2..72412a6 100644
--- a/apps/dsm/mods/mod_sys/ModSys.h
+++ b/apps/dsm/mods/mod_sys/ModSys.h
@@ -39,4 +39,5 @@ DEF_ACTION_2P(SCRenameAction);
 DEF_ACTION_1P(SCUnlinkAction);
 DEF_ACTION_2P(SCUnlinkArrayAction);
 DEF_ACTION_1P(SCTmpNamAction);
+DEF_ACTION_2P(SCPopenAction);
 #endif
diff --git a/doc/dsm/examples/test_popen.dsm b/doc/dsm/examples/test_popen.dsm
new file mode 100644
index 0000000..e3d6df9
--- /dev/null
+++ b/doc/dsm/examples/test_popen.dsm
@@ -0,0 +1,17 @@
+-- test popen
+import(mod_sys);
+import(mod_utils);
+
+initial state START 
+  enter { 
+    log(1,"Got into START state.");
+    sys.popen($myresult="/bin/ls wav/*");
+    logVars(2);
+    utils.splitStringCR($myresult);
+    logVars(2);
+    stop(true);
+  };
+
+transition "error executing" START - exception; test(#type==popen) / 
logParams(1); stop(true) -> END;
+transition "error closing" START - exception; test(#type==pclose) / 
logParams(1); stop(true) -> END;
+state END;
\ No newline at end of file
diff --git a/doc/dsm/mods/Readme.mod_sys.txt b/doc/dsm/mods/Readme.mod_sys.txt
index 557ed0f..5e10c1d 100644
--- a/doc/dsm/mods/Readme.mod_sys.txt
+++ b/doc/dsm/mods/Readme.mod_sys.txt
@@ -7,6 +7,16 @@ Actions:
     Array version of unlink (prefix/filename_0 .. 
prefix/filename_$filename_size) 
  sys.tmpnam(string varname)
 
+
+ sys.popen($var="command")
+   execute a command (using popen) and save result in $var
+   example:
+     sys.popen($myfiles="/bin/ls wav/*");
+   throws exceptions
+    #type=="popen", #cause==reason if fails to exec
+    #type=="pclose", #cause==reason if fails to close pipe
+ 
+
 Conditions: 
  sys.file_exists(string fname)
  sys.file_not_exists(string fname)

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to