From a1802176b5d2dcde6cf5165f05398e24f739fdb5 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Fri, 24 Feb 2006 01:57:09 +0000
Subject: [PATCH] added example of interactive command line driver for Gmsh

---
 utils/solvers/c++/Makefile        |  7 +++-
 utils/solvers/c++/interactive.cpp | 62 +++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 utils/solvers/c++/interactive.cpp

diff --git a/utils/solvers/c++/Makefile b/utils/solvers/c++/Makefile
index 2baa56df25..5706f6bdcb 100644
--- a/utils/solvers/c++/Makefile
+++ b/utils/solvers/c++/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.4 2005-01-16 20:41:42 geuzaine Exp $
+# $Id: Makefile,v 1.5 2006-02-24 01:57:09 geuzaine Exp $
 #
 # Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 #
@@ -21,9 +21,14 @@
 
 include ../../../variables
 
+all: solver.exe interactive.exe
+
 solver.exe: solver.cpp
 	${CXX} ${FLAGS} ${OPTIM} -o solver.exe solver.cpp
 
+interactive.exe: interactive.cpp
+	${CXX} ${FLAGS} ${OPTIM} -o interactive.exe interactive.cpp -lreadline
+
 clean:
 	rm -f *.o *.exe *.pos
 
diff --git a/utils/solvers/c++/interactive.cpp b/utils/solvers/c++/interactive.cpp
new file mode 100644
index 0000000000..fc571b7fcb
--- /dev/null
+++ b/utils/solvers/c++/interactive.cpp
@@ -0,0 +1,62 @@
+#include "GmshClient.h"
+#include <readline/readline.h>
+#include <readline/history.h>
+
+// compile with: g++ interact.cpp -lreadline
+
+class GmshInteractiveClient{
+ private:
+  GmshClient _client;
+ public:
+  GmshInteractiveClient()
+  {
+    using_history();
+    char *socket = "/Users/geuzaine/.gmshsock";
+    if(_client.Connect(socket) < 0) {
+      printf("Unable to connect to Gmsh\n");
+      exit(1);
+    }
+    _client.Start();
+  }
+  ~GmshInteractiveClient()
+  {
+    _client.Stop();
+    _client.Disconnect();
+  }
+  void read(char *prompt)
+  {
+    while (1) {
+      // read input char until CR, LF, EOF, ^D
+      char *ptr = readline(prompt);
+      // exit interactive if EOF or ^D
+      if(!ptr) break;
+      // if there is something in the line
+      if(strlen(ptr)){
+	// add the command in the stack
+	add_history(ptr);
+	if(!strcmp(ptr,"q") || !strcmp(ptr, "quit")){
+	  // exit interactive if q, quit, exit
+	  free(ptr);
+	  break;
+	}
+	else if(!strcmp(ptr, "dir") || !strcmp(ptr, "ls")){
+	  // direct system calls
+	  system("ls -al");
+	}
+	else{
+	  // pass any other command to gmsh
+	  _client.ParseString(ptr);
+	}
+      }
+      free(ptr);
+    }
+  }
+};
+
+int main() 
+{
+  GmshInteractiveClient c;
+  c.read("gmsh> ");
+
+  return 0;
+}
-- 
GitLab